#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int,int> pii;
#define x first
#define y second

const int N=2e5+100;

int a[N];

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    int pref=0;
    int ans=0;
    for(int i=1;i<=n;i++)
        ans+=a[i]*(i-1)-pref,
        pref+=a[i];
    cout<<ans;
}

#include <bits/stdc++.h>
#define ll long long

using namespace std;

class BIT
{
public:
    ll n;
    vector<ll> tree;
    BIT(ll nn)
    {
        n = nn;
        tree = vector<ll>(n + 1, 0);
    }
    
    ll sum(ll k) 
    {
        k++;
        ll s = 0;
        while (k >= 1) 
        {
            s += tree[k];
            k -= k&-k; 
        }
        return s; 
    }
    
    void add(ll k, ll x)
    {
        k++;
        while (k <= n) 
        {
            tree[k] += x;
            k += k&-k; 
        }
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> v(n);
    for(ll i = 0; i < n; i++)
    {
        cin >> v[i];
    }

    ll nidx = 0;
    auto vv = v;
    sort(vv.begin(), vv.end());
    unordered_map<ll, ll> numToIdx, idxToNum;
    for(ll i = 0; i < n; i++)
    {
        if(numToIdx.count(vv[i]))
            continue;
        numToIdx[vv[i]] = nidx++;
    }

    for(auto& [num, idx] : numToIdx)
    {
        idxToNum[idx] = num;
    }

    BIT bit(nidx), bitCnt(nidx);
    ll ret = 0;
    bit.add(numToIdx[v[n - 1]], v[n - 1]);
    bitCnt.add(numToIdx[v[n - 1]], 1);
    for(ll i = n - 2; i > -1; i--)
    {
        ll idx = numToIdx[v[i]];
        ll left = bit.sum(idx);
        ll leftCnt = bitCnt.sum(idx);
        ll right = bit.sum(nidx - 1) - left;
        ll rightCnt = bitCnt.sum(nidx - 1) - leftCnt;
        ret += leftCnt * v[i] - left + right - v[i] * rightCnt;
        bit.add(idx, v[i]);
        bitCnt.add(idx, 1);
    }
    cout << ret << endl;

    return 0;
}


