peterverebics/CodeNet_Python_118 huggingface dataset:
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))
#C
N=int(input())
A = list(map(int, input().split()))
ans=0
for i in range(1,len(A)):
    if A[i]-A[i-1]>0:
        ans+=A[i]-A[i-1]
print(ans)
N = int(input())
A = list(map(int,input().split()))

keep = 0
cnt =0
cnt2=0


for a in A:
    if keep > a:
        cnt += keep -a
        cnt2=0
        cnt2 += keep -a
    keep = a + cnt2
    
print(cnt)
n = input()
s = list(map(int, input().split()))
c = 1
for i in s:
    if max(s) - i == c:
        a = i
        break
    else:
        a = 0
print(a)
n = int(input())
h = list(map(int, input().split()))
left_max = h[0]
steps = 0
for i in range(1, n):
  if(left_max > h[i]):
    steps += h[i]-left_max
    left_max = steps + h[i]
print(steps)
input()
A=list(map(int,input().split()))
print(sum(A[i] - A[i-1] for i in range(1,len(A)) if A[i] > A[i-1]))
# -*- coding: utf-8 -*-
N = int(input())
As = list(map(int, input().split()))
res = 0
for i in range(N-1):
    if As[i] < As[i+1]:
        res += As[i+1] - As[i]
print(res)
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 += abs(int(a[j]) - int(list[j]))
print(ans)
N = int(input())
A = list(map(int,input().split()))

ans = 0
a = A.index(max(A))
l = A[a+1:]
for i in range(1,N):
    bf = A[i-1]
    af = A[i]
    if a <= i:
        ans += max(A)*len(l)-sum(l)
        break
    elif bf > af:
        ans += bf-af

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

sum=0
for i in range(len(l)-1):
    if l[i+1]>l[i]:
        sum+=l[i+1]-l[i]
print(sum)
N = int(input())
A = [int(i) for i in input().split()]
cnt = 0
max_A = A[0]
for i in range(1,N):
    if A[i-1]<A[i]:
        max_A = A[i]
    else:
        cnt+=max_A-A[i]
print(cnt)
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = 0
  for a in range(n-1, -1, -1):
    if(a == 0):
      break
    splitAs = As[0:a-1]
    if As[a] < As[a-1]:
      if(0 < len(splitAs)):
        max_a = max(splitAs)
      tmp = As[a-1] - As[a]
      r += tmp
      if tmp + As[a] < max_a:
        r += max_a - (tmp + As[a])

  print(r)


main()
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)
def main():
    N, A = int(input()), list(map(int, input().split()))

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

    print('{}'.format(ans))

if __name__ == '__main__':
    main()
N = int(input())
height = list(map(int,input().split()))
ans = 0

for i in range(N-1):
    if height[i]>height[i+1]:
        ans += height[i]-height[i+1]+1
print(ans)
N = int(input())
A = list(map(int, input().split()))
now = A[0]
ans = 0
for i in range(1,N):
    if A[i]<now:
        step = now - A[i]
        ans += now
    else:
        step = 0
    now = A[i]+step
print(ans)
n = int(input())
a = list(map(int,input().split()))
ans = 0
d = 0

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

print(ans)
N=int(input())
A=list(map(int,input().split()))
B=0
Mi=A[0]

for i in range(1,N):
    if A[i] > A[i-1]:
        Mi = A[i]
    B += Mi - A[i]
print(B)
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 high > af:
        ans += high - af
    print(high,bf,af,ans)

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

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

for i in range(maxI+1, len(list)):
    x = maxV - list[i]
    if x > 0:
        ans += x

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 = list(map(int,input().split()))
ans = 0
m = a[0]
for i in range(1,n-1):
  if a[i] > m:
    ans += a[i]-m
    m = a[i]
print(ans)
n=int(input())
arr=list(map(int,input().split()))
ans=0
for i in range(n-1):
  val=max(0,arr[i]-arr[i+1])
  ans+=val
  arr[i]+=val
print(ans)
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
    if As[a] < As[a-1]:
      if(max_a not in As[0:a-1]):
        max_a = As[a-1]
      r += max_a - As[a]
  print(r)

main()
addNum = 0
n = input()
s = input().split()

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

print(addNum)
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]
    cost += max(0, mi - a[-1])
    print(cost)
    return 0

if __name__ == "__main__":
    solve()
n = int(input())
a = input().split()
height = 0
for i in range(n):
    if int(a[i]) < int(a[i - 1]):
        height += ( int(a[i - 1]) - int(a[i]) )
print(height)
N = int(input())
A = list(map(int,input().split()))
count=0
for i in range(N):
  try:
    a=A[i]
    b=A[i+1]
    while a>b:
      b+=1
      count+=1
  except:
    pass
print(count)
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)

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]>A[i-1]:
        high=A[i]
    else:
        ans+=high-A[i]

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

ans=0

max=max(A)

for i in range(len(A)):
  if A[i] == max:
    a=i
    break

tmp=0

for i in range(a):
  if tmp < i:
    tmp=i

  
total=0
for i in range(a):
  total += a - A[i]
  
for i in range(a,len(A)):
  total += max -A[i]


print(total)
max_tool=0
n=int(input())
arr=list(map(int,input().split()))
maxi=arr[0]
for i in range(1,len(arr)):
  if arr[i]<maxi:
    max_tool=max(max_tool,maxi-arr[i])
  else:
    maxi=arr[i]
print (max_tool)
N = int(input())
A = list(map(int,input().split()))

ans = 0
a = A.index(max(A))
l = A[a+1:]
for i in range(1,N):
    bf = A[i-1]
    af = A[i]
    if a < i:
        ans += max(A)*len(l)-sum(l)
        break
    elif bf > af:
        ans += bf-af

print(ans)
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()))
m = max(a)
s = 0
for i in range(n):
    if i == n-1:
        break
    else:
        if a[i]-a[i+1] < 0:
            s -= a[i]-a[i+1]
print(s)
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=0
        x += keep -a
    keep = a + x
    
    
print(cnt)
def main():
  N = int(input())
  A = list(map(int, input().split()))
  
  i = A[0]
  T = 0
  
  for n in A:
    if n > i:
      T += n -i
    if n < i:
      T = i
  
  print(T)

if __name__ == "__main__":
    main()
N = int(input())
heights = list(map(int, input().split()))
humidai = []
for i in range(len(heights)):
    if humidai == []:
        humidai.append(0)
    else:
        h_height = heights[i] - heights[i-1]
        if h_height > 0:
            humidai.append(h_height)
        else:
            humidai.append(0)
print(sum(humidai))
def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    highest = A[0]

    for height in range(len(A)-1):
        if A[height] > A[height+1]:
            if A[height] > highest:
                highest = A[height]
            count += highest - A[height+1]

    print(count)


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

s = 0
now = As[0]
for a in As:
    if a<now:
        s += now - a
    else:
        now = a
        
    print(s)
n = int(input())
a = list(map(int,input().split()))
ans = 0

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

for i in range(N):
    if A==sorted(A):
        print(i)
        exit()
    
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s
    if s==0:
        break

print(sum(A)-ans)
n = int(input())
a = list(map(int, input().split()))
total = 0
for i in range(1, n):
  if a[i-1] > a[i]:
    total += a[i-1] - a[i]
    a[i] = a[i-1]
# t=int(input())
# for _ in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
arr1=arr.copy()
arr1.sort()
count=0
for i in range(n):
    if arr[i] != arr1[i]:
        count+=1
print(count)
n=int(input())
a=list(map(int,input().split()))
m=max(a)
cnt=0
for i in range(n):
	if a[i]!=m:
		cnt+=1
print(cnt)
n = int(input())
a = list(map(int, input().split()))
l = []
 
 
for i in range(len(a) - 1):
    if a[i + 1] < a[i]:
        l.append(a[i]-a[i + 1])
        a[i + 1] = a[i]
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+=(a[i]-a[j])
    else:
      pass
print(count)
#!/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
    print(f"result: {m}, {n}, {s}, {ans}")
if A[i] <= m:
    ans += n*m - s

print(ans)
N = int(input())
A = list(map(int,input().split()))
pre = A[0]
ans = 0
for i in range(len(A)):
    now = A[i]
    if pre < now:
        ans += now - pre
        pre = now + (now-pre)
    else:
        pre = now
print(ans)
N = int(input())
L = list(map(int, input().split()))
sum = 0
for i in range(N):
    sum += max(0, L[i-1] - L[i])
print(sum)
N = int(input())
A = list(map(int, input().split()))

print(max(A)-min(A))
n = int(input())

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


highest = 0
ans = 0

a = a[::-1]

for i in a:
    if i > highest:
        highest = i
    ans += highest - i

print(ans)
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:
    if e > s:
      s = e
    else:
      continue
  else:
    continue

print(s)
N=int(input())
A=list(map(int,input().split()))
X=0
for i in range(N-1):
  if A[i+1]>A[i]:
    X+=A[i+1]-A[i]
    A[i+1]=A[i]
print(X)
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
    elif max_int == a[i]:
        ans = 0
print(ans)
n = int(input())
A = [*map(int, input().split())]
s = 0
pre = A[0]
for now in A[1:]:
    d = now - pre
    if d>0:
        s += d
    pre = now
print(s)
#_ = input()
li = list(map(int,input().split()))
ans=0
 
while li!=[] and li!=sorted(li):
    maxValue = max(li)
    print(maxValue)
    maxLocation = li.index(maxValue)
    print(maxLocation)
    li_removed = li[maxLocation:]
    li_removed.sort()
    print(li_removed)
    li = li[:maxLocation]
    maxLocation2 = li_removed.index(maxValue)
    li_calculate = li_removed[:maxLocation2]
    print(li_removed)
    print(li_calculate)
    
    for i in li_calculate:
        ans = ans + (maxValue - i)
        
print(ans)
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:
        pass
    else:
        ans += diff
        A[i] += diff
print(ans)
N = int(input())
A_high_list = list(map(int,input().split()))
B_list = [A_high_list[0]]
Fumidai = 0
s = 0

for n in range(N):
    if B_list[n] > A_high_list[n]:
        s = max(B_list) - A_high_list[n]
        Fumidai += s
        B_list.append(A_high_list[n])
    else:
        B_list.append(A_high_list[n])
        continue

print(Fumidai)
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i, h in enumerate(a):
    if i == 0:
        continue
    ans += max(0, a[i - 1] - h)
print(ans)
n = int(input())
l = list(map(int,input().split()))
#print(n,l)

ans=0
l2=[l[0]]
#print(l2)

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

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

for i in range(1, n):
    if a[i - 1] > a[i]:
        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):
  ans += max(0, A[i] - A[i-1])
print(ans)
for _ in range(1):
    n=int(input())
    a=list(map(int,input().split()))
    k=a[0]
    ans=0
    
    for i in range(n):
        if i==0:
            m=a[i]
            a[i]=0
        else:
            m=max(m,a[i])
            a[i]=m-a[i]
    u=0
    for i in range(n):
        ans+=max(0,a[i]-u)
        u=a[i]
    print(ans)
N = int(input())
A = list(map(int, input().split()))[::-1]
max_val = 0
ret = 0
for i in range(1,N):
    if A[i] > max_val:
        max_val = A[i]
        for j in range(0, i):
            ret += max_val - A[j]
print(ret)
def abc176_c():
    n = int(input())
    a = list(map(int, input().split(' ')))
    ans = 0
    current_h = 0

    # 前の人が大きい時はa[i-1] - a[i]分の台が必要
    for i in range(1, len(a)):
        if i == 1:
            current_h = a[i]

        if a[i - 1] > a[i]:
            current_h = max(a[i - 1], current_h)
            ans += current_h - a[i]

    print(ans)

if __name__ == '__main__':
    abc176_c()
# coding=utf-8

if __name__ == '__main__':
    N = int(input())

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

    M = max(li)
    # print(M)

    MV = li[0]
    C = 0
    hai = li.index(M)
    #print(hai)
    for i in range(N):
        if i < hai:
            if MV > li[i]:
                C += MV - li[i]

        else:
            C += M - li[i]
        #print(C)

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

Step=[0]
maxt=A[0]
i=0
while i<N-1:
  if A[i]<A[i+1]:
    maxt=max(maxt,A[i+1])
    s=0
  else: # A[i]>=A[i+1]
    s=maxt-A[i+1]
  Step+=[s]
  i+=1
print(sum(Step))
N = int(input().rstrip())
As = list(map(int, input().rstrip().split(' ')))

ans = 0

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

print(ans)
n=int(input())
arr=list(map(int,input().split()))
ans=0
for i in range(n-1):
  val=max(0,arr[i+1]-arr[i])
  ans+=val
  arr[i]+=val
print(ans)
N = int(input())
an = list(map(int,input().split()))
solve = []

ans = 0
maxnum = 0

for i in range(1,N):
    if maxnum < i:
        ans += max(an[maxnum:i+1]) - an[i]
        maxnum = an[i]
    if maxnum >= i:
        ans += maxnum - an[i]
print(ans)
n = int(input())

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


highest = 0
diff = 0

a = a[::-1]

for i in a:
    diff = max(highest - i, diff)
    if i > highest:
        highest = i

print(diff)
n=int(input())
ppl=(input().split())
answer=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]
answer
# t=int(input())
# for _ in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
maxa=arr[0]
count=0
for i in range(n):
    if arr[i] > maxa:
        maxa=arr[i]
    else:
        count+=1
print(count)
from math import ceil
def mlt(): return map(int, input().split())


x = int(input())
s = list(mlt())

res = 0

for n in range(1, x):
    if s[n] < s[n-1]:
        d = s[n-1]-s[n]
        res += d
        s[n] += 1

print(res)
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=list(map(int,input().split()))
ans=0
tmp=a_list[0]
for i in range(n-1):
    if a_list[i]>a_list[i+1]:
        #print(tmp,ans)
        ans+=tmp-a_list[i+1]
    else:
        tmp=a_list[i+1]
print(ans)
n = int(input())
a = list(map(int,input().split()))
ans = 0
cnt = a[0]
for i in range(1,n):
    ans += max(cnt-a[i],0)
    cnt = max(a[i-1],a[i])
print(ans)
n = int(input())
a = list(map(int, input().split()))
ans = 0
maxi = 0

for i in range(n-1):
    if a[i] > a[i + 1]:
        if a[i] > maxi:
            maxi = a[i]
        ans += maxi - a[i+1]

print(ans)
# 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])-Als[i]
print(ans)
n = int(input())
a = list(map(int,input().split()))
b = []
c = 0
for i in range(n-1):
    s = a[i]-a[i+1]
    s = max(c,s)
    if s >= 0:
        b.append(s)
        c = s
print(sum(b))
n=int(input())
l=list(map(int,input().split()))
ans=0
maxt=0
for i in l:
  if i<maxt:
    ans+=matx-1
    maxt=max(i,maxt)
print(ans)
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

    print(tmp1)
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 = count + (l_count - k_index) * i
    l_count = k_index

print(count-sum(lst))
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(sum(b))
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(int(sum(s)))
N=int(input())
A=list(map(int,input().split()))
S=0
i=N
while i>1:
    saidai=max(A[0:i])
    S +=(i-A.index(saidai))*saidai
    i=A.index(saidai)
print(S-sum(A))
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())
#A=list(map(int,input().split()))

N=200000
A=[i for i in range(1,200001)]

ans=sum(A)
s=N+1
j=s

if A==sorted(A):
    print(0)
    exit()

for i in range(N//2):
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s
    if  A==sorted(A):
        break

print(sum(A)-ans)
n = int(input())
H = list(map(int, input().split()))
mh = 0
ans = 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(ans, mh - H[i + 1])
    ans += cnt
print(ans)
n = int(input())
list = list(map(int,input().split()))

ans = 0
maxV = max(list)
for i,ele in enumerate(list):
    if i == len(list)-1:
        continue

    if ele == maxV:
        for j in range(i+1,len(list)):
            hoge = maxV-list[j]
            # print("no", i, "maxV hoge=", hoge)
            ans += hoge
        break

    if ele > list[i+1]:
        hoge = ele - list[i+1]
        # print("no", i, "minus hoge=", hoge)
        ans += hoge

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

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

sumV = sum(list[maxI+1:len(list)])
maxM = maxV * (len(list)-1-maxI)
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=list(map(int,input().split()))
X=0
for i in range(N-1):
  if A[i+1]>A[i]:
    X+=A[i+1]-A[i]
print(X)
N = int(input())
Alist = list(map(int,input().split()))

dailist = [0]*N
Alist.append(0)
dailist.append(0)
max = 0
for i in range(N):
  if Alist[i]>max:
    max=Alist[i]
  
  if Alist[i]>Alist[i+1]:
    dailist[i+1]=max-Alist[i+1]
  
del dailist[-1]
print (sum(dailist))
n = int(input())
a = list(map(int, input().split()))
ans = 0
maxh = 0
for i in range(len(a)-1):
    if maxh < a[i]:
        maxh = a[i]
    if a[i] > a[i+1]:
        ans += maxh - a[i+1]


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

print(ans)
n=int(input())
l=map(int,input().split())
ans=0
maxt=0
for i in l:
  if i<maxt:
    ans+=maxt-i
    maxt=max(i,maxt)
print(ans)
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 + ad
    s += ad
print(s)
n = int(input())
A = list(map(int, input().split()))

a=0
result=0

for i in range(n-1):
  if A[i] > A[i+1]:
    a = A[i] - A[i+1]
    result += a
    
print(result)
N = int(input())
A = list(map(int,input().split()))
pre = A[0]
ans = 0
for i in range(len(A)):
    now = A[i]
    if pre < now:
        ans += now - pre
    pre = now
print(ans)
N = int(input())
A = list(map(int,input().split()))
B = [0] * N # 踏み台の高さ
ans = 0
for i in range(1,N):
    h = 0
    if A[i] < A[i-1]:
        h = B[i-1]+A[i-1] - A[i]
    else:
        h = 0
    B[i] = h

for i in range(N):
    ans += B[i]

# print(B)
print(ans)
import numpy as  np

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
maximum = max(A)

if len(set(A)) == 1:
    print(0)
else:
    max_point = int(np.where(A == maximum)[0])
    ans = [A[0]]
    for i in range(1, max_point):
        ans.append(A[0])


    for j in range(max_point+1):
        ans.append(maximum)

    print(sum(ans) - sum(A))
x = [list(map(int, input().split())) for i in range(2)]
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())

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(count)
N = int(input())
A_high_list = list(map(int,input().split()))
Fumidai = 0
s = 0
B_list = []

for n in range(N):
    if n > 0 and A_high_list[n-1] > A_high_list[n]:
        B_list.append(A_high_list[n-1])
        s = max(B_list) - A_high_list[n]
        Fumidai += s
    else:
        continue

print(Fumidai)
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    N = I()
    A = LI()
    a_max = A[0]
    ans = 0
    for i in range(1,len(A)):
        if A[i]>a_max:
            ans += A[i]-A[i-1]
            a_max = A[i]
    print(ans)

main()
item = input().split()
total_num = item[0]
fumidai_sum = 0
before_tall = 0

for idx, tall in enumerate(item[1:]):
  tall = int(tall)
  
  if idx == 0:
    before_tall = tall
    continue
  else:
    if before_tall > tall:
      fumidai_sum += (before_tall - tall)
    else:
      before_tall = tall
      
print(fumidai_sum)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    
    for i in range(1, len(a)):
        humidai = max(0, a[i] - a[i-1])
        ans +=  humidai
        a[i] += humidai
    print(ans)
        
        
if __name__ == '__main__':
    main()