#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
struct edge {
  int to;
  ll cap, cost;
  int rev;
};
int V, E;
const int MAX_V = 5e3;
vector<edge> es[MAX_V];
ll d[MAX_V], h[MAX_V];
int pre_e[MAX_V], pre_v[MAX_V];
void add_edge(int from, int to, ll cap, ll cost) {
  edge e1 = {to, cap, cost, (int)es[to].size()};
  edge e2 = {from, 0, -cost, (int)es[from].size()};
  es[from].emplace_back(e1), es[to].emplace_back(e2);
}
void dijkstra(int s) {
  fill(d, d + V, INF);
  priority_queue<pli, vector<pli>, greater<pli> > que;
  d[s] = 0;
  que.push(pli(0, s));
  while (!que.empty()) {
    pli p = que.top();
    que.pop();
    int i = p.second;
    if (d[i] < p.first) continue;
    for (int j = 0; j < es[i].size(); j++) {
      edge &e = es[i][j];
      if (e.cap > 0 && d[e.to] > d[i] + e.cost + h[i] - h[e.to]) {
        d[e.to] = d[i] + e.cost + h[i] - h[e.to];
        pre_v[e.to] = i;
        pre_e[e.to] = j;
        que.push(pli(d[e.to], e.to));
      }
    }
  }
}
ll min_cost_flow(int s, int t, ll flow) {
  ll res = 0;
  fill(h, h + V, 0);
  while (flow > 0) {
    dijkstra(s);
    if (d[t] == INF) return -1;
    for (int i = 0; i < V; i++) h[i] += d[i];
    ll f = flow;
    for (int now = t; now != s; now = pre_v[now]) {
      f = min(f, es[pre_v[now]][pre_e[now]].cap);
    }
    flow -= f;
    res += f * h[t];
    for (int now = t; now != s; now = pre_v[now]) {
      edge &e = es[pre_v[now]][pre_e[now]];
      e.cap -= f;
      es[now][e.rev].cap += f;
    }
  }
  return res;
}
int main() {
  int N;
  cin >> N;
  V = 2 * N + 2;
  ll A[N], sum = 0;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) sum += A[i];
  for (int i = 0; i < N; i++) {
    add_edge(2 * N, i, 1, 0);
    add_edge(N + i, 2 * N + 1, 1, 0);
    for (int j = 0; j < N; j++) {
      add_edge(i, N + j, 1, A[i] * (N - abs(j - i)));
    }
  }
  cout << N * sum - min_cost_flow(2 * N, 2 * N + 1, N) << endl;
}
