import numpy as np
from scipy.optimize import linear_sum_assignment as lsa


N = int(input())
A = np.array(list(map(int,input().split())))

cost = np.arange(N).reshape(N,1)
cost = -np.abs(cost - cost.T)
cost[np.arange(N),np.arange(N)] = 1000000
cost *= A.reshape(N,1)

row_ind, col_ind = lsa(cost)

print(-cost[row_ind, col_ind].sum())

