n = int(input())
activities = list(map(int,input().split()))

kids_list =[]

for i in range(n):
    kids_list.append([i, activities[i]]) 

left = 0
right = n-1

kids_list = sorted(kids_list, key = lambda x: x[1], reverse = True)
ans = 0
skip = 0

for i in range(n):
    target = kids_list[i][0]
    if target-left > right-target:
        ans += kids_list[i][1]*(target-left)
        left += 1
        if skip == 1:
            right -= 1
            skip = 0
    elif target-left < right-target:
        ans += kids_list[i][1]*(right-target)
        right -= 1
        if skip ==1:
            left +=1
            skip = 0
    else:
        ans += kids_list[i][1]*(right-target)
        if skip ==1:
            left += 1
            right -= 1
            skip = 0
        else:
            skip = 1

print(ans)