#include <bits/stdc++.h>
using namespace std;
struct str {
  int x, y;
};
str a[200005];
bool cmp(str p, str q) {
  if (p.x == q.x)
    return p.y > q.y;
  else
    return p.x > q.x;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].x;
    a[i].y = i;
  }
  sort(a, a + n, cmp);
  int l = 1, r = n, sum = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].y + 1 - l > r - a[i].y - 1) {
      sum += (a[i].y + 1 - l) * a[i].x;
      l++;
    } else {
      sum += (r - a[i].y - 1) * a[i].x;
      r--;
    }
  }
  cout << sum << endl;
}
