#include <bits/stdc++.h>
using namespace std;
const int N = 2009, INF = 0x3f3f3f3f;
int n, k, w[N][N], wx[N], wy[N], D, my[N], g[N], slck[N], ans = 0;
bool vy[N];
int main() {
  fill(w[0], w[0] + N * N, -INF);
  fill(wx, wx + N, -INF);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    for (int j = 1; j <= n; j++) wx[i] = max(wx[i], w[i][j] = abs(i - j) * k);
  }
  for (int i = 1; i <= n; i++) {
    int px, py = 0, yy = 0;
    fill(g, g + N, 0), fill(vy, vy + N, 0), fill(slck, slck + N, INF);
    my[py] = i;
    do {
      px = my[py], D = INF, vy[py] = 1;
      for (int j = 1; j <= n; j++)
        if (!vy[j]) {
          int d = wx[px] + wy[j] - w[px][j];
          if (slck[j] > d) slck[j] = d, g[j] = py;
          if (slck[j] < D) D = slck[j], yy = j;
        }
      if (D == INF) {
        printf("NIE");
        return 0;
      }
      for (int j = 0; j <= n; j++) {
        if (vy[j])
          wx[my[j]] -= D, wy[j] += D;
        else
          slck[j] -= D;
      }
      py = yy;
    } while (my[py]);
    while (py) my[py] = my[g[py]], py = g[py];
  }
  for (int i = 1; i <= n; i++) ans += w[my[i]][i];
  printf("%d", ans);
  return 0;
}
