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

import numpy as np
nd = np.array([np.linspace(1,n,n),ls_a])

nd = nd[:, np.argsort(nd[1])[::-1]]
sum_ = 0
left = 1
right = n

for i, a in zip(nd[0], nd[1]):
    # 左か右か
    l = i-left
    r = right-i
    if l <= r:
        right -= 1
        sum_ += a*abs(r)
        print(a, i, l, r, sum_,'if')
    else:
        left += 1
        sum_ += a*abs(l)
        print(a, i, l, r, sum_, 'else')
print(sum_)