C - Be Together Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200200

問題文

NN 個の整数 a1,a2,..,aNa_1,a_2,..,a_N が与えられます。えび君はこれらを書き換えて全て同じ整数にしようとしています。各ai(1iN)a_i (1≦i≦N)は高々一回しか書き換えられません(書き換えなくても良い)。整数xxを整数yyに書き換えるとき、コストが(xy)2(x-y)^2かかります。仮にai=aj(ij)a_i=a_j (i≠j)だとしても、ひとつ分のコストで同時に書き換えることは出来ません(入出力例22 を参照)。えび君が目的を達成するのに必要なコストの総和の最小値を求めてください。

制約

  • 1N1001≦N≦100
  • 100ai100-100≦a_i≦100

入力

入力は以下の形式で標準入力から与えられる。

NN
a1a_1 a2a_2 ... aNa_N

出力

えび君が全てを同じ整数に書き換えるのに必要なコストの総和の最小値を出力せよ。


入力例 1Copy

Copy
2
4 8

出力例 1Copy

Copy
8

全てを66に書き換えると、コストの総和は(46)2+(86)2=8(4-6)^2+(8-6)^2=8となり、これが最小です。


入力例 2Copy

Copy
3
1 1 3

出力例 2Copy

Copy
3

全てを22に書き換えると(12)2+(12)2+(32)2=3(1-2)^2+(1-2)^2+(3-2)^2=3となります。各aia_iごとに書き換えるので、二つの11を一度にコスト(12)2(1-2)^2で書き換えられるわけではないことに注意してください。


入力例 3Copy

Copy
3
4 2 5

出力例 3Copy

Copy
5

44は書き換えずに、2255を共に44に書き換えることで(24)2+(54)2=5(2-4)^2+(5-4)^2=5が達成できて、これが最小です。


入力例 4Copy

Copy
4
-100 -100 -100 -100

出力例 4Copy

Copy
0

何も書き換えなくともえび君は目的を達成しています。よってこの場合コストは00です。

Score : 200200 points

Problem Statement

Evi has NN integers a1,a2,..,aNa_1,a_2,..,a_N. His objective is to have NN equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer xx into another integer yy costs him (xy)2(x-y)^2 dollars. Even if ai=aj(ij)a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

  • 1N1001≦N≦100
  • 100ai100-100≦a_i≦100

Input

The input is given from Standard Input in the following format:

NN
a1a_1 a2a_2 ... aNa_N

Output

Print the minimum total cost to achieve Evi's objective.


Sample Input 1Copy

Copy
2
4 8

Sample Output 1Copy

Copy
8

Transforming the both into 66s will cost (46)2+(86)2=8(4-6)^2+(8-6)^2=8 dollars, which is the minimum.


Sample Input 2Copy

Copy
3
1 1 3

Sample Output 2Copy

Copy
3

Transforming the all into 22s will cost (12)2+(12)2+(32)2=3(1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (12)2(1-2)^2 dollar separately for transforming each of the two 11s.


Sample Input 3Copy

Copy
3
4 2 5

Sample Output 3Copy

Copy
5

Leaving the 44 as it is and transforming the 22 and the 55 into 44s will achieve the total cost of (24)2+(54)2=5(2-4)^2+(5-4)^2=5 dollars, which is the minimum.


Sample Input 4Copy

Copy
4
-100 -100 -100 -100

Sample Output 4Copy

Copy
0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 00.



2025-04-14 (Mon)
15:17:05 +00:00