#include <bits/stdc++.h>
using namespace std;
int Xr, Fr;
char Cr;
inline int rd() {
  Xr = 0, Fr = 1, Cr = getchar();
  while (Cr < '0' || Cr > '9') {
    if (Cr == '-') Fr = -1;
    Cr = getchar();
  }
  while (Cr >= '0' && Cr <= '9')
    Xr = (Xr << 1) + (Xr << 3) + (Cr & 15), Cr = getchar();
  return Xr * Fr;
}
int n;
long long va[200005], ans;
int l = 1, r;
int main() {
  n = rd();
  r = n;
  for (int i = 1; i <= n; i++) va[i] = rd();
  for (int i = 1; i <= n; i++) {
    int ma = 0, t;
    for (int j = 1; j <= n; j++)
      if (va[j] > ma || ((va[j] == ma) && max(abs(j - l), abs(r - j)) >
                                              max(abs(t - l), abs(r - t))))
        ma = va[j], t = j;
    va[t] = 0;
    if (abs(t - l) <= abs(r - t))
      ans += ma * abs(r - t), r--;
    else
      ans += ma * abs(t - l), l++;
  }
  cout << ans << endl;
}
