peterverebics/CodeNet_Python_118 huggingface dataset:
N = int(input())
A = list(map(int, input().split()))

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

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

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

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

print(S)
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    ans = [0] * n
    for i in range(n-1):
        if a[i] <= a[i + 1]:
            continue
        else:
            ans[i+1] = a[i] - a[i+1]
            a[i+1] = a[i]

    ans = sum(ans)
    print(ans)


resolve()
n=int(input())
l=list(map(int,input().split()))
def solve(n,l):
     if n==1:
        return 0
     ans=0
     m=0
     for i in range(n):
         if l[i]>=m:
            m=l[i]
         else:
            ans+=max(0,m-l[i])
     return ans
print(solve(n,l))
n = int(input())
a = list(map(int,input().split()))

current = a[0]
steps = 0

for i in range(n):
    if a[i] < current:
        steps += current-a[i]
    else:
        current = a[i]

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

sum = 0
m = lis[0]
for i in lis:
    if m > i:
        sum += (m-i)
    else:
        m = i

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

ans=0
for i in range(1,n):
    if a[i-1]<=a[i]:
        continue
    else:
        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 = 0

for i in range(N):
    if max < A[i]:
        max = A[i]
    else:
        ans += 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 - 1] > a[i]:
        diff = a[i - 1] - a[i]
        ans += diff
        a[i] += diff
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)
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n = int(input())
    a = list(map(int, input().split()))

    #output
    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 = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()
n = int(input())
A = list(map(int,input().split()))
cnt=0
for i in range(n-1):
  if A[i+1] < A[i]:
    s = A[i]-A[i+1]
    A[i+1] += s
    cnt += s

print(cnt)
N = int(input())
dai = 0
A = list(input().split())
max = int(A[0])
for i in range(1, len(A)):
    if int(A[i]) < int(A[i-1]):
        dai += int(A[i-1]) - int(A[i])
        A[i] = A[i-1]
print(dai)
n = input()
aa = list(map(int, input().split()))
max_a = aa[0]
step = 0
for i in range(1, len(aa)):
    if max_a > aa[i]:
        # 踏み台必要
        step += max_a - aa[i]
    else:
        max_a = aa[i]
print(step)
if __name__ == "__main__":
    
    n = int(input())
    a = list(map(int, input().split()))

    target = 0
    ans = 0
    for h in a:
        if target <= h:
            target = h
        else:
            ans += target - h

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

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

print(ans)
n = int(input())
a =[int(x) for x in input().split()]
num = 0
min_num = a[0]
for i in a[1:]:
    if i < min_num:
        num += min_num-i
    else:
        min_num = i
print(num)
n = int(input())
a = list(map(int, input().split()))

daisum = 0
minh = 0
for i in a:
  dai = 0
  if i < minh:
    dai = minh - i
    daisum += dai
  elif i > minh:
    minh = i

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

cnt = 0
for i in range(len(A)):
    if i==0:
        continue

    if A[i-1]-A[i]>0:
        cnt +=A[i-1]-A[i]
        A[i]=A[i-1]

print(cnt)
import numpy as np

N=int(input())
A=list(map(int,input().split()))
F=[]
for i in np.arange(len(A)-1):
    if(A[i]>A[i+1]):
        F.append(A[i]-A[i+1])
        A[i+1]=A[i]

print(sum(F))
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

A = np.array(read().split(), np.int64)[1:]

B = np.maximum.accumulate(A)
print(np.sum(B - A))
n = int(input())
a = list(map(int,input().split()))
ans = 0

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

ans = 0
prev = A[0]
for i in range(N):
    if prev > A[i]:
        ans += prev-A[i]
    else:
        prev = A[i]
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]:
        sum += A[i] - A[i+1]
        A[i+1] = A[i]
        
print(sum)
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
nums = map(int, input().split(" "))

out = 0
for i, num in enumerate(nums):
    if i == 0:
        cur_max = num
    else:
        if cur_max > num:
            out += cur_max - num
        else:
            cur_max = num

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

step=0
for i in range(N):
    if i == 0:
        pass
    else:
        if A[i]-A[i-1]>=0:
            pass
        else:
            step += (A[i-1]-A[i])
            A[i] += (A[i-1]-A[i])
print(step)
N = int(input())
A = [int(x) for x in 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()))
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 = list(map(int, input().split()))

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

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

result = 0
tmp = a[0]

for i in range(1, n): 
  if tmp >= a[i]: 
    result += tmp - a[i]
  else: 
    tmp = a[i]

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

standard = height[0]
ans = 0
for i in range(n):
  if height[i] > standard:
    standard = height[i]
  elif height[i] < standard:
    ans += standard - height[i]

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

total = 0
amax = A[0]

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

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

an =0
ki=a[0]
for i in range(n-1):
    wk=ki-a[i+1]
    if wk>0:
        an+=wk
    ki=max(ki,a[i+1])
print(an)
N = int(input())
high_list = list(map(int,input().split()))
l = high_list[0]
count = 0
for i in range(N-1):
    i_a = high_list[i]
    i_b = high_list[i+1]
    if(i_a > i_b):
        dai = i_a - i_b
        i_b = i_b + dai
        high_list[i+1] = i_b
        count += dai
print(count)
N=int(input())
A=list(map(int,input().split()))
count=0
last=A[0]
for i in range(1,N):
    if A[i]<last:
        count+=last-A[i]
    else:
        last=A[i]
print(count)
N = int(input())
A = list(map(int,input().split()))
ans = 0

for i in range(1,N):
    diff = A[i - 1] - A[i]
    if diff <= 0:
        pass
    else:
        ans += diff
        A[i] += diff
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]:
        tmp=a[i-1]-a[i]
        a[i]+=tmp
        ans+=tmp
print(ans)
#!/usr/bin/env python3

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

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

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

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)
def readinput():
    n=int(input())
    a=list(map(int,input().split()))
    return n,a

def main(n,a):
    sum=0
    h=a[0]
    for i in range(1,n):
        if h>a[i]:
            sum+=h-a[i]
        else:
            h=a[i]
    return sum

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)
N = int(input())
A = list(map(int, input().split()))

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

print(step_sum)
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split(' ')))

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

print(sum(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()))
highest = 0
ans = 0
for a in A:
    if a >= highest:
        highest = a
    else:
        ans += highest - a
print(ans)
max_height = 0
step_sum = 0

input()
people_height_array = map(int, input().split())

for height in people_height_array:
    if max_height <= height:
        max_height = height
    else:
        step_sum += max_height - height

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

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

cnt = 0
prv = 0

for i in range(N):
    now = A[i]
    if prv > now:
        cnt += prv - now
    else:
        prv = now

print(cnt)
n,p,*v=map(int,open(0).read().split())
a=0
for t in v:
 if p>t:a+=p-t
 else:p=t
print(a)
N = int(input())
A = list(map(int,input().split()))

ans = 0
if N == 1:
    print(ans)
else:
    tmp_max = A[0]
    for a in A[1:]:
        if a < tmp_max:
            ans += tmp_max-a
        else:
            tmp_max = a

    print(ans)
# coding: utf-8
import math

num = int(input())
total = 0
max = 0
tall = list(map(int, input().split()))
for i in range(num):
    if max <= tall[i]:
        max = tall[i]
    if max > tall[i]:
        total += max -tall[i]

print(total)
#!/usr/bin/env python3

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

front_height = A[0]

ret = 0

for a in A[1:]:
    if a < front_height:
        ret += front_height - a
    front_height = max(front_height, a)

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

out = 0
t_h = A[0]

for i in A:
    a = max(t_h - i, 0)
    out += a
    t_h = i + a

print(out)
n = int(input())
x = list(map(int, input().split()))
result = 0
for i in range(n - 1):
  if x[i] > x[i + 1]:
    result += (x[i] - x[i + 1])
    x[i + 1] = x[i]
print(result)
n = int(input())
a = [int(i) for i in input().split()]
ans=0
for i in range(1,n):
  d = a[i-1] - a[i]
  if d>0:
    ans+= d
    a[i]=a[i]+d
  else:
    continue
    
print(abs(ans))
N = int(input())
L = list(map(int, input().split()))

count = 0
for i in range(N-1):
    if L[i] > L[i + 1]:
        difference = L[i] - L[i + 1]
        L[i+1] += difference
        count += difference

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

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

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

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()))
T = 0
ans = 0
for i in range(N):
  if A[T] >= A[i]:
    ans += A[T] - A[i]
  else:
    T = i
print(ans)
N = int(input())
A = [int(_) for _ in input().split()]

ans = 0
v = A[0]
for a in A:
    if a < v:
        ans += v - a
    v = max(v, a)
print(ans)
n=int(input())
a=[int(x) for x in input().split()]
sum=0
for i in range(len(a)-1):
    if a[i]>a[i+1]:
        sum+=a[i]-a[i+1]
        a[i+1]=a[i]
print(sum)
N = int(input())
A = list(map(int, input().split()))

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

    elif A[i] > maxtall:
        maxtall = A[i]

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

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

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

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

count=0

for n in range(1,N):
    if A[n]<A[n-1]:
        H = A[n-1]-A[n]
        count+=H
        A[n]=A[n-1]

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

print(ans)
n = int(input())
strNums = input().split()
intNums = [int(i) for i in strNums]
step = []
initialValue = intNums[0]
for i in range(len(intNums)):
    if intNums[i] < initialValue:
        step.append(initialValue - intNums[i])
    else:
        step.append(0)
        initialValue = intNums[i]
print(sum(step))
n = int(input())
m = list(map(int,input().split()))
count = 0
for i in range(n-1):
  if m[i]<=m[i+1]:
    pass
  else:
    s = m[i]-m[i+1]
    m[i+1]=m[i+1]+s
    count = count + s
print(count)
n=int(input())
s=list(map(int,input().split()))

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

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

now = 0
cost = 0
for A in As:
    if A > now:
        now = A
    else:
        cost += now-A
print(cost)
N = int(input())
a = [int(s) for s in input().split()]
b = int(0)

for i in range(N-1):
  if a[i] <= a[int(i+1)]:
    pass
  else:
    b = b + (a[i] - a[int(i+1)])
    a[int(i+1)] = a[i]

print(b)
input_n = input()
N = str(input_n)

input_a = input()
A = str(input_a)

listN = A.split(' ')

listA = []

for value in range(len(listN)):
    listA.append(int(listN[value]))

result = 0

for value in range(1, len(listA)):
    if listA[value] < listA[value - 1]:
        result += (int(listA[value - 1]) - int(listA[value]))
        listA[value] += (int(listA[value - 1]) - int(listA[value]))

print(str(result))
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    ans=0
    ceil=A[0]
    for i in range(N):
        if A[i]>ceil:
            ceil=A[i]
        else:
            ans+=ceil-A[i]
            
    print(ans)

main()
n=int(input())
a=list(map(int, input().split()))
m=0
ans=0
for i in a:
    if m>i:
        ans+=m-i
    else:
        m=i
print(ans)
n = int(input())

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


highest = 0
ans = 0

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

print(ans)
def resolve():
    N = int(input())
    A = list(map(int,input().split()))
    sm =0
    for i in range(1,N):
        if A[i]<A[i-1]:
            sm += A[i-1]-A[i]
            A[i]=A[i-1]
    print(sm)
resolve()
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] - A[i+1]
    
print(cnt)
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())
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()))

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

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

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)
# -*- coding: utf-8 -*-
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 sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    forward = 0
    for i in range(n):
        if a[i] < forward:
            ans += forward - a[i]
        else:
            forward = a[i]
    print(ans)


if __name__ == "__main__":
    main()
N = int(input())
A = list(map(int,input().split()))
num_b = 0
cnt = 0
for i in A:
    if num_b > i:
        cnt += num_b - i
    else:
        num_b = i
print(cnt)
N = int(input())
A = list(map(int, input().split()))

step = 0
before_max = A[0]

for i in range(1, N):
    if (A[i] >= before_max):
        before_max = A[i]
    else:
        step += before_max - A[i]

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

ans = 0
maximum = A[0]
for i in range(1,N):
    if A[i] < maximum:
        ans += (maximum - A[i])
    else:
        maximum = A[i]
print(ans)
# coding:utf-8
n = int(input())
a = list(map(int, input().split()))
ans = 0
tall = 0

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

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

b = As[0]
rlt = 0

for i in range(1,N):
  if b > As[i]:
    rlt += b-As[i]
  else:
    b = As[i]
    
print(rlt)
N = int(input())
A = [int(s) for s in input().split()]
ma = A[0]
total = 0
for a in A:
  if a < ma:
    total += ma - a
  ma = max(ma, a)
print(total)
n=int(input())
l=list(map(int,input().split()))
sum=0
m=l[0]
for i in range(1,n):
  m=max(m,l[i])
  sum+=m-l[i]
print(sum)
N = int(input())
A = [int(a) for a in input().split(" ")]

ans = 0
for i in range(len(A)):
  if i == 0:
    continue
  else:
    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()))
h = 0
for i in range(1, len(a)):
    abs = a[i-1] - a[i]
    if(abs > 0):
        h += abs
        a[i] = a[i-1]
print(h)
n = int(input())
a_list = list(map(int, input().split()))
result = [0]
for i in range(n - 1):
    if a_list[i] + result[i] > a_list[i + 1]:
        result.append(a_list[i] + result[i] - a_list[i + 1])
    else:
        result.append(0)
print(sum(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 += pre - now
        pre = now + (pre - now)
    else:
        pre = now
print(ans)
n = int(input())
A = list(map(int, input().split()))

ans = 0
prev = 0
for a in A:
    if prev <= a:
        prev = a
        continue
    ans += prev - a
print(ans)
N = int(input())
A = map(int , input().split())
B = []

flg = True
before = 0
for a in A:
    if flg:
        flg = False
        B.append(0)
        before = a
    else:
        add = max(before - a, 0)
        B.append(add)
        before = a + add

print(sum(B))
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
x = ip()
mx = 0
ans = 0
for i in x:
    mx = max(mx,i)
    ans += mx-i
print(ans)
n = int(input())
a = list(map(int, input().split()))
m = 0
ans = 0
for i in range(n):
    if i > 0:
        ans += max(0, m - a[i])
    m = max(a[i], m)
print(ans)
N = int(input())
A = list(map(int, input().split()))

B = [0]*N

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

ans = sum(B)
print(ans)
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N-1):
    delta = A[i+1]-A[i]
    if delta < 0:
        A[i+1] += -delta
        ans += -delta
print(ans)
n = int(input())
a = list(map(int,input().split()))
mini = a[0]
ans = 0
for i in range(n):
    if a[i]>mini:
        mini = a[i]
    else:
        ans += mini-a[i]
print(ans)
n = int(input())
arr = list(map(int,input().split()))
cur_max = arr[0]
ans = 0
for i in range(1,n):
    if arr[i] <= cur_max:
        ans += abs(arr[i]-cur_max)
    else:
        cur_max = arr[i]
print(ans)
# [] = list(map(int, input().split()))

n = int(input())
aList = list(map(int, input().split()))
hList = aList.copy()
header = aList[0]
for index, item in enumerate(aList[1:]):
    if header > item:
        hList[index+1] = header
    else:
        header = item
dif = 0
for i in range(n):
    dif += hList[i] - aList[i]
print(dif)
N=int(input())
A = list(map(int,input().split()))	
TOT = 0
MAX = 0
for i in range(N):
  if MAX < A[i]:
    MAX = A[i]
  else:
    TOT += MAX - A[i]
print(TOT)
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]:
            continue
        else:
            cnt = A[i-1] - A[i]
            ans += cnt
            A[i] += cnt
    print(ans)

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

min_h, sum_h = 0, 0
for a in A:
  if min_h < a:
    min_h = a
  sum_h += min_h - a

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

if __name__ == "__main__":
    solve()
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))

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

print(ans)
n =  int(input())
a = [int(x) for x in input().split()]
c = 0
for i in range(n-1):
    if a[i] > a[i+1]:
        c += a[i] - a[i+1]
        a[i+1] = a[i]
print(c)
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]-A[i+1]

print(cnt)
n=int(input())
A=[int(_) for _ in input().split()]
c=A[0]
ans=0
for a in A:
    if a<c:
        ans+=c-a
    else:
        c=a

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

s = 0
x = 0
for i in A:
    if i < x:
        s += x - i
    else:
        x = i
print(s)