#include <bits/stdc++.h>
using namespace std;
const int mn = 2006;
int a[mn], id[mn];
long long ans;
bool cmp(int x, int y) { return a[x] > a[y]; }
int main() {
  int n;
  scanf("%d", &n);
  int l = 1, r = n;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    id[i] = i;
  }
  sort(id + 1, id + n + 1, cmp);
  for (int i = 1; i <= n; ++i) {
    if (r - id[i] > id[i] - l)
      ans += a[id[i]] * (r - id[i]), r--;
    else
      ans += a[id[i]] * (id[i] - l), l++;
  }
  printf("%lld", ans);
}
