N = int(input())
A = list(map(int,input().split()))
min = 0
M = [0] * N
for i in range(N) :
    for j in range(i):
        if M[i] > A[i]- A[j] :
            M[i] = A[i]- A[j]
    min = min + abs(M[i])
print(min)