#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[n + 1];
  map<long long, vector<long long>, greater<long long>> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m[a[i]].push_back(i);
  }
  int l = 1, r = n;
  long long ans = 0;
  for (auto k : m) {
    long long num = k.first;
    vector<long long> v = k.second;
    int l1 = 0, r1 = v.size() - 1;
    while (l1 < r1) {
      if (abs(v[l1] - r) > abs(v[r1] - l)) {
        ans += num * abs(v[l1] - r);
        r--;
        l1++;
      } else {
        ans += num * abs(v[r1] - l);
        l++;
        r1--;
      }
    }
    if (abs(v[l1] - l) > abs(v[l1] - r)) {
      ans += num * abs(v[l1] - l);
      l++;
    } else {
      ans += num * abs(v[l1] - r);
      r--;
    }
  }
  cout << ans;
}
