N = int(input())
a_list = list(map(int,input().split(" ")))

index = list(range(N))
sorted_index = sorted(index,key = lambda i: -a_list[i])
sorted_a = [a_list[i] for i in sorted_index]
start = 0
end = N-1
sum = 0
for i in range(N):
    if abs(end - sorted_index[i]) > abs(sorted_index[i] -start):
        sum += sorted_a[i]*abs(end-sorted_index[i])
        end -=1
    else:
        sum += sorted_a[i]*abs(sorted_index[i]-start)
        start +=1
print(sum)
