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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
//using System.Diagnostics;
//using System.Linq;

class inputFile
{
	//変数
	bool b_testMode;
	List<string> list_s_line;
	int i_index;

	//コンストラクタ
	public inputFile()
	{
		//変数初期化
		b_testMode = false;
		list_s_line = new List<string>();
		i_index = 0;

		//ファイル読み込み
		read_file();
	}

	//ファイル読み込み
	void read_file()
	{
		//テストモードの場合のみ
		if (b_testMode == true)
		{
			// StreamReader の新しいインスタンスを生成する
			System.IO.StreamReader cReader =
				new System.IO.StreamReader
				(@"D:\Data\VisualStudio-workspace\paiza\inputFile.txt", Encoding.Default);

			// 読み込みできる文字がなくなるまで繰り返す
			while (cReader.Peek() >= 0)
			{
				// ファイルを 1 行ずつ読み込む
				string s_temp = cReader.ReadLine();
				// 読み込んだものを追加で格納する
				list_s_line.Add(s_temp);
			}

			// cReader を閉じる (正しくは オブジェクトの破棄を保証する を参照)
			cReader.Close();
		}
	}

	//1行ずつ取得
	public string get()
	{
		//テストモードの場合
		if (b_testMode == true)
		{
			if (i_index >= list_s_line.Count)
			{
				return "";
			}

			return list_s_line[i_index++];
		}
		//本番モードの場合
		else
		{
			string s_temp = Console.ReadLine();
			if(s_temp == null || s_temp == "")
			{
				return "";
			}
			else
			{
				return s_temp;
			}
		}
	}
}

static class DeepCopyUtils
{
	public static object DeepCopy(this object target)
	{

		object result;
		BinaryFormatter b = new BinaryFormatter();

		MemoryStream mem = new MemoryStream();

		try
		{
			b.Serialize(mem, target);
			mem.Position = 0;
			result = b.Deserialize(mem);
		}
		finally
		{
			mem.Close();
		}

		return result;

	}
}

class Program
{
	//クラス変数
	static inputFile input;
	static Param PRM;

	//Main
	static void Main(string[] args)
	{
		//ファイル読み込み
		input = new inputFile();

		//データ作成
		PRM = new Param();

		//実行
		PRM.exec();
	}

	//データ作成
	[Serializable]
	class Param
	{
		//==================================================
		int cl_i_numCnt;
		List<int> cl_list_i_num;
		//==================================================
		public Param()
		{
			//string[] a_s = input.get().Split(' ');
			//string s_result = string.Join(" ", cl_list_i_num);
			//==================================================
			cl_i_numCnt = int.Parse(input.get());

			cl_list_i_num = new List<int>();
			string[] a_s = input.get().Split(' ');
			foreach (string in_s in a_s)
			{
				cl_list_i_num.Add(int.Parse(in_s));
			}
			//==================================================
		}
		public void exec()
		{
			//==================================================
			//書き換え値(平均値upper)
			int i_kakikaeNum = get_kakikaeNum();

			//書き換えコスト計算
			int i_kakikaeCost = get_kakikaeCost(i_kakikaeNum);

			//出力
			//えび君が全てを同じ整数に書き換えるのに必要なコストの総和の最小値を出力せよ。
			Console.WriteLine(i_kakikaeCost);
			//==================================================
		}
		//==================================================
		//書き換えコスト計算
		int get_kakikaeCost(int i_kakikaeNum)
		{
			int i_cost = 0;

			foreach(int in_i in cl_list_i_num)
			{
				//整数xを整数yに書き換えるとき、コストが(x−y)2かかります
				i_cost += (int)Math.Pow(in_i - i_kakikaeNum, 2);
			}

			return i_cost;
		}
		//書き換え値(平均値)
		int get_kakikaeNum()
		{
			int i_total = 0;
			foreach(int in_i in cl_list_i_num)
			{
				i_total += in_i;
			}

			int i_kakikaeNum = i_total / cl_i_numCnt;

			return i_kakikaeNum;
		}
		//==================================================
	}
}