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

ls = [[i,A[i]] for i in range(N)]
sorted_ls = sorted(ls, key=lambda x: x[1], reverse=True)
moved = [-1 for i in range(N)]

r = N-1
l = 0
ans = 0
for x,ax in sorted_ls:
  if x-l < r-x:
    moved[r] = x
    ans += ax * (r-x)
    r -= 1
  else:
    moved[l] = x
    ans += ax * (x-l)
    l +=1
print(ans)
  