# coding: utf-8

# Your code here!



# import math

# from decimal import Decimal

# from collections import deque, Counter

# from itertools import product





def LI():

    return list(map(int,input().split()))

    

def OPEN():

    return list(map(int,open(0).read().split()))

    



n,*a=OPEN()



now=a[0]

ans=[]

for i in range(n):

    if a[i]<now:

        ans+=(now-a[i]),

        a[i]=now

    now=a[i]

    

print((sum(ans)))

        
n,*a=list(map(int,open(0).read().split()))

now=a[0]

ans=0

for i in range(n):

  if a[i]<now:

    ans+=(now-a[i])

  now=max(a[i],now)

print(ans)

        

# coding: utf-8
# Your code here!
# import math
# from decimal import Decimal
# from collections import deque, Counter
# from itertools import product
def LI():
    return list(map(int, input().split()))


def OPEN():
    return list(map(int, open(0).read().split()))


n, *a = OPEN()
now = a[0]
ans = []
for i in range(n):
    if a[i] < now:
        ans += ((now - a[i]),)
        a[i] = now
    now = a[i]
print((sum(ans)))

n, *a = list(map(int, open(0).read().split()))
now = a[0]
ans = 0
for i in range(n):
    if a[i] < now:
        ans += now - a[i]
    now = max(a[i], now)
print(ans)

-# coding: utf-8 -# Your code here! -# import math -# from decimal import Decimal -# from collections import deque, Counter -# from itertools import product -def LI(): -    return list(map(int, input().split())) - - -def OPEN(): -    return list(map(int, open(0).read().split())) - - -n, *a = OPEN() +n, *a = list(map(int, open(0).read().split())) -ans = [] +ans = 0 -        ans += ((now - a[i]),) -        a[i] = now -    now = a[i] -print((sum(ans))) +        ans += now - a[i] +    now = max(a[i], now) +print(ans)