atcoder#AGC049F. [AGC049F] Happy Sequence

[AGC049F] Happy Sequence

Score : 24002400 points

Problem Statement

Integer sequences A,BA, B and CC, each of length NN, are given. Snuke is happy if and only if the following condition is met.

  • For every integer xx, $\sum_{1 \leq i \leq N} |A_i-x| \leq \sum_{1 \leq i \leq N} |B_i-x|$ holds.

He decided to change at least 00 elements of AA to become happy. It costs him Ci×(Ait)2C_i \times (A_i-t)^2 to change AiA_i to tt. Here, tt, the value after the change, should be an integer as well.

Find the minimum possible sum of costs in order for Snuke to become happy.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0Ai2×1050 \leq A_i \leq 2 \times 10^5
  • 0Bi2×1050 \leq B_i \leq 2 \times 10^5
  • 1Ci51 \leq C_i \leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BNB_N

C1C_1 C2C_2 \cdots CNC_N

Output

Print the answer.

3
0 1 4
1 2 3
1 3 2
6

After the following operations, the sum of costs is 66.

  • Change A1A_1 to 22. This change costs 1×(02)2=41 \times (0-2)^2=4.
  • Change A3A_3 to 33. This change costs 2×(43)2=22 \times (4-3)^2=2.

After the operations, A=(2,1,3)A=(2,1,3) holds, which makes Snuke happy. It is impossible to achieve the goal with sum of costs less than 66, so the answer is 66.

20
185 89 216 105 56 383 193 161 75 196 322 180 390 15 206 78 275 338 225 167
161 77 294 117 22 382 218 140 57 231 343 160 397 8 264 68 301 349 295 157
3 1 3 5 2 1 3 4 1 4 2 2 2 2 5 1 1 5 4 3
3758
1
0
0
1
0