n = int(input())
arr = list(map(int,input().split()))
children = [[i,arr[i]] for i in range(n)]
buf = [i for i in range(n)]

ans = 0
while True:
    dif = 0
    for i in range(n-1):
        l = buf[i]
        r = buf[i+1]
        il = children[l][0]
        ir = children[r][0]

        tp = -(children[l][1]*abs(il-i)+children[r][1]*abs(ir-(i+1))-children[l][1]*abs(il-(i+1))-children[r][1]*abs(ir-i))
        if tp > 0:
            buf[i],buf[i+1] = buf[i+1],buf[i]
            dif += tp
        else:
            continue
    if dif > 0:
        ans += dif
    else:
        break

print(ans)