#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

#define rep(i,a) for(int i=0;i<(int)(a);i++)
#define INIT(a) memset(a,0,sizeof(a))
#define pb push_back
#define sz size()
#define INF (1<<25)
#define V 300
typedef pair<int,int> P;

struct edge{
  int from,to,cost,cap,rev;
};

vector<edge> G[V];

void AddEdge(int s,int g,int c,int p){
  G[s].pb((edge){s,g,c,p,(int)G[g].sz});
  G[g].pb((edge){g,s,-c,0,(int)G[s].sz-1});
}

int d[V];
int h[V],pv[V],pe[V];
int v;
bool use[V];

int MinCostFlow(int s,int t,int f){
  int res = 0;
  fill(h,h+v,0);
  while(f>0){
    fill(d,d+v,INF);
    memset(use,0,sizeof(use));
    d[s] = 0;

    while(true){
      int u = -1;
      rep(i,v){
	if(!use[i] && (u<0 || d[u] > d[i]))u = i;
      } 

      if(u<0 || d[u] == INF)break;
      use[u] = true;

      rep(i,G[u].sz){
	edge &e = G[u][i];
	if(e.cap>0 && d[e.to] > d[u] + e.cost + h[u] - h[e.to]){
	  d[e.to] = d[u] + e.cost + h[u] - h[e.to];
	  pv[e.to] = u; pe[e.to] = i;
	}
      }	  
    }

    if(d[t]==INF)return -1;
    rep(u,v)h[u] += d[u];

    int x = f;
    for(int u=t;u!=s;u=pv[u])x = min(x,G[pv[u]][pe[u]].cap);
    f -= x;
    res += x*h[t];
    for(int u=t;u!=s;u=pv[u]){
      edge &e = G[pv[u]][pe[u]];
      e.cap -= x; G[u][e.rev].cap += x;
    }
  }
  return res;
}


int main(){
  int n;
  cin >> n;
  int p[110];
  for(int i=0;i<n;i++)cin >> p[i];

  v = 2*n+2;
  for(int i=0;i<n;i++){
    AddEdge(0,i+1,0,1);
    AddEdge(i+n+1,v-1,0,1);
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(p[i]!=j+1){
	AddEdge(p[i],j+n+1,p[i]*abs(i-j),1);
      }
    }
  }

  cout << MinCostFlow(0,v-1,n) << endl;
}
### Prompt

Generate 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<cstring>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

#define rep(i,a) for(int i=0;i<(int)(a);i++)
#define INIT(a) memset(a,0,sizeof(a))
#define pb push_back
#define sz size()
#define INF (1<<25)
#define V 300
typedef pair<int,int> P;

struct edge{
  int from,to,cost,cap,rev;
};

vector<edge> G[V];

void AddEdge(int s,int g,int c,int p){
  G[s].pb((edge){s,g,c,p,(int)G[g].sz});
  G[g].pb((edge){g,s,-c,0,(int)G[s].sz-1});
}

int d[V];
int h[V],pv[V],pe[V];
int v;
bool use[V];

int MinCostFlow(int s,int t,int f){
  int res = 0;
  fill(h,h+v,0);
  while(f>0){
    fill(d,d+v,INF);
    memset(use,0,sizeof(use));
    d[s] = 0;

    while(true){
      int u = -1;
      rep(i,v){
	if(!use[i] && (u<0 || d[u] > d[i]))u = i;
      } 

      if(u<0 || d[u] == INF)break;
      use[u] = true;

      rep(i,G[u].sz){
	edge &e = G[u][i];
	if(e.cap>0 && d[e.to] > d[u] + e.cost + h[u] - h[e.to]){
	  d[e.to] = d[u] + e.cost + h[u] - h[e.to];
	  pv[e.to] = u; pe[e.to] = i;
	}
      }	  
    }

    if(d[t]==INF)return -1;
    rep(u,v)h[u] += d[u];

    int x = f;
    for(int u=t;u!=s;u=pv[u])x = min(x,G[pv[u]][pe[u]].cap);
    f -= x;
    res += x*h[t];
    for(int u=t;u!=s;u=pv[u]){
      edge &e = G[pv[u]][pe[u]];
      e.cap -= x; G[u][e.rev].cap += x;
    }
  }
  return res;
}


int main(){
  int n;
  cin >> n;
  int p[110];
  for(int i=0;i<n;i++)cin >> p[i];

  v = 2*n+2;
  for(int i=0;i<n;i++){
    AddEdge(0,i+1,0,1);
    AddEdge(i+n+1,v-1,0,1);
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(p[i]!=j+1){
	AddEdge(p[i],j+n+1,p[i]*abs(i-j),1);
      }
    }
  }

  cout << MinCostFlow(0,v-1,n) << endl;
}
```