#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (auto &t : a) {
    cin >> t;
  }
  vector<long long> ind(n);
  iota(ind.begin(), ind.end(), 0);
  while (true) {
    long long fn = 0;
    for (long long i = 0; i + 1 < n; i++) {
      auto f =
          abs(i - ind[i]) * a[ind[i]] + abs(i + 1 - ind[i + 1]) * a[ind[i + 1]];
      auto s =
          abs(i - ind[i + 1]) * a[ind[i + 1]] + abs(i + 1 - ind[i]) * a[ind[i]];
      if (f < s) {
        fn = 1;
        swap(ind[i], ind[i + 1]);
      }
    }
    if (!fn) break;
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += abs(i - ind[i]) * a[ind[i]];
  }
  cout << ans << endl;
}
