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.



Sample input:
2
4 8


Sample output:
8

Submission status: Runtime Error

Solution code:
process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});
/*
process.on('SIGINT', function(){
    input_stdin_array = input_stdin.split("\n");
    main();
    process.exit();
});
*/
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////


function main() {
	var s = readLine();	
	var N = parseInt(s);
    s = readLine().split(" ");
	for (var i=0; i < N; i++){
		s[i]=parseInt(s[i]);
	}
	
	var ans = 200*200*200+1;
	
	for (var goal = -100; goal <= 100; goal++){
		var cost=0;
		for (var i=0; i < N; i++){
			cost += (s[i]-goal)*(s[i]-goal);
		}
		if (ans > cost){
			ans = cost;
		}
	}
	
	console.log(ans.toString());
}

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
s=sum(a)
ans=round(s/n)
count=0
for i in range(n):
    count+=(a[i]-ans)**2
print(count)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
INF=10**10

def main():
    
    n=int(input())
    array=[int(input()) for i in range(n)]
    
    answer=INF
    
    for a in range(-100,101):
        temp=0
        for b in array:
            temp+=(a-b)**2
        if temp<answer:
            answer=temp
    print(answer)
            

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
INF=10**20

def main():
    
    n=int(input())
    array=[int(input()) for i in range(n)]
    
    answer=INF
    
    for a in range(-100,101):
        temp=0
        for b in array:
            temp+=(a-b)**2
        if temp<answer:
            answer=temp
    print(answer)
            

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
from functools import reduce

n = int(input())
a = list(map(int,input().split()))

end = max(a)
ansAry = []

b = min(a)
while(True):
    suma = []
    for i in range(0,len(a)):
        if b == a[i]:
            pass
        else:
            suma.append((a[i] - b)*(a[i] - b))
    ansAry.append(reduce(lambda x,y:x+y,suma))
    b += 1
    if b == end:
        break

print(min(ansAry))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 13 20:59:33 2016

@author: kt
"""
max=-1000
min=1000
cost=[0]*100
N=input()
i=0
l = map(int, raw_input().split())
for var in l:
    if var>max:
        max=var
    if var<min:
        min=var
        
"""if max-min==1:
        if cost[0]>cost[1]:
            print cost[1]
        else: 
            print cost[1]
    else :
        print cost[0]"""
if max==min:
    print 0
if max-min==1:
    if l.index(max)>l.index(min): 
        print l.count(min)

    else:      
        print l.count(max)
        print max
else :
    for var in range(min,max+1):
        for var2 in l:
           cost[i]+=(var-var2)*(var-var2)
        i+=1
        if i>2:
            if cost[i-2]<cost[i-1] and cost[i-2]<cost[i-3]:
                print cost[i-2]

Submission status: Runtime Error

Solution code:
# include <algorithm>
# include <cassert>
# include <cctype>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iostream>
# include <map>
# include <queue>
# include <set>
# include <sstream>
# include <stack>
# include <string>
# include <vector>
# include <iomanip>

# define rep(i, n) for (int i = 0; i < (int)(n); i++)
# define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;
typedef long long ll;


int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  rep (i, N) cin >> A[i];

  double mean = 0.0;
  rep (i, N) mean += A[i];
  mean /= A.size();
  mean = roundf(mean);

  ll ans = 0;

  rep (i, N) {
    ans += pow(A[i] - mean, 2);
  }

  cout << ans << endl;
}