n = int(eval(input()))

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

ans = 0

m = a[0]

for i in range(1, n):

    m = max(a[i-1], m)

    if a[i] - m < 0:

        ans += abs(a[i] - m)

print(ans)

n = int(eval(input()))

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

m = 1

ans = 0

for i in range(n):

    if m > a[i]:

        ans += m - a[i]

    m = max(a[i], m)

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
ans = 0
m = a[0]
for i in range(1, n):
    m = max(a[i - 1], m)
    if a[i] - m < 0:
        ans += abs(a[i] - m)
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
m = 1
ans = 0
for i in range(n):
    if m > a[i]:
        ans += m - a[i]
    m = max(a[i], m)
print(ans)

+m = 1 -m = a[0] -for i in range(1, n): -    m = max(a[i - 1], m) -    if a[i] - m < 0: -        ans += abs(a[i] - m) +for i in range(n): +    if m > a[i]: +        ans += m - a[i] +    m = max(a[i], m)