#include <bits/stdc++.h>
using namespace std;
int app() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
  }
  int64_t sum = 0;
  int rev;
  vector<int> used(n);
  for (int i = 0; i < (int)(n); i++) used[i] = 0;
  for (int i = 0; i < (int)(n); i++) {
    int max = 0, jmax = -1;
    for (int j = (i); j < (int)(n); j++) {
      if (used[j]) continue;
      int t = a[j] * (j - i);
      if (t > max) {
        max = t;
        jmax = j;
      }
    }
    if (jmax < 0) {
      rev = i;
      break;
    }
    used[jmax] = 1;
    sum += max;
  }
  for (int i = (n)-1; i >= (int)(rev); i--) {
    int max = 0, jmax = -1;
    for (int j = (0); j < (int)(i + 1); j++) {
      if (used[j]) continue;
      int t = a[j] * (i - j);
      if (t > max) {
        max = t;
        jmax = j;
      }
    }
    if (jmax >= 0) {
      used[jmax] = 1;
      sum += max;
    }
  }
  return sum;
}
int main() {
  cout << app() << endl;
  return 0;
}
