View Code of Problem 4065

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a[1001];
    int n;
    cin>>n;
    a[0] = 0;
    for(int i= 1; i<=n; i++){
        cin>>a[i];
    }
    sort(a+1, a+n+1);
    int i = 0, j=n;
    int ans = 0;
    bool f = true;
    while(i<j){
        ans += pow(a[i] - a[j], 2);
        if(f){
            i++;
            f = false;
        }
        else{
            j--;
            f = true;
        }
        
        
    }
    cout<<ans<<endl;
    return 0;
}

Double click to view unformatted code.


Back to problem 4065