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

bn = [-1]*n
left = 0
right = n-1
an_ = [(an[i], i) for i in range(n)]
an_ = sorted(an_, key=lambda x:x[0], reverse=True)
# print(an_)

for a in an_:
    if abs(a[1]-left) > abs(a[1]-right):
        bn[left] = a[1]
        left += 1
    else:
        bn[right] = a[1]
        right -= 1

res = 0
for i in range(len(bn)):
    b = bn[i]
    res += an[b]*abs(b-i)
print(res)