#include <iostream>
#include <cmath>
using namespace std;
#define repeat(i,n) for (int i = 0; i < (n); ++i)
#include <vector>
#include <queue>
typedef pair<int,int> pii;

class min_cost_flow {
public:
    struct edge_t { int to, cap, cost, rev; };
    static int infinity;

    min_cost_flow() : graph() {}
    min_cost_flow(int n) : graph(n) {}

    vector<vector<edge_t> > graph; // adjacency_list

    int add_vertex() {
        graph.push_back(vector<edge_t>());
        return graph.size()-1;
    }

    void add_edge(int from, int to, int cap, int cost) {
        graph[from].push_back((edge_t){   to, cap, cost, (int)graph[  to].size() });
        graph[  to].push_back((edge_t){ from,  0, -cost, (int)graph[from].size() - 1 });
    }

    int run_destructive(int s, int t, int f) {
        int result = 0;
        vector<int> potential(graph.size(), 0);
        while (0 < f) {
            // update potential using dijkstra
            vector<int> distance(graph.size(), infinity); // minimum -
            vector<int> prev_v(graph.size()); // constitute a single-linked-list represents the flow-path
            vector<int> prev_e(graph.size());
            { // initialize distance and prev_{v,e}
                priority_queue<pii, vector<pii>, greater<pii> > q; // distance * vertex
                distance[s] = 0;
                q.push(pii(0,s));
                while (not q.empty()) {
                    int d = q.top().first;
                    int v = q.top().second; q.pop();
                    if (distance[v] < d) continue;
                    // look round the vertex
                    repeat (e_ix,(int)graph[v].size()) {
                        // consider updating
                        edge_t & e = graph[v][e_ix];
                        int w = e.to;
                        int d1 = distance[v] + e.cost + potential[v] - potential[w]; // updated distance
                        if (0 < e.cap and d1 < distance[e.to]) {
                            distance[w] = d1;
                            prev_v[w] = v;
                            prev_e[w] = e_ix;
                            q.push(pii(d1,w));
                        }
                    }
                }
            }
            if (distance[t] == infinity) return -1; // no such flow
            repeat (v,(int)graph.size()) potential[v] += distance[v];
            {
                int d = f;
                // let flow on the s->t minimum path
                for (int v = t; v != s; v = prev_v[v]) {
                    d = min(d, graph[prev_v[v]][prev_e[v]].cap);
                }
                f -= d;
                result += d * potential[t];
                for (int v = t; v != s; v = prev_v[v]) {
                    edge_t & e = graph[prev_v[v]][prev_e[v]];
                    e.cap -= d;
                    graph[v][e.rev].cap += d;
                }
            }
        }
        return result;
    }
};
int min_cost_flow::infinity = 1e9;
ostream & operator << (ostream & out, const min_cost_flow & mcf) {
    repeat (i,(int)mcf.graph.size()) repeat (j,(int)mcf.graph[i].size()) {
        if (i) out << endl;
        min_cost_flow::edge_t e = mcf.graph[i][j];
        out << i << " -> " << e.to << "\t: { cap : " << e.cap << ",\tcost : " << e.cost << ",\trev : " << e.rev;
    }
    return out;
}


#define MAX_N (100)
int N;
static int P[MAX_N];
int A(int i) { return i; }
int B(int i) { return N+i; }
int S() { return N*2; }
int T() { return N*2+1; }
int graph_size() { return N*2+2; }
int cost(int i, int j) {
    return ((i == N*2 and j < N) ? 0 :
            (i < N and N <= j and j < N*2) ? (P[i] + P[j-N]) * (j-N - i) :
            (N <= i and i < N*2 and j == N*2+1) ? 0 :
            cost(i,j) /* diverge */);
}

int main() {
    cin >> N;
    repeat (i,N) cin >> P[i];

    // min cost flow of such a graph:
    /*         S      -- S N*2
     *     / / | \ \
     *     5 3 1 2 4  -- A i
     *     . . . . |
     *             |
     *     +-+-+---+
     *     | | |   |
     *     1 2 3 4 5  -- B N+i
     *     \ \ | / /
     *         T      -- T N*2+1
     */
    // the swap means:
    /*    1   2
     *     \ /    1 -> 2 : cost 1
     *      X     2 -> 1 : cost 2
     *     / \  the swap : cost 3
     *    1   2
     */
    static min_cost_flow mcf(graph_size());
    repeat (i,N) repeat (j,N) if (P[i]-1 != j) {
        mcf.add_edge(A(i), B(j), 1, P[i] * abs(i - j));
    }
    repeat (i,N) {
        mcf.add_edge(S(), A(i), 1, 0);
        mcf.add_edge(B(i), T(), 1, 0);
    }
    cout << mcf.run_destructive(S(), T(), N) << endl;
}

Measured memory_bytes: 1572