#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) begin(v), end(v)
#define sz(v) v.size()
#define INF 1e17
#define EPSILON 1e-14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//浮動小数点はなるべく使うな、powも気をつけろ
int main()
{
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A.at(i);
    vector<ll> acc(N+1, 0);
    rep(i, N){
        acc.at(i+1) = acc.at(i) + A.at(i);
    }
    ll ans = 0;
    rep(i, N){
        ans += A.at(i)*A.at(i);
    }
    ans *= N-1;
    rep(i, N-1){
        ans -= 2 * A.at(i) * (acc.at(N)-acc.at(i+1));
    }
    cout << ans << '\n';
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;

int main(){
    int n; cin >> n;
    VI p[3];
    ll a; char c;
    REP(i,n*2){
        cin >> a >> c;
        if(c=='R')
            p[0].push_back(a);
        else if(c=='G')
            p[1].push_back(a);
        else
            p[2].push_back(a);
    }
    if(p[0].size()%2==0&&p[1].size()%2==0&&p[2].size()%2==0){
        cout << 0 << endl;
        return 0;
    }
    REP(i,3){
        sort(ALL(p[i]));
    }
    VI x, y, z; bool f=0;
    REP(i,3){
        if(p[i].size()%2==1){
            if(!f)
                x=p[i];
            else
                y=p[i];
            f=1;
        }
        else
            z=p[i];
    }
    ll ans=INF;
    for(auto i:x){
        auto it=lower_bound(ALL(y),i);
        if(it!=y.end())
            ans=min(ans,abs(i-*it));
        if(it!=y.begin())
            ans=min(ans,abs(i-*(it-1)));
    }
    for(auto i:y){
        auto it=lower_bound(ALL(x),i);
        if(it!=x.end())
            ans=min(ans,abs(i-*it));
        if(it!=x.begin())
            ans=min(ans,abs(i-*(it-1)));
    }
    if(z.size()>0){
        vector<P> xmxs, ymxs;
        REP(i,z.size()){
            ll xmx=INF, ymx=INF;
            auto it=lower_bound(ALL(x),z[i]);
            if(it!=x.end())
                xmx=min(xmx,abs(z[i]-*it));
            if(it!=x.begin())
                xmx=min(xmx,abs(z[i]-*(it-1)));
            auto it2=lower_bound(ALL(y),z[i]);
            if(it2!=y.end())
                ymx=min(ymx,abs(z[i]-*it2));
            if(it2!=y.begin())
                ymx=min(ymx,abs(z[i]-*(it2-1)));
            xmxs.push_back({xmx,i});
            ymxs.push_back({ymx,i});
        }
        sort(ALL(xmxs));
        sort(ALL(ymxs));
        if(xmxs[0].second!=ymxs[0].second)
            ans=min(ans,xmxs[0].first+ymxs[0].first);
        else{
            ans=min(ans,xmxs[0].first+ymxs[1].first);
            ans=min(ans,xmxs[1].first+ymxs[0].first);
        }
    }
    cout << ans << endl;
    return 0;
}