#include <bits/stdc++.h>
using namespace std;
struct Edge {
  long long src, dst;
  long long weight;
  Edge(long long src, long long dst, long long weight)
      : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
int main() {
  long long n_main;
  cin >> n_main;
  vector<long long> a_main(n_main);
  for (int i = 0; i < n_main; i++) {
    cin >> a_main[i];
  }
  vector<vector<long long>> a(n_main, vector<long long>(n_main, 0));
  for (long long i = 0; i < n_main; i++) {
    for (long long j = 0; j < n_main; j++) {
      a[i][j] = a_main[i] * abs(i - j);
    }
  }
  int n = a.size(), p, q;
  vector<long long> fx(n, LLONG_MIN / 2), fy(n, 0);
  vector<int> x(n, -1), y(n, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) fx[i] = max(fx[i], a[i][j]);
  for (int i = 0; i < n;) {
    vector<int> t(n, -1), s(n + 1, i);
    for (p = q = 0; p <= q && x[i] < 0; ++p)
      for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)
        if (fx[k] + fy[j] == a[k][j] && t[j] < 0) {
          s[++q] = y[j], t[j] = k;
          if (s[q] < 0)
            for (p = j; p >= 0; j = p) y[j] = k = t[j], p = x[k], x[k] = j;
        }
    if (x[i] < 0) {
      long long d = LLONG_MIN / 2;
      for (int k = 0; k <= q; ++k)
        for (int j = 0; j < n; ++j)
          if (t[j] < 0) d = min(d, fx[s[k]] + fy[j] - a[s[k]][j]);
      for (int j = 0; j < n; ++j) fy[j] += (t[j] < 0 ? 0 : d);
      for (int k = 0; k <= q; ++k) fx[s[k]] -= d;
    } else
      ++i;
  }
  long long ret = 0;
  for (int i = 0; i < n; ++i) ret += a[i][x[i]];
  cout << ret << endl;
}
