#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct edge {
  int to, cap, cost, rev;
  edge(){}
  edge(int to, int cap, int cost, int rev)
    :to(to), cap(cap), cost(cost), rev(rev){}
};

using Graph = vector<vector<edge>>;

struct PrimalDual {
  Graph graph;
  vint mincost, h, prevv, preve;
  PrimalDual(int V):graph(V), mincost(V), h(V), prevv(V), preve(V){}
  void add_edge(int from, int to, int cap, int cost) {
    graph[from].emplace_back(to, cap, cost, graph[to].size());
    graph[to].emplace_back(from, 0, -cost, graph[from].size()-1);
  }
  int min_cost_flow(int s, int t, int f) {
    fill(all(h), 0);
    fill(all(prevv), -1);
    fill(all(preve), -1);
    int res = 0;
    while(f > 0) {
      fill(all(mincost), inf);
      priority_queue<Pi, vector<Pi>, greater<Pi>> que;
      que.emplace(0, s);
      mincost[s] = 0;
      while(!que.empty()) {
	Pi p = que.top(); que.pop();
	int v = p.second;
	if(mincost[v] < p.first) continue;
	rep(i, graph[v].size()) {
	  edge& e = graph[v][i];
	  int cost = mincost[v] + e.cost + h[v] - h[e.to];
	  if(e.cap > 0 && cost < mincost[e.to]) {
	    mincost[e.to] = cost;
	    que.emplace(cost, e.to);
	    prevv[e.to] = v;
	    preve[e.to] = i;
	  }
	}
      }
      if(mincost[t] == inf) return -1;
      rep(v, graph.size()) h[v] += mincost[v];
      int d = f;
      for(int v = t; v != s; v = prevv[v])
	chmin(d, graph[prevv[v]][preve[v]].cap);
      f -= d;
      res += d*h[t];
      for(int v = t; v != s; v = prevv[v]) {
	edge& e = graph[prevv[v]][preve[v]];
	e.cap -= d;
	graph[e.to][e.rev].cap += d;
      }
    }
    return res;
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n;
  cin >> n;
  vint p(n);
  rep(i, n) cin >> p[i];
  int s = n*2, t = s+1, V = t+1;
  PrimalDual graph(V);
  rep(i, n) {
    graph.add_edge(s, i, 1, 0);
    graph.add_edge(n+i, t, 1, 0);
  }
  rep(i, n) rep(j, n) {
    if(p[i]-1 == j) continue;
    int cost = p[i]*abs(i-j);
    graph.add_edge(i, n+j, 1, cost);
  }
  cout << graph.min_cost_flow(s, t, n) << 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 <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct edge {
  int to, cap, cost, rev;
  edge(){}
  edge(int to, int cap, int cost, int rev)
    :to(to), cap(cap), cost(cost), rev(rev){}
};

using Graph = vector<vector<edge>>;

struct PrimalDual {
  Graph graph;
  vint mincost, h, prevv, preve;
  PrimalDual(int V):graph(V), mincost(V), h(V), prevv(V), preve(V){}
  void add_edge(int from, int to, int cap, int cost) {
    graph[from].emplace_back(to, cap, cost, graph[to].size());
    graph[to].emplace_back(from, 0, -cost, graph[from].size()-1);
  }
  int min_cost_flow(int s, int t, int f) {
    fill(all(h), 0);
    fill(all(prevv), -1);
    fill(all(preve), -1);
    int res = 0;
    while(f > 0) {
      fill(all(mincost), inf);
      priority_queue<Pi, vector<Pi>, greater<Pi>> que;
      que.emplace(0, s);
      mincost[s] = 0;
      while(!que.empty()) {
	Pi p = que.top(); que.pop();
	int v = p.second;
	if(mincost[v] < p.first) continue;
	rep(i, graph[v].size()) {
	  edge& e = graph[v][i];
	  int cost = mincost[v] + e.cost + h[v] - h[e.to];
	  if(e.cap > 0 && cost < mincost[e.to]) {
	    mincost[e.to] = cost;
	    que.emplace(cost, e.to);
	    prevv[e.to] = v;
	    preve[e.to] = i;
	  }
	}
      }
      if(mincost[t] == inf) return -1;
      rep(v, graph.size()) h[v] += mincost[v];
      int d = f;
      for(int v = t; v != s; v = prevv[v])
	chmin(d, graph[prevv[v]][preve[v]].cap);
      f -= d;
      res += d*h[t];
      for(int v = t; v != s; v = prevv[v]) {
	edge& e = graph[prevv[v]][preve[v]];
	e.cap -= d;
	graph[e.to][e.rev].cap += d;
      }
    }
    return res;
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n;
  cin >> n;
  vint p(n);
  rep(i, n) cin >> p[i];
  int s = n*2, t = s+1, V = t+1;
  PrimalDual graph(V);
  rep(i, n) {
    graph.add_edge(s, i, 1, 0);
    graph.add_edge(n+i, t, 1, 0);
  }
  rep(i, n) rep(j, n) {
    if(p[i]-1 == j) continue;
    int cost = p[i]*abs(i-j);
    graph.add_edge(i, n+j, 1, cost);
  }
  cout << graph.min_cost_flow(s, t, n) << endl;

  return 0;
}
```