#include <bits/stdc++.h>
using namespace std;
long long n, m, a[200005], b[200005];
vector<pair<long long, long long> > v;
int main() {
  long long int i, j, c = 0, t, l, r, mid, z = 0, k, x, y;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  l = 1;
  r = n;
  z = 0;
  for (i = 1; i <= n; i++) {
    c = -1;
    x = 0, y = 0;
    for (j = 1; j <= n; j++) {
      if (a[j] == 0) continue;
      if ((abs(j - l) * a[j]) >= c) {
        c = abs(j - l) * a[j];
        x = l;
        y = j;
      }
      if ((abs(j - r) * a[j]) >= c) {
        c = abs(j - r) * a[j];
        x = r;
        y = j;
      }
    }
    b[x] = y;
    while (x == 0 || y == 0 || a[y] == 0) {
    }
    z += (a[y] * abs(x - y));
    a[y] = 0;
    if (x == l)
      l++;
    else
      r--;
  }
  cout << z << endl;
}
