n = int(input())
a = list(map(int,input().split(' ')))
b = list(range(0,n))

changed = True
ans = 0
while changed:
    changed=False
    for k in range(0,n-1):
        if b[k]<=k and k+1<=b[k+1]:
            ans+=a[b[k]]+a[b[k+1]]
            b[k],b[k+1]=b[k+1],b[k]
            changed=True
        elif k<=b[k] and k+1<=b[k+1] and a[b[k]]<a[b[k+1]]:
            ans+=a[b[k+1]]-a[b[k]]
            b[k],b[k+1]=b[k+1],b[k]
            changed=True
        elif b[k]<=k and b[k+1]<=k+1 and a[b[k]]>a[b[k+1]]:
            ans+=a[b[k]]-a[b[k+1]]
            b[k],b[k+1]=b[k+1],b[k]
            changed=True
print(ans)