Problem:
 Score : 300 points    Problem Statement 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 1 5
2 1 5 4 3      Sample Output 1 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.       Sample Input 2 5
3 3 3 3 3      Sample Output 2 0  Giving a stool of height 0 to everyone will work.

Solution (Python):
n = int(input())
heights = list(map(int, input().split(' ')))

current_highest = heights[0]
steps_sum = 0
for h in heights[1:]:
  if h > current_highest:
    current_highest = h
  else:
    steps_sum += current_highest - h
 
print(steps_sum)

Solution (Python):
n,*a=map(int,open(0).read().split())
count,max_n=0,a[0]
for i in range(1,n):
    count+=max(0,max_n-a[i])
    max_n=max(max_n,a[i])
print(count)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = [int(x) for x in 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)

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
base = A[0]
c = 0
for i in range(len(A)):
  if A[i] < base:
    c += base-A[i]
  elif A[i] > base:
    base = A[i]
print(c)

Solution (Python):
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline

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

answer = 0
max_a = A[0]

for a in A:
    max_a = max(max_a, a)
    answer += max_a - a

print(answer)

Solution (Python):
n = int(input())
A = [*map(int, input().split())]
s = maxA = 0
for a in A:
    maxA = max(maxA, a)
    s += maxA - a
print(s)

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

Solution (Python):
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)

Solution (Python):
n=int(input())
lst=list(map(int,input().split()))
prev=lst[0]
ans=0
for i in range(1,len(lst)):
  if prev>lst[i]:
    ans+=prev-lst[i]
    
  elif prev<lst[i]:
    prev=lst[i]

  
print(ans)

Solution (Python):
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)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
memo = 0
ans = 0
for j in a:
	if memo > j:
		ans += memo - j
	else:
		memo = j
print(ans)

Solution (Python):
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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


def main():
	N = int(input())
	A = getlist()
	ans = 0
	maxsum = A[0]
	for i in range(1, N):
		ans += max(0, maxsum - A[i])
		maxsum = max(maxsum, A[i])

	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
N=int(input())
num=map(int, input().split())
list1=[]
list2=[]
for x in num:
    list1.append(x)
for i in range(0,N-1):
    if list1[i+1]<list1[i]:
        list1[i]+=0
        re=list1[i]-list1[i+1]
        list1[i+1]+=re
        list2.append(re)
    elif list1[i+1]>=list1[i]:
        list1[i+1]+=0
        list1[i]+=0
print(sum(list2))

Solution (Python):
import sys

n, *aaa = map(int, sys.stdin.buffer.read().split())
cur = aaa[0]
ans = 0
for a in aaa[1:]:
    if cur > a:
        ans += cur - a
    else:
        cur = a
print(ans)

Solution (Python):
N=int(input())
S=list(input().split())
A=[int(n) for n in S]

ans=0

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = map(int, input().split())
ans = 0
ma = 0
for a in A:
    if ma < a:
        ma = a
    ans += ma - a
print(ans)

Solution (Python):


import sys
input=sys.stdin.readline

n=int(input())
arr=list(map(int,input().split()))
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)

Solution (Python):
n = int(input())
As =list(map(int,input().split()))

sum = 0
step =0


for i in range(n):
    try:
        if As[i] > As[i + 1]:
            step = As[i] - As[i + 1]
            As[i + 1] += step
            sum += step 
    except IndexError:
        continue



print(sum)

Solution (Python):
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    N = NI()
    A = NLI()
    now = 0
    ans = 0
    for a in A:
        if now <= a:
            now = a
        else:
            ans += now - a
    print(ans)



if __name__ == "__main__":
    main()

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

max_ = 0
c=0
for i in range(n):
    if max_>=A[i]:
        c +=(max_-A[i])
    else:
        max_ = A[i]
print(c)

Solution (Python):
b=0
print(sum((b:=max(c:=int(a),b))-c for a in[*open(0)][1].split()))

Solution (Python):
def main():

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

    m = x[0]
    ans = 0
    for i in range(1,n):
        if m>x[i]:
            ans += m-x[i]
        else:
            m = x[i]
    
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
pre_a = A[0]
for a in A[1:]:
    if a < pre_a:
        ans += pre_a - a
    else:
        pre_a = a

print(ans)

Solution (Python):
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)

Solution (Python):
n=int(input())
lst=list(map(int,input().split()))
prev=lst[0]
ans=0
for i in range(1,len(lst)):
  if prev>lst[i]:
    ans+=prev-lst[i]
    
  elif prev<lst[i]:
    prev=lst[i]

  
print(ans)

Solution (Python):
n = int(input())
A = [*map(int, input().split())]
s = 0
pre = A[0]
for now in A[1:]:
    if pre > now: s += pre - now
    else: pre = now
print(s)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
s=0
p=0
h_need=l[0]
for i in range(n):
    if h_need>l[i]:
        p=h_need-l[i]
        s+=p
    else:
        h_need=l[i]
print(s)

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

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    max_height = A[0]
    ans = 0
    for i in range(1, N):
        if A[i] >= max_height:
            max_height = A[i]
        else:
            ans += max_height - A[i]
    
    print(f'{ans}')

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(0, n-1):
        if a[i] > a[i+1]:
            step = a[i] - a[i+1]
            a[i+1] = a[i+1] + step
            cnt = cnt + step

    print(cnt)
main()

Solution (Python):


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

    
    ma = A[0]

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

if __name__ == "__main__":
    main()

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

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

m = 0
ans = 0
for ai in a:
    if ai < m:
        ans += m - ai
    m = max(m, ai)

print(ans)

Solution (Python):
listX = input().split()
listY = input().split()
s=0

for cnt in range(int(listX[0])):
    if cnt==0:
        st = int(listY[cnt])
        continue
    if st > int(listY[cnt]):
        s = s + st - int(listY[cnt])
    else:
        st = int(listY[cnt])

print(s)

Solution (Python):
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)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  

N = I()
A = LI()

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
a,b = 0,-1
for i in range(N):
    if i == 0:
        b = A[i]
    else:
        if b > A[i]:
            a += b-A[i]
        b = max(b,A[i])
print(a)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sa = 0
i = 0
mx = max(A)
flg = 0

while i < len(A)-1:
    if A[i] > A[i+1]:
        sa += A[i]-A[i+1]
        A[i+1] = A[i]
    i += 1

print(sa)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
mx=a[0]
for x in a:
    if mx>x:ans+=mx-x
    else:mx=x
print(ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=sum(A)

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

print(sum(A)-ans)

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

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]
"""
N人が1列に並んでおり、前からi番目の人の身長は A_iです。
それぞれの人の足元に、高さが0以上の踏み台を設置し、全ての人が次の条件を満たすようにしたいです。
条件：踏み台を込めて身長を比較したとき、自分より前に、自分より背の高い人が存在しない
この条件を満たす時の、踏み台の高さの合計の最小値を求めてください。
"""
N = int(input())
A = list(map(int, input().split()))
out = 0




vmax = 0
for a in A:
    vmax = max(vmax, a)
    out += vmax - a
print(out)

Solution (Python):
N = input()
A = []
A = input().split()
j = 0
t = A[0]
for i in A:
    if int(i)<int(t):
        j += int(t)-int(i)
    if int(t)<int(i):
        t = i
print(j)

Solution (Python):
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
K = 0
for i in range(N-1):
  if A[i] > A[i+1]:
    S = A[i] - A[i+1]
    
    A[i+1] += S
    K += S
  else:
    pass
print(K)

Solution (Python):

input()
A=list(map(int,input().split()))
m=0
s=0
for a in A:
    d=max(0, m-a)
    s+=d
    m=max(m, a)
print(s)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
readline = sys.stdin.buffer.readline
read = sys.stdin.readline  
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))



def a_int(): return int(readline())


def ints(): return list(map(int, readline().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  
from functools import reduce
from math import gcd


def lcm(a, b):
    
    g = gcd(a, b)
    return a // g * b


N = a_int()
A = ints()
ma = -1
ans = 0
for a in A:
    ma = max(ma, a)
    ans += ma - a
print(ans)

Solution (Python):
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = 0
  for i in range(n):
    max_a = max(max_a, As[i])
    r += max_a - As[i]
  print(r)

main()

Solution (Python):
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)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
cum=[A[i] for i in range(N)]
for i in range(1,N):
    cum[i]=max(cum[i],cum[i-1])

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

print(res)

Solution (Python):
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)

Solution (Python):


if __name__ == "__main__":
    n = int(input())
    x =list((int(x) for x in input().split()))
    
    
    tmp1 = 0
    tmp2 = x[0]
    for i in range(1,n):
        if tmp2 > x[i]:
            tmp3 = tmp2 - x[i]
            tmp1 += tmp3

            x[i] = x[i] + tmp3
            tmp2 = x[i]
        else:
            tmp1 += 0
            tmp2 = x[i]

    print(tmp1)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    idx_start = 0
    sum_a = 0
    ans = 0
    cnt = 0
    len_a = len(a)

    for i in range(1, len_a):
        if a[idx_start] < a[i]:
            ans += (cnt * a[idx_start]) - sum_a
            idx_start = i
            sum_a = 0
            cnt = 0
        else:
            sum_a += a[i]
            cnt += 1
            if i == len_a - 1:
                ans += (cnt * a[idx_start]) - sum_a
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
total = int(input())
tall = list(map(int, input().split()))
dif = 0
for i in range(total - 1):
    if tall[i] > tall[i+1]:
        var = tall[i] - tall[i+1]
        tall[i+1] += var
        dif += var
    else:
        pass
print(dif)

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

Solution (Python):
num = int(input())
line = [int(line) for line in input().split()]

sum_step = 0
max_value = 0
for i, x in enumerate(line):
    if i == 0:
        max_value = x
        continue
    if max_value > x:
        sum_step += max_value - x
    else:
        max_value = x

print(sum_step)

Solution (Python):
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))

Solution (Python):
import sys
sys.setrecursionlimit(2000000)
def f(i,ans,l,t):
  if (i+1) == (len(l)-1):
    if (ans+l[i]) > l[i+1]:
      ans = ans + l[i]-l[i+1]
      return t+ans
    else:
      return t
  else:
    if (ans+l[i]) > l[i+1]:
      ans = ans + l[i]-l[i+1]
      return f(i+1,ans,l,t+ans)
    else:
      return f(i+1,0,l,t)
n = input("")
an = input("").split(" ")
ani = [int(a) for a in an]
if len(ani) == 1:
  print("0")
else:
  print(str(f(0,0,ani,0)))

Solution (Python):
N = int(input())

dateSet = [int(i) for i in input().split(" ")]

temp = sum(dateSet)

max = dateSet[0]
for i in range(N):
    if dateSet[i] < max:
        dateSet[i] = max
    else:
        max = dateSet[i]

print(sum(dateSet) - temp)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]

highest = A[0]
ans = 0

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

print(ans)

Solution (Python):
N = int(input())
s  = [int(N) for N in input().split()]
sum_list = []
for i in range(N):
    if i >= 1:

        num = s[i-1] - s[i]
        if num > 0:
            sum_list.append(num)
            s[i] += num
print(sum(sum_list))

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al=lin()
	ret=0
	pre = al[0]
	for v in al:
		if pre > v:
			ret += pre - v
		elif pre < v:
			pre = v
	return ret
		
		
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

Solution (Python):
def stepUp(val_list):
    temp = 0
    counter = 0

    for i in range(len(val_list) - 1):
        current = val_list[i]
        up = val_list[i + 1]

        if (current > up):
            counter += current - up
            temp += current - up
            val_list[i + 1] = current
          
        temp = 0

    if (val_list[len(val_list) - 2] > val_list[len(val_list) -1]):
        counter += val_list[len(val_list) - 2] - val_list[len(val_list) -1]

    return counter

def userInput():
    a_count = int(input())
    a = list(map(int, input().rstrip().split()))
    
    return stepUp(a)
  
print(userInput())

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
r = 0
for x in range(1, len(li)):
    if(li[x] < li[x-1]):
        r += li[x-1] - li[x]
        li[x] = li[x-1]
print(r)

Solution (Python):
import sys

def main():

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

if __name__ == "__main__":
    main()

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

mx = lst[0]
if n == 1:
  print(0)
else:
  s =0
  for i in range(1,n):
    if lst[i] < mx:
      s += mx -lst[i]
    if lst[i] > mx:  
      mx = lst[i]
      
  print(s)

Solution (Python):
n = input()
summ = 0
lst = list(map(int, input().split()))
for i in range(len(lst) - 1):
    if lst[i] > lst[i + 1]:
        k = lst[i] - lst[i + 1]
        lst[i + 1] = lst[i + 1] + k
        summ += k
print(summ)

Solution (Python):
def test(N, A):
  B = 0
  mx = A[0]
  if N != 1: 
    for i in range(1,N):
      if A[i] - mx < 0:
        B = B + mx - A[i]
      else:
        mx = A[i];
  return B

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

B = test(N, A)

print(B)

Solution (Python):
N = int(input())
array = list(map(int, input().split()))
ans = 0
next = 1
a = True
while (a):
    a = False
    b = False
    for i in range(next,len(array)):
        if array[i] < array[i-1]:
            ans += array[i-1] - array[i]
            array[i] = array[i - 1]
            a = True
            if b == False:
                b= True
                next = i

print(ans)

Solution (Python):
N = int(input())
steps = list(map(int, input().split()))
sum_step = 0

for i in range(N-1):
  if steps[i] < steps[i+1]:
    continue
  else:
    step = steps[i] - steps[i+1]
    steps[i+1] += step
    sum_step += step
print(sum_step)

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

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

Solution (Python):
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)

Solution (Python):
n = int(input())
members = list(map(int, input().split(" ")))


sum = 0
for i in range(1, len(members)):
    if members[i] < members[i-1]:
        fumidai = members[i-1] - members[i]
        sum += fumidai
        members[i] += fumidai

print(sum)

Solution (Python):
N = int(input())

a = input().rstrip().split(" ")
for i in range(N):
    a[i] = int(a[i])
    
cnt = 0

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

print(cnt)

Solution (Python):
n = int(input())
height = list(map(int,input().split()))
ans = 0
for i in range(1,len(height)):
  if height[i] >= height[i-1]:
    continue
  if height[i] < height[i-1]:
    ans += height[i-1] - height[i]
    height[i] = height[i-1]
print(ans)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
cnt=h=0
for i in range(len(A)):
    tmp=A[i]
    if i==0:
        h=A[i]
    else:
        if tmp>=h:
            h=tmp
        else:
            cnt+=h-tmp
print(cnt)

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

dp =  [float("inf")]  * n 
dp[0] = 0

for i in range(0, n - 1):
    if a[i  + 1] < a[i]:
        dp[i + 1] = dp[i] + (a[i] - a[i +1])
        a[i + 1] = a[i]
    else:
        dp[i + 1] = dp[i]
res = dp[n - 1]
print(res)

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

c = 0
mae = A[0]

for i in A:
  if i < mae:
    c += mae - i
  else:
    mae = i

print(c)

Solution (Python):
N = int(input())
A = list(input().split())
An = [int(num) for num in A]

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s=a[0]
res=0
for i in a:
    if s>i:
        res+=s-i
    else:
        s=i            
print(res)

Solution (Python):
t=int(input())
arr=list(map(int,input().strip().split()))[:t]
ans=0
for i in range(1,t):
    if(arr[i]<arr[i-1]):
        ans=ans+arr[i-1]-arr[i]
        arr[i]=arr[i-1]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = []
c = 0
for i in range(1,n):
    c = max(c,a[i-1])
    s = c-a[i]
    if s >= 0:
        b.append(s) 
print(sum(b))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
res=0

if n==1:
	print(res)
else:

	for i in range(1,n):

		if a[i]<a[i-1]:
			res= ( a[i-1] - a[i] ) + res
			a[i] = a[i-1]

	print(res)

Solution (Python):
input()
array = list(map(int, input().split()))

ans = 0
top = 0

for x in array:
  if top < x:
    top = x
  else:
    ans += top - x
print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from copy import deepcopy

INF = float('inf')


def LI(): return list(map(int, sys.stdin.readline().split()))


def I(): return int(sys.stdin.readline())


def LS(): return sys.stdin.readline().split()


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


def IR(n): return [I() for i in range(n)]


def LIR(n): return [LI() for i in range(n)]


def SR(n): return [S() for i in range(n)]


def LSR(n): return [LS() for i in range(n)]


def SRL(n): return [list(S()) for i in range(n)]


def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]


mod = 10 ** 9 + 7

n = I()
A = LI()
ret = 0
ans = 0
for i in range(n):
    if A[i] > ret:
        ret = A[i]
    elif A[i] < ret:
        ans += ret - A[i]

print(ans)

Solution (Python):

n = int(input())
l = list(map(int, input().split()))
small=large=l[0]
table = 0
for i in range(1,len(l),1):
    if l[i] < large:
        table += (abs(l[i]-large))
    if l[i] > large:
        large = l[i]
print(table)

Solution (Python):
n = int(input())

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


top = s[0]
count = 0

for x in s :
    if x <= top :
        count = count + top - x
    else :
         top = x

print(count)

Solution (Python):
N=int(input())
A = list(map(int,input().split()))
target=A[0]
cost=0
for v in range(N-1):
    B=A[v+1]-target
    if B>=0:
        target=A[v+1]
    elif B<0:
        cost+=-B
print(cost)

Solution (Python):
def minimumStoolHeight(A):
    stool_tot = 0
    height_min = 0
    
    if len(A) == 1:
        return 0
    
    height_min = A[0]
    for i in range(1, len(A)):
        if A[i] < height_min:
            stool_tot += (height_min - A[i])
        else:
            height_min = A[i]         
            
    return stool_tot  
  
N = int(input())
A = list(map(int, input().rstrip().split()))
print(minimumStoolHeight(A))

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

ans = 0
for i,f in enumerate(a):
    if i == 0:
        continue
    if a[i-1] > f:
        sa = a[i-1] - f
        a[i] += sa
        ans += sa

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
count = 0
c = 0

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

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7






n = I()
a_l = LI()
ans = [0]


for i in range(n-1):
    diff = a_l[i + 1] - a_l[i]
    a = 0
    if diff < 0:
        a_l[i + 1] = a_l[i + 1] + abs(diff)
        a = abs(diff)
    ans.append(a)
print(sum(ans))

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

i = 0
gapsum = 0
max = a[0]
for i in range(n):
    if a[i] < max:
        gap = max - a[i]
        gapsum += gap
    elif a[i] > max:
        max = a[i]
    i += 1

print(gapsum)

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

heighest = 0
stepsum = 0
for i in A:
  if heighest < i:
    heighest = i
    continue
  else:
    stepsum += heighest-i

print(stepsum)

Solution (Python):
n=int(input())
l=[int(_) for _ in input().split()]
s=0
for ind,i in enumerate(l):
  if ind>0:
    if i<l[ind-1]:
    	d=l[ind-1]-i
    	l[ind]=i+d
    	s=s+d
print(s)

Solution (Python):
z = int(input())
result = []
arr = list(map(int, input().split()))
for k in range(z - 1):
    if arr[k] > arr[k+1]:
        result.append(arr[k] - arr[k+1])
        arr[k+1] = arr[k]
print(sum(result))

Solution (Python):
n = int(input())
list = list(map(int,input().split()))

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

sumV = sum(list[maxI:len(list)])
maxM = maxV * (len(list)-maxI)
ans += maxM - sumV

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

Solution (Python):
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)

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

height = 0
step = []
for i in A:
    if height > i:
        step.append(height - i)
    else:
        step.append(0)
        height = i
print(sum(step))

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

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

highest, ans = 0, 0

for l in a:
    if highest > l:
        ans += highest - l
    elif highest < l:
        highest = l
    else:
        pass
print(ans)

Solution (Python):
n = input()
l = [int(i) for i in input().split()]
max = 0
sum = 0
for i in range(1,len(l)):

  if l[i-1] > max:
    max = l[i-1]
  if l[i] < max:
    sum += max - l[i]
print(sum)

Solution (Python):
n=int(input())
val= input()
a = val.split()
l = list(map(int, a))
s=0
max_ =0
for i in range(len(l)):
    if max_ < l[i]:
        max_ = l[i]
    if l[i]<max_:
        k =max_-l[i]
        s = s +k
print(s)

Solution (Python):
n = int(input())
a = [int(s) for s in input().split()]
count = 0
max = a[0]
for i in range(1, len(a)):
    if max >  a[i]:
        if a[i - 1] > max:
            max = a[i - 1]
        count += max - a[i]
    else:
        max = a[i]
print(count)