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

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

print(sum_)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
wa = 0
for x in range(len(A)):
    y = min(x+1, len(A)-1)
    z = A[x] - A[y]
    if z > 0:
        wa += z
        A[y] += z
print(wa)

Submission status: Accepted

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
###############################################################################\
n=int(input())
A=INPUT()
B=[A[0]]

currmax=A[0]
ans=0
for i in range(1,n):
    if A[i]<=currmax:
        ans+=currmax-A[i]
    currmax=max(currmax,A[i])
print(ans)

Submission status: Accepted

Solution code:
import copy
N = int(input())
Ai = list(map(int, input().strip().split()))
height = 0;
boards = copy.copy(Ai)
for i in range(len(boards)-1):
    if boards[i+1] >= boards[i]:
        continue;
    else:
        height += boards[i] - boards[i + 1]
        boards[i+1]= boards[i]
print(height)

Submission status: Accepted

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

B = [0]*N

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

ans = sum(B)
print(ans)

Submission status: Accepted

Solution code:
# from pprint import pprint
# import math
# import collections

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

now_high = a[0]
fumidai = 0

for _a in a:
    if now_high > _a:
        fumidai += now_high - _a
    else:
        now_high = _a

print(fumidai)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))

mx = 0
ans = 0
for i in lst1:
    if i < mx:
        ans += mx-i
    else:
        mx = max(mx,i)

print(ans)

Submission status: Accepted

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

B = 0
A0, *AA = A

for a in AA:
  if A0 > a:
    B += A0 - a
  else:
    A0 = a

print(B)

Submission status: Accepted

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

#print(A)

cou = 0

for i in range(1,len(A)):
    if A[i] < A[i - 1]:
        dan = A[i - 1] - A[i]
        #print(dan)
        cou = cou + dan
        A[i] = A[i - 1]
        #print(A)

print(cou)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list((int(x) for x in input().split()))

beforeAi=0
counter=0

for Ai in range(N):
    if beforeAi>A[Ai]:
        counter+=beforeAi-A[Ai]
        A[Ai]=beforeAi
    beforeAi=A[Ai]

print(counter)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

Solution code:
def Step():
    
    num = int(input())

    data = list(map(int,input().split()))
    
    count = 0
    
    fn = 0
    
    for i in range(len(data)-1):
        if (data[i+1] - (data[i] + fn)) < 0:
            count = count + abs(data[i+1] - (data[i] + fn))
            fn = abs(data[i+1] - (data[i] + fn))
    
        else:
            fn = 0
    
    print(count)
    
if __name__ == '__main__':
    Step()

Submission status: Accepted

Solution code:
n = int(input())

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


before_max = a[0]
humidai = 0
humidai_sum = 0
for i in range(1,n):
    humidai = 0
    if(before_max >= a[i]):
        humidai = before_max - a[i]
        humidai_sum += humidai

    if(before_max < a[i]):
        before_max = a[i]
        

    #print(humidai,before_max,before_max)
print(humidai_sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

diff_sum = 0
for i in range(N-1):
    if A[i+1] < A[i]:
        diff = A[i] - A[i+1] 
        diff_sum += diff
        A[i+1] += diff


print(diff_sum)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
def add_each_number(num):
  s = str(num)
  array = list(map(int, s))
  return sum(array)
 
n = int(input())
first_stand = 0
second_stand = 0
first_height = 0
second_height = 0
total = 0
height_list = map(int, input().split())
for second_height in height_list:
  if( (first_stand + first_height) > second_height ):
    second_stand = (first_stand + first_height) - second_height
  else:
    second_stand = 0
  total += second_stand
  first_stand = second_stand
  first_height = second_height
print(total)

Submission status: Accepted

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

step = 0
ma = max(A)
idx = A.index(ma)
for i in range(idx-1):
    dif = A[i+1]-A[i]
    if dif < 0:
        step += abs(dif)
        A[i+1] += abs(dif)
for i in range(idx,n):
    step += ma-A[i]
print(step)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

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

pre = A[0]
s = 0
for a in A:
    ad = max(0, pre - a)
    pre = max(pre, a)
    s += ad
print(s)

Submission status: Accepted

Solution code:
import sys

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

h =[]

if n == 1:
    print(0)
    sys.exit()

if a[0] > a[1]:
    h.append(a[0] - a[1])
    a[1] = a[0]
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] = a[i-1]
    else:
        h.append(0)
        
print(sum(h))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))
if N == 1:
  print(0)
else:
  x = 0
  for i in range(0,len(L)-1):
    if L[i] > L[i + 1]:
      x = x + (L[i] - L[i + 1])
      L[i + 1] = L[i + 1] + (L[i] - L[i + 1])
  print(x)

Submission status: Accepted

Solution code:
N = int(input())

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

DEBUG = False

cnt = 0

for i in range(1, N):
    if A[i-1]>A[i]:
        diff = A[i-1]-A[i]
        cnt += diff
        A[i] += diff
        if DEBUG:print(f"A[{i-1}] = {A[i-1]}\t,\tA[{i}] = {A[i]}\t,\tcnt = {cnt}")

print(cnt)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.asarray(input().split()).astype(np.int)

ind_maxA = A.argmax()

diffA = A.max() - A[ind_maxA:]
ind = np.where(diffA > 0)
cost = np.sum(diffA[ind])

maxA = 0
for n in range(1, ind_maxA):
    if A[n-1] > A[n]:
        diff = A[n-1] - A[n]
        cost += diff
        A[n] += diff 

print(cost)

Submission status: Accepted

Solution code:
n=int(input())
x=input().split()
a=[]
for i in range(n):
	a.append(int(x[i]))
counter=0
for i in range(n-1):
	if a[i+1]-a[i]<0: 
		counter+=-a[i+1]+a[i]
		a[i+1]+=-a[i+1]+a[i]

print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,N):
    step = -min(0, A[i]-B[i-1])
    S.append(step)
    B.append(A[i] + step)

print(sum(S))

Submission status: Accepted

Solution code:
# N, X, T = map(int, input().split())
from functools import reduce
N = int(input())
An = list(map(int, input().split()))

b = 0
sum = 0

for i in range(N):
  if An[i] > b : 
    b = An[i]
  else:
    sum += b - An[i]

print(sum)

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
length = 0
for i in range(N-1):
    if arr[i+1] < arr[i]:
        length +=  arr[i] - arr[i+1]
        arr[i+1] += arr[i] - arr[i+1]
print(length)

Submission status: Accepted

Solution code:
N = int(input())
A = input()
A = A.split()
a = []
for i in A:
    a.append(int(i))
#print(a)
c = []
for i in range(N-1):
    if(a[i]<=a[i+1]):
        c.append(0)
    if(a[i]>a[i+1]):
        diff = a[i] - a[i+1]
        a[i+1] += diff
        c.append(diff)
#print(c)
result = sum(c)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from typing import List


def f(n: int, a_list: List[int]) -> int:
    """
    >>> f(5, [2, 1, 5, 4, 3])
    4
    """
    ans = 0

    n_max: int = a_list[0]
    for i, a_i in enumerate(a_list):
        if n_max > a_i:
            ans += n_max - a_i
        else:
            n_max = a_i

    return ans


if __name__ == '__main__':
    n = int(input())
    a_list = list(map(lambda x: int(x), input().split()))
    print(f(n, a_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import math

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

print(total)

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=a[0]
sum=0

for i in range(len(a)):
  m=max(m,a[i])
  sum += max(0,m-a[i])

print(sum)

Submission status: Accepted

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

Step=[0]
maxt=A[0]
i=0
while i<N-1:
  if maxt<A[i+1]:
    maxt=max(maxt,A[i+1])
    s=0
  else: 
    s=maxt-A[i+1]
  Step+=[s]
  i+=1
print(sum(Step))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = int(input())
arr = list(map(int, input().split()))

ans = 0

for i in range(1,s):
    if arr[i] < arr[i-1]:
        #print((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())
lst=list(map(int,input().strip().split()))
lst1=list(set(lst))
ans=0 
for i in range(1,n):
    if(lst[i-1]>lst[i]):
        a=(lst[i-1]-lst[i])
        lst[i]+=a 
        ans+=a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l_a = list(map(int, input().split()))
high_max = 0
sum_hight = 0
for i in range(n):
    if high_max <= l_a[i]:
        high_max = l_a[i]
    else:
        sum_hight += high_max - l_a[i]

print(sum_hight)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
s = [0] * n

for i in range(1, n):
  if ((h[i-1] + s[i-1]) > h[i]):
    s[i] = (h[i-1] + s[i-1]) - h[i]

print(sum(s))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
import copy
b = copy.copy(a)
tall = a[0]
for i in range(n):
    if a[i]<tall:
        a[i] = tall
    elif a[i]>tall:
        tall = a[i]
print(sum(a)-sum(b))

Submission status: Accepted

Solution code:
n = int(input())
hight_list = list(map(int, input().split()))
sum_step = 0
max_hight = 0

for i in range(n):
    if hight_list[i] < max_hight:
        
        sum_step += max_hight - hight_list[i] 
    else:
        max_hight = hight_list[i]

print(sum_step)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
tmp = 0
sum = 0
for a in a_list:
  if tmp < a:
    tmp = a
  else:
    sum += (tmp-a)

print (sum)

Submission status: Accepted

Solution code:
n = int(input())
num = [int(i) for i in input().split()]
limit = num[0]
sum = 0
for i in range(n):
    if(limit > num[i]):
        sum += limit - num[i]
    else:
        limit = num[i]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(" ")))
count=0
m=0
for i in A:
    if i>=m:
        m=i
    else:
        count+=m-i
print(count)

Submission status: Accepted

Solution code:




##main#######################################

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

## PROCESSING ###############################
max=0
ans=0
for n in A:
	if max<n : max=n
	
	if max>n : ans=ans + max-n

## OUTPUT ###################################
#print("++++OUTPUT++++")
#print(A)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
num = [int(i) for i in input().split()]
count = 0
for i in range(N):
    if i == 0:
        continue
    else:
        if num[i] < num[i-1]:
            count += num[i-1] - num[i]
            num[i] = num[i-1]
print(int(count))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.insert(0,0)
a_ = a.copy()
s = 0
for i in range(1,n):
    if a_[i+1] < a_[i]:
        a_[i+1] = a_[i]
ans = [a_[i] - a[i] for i in range(len(a))]
print(sum(ans))

Submission status: Accepted

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

print(sum)

Submission status: Accepted

Solution code:
# Hey, there Stalker!!!
# This Code was written by:
# ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
# ▒▒╔╗╔═══╦═══╗▒▒▒╔╗▒▒▒
# ▒╔╝║║╔═╗║╔═╗╠╗▒▒║║▒▒▒
# ▒╚╗║║║║║║║║║╠╬══╣║╔╗▒
# ▒▒║║║║║║║║║║╠╣║═╣╚╝╝▒
# ▒╔╝╚╣╚═╝║╚═╝║║║═╣╔╗╗▒
# ▒╚══╩═══╩═══╣╠══╩╝╚╝▒
# ▒▒▒▒▒▒▒▒▒▒▒╔╝║▒▒▒▒▒▒▒
# ▒▒▒▒▒▒▒▒▒▒▒╚═╝▒▒▒▒▒▒▒
# ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
#from functools import reduce

#mod=int(1e9+7)
#import resource
#resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY])
#import threading
#threading.stack_size(2**26)
"""fact=[1]
#for i in range(1,100001):
#    fact.append((fact[-1]*i)%mod)
#ifact=[0]*100001
#ifact[100000]=pow(fact[100000],mod-2,mod)
#for i in range(100000,0,-1):
#    ifact[i-1]=(i*ifact[i])%mod"""
#from collections import deque, defaultdict, Counter, OrderedDict
#from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
#from heapq import heappush, heappop, heapify, nlargest, nsmallest


# sys.setrecursionlimit(10**6) 
from sys import stdin, stdout
import bisect            #c++ upperbound
from bisect import bisect_left as bl              #c++ lowerbound bl(array,element)
from bisect import bisect_right as br             #c++ upperbound
import itertools
from collections import Counter
from math import sqrt
import collections
import math
import heapq
import re
def modinv(n,p):
    return pow(n,p-2,p)
def cin():
    return map(int,sin().split())
def ain():                           #takes array as input
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
  
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l

def most_frequent(list):
    return max(set(list), key = list.count) 
def GCD(x,y):
    while(y):
        x, y = y, x % y
    return x

def ncr(n,r,p):                                              #To use this, Uncomment 19-25 
    t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p
    return t

def Convert(string): 
    li = list(string.split("")) 
    return li 

def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
prime=[]
q=[]
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j



#Implement heapq
#grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90] 
#print(heapq.nlargest(3, grades)) #top 3 largest
#print(heapq.nsmallest(4, grades))
#Always make a variable of predefined function for  ex- fn=len
#n,k=map(int,input().split())
"""*******************************************************"""
def main():
    n=inin()
    a=ain()
    arr=[0]*n
    for i in range(n-1):
        if a[i+1]<a[i]:
            arr[i+1]=abs(a[i]-a[i+1])
            a[i+1]=a[i]
    print(sum(arr))
    #print(arr)
    #print(a)
"""*******************************************************"""
######## Python 2 and 3 footer by Pajenegod and c1729
py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
 
import os, sys
from io import IOBase, BytesIO
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__== "__main__":
    main()
#threading.Thread(target=main).start()

Submission status: Accepted

Solution code:
n = int(input())
l = input()
a = l.split(' ')

count = 0

for i in range(len(a)-1):
    if(int(a[i]) > int(a[i+1])):
        count += int(a[i]) - int(a[i+1])
        a[i+1]=a[i]

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

A=[int(i) for i in input().split()]
maxA=A[0]
sumA=0
for i in range(1,N):
    if A[i]<maxA:
        sumA+=maxA-A[i]
    else:
        maxA=A[i]
print(sumA)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
a=int(input())
b=list(map(int,input().split()))
c=copy.deepcopy(b)
for i in range(a-1):
    if b[i]>b[i+1]:
        b[i+1]=b[i]
ans=0
for i in range(a):
    ans+=b[i]-c[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

step = 0

for i in range(n):
    if i == 0:
        continue
    if h[i] >= h[i-1]:
        continue

    step += (h[i-1] - h[i])
    h[i] = h[i-1]

print(step)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

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

print(ans)

Submission status: Accepted

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

for i in range(N):
    if A[i] > K:
        K = A[i]
    else:
        S += (K - A[i])
print(S)

Submission status: Accepted

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

need_height = height_list[0]
total_need_step = 0

for i in range(1, len(height_list)):
    if height_list[i] < need_height:
        degree = need_height - height_list[i]
        total_need_step += degree
    else:
        need_height = height_list[i]

print(total_need_step)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(s) for s in input().split()]

cnt = 0
highest = 0
for x in a:
    if highest > x:
        cnt += (highest - x)
    else:
        highest = x
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3

"""
https://atcoder.jp/contests/abc176/tasks/abc176_c
Step
"""


def solve(a, n):
    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]

    return ans

if __name__ == "__main__":
    n = int(input())
    a = [(lambda x: int(x))(x) for x in input().split()]

    print(solve(a, n))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dai=[0]*n
x=a[0]
for i in range(n-1):
    if a[i+1]-x<0:
        dai[i+1]=x-a[i+1]
        x=a[i+1]+dai[i+1]
    else:
        x=a[i+1]
print(sum(dai))

Submission status: Accepted

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

max_num = 0
step = []
for i in range(n):
    if a[i] >= max_num:
        max_num = a[i]
    else:
        step.append(max_num - a[i])
print(sum(step))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline().strip())
A = list(map(int, readline().split()))

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

print(d)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum1=0
mini=a[0]
for i in range(len(a)-1):
    s=a[i+1]
    if mini<s:
        mini=s
        continue
    elif mini>=s:
        sum1+=abs(mini-s)

print(sum1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
N : 人数
A1.....An : n番目の人の身長
自分より前に自分より背の高い人がいないようにするための踏み台の最小値
"""

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

before_p = A_list[0]
ans = 0

for i in A_list:
  comp = 0
  if i == before_p:
    pass
  elif i >= before_p:
    pass
  elif i < before_p:
    comp = before_p - i
    i += comp
  before_p = i
  ans += comp

print(ans)

Submission status: Accepted

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

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

print(sum(height))

Submission status: Accepted

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

siz = len(A)
for i in range(siz-1):
    if A[i] <= A[i+1]:
        B.append(0)
    elif A[i] > A[i+1]:
        B.append(A[i] - A[i+1])
        A[i+1] = A[i+1] + B[i+1]
    else:
        pass
ans = 0
for i in range(siz):
    ans += B[i]
print("{0}".format(ans))

Submission status: Accepted

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

t = 0

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

print(t)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

Submission status: Accepted

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

max_height = 0
step_sum = 0

for i in heights:
    if max_height < i:
        max_height = i
    if max_height > i:
        step_sum += max_height - i
print(step_sum)

Submission status: Accepted

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

print(cnt)

Submission status: Accepted

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

pre_max = 0
ans = 0
for An in A:
    if pre_max <= An:
        pre_max = An
    else:
        ans += pre_max - An

print(ans)

Submission status: Accepted

Solution code:
from typing import List
import sys


def solution(inp: List) -> int:
    inp.pop(0)
    ans: int = 0
    recent: int = 0
    for n in inp:
        if recent > int(n):
            ans += recent - int(n)
            continue
        recent = int(n)
    return ans


inp: List = []
for j in sys.stdin:
    inp += j.split()
print(solution(inp))

Submission status: Accepted

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

cnt, tmp = 0, A[0]
for a in A[1:]:
    if tmp > a:
        cnt += (tmp-a)
    else:
        tmp = a
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

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

max = A[0]
sum = 0
for i in range(N):
    if A[i] < max:
        sum += max - A[i]
    else:
        max = A[i]
        
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

fwd_tall = ls[0]
sum_e = 0
for i in range(N):
    tall = ls[i]
    e = 0
    if fwd_tall > tall:
        e = fwd_tall - tall
        ls[i] += e
    sum_e += e
    #print(e)
    fwd_tall = ls[i]
print(sum_e)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(x) for x in input().split()]
maxi=l[0]
sum1=0
for i in range(1,len(l)):
    if maxi>l[i]:
        sum1=sum1+(maxi-l[i])
    maxi=max(maxi,l[i])
print(sum1)