n=int(input())
a=list(map(int,input().split()))
count=0
while len(a)>=2:
    if a[0]>a[1]:
        count+=a[0]-a[1]
        w=a[0]
        a.pop(0)
        a[0]=w
    else:
        a.pop(0)
print(count)