N = int(input())
A = list(map(int,input().split()))
flag = [1 for i in range(N)]
left = 0
right = N - 1
ans = 0
for i in range(N):
    index = 0
    MAX = 0
    for j in range(N):
        if flag[j] and MAX < A[j]:
            MAX = A[j]
            index = j
    if sum(A[:index]) < sum(A[index+1:]):
        ans += A[index] * abs(right - index)
        right -= 1
    else:
        ans += A[index] * abs(left - index)
        left += 1
    flag[index] = 0
print(ans)