# -*- coding: utf-8 -*-

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#print("N",N)
#print("A_list",A_list)
#-----

A2_list = [None]*len(A_list)
A_val_dic = {}

for i,v in enumerate(A_list):
    A_val_dic.setdefault(v, [])
    A_val_dic[v].append(i)

#print("A_val_dic",A_val_dic)

sum = 0
for k in sorted(A_val_dic.keys(), reverse=True):
    
    for idx_A in A_val_dic[k]:
        L = A2_list.index(None)
        
        for i_A2 in range(len(A2_list)-1,0,-1):
            if A2_list[i_A2] == None:
                R = i_A2
                break
        
        if abs(L - idx_A) >= abs(R - idx_A):
            A2_list[L] = A_list[idx_A]
            sum += A_list[idx_A] * abs(L - idx_A)
        else:
            A2_list[R] = A_list[idx_A]
            sum += A_list[idx_A] * abs(R - idx_A)
    
    #print("sum",sum,"A2_list",A2_list)
    
#print("END sum",sum,"A2_list",A2_list)
print(sum)
