#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T>
struct MinCostFlow {
  struct Edge {
    int u, v;
    T c, w, f;
  };
  const T eps = T(1e-9), INF = numeric_limits<T>::max();
  vector<Edge> E;
  int n, src, sink;
  vector<vector<int>> G;
  T flow = T(), cost = T();
  MinCostFlow(int n, int src, int sink) : n(n), src(src), sink(sink), G(n) {}
  void clear_flow() {
    for (auto& e : E) e.f = T();
    flow = cost = T();
  }
  void reduce() {
    for (auto& e : E) e.c -= e.f;
  }
  void add_edge(int u, int v, T cap, T w) {
    assert(0 <= u && u < n && 0 <= v && v < n);
    G[u].push_back(E.size()), E.push_back({u, v, cap, w, T()});
    G[v].push_back(E.size()), E.push_back({v, u, 0, -w, T()});
  }
  T min_cost_flow(T f = -1) {
    if (f < 0) f = INF;
    if (flow > f) clear_flow();
    f -= flow;
    while (f > eps) {
      vector<T> D(n, INF);
      vector<int> Inq(n), P(n);
      queue<int> Q;
      Q.push(src), D[src] = T(), Inq[src] = 1;
      while (!Q.empty()) {
        int u = Q.front();
        Q.pop(), Inq[u] = 0;
        for (auto i : G[u]) {
          const auto& e = E[i];
          if (e.c - e.f > eps && D[e.v] > D[u] + e.w) {
            D[e.v] = D[u] + e.w, P[e.v] = i;
            if (!Inq[e.v]) Q.push(e.v), Inq[e.v] = 1;
          }
        }
      }
      if (D[sink] == INF) break;
      T can = f;
      for (int u = sink; u != src; u = E[P[u]].u)
        can = min(can, E[P[u]].c - E[P[u]].f);
      f -= can, flow += can, cost += D[sink] * can;
      for (int u = sink; u != src; u = E[P[u]].u)
        E[P[u]].f += can, E[P[u] ^ 1].f -= can;
    }
    return cost;
  }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  MinCostFlow<ll> mf(n * 2 + 2, 0, n * 2 + 1);
  for (int i = 1; i <= n; ++i) {
    mf.add_edge(0, i, 1, 0);
    mf.add_edge(i + n, n * 2 + 1, 1, 0);
    for (int j = 1; j <= n; ++j) {
      mf.add_edge(i, j + n, 1, -1ll * abs(i - j) * a[i]);
    }
  }
  cout << -mf.min_cost_flow(-1) << endl;
}
