Problem:
Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if 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

1≦N≦100

-100≦a_i≦100

Input

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

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

2
4 8

Sample Output 1

8

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

Sample Input 2

3
1 1 3

Sample Output 2

3

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

Sample Input 3

3
4 2 5

Sample Output 3

5

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

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

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

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
class Solve{
    int N;
    int C;
    int[] a;
    int[] b;
    public Solve(){}
    public static int Main(){
        new Solve().Calc();
        return 0;
    }
    void Calc(){
        getinput();
        long[,] pow = new long[401,C+1];
        long[,] powsum = new long[401,C+1];
        long[,] dp = new long[N+1,C+1];
        for(int i=0;i<401;i++){
            for(int j=0;j<C+1;j++){
                if(j == 0){
                    pow[i,j] = 1;
                }
                else{
                    pow[i,j] = (pow[i,j-1]*i)%Define.mod;
                }
            }
        }
        for(int i=0;i<401;i++){
            for(int j=0;j<C+1;j++){
                if(i == 0){
                    powsum[i,j] = pow[i,j]; 
                }
                else{
                    powsum[i,j] = (pow[i,j]+powsum[i-1,j])%Define.mod;
                }
            }
        }
        for(int i=0;i<N+1;i++){
            for(int j=0;j<C+1;j++){
                if(i == 0 && j == 0){
                    dp[i,j] = 1;
                }
                else if(j == 0){
                    dp[i,j] = (b[i-1]-a[i-1]+1)*dp[i-1,j]%Define.mod;
                }
                else if(i == 0){
                    dp[i,j] = 0;
                }
                else{
                    long count = dp[i-1,j]*(b[i-1]-a[i-1]+1)%Define.mod;
                    for(int k=1;k<j+1;k++){
                        count = (count+((powsum[b[i-1],k]-powsum[a[i-1]-1,k])%Define.mod*dp[i-1,j-k]%Define.mod))%Define.mod; 
                    }
                    dp[i,j] = count;
                }
            }
        }
        Console.WriteLine(dp[N,C]);
        Define.DebugPrint(dp);
    }
    void getinput(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(str[0]);
        C = int.Parse(str[1]);
        a = new int[N];
        b = new int[N];
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            a[i] = int.Parse(str[i]);
        }
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            b[i] = int.Parse(str[i]);
        }
    }    
}
public static class Define{
    public const long mod = 1000000000;
    public static void DebugPrint(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
}