n = int(input())
a_list = list(map(int,input().split()))
idx_list = [i for i in range(n)]
c = sorted(zip(a_list, idx_list),reverse=True)

start = 0
end = n-1
count = 0

for (num,idx) in c:
    # idx = a_list.index(num)
    if abs(idx - start) > abs(idx - end):
        count += num*abs(idx - start)
        # print("a:",idx,num,"_",start)
        start += 1
    elif abs(idx - start) < abs(idx - end):
        count += num*abs(idx - end)
        # print("b:",idx,num,"_",end)
        end -= 1
    else:
        if start > n-1-end:
            count += num*abs(idx - end)
            # print("c:",idx,num,"_",end)
            end -= 1
        else:
            count += num*abs(idx - start)
            # print("d:",idx,num,"_",start)
            start += 1

    # print(num,count)

print(count)