#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long p) {
  long long a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
long long mpower(long long x, long long p) {
  long long a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % 1000000007;
      p /= 2;
    } else {
      a = a * x % 1000000007;
      p--;
    }
  }
  return a;
}
long long c(long long n, long long k) {
  long long a = 1;
  for (auto i = 0; i < k; i++) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
long long mc(long long n, long long k) {
  long long a = 1, b = 1;
  for (auto i = 0; i < k; i++) {
    a = a * (n - i + 1) % 1000000007;
    b = b * i % 1000000007;
  }
  return a * mpower(b, 1000000007 - 2) % 1000000007;
}
const double PI = 3.14159265358979323846;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
  int N, d, t, tt, ttt;
  long long s = 0;
  map<int, vector<int>> p;
  scanf("%d", &N);
  vector<int> A(N), f(N, 0), R(N);
  for (auto i = 0; i < N; i++) scanf("%d", &A[i]), p[A[i]].push_back(i);
  sort(A.begin(), A.end(), std::greater<int>());
  for (auto i = 0; i < N; i++) {
    d = 0;
    tt = 0;
    for (auto v : p[A[i]]) {
      for (auto x = 0; x < N; x++) {
        if (f[x] == 0) {
          if (d < abs(x - v)) d = abs(x - v), t = x, ttt = tt;
        }
      }
      tt++;
    }
    s += A[i] * d;
    f[t] = 1;
    p[A[i]].erase(p[A[i]].begin() + ttt);
  }
  printf("%lld", s);
  return 0;
}
