N = int(input())
A = list(map(int, input().split()))
print(type(A))

#current height
now = 0
total = 0

for a in A:
    print(type(a))
    if a >= now:
        now = a
        pass
    else:
        total += now - a

print(total)
