#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int toright[n];
  int rightend = n - 1;
  int toleft[n];
  int leftend = 0;
  fill(toright, toright + n, 0);
  fill(toleft, toleft + n, 0);
  ll ans = 0;
  int rightmax = 0;
  int rightmaxindex = -1;
  int leftmax = 0;
  int leftmaxindex = -1;
  for (int i = 0; i < n; i++) {
    rightmax = 0;
    rightmaxindex = -1;
    for (int j = 0; j < n; j++) {
      toright[j] = a[j] * abs(rightend - j);
      if (rightmax < toright[j]) {
        rightmax = toright[j];
        rightmaxindex = j;
      } else if (rightmax == toright[j]) {
        if (rightmaxindex == -1 || toleft[j] < toleft[rightmaxindex]) {
          rightmax = toright[j];
          rightmaxindex = j;
        }
      }
    }
    leftmax = 0;
    leftmaxindex = -1;
    for (int j = 0; j < n; j++) {
      toleft[j] = a[j] * abs(j - leftend);
      if (leftmax < toleft[j]) {
        leftmax = toleft[j];
        leftmaxindex = j;
      } else if (leftmax == toleft[j]) {
        if (leftmaxindex == -1 || toright[j] < toright[leftmaxindex]) {
          leftmax = toleft[j];
          leftmaxindex = j;
        }
      }
    }
    if (leftmax > rightmax) {
      ans += leftmax;
      a[leftmaxindex] = 0;
      leftend++;
    } else {
      ans += rightmax;
      a[rightmaxindex] = 0;
      rightend--;
    }
  }
  cout << ans << endl;
}
