#include <bits/stdc++.h>
int compare_int(const void* a, const void* b) { return *(int*)b - *(int*)a; }
int LorR(long long left, long long right, long long n) {
  if (right + left >= n * 2) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  long long i, n, l, r, a[2020][2];
  long long ans = 0;
  for (i = 0; i < 2020; i++) {
    a[i][0] = 0;
    a[i][1] = 0;
  }
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    a[i][1] = i;
    scanf("%lld", &a[i][0]);
  }
  qsort(a, n, sizeof(a[0]), compare_int);
  l = 0;
  r = n + 1;
  for (i = 1; i <= n; i++) {
    if (LorR(l, r, a[i][1]) == 1) {
      ans += a[i][0] * (r - 1 - a[i][1]);
      r--;
    } else {
      ans += a[i][0] * (a[i][1] - l + 1);
      l++;
    }
  }
  printf("%lld", ans);
}
