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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math

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

# 現在の最大値を覚える
b = [a[0] for _ in range(n)]
tmp = a[0]

for i in range(1, n): 
    if a[i] >= tmp:
        tmp = a[i]
    b[i] = tmp 

ans = 0 
for i in range(n):
    ans += (b[i]-a[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
d=[]
m=0
for i in range(n):
    if(l[i]>=m):
        d.append(0)
        m=l[i]
    else:
        d.append(m-l[i])
print(sum(d))

Submission status: Accepted

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

ans = 0
max_a = A[0]

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

print(ans)

Submission status: Accepted

Solution code:
def abc176c_step():
    n = int(input())
    a = list(map(int, input().split()))
    total = 0
    max_val = a[0]
    for v in a:
        if v <= max_val:
            total += max_val - v
        else:
            max_val = v
    print(total)

abc176c_step()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input().rstrip())
main=list(map(int,input().rstrip().split()))
count=0
sai=0
for i in range(n):
    if sai<main[i]:
        sai=main[i]
    else:
        count+=sai-main[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
max_h = a[0]
for aa in a:
    if aa > max_h:
        max_h = aa
    cnt += max_h - aa

print(cnt)

Submission status: Accepted

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

max_h=a[0]
ans=0
for h in a:
    if h<max_h:
        ans+=(max_h-h)
    else:
        max_h=max(h,max_h)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a =  list(map(int,input().split()))
wk = 0  #比較用
tp = 0  #背の基準
ans = 0

num = 0
while num < n:
  #比較処理
  wk = a[num]
  if tp < wk:
    tp = wk
  if a[num] < tp:
    ans = ans + (tp - a[num])
  num = num + 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(_) for _ in input().split()]
height=A[0]
s=0
for a in range(len(A)-1):
    if A[a+1]<height:
        s+=height-A[a+1]
    elif A[a+1]>height:
        height=A[a+1]
print(s)

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())

A=np.array(input().split(" "),dtype=np.int64)

step=[0]
Max=A[0]
for i in range(1,N):
    if(Max>A[i]):
        step.append(Max-A[i])
    else:
        Max=A[i]
        step.append(0)

print(sum(step))

Submission status: Accepted

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

Submission status: Accepted

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

c_max = 0
total = 0

for i in range(N):
  if i == 0:
    c_max = A[0]
  elif A[i] < c_max:
    total += c_max - A[i]
  else:
    c_max = A[i]

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

I=sys.stdin.readline

 #s="abcdefghijklmnopqrstuvwxyz"

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])
        #print(0,fumidai)
    else:
        height=a[i]
#print(height)
print(fumidai)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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):
    # print(a)
    if a[i] > a[i+1]:
        a[i+1] = a[i]

a_after = np.sum(a)

print(a_after - a_before)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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: #自分の方が高ければOK
        continue
ans = sum(D)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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, x, t = map(int, input().split())
  # print(A(n, x, t))
  # n_str = input()
  # print(B(n_str))
  n = int(input())
  a = list(map(int, input().split()))
  print(C(n, a))


if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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