#include <bits/stdc++.h>
using namespace std;
struct node {
  long long flag;
  long long x;
  long long y;
} p[2050];
bool camp(node a, node b) {
  if (a.y == b.y) {
    return a.x < b.y;
  } else
    return a.y > b.y;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> p[i].y;
    p[i].x = i + 1;
  }
  long long sum = 0;
  sort(p, p + n, camp);
  for (long long i = 0; i < n; i++) {
    if (p[i].flag == 0) {
      p[i].flag = 1;
      long long max1 = 0, flag1 = 0;
      for (long long j = i + 1; j < n; j++) {
        if (flag1 == 0 && p[j].flag == 0) {
          flag1 = 1;
          max1 = j;
        } else if (p[j].flag == 0 &&
                   fabs(p[j].x - p[i].x) > fabs(p[max1].x - p[i].x)) {
          max1 = j;
        }
      }
      if (flag1 == 1) {
        sum += (p[i].y + p[max1].y) * fabs(p[i].x - p[max1].x);
        p[max1].flag = 1;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
