AtCoder Be Together
//#include<stdio.h>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
int main()
{
fast;
int n;
vii v;
while(cin >> n)
{
v.clear();
int i=0 , x;
for(; i<n; i++)
{
cin >> x;
v.push_back(x);
}
LL mxe = *max_element(v.begin(), v.end()) , mini = 1000000000 ,sum=0 , df=0;
//cout << mxe << "\n";
LL tmini = mini;
int j=0 ;
for(i=-101; i<=100; i++)
{
sum=0;
for(j=0; j<n; j++)
{
df = i > v[j] ? i - v[j] : v[j] - i ;
sum+=(df * df);
}
mini = min(mini, sum);
}
// if(mini == tmini) mini = 0;
cout << mini << "\n";
}
return 0;
}
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
int main()
{
fast;
int n;
vii v;
while(cin >> n)
{
v.clear();
int i=0 , x;
for(; i<n; i++)
{
cin >> x;
v.push_back(x);
}
LL mxe = *max_element(v.begin(), v.end()) , mini = 1000000000 ,sum=0 , df=0;
//cout << mxe << "\n";
LL tmini = mini;
int j=0 ;
for(i=-101; i<=100; i++)
{
sum=0;
for(j=0; j<n; j++)
{
df = i > v[j] ? i - v[j] : v[j] - i ;
sum+=(df * df);
}
mini = min(mini, sum);
}
// if(mini == tmini) mini = 0;
cout << mini << "\n";
}
return 0;
}
Comments
Post a Comment