/**
 *    author:  avoas
 *    created: 12.05.2021 18:03:29
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

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

constexpr double EPS = 1e-12;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    
    vector<long long> a(n);
    long long sum = 0;
    rep(i,n)
    {
        cin >> a[i];
        sum += a[i];
    }

    sort(all(a));
    reverse(all(a));

    vector<long long> sn(n);
    sn[0] = sum - a[0];
    for(int i = 1; i < n; ++i)
    {
        sn[i] = sn[i-1] - a[i];
    }

    long long ans = 0;

    rep(i,n)
    {
        ans += (n-1-i)*a[i] - sn[i];
    }

    cout << ans << endl;

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define endl ("\n")
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define vi vector<lli>
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define mod 1000000007
#define all(s) s.begin(), s.end()
vector<lli>ar[200005];
lli sz = 0, vis[200005] = {0};
void solve(lli n) {
    vis[n] = 1;
    sz++;
    for (auto c : ar[n]) {
        if (!vis[c]) solve(c);
    }
}
int main() {
    fast
    lli tt, n;
    tt = 1;
    // cin>>tt;
    while (tt--) {
        cin>>n;
        lli a[n], ans = 0;
        rep (i, 0, n) cin>>a[i];
        rep (i, 0, n/2) {
            if (a[i] != a[n - i - 1]) ar[a[i]].pb(a[n - i - 1]), ar[a[n - i - 1]].pb(a[i]);
        }
        // map<lli, lli>mp;
        // for (int i = 1; i < 100005; i++) {
        //     if (ar[i].size() > 0) {
        //         sort(all(ar[i]));
        //         lli p = ar[i].size();
        //         if (ar[i][0] == ar[i][p - 1] && mp.find(ar[i][p - 1]) == mp.end()) ans++, mp[i] = ar[i][p - 1];
        //         else {
        //             ar[i][p - 1] = mp[ar[i][p - 1]];
        //             if (i == ar[i][p - 1]) continue;
        //         }
        //     }
        // }
        // cout<<ans;

        rep (i, 1, 200005) {
            if (ar[i].size() > 0 && !vis[i]) {
                sz = 0;
                solve(i);
                ans += (sz - 1);
            }
        }
        cout<<ans;
    }
    return 0;
}