#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
const int M = 1e6 + 10;
const int N = 2e3 + 10;
inline long long read() {
  long long b = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
struct node {
  int loc;
  long long val;
} a[N];
int n, m, k, T;
long long ans;
bool cmp(node x, node y) { return x.val > y.val; }
int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i].val = read();
    a[i].loc = i;
  }
  sort(a + 1, a + n + 1, cmp);
  int l = 1, r = n;
  for (i = 1; i <= n; i++) {
    int mid = (l + r) >> 1;
    if (a[i].loc - l >= r - a[i].loc) {
      ans += a[i].val * (a[i].loc - l);
      l++;
    } else {
      ans += a[i].val * (r - a[i].loc);
      r--;
    }
  }
  printf("%lld", ans);
  return 0;
}
