#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;
}
### Prompt

In CPP, your task is to solve the following problem:

G --Derangement / Derangement

Story

Person D is doing research on permutations. At one point, D found a permutation class called "Derangement". "It's cool to have a perfect permutation !!! And it starts with D !!!", the person of D who had a crush on Chunibyo decided to rewrite all the permutations given to him.

Problem

Given a permutation of length n, p = (p_1 p_2 ... p_n). The operation of swapping the i-th element and the j-th element requires the cost of (p_i + p_j) \ times | i-j |. At this time, find the minimum cost required to make p into a perfect permutation using only the above swap operation.

However, the fact that the permutation q is a perfect permutation means that q_i \ neq i holds for 1 \ leq i \ leq n. For example, (5 3 1 2 4) is a perfect permutation, but (3 2 5 4 1) is not a perfect permutation because the fourth number is 4.

Input

The input consists of the following format.


n
p_1 p_2 ... p_n

The first row consists of one integer representing the length n of the permutation. The second line consists of n integers, and the i-th integer represents the i-th element of the permutation p. Each is separated by a single blank character.

Constraints:

* 2 \ leq n \ leq 100
* 1 \ leq p_i \ leq n, i \ neq j ⇔ p_i \ neq p_j



Output

Print the minimum cost to sort p into a complete permutation on one line. Be sure to start a new line at the end of the line.

Sample Input 1


Five
1 2 3 5 4

Sample Output 1


7

After swapping 1 and 2, swapping 1 and 3 yields (2 3 1 5 4), which is a complete permutation.

Sample Input 2


Five
5 3 1 2 4

Sample Output 2


0

It is a perfect permutation from the beginning.

Sample Input 3


Ten
1 3 4 5 6 2 7 8 9 10

Sample Output 3


38





Example

Input

5
1 2 3 5 4


Output

7

### Response

```cpp
#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;
}
```