#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> a[2003];
bool ok[2003];
long long n, i, tmp, c, x, j;
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%lld", &a[i].first);
    a[i].second = i;
  }
  sort(a + 1, a + 1 + n);
  for (i = n; i > 0; --i) {
    tmp = 0;
    x = 0;
    for (j = 1; j <= n; ++j)
      if ((!ok[j]) && (abs(a[j].second - a[i].second) > tmp)) {
        tmp = abs(a[j].second - a[i].second);
        x = j;
      }
    ok[x] = 1;
    c += a[i].first * tmp;
  }
  printf("%lld", c);
}
