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):
import math

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

highest = A[0]
sum = 0
for i in range(1,n):
    if A[i] < highest:
        sum+=(highest-A[i])
    else:
        highest = A[i]

print(sum)

Solution (Python):
n = int(input())
la = list(map(int, input().split()))
amax = la[0]
ans = 0
for i in range(n):
  d = amax-la[i]
  ans += d if d>0 else 0
  amax = max(amax, la[i])
print(ans)

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

ans = 0
n = 0
for a in A:
    if n > a:
        m = n - a
        ans += m
        n = a + m
    else:
        n = a

print(ans)

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

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

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

print(sum)

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

h = 0
max = a[0]
for x, i in enumerate(a):
  if i < max:
    h += max - i
  else:
    max = i
print(h)

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

Solution (Python):
n=int(input())
li = list(map(int,input().split()))
high = 0
humi = 0
for i in range(n):
    if high >= li[i]:
        humi += high-li[i]
    else:
        high = li[i]
print(humi)

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

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

print(sum)

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

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

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

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

myans = 0
mytgt = A[0]
for n in range(1,N):
    
    if A[n] < mytgt:
        myans += (mytgt - A[n])
    mytgt = max(A[n], mytgt)
    
print(myans)

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

a_max = A[0]
steps = [0]
for a in A[1:]:
    if a < a_max:
        steps.append(a_max - a)
    else:
        steps.append(0)
    a_max = max(a, a_max)
print(sum(steps))

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

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

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


total = 0
prev = 0
for a in Ai:
    plus = 0
    if prev > a:
        plus = prev - a
    prev = a + plus
    total += plus

print(total)

Solution (Python):
import sys,math
from collections import deque,defaultdict
import operator as op
from functools import reduce
sys.setrecursionlimit(10**6) 

I=sys.stdin.readline

 

"""
x_move=[-1,0,1,0,-1,1,1,-1]
y_move=[0,1,0,-1,1,1,-1,-1]
"""
def ii():
	return int(I().strip())
def li():
	return list(map(int,I().strip().split()))
def mi():
	return map(int,I().strip().split())

def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


	
	

	

def main():
	n=ii()
	arr=li()
	cnt=0
	for i in range(n-1):
		if arr[i]>arr[i+1]:
			cnt+=arr[i]-arr[i+1]
			arr[i+1]=arr[i]





	print(cnt)

	


	
	
	
		







	












	
	
	





if __name__ == '__main__':
	main()

Solution (Python):
N = int(input())
List_A = list(map(int,input().split()))
a = 0

for i in range(N-1):
    if List_A[i] <= List_A[i+1]:
        a = a + 0
    else:
        a = a + List_A[i] - List_A[i+1]
        List_A[i+1] = List_A[i]
        
print(a)

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

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

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 = list(map(int, input().split()))
total = 0
for i in range(n - 1):
    if a[i] > a[i + 1]:
        diff = abs(a[i] - a[i + 1])
        total += diff
        a[i + 1] += diff
        
print(total)

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

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

Solution (Python):
N = int(input())
sin_list = list(map(int, input().split()))
max_height = 0
total_additional_height = 0

for sin in sin_list:
  if max_height == 0:
    max_height = sin
  else:
    if sin >= max_height:
      max_height = sin
    else:
      total_additional_height += (max_height - sin)

print(total_additional_height)

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

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

print(step)

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

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

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

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

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

max_height = heights[0]

sum_stool = 0

for height in heights:
    if max_height < height:
        max_height = height
    else:
        sum_stool += max_height - height

print(sum_stool)

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

height=a[0]
fumidai=0

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

print(fumidai)

Solution (Python):
def solve(arr,n):
    maxx=[]
    ma=0  
    for i in arr:
        ma=max(ma,i)  
        maxx.append(ma) 
    ans=0 
    for i in range(n):
        ans+=abs(maxx[i]-arr[i])
    print(ans) 
n=int(input()) 
a=[int(x) for x in input().split()] 
solve(a,n)

Solution (Python):
import numpy as np

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

a_before = np.sum(a)
for i in range(len(a)-1):
    
    if a[i] > a[i+1]:
        a[i+1] = a[i]

a_after = np.sum(a)

print(a_after - a_before)

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

Solution (Python):
def read():
    N = int(input())
    As = list(map(int, input().split(' ')))
    return N, As


def func(N, As):
    prev = -1
    ans = 0
    for n in range(N):
        if n == 0:
            prev = As[n]
            continue
        t = As[n]
        if prev > t:
            ans += prev - t
        prev = max(t, prev)
    return ans


if __name__ == '__main__':
    N, As = read()
    print(func(N, As))

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

Solution (Python):

N = int(input())


A_ = list(map(int, input().split(" ")))

dai = 0
now = A_[0]
for A in A_[1:]:
    if now <= A:
        now = A
        continue
    else:
        dai += now - A
        
print(dai)

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

ma=a[0]

cnt=0

for i in range(1,n):
    if ma > a[i]:
        cnt = cnt + ma-a[i]
    ma=max(ma,a[i])

print(cnt)

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

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

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

Solution (Python):
input()
ans = 0
tmp = 0
for i in map(int, input().split()):
    if tmp < i:
        tmp = i
    else:
        ans += tmp - i
print(ans)

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
max_t = 0
t = 0
for i in range(n):
  n_t = lst[i]
  if i == 0:
    max_t = n_t
  else:
    if n_t < max_t:
      t += max_t - n_t
    elif n_t > max_t:
      max_t = n_t
print(t)

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

Solution (Python):


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

prev = 0
total = 0

for i, a in enumerate(A):
    if i == 0:
        prev = a
        continue

    if a >= prev:
        prev = a
        continue

    total += prev - a

print(total)

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

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

maxHeight = A[0]
result = 0

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

print(result)

Solution (Python):
import math
def main():
    n = int(input())
    a_li = list(map(int,input().split()))
    ans = 0
    for i in range(1,n):
        if(a_li[i] >= a_li[i-1]):
            continue
        ans += a_li[i-1] - a_li[i]
        a_li[i] = a_li[i-1]
    print(ans)
    
        
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
Ai = list(map(int, input().split()))
s = 0

for i in range(1, len(Ai)):
  if Ai[i-1] > Ai[i]:
    sa = abs(Ai[i-1] - Ai[i])
    Ai[i] += sa
    s += sa

print(s)

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

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

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

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

print(count)

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

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

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

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

front = A[0]
count = 0
for i in range(1,N):
    hight = 0
    if front > A[i]:
        hight = front -A[i]
        count += hight
    front = hight + A[i] 
print(count)

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

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

print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())
r = 0
pre = None
for x in A:
    if pre is None:
        pre = x
        continue
    if pre > x:
        r += pre - x
    else:
        pre = x
print(r)

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

    h = A[0]
    ans = 0

    for i in A:
        if i < h:
            ans += h - i
        elif h < i:
            h = i

    print(ans)


if __name__ == '__main__':
    main()

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

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

step = 0

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

print(step)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 0
t = a[0]

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

print(ans)

Solution (Python):

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

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

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

result=0
current_max = As[0]
for A in As[1:]:
    diff = current_max-A
    if diff>0:
        result+=diff
        A+=diff
    if current_max<A:
        current_max=A
print(result)

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

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

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

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

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

ans = 0

for i, a in enumerate(A):
    if i == 0:
        continue

    if a < A[i-1]:
        ans += A[i-1]-a
        A[i] = A[i-1]

print(ans)

Solution (Python):
from math import ceil


def A(n, x, t):
  return ceil(n / x) * t


def B(n_str):
  s = 0
  for c in n_str:
    s += int(c)
  if s % 9 == 0:
    return 'Yes'
  return 'No'


def C(n, 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]
  return ans


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


if __name__ == '__main__':
  main()

Solution (Python):
num=int(input())
date=input().split(" ")
date=list(map(int,date))
max=0
sum=0
for i in range(num):
    if date[i]>max:
        max=date[i]
    else:
        sum+=max-date[i]
print(sum)

Solution (Python):
n = int(input())
a_all = input()
a_l = a_all.split()
c = 0
for i in range(1,n):
  if int(a_l[i]) < int(a_l[i-1]):
    c = c - int(a_l[i]) + int(a_l[i-1])
    a_l[i] = a_l[i-1]
print(c)

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

print(ans)

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

current_max = A[0]
total = 0

for a in A:
    if current_max < a:
        current_max = a
    else:
        total += current_max - a

print(total)

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

sum_step = 0
max_height = a[0]
for i in range(1, n):
    if a[i] > max_height:
        max_height = a[i]
    if a[i] < max_height:
        sum_step += max_height - a[i]

print(sum_step)

Solution (Python):





"""
"""

from collections import defaultdict
import bisect

import sys
input = sys.stdin.readline

def solve(n, a):
    mm = 0
    ans = 0
    for aa in a:
        ans += max(mm-aa,0)
        mm = max(mm,aa)
    print(ans)
            

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


if __name__ == "__main__":
    main()

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

    m = A[0]
    ans = 0
    for i, a in enumerate(A[1:]):
        m = max(m, a)
        if m > a:
            ans += m - a
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

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

    ans = 0
    max_tall = 0

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


    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
c = 0
t = a[0]
for i in a:
  if t>i:
    b=t-i
    c+=b
  else:
    t=i

	
print(c)

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

Solution (Python):
n = int(input())
a = [int(an) for an in input().split()]

ans = 0
pre = a[0]

for an in a[1:]:
    if an < pre:
        ans += pre - an
    else:
        pre = an

print(ans)

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

Solution (Python):
import sys
n = int(sys.stdin.readline().rstrip("\n"))
a = [int(s) for s in sys.stdin.readline().rstrip("\n").split()]
step = 0
tallest = a[0]
for p in a:
    if p >= tallest:
        tallest = p
    else:
        step += (tallest-p)
print(step)

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

c = 0

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

print(c)

Solution (Python):

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

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

result = 0
a_prev = A[0]
for a in A:
    fumidai = max(0, a_prev - a)
    result += fumidai
    a_prev = a + fumidai

print(result)

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

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

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

print(ans)

Solution (Python):
import math


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

    ans = 0
    prev = 0

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

    print(ans)


if __name__ == "__main__":
    main()

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

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

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 = list(map(int, input().split()))
dif  = 0
sum = 0

for i in range(len(a) - 1):
    dif = a[i] - a[i+1]
    if dif > 0:
        sum = sum + dif
        a[i + 1] = a[i + 1] + dif

print(sum)

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

Solution (Python):
from sys import stdin

def main():
    n = int(input())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    humidai = 0
    pre_a = 0
    for a in A:
        if pre_a > a:
            humidai += pre_a -a 
        else:
            pre_a = a

    print(humidai)
    
if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
height=list(map(int,input().split()))
fumidai=[0 for i in range(n)]

for i in range(1,n):
    if height[i]<height[i-1]:
        key=height[i-1]-height[i]
        fumidai[i]=key
        height[i]+=key

print(sum(fumidai))

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

lst = input().split()

ans = 0

for i in range(N - 1):
   d = int(lst[i]) - int(lst[i+1])
   if 0 < d:
      ans += d
      lst[i+1] = int(lst[i+1]) + d

print(ans)

Solution (Python):
import sys
import math 


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

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

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

Solution (Python):
import sys
import copy
from collections import deque
import collections
import itertools
stdin = sys.stdin
import math

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()

a = na()

prev = 0
cnt = 0
for ai in a:
    if ai >= prev:
        prev = ai
    else:
        cnt += (prev-ai)

print(cnt)

Solution (Python):
from collections import deque

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

max_A = 0
ans = 0
while A_deq:
    anum = A_deq.popleft()
    if max_A <= anum:
        max_A = anum
    else:
        ans += max_A - anum

print(ans)

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

maxA = 0
count = 0
for a in As:
    
    if a >= maxA:
        pass
    else:
        
        count += maxA - a
    maxA = max(a, maxA)
    
    
print(count)

Solution (Python):

def li():
    return [int(x) for x in input().split()]


N = int(input())
A = [0] + li()
total = 0
for i in range(1, N+1):
    diff = A[i] - A[i - 1]
    if diff < 0:
        total -= diff
        A[i] = A[i - 1]

print(total)

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

B = []
S = 0
for i in range(N):
    
    if i == 0:
        B.append(A[i])
        continue
    last_B = B[i - 1]
    humidai = last_B - A[i]
    if humidai <= 0:
        humidai = 0
    now_B = A[i] + humidai
    
    S += humidai
    B.append(now_B)

print(S)

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

total_step = 0
for i in range(N):
    if i == 0:
        prev_max_height = heights[i]
        continue
    height = heights[i]
    if height > prev_max_height:
        prev_max_height = height
    else:
        total_step += prev_max_height - height

print(total_step)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
ma = 0
ans = 0
for i in a:
    ma = max(ma, i)
    ans += (ma - i)
print(ans)

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):
n=int(input())
a=[int(x) for x in input().split()]

offset=0
now=0
for i in a:
    if i<=now:
        offset+=now-i
    else:
        now=i

print(offset)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    if l[i]<l[i-1]: ans+=l[i-1]-l[i]; l[i]+=l[i-1]-l[i]
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
pre = 0

for a in A:
    if pre > a:
        ans += pre - a
        a = pre
    pre = a

print(ans)

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
base = a[0]
Sum = 0

for i in a:
    if base > i:
        Sum += (base - i)
    
    if base < i:
        base = i
        
print(Sum)

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 = list(map(int,input().split()))
cnt = 0
tmp = a[0]
for i in range(1,n):
    if a[i] <= tmp:
        cnt += tmp - a[i]
    else:
        tmp = a[i]
print(cnt)

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

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

maxi = 0
humi = 0
maxi = an[0]
for i in range(1, n):
    if an[i] < maxi:
        hu = maxi -an[i]
        humi += hu
    maxi = max(maxi, an[i])

print(humi)

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

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

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


main()

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

for i in range(1, n):
  ai = arr[i]
  ai_ = arr[i-1]
  if ai >= ai_:
    pass
  else:
    cnt += ai_ - ai
    arr[i] = ai_
    
print(cnt)

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]:
    continue
  else:
    ans+=(a[i]-a[i+1])
    a[i+1]=a[i]

print(ans)

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

Solution (Python):
N = int(input())
A=[]
A = list(map(int,input().split(" ")))
current = 0
res = 0
for i in range(N):
    if A[i] < current:
        res += current - A[i]
    else:
        current = A[i]
print(res)

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

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

ms = c[0]
ans = 0

for i in range(1, a):
  ans += max(0, ms-c[i])
  if c[i]>ms:
    ms = c[i]
    
print(ans)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

_max = 0
ans = 0
for v in a:
    _max = max(_max, v)
    ans += (_max - v)

print(ans)

Solution (Python):
input()
a = input().split(" ")

a = list(map(int, a))

count = 0
for i in range(1, len(a)):
  if a[i]<a[i-1]:
    count += (a[i-1] - a[i])
    a[i] += (a[i-1] - a[i])
    
print(count)

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

ans = 0
x = a[0]

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

print(ans)

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

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

maxa = 0
box = 0
for value in A:
    if maxa < value:
        maxa = value
    if maxa > value:
        box += maxa - value

print(box)

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

Solution (Python):
def C():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    a_max = 0
    for t in a:
        if a_max > t:
            ret += a_max-t
        else:
            a_max = t
    print(ret)
C()