N = int(input())
A = list(map(int,(input().split())))
odr = [[0]*3 for _ in range(N)]
for i in range(N):
    odr[i][0] = A[i]
    odr[i][1] = i
    odr[i][2] = i #"現在地"


for _ in range(N):
    odr.sort(key=lambda x:x[2])
    for i in range(N-1):
        t = 0
        if odr[i][2] >= odr[i][1]:
            t += odr[i][0]
        else:
            t -= odr[i][0]
        if odr[i+1][2] <= odr[i+1][1]:
            t += odr[i+1][0]
        else:
            t -= odr[i+1][0]
        if t > 0:
            odr[i][2] += 1
            odr[i+1][2] -= 1

#print(odr)
ans = 0
for i in range(N):
    ans += abs(odr[i][1] - odr[i][2])*odr[i][0]
print(ans)