#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = 998244353LL;
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef ll FLOW;
typedef ll COST;
const int MAX_V = 5050;
struct Edge {
  ll rev, from, to;
  FLOW cap, icap;
  COST cost;
  Edge(ll r, ll f, ll t, FLOW ca, COST 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(ll from, ll to, FLOW cap, COST 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));
  }
};
COST MinCostFlow(Graph &G, int s, int t, FLOW inif) {
  COST dist[MAX_V];
  int prevv[MAX_V];
  int preve[MAX_V];
  COST 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;
}
void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int(i) = (int)(0); i < (int)(n); i++) cin >> a[i];
  Graph G(2 * n + 2);
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      ll gain = a[i] * abs(i - j);
      G.addedge(i, j + n, 1, -gain);
    }
  }
  for (int i = 0; i < n; ++i) {
    G.addedge(2 * n, i, 1, 0);
  }
  for (int j = 0; j < n; ++j) {
    G.addedge(j + n, 2 * n + 1, 1, 0);
  }
  COST res = MinCostFlow(G, 2 * n, 2 * n + 1, n);
  ll cost = 0;
  for (int i = 0; i < n; ++i) {
    for (auto e : G[i]) {
      if (e.icap == 1 && e.cap == 0) {
        cost += a[i] * abs(i - e.to + n);
      }
    }
  }
  cout << cost << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
