N = int(input())
l = 0
r = N-1
A = list(map(int, input().split()))
for i in range(N):
  A[i] = (A[i], i)
ans = 0
#print(A)
for i in range(N):
  A[i:N] = sorted(A[i:N], key=lambda x:x[0]*max(abs(x[1]-r),abs(x[1]-l)), reverse=True)
  #print(A)
  a, x = A[i]
  if abs(x-r)>abs(x-l):
    ans += abs(x-r)*a
    r -= 1
  else:
    ans += abs(x-l)*a
    l += 1
print(ans)