peterverebics/CodeNet_Python_118 huggingface dataset:
def main():

    n = int(input())
    x = tuple([int(t)for t in input().split()])

    m = x[0]
    ans = 0
    for i in range(1,n):
        if m>x[i]:
            ans += m-x[i]
        else:
            m = x[i]
    
    print(ans)



if __name__ == "__main__":
    main()
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
  if a[i - 1] > a[i]:
    step = abs(a[i] - a[i - 1])
    ans += step
    a[i] = a[i - 1]
print(ans)
N = int(input())
A = list(map(int, input().split()))
S = 0
h = A[0]

for i in range(1, len(A)):
  if A[i] < h:
    S += (h - A[i])
    h = max(h, A[i-1])
  else:
    h = A[i]

print(S)
n = int(input())
lst = list(map(int,input().split()))

mx = lst[0]
if n == 1:
  print(0)
else:
  s =0
  for i in range(1,n):
    if lst[i] < mx:
      s += mx -lst[i]
    if lst[i] > mx:  
      mx = lst[i]
      
  print(s)
n,*a=map(int,open(0).read().split())
z=a[0]
b=0
for q in a:
  if z>q:
    b+=(z-q)
  z=max(z,q)
print(b)
N = int(input())
A = list(map(int,input().split()))
maxnow = A[0]
fumidaisum = 0
for i in range(1,N):
    if A[i]<maxnow:
        fumidaisum += -A[i]+maxnow
    elif A[i]>maxnow:
        maxnow = A[i]

print(fumidaisum)
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
for i in range(1,n):
    if a[i] < a[i-1]:
        b[i] = a[i-1] - a[i]
        a[i] = a[i-1]
print(sum(b))
n=int(input())
A=list(map(int,input().split()))
step=0
for i in range(n-1):
    if A[i+1]<A[i]:
        step+=A[i]-A[i+1]
        A[i+1]=A[i]
print(step)
n = int(input())
a = list(map(int, input().split()))

tmp = 0
sum = 0
for i in a:
    if tmp > i:
        dif = tmp - i
        sum += dif
    else:
        tmp = i

print(sum)
n = int(input())
a = list(map(int, input().split(" ")))

sum_step = 0
max_height = a[0]
for i in range(1, n):
    if a[i] > max_height:
        max_height = a[i]
    if a[i] < max_height:
        sum_step += max_height - a[i]

print(sum_step)
n = int(input())
a = list(map(int, input().split()))
mae=a[0]
sum=0
for i in range(len(a)-1):
  if a[i+1]<mae:
    sum=sum+(mae-a[i+1])
  else:
    mae=a[i+1]
print(sum)
N=int(input())
A=list(map(int, input().split()))
ans=0
M=A[0]
for i in range(1,N):
    if A[i]>M:
        M=A[i]
    else:
        ans+=M-A[i]
print(ans)
n = int(input())
a = list(map(int, input().split()))

ans = 0
x = a[0]
for i in range(1, n):
    ans += max(x - a[i], 0)
    x = max(a[i], x)
print(ans)
N=int(input())
Alist=list(map(int,input().split()))
sum=0
if N==1:
  print(0)
else:
  for i in range(1,N):
    if Alist[i]<=Alist[i-1]:
      sum=sum+Alist[i-1]-Alist[i]
      Alist[i]=Alist[i-1]
  print(sum)
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
#if l[i] == l[j]: continue
n = int(input())
a = list(map(int, input().split()))
ans = 0
num = a[0]

for i in range(n-1):
    if num < a[i+1]:
        num = a[i+1]

    else:
        ans += num - a[i+1]

print(ans)
n = int(input())
a = list(map(int, input().split()))
ans = 0
high = a[0]
for i in range(1, n):
    if a[i] < high:
        ans += high - a[i]
    else:
        high = a[i]
print(ans)
def main():
    n = int(input())
    A = list(map(int, input().split()))
    s = 0
    ans = 0

    for a in A:
        if a > s:
            s = a
        else:
            ans += s - a

    print(ans)


if __name__ == '__main__':
    main()
n=int(input())
a_list = list(map(int,input().split()))

a_tmp = a_list[0]

sum = 0
for i in range(1, len(a_list)):
    if a_tmp > a_list[i]:
        sum += a_tmp - a_list[i]
    else:
        a_tmp = a_list[i]

print(sum)
n=int(input())
a=list(map(int,input().split()))
maxi=a[0]
ans=0
for i in range(n):
    if a[i]<maxi:
        ans+=maxi-a[i]
    else:
        maxi=a[i]
print(ans)
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(1, N):
    if A[i] > A[i-1]:
        continue
    else:
        ans += A[i-1] - A[i]
        A[i] = A[i-1]
print(ans)
n = int(input())
#a, b = map(int,input().split())
a = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
ans=0
for i in range(1,n):
    if a[i-1]<=a[i]:
        pass
    else:
        dif=a[i-1]-a[i]
        ans+=dif
        a[i]=a[i]+dif

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

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

Sum = 0

for i in range(N-1):
    if A[i] > A[i+1]:
        Sum += A[i] - A[i+1]
        A[i+1] = A[i]
        
print(Sum)
N = int(input())
A = list(map(int,input().split( )))
m = A[0]
ans = 0
for Ai in A[1:]:
    if Ai <= m:
        ans += (m - Ai)
    else:
        m = Ai
print(ans)
n = int(input())
arr = list(map(int, input().split()))
cnt = 0

for i in range(1, n):
  ai = arr[i]
  ai_ = arr[i-1]
  if ai >= ai_:
    pass
  else:
    cnt += ai_ - ai
    arr[i] = ai_
    
print(cnt)
n=int(input())
a=list(map(int,input().split()))

ans = 0

for i in range(n-1):

    if a[i] > a[i+1]:
        
        stand = a[i]-a[i+1]
        a[i+1] = a[i]
        ans += stand

print(ans)
n=int(input())
li = list(map(int,input().split()))	
ans=0
for i in range(n-1):
    if li[i]<=li[i+1]:
        pass
    else:
        ans+=li[i]-li[i+1]
        li[i+1]=li[i]
print(ans)
n=int(input())
a=list(map(int,input().split()))
b=0
for i in range(n-1):
  if a[i]>a[i+1]:
    b+=a[i]-a[i+1]
    a[i+1]=a[i]
print(b)
def solve(string):
    _, *a = map(int, string.split())
    ans = step = 0
    for a0, a1 in zip(a, a[1:]):
        step = max(0, a0 + step - a1)
        ans += step
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))
#c
n = int(input())
a = list(map(int, input().split()))
i = 1
ans = 0
before = a[0]
while i < len(a):
    if before > a[i]:
        ans += before - a[i]
    before = max(before,a[i])
    i+=1
print(ans)
#C
N=int(input())
A = list(map(int, input().split()))
ans=0
a=A[0]
for i in range(1,len(A)):
    if A[i]-A[i-1]<0:
        ans+=A[i-1]-A[i]
        A[i]=A[i-1]
print(ans)
import sys

n = int(input())
r = list(map(int,input().split()))
ans = 0

for i in range(len(r)-1):
    if r[i]>r[i+1]:
        ans += r[i]-r[i+1]
        r[i+1] = r[i]

print(ans)
n = int(input())
a = [int(s) for s in input().split()]

t=a[0]
total=0

for i in range(n):
  if t>a[i]:
    total=total+t-a[i]
  else:
    t=a[i]
    
print(total)
n = int(input())
a = list(map(int, input().split()))

sum = 0
max = 0
for i in a:
  if i >= max:
    max = i
  else:
    sum += max - i

print(sum)
N = int(input())

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

num_before = A[0]
dai_sum = 0
for num in A:
    if num_before > num:
        dai_sum += num_before - num
    else:
        num_before = num
print(dai_sum)
n = int(input())
a = list(map(int, input().split()))
prev = a[0]
dai = 0
for i in range(n-1):
  if a[i+1] >= prev:
    prev = a[i+1]
  else:
    dai += prev - a[i+1]
print(dai)
import copy

N = int(input())
A = list(map(int, input().split()))
A2 = copy.copy(A)
B = [A[0]]

for i in range(N-1):
  if A[i+1] < A[i]:
    B.append(A[i])
    A[i+1] = A[i]
  else:
    B.append(A[i+1])
ans = 0
for i in range(N):
  ans += B[i] -A2[i]
print(ans)
import sys
input = sys.stdin.buffer.readline

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

res = 0

for i in range(1,n):
    if a[i] >= a[i-1]:
        continue
    res += a[i-1] - a[i]
    a[i] = a[i-1]

print(res)
n = int(input())
arr = list(map(int,input().split()))
ans = 0
for i in range(1,n):

    if arr[i]<arr[i-1]:
        ans += arr[i-1]-arr[i]
        arr[i] += arr[i-1]-arr[i]
    
print(ans)
n = int(input())
a = [int(x) for x in input().split()]

base = a[0]
ans = 0;

for i in range(1,n):
  if a[i] <= base:
    ans += base - a[i]
  else:
    base = a[i]

print(ans)
N = int(input())
A = list(map(int,input().split()))

ans = 0

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

print(ans)
N = int(input())
A = [int(i) for i in input().split()]
h = A[0]
res = 0 #sum
for i in range(N):
    if A[i] >= h:
        h = A[i]
    else:
        res += h - A[i]
        
print(res)
n=int(input())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(n-1):
  d=a_list[i]-a_list[i+1]
  if(d>=0):
    b_list.append(d)
    a_list[i+1]+=d
    
ans=sum(b_list)
print(ans)
n = int(input())
row = list(map(int,input().split()))

a = row[0]
b =0
fumi = 0
for i in row:
  b = i
  if a <= b:
    a = b
    next
  else:
    fumi += (a - b)
    a = max(a, b)
print(fumi)
n = input()
a = [int(i) for i in input().split()]

maxer = a[0]
sumer = 0
for i in a:
    if i <= maxer:
        sumer += maxer - i
    else:
        maxer = i

print(sumer)
N = int(input())
A = list(map(int, input().split()))
ans = 0
M = -1

for i in range(N):
  M = max([M, A[i]])
  if A[i] < M:
    ans += M - A[i]
print(ans)
N = int(input())
A = list(map(int, input().split()))

sumF = 0
maxa = A[0]
fumidai = 0
for i in range(N-1):
  if A[i] > A[i+1]:
    sumF += A[i] - A[i+1]
    A[i+1] = A[i]
print(sumF)
N = int(input())
A = list(map(int,input().split()))

keep = 0
cnt =0
x=0


for a in A:
    if keep > a:
        cnt += keep -a
        x = keep -a
        keep = a + x
    else:
      keep = a
    
    
print(cnt)
import sys

def main():

	N = int(input())
	A = list(map(int, input().split()))
	H = 0
	max = A[0]
	for i in range(N-1):
		if max>A[i+1]:
			H = H + max - A[i+1]
		if max < A[i+1]:
			max = A[i+1]
	print(H)

if __name__ == "__main__":
    main()
n = int(input())
a_list = list(map(int, input().split()))
ans = 0

for i in range(1, n):
    diff = a_list[i - 1] - a_list[i]
    if diff > 0:
        a_list[i] += diff
        ans += diff

print(ans)
n = int(input())
l = list(map(int,input().split()))

h = 0
sum = 0
for i in range(n):
    if l[i] <= h:
        sum += h - l[i]
    else:
        h = l[i]

print(sum)
def main():
    N = int(input())
    A = [int(c) for c in input().split()]

    m = A[0]
    ans = 0
    for i, a in enumerate(A[1:]):
        m = max(m, a)
        if m > a:
            ans += m - a
    print(ans)

if __name__ == "__main__":
    main()
n = int(input())
l = list(map(int,input().split()))
p=l[0]
s=0
for i in l[1:]:
    s+= max(0,p-i)
    p=max(p,i)
print(s)
n = int(input())
a = list(map(int, input().strip().split()))
ans = 0
for i in range(1, n):
    if(a[i-1] > a[i]):
        ans += abs(a[i-1]-a[i])
        a[i] += abs(a[i-1]-a[i])
print(ans)
n = int(input())
A = list(map(int,input().split()))
step = [0]
for k,i in enumerate(A[1:]):
    if A[k] >= i:
        step.append(A[k] - i)
        A[k+1] = A[k+1] + A[k] - i
print(sum(step))
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
import copy
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)

n = int(input())
A = list(map(int, input().split()))
s = 0
for i in range(1, n):
    if A[i-1] > A[i]:
        s += A[i - 1] - A[i]
        A[i] += A[i-1] - A[i]

print(s)
n = int(input())
a = [int(x) for x in input().split(" ")]
s = [0]

for x in range(1, n):
    stool = max(a[x - 1] - a[x], 0)
    s.append(stool)
    a[x] += stool
    
print(sum(s))
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, -1, mod)


def main():
    n =int(input())
    a = list(map(int, input().split()))
    x = a[0]
    ans = 0
    for y in a[1:]:
        if x > y:
            ans += x - y
        x = max(x, y)
    print(ans)

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

ans = 0
maxA = 0
for a in A:
    if a > maxA:
        maxA = a
    else:
        ans += (maxA - a)
#    print(maxA)
print(ans)
n = int(input())
a = list(map(int, input().split()))

sum=0
diff=0
for i in range(1,n):
    diff=a[i-1]-a[i]
    
    if diff>=0:
         sum+=diff
         a[i]+=diff
   
    

print(sum)
N = int(input())
A = list(map(int, input().split()))

ans = 0
l = A[0]

for i in range(N-1):
  a = A[i+1]
  if a < l:
    ans += l - a
  else:
    l = a
    
print(ans)
N = int(input())
A = list(map(int, input().split()))

step = 0
for i in range(1, len(A)):
    if A[i-1] <= A[i]:
        continue
    else:
        step += (A[i-1] - A[i])
        A[i] += (A[i-1] - A[i])

print(step)
n = int(input())
A = list(map(int,input().split()))

now = A[0]
ans = 0
for a in A[1:]:
    if now > a:
        ans += now - a
    else:
        now = a
print(ans)
import math
t = int(input())
a = list(map(int,input().split()))
highest = a.index(max(a))
ans=0
for i in range(1,highest):
    if a[i]<a[i-1]:

        ans+= (a[i-1]-a[i])
        a[i] = a[i-1]
for i in range(highest+1,len(a)):
    if a[i]<a[i-1]:
        ans+=(a[i-1]-a[i])
        a[i]=a[i-1]
print(ans)
n = int(input())
a = list(map(int, input().split()))

step = 0
for i in range(1, n):
    if a[i-1] > a[i]:
        step += a[i-1]-a[i]
        a[i] = a[i-1]
print(step)
n = int(input())
a = list(map(int, input().split()))
x = a[0]
ans = 0
for i in range(1,n):
    if a[i] <= x:
        ans += x - a[i]
    else:
        x = a[i]
print(ans)
n = int(input())
A = list(map(int,input().split()))
ans = 0 
t = 0
for e,(i , j) in enumerate(zip(A,A[1:])):
    if i + t > j : 
        ans += t + i-j
        t= t + i-j
    else:t = 0
    # print(t,ans)
print(ans)
n=int(input())
alist=list(map(int, input().split()))
ans=0
num=alist[0]
for i in range(1,n):
  if alist[i]<num:
    ans+=(num-alist[i])
  else:
    num=alist[i]
print(ans)
N = int(input())
A = [int(i) for i in input().split()]

count = 0
for i in range(N) :
    if i==0 :
        continue
    if A[i]<A[i-1] :
        count += abs(A[i]-A[i-1])
        A[i] += abs(A[i]-A[i-1])

print(count)
N = int(input())
A = list(map(int,input().split()))
max = 0
ans = 0

for i in range(N):
  if max <= A[i]:
    max = A[i]
  else:
    ans += (max-A[i])
    
print(ans)
N = int(input())
A = list(map(int, input().split()))

max_ = 0
ans = 0

for a in A:
    max_ = max(a, max_)
    if a < max_:
        ans += (max_ - a)

print(ans)
def main():
    N = int(input())
    A = list(map(int,input().split()))
    
    high = 0
    ans = 0
    for i in range(N):
        if A[i] < high:
            ans += (high - A[i])
        else:
            high = A[i]
            
    print(ans)
    
if __name__ == '__main__':
    main()
import math

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    tmp=0
    Max=0
    for i in range(1,N):
        Max=max(Max,A[i-1])
        tmp+=max(0, Max - A[i])

    print(tmp)
resolve()
##main#######################################

## INPUT ####################################
N=input()
A = list(map(int,list(input().split())))

## PROCESSING ###############################
max=0
ans=0
for n in A:
	if max<n : max=n
	
	if max>n : ans=ans + max-n

## OUTPUT ###################################
#print("++++OUTPUT++++")
#print(A)
print(ans)
def main():
    N = int(input())
    A = list(map(int, input().split()))

    tallest = A[0]
    ans = 0

    for height in A:
        if height < tallest:
            ans += tallest - height
        elif height > tallest:
            tallest = height

    print(ans)


if __name__ == "__main__":
    main()
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(len(A)-1):
    if A[i] > A[i+1]:
        ans += A[i] - A[i+1]
        A[i+1] = A[i]
    else:
        continue

print(ans)
N = int(input())

data = input().split()

sum = 0

for i in range(N-1) :
    if(int(data[i]) > int(data[i+1])) :
        sum += int(data[i]) - int(data[i+1])
        data [i+1] = data[i]
        
print(sum)
n = int(input())
l = list(map(int,input().split()))

ans = 0

for i in range(n-1):
  if l[i+1]<l[i]:
    ans+= l[i]-l[i+1]
    l[i+1]=l[i]
    
print(ans)
n = int(input())

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

b = [0]
for i in range(n-1):
    s = a[i+1] - (a[i] + b[i])
    if s < 0:
        b.append(abs(s))
    else:
        b.append(0)

c = sum(b)
print(c)
N = int(input())
tall = list(map(int, input().split()))
step = [0]*N
sum_step = 0

for i in range(0, N-1):
    if tall[i]+step[i] > tall[i+1]:
        step[i+1] = tall[i] - tall[i+1] + step[i]
        sum_step += step[i+1]
        
print(sum_step)
n=int(input())
a=list(map(int, input().split()))

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

current = a[0]

sum_ = 0

for aa in a:
    if current <= aa:
        current = aa
    else:
        sum_ += current - aa


print(sum_)
N = int(input())
A = list(map(int, input().split()))

height = A[0]
cnt = 0
for a in A:
    if a < height:
        cnt += (height - a)
    else:
        height = a
print(cnt)
n = int(input())
a_L = list(map(int,input().split()))


if len(a_L) == 1:
    print(0)
    exit()

ans = 0
for i in range(0,len(a_L)-1):
    if a_L[i] > a_L[i+1]:
        ans += abs(a_L[i+1] - a_L[i])
        a_L[i+1] = a_L[i]


if a_L[-2] > a_L[-1]:
    ans += abs(a_L[-1]-a_L[-2])
print(ans)
n = int(input())
*arr, = map(int, input().split())


mx = arr[0]
i = 1
ans = 0
while i < n:
    if arr[i] < mx:
        ans += mx - arr[i]
    mx = max(mx, arr[i])
    i += 1
print(ans)
N = input()
A = list(map(int, input().split()))

ans = 0
cmax = A[0]
for a in A[1:]:
    if a < cmax:
        ans += cmax - a
    else:
        cmax = a
print(ans)
N = int(input())
a = list(map(int,input().split()))
ans = sum(a)

for i in range(N-1):
  if a[i]>a[i+1]:
    a[i+1]=a[i]
ans_1 = sum(a)
print(ans_1-ans)
n = int(input())
a = list(map(int,input().split()))
now = a[0]
ans = 0
for i in range(n):
    if a[i] < now:
        ans += now - a[i]
    elif a[i] > now:
        now = a[i]
print(ans)
N = int(input())
A = [int(i) for i in input().split()]
humidais = [0]
for i in range(N-1):
    humidais.append(max(A[i]+humidais[i]-A[i+1], 0))

print(sum(humidais))
n = int(input())
a = list(map(int,input().split()))
s = 0
now = 0
for i in a:
    if i > now:
        now = i
    else:
        s += now - i
print(s)
N = int(input())
A = list(map(int, input().split()))

prevMax = A[0]
ans = 0
for i in range(1, N):
    prevMax = max(A[i-1], prevMax)
    if A[i] < prevMax :
        ans += prevMax - A[i]

print(ans)
n=int(input())
A=list(map(int,input().split()))
ans=0
ma=A[0]
for i in range(1,n):
  if ma<A[i]:
    ma=A[i]
  else:
    ans+=(ma-A[i])
print(ans)
N = int(input())
A = [int(x) for x in input().split()]

c = 0
p = 0
for a in A:
  if p < a:
    p = a
  else:
    c += p - a
    p = max(p, a)
print(c)
N = int(input())
high = list(map(int, input().split()))
dai = []
est = high[0]
for i in range(N):
  if est > high[i]:
    
    
    dai.append(est-high[i])
  elif est <= high[i]:
    est = high[i]
print(sum(dai))
N = int(input())
A = list(map(int, input().split()))

lim = 0
ans = 0
for a in A:
  ans += max(0, lim - a)
  lim = max(lim, a)
  #print(lim, a, ans)
  
print(ans)
N=int(input())
re=0

A=list(map(int,input().split()))
co=A[0]
for i in range(N-1):
  s=A[i+1]-co
  if s<0:
    re=re-s
  else:
    co=A[i+1]
print(re)
# from pprint import pprint
# import math
# import collections

n = int(input())
# n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

now_high = a[0]
fumidai = 0

for _a in a:
    if now_high > _a:
        fumidai += now_high - _a
    else:
        now_high = _a

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

print(count)
N = int(input())
sin_list = list(map(int, input().split()))
max_height = 0
total_additional_height = 0

for sin in sin_list:
  if max_height == 0:
    max_height = sin
  else:
    if sin >= max_height:
      max_height = sin
    else:
      total_additional_height += (max_height - sin)

print(total_additional_height)
N = int(input())
A = map(int, input().split())

ma = 0
ans = 0
for a in A:
    ans -= max(0, ma-a)
    ma = max(ma, a)

print(-ans)
n=int(input())
a=list(map(int, input().split()))

ans=0
flag=a[0]

for i in range(len(a)):
    if a[i] < flag:
        ans+=flag-a[i]
    else:
        flag = a[i]
    
print(ans)
N = int(input())
arr = list(map(int,input().split(" ")))
res = 0

for i in range(1,N) :
    if arr[i] < arr[i-1]:
        res += arr[i-1]-arr[i]
        arr[i] = arr[i-1]

print(res)
import sys
cin = sys.stdin.readline
n = int(cin())
height = cin().split()
last, ans = 0, 0
for i in height:
    diff = last - int(i)
    if diff > 0:
        ans += diff
        continue
    last = int(i)

print(ans)
# cook your dish here
n=int(input())
ans=0
v=list(map(int,input().split()))

for i in range(1,n):
    if v[i]<v[i-1]:
        ans+=(v[i-1]-v[i])
        v[i]=v[i-1]
        
print(ans)
N = int(input())
A = list(map(int,input().split()))

ans = 0
max_num = A[0]
for i in range(1,N):
    ans += max(max_num - A[i],0)
    max_num = max(A[i],max_num)

print(ans)
n = int(input())
a = list(map(int, input().split()))
max_a_list = [0]*n
max_a_list[0] = a[0]

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

humidai = 0

for i in range(1,N):
  if A[i-1] > A[i]:
    humidai_plus = (A[i-1] - A[i])
    A[i] += humidai_plus
    humidai += humidai_plus
    
print(humidai)
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    H = 0
    ans = 0
    for a in A:
        if H > a:
            ans += H - a
        else:
            H = a
    print(ans)


if __name__ == '__main__':
    main()
N=int(input())
A=list(map(int,input().split()))
h=0
for i in range(1,N):
    a=A[i]-A[i-1]
    if a<0:
        h-=a
        A[i]-=a
print(h)
N = int(input())
A = list(map(int, input().split()))

before = 0
d = [0] * N
for i in range(N):
    if i == 0:
        before = i
        continue

    if (A[before] + d[before]) > A[i]:
        d[i] = (A[before] + d[before]) - A[i]
        before = i
    else:
        before = i

print(sum(d))
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(1, N):
    d = A[i - 1] - A[i]
    if d > 0:
        ans += d
        A[i] += d
print(ans)
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

ans = 0

if n==1:
	print(ans)
	exit()

for i in range(1,len(a)):
	t = max(a[i],a[i-1])
	ans += max(0, t-a[i])
	a[i] = t

print(ans)
n=int(input())
ppl=(input().split())
answer=0
if n==1:
    print(0)
for x in range(0,n-1):
    ppl[x]=int(ppl[x])
    ppl[x+1]=int(ppl[x+1])
    if ppl[x]>ppl[x+1]:
        answer+=ppl[x]-ppl[x+1]
        ppl[x+1]=ppl[x]
if(n!=1):
    print(answer)
N = int(input())
A = list(map(int,input().split()))
B = [0] * N
ans = 0
for i in range (N):
 if i == 0:
  B[0] = A[0]
 else:
  if B[i-1] > A[i]:
   B[i] = B[i-1]
   ans = ans + B[i-1] - A[i]
  else:
   B[i] = A[i]
   ans = ans
print (ans)
N = int(input())
A = list(map(int, input().split()))

h = 0
tot = 0

for a in A:
    tot += max(h-a, 0)
    h = max(h, a)
print(tot)