#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;
  for (int i = 0; i < n; ++i) {
    ll temp = a[i][0];
    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++;
    }
  }
  cout << score << endl;
}
