vfrom sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
###############################################################################\
n=int(input())
A=INPUT()
B=[A[0]]

currmax=A[0]
ans=0
for i in range(1,n):
    if A[i]<=currmax:
        ans+=currmax-A[i]
    currmax=max(currmax,A[i])
print(ans)
