n = int(input())
a = list(map(int, input().split()))
pt = [0] * n

st = 0
ed = n - 1

ans = 0

for i in range(n):
    ax = max(a)
    p = a.index(ax)
    a[p] = 0
    if (abs(p - st) > abs(p - ed)):
        ans += ax * abs(p - st)
        st += 1
    else:
        ans += ax * abs(p-ed)
        ed -= 1
    #print("{} {} {}".format(ans,st,ed))
print(ans)