#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, a, maxval, uniques, ctr = 0;
  vector<long long int> v, s;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  uniques = set<long long int>(v.begin(), v.end()).size();
  while (uniques > 1) {
    maxval = *max_element(v.begin(), v.end());
    auto maxidx = find(v.begin(), v.end(), maxval);
    ctr +=
        (maxval * distance(maxidx, v.end())) - accumulate(maxidx, v.end(), 0);
    v.erase(maxidx, v.end());
    s = v;
    sort(s.begin(), s.end());
    if (s == v) {
      cout << ctr;
      return 0;
    }
    uniques = set<long long int>(v.begin(), v.end()).size();
  }
  cout << ctr;
  return 0;
}
