n = int(input())
a_L = list(map(int,input().split()))


ans = 0
for i in range(0,len(a_L)-1):
    if a_L[i] > a_L[i+1]:
        ans += abs(a_L[i+1] - a_L[i])
        a_L[i+1] = a_L[i]

if a_L[-1] > a_L[-2]:
    ans += abs(a_L[-1]-a_L[-2])
print(ans)