#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
int main() {
  int n;
  cin >> n;
  ll a[n][2];
  ll b;
  for (int i = 0; i < n; i++) {
    cin >> b;
    a[i][0] = b;
    a[i][1] = i;
  }
  qsort(a, n, sizeof(a[0]), cmp);
  int min = 0;
  int max = n - 1;
  ll score = 0;
  bool check = false;
  vector<int> remain{};
  while (!(check && remain.size() == 0)) {
    remain = {};
    for (int i = 0; i < n; ++i) {
      ll temp = a[i][0];
      if ((min + max) % 2 == 0 && a[i][1] == (min + max) / 2) {
        remain.push_back(i);
        continue;
      } else {
        if (a[i][1] < (min + max) / 2) {
          score += (max - a[i][1]) * a[i][0];
          max--;
        } else {
          score += (a[i][1] - min) * a[i][0];
          min++;
        }
      }
    }
    check = true;
  }
  cout << score << endl;
}
