#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



struct Edge {
    int rev, from, to, cap, icap, cost;
    Edge(int r, int f, int t, int ca, int co) : rev(r), from(f), to(t), cap(ca), icap(ca), cost(co) {}
    friend ostream& operator << (ostream& s, const Edge& E) {
        if (E.cap > 0) return s << E.from << "->" << E.to << '(' << E.cap << ',' << E.cost << ')';
        else return s;
    }
};

const int MAX_V = 1000;
const int INF = 1<<29;

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(int from, int to, int cap, int cost) {
        list[from].push_back(Edge(list[to].size(), from, to, cap, cost));
        list[to].push_back(Edge(list[from].size()-1, to, from, 0, -cost));
    }
    
    void addbiedge(int from, int to, int cap, int cost) {
        list[from].push_back(Edge(list[to].size(), from, to, cap, cost));
        list[to].push_back(Edge(list[from].size()-1, to, from, cap, cost));
    }
    
    friend ostream& operator << (ostream& s, const Graph& G) {
        for (int i = 0; i < G.V; ++i) {s << i << " : " << G.list[i];}
        return s;
    }
};

Graph G;

int pot[MAX_V];
int dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];

int MinCost(Graph &G, int s, int t, int f) {
    int res = 0;
    fill(pot, pot+G.V, 0);
    while (f > 0) {
        priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > que; 
        fill(dist, dist+G.V, INF);
        dist[s] = 0;
        que.push(make_pair(0,s));
        while(!que.empty()) {
            pair<int,int> p = que.top();
            que.pop();
            int v = p.second;
            if (dist[v] < p.first) 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 + pot[v] - pot[e.to]) {
                    dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    que.push(make_pair(dist[e.to], e.to));
                }
            }
        }
        
        if (dist[t] == INF) return -1;
        
        for (int v = 0; v < G.V; ++v) pot[v] += dist[v];
        int d = f;
        for (int v = t; v != s; v = prevv[v]) {
            d = min(d, G[prevv[v]][preve[v]].cap);
        }
        f -= d;
        res += d * pot[t];
        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;
}


int n, p;
map<int,int> ind;

int main() {
    while (cin >> n) {
        ind.clear();
        for (int i = 0; i < n; ++i) {
            cin >> p; --p;
            ind[p] = i;
        }
        
        G.init(2*n+2);
        int s = 2*n, t = 2*n+1;
        
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) continue;
                
                int cost = (j+1) * abs( i - ind[j] );
                G.addedge(i, n+j, 1, cost);
            }
        }
        for (int i = 0; i < n; ++i) {
            G.addedge(s, i, 1, 0);
            G.addedge(i+n, t, 1, 0);
        }
    
        int res = MinCost(G, s, t, n);
        cout << res << endl;
    }
    
    return 0;
}

### Prompt

Your challenge is to write a CPP solution to 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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



struct Edge {
    int rev, from, to, cap, icap, cost;
    Edge(int r, int f, int t, int ca, int co) : rev(r), from(f), to(t), cap(ca), icap(ca), cost(co) {}
    friend ostream& operator << (ostream& s, const Edge& E) {
        if (E.cap > 0) return s << E.from << "->" << E.to << '(' << E.cap << ',' << E.cost << ')';
        else return s;
    }
};

const int MAX_V = 1000;
const int INF = 1<<29;

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(int from, int to, int cap, int cost) {
        list[from].push_back(Edge(list[to].size(), from, to, cap, cost));
        list[to].push_back(Edge(list[from].size()-1, to, from, 0, -cost));
    }
    
    void addbiedge(int from, int to, int cap, int cost) {
        list[from].push_back(Edge(list[to].size(), from, to, cap, cost));
        list[to].push_back(Edge(list[from].size()-1, to, from, cap, cost));
    }
    
    friend ostream& operator << (ostream& s, const Graph& G) {
        for (int i = 0; i < G.V; ++i) {s << i << " : " << G.list[i];}
        return s;
    }
};

Graph G;

int pot[MAX_V];
int dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];

int MinCost(Graph &G, int s, int t, int f) {
    int res = 0;
    fill(pot, pot+G.V, 0);
    while (f > 0) {
        priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > que; 
        fill(dist, dist+G.V, INF);
        dist[s] = 0;
        que.push(make_pair(0,s));
        while(!que.empty()) {
            pair<int,int> p = que.top();
            que.pop();
            int v = p.second;
            if (dist[v] < p.first) 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 + pot[v] - pot[e.to]) {
                    dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    que.push(make_pair(dist[e.to], e.to));
                }
            }
        }
        
        if (dist[t] == INF) return -1;
        
        for (int v = 0; v < G.V; ++v) pot[v] += dist[v];
        int d = f;
        for (int v = t; v != s; v = prevv[v]) {
            d = min(d, G[prevv[v]][preve[v]].cap);
        }
        f -= d;
        res += d * pot[t];
        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;
}


int n, p;
map<int,int> ind;

int main() {
    while (cin >> n) {
        ind.clear();
        for (int i = 0; i < n; ++i) {
            cin >> p; --p;
            ind[p] = i;
        }
        
        G.init(2*n+2);
        int s = 2*n, t = 2*n+1;
        
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) continue;
                
                int cost = (j+1) * abs( i - ind[j] );
                G.addedge(i, n+j, 1, cost);
            }
        }
        for (int i = 0; i < n; ++i) {
            G.addedge(s, i, 1, 0);
            G.addedge(i+n, t, 1, 0);
        }
    
        int res = MinCost(G, s, t, n);
        cout << res << endl;
    }
    
    return 0;
}

```