n = int(input())
a = list(map(int,input().split()))
m = max(a)
s = 0

ind = a.index(m)
for i in range(n):

    if i == n-1:
#        print(i)
        break
    else:
        if a[i]>a[i+1] and i < ind:
            s += a[i]-a[i+1]
#            print(i,":",s)

        elif a[ind]>=a[i] and i >= ind:
            s += a[ind]-a[i+1]
#            print(i,":",s)
#        else:
#            print(i,a[i],a[i+1],":",s)
 
print(s)