try:

    k = int(eval(input()))

    l = list(map(int,input().split(' ')))

    c = 0

    for i in range(len(l)-1):

      x = l[i+1]-l[i]

      if x<0:

        l[i+1]+=abs(x)

        c+=abs(x)

    print(c)

except:

    pass

def sol(lis):

    m = lis[0]

    ans = []

    for i in range(len(lis)):

        if lis[i]<m:

            ans.append(m-lis[i])

            lis[i] = m

        elif  lis[i]!=m:

            m = max(m,lis[i])

    print((sum(ans)))

eval(input())

lis = list(map(int,input().split(' ')))

sol(lis)

try:
    k = int(eval(input()))
    l = list(map(int, input().split(" ")))
    c = 0
    for i in range(len(l) - 1):
        x = l[i + 1] - l[i]
        if x < 0:
            l[i + 1] += abs(x)
            c += abs(x)
    print(c)
except:
    pass

def sol(lis):
    m = lis[0]
    ans = []
    for i in range(len(lis)):
        if lis[i] < m:
            ans.append(m - lis[i])
            lis[i] = m
        elif lis[i] != m:
            m = max(m, lis[i])
    print((sum(ans)))


eval(input())
lis = list(map(int, input().split(" ")))
sol(lis)

-try: -    k = int(eval(input())) -    l = list(map(int, input().split(" "))) -    c = 0 -    for i in range(len(l) - 1): -        x = l[i + 1] - l[i] -        if x < 0: -            l[i + 1] += abs(x) -            c += abs(x) -    print(c) -except: -    pass +def sol(lis): +    m = lis[0] +    ans = [] +    for i in range(len(lis)): +        if lis[i] < m: +            ans.append(m - lis[i]) +            lis[i] = m +        elif lis[i] != m: +            m = max(m, lis[i]) +    print((sum(ans))) + + +eval(input()) +lis = list(map(int, input().split(" "))) +sol(lis)