peterverebics/CodeNet_Python_118 huggingface dataset:
n=int(input())
arr=list(map(int,input().split()))
ma=0
ans=0
for i in range(1,n):
    if arr[i]<arr[i-1]:

        ans+=arr[i-1]-arr[i]
        arr[i] = arr[i - 1]
print(ans)
import numpy as np
from numba import jit
N = int(input(''))
A1 = np.array(list(map(int, input('').split(' '))))

@jit(nopython=True)
def func(A):
    step = 0
    for k in range(0,N-1):
        if A[k] > A[k+1]:
            add = A[k] - A[k+1]
            step += add
            A[k+1] += add
    return step
        
print(func(A1))
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(1,n):
    ans += max(a[i-1]-a[i],0)
    a[i] = max(a[i-1],a[i])
print(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]
print(total)
n = int(input())
A = list(map(int,input().split()))

x = A[0]
ct = 0
for a in A:
    if x < a:
        x = a
    elif x > a:
        ct += (x-a)
print(ct)
n=int(input())
a=list(map(int,input().split()))
mx=0
s=0
for i in a:
  if mx<i:
    mx=i
  s+=mx-i
print(s)
n = int(input())
a = list(map(int,input().split()))

res = 0
pre = a[0]
for i in range(1, n):
    if pre <= a[i]:
        pre = a[i]
        continue
    res += pre - a[i]
print(res)
n = int(input())
a = list(map(int, input().split()))
k = 0
m = a[0]
for i in range(1, n):
    if m > a[i]:
        k += m-a[i]
    else:
        m = a[i]
print(k)
def solve(n,arr):
    maxh = 0
    h = 0
    for i in range(1,n):
        curr = arr[i]
        curh = arr[i-1]
        f = max(curr,curh)
        maxh = max(f,maxh)

        if curr < maxh:
            h += maxh - curr

        # print(f"curr:{curr},curh:{curh},maxh:{maxh}")
        # print(f"h:{h}")

    return h
        



n = int(input())
arr = list(map(int, input().strip().split()))

print(solve(n,arr))
n=int(input())
a=list(map(int, input().split()))
res=0
max=a[0]
for i in range(1,n):
    if a[i]<max:
        res+=max-a[i]
    else:
        max=a[i]
print(res)
import sys
N = sys.stdin.readline()
Alst = list(map(int, sys.stdin.readline().split()))
now = 1
x = 0
for i in Alst:
    if now < i:
        now = i
    else:
        x += (now - i)

print(x)
N=int(input())
A=list(map(int, input().split()))
c=0

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

print(c)
N = int(input())
L = list(map(int, input().split()))
ma = max(L)
mi = L.index(ma)
S = 0
nma = L[0]

for i in range(1, len(L)):
  if mi <= i:
    S += ma - L[i]
  else:
    nma = max(nma, L[i])
    S += nma - L[i]
print(S)
n = int(input())
a = list(map(int,input().split()))
cnt = 0
for i in range(n-1):
    if a[i+1]<a[i]:
        cnt += a[i]-a[i+1]
        a[i+1] = a[i]
print(cnt)
n = int(input())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    max_height_idx = 0
    continue

  if an >= max_height:
    max_height = an
    max_height_idx = i
    continue

  result += (max_height - an)

print(result)
N = int(input())
A = list(map(int, input().split()))
ap = 0
xp = 0
xcount = 0

for i in range(N):
    a = A[i]
    x = (ap+xp)-a
    if x <= 0:
        xp = 0
    else:
        xp = x
    xcount = xcount + xp
    ap = a
print(xcount)
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

    arr = A[0]
    ans = 0
    for i in A:
        ans += abs(arr - i) if i <= arr else 0
        arr = max(arr, i)
    print(ans)


if __name__ == "__main__":
    main()
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = int(input())
a_list = np.array(list(map(int, input().split())))

a_mx = np.maximum.accumulate(a_list)

ans = (a_mx - a_list).sum()

print(ans)
def main():
    n = int(input())
    ai = list(map(int, input().split()))
    cnt = 0
    
    for i in range(n):
        if i != n-1:
            if ai[i] > ai[i+1]:
                cnt += ai[i]-ai[i+1]
                ai[i+1] = ai[i]
    print(cnt)
    
if __name__ == "__main__":
    main()
from sys import stdin
input = stdin.readline

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

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

count=0
high=0

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

for i in range(N-1):
    v1, v2 = A[i], A[i+1]
    if v1<=v2:
        continue
    ans += v1-v2
    A[i+1] = v1
print(ans)
import math
def main():
    n = int(input())
    a_li = list(map(int,input().split()))
    ans = 0
    for i in range(1,n):
        if(a_li[i] >= a_li[i-1]):
            continue
        ans += a_li[i-1] - a_li[i]
        a_li[i] = a_li[i-1]
    print(ans)
    
        
if __name__ == "__main__":
    main()
n = input()
a_list = list(map(int, input().split())) 
cnt = 0
tmp = 0

for a in range(len(a_list)-1):
    if (a_list[a] > a_list[a+1]):
        cnt += a_list[a] -a_list[a+1]
        a_list[a+1] += a_list[a] -a_list[a+1]
        # print(cnt)

print(cnt)
n=int(input())
a=list(map(int,input().split()))
dai=[0]*n
x=a[0]
for i in range(n-1):
    if a[i+1]-x<0:
        dai[i+1]=x-a[i+1]
        x=a[i+1]+dai[i+1]
    else:
        x=a[i+1]
print(sum(dai))
N = int(input())
As = list(map(int, input().split()))
ans = 0

tall = int(As[0])

for A in As:
	if(tall > A):
		ans += tall - A
	else:
		tall = A

print(ans)
n=int(input())
a=[int(i) for i in input().split()]
max_height=0
ans=0
for i in a:
  if max_height>i:
    ans+=max_height-i
  else:
    max_height=i
    
print(ans)
N, *A = map(int, open(0).read().split())

ans = []
p = A[0]
for i in range(N):
    if i == 0:
        continue
    if p > A[i]:
        ans.append(p - A[i])
    else:
        p = A[i]

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

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

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

lmax = a[0]
ans = 0
for i in a:
    ans += max(0, lmax-i)
    lmax = max(lmax, i)
print(ans)
N = int(input())
A = list(map(int, input().split()))

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

print(ans)
n = int(input())
a = list(map(int,input().split()))
max = 0
sum = 0
for i in range(n):
  if a[i] > max:
    max = a[i]
  else:
    sum += (max - a[i]) 
print(sum)
n=int(input())
L=list(map(int,input().split()))
max=0
ans=0
for i in range(n):
	if max>=L[i]:
		ans+=max-L[i]
	else:
		max=L[i]
print(ans)
n = int(input())
a = list(map(int, input().split()))
b = [0] * n
b[0] = 0
for i in range(1, n):
    if a[i-1] + b[i-1] > a[i]:
        b[i] = a[i-1] + b[i-1] - a[i]
    else:
        b[i] = 0
c = 0
for i in range(1, n):
    c = c + b[i]
print(c)
n = int(input())
ls = list(map(int,input().split()))
sm = 0
for i in range(1,n):
    if ls[i] < ls[i-1]:
        sm += ls[i-1]-ls[i]
        ls[i] = ls[i-1]
print(sm)
n=int(input())
a=list(map(int, input().split()))
l=a[0]
sm=0
for h in a:
  if(h<l):
    sm+=(l-h)
  elif(h>l):
    l=h
print(sm)
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
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)
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)
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N):
  if i == 0:
    tmp = A[i]
  else:
    if tmp > A[i]:
      ans += (tmp - A[i])
    else:
      tmp = A[i]
print(ans)
N, A = input(), [int(v) for v in input().split()]

# max(abs(x-y) for x, y in  zip(A, A[1:]))

stool = A[0]
ans = 0
for i, h in enumerate(A):
    if h < stool:
        ans += stool - h
    stool = max(stool, h)

print(ans)
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
ans=0
tmp=A[0]
for x in A[1:]:
    if x<tmp:
        ans+=tmp-x
    tmp=max(tmp,x)
print(ans)
s = int(input())
arr = list(map(int, input().split()))

ans = 0

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

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

level = 0
step = 0
for i in range(n):
  if level < a[i]:
    level = a[i]
  else:
    step += level - a[i]
print(step)
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

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

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

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

ans = 0
maximum = 0
for num in nums:
    maximum = max(maximum, num)
    ans += maximum - num

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

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

h=0
k=0

for i in range(n):
  if h<=li[i]:
    h=max(h,li[i])
  else:
    k=k+(h-li[i])
    h=max(h,li[i])
    
print(k)
N = int(input())
N_List = list(map(int,input().split()))
ct = 0
for i in range(1,N):
    if N_List[i] < N_List[i-1]:
        ct += N_List[i-1] - N_List[i]
        N_List[i] = N_List[i-1]
    

print(ct)
n = int(input())
num = list(map(int,input().split()))
ans = 0
m = num[0]

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

ans = 0
cur_max=0

for x in a:
  if cur_max > x:
    ans += cur_max - x
  cur_max = max(cur_max, x)
print(ans)
# -*- coding: utf-8 -*-

def main():
    N = int(input())
    A  = [int(i) for i in input().split()]
    ans = 0
    max = A[0]
    for a in A:
        if max > a:
            ans += max-a
        else:
            max = a
    print(ans)


if __name__ == "__main__":
    main()
n = int(input())
A = [0]+list(map(int,input().split()))
ans = 0
for i in range(1,n+1):
    d = A[i-1] -A[i]
    if(d >0):
        A[i] += d 
        ans += d
        
print(ans)
# C - Step

from itertools import accumulate
import operator

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

b = accumulate(a, max)
s = sum(map(operator.sub, b, a))
print(s)
N = int(input())
A = [int(x) for x in input().split()]

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

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

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

sum_min = 0

before_height = A[0]

for a in A[1:]:
    if before_height <= a:
        before_height = a
    else:
        sum_min += before_height - a
print(sum_min)
n = int(input())
la = [int(w) for w in input().split()]

ans = 0
h = la[0]

for a in la[1:]:
    if a <= h:
        ans += h-a
    else:
        h = a

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

print(step)
N = int(input())
arr = list(map(int, input().split()))
length = 0
for i in range(N-1):
    if arr[i+1] < arr[i]:
        length +=  arr[i] - arr[i+1]
        arr[i+1] += arr[i] - arr[i+1]
print(length)
N = int(input())
h = list(map(int, input().split()))
s = 0
for i in range(N-1):
    d = h[i] - h[i+1]
    if d > 0:
        s += d
        h[i+1] = h[i]
print(s)
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""

# %% import phase

# %% define phase

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

# %% process phase
ans = 0
f = 0
for ai in a:
    if f <= ai:
        f = ai
    else:
        ans += f - ai


# %%output phase
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()))

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

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

print(ans)
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    A=list(mi())

    mx = 0
    ans = 0
    for i in range(N):
        if mx > A[i]:
            ans += mx - A[i]
            A[i] = mx
        
        mx = max(mx,A[i])

    print(ans)

        
        






if __name__ == "__main__":
    main()
n = int(input())
a = list(map(int, input().split()))
temp, ans = a[0], 0
for i in range(1, n):
    if a[i] < temp:
        ans += temp - a[i]
    else: temp = a[i]
print(ans)
N = int(input())
A = list(map(int, input().split()))
ans = 0
max_a = 0
for ai in A:
    max_a = max(ai, max_a)
    ans += (max_a-ai)

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

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

print(ans)
N = int(input())
List = list(map(int,input().split()))
counter = 0
height = List[0]
for i in range(1,N):
  if List[i] < height:
    counter += height - List[i]
  else:
    height = List[i]
print(counter)
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()

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

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


main()
n = input()
heights = map(int, input().split())

max_height = 0
step_sum = 0

for i in heights:
    if max_height < i:
        max_height = i
    if max_height > i:
        step_sum += max_height - i
print(step_sum)
N = int(input())
A = [int(x) for x in 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)
n = int(input())
A = list(map(int, input().split()))

ans = 0
max = A[0]

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

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

ans = 0
max_a = a[0]
for i in range(1,n):
    if max_a > a[i]:
        ans += max_a - a[i]
    else:
        max_a = a[i]
print(ans)
_ = input()
A = [int(i) for i in input().split()]

ans = 0
for i in range(len(A) - 1):
  h = max(A[i] - A[i + 1], 0)
  if h != 0:
    A[i + 1] += h
  ans += h

print(ans)
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    temp = max(a[i - 1] - a[i], 0)
    a[i] += temp
    ans += temp
print(ans)
x = int(input())
lst = list(map(int, input().split()))
i = 1
cnt = 0
a = lst[0]
if x == 1:
    print(0)
else:
    while True:
        b = lst[i]
        if b < a:
            cnt += abs(b - a)
            a = a
        else:
            a = b
        if i >= x - 1:
            break
        i += 1
    print(cnt)
def main():
  N=input()
  arr=list(map(int,input().split()))
  sum=0
  for i in range(1,int(N)):
    if arr[i] < arr[i-1]:
      sum+= (arr[i-1]-arr[i])
      arr[i]=arr[i-1]
  print(sum)

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

top = A[0]
score = 0
for a in A[1:]:
  if a <= top:
    score += (top - a)
  else:
    top = a
print(score)
#!/usr/bin/python3

"""
https://atcoder.jp/contests/abc176/tasks/abc176_c
Step
"""


def solve(a, n):
    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]

    return ans

if __name__ == "__main__":
    n = int(input())
    a = [(lambda x: int(x))(x) for x in input().split()]

    print(solve(a, n))
import math
n = int(input())
alist = list(map(int,input().split()))

maxmin = -math.inf

board = 0
for i in range(n):
    a = alist[i]

    if a > maxmin:
        maxmin = a
    else:
        board += maxmin - a

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

temp = alist[0]
count = 0
for a in alist[1:]:
    if a < temp:
        count += temp - a
    elif a > temp:
        temp = a

print(count)
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    high = 0
    ans = 0
    for a in A:
        if high > a:
            ans += high - a
        elif high < a:
            high = a
    print(ans)

if __name__=='__main__':
    main()
n=int(input())
a=list(map(int,input().split()))
count=0
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=list(map(int,input().split()))
b=A[0]
c=0
d=len(A)

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

print(c)
N=int(input())
A=list(map(int,input().split()))
a=0
b=A[0]
for i in range(1,N):
    if b<=A[i]:
        b=A[i]
    elif b>A[i]:
        a+=(b-A[i])
        A[i]=b
        b=A[i]
print(a)
l = int(input())
xs = [int(x) for x in input().split()]
step = 0
max = 0
for x in xs:
    # This x must be the max in the preceeding numbers
    if x > max:
        max = x
        continue  # Current max, so no step is needed
    step += max - x
print(step)
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(1, N):
    t = A[i - 1] - A[i]
    if t > 0:
        ans += t
        A[i] += t
print(ans)
n = int(input())
a = list(map(int,input().split()))

ans = 0
prev_max=a[0]

for i in a:
    if prev_max > i:
        ans+= prev_max-i
    elif prev_max < i:
        prev_max = 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)
N = int(input())
A = list(map(int,input().split()))

sum = 0

for i in range(len(A)-1):
  if A[i+1] < A[i]:
    sum += A[i] - A[i+1]    
    A[i+1] = A[i]

  else:
    pass
print(sum)
def main():
    n = int(input())
    A = list(map(int,input().split()))
    front = 0
    ans = 0
    for a in A:
        if front < a:
            front = a
        else:
            ans += front - a
    print(ans)
    return

if __name__ == '__main__':
    main()
x=int(input())
l=list(map(int,input().split(" ")))
sum=0
mx=-1000
for i in range(len(l)-1):
    mx=max(mx,l[i])
    if l[i+1]<mx:
        sum+=mx-l[i+1]

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

sum_of_Humidai = 0
for i in range(1, len(ARR)):
    diff = (ARR[i - 1] - ARR[i])
    if diff > 0:
        ARR[i] += diff
        sum_of_Humidai += diff
else:
    print(sum_of_Humidai)
N = int(input())
A = list(map(int, input().split()))
count = 0
now = A[0]

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

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

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

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

f_sum = 0
prev = a[0]
for i in range(n-1):
    if prev > a[i+1]:
        f_sum += prev - a[i+1]
    else:
        prev = a[i+1]
print(f_sum)
n=int(input())
*a,=map(int, input().split())
mx=0
ans=0
for i in range(n):
    if mx>a[i]:
       ans+=mx-a[i]
    else:
        mx=a[i]
print(ans)
N = int(input())
A_list = input().split()
A_list = [int(x) for x in A_list]

sum = 0
tmp_mae = A_list[0]
for i in range(1,N):
    tmp_ushiro = A_list[i]
    if(tmp_mae > tmp_ushiro):
        sabun = tmp_mae - tmp_ushiro
        sum += sabun
    else:
        sabun = 0
    tmp_mae = tmp_ushiro + sabun

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

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

print(cnt)
n=int(input())
a = list(map(int,input().split()))
b = a[0]
total=0
for i in a:
  if i<b:
    total+=b-i
  else:
    b=i
print(total)
from sys import stdin
readline = stdin.readline
n = int(readline())
a = list(map(int, readline().split()))
x = 0
ans = 0
for i in a:
    if x > i:
        ans += (x-i)
    else:
        x = i
print(ans)
from math import gcd

from math import factorial as f

from math import ceil, floor, sqrt
import math

import bisect
import re
import heapq

from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

yes = "Yes"
no = "No"


def main():
    n = ii()
    a = li()
    ans = 0
    m = a[0]
    for i in range(1, n):
        ans += max(0, m - a[i])
        m = max(m, a[i])
    print(ans)


main()