Evi has N integers a1, a2,..., aN. His goal is to get N equal integers by transforming some of the integers.
He can transform each integer at most once. Converting an integer x to another integer y costs him (x−y) 2 dollars. Even if ai=aj (i≠j), he must pay the cost of converting them separately.
Find the minimum total cost of achieving the goal.
Restrictions
1≤N≤100
−100≤ai≤100
enter
The input format of standard input is as follows:
Do not
a1 a2. . . An
Output
Print the lowest total cost of achieving Evi's goals.
Sample input
2
4 8
Copy
8
prompt
Converting the two to 6s will cost (4−6)2+(8−6)2=8 USD, which is the lowest cost.
HUSTOJ 2021 licensed GPLv2
Violent enumeration
#include<bits/stdc++.h>
int cnt=0;
using namespace std;
typedef struct stu{
int x,j;
}sss;
int cmp(int a,int b)
{
return a>b;
}
typedef long long ll;
const int maxn=1e3+199;
int a[130];
int k,n;
int sum=0;
int main(){
scanf("%d",&n);
int minn=101,maxx=-101;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
minn=min(minn,a[i]);
maxx=max(maxx,a[i]);
}
int res=1000000;
for(int i=minn;i<=maxx;i++){
int temp=0;
for(int j=1;j<=n;j++)
temp+=( (a[j]-i)*(a[j]-i) );
res=min(res,temp);
}
printf("%d",res);
return 0;
}