N = int(input())
A = list(map(int,input().split()))
pre = A[0]
ans = 0
for i in range(len(A)):
    now = A[i]
    if pre < now:
        ans += now - pre
        pre = now + (now-pre)
    else:
        pre = now
print(ans)
