peterverebics/CodeNet_Python_118 huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
ans = 0
h = A[0]
for a in A:
  if h > a:
    ans += h - a
  h = a
print(ans)
N = input()
A = list(map(int,input().split()))
raw =sum(A)
con = 0

if len(A) == 1:
    print(A[0])
    
else:
    for i in A:
        if A[con] > A[con+1]:
            A[con+1] =  A[con]
        
        if A[con+1] == A[-1]:
            break

        con += 1

    ans = sum(A) - raw
    print(ans)
N = int(input())#整数
A = list(map(int, input().split()))#配列Int型

ans = 0
max_val = A[0]

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

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

print(step_ttl)
import sys
li  =   lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw  =   lambda : sys.stdin.readline().strip().split()
ni  =   lambda : int(sys.stdin.readline().strip())
nsi =   lambda : sys.stdin.readline().strip()

from collections import defaultdict as df
import math

n=ni()
l=list(map(int,input().split()))
mn=l[0]
ans=0
for i in range(1,n):
    if(mn<l[i]):
        mn=l[i]
        print(l[i])
    else:
        ans+=mn-l[i]
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 = int(input())
a = list(map(int,input().split()))
b = []
c = 0
for i in range(1,n):
    c = max(c,a[i-1])
    s = c-a[i]
    if s >= 0:
        b.append(s) 
print(b)
N = int(input())
array = list(map(int,input().split()))
d,f = 0,0
ans = []
for i in range(N):
  if i == 0:
    ans.append(0)
    f = array[0]
    continue
  n = array[i]
  d = n - f
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  f = n
print( sum(ans) )
n = int(input())
vals = list(map(int, input().split()))

ma = vals[0]
res = [0 for _ in range(n)]
for i, val in enumerate(vals):
    if val < ma: 
        res[i] = ma - val
    if ma < val:
        ma = val

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

diff = []
for i in range(N - 1):
  diff.append(A[i + 1] - A[i])
    
print(sum(diff))
n = int(input())
a = list(map(int,input().split()))

ans = 0

#a.insert(0,0)
a.append(999999999)
tmp = 0
for i in range(n):
  #print(tmp)
  if a[i] <= a[i+1]:
    tmp = 0
    continue
  else:
    tmp = a[i] - a[i+1]
    a[i+1] += tmp
    ans += tmp
    
print(ans)
#print(a)
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
#N, K = map(int, input().split())
# 文字列の入力
N = input()
#開業されたN個要素を受け付ける
input_list = list(map(int, input().split()))

i=1

ans = 0

while i < len(input_list):
    if(input_list[i] < input_list[i-1]):
        tmp = input_list[i-1] - input_list[i]
        ans = ans + tmp
        input_list[i] = input_list[i] + 1
    i = i+1

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

s = [0] * n
for i in range(1, n):
    if a[i] - a[i - 1] <= 1:
        s[i] = a[i - 1] - a[i]
        a[i] = a[i - 1]
print(sum(s))
n = int(input())
a_L = list(map(int,input().split()))

tall = a_L[0]
ans = 0
diff = 0
for i in range(1,len(a_L)):
    if tall < a_L[i]:
        diff = abs(tall - a_L[i])
        tall = a_L[i]
    else:
        tall = a_L[i]
        diff = 0
    ans += diff
print(ans)
N = int(input())
A = list(map(int, input().split()))

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

result = 0
for i in range(1, n):
    hoge = a[i]
    a[i] = a[i]+(a[i]-a[i-1]) if a[i]-a[i-1] > 0 else a[i]
    result += a[i] - hoge
    
print(result)
a= int(input())
mod = a%9
if mod==0:
  print('Yes')
else:
  print('No')
N = int(input())
A = list(map(int, input().split()))
ans=0

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

print(ans)
import copy

n = int(input())
a = input().split()
list = copy.copy(a)
ans = 0

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

ans = 0
maxV = max(list)
maxI = list.index(maxV)

sumV = sum(list[maxI:len(list)])
maxM = maxV * (len(list)-maxI)
# print(sumV, maxM)
ans += maxM - sumV

if maxI != 0:
    for i in range(0,maxI):
        if i != maxI:
            x = list[i] - list[i+1]
            if x > 0:
                ans += x
print(ans)
N = int(input())
A = [int(i) for i in input().split()]
s = [0]*N

for i,n in enumerate(A[1:]):
    s[i] = max(0, A[i] - A[i-1])

print(sum(s))
number = int(input())
values = [int(i) for i in input().split()]
sum = 0

max = max(values)
min = min(values)

while max != min:
  temp = max/2
  for element in values:
    if max >= element > temp:
      sum += (int(max) - element)
      print(max, element)
    
  if int(temp) <= min:
    sum += (int(max) - min)
      
  max = int(temp)

print(sum)
N = int(input())
A = input().replace(" ","")
Alist = list(A)
An = list(map(int,Alist))

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)
n = int(input())
a = list(map(int,input().split()))
c = 0
for i in range(1,n):
    if a[i-1]>a[i]:
        c += (a[i-1]-a[i])
print(c)
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())

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

print(s)

top = s[0]
count = 0

for x in s :
    if x <= top :
        count = count + top - x
    else :
         top = x

print(str(count))
n=int(input())
a=list(map(int,input().split()))
print(max(a)-min(a))
N = input()
lists = list(map(int, input().split()))

high_sum = 0

for x in range(int(N)):
    if lists[x - 1] < lists[x]:
        a = lists[x] - lists[x - 1]
        high_sum += a
    else:
        continue
        
print(high_sum)
N=int(input())
A=list(map(int,input().split()))
B=list()
B.append(A[0])
for i in range(1,N):
  if A[i-1]-A[i]>0:
    B.append(B[i-1])
  else:
    B.append(A[i])
print(sum(B)-sum(A))
N = int(input())
ary_A = list(map(int, input().split()))

h_sum  = 0

for i in range(len(ary_A) - 1):
    diff = ary_A[i + 1] - ary_A[i]
    if diff > 0:
        h_sum += diff

print(h_sum)
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]=2*A[i]-A[i+1]
    #print(sum)
    #print(A)

print(sum)
n = int(input())
a = list(map(int,input().split()))
b = []
c = 0
for i in range(n-1):
    s = a[i]-a[i+1]
    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()))
x = 0
total = 0


for i in range(n):
    if a[i-1] >= a[i]:
        x = a[i-1] - a[i]
        total += x
        i = i+1
    else:
        total += 0
        i = i+1
print(total)
N = int(input())
A = list(map(int,input().split()))
sum_height = 0
max_height = A[0]
for i in range(1,len(A)):
    if A[i] < A[i-1]:
        sum_height += max_height - A[i]
    else:
        max_height = A[i]
print(sum_height)
N = int(input())
A = list(map(int, input().split()))
sum=0
for i in range(N-1):
    if A[i+1]>A[i]:
        sum+=A[i+1]-A[i]
        A[i+1]=A[i]
print(sum)
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
Ai = input().split()
Ai_i = list(map(int, Ai))
now_ind = -1
tmp_list = Ai_i
count = 0
while True:
    max_value = max(tmp_list)
    now_ind = tmp_list.index(max_value)
    if now_ind == 0:
        break
    count = count + sum(list(map(lambda x: max_value - x, tmp_list[now_ind:len(tmp_list)])))
    tmp_list = tmp_list[0:now_ind]
print(count)
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = max(As)
  for a in range(n-1, -1, -1):
    if(a == 0):
      break
    splitAs = As[0:a-1]
    if As[a] < As[a-1]:
      if(max_a not in splitAs):
        max_a = As[a-1]
      tmp = As[a-1] - As[a]
      r += tmp
      if tmp + As[a] < max_a:
        r += max_a - (tmp + As[a])
  print(r)


main()
total = int(input())
tall = list(map(int, input().split()))
dif = 0
for i in range(total - 1):
    if tall[i] > tall[i+1]:
        var = tall[i] - tall[i+1]
        tall[i+1] += var
        dif += var
    else:
        pass
print(dif)
print(tall)
N = int(input())
As = list(map(int , input().split()))
mx = As[-1]
sm = 0
for i in range(len(As)-2,-1,-1):
  if As[i] < mx:
    sm+= mx - As[i]
  mx = As[i]
print(sm)
n = int(input())
a = list(map(int, input().split()))
s = 0

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

print(str(s))
addNum = 0
n = input()
s = input().split()

for i in range(len(s)-1):
    if(s[i]>s[i+1]):
        print
        a = int(s[i])
        b = int(s[i+1])
        c =  a-b
        s[i+1] = s[i]
        addNum += c

print(addNum)
import sys
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
count = 0
for i in range(N-1):
    A[i] > A[i+1]
    A[i + 1] = A[i + 1] + 1
    count += 1
print(count)
N = int(input())
H = list(map(int, input().split()))

T = 0
for i in range(N):
    if int(H[i-1]) - int(H[i]) > 0:
        T += int(H[i-1]) - int(H[i])
        H[i-1] = H[i]

print(T)
N = int(input())
A = list(map(int,input().split()))
total = 0
mxm = A[0]
for i in range(1,N):
  if A[i-1]<A[i]:
    mxm = A[i]
  else:
    total += (mxm - A[i])
print(total)
N = int(input())
A = [int(i) for i in input().split()]
for i in range(N-1):
  if A[i] > A[i+1]:
    S = A[i] - A[i+1]
    K = A[i+1] + S
    A[i+1] = K
print(A)
# AtCoder Beginner Contest 176
# C - Step

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

ans=0

for i in range (1,N):
    if Als[i]>=Als[i-1]:
        pass
    else :
        ans+= abs(max(Als[0:i])-Als[i])

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

for i in range(N-1) :
    if A[i] - A[i+1] >0 :
        buf += A[i] - A[i+1]
if A[N-2] - A[N-1] >0 :
    buf += A[N-2] - A[N-1]
print(buf)
l=list(map(int,input().split()))
ans=0
max=0
for i in l:
  if i<max:
    ans+=max-1
    max=max(i,max)
print(ans)
a=int(input())
b=list(map(int,input().split()))
c=0
for i in range(a-1):
  if b[i]>b[i+1]:
    c=c+b[i]-b[i+1]
print(c)
n=int(input())
l=map(int,input().split())
ans=0
maxt=0
for i in l:
  if i<maxt:
    ans+=maxt-1
    maxt=max(i,maxt)
print(ans)
n = int(input())
a = list(map(int, input().split( )))
front = 0
step = 0
for height in a :
    x = 0
    if front > height :
        x = front - height
        step += x
    print(front, height, x)
    if front < height:
        front = height
print(step)
n=int(input())
a=list(map(int,input().split()))
sm=0
b=a[0]
for i in range(1,n): 
  if a[i]<a[i-1]:
    sm+=b-a[i]
  else:
    b=a[i]
print(sm)
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+1]-A[i]
    else:
        K += 0

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

x = 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]):
		cnt += x - A[i]
	else:
		x = A[i]
print(cnt)
n = int(input())
a = list(map(int, input().split()))

amax = max(a)
x = a.index(amax)
if x > 0:
   bmax = max(a[:x])
else:
   bmax = 0

ans = 0
for i in range(n):
   if i < x:
      ans += max(0, bmax-a[i])
   else:
      ans += max(0, amax-a[i])
print(ans)
a=int(input())
b=list(map(int,input().split()))
c=0
for i in range(a-1):
  if b[i]>b[i+1]:
    b[i+1]=b[i+1]+1
    c=c+1
print(c)
l=list(map(int,input().split()))
u=max(l)
d=0
i=0
while l[i]!=u:
  if l[i]>l[i+1] and l[i]:
    d=d+abs(l[i+1]-l[i])
    l[i+1]=l[i]
  i=i+1
r=i
for b in range(r,len(l)):
    d=d+(u-l[b])
print(d)
N=int(input())
A=list(map(int,input().split()))
s=0
if N==1:
    print(0)
else:
    for i in range(N-1):
        if A[i+1]<A[i]:
            s=s+A[i]-A[i+1]
            A[i+1]=A[i]
print(s)
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] - a[i+1])
    ans += x
    a[i+1] += x

print(ans)
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]
    elif A[i] > x:
        x = A[i]
    print(x, ans)
print(ans)
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(mi - a[i+1], mi)
    cost += max(0, mi - a[-1])
    print(cost)
    return 0

if __name__ == "__main__":
    solve()
n = int(input())
a = list(map(int,input().split()))
s = 0
for i in range(n-1):
  e = a[i+1]-a[i]
  if e > 0:
    s += e
  else:
    continue

print(s)
N = int(input())
An = [int(val) for val in input().split()]

# 各踏み台を初期化
Fn = [0 for _ in range(N)]

an_1 = An[0]
fn_1 = 0

for i in range(1, N):
    an = An[i]
    fn =  max(0, an - an_1 - fn_1)

    Fn[i] = fn
    fn_1 = fn
    an_1 = an

sum = 0
for fi in Fn:
    sum += fi

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

h_list = [0]*(n+10)

for i in range(1,len(a_list)):
    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 = [int(i) for i in input().split()]
s = [0]*N
for i,n in enumerate(A):
    if i == 0:
        continue
    s[i] = max(0, A[i-1] + s[i-1] - A[i])

print(sum(s))
print(s)
N = input()
A = []
A = input().split()
j = 0
t = A[0]
for i in A:
    if t>i:
        j += int(t)-int(i)
    if t<i:
        t = i
print(j)
n = int(input())
a = list(map(int, input().split()))

count = 0

for i, j in enumerate(a):
    try:
        if j < a[i - 1]:
            b = a[i - 1] - j
            count += b
        else:
            pass
    except IndexError:
        pass

print(count)
n=int(input())
a=list(map(int, input().split()))
ans=0
for i in range(1, n):
    s=a[i] - a[i-1]
    if s<0:
        ans-=s
        a[i]-=s
N = int(input())
A = list(map(int, input().split()))
min_step = 10**9
step = 0
for i in range(N-1):
    if A[i] <= A[i+1]:
        step = A[i+1] - A[i]
        for j in range(N-1):
            if step + A[j] < A[j+1]:
                break
        else:
            min_step = min(min_step, step)
print(min_step)
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 = max(c,s)
    else:
        c = 0
        b.append(c)
print(b)
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

    print(tmp1)
from sys import stdin


length = int(stdin.readline())
heights = list(stdin.readline().replace(" ", ""))

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))
n = int(input())
a = [int(i) for i in input().split()]

ans = 0
step = 0

for x in range(n):
  while a[x-1] > a[x]+step:
    step += 1
  else:
    ans = ans + step
    step = 0
    
print(ans)
n=int(input())
l=list(map(int,input().split()))
tmp=l[0]
ans=0
for i in range(n-1):
    if l[i+1]<=l[i]:
        ans+=(tmp-l[i+1])
    else:
        tmp=l[i+1]
print(ans)
N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(set(lst), reverse=True)

l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    count += (l_count - k_index) * i
    l_count = k_index

print(count-sum(lst))
N = int(input())
A = list(map(int, input().split()))
pre = A[0]
ans = 0
for a in A[1:]:
    if pre > a:
        ans += pre - a
    pre = a
print(ans)
n=int(input())
A=list(map(int,input().split()))
ans=0
for i in range(1,n):
  d=A[i]-A[i-1]
  if d>0:
    ans += d
    A[i]+=d
print(ans)
n=int(input())
l = list(map(int,input().split()))
s=0
for i in range(n):
    if int(l[i-1])>int(l[i]):
        p=int(l[i-1])-int(l[i])
        s+=p
print(s)
N = int(input())
A = list(map(int, input().split()))
out = 0
for i in range(1, N):
  d = max(A[i] - A[i-1], 0)
  out += d
  A[i] += d
print(int(out))
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))
import numpy as np

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

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

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

a=0
result=0

for i in range(n):
  if A[i-1] > A[i]:
    a = A[i-1] - A[i]
    result += a
    
print(result)
N = int(input())
A = list(input().split())
An = [int(num) for num in A]
print(An)

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)
N = input()
A = input().split()
S = int(min(A))
L = int(max(A))
print(L-S)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(0, n-1):
        if a[i] < a[i+1]:
            step = a[i+1] - a[i]
            a[i] = a[i] + step
            cnt = cnt+step

    print(cnt)
main()
N=int(input())
A = list(map(int, input().split()))
#print(A)
count=0
count2=0
for i in range(len(A)-1):
    if A[i]>A[i+1]:
        count=A[i]-A[i+1]
        #print(count)
        A[i+1]=A[i]
    count2+=count
print(count2)
N = int(input())
A = list(map(int, input().split()))
sa = 0
i = 0
mx = max(A)
flg = 0


while i < len(A)-1:
    if (A[i] < mx) and A[i] > A[i+1] and flg != 1:
        sa += A[i]-A[i+1]
    elif A[i] == mx:
        sa += mx-A[i+1]
        flg = 1
    elif flg == 1:
        sa += mx-A[i+1]
    i += 1


print(sa)
N = int(input())
heights = list(map(int, input().split()))
steps = [0] * N
for i in range(1, N):
    if heights[i-1] + steps[i-1] > heights[i]:
        steps[i] = 0
    else:
        steps[i] = heights[i] - (heights[i-1] + steps[i-1])
print (sum(steps))
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
 
sum_step = 0
 
for n in range(0, N):
  if A[n] >= A[n-1]:
    pass
  else:
    sum_step += A[n-1] - A[n]
    A[n] = A[n-1]
  
print(sum_step)
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 += mh - H[i + 1]
print(cnt)
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
  else:
    a=i
print(sum(s))
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)
n=int(input())
arr=list(map(int,input().split(" ")))
s=0
for i in range(n-1,0,-1):
    if arr[i]>arr[i-1]:
        s+= arr[i]-arr[i-1]
print(s)
N = int(input())
A = list(map(int, input().split()))

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

r = 0

for a in range(len(As)-1, -1, -1):
  if As[a] < As[a-1]:
    r += As[a-1] - As[a]
print(r)
n = int(input())
arr = list(map(int,input().split()))
res = [0]*n
for i in range(1, n):
    if arr[i] < arr[i-1]:
        res[i] += (res[i-1] + abs(arr[i-1]-arr[i]))
print(sum(res))
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]
print(total)
N = int(input(''))
A = input('')
A_N = A.split()
num = 0 
for i in range(1,N):
    if int(A_N[i]) >= int(A_N[i-1]):
        num += 0
    elif (A_N[i]) < (A_N[i-1]):
        num += int(A_N[i-1]) - int(A_N[i])
print(num)
N = int(input())
numbers = input().split()
numbers = list(map(int,numbers))
m = 0
gross_count = 0
for n in numbers:
    if m > n:
        gross_count += (m-n)
        print(gross_count)
    else:
        m = n
print(gross_count)
N = int(input())
A = list(map(int, input().split()))
A.reverse()
step = 0
ans = 0
max_step = 0
for i in range(N):
    for j in range(i, N):
        if A[i] - A[j] > 0:
            step = A[i] - A[j]
            max_step = max(max_step, step)
    ans += max_step
print(ans)
num = int(input())
height = list(map(int,input().split()))
count = 0

for i in range(len(height) - 1):
    while height[i] < height[i + 1]:
        height[i] += 1
        count += 1
print(count)
N = int(input())
A = list(map(int, input().split()))
pre = A[0]
ans = 0
if N == 1:
    print(0)
else:
    for a in A[1:]:
        print(ans)
        if pre > a:
            ans += pre - a
        else:
            pre = a
    print(ans)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(0, n-1):
        if a[i] < a[i+1]:
            step = a[i+1] - a[i]
            cnt = cnt+step
            a[i] = a[i] + step
    
    print(cnt)
main()
n = int(input())
lst = list(map(int,input().split()))
ans = 0
for i in range(n):
  tall = 0
  for j in range(i):
    if (lst[j] > tall):
      tall = lst[j]
  print (i, tall)
  if (lst[i] < tall):
    sa = tall - lst[i]
    print(i,sa)
    ans = ans + sa
    lst[i] = tall
print(ans)
n = int(input())
a = input().split()
s = 0
for i in range(n-1):
  s+=max(int(a[i+1])-int(a[i]), 0)
print(s)
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
 
sum_step = 0
 
for n in range(1,N):
  if A[n] < A[n-1]:
    sum_step += A[n-1] - A[n]
    A[n] = A[n-1]
  print(sum_step)
n=int(input())
l = list(map(int,input().split()))
s=0
for i in range(n):
    if l[i-1]>l[i]:
        p=int(l[i-1])-int(l[i])
        s+=p
print(s)
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)
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  sortAs = sorted(As)
  max_a = sortAs[-1]
  for i in range(n-1, -1, -1):
    if i == 0:
      break
    if As[i] < As[i-1]:
      if(max_a not in As[0:i-1]):
        max_a = As[i-1]
      r += max_a - As[i]
  print(r)

main()
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    top = a[0]
    ans = 0

    for height in a:
        if top < height:
            ans += height - top
            top = height

    print(ans)


main()
n = int(input())
a = list(map(int, input().split()))
l = []
for i in range(0, n-1):
    if(a[i] > a[i+1]):
        l.append(a[i]-a[i+1])
    else:
        l.append(0)
for i in range(0, len(l)-1):
    if((l[i] > 0) and (l[i+1] != 0)):
        l[i+1] = l[i+1] + l[i]
print(sum(l))
num = int(input())
line = list(map(int,input().split()))

mini = line[0]
x = 0
for i in range(1,num):
  if mini > line[1]:
    x += 1
  else:
    mini = line[i]
print(x)
a=int(input())
b=input().split(' ')
print(int(max(b))-int(min(b)))
N = input()
A = []
A = input().split()
j = 0
t = A[0]
for i in A:
    if t>i:
        j += int(t)-int(i)
    if t<=i:
        t = i
print(j)
# AtCoder Beginner Contest 176
# C - Step

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

ans=0

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

tmp=0
count=0
for i in range(N):
  if(i==0):
    tmp=A[0]
  if(tmp<=A[i]):
    #print(A[i])
    tmp=A[i]
  else:
    count+=1

print(count)