N persons are standing in a row. The height of the i-th person from the front is A_i.
We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:
Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.
Find the minimum total height of the stools needed to meet this goal.

-----Constraints-----
 - 1 \leq N \leq 2\times 10^5
 - 1 \leq A_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 \ldots A_N

-----Output-----
Print the minimum total height of the stools needed to meet the goal.

-----Sample Input-----
5
2 1 5 4 3

-----Sample Output-----
4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.
We cannot meet the goal with a smaller total height of the stools.

Difficulty: introductory

Starter code:

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

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 = input().split()
a = [ int(i) for i in a ]
num = 0
maxa = a[0]
for i in range(1, len(a) ):
  if maxa < a[i]:
    maxa = a[i]
  elif maxa > a[i]:
    num += maxa - a[i]
  else:
    pass
print(num)
  
    
    



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

from typing import List


def answer(n: int, a: List[int]) -> int:
    height_sum = 0
    current_tallest = 0

    for i in a:
        if i < current_tallest:
            height_sum += current_tallest - i
        elif current_tallest < i:
            current_tallest = i

    return height_sum


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print((answer(n, a)))


def __starting_point():
    main()

__starting_point()

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

N = int(input())
A = list(map(int, input().split()))
A_sub = A[1:]
a_before = A[0]
count = 0
for a in A_sub:
    if a < a_before:
        count += a_before - a
    else:
        a_before = a
print(count)

n = int(input())
a = list(map(int,input().split()))
tall = 0
ans = 0
for an in a:
  if an < tall:
    ans += tall - an
  elif an > tall:
    tall = an
print(ans)

s=int(input())
x=list(map(int,input().split()))
sum=0
maxi=x[0]
for i in x:
  maxi=max(maxi,i)
  sum+=(maxi-i)
print(sum)

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)

# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

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

ans = 0
m = AN[0]
for i in range(1, N):
    if m > AN[i]:
        ans += m - AN[i]
    m = max(m, AN[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()))
result=0
minimum=0
for a in A:
  if a>minimum:
    minimum=a
  elif a<minimum:
    result+=minimum-a
print(result)

n = int(input())
a = list(map(int, input().split()))
max = a[0]
ans = 0

for i in range(1, n):
    if max < a[i]:
        max = a[i]

    if a[i] < max:
        ans += max - a[i]

print(ans)

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

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

max_n = A[0]
ans = 0
for i in range(N):
    tmp = max_n - A[i]
    if tmp > 0:
        ans += tmp
    if max_n < A[i]:
        max_n = A[i]
print(ans)
    


# -*- coding: utf-8 -*-


# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return list(map(int, input().split()))


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    N = input_int()
    A = input_to_int_tuple()

    result = 0
    max_a = 0
    for a in A:
        if max_a > a:
            result += max_a - a
        max_a = max(a, max_a)

    return result


def __starting_point():
    print((main()))

__starting_point()

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

tmp = x[0]
tot = 0


for i in x:
  if i <= tmp:
    tot += tmp - i
  else:
    tmp = i


print(f"{tot}")

def c176(n, alist):

    ans = 0
    tmp = alist[0]

    for i in alist:
        if tmp <= i:
            tmp = i
            continue
        ans += tmp - i

    return ans

def main():
    n = int(input())
    alist = list(map(int, input().split()))
    print(c176(n, alist))

def __starting_point():
    main()
__starting_point()

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

max_height = -1
s = 0

for i in a:
    max_height = max(max_height, i)
    d = max_height - i
    s += d

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

_,A=open(0);m=x=0
for a in map(int,A.split()):x+=max(m-a,0);m=max(m,a)
print(x)

n = int(input())
A = list(map(int,input().split()))
ans = 0
prev_h = 0
for i,h in enumerate(A):
    if i == 0:
        prev_h = h
    else:
        if h < prev_h:
            ans += prev_h - h
        else:
            prev_h = h

print (ans)


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

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

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

print(ans)


n = int(input())

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

h = 0
ans = 0

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


print(ans)


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

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

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


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

ans = 0
h = 0
for x in a:
	ans += max(0, h-x)
	h = max(h, x)

print(ans)


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

ans = 0
max_num = 0

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

print(ans)


# -*- coding: utf-8 -*-
n=int(input())
alist=list(map(int,input().split()))
ans=0
b=0
c=0
k=0
for i in range(n):
  b+=alist[i]
  c+=alist[0+k]
  if b<c:
    ans+=c-b
    b=c
  elif b>c:
    k=i
    c=b
print(ans)

n=int(input())
s=0
x=list(map(int,input().split()))
z=x[0]
for i in range(len(x)):
    if(z>x[i]):
        s+=z-x[i]
    else:
        z=x[i]
print(s)

def solve(n, a):
    res, th = 0, 0
    for x in a:
        th = max(th, x)
        res += th - x
    return res

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

import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


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


def __starting_point():
    main()

__starting_point()

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]-a[i+1]
print(cnt)


# 1行目の入力は使用しない
input()
# 2行目の入力は整数の配列として取得
A = list(map(int, input().split()))

# 最大のAiを設定
max_A = 0
# output
sum_A = 0

for Ai in A:
    max_A = max(Ai, max_A)
    if max_A > Ai:
        sum_A += max_A - Ai

print(sum_A)

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

import sys
from sys import stdin
def I():
    return stdin.readline().rstrip()
def MI():
    return map(int,stdin.readline().rstrip().split())
def LI():
    return list(map(int,stdin.readline().rstrip().split()))
#main part
n=int(I())
a=LI()
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()))
s=0
for i in range(1,n):
  if a[i-1] > a[i]:
    s+=a[i-1]-a[i]
    a[i] = a[i-1]
    
print(s)


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

N = int(input())
An = list(map(int, input().split()))
m = 0
ans = 0
for a in An:
  if a < m:
    ans += m - a
  else:
    m = a
print(ans)

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


def main():
    N = int(input())
    a = list(map(int, input().split()))
    
    DP = [0] * N
    max_i = 0
    
    for i in range(1, N):
        if a[max_i] > a[i]:
            DP[i] += (a[max_i]-a[i])
        elif a[max_i] < a[i]:
            max_i = i
            
    
    print(sum(DP))

def __starting_point():
    main()
__starting_point()

N = int(input())
nums = [int(i) for i in input().split()]

before = 0
ans = 0
for i in range(N):
    if i == 0:
        before =nums[i]
        continue
    if before > nums[i]:
        ans +=before - nums[i]
    else:
        before = nums[i]

print(ans)


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


n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(len(a) - 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()))

height = 0
for i in range(n - 1):
  if a[i] > a[i + 1]:
    height += a[i] - a[i + 1]
    a[i + 1] = a[i]
print(height)

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


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

n = int(input())
a = list(map(int,input().split()))
ans = 0
if a == sorted(a):
    print(ans)
    return
b = a.index(max(a))
for i in range(0,b):
    if a[i] > a[i+1]:
        ans = ans + a[i]-a[i+1]
        a[i+1] = a[i]
ans = ans + max(a)*len(a[b+1:])-sum(a[b+1:])
print(ans)

N=int(input())
A=list(map(int,input().split()))
a=0
s=[]
for i in A:
  if i<a:
    s.append(a-i)
    
  else:
    a=i
print((sum(s)))


import sys

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

m = A[0]
count = 0
for a in A:
    if m > a:
        count += m-a
    else:
        m = a
print(count)


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

ans = 0

for i in range(n-1):
  if A[i+1] >= A[i]:
    pass
  elif A[i+1] < A[i]:
    ans += A[i] - A[i+1]
    A[i+1] += A[i] - A[i+1]

print(ans)

n = int(input())
aaa = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    diff = aaa[i] - aaa[i - 1]
    if diff < 0:
        aaa[i] -= diff
        ans -= diff
print(ans)

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

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

A_max = A_list[0]
ans = 0

for i in(A_list):
  if i > A_max:
    A_max = i
  elif i < A_max:
    ans += A_max - i

print(ans)

#abc176c
n=int(input())
a=list(map(int,input().split()))
res=0
for i in range(1,n):
 d=max(a[i-1]-a[i],0)
 res+=d
 a[i]+=d
print(res)


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]-A[i+1]

print(ans)


n = int(input())
a = list(map(int, input().split(" ")))
a_max = 0
step = 0

for i in range(n):
    if a_max < a[i]:
        a_max = a[i]
        a_num = i
    else:
        step += a_max - a[i]

print(step)

n = int(input())
l = list(map(int,input().split()))
al = []
 
for i in range(0,n-1):
  if(l[i]<=l[i+1]):
    al.append(0)
  else:
    al.append(l[i]-l[i+1])
    l[i+1] = l[i]

print(sum(al))

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

N = int(input())
A = list(map(int, input().split()))
tot = 0
for i, a in enumerate(A):
    if i == 0:
        forward = a
        continue
    if forward >= a:
        tot += forward - a
    else:
        forward = a
print(tot)

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

ANS = 0
tall = 0
pre = 0

for i in range(N):
  if tall > num_list[i]:
    pre = (tall - num_list[i])
    ANS += pre
    tall = num_list[i] + pre
  else:
    tall = num_list[i]

print(ANS)


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

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

cnt = 0

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

print(cnt)

n = int(input())
a = [int(i) for i in input().split()]
mx = a[0]
ans = 0
for i in a:
    if mx > i:
        ans += mx - i
    elif mx < i:
        mx = i
print(ans)


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

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

    print((dp[-1]))
    return


def __starting_point():
    main()

__starting_point()

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

from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
ans, tmp_max = 0, 0
for a in A:
    if a > tmp_max:
        tmp_max = a
    else:
        ans += (tmp_max - a)
print(ans)

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

count = 0
max_value = A[0]
for i in range(N-1):
    #pdb.set_trace()
    if(max_value > A[i+1]):
        count += max_value-A[i+1]
    if (max_value < A[i+1]):
        max_value = A[i+1]
print(count)

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

sum = 0
max = a[0]

for i in range(n):
	if(max > a[i]):
		sum += (max - a[i])
	else:
		max = a[i]
print(sum)

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]
    else:
        pass

print(ans)


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

n = int(input())
A = [int(i) for i in input().split()]

tmp = 0
res = 0

for i in range(n):
    if tmp < A[i]:
        tmp = A[i]
    else:
        res += tmp - A[i]

print(res)


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


import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

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

"""
1 2 3 4 5 6 ..... N(人)
        i = 身長:Ai(cm)

if A[i] > A[i+1]:
    A[i] == A[i+1] + 踏み台
"""
def step():
    # 入力
    N = int(input())
    A = list(map(int, input().split()))
    # 処理
    step_height_sum = 0
    for i in range(len(A)-1):
        if A[i] > A[i+1]:
            step_height_sum += A[i]-A[i+1]
            A[i+1] = A[i]
    return step_height_sum
    
result = step()
print(result)

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

n=int(input())
lst=list(map(int,input().split()))

now=lst[0]
sm=0

for i in range(1,n):
    if lst[i]>=now :
        now=lst[i]
        continue
    sm+=now-lst[i]

print(sm)

n = int(input())

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

count = 0

for i in range(n):
    if i == 0 or people[i-1] <= people[i]:
        continue
    else:
        count += people[i-1] - people[i]
        people[i] += people[i-1] - people[i]
print(count)

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

Aa = A.split()

i = 0

total = 0

for i in range(N-1):
    left = int(Aa[i])
    right = int(Aa[i+1])

    diff = 0

    if left>right:
        diff = abs(right-left)
        total = total + diff
        Aa[i+1] = right + diff
    else:
        total = total + 0
    

print(total)


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


def main(A):
    ans = 0
    max_num = A[0]
    for a in A:
        max_num = max(max_num, a)
        ans += max_num - a

    return ans

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


__starting_point()

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

ans_step_sum = 0
current_max_height = As[0]

for Ai in As[1:]:
  diff = current_max_height - Ai
  if diff > 0: ans_step_sum += diff
  else: current_max_height = Ai
print(ans_step_sum)

n=int(input())
x=list(map(int,input().split()))

a=x[0]
c=[]

for i in range(1,n):
    if x[i]<a:
        c.append(a-x[i])
    if x[i]>a:
        a=x[i]
print(sum(c))

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

n = int(input())
a = [int(i) for i in input().split()]

ans = 0
max_a = a[0]

for i in range(1, n):
    if max_a > a[i]:
        ans += max_a - a[i]
    elif max_a == a[i]:
        pass
    else:
        max_a = a[i]
print(ans)

# coding: utf-8
# Your code here!

class Person:
    def __init__(self, num, height):
        self.num = num
        self.height = height
    
    @staticmethod
    def static():
        print("static")


def main():
    number = int(input())
    person_list = list(map(int, input().split()))
    
    ans = 0
    for num, person in enumerate(person_list):
        if num > 0:
            if person < person_list[num-1]:
                person_list[num] = person_list[num-1]
                ans += person_list[num-1] - person
        
    
    print(ans)

def __starting_point():
    main()
    

__starting_point()

N = int(input())
A_l = map(int, input().split())
M = 0
B = 0
for i in A_l:
    if i >= M:
        B += 0
        M = i
    else:
        B += M-i
print(B)

N = int(input())
As = list(map(int, input().split()))
tlest, total = As[0], 0
for i in range(1, N):
	tlest = max(tlest, As[i])
	if tlest - As[i]:
		total += tlest - As[i]
print(total)


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


n=int(input())
a=list(map(int, input().split()))
max=a[0]
dai=0
for i in range(n):
    #print(f"{i}回目")
    if max>a[i]:
        dai+=max-a[i]
       
    else:
        max=a[i]
    #print(max,dai)
print(dai)

def steps():
    biggerone=0
    ans=0
    list_a=[]
    cnt=int(input())
    a=input()
    list_a=[int(x) for x in a.split()]
    if len(list_a)!=cnt:
        print('Wrong input')

        if ans==0:
            ans=None

        steps()

    else:

        for i in range(0,len(list_a)-1):
            if i ==0:
                biggerone=list_a[i]

            if biggerone>list_a[i+1]:
                ans+=biggerone-list_a[i+1]
            else:
                biggerone=list_a[i+1]

    print(ans)

steps()


n = int(input())

heights=list(map(int, input().split()))
humidais=[]

for i in range(n):
  if i >= 1:
    if heights[i-1] > heights[i]:
      humidai = heights[i-1] - heights[i]
      humidais.append(humidai)
      heights[i] = heights[i] + humidai

ans = 0
for i in range(len(humidais)):
  ans += humidais[i]
  
print(ans)

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

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

print(ans)

import math


def cin():
    return list(map(int, input().split()))


N = int(input())
data = cin()
total = 0


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

print(total)


N = int(input())
A = [int(d) for d in input().split()]

S = []
X = 0
for i in range(N):
    if X < A[i]:
        X = A[i]
    else:
        pass
    S.append( X - A[i] )

print(sum(S))

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

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


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

heights = 0
for i in range(1,len(arr)):
    if arr[i-1] > arr[i]:
        heights += arr[i-1] - arr[i]
        arr[i] = arr[i-1]

print(heights)