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.



Sample input:
5
2 1 5 4 3


Sample output:
4

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#coding: utf-8
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
# for x, a in enumerate(A):
#     if x==0:
#         continue
#     out += max(0, max(A[:x])-a)
vmax = 0
for a in A:
    vmax = max(vmax, a)
    out += vmax - a
print(out)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
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))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


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
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
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  # , insort_left, insort_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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:


if __name__ == "__main__":
    n = int(input())
    x =list((int(x) for x in input().split()))
    # print(x)
    
    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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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())
	#b , c = tin()
	#s = 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
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())

a = input().rstrip().split(" ")
for i in range(N):
    a[i] = int(a[i])
    
cnt = 0
#print(a)
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# cook your dish here
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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(a)
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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 人が 1 列に並んでおり、前から i 番目の人の身長は Ai です。
# それぞれの人の足元に、高さが 0 以上の踏み台を設置し、全ての人が次の条件を満たすようにしたいです。
# 条件：踏み台を込めて身長を比較したとき、自分より前に、自分より背の高い人が存在しない
# この条件を満たす時の、踏み台の高さの合計の最小値を求めてください。

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
diff = 0

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

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))

ans_list = [0]
for i in range(1, N):
    d = a_list[i-1] + ans_list[i-1] - a_list[i]
    if d > 0:
        ans_list.append(d)
    else:
        ans_list.append(0)
print(sum(ans_list))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=0
Mi=A[0]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = 0
if N==1:
  print(0)
  exit()

for i,a in enumerate(A):
  if i==0:
    continue
  
  tmp = A[i-1] - a
  if tmp>0:
    A[i] = A[i-1]
    s += tmp

print(s)

Submission status: Accepted

Solution code:
N=input()
list=[int(x) for x in input().split()]
i=0
kei=0
k=0
while(i!=len(list)-1):
    if(list[i]>list[i+1]):
        kei+=list[i]-list[i+1]
        list[i+1]=list[i]
    i+=1
print(kei)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
res=0
for i in range(N-1):
    if(A[i]>A[i+1]):
        res+=A[i]-A[i+1]
        A[i+1]=A[i]
print(res)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
sum = 0
for i in range(len(l)-1):
    if l[i] > l[i+1]:
        sum += l[i]-l[i+1]
        l[i+1] = l[i]
print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkler var mı?
# Herkese memnün oldum
import sys

readline = sys.stdin.buffer.readline

#iimr = lambda: map(int, sys.stdin.buffer.readline().rstrip().split())
# il ne y a pas plus le buffer sur atcoder
iimr = lambda: map(int, sys.stdin.readline().rstrip().split())
#sys.setrecursionlimit(10 ** 8)

def debug(*x):
    print(*x, file=sys.stderr)


class atcoder():
    def __init__(self):
        pass

    def çözmek(self):
        N = int(input())
        A = list(map(int, sys.stdin.readline().split()))
        res = []
        res.append(self.関数(N, A))
        print(*res, "")	
    
    def 関数(self, n, Aliste):
        res = 0
        for i in range(1, n):
            if Aliste[i - 1] > Aliste[i]:
                res += Aliste[i - 1] - Aliste[i]
                Aliste[i] = Aliste[i - 1]

        return res

if __name__ == "__main__":
    ima = atcoder()
    ima.çözmek()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
b=0
for i in range(1,n):
  if a[i-1]>a[i]:
    b+=a[i-1]-a[i]
    a[i]=a[i-1]
print(b)

Submission status: Accepted

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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j-1] - a[j]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
        a[j] += stepping_stone
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split(" ")]

maximum=A[0]
dai=0
for a in A:
    if a<maximum:
        dai+=maximum-a
    if a>maximum:
        maximum=a

print(dai)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=A
M=max(A)
k=0
Fumi=0

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

print(Fumi)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
p=l[0]
x=0
for i in range(1,n):
       if l[i]<p:
       		x+=p-l[i]
       elif l[i]>p:
       		p=l[i]
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = []

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

print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = 0
p = 0

for i in range(N):
    if A[i] < p:
        s = s + p - A[i]
    else:
        p = max(A[i], p)
print(s)

Submission status: Accepted

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

Sum = 0

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

print(Sum)