import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

const INF = int.max / 4;

struct P {
    int v, cost;
}

class Edge {
    int from, to, cap, cost;
    this() {}
    this(int from, int to, int cap, int cost) {
        this.from = from;
        this.to = to;
        this.cap = cap;
        this.cost = cost;
    }
    Edge rev;
    override string toString() {
        return "Edge " ~ [from, to, cap, cost].to!string;
    }
}

class PrimalDual {
    int V;
    Edge[][] G;
    int[] H;
    int[] dist;
    Edge[] preve;
    void addEdge(int from, int to, int cap, int cost) {
        //writeln([from, to, cap, cost]);
        auto a = new Edge(from, to, cap, cost),
             b = new Edge(to, from, 0, -cost);
        a.rev = b;
        b.rev = a;
        G[from] ~= a;
        G[to] ~= b;
    }
    this(int V) {
        this.V = V;
        this.G = new Edge[][V];
        this.H = new int[V];
        this.dist = new int[V];
        this.preve = new Edge[V];
    }

    int min_cost_flow(int s, int t, int flow) {
        int ret = 0;
        H[] = 0;
        while (flow > 0) {
            //writeln([s, t, flow]);
            dist[] = INF;
            dist[s] = 0;
            BinaryHeap!(Array!P, "a.cost > b.cost") PQ;
            PQ.insert(P(s, 0));
            while (!PQ.empty) {
                P p = PQ.front; PQ.removeFront;
                if (dist[p.v] < p.cost) continue;
                foreach (Edge e; G[p.v]) {
                    if (e.cap > 0 && dist[e.to] > dist[p.v] + e.cost + H[p.v] - H[e.to]) {
                        dist[e.to] = dist[p.v] + e.cost + H[p.v] - H[e.to];
                        preve[e.to] = e;
                        PQ.insert(P(e.to, dist[e.to]));
                    }
                }
            }
            if (dist[t] == INF) return -1;
            foreach (v; 0 .. V) H[v] += dist[v];
            int d = flow;
            for (int v = t; v != s; v = preve[v].from) {
                d = min(d, preve[v].cap);
            }
            flow -= d;
            ret += d * H[t];
            for (int v = t; v != s; v = preve[v].from) {
                auto e = preve[v];
                e.cap -= d;
                e.rev.cap += d;
            }
        }
        return ret;
    }
}

void main() {
    int N; scanf("%d\n", &N);
    int[] A = readln.chomp.split(" ").map!((x) => x.to!int - 1).array;
    auto G = new PrimalDual(2 * N + 2);
    foreach (i; 0 .. N) {
        foreach (j; 0 .. N) {
            if (A[i] == j) continue;
            G.addEdge(i + 1, j + N + 1, 1, abs(i - j) * (A[i] + 1));
        }
    }
    foreach (i; 0 .. N) {
        G.addEdge(0, i + 1, 1, 0);
    }
    foreach (i; 0 .. N) {
        G.addEdge(i + N + 1, 2 * N + 1, 1, 0);
    }
    writeln(G.min_cost_flow(0, 2 * N + 1, N));
}