#include<iostream>
#include<queue>
#include<cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

class MCF {
private:
  struct edge { int to, cap, cost, rev; };
  typedef pair<int, int> P;

  const static int INF = 1e9;

  int v;
  vector<vector<edge> > g;
  vector<int> h, dist, prevv, preve;

public:
  MCF() : v(0) {}

  int next() {
    g.push_back(vector<edge>());
    h.push_back(0);
    dist.push_back(0);
    prevv.push_back(0);
    preve.push_back(0);
    return v++;
  }

  void add_edge(int from, int to, int cap, int cost) {
    g[from].push_back((edge){to, cap, cost, (int)g[to].size()});
    g[to].push_back((edge){from, 0, -cost, (int)g[from].size() - 1});
  }

  int run(int s, int t, int f) {
    int res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P> > que;
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      que.push(P(0, s));
      while (!que.empty()) {
	P p = que.top(); que.pop();
	int v = p.second;
	if (dist[v] < p.first) continue;
	rep (i, g[v].size()) {
	  edge &e = g[v][i];
	  if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
	    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
	    prevv[e.to] = v;
	    preve[e.to] = i;
	    que.push(P(dist[e.to], e.to));
	  }
	}
      }
      if (dist[t] == INF) {
	return -1;
      }
      rep (i, v) h[i] += dist[i];
      int d = f;
      for (int i = t; i != s; i = prevv[i]) d = min(d, g[prevv[i]][preve[i]].cap);
      f -= d;
      res += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
	edge &e = g[prevv[i]][preve[i]];
	e.cap -= d;
	g[i][e.rev].cap += d;
      }
    }
    return res;
  }

};

int main() {
  int n;
  cin >> n;
  int p[n];
  rep (i, n) cin >> p[i];
  MCF mcf;
  int from = mcf.next();
  int to = mcf.next();
  int left[n], right[n];
  rep (i, n) left[i] = mcf.next();
  rep (i, n) right[i] = mcf.next();
  rep (i, n) mcf.add_edge(from, left[i], 1, 0);
  rep (i, n) mcf.add_edge(right[i], to, 1, 0);
  rep (i, n) rep (j, n) if (p[i] - 1 != j) mcf.add_edge(left[i], right[j], 1, p[i] * abs(i - j));
  cout << mcf.run(from, to, n) << endl;
}
### 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<queue>
#include<cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

class MCF {
private:
  struct edge { int to, cap, cost, rev; };
  typedef pair<int, int> P;

  const static int INF = 1e9;

  int v;
  vector<vector<edge> > g;
  vector<int> h, dist, prevv, preve;

public:
  MCF() : v(0) {}

  int next() {
    g.push_back(vector<edge>());
    h.push_back(0);
    dist.push_back(0);
    prevv.push_back(0);
    preve.push_back(0);
    return v++;
  }

  void add_edge(int from, int to, int cap, int cost) {
    g[from].push_back((edge){to, cap, cost, (int)g[to].size()});
    g[to].push_back((edge){from, 0, -cost, (int)g[from].size() - 1});
  }

  int run(int s, int t, int f) {
    int res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P> > que;
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      que.push(P(0, s));
      while (!que.empty()) {
	P p = que.top(); que.pop();
	int v = p.second;
	if (dist[v] < p.first) continue;
	rep (i, g[v].size()) {
	  edge &e = g[v][i];
	  if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
	    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
	    prevv[e.to] = v;
	    preve[e.to] = i;
	    que.push(P(dist[e.to], e.to));
	  }
	}
      }
      if (dist[t] == INF) {
	return -1;
      }
      rep (i, v) h[i] += dist[i];
      int d = f;
      for (int i = t; i != s; i = prevv[i]) d = min(d, g[prevv[i]][preve[i]].cap);
      f -= d;
      res += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
	edge &e = g[prevv[i]][preve[i]];
	e.cap -= d;
	g[i][e.rev].cap += d;
      }
    }
    return res;
  }

};

int main() {
  int n;
  cin >> n;
  int p[n];
  rep (i, n) cin >> p[i];
  MCF mcf;
  int from = mcf.next();
  int to = mcf.next();
  int left[n], right[n];
  rep (i, n) left[i] = mcf.next();
  rep (i, n) right[i] = mcf.next();
  rep (i, n) mcf.add_edge(from, left[i], 1, 0);
  rep (i, n) mcf.add_edge(right[i], to, 1, 0);
  rep (i, n) rep (j, n) if (p[i] - 1 != j) mcf.add_edge(left[i], right[j], 1, p[i] * abs(i - j));
  cout << mcf.run(from, to, n) << endl;
}
```