#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF=1e9+7;
struct edge{
    int to, cap, cost, rev;
};

int V;
vector<edge> g[202];
int h[202];
int dist[202];
int prevv[202], preve[202];

void add_edge(int from, int to, int cap, int cost){
    edge e;
    e.to=to, e.cap=cap, e.cost=cost, e.rev=g[to].size();
    g[from].push_back(e);
    e.to=from, e.cap=0, e.cost=-cost, e.rev=g[from].size()-1;
    g[to].push_back(e);
}
  
int min_cost_flow(int s, int t, int f){
    int res=0;
    fill(h, h+V, 0);
    while(f>0){
        priority_queue<P, vector<P>, greater<P>> que;
        fill(dist, dist+V, 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;
            for(int i=0; i<g[v].size(); i++){
                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));
                }
            }
        }
        for(int v=0; v<V; v++) h[v]+=dist[v];
        if(dist[t]==INF) return -1;
        int d=f;
        for(int v=t; v!=s; v=prevv[v]){
            d=min(d, g[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[t];
        for(int v=t; v!=s; v=prevv[v]){
            edge &e=g[prevv[v]][preve[v]];
            e.cap-=d;
            g[v][e.rev].cap+=d;
        }
    }
    return res;
}

int main()
{
    int n; cin>>n;
    int ind[101];
    for(int i=0; i<n; i++){
        int p; cin>>p; p--;
        ind[p]=i;
    }
    V=2*n+2;
    int s=V-1, t=V-2;
    for(int i=0; i<n; i++){
        add_edge(s, i, 1, 0);
        add_edge(i+n, t, 1, 0);
        for(int j=0; j<n; j++){
            if(i==j) continue;
            add_edge(i, j+n, 1, abs(ind[i]-j)*(i+1));
        }
    }
    cout<<min_cost_flow(s, t, n)<<endl;
    return 0;
}
