peterverebics/CodeNet_Python_118 huggingface dataset:
n=int(input())
alist=list(map(int,input().split()))
height=alist[0]
ans=0

for i in range(1,n):
    if height>alist[i]:
        ans+=height-alist[i]
    
    else:
        height=alist[i]

print(ans)
n = int(input())
arr = [int(j) for j in input().split()]

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

print(ans)
def step(arr):
    res = 0
    for i in range(1,len(arr)):
        if arr[i] >= arr[i-1]:
            continue
        else:
            res += abs(arr[i-1] - arr[i])
            arr[i] = arr[i - 1]
    return res

def start():
    n = int(input())
    arr = [int(x) for x in input().split(" ")]
    print(step(arr))
start()
N = int(input())
A = list(map(int, input().split()))
hight = A[0]
k = 0
for i in range(1, N):
  if hight > A[i]:
    k += (hight - A[i])
  else:
    hight = A[i]
print(k)
n = int(input())
num_list = [int(i) for i in input().split()]
count = 0

for i in range(1,n):
    if num_list[i] < num_list[i-1]:
        count += (num_list[i-1] - num_list[i])
        num_list[i] = num_list[i-1]

print(count)
n = int(input())
a = [int(i) for i in input().split()]
m = a[0]
sum = 0
for i in a[1:]:
    if i <= m:
        sum += m - i
    else:
        m = i
print(sum)
n=int(input(""))
lisa=input("").split(" ")
t=0
s=int(lisa[0])
for i in lisa:
    if(s<int(i)):
        s=int(i)
    else:
        t+=s-int(i)
print(t)
N = input()
A = map(int, input().split())
current = 0
ans = 0
for a in A:
  ans += current - min(current, a)
  current = max(current, a)
print(ans)
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]
    A[i+1] += a
    result += a
    
print(result)
N = int(input())
A = list(map(int,input().split()))
H = 0
for i in range(1,N):
    if A[i] < A[i-1]:
        sa = A[i-1] - A[i]
        A[i] = A[i-1]
        H += sa
print(H)
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)
n=int(input())
l=list(map(int,input().split()))
ans = 0
for i in range(n):
  if i > 0 and l[i] < l[i-1]:
    ans += l[i-1] - l[i]
    l[i] = l[i-1]
print(ans)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    temp = A[0]
    su = 0
    for i in range(1, N):
        if A[i - 1] > A[i]:
            su += A[i - 1] - A[i]
            A[i] = A[i - 1]
    print(su)


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]:
        ans += a[i-1] - a[i]
        a[i] = a[i-1]
print(ans)
import numpy as np
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)

N = int(input())
A_str = input()

A_lst = A_str.split(" ")
A_lst = [int(A_lst[i]) for i in range(N)]

dai_lst = []

tmp = A_lst[0]

for a in A_lst:
  if a<tmp:
    dai_lst.append(tmp-a)
  elif a>tmp:
    tmp = a

out = sum(dai_lst)
print(out)
from sys import stdin
import math
inp = lambda : stdin.readline().strip()

n = int(inp())
a = [int(x) for x in inp().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] - a[i]
print(ans)
N = int(input())
A = list(map(int,input().split()))
step = 0
height = 0
for i in range(N):
  if A[i] <= height:
    step += height - A[i]
  else:
    height = A[i]
print(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())
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 = 0
c = 0
for ai in a:
    if ai < h:
        c += h - ai
    h = max(ai, h)
print(c)
n = int(input())
aa = list(map(int, input().split()))
amax = aa[0]
total = 0
for a in aa:
    amax = max(amax,a)
    total += (amax - a)
print(total)
n = int(input())
a = list(map(int,input().split()))
x = 0
total = 0


for i in range(n-1):
    if a[i] >= a[i+1]:
        x = a[i] - a[i+1]
        total += x
        a[i+1] = a[i]
        i = i+1
    else:
        total += 0

print(total)
n = int(input())
a = list(map(int,input().split()))
m = a[0]
s = 0
for i in range(1,n):
    m = max(m,a[i])
    s += m-a[i]
print(s)
N = int( input() )
A = list( map( int, input().split() ))

ret = 0
bafore = A[0]
for a in A:
  if bafore > a:
    ret += bafore - a
  bafore = max( a, bafore )
    
print( ret )
N= int(input())
A = list(map(int, input().split()))
res = 0
current = A[0]
for i in range(1, N):
    res += (current - A[i]) if current > A[i] else 0
    current = current if current > A[i] else A[i]
print(res)
n = int(input())
num_list = list(map(int, input().split()))
a = int(num_list[0])
p = 0

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

print(ans)
N = int(input())
height = [int(i) for i in input().split()]
step = 0
for i, h in enumerate(height):
    if i == 0:
        pre = h
    else:
        if h < pre:
            step += (pre - h)
            pre = pre
        else:
            step += 0
            pre = h
print(step)
n = int(input())
As = list(map(int, input().split()))
mx = 0
ans = 0
for a in As:
    if mx < a:
        mx = a
    else:
        ans += mx - a
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]:
        b = a[i] - a[i+1]
        a[i+1] += b
        sum += b
print(sum)
n=int(input())
li = list(map(int,input().split()))

now=li[0]
sum=0
for i in li[1:]:
  if now>i:
    sum+=now-i
  elif i>now:
    now=i

print(sum)
def solve(n, arr):
    ans_arr = [0]
    for i in range(0, n-1):
        if arr[i] > arr[i + 1]:
            diff = arr[i] - arr[i + 1]
            ans_arr.append(diff)
            arr[i + 1] += diff
        else:
            ans_arr.append(0)
    return sum(ans_arr)

if __name__ == "__main__":
    n = int(input())
    arr = list(map(int, input().split(' ')))
    print(solve(n, arr))
N=int(input())
A=list(map(int,input().split()))

ans=0
mx=0
for x in A:
    if mx>=x:
        ans+=mx-x
    else:
        mx=x

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

max_value = max(A)
max_index = A.index(max_value)

c=0

for i in range(max_index,n):
    c+=(max_value-A[i])

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

print(c)
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
a = intl()

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()))

ans = 0
chk = a[0]
for x in a[1:]:
    if chk > x:
        ans += chk - x
    else:
        chk = x
print(ans)
#!/usr/bin/env python

from typing import List


def f(n: int, a_list: List[int]) -> int:
    """
    >>> f(5, [2, 1, 5, 4, 3])
    4
    """
    ans = 0

    n_max: int = a_list[0]
    for i, a_i in enumerate(a_list):
        if n_max > a_i:
            ans += n_max - a_i
        else:
            n_max = a_i

    return ans


if __name__ == '__main__':
    n = int(input())
    a_list = list(map(lambda x: int(x), input().split()))
    print(f(n, a_list))
N, *A = map(int, open(0).read().split())

result = 0
p = A[0]
for i in range(1, N):
    if p >= A[i]:
        result += p - A[i]
    else:
        p = A[i]
print(result)
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=LIST()
    ans=0
    mina=0
    for a in A:
        mina=max(mina,a)
        ans+=max(0,mina-a)
    
    print(ans)
    
if __name__ == '__main__':
    do()
N = int(input())
L = list(map(int, input().split()))

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

print(ans)
n = int(input())
a = list(map(int, input().split()))
ans = 0
highest = a[0]
for ai in a:
    if ai <= highest:
        ans += highest - ai
    else:
        highest = ai
print(ans)
N = int(input())
A = list(map(int,input().split()))
hight = A[0]
base = 0
for i in range(1,N):
  if A[i] < hight:
    base += hight - A[i]
  else:
    hight = A[i]
print(base)
N = input()
AL = list(map(int,input().split()))

Amax = 0
FD = 0

for Ai in AL:
#  print('FD=%d' % FD)
  if Amax > Ai:
#    print('1')
    FD = FD + (Amax - Ai)
  elif Amax < Ai:
#    print('2')
    Amax = Ai

print(FD)
N=int(input())
A=list(map(int,input().split()))
H=A[0]
ans=0
for i in range(N):
    ans+=max(0,(H-A[i]))
    H=max(H,A[i])
    #print(H)
print(ans)
import sys
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))
if N == 1:
  print(0)
else:
  x = 0
  for i in range(0,len(L)-1):
    if L[i] > L[i + 1]:
      x = x + (L[i] - L[i + 1])
      L[i + 1] = L[i + 1] + (L[i] - L[i + 1])
  print(x)
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)
try:
    k = int(input())
    l = list(map(int,input().split(' ')))
    c = 0
    for i in range(len(l)-1):
      x = l[i+1]-l[i]
      if x<0:
        l[i+1]+=abs(x)
        c+=abs(x)
    print(c)
except:
    pass
N = int(input())
A = map(int, input().split())
A = list(A)

stage_hight = []
for i, ai in enumerate(A):
	if i != 0 and ai < A[i-1]:
		stage_hight.append(A[i-1] - ai)
		A[i] = A[i-1]
print(sum(stage_hight))
n = int(input())
a = list(map(int,input().split()))
ans = 0
m = a[0]
for i in range(1,n):
  if a[i] <= m:
    ans += -a[i]+m
  else:
    m = a[i]
print(ans)
n = int(input())
p = list(map(int, input().split()))
a = 0
for i in range(1,n):
    if p[i-1] > p[i]:
        a = a + p[i-1] - p[i]
        p[i] = p[i-1]
    else:
        p[i] = p[i]
        a = a
print(a)
N=int(input())
A=[int(i) for i in 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)
ｎ = int(input())
a = list(map(int,input().split()))
cnt = 0
mx = a[0]

for i in range(1,n):
  if a[i] > mx:
    mx = a[i]
  if a[i] < mx:
    cnt = cnt + (mx - a[i])

print(cnt)
n = int(input())
a = [int(i) for i in input().split()]
c = 0
t = a[0]
for i in a:
  if t>i:
    b=t-i
    c+=b
  else:
    t=i

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

max_num = 0
step = []
for i in range(n):
    if a[i] >= max_num:
        max_num = a[i]
    else:
        step.append(max_num - a[i])
print(sum(step))
n = int(input())
a = list(map(int,input().split(" ")))
answer = 0
for i in range(1,n):
    if a[i] < a[i - 1]:
        answer += a[i-1] - a[i]
        a[i]= a[i - 1]

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

m=0
K=0
for i in range(N):
    if i==0:
        m=A[i]
    else:
        S=max(0,m-A[i])
        K+=S
        m=A[i]+S
print(K)
n = int(input())
a = list(map(int,input().split())) + [0]
ans = 0
maxh = a[0] 
for i in range(n):
    if maxh > a[i]:
        ans += maxh - a[i]
        a[i] = maxh
    else:
        maxh = a[i]
print(ans)
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)
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
    if front < height:
        front = height
print(step)
import heapq
import math

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

    res = 0
    max_a = A[0]
    for a in A[1:]:
        if max_a > a:
            res += (max_a - a)
        else:
            max_a = a

    print(res)

if __name__ == "__main__":
    main()
N = int(input())
A = list(map(int, input().split()))
step = [0] * N
for i, a in enumerate(A[1:], 1):
    step[i] = max(0, A[i - 1] + step[i - 1] - a)
print(sum(step))
n=int(input())
l=list(map(int,input().split()))
ans=0
m=l[0]
for i in l[1:]:
    if i<m:
        ans+=m-i
    else:
        m=i
print(ans)
def main():
  N = int(input())
  A = list(map(int, input().split()))
    
  m = A[0]
  S = 0

  for a in A:
      if m - a > 0:
          S += m - a
      if m < a:
          m = a

  print(S)


if __name__ == "__main__":
  main()
#!/usr/bin/env python3

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = 0
prev = None
for a in A:
    if prev is None:
        prev = a
        continue
    if prev > a:
        diff = prev - a
    else:
        diff = 0
    ans += diff
    prev = a + diff

print(ans)
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] = A[i-1]
    
print(sum)
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())
As = list(map(int, input().split()))
 
sum_humidai = 0
max_front_person = 0
  
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(max_front_person, As[person_idx - 1])
  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()))


sum_value = 0
for i in range(len(A)-1):
  if A[i] >= A[i+1]:
    x =  A[i] - A[i+1]
    A[i+1] = x + A[i+1]
    sum_value += x

print(sum_value)
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    n = ri()
    A = ril()
    h = 0
    ans = 0
    for a in A:
        dh = max(h-a, 0)
        h = a + dh
        ans += dh
    print(ans)

if __name__ == '__main__':
    main()
n=int(input())
lst=list(map(int,input().split()))
_max=lst[0]
st=0
for i in lst:
  if i>=_max:
    _max=i
  else:
    st=st+(_max-i)
print(st)
N = int(input())
A = list(map(int, input().split()))
cnt = 0

if N == 1:
    print(0)
    exit()

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

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

ans=0
max=a[0]
for i in range(1,n):
    if max>a[i]:
        ans+=max-a[i]
    else:
        max=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]:
        ans+=a[i-1]-a[i]
        a[i]=a[i-1]
print(ans)
N=int(input())
A=list(map(int,input().split()))
M=0
P=0
for i in range(N):
  P+=max(0,M-A[i])
  M=max(M,A[i])
print(P)
n = int(input())

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


before_max = a[0]
humidai = 0
humidai_sum = 0
for i in range(1,n):
    humidai = 0
    if(before_max >= a[i]):
        humidai = before_max - a[i]
        humidai_sum += humidai

    if(before_max < a[i]):
        before_max = a[i]
        

    #print(humidai,before_max,before_max)
print(humidai_sum)
n=int(input())
a=list(map(int,input().split()))
ans=0
maenomax=a[0]
for i in range(n):
    if maenomax > a[i]:
        ans += maenomax-a[i]
    elif maenomax < a[i]:
        maenomax=a[i]
print(ans)
# -*- coding: utf-8 -*-

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]


print(sum(l))
N = int(input())
A = list(map(int,input().rstrip().split(" ")))
ans = 0
minnow = A[0]
for i in range(1,N):
    if(minnow > A[i]):
        ans += minnow - A[i]
    else:
        minnow = A[i]
print(ans)
N = int(input())
A = list(map(int, input().split()))

mx = A[0]
ans = 0
for i in range(1, N):
    if A[i] < mx:
        ans += mx-A[i]
    elif A[i] > mx:
        mx = A[i]
print(ans)
n = int(input())
a = list(map(int, input().split()))
fumi_sum = 0
for i in range(1,n):
    if a[i] < a[i-1]:
        fumi_sum += a[i-1] - a[i]
        a[i] += a[i-1] - a[i]
print(fumi_sum)
N = int(input())
L = list(map(int,input().split()))

ans=0
for i in range(N-1):
    if L[i]-L[i+1]>0:
        ans += L[i]-L[i+1]
        L[i+1]=L[i]
print(ans)
N = int(input())
A = [int(i) for i in input().split()]
pre_a = A[0]
ans = 0

for a in A[1:]:
  ans += max(0, pre_a - a)
  pre_a = max(pre_a, a)

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



def calculate(n, arr):
    currentHeight = arr[0]
    result = 0
    for i in range(1,n):
        if arr[i] < currentHeight:
            result += (currentHeight - arr[i])
        else:
            currentHeight = arr[i]

    return result

res = calculate(N, ARR)

print(res)
n=int(input())

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


s=0
for i in range(1,n):
    if row[i-1]>row[i]:
        s+=row[i-1]-row[i]
        row[i]=row[i-1]
print(s)
N=int(input())

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

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

print(ans)
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]
        A[i+1] = A[i]

print(ans)
N=int(input())
s=[int(x) for x in input().split()]

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

tmp = a[0]
ans = 0
for i in a:
    if i < tmp:
        ans += tmp-i
    else:
        tmp = i
print(ans)
# coding: utf-8
# Your code here!

n = int(input())

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

dai = 0

for i in range(1,n):
    if a[i] < a[i-1]:
        dai += a[i-1] - a[i]
        a[i] = a[i-1]
        
print(dai)
N = int(input())
a = list(map(int, input().split()))
count = 0
i = 0
for i in range(N-1):
  if a[i] <= a[i+1]:
    continue
  elif a[i] > a[i+1]:
    diff = a[i] - a[i+1]
    count = count + diff
    a[i+1] = a[i]
print(count)
N = int(input())
people = [int(i) for i in input().split()]
step = 0
front = people[0]
for i in range(N):
    if front > people[i]:
        step += front - people[i]
    else:
        front = people[i]
print(step)
n=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(1,n):
    if a[i-1]>a[i]:
        cnt+=(a[i-1]-a[i])
        a[i]=a[i-1]
print(cnt)
n = int(input())
a = list(map(int, input().split()))
count = 0
i = 1
while i < len(a):
    if a[i] < a[i-1]:
        count += a[i-1] - a[i]
        a[i] = a[i-1]
    i += 1
print(count)
N=int(input())
A=list(map(int, input().split()))
h=0
for i in range(1,N):
    if A[i]<A[i-1]:
        h+=A[i-1]-A[i]
        A[i]+=A[i-1]-A[i]
print(h)
n = int(input())
a = list(map(int,input().split()))
bmax = a[0]
st = 0
for i in range(1,n):
	if a[i] < bmax:
		st += bmax - a[i]
	else:
		bmax = a[i]
print(st)
n = int(input())
a = list(map(int,input().split()))

t = a[0]
ans = 0

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

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

for i in range(N - 1):
  if A[i] > A[i + 1]:
    dif = A[i] - A[i + 1]
    sum += dif
    A[i +1] = A[i + 1] + dif
    
print(sum)
number = int(input())
tall = list(map(int, input().split()))
addition = 0
for each in range(1, number):
    difference = tall[each - 1] - tall[each]
    if difference >= 0:
        addition += difference
        tall[each] += difference
print(addition)
n = int(input())
L = list(map(int,input().split()))

maxh = L[0]
sum1 = 0
for i in range(1,n):
    if L[i] > maxh:
        maxh = L[i]
    else:
        sum1 += maxh-L[i]
print(sum1)
n=int(input())
heights=list(map(int,input().split()))
step=0

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

print(step)
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
a = lmiinps()

ans = 0

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

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)
#template
def inputlist(): return [int(j) for j in input().split()]
#template
N = int(input())
A = inputlist()
maxa = A[0]
ans= 0
for i in range(1,N):
    if A[i] < maxa:
        ans += maxa - A[i]
    else:
        maxa = A[i]
print(ans)
n = int(input())
a = list(map(int,input().split()))
m = a[0]
stool = 0
for i in range(1,n):
  if a[i]>m:
    m = a[i]
  stool = stool + m-a[i]
print(stool)
n = int(input())
h = [int(i) for i in input().split()]
m = h[0]
s = 0

for i in h:
  if(i > m):
    m = i
  else:
    s += m - i
    
print(s)
N = int(input())
l = input().split()

cnt = 0
current = int(l[0])
for i in range(1, N):
    now = int(l[i])
    if now >= current:
        current = now
    if now < current:
        cnt += current - now

print(cnt)
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

print(count)
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(1,len(a)):
    if a[i-1] > a[i]:
        ii = a[i-1] - a[i]       
        a[i] += ii
        ans += ii
print(ans)
N = int (input ())
A = [int (x) for x in input().split()]
X = A[0]
P = 0
for i in range (N-1):
  if A[i+1] < X:
    P += X-A[i+1]
  elif A[i+1] > X:
    X = A[i+1]
print (P)
N = int(input())
A = list(map(int,input().split()))
B = A[0]
ans = 0
for i in range(N):
  if B > A[i]:
    ans += abs(A[i] - B)
  B = max(A[i],B)
print(ans)
n=int(input())
a=list(map(int,input().split()))
tmp=a[0]
ans=0
for i in a:
  if tmp>i:
    ans+=tmp-i
  else:
    tmp=i
print(ans)
N = int(input())
A = list(map(int,input().split()))

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

print(ans)
def main():
    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)

if __name__ == "__main__":
    main()
def main():
    number_of_person = int(input())
    array = [int(x) for x in input().split()]
    pre_h = 0
    step_total = 0
    for height in array:
        step_total += max(pre_h - height, 0)
        pre_h = max(height, pre_h)
    return step_total


if __name__ == '__main__':
    ans = main()
    print(ans)
if __name__ == "__main__":
  n = int(input())
  ai = list(map(int, input().split()))
  li = []
  for i in range(n - 1):
    if ai[i] > ai[i + 1]:
      li.append(ai[i] - ai[i + 1])
      ai[i+1]=ai[i]
    else:
      pass
  print(sum(li))