N = int(eval(input()))

A = input().split()

count = 0

for i in range(N-1):

  if int(A[i]) > int(A[i+1]):

    count += int(A[i]) - int(A[i+1])

    A[i+1] = int(A[i])



print(count)
N = int(eval(input()))

A = input().split()

count = 0



for i in range(N):

  A[i] = int(A[i])

for i in range(N-1):

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

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

    A[i+1] = A[i]



print(count)
N = int(eval(input()))
A = input().split()
count = 0
for i in range(N - 1):
    if int(A[i]) > int(A[i + 1]):
        count += int(A[i]) - int(A[i + 1])
        A[i + 1] = int(A[i])
print(count)

N = int(eval(input()))
A = input().split()
count = 0
for i in range(N):
    A[i] = int(A[i])
for i in range(N - 1):
    if A[i] > A[i + 1]:
        count += A[i] - A[i + 1]
        A[i + 1] = A[i]
print(count)

+for i in range(N): +    A[i] = int(A[i]) -    if int(A[i]) > int(A[i + 1]): -        count += int(A[i]) - int(A[i + 1]) -        A[i + 1] = int(A[i]) +    if A[i] > A[i + 1]: +        count += A[i] - A[i + 1] +        A[i + 1] = A[i]