import numpy as np
N = int(input())
A = list(map(int,input().split()))
A = [[a,i] for a,i in zip(A,range(N))]
now = 0
ans = 0
while now!=N:
  score = A[now][0] * abs(A[now][1]-now)
  move_l = np.array([0]*N)
  for i in range(now+1,N):
    move_l[i] = A[now][0] * abs(A[now][1]-i) - score
  
  for i,a in enumerate(A[now+1:],now+1):
    score = (a[0] * abs(a[1] - (i - 1))) - (a[0] * abs(a[1] - i)) 
    move_l[i:] += score
  ma = np.argmax(move_l)
  if ma<=0:
    now+=1
  else:
    ans += move_l[ma]
    tmp = A.pop(now)
    A.insert(ma,tmp)
    
          
print(ans)