#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long inf64 = 1e18;
const long long MOD = inf + 7;
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  ;
  long long n;
  cin >> n;
  pair<long long, long long> a[n + 1];
  for (long long i = 1; i < (long long)n + 1; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  set<long long> s;
  for (long long i = 1; i < (long long)n + 1; i++) s.insert(i);
  sort(a + 1, a + 1 + n);
  long long ans = 0;
  for (long long i = n; i >= 1; i--) {
    long long id = a[i].second;
    long long val = a[i].first;
    long long left = *s.begin();
    long long right = *s.rbegin();
    if (abs(left - id) >= abs(right - id)) {
      ans += abs(left - id) * val;
      s.erase(left);
    } else {
      ans += abs(right - id) * val;
      s.erase(right);
    }
  }
  cout << ans;
}
