#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  std::vector<int> indices(a.size());
  std::iota(indices.begin(), indices.end(), 0);
  std::sort(indices.begin(), indices.end(),
            [&a](int i1, int i2) { return a[i1] >= a[i2]; });
  long right = 0;
  int border = 0;
  int c = 0;
  for (int i = 0; i < n; ++i) {
    c += abs(indices[i] - border) * a[indices[i]];
    c += right;
    if (indices[i] <= border) {
      right += a[indices[i]];
    } else {
      border++;
    }
  }
  cout << c << endl;
}
