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
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
int n,arr[101];
int cal(int a,int b){return (arr[a]+arr[b])*(abs(a-b));}
void Min(int &a,int b){a=min(a,b);}

int mem[3][101][101][101];
int dfs(int f,int num,int l,int r){
  if(l>r)return 0;
  if(l==r)return (arr[l]==l+1) ? INF:0;
  if(r-l==1)return (arr[l]==l+1||arr[r]==r+1)? cal(l,r):0;
  if(mem[f][num][l][r]!=-1)return mem[f][num][l][r];

  int res=INF;  
  for(int i=l+1;i<r;i++){
    if((f==0||f==1)&&arr[i]!=l+1){
      swap(arr[l],arr[i]);
      Min(res,cal(l,i)+dfs(0,0,l+1,i-1)+dfs(1,arr[i],i,r));
      swap(arr[l],arr[i]);
      }

    if((f==0||f==2)&&arr[i]!=r+1){
      swap(arr[i],arr[r]);
      Min(res,cal(i,r)+dfs(2,arr[i],l,i)+dfs(0,0,i+1,r-1));
      swap(arr[i],arr[r]);
    }
  }

  for(int i=l;i<r;i++){
    if(f==1)Min(res,dfs(1,num,l,i)+dfs(0,0,i+1,r));
    else if(f==2)Min(res,dfs(0,0,l,i)+dfs(2,num,i+1,r));
    else Min(res,dfs(0,0,l,i)+dfs(0,0,i+1,r));
  }

  return mem[f][num][l][r]=res;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++)cin>>arr[i];
  memset(mem,-1,sizeof(mem));
  cout<<dfs(0,0,0,n-1)<<endl;
}