n=int(eval(input()))



A = list(map(int, input().split()))

l = [0] * len(A)



ans=0



for i in range(n-1):

  if A[i]>=A[i+1]:

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

    A[i+1]=A[i]

  else:

    l[i+1]=0

  ans=ans+l[i+1]  

print(ans)  

n=int(eval(input()))



A = list(map(int, input().split()))

l = [0] * len(A)



ans=0

m=A[0]



for i in A:

  if m>i:

    ans = ans +(m-i)

  

  else:

    m=i

  

print(ans)  

n = int(eval(input()))
A = list(map(int, input().split()))
l = [0] * len(A)
ans = 0
for i in range(n - 1):
    if A[i] >= A[i + 1]:
        l[i + 1] = A[i] - A[i + 1]
        A[i + 1] = A[i]
    else:
        l[i + 1] = 0
    ans = ans + l[i + 1]
print(ans)

n = int(eval(input()))
A = list(map(int, input().split()))
l = [0] * len(A)
ans = 0
m = A[0]
for i in A:
    if m > i:
        ans = ans + (m - i)
    else:
        m = i
print(ans)

-for i in range(n - 1): -    if A[i] >= A[i + 1]: -        l[i + 1] = A[i] - A[i + 1] -        A[i + 1] = A[i] +m = A[0] +for i in A: +    if m > i: +        ans = ans + (m - i) -        l[i + 1] = 0 -    ans = ans + l[i + 1] +        m = i