#include <bits/stdc++.h>
using namespace std;
long long s[2010];
struct node {
  long long x;
  long long p;
} q[2010];
bool cmp(node a, node b) {
  if (a.x != b.x) return a.x > b.x;
  return a.p < b.p;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) {
    q[i].x = s[i];
    q[i].p = i;
  }
  sort(q + 1, q + n + 1, cmp);
  map<long long, long long> jud;
  long long l = 1;
  long long r = n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    int po = q[i].p;
    if (jud[po] == 0) {
      int xx = abs(po - l);
      int yy = abs(po - r);
      if (xx < yy) {
        sum += yy * (q[i].x + s[r]);
        jud[po] = 1;
        jud[r] = 1;
        for (int j = r - 1; j > 0; j--) {
          if (jud[j] == 0) {
            r = j;
            break;
          }
        }
      } else if (yy < xx) {
        sum += xx * (q[i].x + s[l]);
        jud[po] = 1;
        jud[l] = 1;
        for (int j = l + 1; j <= n; j++) {
          if (jud[j] == 0) {
            l = j;
            break;
          }
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}
