stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define R register

#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)

#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)

#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)

template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}

template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}

using namespace std;

typedef long long ll;

const char s[2]={'A','B'};

int a,b,c,d,q,k,nb;ll lim;

int calc(int x)

{

	return (a-(b-x+k-1)/k)/k;

}

inline int find(){

    R int l=0,r=b,mid,ans=0;

    while(l<=r){

        mid=(l+r)>>1;

        mid<=calc(mid)?(ans=mid,l=mid+1):r=mid-1;

    }

    return ans;

}

int main(){

//  freopen("testdata.in","r",stdin);

    for(scanf("%d",&q);q;--q){

        scanf("%d%d%d%d",&a,&b,&c,&d),--c;

		k=(max(a,b)+min(a,b))/(min(a,b)+1);

        if(k==1)fp(i,c,d-1)putchar(s[(i+(b>a))&1]);

        else{

            nb=find();

//          printf("qwq %d %d %d %d\n",a,b,k,nb);

            lim=nb+a-(b-nb+k-1)/k+1;

            fp(i,c,d-1)putchar(i<lim?s[i%(k+1)==k]:s[(a+b-i)%(k+1)>0]);

        }

        putchar('\n');

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

int T,k,n,a,b,c,d;

int main(){

    scanf("%d",&T);

    while(T--){

        scanf("%d%d%d%d",&a,&b,&c,&d);n=a+b;

        int l=0,r=n+1,mid,A,B;k=(max(a,b)-1)/(min(a,b)+1)+1;;

        while(l<r){

            mid=(l+r)>>1;

            A=a-mid/(k+1)*k-mid%(k+1);B=b-mid/(k+1);

            if(B<=1ll*A*k)l=mid+1;else r=mid;

        }

        A=a-l/(k+1)*k-l%(k+1);B=b-l/(k+1);r=l+B-A*k+1;

        for(int i=c;i<=min(d,l);i++)putchar(i%(k+1)?'A':'B');

        for(int i=max(c,l+1);i<=d;i++)putchar((i-r)%(k+1)?'B':'A');

        puts("");

    }

    return 0;

}//ONION_CYC QwQ

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define rep(i,n) for ((i)=1;(i)<=(n);(i)++)

#define per(i,n) for ((i)=n;(i)>=(1);(i)--)

using namespace std;

int t,a,b,c,d,k;

int calc(int x)

{

	return (a-(b-x+k-1)/k)/k;

}

int main()

{

	int i;

	scanf("%d",&t);

	while(t--)

	{

		scanf("%d%d%d%d",&a,&b,&c,&d);

		k=(max(a,b)+min(a,b))/(min(a,b)+1);

		if(k==1)

		{

			for(i=c;i<=d;i++)

			{

				putchar('A'+((i&1)^(a>=b)));

			}

			puts("");

			continue;

		}

		int l=0,r=b+1,mid,ans=0;

		while(l<r)

		{

			mid=(l+r)/2;

			if(mid<=calc(mid))

			{

				l=mid+1;

				ans=mid;

			}

			else r=mid;

		}

		int lim=ans+a-(b-ans+k-1)/k+1;

		for(i=c;i<=d;i++)

		{

			if(i<=lim)

			{

				putchar('A'+(i%(k+1)==0));

			}

			else

			{

				putchar('A'+((a+b-i)%(k+1)!=k));

			}

		}

		puts("");

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

int T,k,n,a,b,c,d;

int main(){

    scanf("%d",&T);

    while(T--){

        scanf("%d%d%d%d",&a,&b,&c,&d);n=a+b;

        int l=0,r=n+1,mid,A,B;k=(max(a,b)-1)/(min(a,b)+1)+1;;

        while(l<r){

            mid=(l+r)>>1;

            A=a-mid/(k+1)*k-mid%(k+1);B=b-mid/(k+1);

            if(B<=1ll*A*k)l=mid+1;else r=mid;

        }

        A=a-l/(k+1)*k-l%(k+1);B=b-l/(k+1);r=l+B-A*k+1;

        for(int i=c;i<=min(d,l);i++)putchar(i%(k+1)?'A':'B');

        for(int i=max(c,l+1);i<=d;i++)putchar((i-r)%(k+1)?'B':'A');

        puts("");

    }

    return 0;

}//ONION_CYC QwQ

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int a, b, c, d, len, k;



int cal(int a, int b) {

	if (!a || !b) return a + b;

	if (a < b) swap(a, b);

	return (a - 1) / (b + 1) + 1;

}



bool check(int mid) {

	int l = mid / (k + 1) * k + mid % (k + 1), r = mid / (k + 1) - (mid % (k + 1) == 0);

	if (l > a) return 0;

	return cal(a - l, b - r) <= k;

}



void workleft(int l, int r) {

	for (int i = l; i <= r; i++)

		putchar(i % (k + 1) ? 'A' : 'B');

}



void workright(int l, int r) {

	for (int i = l; i <= r; i++)

		putchar((a + b - i + 1) % (k + 1) ? 'B' : 'A');

}



int main() {

	int T;

	scanf("%d", &T);

	while (T--) {

		scanf("%d%d%d%d", &a, &b, &c, &d);

		len = a + b;

		k = cal(a, b);

		int l = 0, r = len + 1;

		while (l < r) {

			int mid = l + r >> 1;

			if (check(mid)) l = mid + 1;

			else r = mid;

		}

		l--;

		if (d <= l) workleft(c, d);

		else if (c > l) workright(c, d);

		else workleft(c, l), workright(l + 1, d);

		puts("");

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

int T,k,n,a,b,c,d;

int main(){

    scanf("%d",&T);

    while(T--){

        scanf("%d%d%d%d",&a,&b,&c,&d);n=a+b;

        int l=0,r=n+1,mid,A,B;k=(max(a,b)-1)/(min(a,b)+1)+1;;

        while(l<r){

            mid=(l+r)>>1;

            A=a-mid/(k+1)*k-mid%(k+1);B=b-mid/(k+1);

            if(B<=1ll*A*k)l=mid+1;else r=mid;

        }

        A=a-l/(k+1)*k-l%(k+1);B=b-l/(k+1);r=l+B-A*k+1;

        for(int i=c;i<=min(d,l);i++)putchar(i%(k+1)?'A':'B');

        for(int i=max(c,l+1);i<=d;i++)putchar((i-r)%(k+1)?'B':'A');

        puts("");

    }

    return 0;

}//ONION_CYC QwQ

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>



using namespace std;



int main(void) {

    int N;

    cin >> N;



    vector<int> a(N);

    int i, j;

    for (i=0; i<N; i++) {

        cin >> a[i];

    }



    int min_cost=1<<28LL;

    for (i=-100; i<=100; i++) {        

        int cost=0;

        for (j=0; j<N; j++) {

            cost += (a[j]-i)*(a[j]-i);

        }

        if (cost < min_cost) min_cost = cost;

    }

    cout << min_cost << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>



using namespace std;



int main() {

    int N;

    scanf("%d", &N);



    int a[100];

    for (int i=0; i<N; ++i)

        scanf("%d", &a[i]);



    int res=1<<28;

    for (int i=-100; i<=100; ++i) {

        int cur=0;

        for (int j=0; j<N; ++j)

            cur += (a[j]-i)*(a[j]-i);



        if (res > cur)

            res = cur;

    }



    printf("%d\n", res);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

int a[105];

int main(){

    int n;

    scanf("%d", &n);   

    for(int i=1;i<=n;i++) scanf("%d", &a[i]);

    int ans = 1e9;

    for(int j=-100;j<=100;j++){

        int res = 0;

        for(int i=1;i<=n;i++) res += abs(a[i]-j)*abs(a[i]-j);

        ans = min(ans, res);

    }

    printf("%d\n", ans);

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//BISMILLAHIR RAHMANIR RAHIM

//RHYTHM_086_NS_01

//ALAN_IS_ON....



#include <bits/stdc++.h>



#define     boost_              ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define     iter_(i,n)          for (int i = 0; i < int(n); i++)

#define     for_n(i, n)         for (int i = 1; i <= int(n); i++)

#define     pb                  push_back

#define     mp                  make_pair

#define     itr                 ::iterator

#define     s_sort(s)           sort(s.begin(),s.end())

#define     n_sort(a)           sort(a,a+n)

#define     thank_you           return 0

#define     precise_impact      cout<<setprecision(10)<<fixed;



using namespace std;



typedef long long int ll;

typedef double db;

#define MOD 1000000007



ll bigmod(ll a,ll b){

    if(b==0){

        return 1;

    }

    ll tm=bigmod(a,b/2);

    tm=(tm*tm)%MOD;

    if(b%2==1)

        tm=(tm*a)%MOD;

    return tm;



}

void solve();





int main()

{

    boost_;

    precise_impact;

    //freopen("in.txt","r",stdin);

    //freopen("out.txt","w",stdout);

    //Here we go...

    solve();



    thank_you;

}





void solve()

{

    ll n;

    cin>>n;

	ll a[n];

	ll b=0;

	iter_(i,n){

		cin>>a[i];

		b+=a[i];

	}

	b/=n;

	ll res=MOD;

	for (ll i=-1;i<=1;i++) {

		ll car=0;

		iter_(j,n){

			car+=(a[j]-(b+i))*(a[j]-(b + i));

		}

		res=min(res,car);

	}

	cout<<res<<endl;



}




// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
///Bismillahir Rahmanir Rahim

#include "bits/stdc++.h"



#define                           ll                          long long

#define                           int                         ll

#define                           fi                          first

#define                           si                          second

#define                           mp                          make_pair

#define                           pb                          push_back

#define                           pi                          pair<ll,ll>

#define                           clr(x)                      memset(x,0,sizeof(x));

#define                           f(i,l,r)                    for(int i=l;i<=r;i++)

#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)

#define                           done(i)                     cout<<"done = "<<i<<endl;

#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;

#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



using namespace std;



const ll inf=1e18;

const int mod=1e9+7;

const int M=100005;



 main()



{

    fast

    int n;

    cin>>n;

    int a[n+2];

    f(i,1,n)

    {

        cin>>a[i];

    }

    int ses=inf;

    for(int i=-100;i<=100;i++)

    {

        int cost=0;

        f(j,1,n)

        {

            int x=a[j];

            int tmp=(x-i)*(x-i);

            cost+=(tmp);

        }

        ses=min(ses,cost);

    }

    cout<<ses<<endl;

    return 0;



}








































































// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//Bismillahir Rahmanir Rahim

//In the name of Allah the Most Merciful

//Astagfirullahil laji Rabbi Minkulli Jamio

#include<bits/stdc++.h>

using namespace std;

#define int long long int

#define double long double

int i,j,k;

main()

{

    int n;cin>>n;int a[n];

    for(i=0;i<n;i++) cin>>a[i];

    set<int> s;

    int sm=0;

    for(i=0;i<n;i++)

    {

        s.insert(a[i]);

        sm=sm+a[i];

    }

    sort(a,a+n);

    if(s.size()==1) cout<<0<<endl;

    else

    {

        //int k=sm/2;

        int ans1=1e12;

        int k=a[0];

        int ans=0;

        while(k<a[n-1]){

                int ans=0;

        for(i=0;i<n;i++)

        {

            ans=ans+((k-a[i])*(k-a[i]));

        }

        k++;

        //cout<<ans<<endl;

        ans1=min(ans1,ans);

        }

        cout<<ans1<<endl;

    }

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main()

{

    int t=1;

    while(t--)

    {

        int n;

        cin>>n;

        int ans=INT_MAX;

        int a[n+10];

        for(int i=0; i<n; i++) cin>>a[i];

        for(int i=-100; i<=100; i++)

        {

            int tm=0;

            for(int j=0; j<n; j++)

            {

                tm+=(i-a[j])*(i-a[j]);

            }

            ans=min(ans,tm);

        }

        cout<<ans<<endl;

    }

}




// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define LL long long

#define PII pair<int,int>

#define PIL pair<int,LL>

#define PLI pair<LL,int>

#define PIII pair<int,PII>

#define PLL pair<LL,LL>

#define PLII pair<LL,PII>

#define VI vector<int>

#define VVI vector<VI>

#define VL vector<LL>

#define VVL vector<VL>

#define VPII vector<PII>

#define FF first

#define SS second

#define MP make_pair

#define PB push_back

#define all(x) x.begin(),x.end()

#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'

#define mset(a,v) memset(a,v,sizeof(a))

#define setp(x) cout<<fixed<<setprecision(x)

#define EPS 0.00000000001

#define PI acos(-1)

#define loop(i,b,n) for(int i=b;i<n;++i)

#define rev_loop(i,b,n) for(int i=b;i>=n;--i)

using namespace std;



const int MOD = 1e9 + 7;

const LL MX = 1e9;

const LL INF = 1e8;



void print_a(VI v)

{

    if(v.size()) cout<<v[0];

    loop(i,1,v.size()) cout<<' '<<v[i];

    cout<<'\n';

}



VVI init_vvi(int n, int m, int val)

{

    return VVI(n,VI(m,val));

}



VVL init_vvl(int n, int m, LL val)

{

    return VVL(n,VL(m,val));

}



int main()

{

    //ofstream out("output.txt");

    //ifstream in("input.txt");

    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);



    int n;

    cin>>n;

    LL a[n], sq = 0, sum = 0, mn = INF, mx = -INF;

    loop(i,0,n)

    {

        cin>>a[i];

        sum += a[i];

        sq += (a[i] * a[i]);

        mn = min(mn,a[i]);

        mx = max(mx,a[i]);

    }



    LL ans = INT_MAX, cst;

    loop(i,mn,mx+1)

    {

        cst = sq + (n * i * i) - (2 * i * sum);

        ans = min(ans,cst);

    }



    cout<<ans<<'\n';



    /*

    int t,n;

    cin>>t;

    while(t--)

    {

        cin>>n;

        int a[n];

        loop(i,0,n) cin>>a[i];

    }

    */



    /*

    int n;

    cin>>n;

    int a[n];

    loop(i,0,n) cin>>a[i];

    */



    /*

    cout<<ans<<'\n';



    if(f) cout<<"YES\n";

    else cout<<"NO\n";



    if(f) cout<<"Yes\n";

    else cout<<"No\n";

    */



    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

typedef long long ll;

#define pii pair<int,int>

#define ff first

#define ss second

using namespace std;

void fast()

{

ios::sync_with_stdio(false);

cin.tie(NULL); cout.tie(NULL);

}

int main()

{

    ll n,i,j;

    ll a[105],ans1=0,sum=0,temp,ans=100000000;

    cin>>n;

    for(i=0;i<n;i++)

    {

        cin>>a[i];

    }

    for(i=-100;i<=100;i++)

    {

        temp=0;

        for(j=0;j<n;j++)

        {

            temp+=(i-a[j])*(i-a[j]);

        }

        ans=min(ans,temp);

    }

    cout<<ans<<endl;



}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define ll long long

#define ull unsigned long long

#define pii pair<int,int>

#define piii pair<int,pii>

#define pll pair<ll,ll>

#define plii pair<ll,pii>

#define vi vector<int>

#define ff first

#define ss second

#define mp make_pair

#define pb push_back

#define SORT(x) sort(x.begin(), x.end())

#define GSORT(x) sort(x.begin(), x.end(), greater<int>())

#define mx 100001

#define eps 0.00000000001

#define mod 1000000007

#define pi acos(-1)

#define inf 100000000

#define loop(i,b,n) for(int i=b;i<n;++i)

#define rev_loop(i,b,n) for(int i=b;i>=n;--i)

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

int main()

{

    fio;

    int n;

    cin>>n;

    int a1;

    vi v1;

    set<int> s1;

    loop(i,0,n)

    {

        cin>>a1;

        v1.pb(a1);

        s1.insert(a1);

    }

    if(s1.size()==1)

        cout<<"0\n";

    else

    {

        SORT(v1);

        ll k = INT_MAX;

        for(int i=v1[0];i<=v1[n-1];i++)

        {

            ll sum = 0;

            loop(j,0,n)

            {

                int r = abs(v1[j]-i);

                sum += (r*r);

            }

            k = min(k,sum);

        }



        cout<<k<<endl;

    }

    return 0;

}




// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;



#define        optimize           ios::sync_with_stdio(0); cin.tie(0);

#define PI    acos(-1.0)

#define pb push_back

#define YES cout<<"YES"<<endl;

#define NO cout<<"NO"<<endl;

#define RESET(a, b) memset(a, b, sizeof(a))

#define pii pair <int, int>

#define pll pair <ll, ll>

#define setbiti(x) __builtin_popcount(x)

#define setbitll(x) __builtin_popcountll(x)

#define gcd(a, b) __gcd(a, b)



#define        MX        200005

int ara[105];

int main()

{

    optimize

    int n;

    cin>>n;

    int sum = 0;

    int l = 105, r = -105;

    for(int i = 0; i < n; i++){

        cin>>ara[i];

        l = min(ara[i], l);

        r = max(ara[i], r);

    }

    int ans;

    for(int i = l; i <= r; i++){

        int cur = 0;

        for(int j = 0; j < n; j++){

            cur += ((ara[j]-i)*(ara[j]-i));

        }

        if(i == l) ans = cur;

        ans = min(ans, cur);

    }

    cout<<ans<<endl;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



const int N = 2e5+5;



const int mod = 1e9+7;



int n;

int ar[N];



long long get(int x){

    long long cost = 0;

    for(int i = 1; i <= n; i++){

        cost += (x - ar[i]) * (x - ar[i]);

    }

    return cost;

}



int main(){

    cin >> n;

    long long av = 0;

    for(int i = 1; i <= n; i++){

        cin >> ar[i];

        av += ar[i];

    }

    av /= n;

    printf("%lld\n" , min(get(av) , get(av + 1)));

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>



int main(){

	

	int n;

	scanf("%d", &n);

	

	int num[n];

	int high = -101;

	int low = 101;

	

	for(int i = 0; i < n; i++){

		scanf("%d", &num[i]);

		if(high < num[i]){

			high = num[i];

		}

		if(low > num[i]){

			low = num[i];

		}

	}

		

	int bayar = 10000000;

	

	for(int i = low; i <= high; i++){

		int total = 0;

		for(int j = 0; j < n; j++){

			total += pow(i - num[j], 2);

		}

		if(total < bayar){

			bayar = total;

		}

	}

	

	printf("%d\n", bayar);

	

	return 0;

	

}

// Fastest known solution for this problem: