peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
def main(lines):
	line = lines  

if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)
n = int(input())
arr = list(map(int,input().split()))
ans = 0
for i in range(1,n):
    if arr[i]<arr[i-1]:
        arr[i] += arr[i-1]-arr[i]
        ans += arr[i-1]-arr[i]
print(ans)
lst = []
count = 0
n = int(input())
lst = list(map(int, input().split(' ')[:n]))
for j in range(len(lst)-1):
    if lst[j]<lst[j+1]:
        count = count + (lst[j+1]-lst[j])
    else:
        count = count - (lst[j+1]-lst[j])
print(count)
N = int(input())
A = list(map(int, input().split()))
print(type(A))

#current height
now = 0
total = 0

for a in A:
    print(type(a))
    if a >= now:
        now = a
        pass
    else:
        total += now - a

print(total)
# -*- coding: utf-8 -*-

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

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

print(diff_sum)
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]

print(ans)

if a_L[-2] > a_L[-1]:
    ans += abs(a_L[-1]-a_L[-2])
print(ans)
n = int(input())
a = [int(i) for i in input().split()]
ans=0
for i in range(n-1):
  d = a[i-1] - a[i]
  if d<0:
    ans+= d+1
    a[i]=a[i]+d+1
  else:
    continue
    
print(abs(ans))
n = int(input())
H = list(map(int, input().split()))
mh = 0
cnt = 0
for i in range(n - 1):
    mh = max(mh, H[i])
    if H[i] <= H[i + 1]:
        continue
    else:
        if H[i + 1] < H[i]:
            cnt += max(cnt, mh - H[i + 1])
print(cnt)
from sys import stdin


length = int(stdin.readline())
heights = stdin.readline().split(" ")

stool = 0
for i in range(1, length):
  if heights[i-1] > heights[i]:
    stool += (int(heights[i-1]) - int(heights[i]))
    heights[i] = heights[i-1]
print(int(stool))
import numpy as np
N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
max_ind = np.argmax(A)
min_array = np.full(max_ind, A[0])
max_array = np.full(len(A)-max_ind, max(A))
bin_array = np.hstack((min_array, max_array))
hikizan = bin_array - A

y = np.where(hikizan < 0)
if y != 0:
    hikizan[y] = 0
print(np.sum(hikizan))
N = int(input())
members = list(map(int, input().strip().split()))
stands = []

for i, member in enumerate(members):
  if i>0:
    if members[i] > members[i-1]:
      stands.append(0)
    else:
      stands.append(members[i-1] - members[i])
      
print(sum(stands))
n = int(input())
l = list(map(int,input().split()))
m = l[0]
s = 0
for x in range(1,n):
  if l[x-1]>l[x]:
    s+=(m - l[x])
  else:
    m = l[x]
print(s)
n = input()
m = input().split()
m = [int(i) for i in m]

mx = max(m)
ind = m.index(mx)

c = 0
j = len(m)
for i in reversed(m[ind:]):
    if i < mx:
        d = mx - i
        c += d
ind_i = 0
for i in m[1:ind]:
    if m[ind_i] > i:
        num = m[ind_i] - i
        c+= num
    ind_i += 1
    
print(c)
n = int(input())
a = list(map(int,input().split()))
hights = int()
for i in range(n-1):
    if(i == n):
        break
    if(a[i] > a[i+1]):
        hights =hights + a[i] - a[i+1]
#        a[i+1] =a[i+1] + (a[i] - a[i+1])
    else:
        continue
#print(a)
print(hights)
if __name__ == "__main__":
    n = int(input())
    x =list((int(x) for x in input().split()))
    print(x)
    
    tmp1 = 0
    tmp2 = x[0]
    for i in range(1,n):
        if tmp2 > x[i]:
            tmp3 = tmp2 - x[i]
            tmp1 += tmp3

            x[i] = x[i] + tmp3
            tmp2 = x[i]
        else:
            tmp1 += 0
            tmp2 = x[i]

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

ans=0

for i in range(1,n):
  diff=a[i]-a[i-1]
  if diff<0:
    ans+=-diff
    a[i]=a[i]-diff
n = int(input())
a = list(map(int, input().split()))
max = a[0]
sum = 0
count = 0
result = 0

for i in range(0,n):
  if max > a[i]:
    count+=1
    sum+=a[i]
  else:
    result += max*count-sum
    
print(result)
n = int(input())
inputs = input().split()
As = []
sum = 0
step =0
for i in inputs:
    As.append(int(i))
try:
    for i in range(len(As)):
        if As[i] < As[i + 1]:
            step = As[i + 1] - As[i]
            sum += step 
except:
    pass

print(sum)
n=int(input())
a=[int(x) for x in input().split()]
mx=0
mae=a[0]
for i in a:
    mx+=i-mae
    mae=i
print(mx)
N = int(input())
A = list(map(int,input().split()))
count=0
for i in range(N):
  try:
    a = A[i]
    b = A[i+1]
    if a <= b:
      pass
    else:
      c = abs(a-b)
      count+=c
  except:
    pass
print(count)
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    mi = 0
    le = len(a)
    cost = 0
    for i in range(le-1):
        if a[i] > a[i+1]:
            mi = max(a[i], mi)
            cost += mi - a[i+1]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()
n=int(input())
arr=list(map(int,input().split()))
prev=arr[0]
ma=0
for i in arr[1:]:
    if i>=prev:
        ma=max(ma,i-prev)
    prev=i
print(ma)
n = int(input())
a = list(map(int,input().split()))
res=0

if n==1:
	print(0)

for i in range(1,n):

	if a[i]<a[i-1]:
		res= ( a[i-1] - a[i] ) + res
		a[i] = a[i-1]

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

b=0
c=[]

for i in range(n-1):

    c.append(a[i])
    if a[i]>a[i+1]:
        b+=max(c)-a[i+1]

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

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

print(ans)
n = int(input())
a = input()
a = a.split()
count = 0

boo = False

for i in a:
    if int(i) >= n:
        boo = True
    else:
        count += 1

if(boo):
    print(count)
else:
    print(0)
n = int(input())
a = list(map(int, input().split()))

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

print(ans)
n=int(input())
a=list(map(int,input().split()))
m=0
ans=0
for i in range(n):
    if i==0:
        m=max(a[i],m)
    else:
        if a[i]<m:
            ans+=1
        else:
            m=a[i]
print(ans)
n = int(input())
a = list(map(int, input().split()))
max_int = max(a)
ans = 0
for i in range(n):
    if i == 0 and max_int:
        continue
    elif i < max_int:
        ans += 1

print(ans)
a=input()
b=input()
c=b.split()
d=map(int,c)
main=list(d)
sum=0
print(main)
for i in range(len(main)-2):
    stool=main[i]-main[i+1]
    if stool>0:
        main[i+1]+=stool
        sum+=stool

stool=main[len(main)-2]-main[len(main)-1]
if stool>0:
    sum+=stool
    main[len(main)-1]+=stool

print("sum = ",sum, "main = ",main)
n=int(input())
a=list(map(int,input().split()))
s=0
i=n
while i>1:
    maxi=max(a[0:i])
    s+=(i-a.index(maxi))*maxi
    i=a.index(maxi)
print(s-sum(a))
N=int(input())
A = list(map(int,input().split()))
target=A[0]
cost=0
for v in range(N-1):
    B=A[v+1]-A[v]
    if B>=0:
        target=A[v+1]
    elif B<0:
        cost+=target-A[v+1]
print(cost)
lst = []
count = 0
n = int(input())
lst = list(map(int, input().split(' ')[:n]))
for j in range(len(lst)-1):
    if lst[j]<=lst[j+1]:
        count = count + (lst[j+1]-lst[j])
    else:
        count = count + 0
print(count)
def c_step(n, ls):
    acc = 0
    for i in range(n - 1):
        if ls[i] > ls[i + 1]:
            stool = ls[i] - ls[i + 1]
            ls[i + 1] += stool
            acc += stool
    return acc
n=int(input())
a=[int(x) for x in input().split()]
mx=0
mae=a[0]
for i in a:
    if i-mae>mx:
        mx=i-mae
    mae=i
print(mx)
n=int(input())
a=list(map(int,input().split(" ")))
s=0
for x in range(1,n):
  s=s+max(0,a[x]-a[x-1])
print(s)
N = int(input())
A = list(map(int, list(input().split())))

total = 0
for i in range(0, N-1):
  step = A[i+1] - A[i]
  if step > 0 :
    total = total + step
    A[i] = A[i] + step
 
print(total)
n = int(input())
wa = 0
l = list(map(int, input().split()))
for i in range(1, n):
    m = max(l[i], max(l[:i]))
    if m >= l[i]:
        wa += max(l[:i]) - l[i]
print(wa)
N = int(input())
A = list(map(int, input().split()))
max_num = 0
for num in A:
  if num > max_num:
    max_num = num

border = A.index(max_num)
height = []
before_h = A[0]
for h in A[:border]:
  if before_h <= h:
    height.append(0)
  elif before_h > h:
    height.append(before_h-h)
  before_h = h

for h in A[border:]:
  height.append(max_num-h)

print(sum(height))
n = int(input())
A = list(map(int,input().split()))
smallest = A[0]
ans = 0
for x in range(len(A)-1):
	if A[x] < A[x+1]:
		ans += A[x+1] - A[x] 

print(ans)
n = input()
a_list = list(map(int, input().split()))
score = 0

for i in range(len(a_list) - 1):
  if a_list[i] > a_list[i+1]:
    score += a_list[i] - a_list[i+1]
    
print(score)
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n=int(input())
a=get_array()
print(max(a)-min(a))
N = int(input())
array = list(map(int,input().split()))
d,f = 0,0
ans = [0]
for i in range(1,N):
  n = array[i]
  d = f - n
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  f = max(ans[0:i])
print( sum(ans) )
n = int(input())
a_list = list(map(int, input().split()))

_sum = 0

for e in range(len(a_list)):
    # print('aaa')
    if e != 0 :
        # print('bbb')
        if a_list[e] < a_list[e - 1] :
            # print('ccc')
            _sum = _sum + (a_list[e - 1] - a_list[e])

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

tmp = [0]*N
s = 0

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

print(ans)
n = int(input())
input_line = input().split()
member = [int(input_line[i]) for i in range(n)]

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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j] - a[j-1]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
        a[j] += stepping_stone
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)
As = list(map(int, input().split()))
 
sum_humidai = 0
 
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(As[:person_idx])
  
  if max_front_person > person_height:
    humidai = max_front_person - person_height
    sum_humidai += humidai
 
print(sum_humidai)
N = int(input())
A = list(map(int,input().split()))
K = 0
for i in range(0,N-2):
    if A[i+1] < A[i] + K:
      K += A[i]-A[i+1] + K
    else:
        K += 0

print(K)
n = input()
a = input().split()
b = int(a[0])
c = 0

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

maxi = max(A)
index_maxi = A.index(maxi)

sum = 0
if index_maxi != 0:
    mini_max = max(list(A[:index_maxi]))

    for i in A[index_maxi:]:
        sum += (maxi - i)

    for j in A[:index_maxi]:
        sum += (mini_max - j)

print(sum)
N = int(input())
A = list(map(int,input().split()))
A.reverse()
mx1 = A[0]
mx2 = 0
for i in range(1,N):
    mx1 = max(mx1,A[i])
    mx2 = max(mx2,mx1-A[i])
print(mx2)
N=int(input())
A=list(map(int,input().split()))
fum=0
for i in range(N):
  if (A[i]<A[i-1]):
    B=A[i-1]-A[i]
    fum = fum+B
  else:
    fum=fum
print(fum)
n = int(input())
wa = 0
m = 0
l = list(map(int, input().split()))
for i in range(1, n):
    m = max(l[i], m)
    if m >= l[i]:
        wa += max(l[:i]) - l[i]
print(wa)
n = int(input())
a = list(map(int, input().split()))

ans = 0

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

print(ans)
n=int(input())
arr=list(map(int,input().split()))
ma=0
for i in range(1,n):
    ma=max(ma,arr[i]-arr[i-1])
print(ma)
N=int(input())
A=list(map(int,input().split()))
a=0
s=[]
for i in A:
  if i<a:
    s.append(i-a)
    a=i
print(sum(s))
#coding: utf-8
from collections import defaultdict
import sys

N = int(input())
A = [int(x) for x in input().split()]

step = 0

prev = 0

for a in A:
    if a < prev:
        step = max(step, prev-a + 1)
        a += step

    prev = a
#    print(a)

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

count = 0
for i in range(n-1):
    if lis[i] < lis[i+1]:
        count += lis[i+1] - lis[i]
print(count)
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(1,n-1):
  ans += max(0,a[i+1]-a[i])
print(ans)
n = int(input())
a = list(map(int, input().split()))
max_a = max(a)
min_a = min(a)
ans = max_a - min_a
print(ans)
n = int(input())
A = list(map(int, input().split()))

dp = [0 for i in range(n)]
dp[0] = A[0]
cnt = 0
for i in range(1, len(A)):
	# if(A[i] > dp[i-1]+A[i-1] 
	if(A[i] <= A[i-1]):
		dp[i] = dp[i-1]
		cnt += dp[i] - A[i]
	else:
		dp[i] = A[i]
print(cnt)
n = int(input())
a = list(map(int,input().split()))
b = []
c = 0
for i in range(1,n):
    s = a[i-1]-a[i]
    if s >= 0:
        s = s+c 
        b.append(s)
        c = s
    else:
        c = 0
print(sum(b))
N = int(input())
A = list(map(int,input().split()))
cost = 0
if N == 1:
    print(0)
for i in range(1,N):
    if A[i-1] > A[i]:
        cost += A[i-1] - A[i]
        A[i] = A[i-1]
print(cost)
n = int(input())
As = list(map(int, input().split()))
h = 0
for i in range(n - 1):
  d = As[i + 1] - As[i]
  if d > 0:
    As[i+1] += d
    h += d
print(h)
n = int(input())
row = list(map(float, input().split()))
ans = 0
for index in range(1,n):
  if row[index] < row[index - 1]:
    ans += row[index - 1] - row[index]
    row[index] = row[index - 1]
print(ans)
n = int(input())
a = list(map(int, input().split()))
ans = 0
tmp = 0
for i in range(1,n):
    if a[i-1] > a[i]:
        tmp = max(a[i-1], tmp)
        ans += tmp - a[i]
print(ans)
N = int(input())
A = list(map(int,input().split()))
K = 0
for i in range(0,N-2):
    if A[i+1]-A[i]<0:
        K += A[i]-A[i+1]
    else:
        K += 0

print(K)
n,*a=map(int,open(0).read().split())
ma=a[0]
ans=0
for q in a:
  if q>ma:
    ans+=(q-ma)
  ma=q
print(ans)
n = int(input())
a = list(map(int,input().split()))
s = 0
for i in range(n-1):
	s += max(a[i+1] - a[i],0)
print(s)
N = int(input())
line = input()
A = [int(l) for l in line.split()]

total = 0

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

print(total)
n=int(input())
s=list(map(int,input().split()))

ans=0
for i in range(n-1):
    ans+=max(s[i+1]-s[i],0)
    s[i+1]=max(s[i+1],s[i])

print(ans)
n = int(input())

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

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

print(ans)
n=int(input())

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

t=0
x=0

for i in range(n):
  if t>l[i]:
    x+=t-l[i]
    t=l[i]
    
print(x)
N = int(input())
A = list(map(int, input().split())) 
s = [0]*(N-1)
 
for n in range(N-1):
  s[n] = A[n] - A[n+1]
  if s[n] <= 0: 
    continue
  else:
    A[n+1] += s[n]

print(sum(s))
N = int(input())
A = [int(x) for x in input().split()[:N]]
temp = 0
overall = 0
# for i in range(N):
#     S = int(input())
#     A.append(S)
for i in range(0, len(A) -1 ):
    if A[i] > A[i+1]:
        temp = A[i] - A[i+1]
        A[i + 1] += 1
        overall += temp
print(overall)
n=int(input())
a=list(map(int, input().split()))
list=[0]
hl=[a[0]]

for i in range(1,n):
  if a[i]>=a[i-1]:
    list.append(0)
  else:
    mn=max(hl)
    tmp=mn-a[i]
    list.append(tmp)
  hl.append(a[i])

print(sum(list))
n=int(input())
a=list(map(int,input().split()))
s=0
for i in range(1,n):
  s+=max(0,a[i-1]-a[i])
print(s)
N = int(input())
L = list(map(int,input().split()))
ans = 0
for i in range(0, N-1):
  b = L[i] - L[i+1]
  c = int(b)
  if c >> 0:
    ans = ans + c
    L[i + 1] = L[i]
print(ans)
N = int(input())
A = list(map(int,input().split()))
total=0
for i in range(N):
  if A[i]<A[i-1]:
    total+=A[i-1]-A[i]
  else:
    total+=0
print(total)
N = int(input())
A = list(map(int,input().split()))
B = A[0]
ans = 0
for i in range(N):
  if B < A[i]:
    ans += B - A[i]
    B = A[i]
print(ans)
N = int(input())
height = list(map(int,input().split()))
ans = 0

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

step = 0
ma = max(A)
idx = A.index(ma)
for i in range(idx-1):
    dif = A[i+1]-A[i]
    if dif < 0:
        step += abs(dif)
for i in range(idx,n):
    dif = ma-A[i]
    step += dif
print(step)
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]<max(Alist[:i]):
      sum=sum+max(Alist[:i])-Alist[i]
      Alist[i]=max(Alist)
print(sum)
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
stool = 0
for i in range(N):
    for j in range(i+1,i+2):
        if j >= N:
            break
        if A[i] > A[j]:
            stool += A[i] - A[j]
            A[j] += 1

print(stool)
n=int(input())
l=[int(i) for i in input().split()]
c=0
for i in range(1,n):
    c=max(c,max(l[i-1]-l[i],0))
    l[i]+=max(l[i-1]-l[i],0)
print(c)
N = int(input())
A = list(map(int, input().split()))
ans = 0

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

print(ans)
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] > A[i]:
        d[i] = (A[before] + d[before]) - A[i]
        before = i
    else:
        before = i

print(sum(d))
n = int(input())
a_list = list(map(int, input().split()))

steps = []
step_ttl = 0
step = 0
for i, a in enumerate(a_list):
  if i == 0:
    steps.append(step)
    continue
  if a_list[i-1] + step < a:
    step = 0
    steps.append(step)
    continue
  if a < step + a_list[i-1]:
    step = (step + a_list[i-1]) - a
  step_ttl += step
  steps.append(step)

print(step_ttl)
print(steps)
n = int(input())
lis = list(map(int,input().split()))
sum = 0
for i in range(n-1):
  if int(lis[i])>int(lis[i+1]):
    sum += lis[i]-lis[i+1]
  else:
    continue
print(sum)
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_list[i] > a_tmp:
        sum += a_list[i] - a_tmp
        a_tmp = a_list[i]
    else:
        a_tmp = a_list[i]

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

print(A[N - 1] - A[0])
N = int(input())
a = list(map(int, input().split()))
count = 0
i = 1
for i in range(N-1):
  if a[i-1] <= a[i]:
    continue
  elif a[i-1] > a[i]:
    diff = a[i-1] - a[i]
    count = count + diff
    a[i] = a[i-1]
print(count)
n = int(input())
a = list(map(int,input().split()))
ans = 0
cnt = a[0]
for i in range(n-1):
    ans += max(a[i+1]-a[i],0)
print(ans)
def abc176_c():
    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 + 1] - a[i]

    print(ans)



if __name__ == '__main__':
    abc176_c()
N = int(input())
A = list(map(int, input().split()))
s = 0
if N==1:
  print(0)
  exit()

for i,a in enumerate(A):
  if i==0:
    continue
  
  tmp = A[i-1] - a
  s += (tmp>0)*tmp

print(s)
N = int(input())
A = [int(z) for y, z in enumerate(input().split()) if y < N and z.isdigit()]
print(A)
a = min(A)
b = max(A)
c = b - a
print(c)
N = int(input())
A = list(map(int,input().split()))

high = 0
cnt = 0
for i in range(1,N):
    if high <= A[i]:
        high = A[i]
    if A[i] >= A[i-1]:
        cnt += high - A[i-1]
        
print(cnt)
# coding: utf-8
N = input()
N = int(N)
An = list(map(int, input().split()))

cnt = 0
step = []
step_sum = 0

for i in range(0, N):
    if i == 0:
        step.append(0)
        continue
    diff = (An[i - 1] + step[i - 1]) - An[i]
    print('i ', i, 'diff ', diff)
    if diff <= 0:
        step.append(0)
    else:
        step.append(diff)

for num in step:
    step_sum += num

print(step_sum)
n=int(input())

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

t=0
x=0

for i in range(n):
  if t>l[i]:
    t+=t-l[i]
    x+=t-l[i]
    
print(x)
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n = ini()
a = inl()
ans = 0
for i in range(n):
    ans += max(0, a[i-1] - a[i])
print(ans)
loop = int(input())

tall = list(map(int, input().split()))
front = tall[0]
standard = front
result = 0


for i in range(1, loop):
  back = tall[i]
  
  if back-front > 0:
    standard = back
  elif back-front < 0:
    result += standard - back
  front = back

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

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

print(ans)
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))[::-1]
 
ans = 0
m = A[0]
s = n = 0
for i in range(N):
    if A[i] <= m:
        ans += n*m - s
        s = n = 0
    m = A[i]
    s += A[i]
    n += 1
if A[i] <= m:
    ans += n*m - s

print(ans)
n = int(input())

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

highest = 0
last = 0
for i in range(n):
    if a[i] >= highest:
        highest = a[i]
        last = i

shortest = highest
for j in range(0, last):
    if a[j] <= shortest:
        shortest = a[j]
        #print(shortest)

print(highest - shortest)
import sys
input = sys.stdin.readline
import numpy as np

n = int(input())
A = [int(a) for a in input().strip().split()]

pre = A[0]
s = 0
for a in A:
    ad = max(0, a-pre)
    pre = a
    s += ad
print(s)
x = list(map(int, input().split()))

print(x)

A = x[1:]
step = 0
num_min = 1000

for i in range(len(A)-1):
    if A[i] > A[i+1]:
        for j in range(A[i] - A[i+1]):
            step += 1
            A[i+1] += 1
    else: continue
    
print(step)
n = int(input())
a = list(map(int, input().split()))

ans = 0
for i in range(n-1):
    if a[i+1] < a[i]:
        ans += a[i] - a[i+1]
print(ans)
N = int(input())
array = list(map(int,input().split()))
new_array = [0] * N


for i in range(N-1):
    if array[i] > array[i+1]:
        new_array[i+1] = array[i]
print(sum(array) - sum(new_array))
n = int(input())
A = list(map(int, input().split()))
b = sum(A)
s = 0 
M = 0
for i in range(n-1):
    if A[i] <= A[(i+1)]:
        M = 0
        continue
    else:
        if M == 0:
            s += A[i] - A[(i+1)]
            M = A[(i+1)] + (A[i] - A[(i+1)])
        else:
            s += M - A[(i+1)]
print(s)
n=int(input())
A=list(map(int,input().split()))
count=0
for i in range(1,len(A)):
  for j in range(i,0):
    if(a[j]<a[i]):
      count+=(abs(a[i]-a[j]))
    else:
      pass
print(count)
N=int(input())
A=list(map(int,input().split()))
c=1000000000
for i in range(len(A)):
  base=A[i]
  cc=0
  p=8
  for t in range(0, i):
    if base<A[t]:
      p=6
      break
  if p==6:
    continue
  for t in range(i, len(A)):
    if base>A[t]:
      cc+=base-A[t]
  if c > cc:
    c = cc
print(c)
N = int(input())
A = list(map(int,input().split()))

ans = 0
high = 0
for i in range(1,N):
    af = A[i]
    bf = A[i-1]
    if bf > high:
        high = bf
    if af < bf:
        ans += high - af
    
print(ans)
n = int(input())
a_list = list(map(int, input().split()))

h_list = [0]*(2*10**5+10)

for i in range(1,n):
    if a_list[i] < a_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))
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]-a[i-1]
print(ans)