#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

typedef int weight;
typedef vector<weight> Array;
typedef vector<Array> matrix;

weight hungarian(const matrix &a) {
  int n = a.size(), p, q;
  Array fx(n, INF), fy(n, 0);
  vector<int> x(n, -1), y(n, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      fx[i] = max(fx[i], a[i][j]);
  for (int i = 0; i < n; ) {
    vector<int> t(n, -1), s(n+1, i);
    for (p = q = 0; p <= q && x[i] < 0; ++p)
      for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)
        if (fx[k] + fy[j] == a[k][j] && t[j] < 0) {
          s[++q] = y[j], t[j] = k;
          if (s[q] < 0)
            for (p = j; p >= 0; j = p)
              y[j] = k = t[j], p = x[k], x[k] = j;
        }
    if (x[i] < 0) {
      weight d = INF;
      for (int k = 0; k <= q; ++k)
        for (int j = 0; j < n; ++j)
          if (t[j] < 0) d = min(d, fx[s[k]] + fy[j] - a[s[k]][j]);
      for (int j = 0; j < n; ++j) fy[j] += (t[j] < 0 ? 0 : d);
      for (int k = 0; k <= q; ++k) fx[s[k]] -= d;
    } else ++i;
  }
  weight ret = 0;
  for (int i = 0; i < n; ++i) ret += a[i][x[i]];
  return ret;
}

int main() {
  int n;
  while(cin >> n) {
    int p[n];
    REP(i,n) {
      cin >> p[i];
    }
    matrix a(n,Array(n));
    REP(i,n) {
      REP(j,n) {
        if (p[i]-1==j) a[i][j] = -INF;
        else {
          a[i][j] = -p[i]*abs(j-i);
        }
      }
    }
    cout << -hungarian(a) << endl;
  }
}
### Prompt

Create a solution in cpp for 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 REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

typedef int weight;
typedef vector<weight> Array;
typedef vector<Array> matrix;

weight hungarian(const matrix &a) {
  int n = a.size(), p, q;
  Array fx(n, INF), fy(n, 0);
  vector<int> x(n, -1), y(n, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      fx[i] = max(fx[i], a[i][j]);
  for (int i = 0; i < n; ) {
    vector<int> t(n, -1), s(n+1, i);
    for (p = q = 0; p <= q && x[i] < 0; ++p)
      for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)
        if (fx[k] + fy[j] == a[k][j] && t[j] < 0) {
          s[++q] = y[j], t[j] = k;
          if (s[q] < 0)
            for (p = j; p >= 0; j = p)
              y[j] = k = t[j], p = x[k], x[k] = j;
        }
    if (x[i] < 0) {
      weight d = INF;
      for (int k = 0; k <= q; ++k)
        for (int j = 0; j < n; ++j)
          if (t[j] < 0) d = min(d, fx[s[k]] + fy[j] - a[s[k]][j]);
      for (int j = 0; j < n; ++j) fy[j] += (t[j] < 0 ? 0 : d);
      for (int k = 0; k <= q; ++k) fx[s[k]] -= d;
    } else ++i;
  }
  weight ret = 0;
  for (int i = 0; i < n; ++i) ret += a[i][x[i]];
  return ret;
}

int main() {
  int n;
  while(cin >> n) {
    int p[n];
    REP(i,n) {
      cin >> p[i];
    }
    matrix a(n,Array(n));
    REP(i,n) {
      REP(j,n) {
        if (p[i]-1==j) a[i][j] = -INF;
        else {
          a[i][j] = -p[i]*abs(j-i);
        }
      }
    }
    cout << -hungarian(a) << endl;
  }
}
```