import sys
li  =   lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw  =   lambda : sys.stdin.readline().strip().split()
ni  =   lambda : int(sys.stdin.readline().strip())
nsi =   lambda : sys.stdin.readline().strip()

from collections import defaultdict as df
import math

n=ni()
l=list(map(int,input().split()))
mn=l[0]
ans=0
for i in range(1,n):
    if(mn<l[i]):
        mn=l[i]
        print(l[i])
    else:
        ans+=mn-l[i]
print(ans)
