#include <bits/stdc++.h>
using namespace std;
int n;
long long s;
bool b[2001];
struct hzy {
  int x, y;
} a[2001];
bool mycmp(hzy a, hzy b) {
  if (a.x > b.x) {
    return true;
  } else {
    if (a.x == b.x && a.y < b.y) {
      return true;
    }
  }
  return false;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].x;
    a[i].y = i;
  }
  sort(a + 1, a + n + 1, mycmp);
  for (int i = 1; i <= n; i++) {
    for (int j = max(n - a[i].y, a[i].y - 1); j >= 1; j--) {
      if (a[i].y - j >= 1) {
        if (b[a[i].y - j] == 0) {
          b[a[i].y - j] = 1;
          s += a[i].x * j;
          break;
        }
      }
      if (a[i].y + j <= n) {
        if (b[a[i].y + j] == 0) {
          b[a[i].y + j] = 1;
          s += a[i].x * j;
          break;
        }
      }
    }
  }
  cout << s;
  return 0;
}
