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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program {
	int[,] dp = new int[50, 2501];
    static void Main(string[] args) {
        new Calc().calc();
        return;
    }

    public class Calc {
        public void calc() {
			int N = ReadLine<int>();
			var a = ReadLine<int>(' ');
			var ave = a.Average();
			int upper = (int)Math.Ceiling(ave), lower = (int)ave;
			int[] sums = new int[2] { 0, 0 };
			foreach (var item in a) {
				sums[0] += (item - upper) * (item - upper);
				sums[1] += (item - lower) * (item - lower);
			}
			Math.Min(sums[0], sums[1]).WL();
			return;
        }
    }
	

    public static T[] ReadLine<T>(params char[] separators) {
        return Console.ReadLine()
            .Split(separators)
            .Where(_ => _.Length > 0)
            .Select(_ => (T)Convert.ChangeType(_, typeof(T)))
            .ToArray();
    }
	public static T ReadLine<T>() {
		return (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
	}
    public static List<T> ReadLines<T>(int N) {
        List<T> rt = new List<T>();
        for (int i = 0; i < N; i++) {
            T item = (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
            rt.Add(item);
        }
        return rt;
    }
}
public static class Utils {
    public static void WL(this object obj) {
        Console.WriteLine(obj);
    }
	public static string Docking<T>(this IEnumerable<T> collection,Func<T,string> filter = null) {
		StringBuilder sb = new StringBuilder();
		foreach (var item in collection) {
			string str = filter != null ? filter(item) : item.ToString();
			sb.Append(str);
		}
		return sb.ToString();
	}
	public static string RangeDocking<T>(this IEnumerable<T> collection,
		int start,int end,Func<T,string> filter = null) {
		if (start > collection.Count()) throw new IndexOutOfRangeException();
		if (end < 0) throw new IndexOutOfRangeException();
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < end; i++) {
			string str = filter != null ? filter(collection.ElementAt(i)) : collection.ElementAt(i).ToString();
			sb.Append(str);
		}
		return sb.ToString();
	}
	public static void WriteLineAll<T>(this IEnumerable<T> collection) {
		foreach (var item in collection) {
			item.WL();
		}
	}
}