n = int(input())
a = [(i, ai) for i, ai in enumerate(map(int, input().split()))]
i = 0
while True:
    if not i <= n - 2:
        break
    x, ax = a[i]
    y, ay = a[i+1]
    current = ax * abs(x - i) + ay * abs(y - i - 1)
    ifswap = ax * abs(x-i-1) + ay * abs(y-i)
    if current < ifswap:
        a[i], a[i+1] = a[i+1], a[i]
        if i > 0:
            i -= 1
    else:
        i += 1

ans = sum(ai * abs(i - i_orig)  for i, (i_orig, ai) in enumerate(a))
print(ans)


