#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const int mod = 1000000007;
long long pow(long long base, long long order) {
  long long ans = 1;
  for (int i = 0; i < (order - 1); i++) ans *= base;
  return ans;
}
int gcd(int a, int b) {
  if (a < b) {
    int c = a;
    a = b;
    b = c;
  }
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < (n); i++) {
    cin >> a.at(i).first;
    a.at(i).second = i;
  }
  sort(a.rbegin(), a.rend());
  vector<bool> filled(n, false);
  long long ans = 0;
  for (int i = 0; i < (n); i++) {
    int orig = a.at(i).second, grad = a.at(i).first, left = INF, right = INF;
    for (int j = 0; j < (n / 2); j++) {
      if (left == INF && !filled.at(j)) {
        left = j;
        filled.at(left) = true;
      }
      if (right == INF && !filled.at(n - j - 1)) {
        right = n - j - 1;
        filled.at(right) = true;
      }
      if (left != INF && right != INF) break;
    }
    if (left == INF || right == INF) {
      ans += min(abs(left - orig), abs(right - orig));
      continue;
    }
    if (abs(left - orig) > abs(right - orig)) {
      ans += grad * abs(left - orig);
      filled.at(right) = false;
    } else if (abs(left - orig) < abs(right - orig)) {
      ans += grad * abs(right - orig);
      filled.at(left) = false;
    }
  }
  cout << ans << endl;
}
