n = int(input())
A = [int(_) for _ in input().split()]

d = {}
for i in range(n):
    d[i+1] = (A[i], i)
L = [i+1 for i in range(n)]
print(L)
flag = 0
ans = 0
while flag == 0:
    flag = 1
    for i in range(n-1):
        res = 0
        if i >= d[L[i]][1]:
            res += d[L[i]][0]
        else:
            res -= d[L[i]][0]
        if i+1 <= d[L[i+1]][1]:
            res += d[L[i+1]][0]
        else:
            res -= d[L[i+1]][0]
        if res > 0:
            t = L[i]
            L[i] = L[i+1]
            L[i+1] = t
            ans += res
            flag = 0
            # print(L)
print(ans)