#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;
long int M = 1e+9 + 7;
int main(void) {
  int n;
  cin >> n;
  vector<long int> a(n);
  long int sum = 0;
  vector<bool> bo(n, true);
  int r = 0, l = n - 1;
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < (n); ++i) {
    vector<long int> ans(n);
    for (int j = 0; j < (n); ++j) {
      if (bo[j]) {
        ans[j] = max(a[j] * (j - r), a[j] * (l - j));
      }
    }
    int maxj = 0;
    for (int j = 0; j < (n); ++j) {
      if (ans[maxj] < ans[j] || (ans[maxj] == ans[j] && a[maxj] > a[j])) {
        maxj = j;
      }
    }
    sum += ans[maxj];
    bo[maxj] = false;
    if (ans[maxj] == a[maxj] * (maxj - r)) {
      r++;
    } else {
      l--;
    }
  }
  cout << sum << endl;
  return 0;
}
