#include <bits/stdc++.h>
const int INF = (int)1e9 + 10;
const long long MOD = (long long)1e9 + 7;
const long long LINF = (long long)4e18 + 20;
const long double PI = acos(-1.0);
const double EPS = 1e-9;
using namespace ::std;
void chmax(long long& a, long long b) { a = max(a, b); }
void chmin(long long& a, long long b) { a = min(a, b); }
class BITree {
  int N;
  vector<long long> bit;

 public:
  BITree(int n) {
    N = n;
    bit = vector<long long>(N + 1, 0);
  }
  void add(int a, long long w) {
    if (a <= 0 || N < a) return;
    for (int i = a; i <= N; i += i & -i) {
      bit[i] += w;
    }
  }
  long long sum(int a) {
    long long ret = 0;
    if (a > N) a = N;
    for (int i = a; i > 0; i -= i & -i) {
      ret += bit[i];
    }
    return ret;
  }
  long long sum(int a, int b) {
    long long ret = 0;
    ret = sum(b) - sum(a - 1);
    return ret;
  }
};
void solve() {
  int N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  vector<long long> B(N);
  pair<long long, long long> temp;
  for (int i = 0; i < N; ++i) {
    cin >> temp.first;
    temp.second = i;
    A[i] = temp;
    B[i] = temp.first;
  }
  vector<int> D(N, 0);
  vector<bool> use(N, false);
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  long long ans = 0;
  int Lindex = 0;
  int Rindex = N - 1;
  for (int i = 0; i < N; ++i) {
    int ter = A[i].second;
    int cnt = 0;
    for (int i = 0; i < ter; ++i) {
      if (use[i] == false) {
        ++cnt;
      }
    }
    int now = Lindex + cnt;
    use[ter] = true;
    long long LDL = 0;
    long long LDM = 0;
    long long LDR = 0;
    for (int i = 0; i < ter; ++i) {
      if (use[i] == true) continue;
      if (D[i] > 0) {
        LDR += B[i];
      } else if (D[i] < 0) {
        LDL += B[i];
      } else {
        LDM += B[i];
      }
    }
    long long RDL = 0;
    long long RDM = 0;
    long long RDR = 0;
    for (int i = ter + 1; i < N; ++i) {
      if (use[i] == true) continue;
      if (D[i] > 0) {
        RDR += B[i];
      } else if (D[i] < 0) {
        RDL += B[i];
      } else {
        RDM += B[i];
      }
    }
    long long Lsum = LDM + LDR - LDL + A[i].first * abs(Lindex - A[i].second);
    long long Rsum = RDM + RDL - RDR + A[i].first * abs(Rindex - A[i].second);
    if (Lsum < Rsum) {
      ans += A[i].first * abs(Rindex - A[i].second);
      for (int i = ter + 1; i < N; ++i) {
        if (use[i] == true) continue;
        D[i]--;
      }
      --Rindex;
    } else {
      ans += A[i].first * abs(Lindex - A[i].second);
      for (int i = 0; i < ter; ++i) {
        if (use[i] == true) continue;
        D[i]++;
      }
      ++Lindex;
    }
  }
  cout << ans << endl;
}
signed main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed;
  cout << setprecision(16);
  solve();
}
