#include <bits/stdc++.h>
using namespace std;
int n, vis[2010], pos[2010], tmp[2010];
long long ans, a[2010];
int bs(int num) {
  if (num < 0) return -num;
  return num;
}
int main() {
  memset(vis, 0, sizeof(vis));
  memset(tmp, 0, sizeof(tmp));
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; ++i) {
    pos[i] = 0;
    for (int j = 1; j <= n; ++j)
      if (!tmp[j] && a[j] > a[pos[i]]) pos[i] = j;
    tmp[pos[i]] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    int dis = -1, ps = 0;
    for (int j = 1; j <= n; ++j)
      if (!vis[j] && bs(pos[i] - j) > dis) dis = bs(pos[i] - j), ps = j;
    vis[ps] = 1;
    ans += a[pos[i]] * (long long)(dis);
  }
  printf("%lld", ans);
  return 0;
}
