#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, ans;
  ans = 0;
  cin >> n;
  vector<pair<long long, long long>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }
  sort(a.begin(), a.end());
  vector<bool> b(n);
  for (int i = 0; i < n; i++) b[i] = true;
  for (int i = n - 1; i <= 0; i--) {
    long long left, right;
    for (long long j = 0; j < n; j++) {
      if (b[j]) {
        left = j + 1;
        break;
      }
    }
    for (long long j = 0; j < n; j++) {
      if (b[n - 1 - i]) {
        right = n - i;
        break;
      }
    }
    if (abs(left - a[i].second) > abs(right - a[i].second)) {
      ans += (left - a[i].second) * a[i].first;
      b[left - 1] = false;
    } else if (abs(left - a[i].second) < abs(right - a[i].second)) {
      ans += (right - a[i].second) * a[i].first;
      b[right - 1] = false;
    } else if (i == 0)
      ans += (right - a[i].second) * a[i].first;
    else {
      if (abs(left - a[i + 1].second) > abs(right - a[i - 1].second)) {
        ans += (right - a[i].second) * a[i].first;
        b[right - 1] = false;
      } else {
        ans += (left - a[i].second) * a[i].first;
        b[left - 1] = false;
      }
    }
  }
  cout << ans << endl;
}
