#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
typedef long long FLOW;
const int MAX_V = 4040;
const FLOW INF = 1e15;
struct Edge {
  int rev, from, to;
  FLOW cap, icap;
  long long cost;
  Edge(int r, int f, int t, FLOW ca, long long co)
      : rev(r), from(f), to(t), cap(ca), icap(ca), cost(co) {}
};
struct Graph {
  int V;
  vector<Edge> list[MAX_V];
  Graph(int n = 0) : V(n) {
    for (int i = 0; i < MAX_V; ++i) list[i].clear();
  }
  void init(int n = 0) {
    V = n;
    for (int i = 0; i < MAX_V; ++i) list[i].clear();
  }
  void resize(int n = 0) { V = n; }
  void reset() {
    for (int i = 0; i < V; ++i)
      for (int j = 0; j < list[i].size(); ++j) list[i][j].cap = list[i][j].icap;
  }
  inline vector<Edge>& operator[](int i) { return list[i]; }
  Edge& redge(Edge& e) {
    if (e.from != e.to)
      return list[e.to][e.rev];
    else
      return list[e.to][e.rev + 1];
  }
  void addedge(int from, int to, FLOW cap, long long cost) {
    list[from].push_back(Edge((int)list[to].size(), from, to, cap, cost));
    list[to].push_back(Edge((int)list[from].size() - 1, to, from, 0, -cost));
  }
};
long long MinCostFlow(Graph& G, int s, int t, FLOW inif) {
  long long dist[MAX_V];
  int prevv[MAX_V];
  int preve[MAX_V];
  long long res = 0;
  FLOW f = inif;
  while (f > 0) {
    fill(dist, dist + G.V, INF);
    dist[s] = 0;
    while (true) {
      bool update = false;
      for (int v = 0; v < G.V; ++v) {
        if (dist[v] == INF) continue;
        for (int i = 0; i < G[v].size(); ++i) {
          Edge& e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            update = true;
          }
        }
      }
      if (!update) break;
    }
    if (dist[t] == INF) return 0;
    FLOW d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += dist[t] * d;
    for (int v = t; v != s; v = prevv[v]) {
      Edge& e = G[prevv[v]][preve[v]];
      Edge& re = G.redge(e);
      e.cap -= d;
      re.cap += d;
    }
  }
  return res;
}
int main(void) {
  long long n;
  cin >> n;
  vector<long long> as(n);
  for (long long i = 0; i < n; i++) {
    cin >> as[i];
  }
  Graph g(2 * n + 2);
  for (long long from = 0; from < n; from++) {
    for (long long to = 0; to < n; to++) {
      long long m = abs(from - to);
      g.addedge(from, to + n, 1, -m * as[from]);
    }
  }
  for (long long to = 0; to < n; to++) {
    g.addedge(2 * n, to, 1, 0);
  }
  for (long long to = 0; to < n; to++) {
    g.addedge(to + n, 2 * n + 1, 1, 0);
  }
  auto p = MinCostFlow(g, 2 * n, 2 * n + 1, n);
  cout << -p << endl;
  return 0;
}
