#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
struct MinCostFlow {
  struct Edge {
    ll to, cap, cost, rev;
  };
  using P = pair<ll, ll>;
  ll n;
  vector<vector<Edge>> g;
  vector<ll> h;
  vector<ll> dist;
  vector<ll> prevv, preve;
  MinCostFlow(ll n) : n(n), g(n), h(n), dist(n), prevv(n), preve(n) {}
  void add_edge(ll from, ll to, ll cap, ll cost) {
    g.at(from).push_back((Edge){to, cap, cost, (ll)g.at(to).size()});
    g.at(to).push_back((Edge){from, 0, -cost, (ll)g.at(from).size() - 1});
  }
  ll min_cost_flow(ll s, ll t, ll f) {
    ll res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> que;
      fill(dist.begin(), dist.end(), numeric_limits<ll>::max());
      dist.at(s) = 0;
      que.push(make_pair(0, s));
      while (!que.empty()) {
        const P p = que.top();
        que.pop();
        const ll v = p.second;
        if (dist.at(v) < p.first) {
          continue;
        }
        for (ll i = 0; i < (g.at(v).size()); ++i) {
          Edge& e = g.at(v).at(i);
          if (e.cap > 0 &&
              dist.at(e.to) > dist.at(v) + e.cost + h.at(v) - h.at(e.to)) {
            dist.at(e.to) = dist.at(v) + e.cost + h.at(v) - h.at(e.to);
            prevv.at(e.to) = v;
            preve.at(e.to) = i;
            que.push(P(dist.at(e.to), e.to));
          }
        }
      }
      if (dist.at(t) == numeric_limits<ll>::max()) {
        return -1;
      }
      for (ll v = 0; v < (n); ++v) {
        h.at(v) += dist.at(v);
      }
      ll d = f;
      for (ll v = t; v != s; v = prevv.at(v)) {
        d = min(d, g.at(prevv.at(v)).at(preve.at(v)).cap);
      }
      f -= d;
      res += d * h.at(t);
      for (ll v = t; v != s; v = prevv.at(v)) {
        Edge& e = g.at(prevv.at(v)).at(preve.at(v));
        e.cap -= d;
        g.at(v).at(e.rev).cap += d;
      }
    }
    return res;
  }
};
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < (n); ++i) {
    cin >> a.at(i);
  }
  MinCostFlow mcf(2 * (n + 1));
  for (ll i = 0; i < (n); ++i) {
    mcf.add_edge(0, i + 1, 1, 0);
  }
  for (ll i = 0; i < (n); ++i) {
    mcf.add_edge(n + 1 + i, 2 * n + 1, 1, 0);
  }
  for (ll i = 0; i < (n); ++i) {
    for (ll j = 0; j < (n); ++j) {
      mcf.add_edge(i + 1, n + 1 + j, 1, -a.at(i) * abs(i - j));
    }
  }
  const ll f = mcf.min_cost_flow(0, 2 * n + 1, n);
  ll ans = 0;
  for (ll i = 0; i < (n); ++i) {
    for (const auto e : mcf.g.at(i + 1)) {
      if (e.to > i + 1 && e.cap == 0) {
        const ll j = e.to - (n + 1);
        ans += a.at(i) * abs(i - j);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
