import numpy as np
N = int(input())
A = np.array([int(x) for x in input().split()])
B = range(N)
left = 0
right = N-1
score = 0
for i in range(N-1):
  val = np.max(A)
  pos = B[np.argmax(A)]
  if (pos-left)>(right-pos):
    score+=val*(pos-left)
    left +=1
  else:
    score+=val*(right-pos)
    right -=1
  A=np.delete(A, pos)
  B=np.delete(B, pos)
print(score)