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

Submission status: Accepted

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

tmp = a[0]
ans = 0
for i in a:
    if i < tmp:
        ans += tmp-i
    else:
        tmp = i
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

mx = a[0]
ans = 0
for x in a:
  if x > mx:
    mx = x
  ans += mx
print(ans-sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

check = A[0]
ans = 0

for i in range(N):
    if check < A[i]:
        check = A[i]
    elif check > A[i]:
        ans += check - A[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

data = list(map(int, input().split(' ')))

before_max = 0

dai = 0

for i in range(len(data)):
    if(i == 0):
        before_max = data[i]
    else:
        # 自分の身長がこれまでの最大身長よりも高いとき
        if(before_max < data[i]):
            before_max = data[i]
            continue
        # これまでの最大身長よりも自分の身長が低いとき
        elif(before_max > data[i]):
            dai += before_max - data[i]

print(dai)

Submission status: Accepted

Solution code:
num = int(input())
line = list(map(int,input().split()))

mini = line[0]
x = 0
for i in range(1,num):
  if mini > line[i]:
    x += mini - line[i]
  else:
    mini = line[i]
print(x)

Submission status: Accepted

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

sum_of_Humidai = 0
for i in range(1, len(ARR)):
    diff = (ARR[i - 1] - ARR[i])
    if diff > 0:
        ARR[i] += diff
        sum_of_Humidai += diff
else:
    print(sum_of_Humidai)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sum = 0
for i in range (N):
    if(i != 0)&(A[i - 1] > A[i]):
        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()))
b = a[0]
total=0
for i in a:
  if i<b:
    total+=b-i
  else:
    b=i
print(total)

Submission status: Accepted

Solution code:
n = int(input())
a = map(int, input().split())
m=0
cut=0
for i in a:
    if m<i:
        m=i
    elif m>=i:
        cut+=m-i
print(cut)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
nowh,stand=0,0
a=list(map(int,input().split()))
#print(*a)

for i in range(n):
    if nowh<a[i]:
        nowh=a[i]
    else:
        stand+=nowh-a[i]
        
print(stand)

Submission status: Accepted

Solution code:
n = input()
x = input().split()
m = 0
cnt = 0
 
for i in x:
    ii = int(i)
    if m < ii:
        m = ii
    elif m > ii:
        cnt += m - ii
 
print(cnt)

Submission status: Accepted

Solution code:
n = input()
x = input().split()
m = 0
cnt = 0

for i in x:
    ii = int(i)
    if m < ii:
        m = ii
    elif m > ii:
        cnt += m - ii

print(cnt)

Submission status: Accepted

Solution code:
a = input()
b = input().split()
b = [int(x) for x in b]
X = []
for i in range(len(b) - 1):
    if b[i + 1] <= b[i]:
        X.append(b[i] - b[i + 1])
        b[i + 1] = b[i + 1] + b[i] - b[i + 1]
print(sum(X))

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

Submission status: Accepted

Solution code:
n = input()
a = [int(i) for i in input().split()]
m = a[0]
step = 0
for i in a[1:]:
    if i < m:
        step += abs(m - i)
    m = max(m, i)
print(step)

Submission status: Accepted

Solution code:
# C
N = int(input())
As = list(map(int, input().split()))

humidai = []

humidai.append(0)
for i in range(len(As))[1::]:
    humidai.append(As[i-1]+humidai[i-1]-As[i] if As[i-1]+humidai[i-1]-As[i] > 0 else 0)

print(sum(humidai))

# B
# N = str(input())

# sum = 0
# for n in N:
#     sum += int(n)

# print('Yes' if sum % 9 == 0 else 'No')

# #A
# N, X, T = map(int, input().split())

# print(T * (N//X if N % X == 0 else N//X+1))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
t=0
a=0
for i in A:
    if t<=i:
        t=i
    else:
        a+=t-i
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def change_height(list, n):
    for i in range(n-1):
        if list[i] > list[i+1]:
            diff = list[i] - list[i+1]
            list[i+1] += diff
    return list

c_num_list = []
for i in range(N):
    c_num_list.append(num_list[i])
changed_list = change_height(num_list, N)
total_step = 0
for j in range(N):
    total_step += changed_list[j] - c_num_list[j]

print(total_step)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
#a = l.index(max(l))
#b = l[0:a]
#c = l[a:]
max_l = l[0]
count = 0
for i in range(1,len(l)):
    if max_l >= l[i]:
        count =count + (max_l - l[i])
    else:
        max_l = l[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
elm_cont = int(input())
array = [int(i) for i in input().split()]

current = array[0]
sum = 0

for i in range(1, elm_cont):
    if array[i] < current:
        sum += current - array[i]
    else:
        current = array[i]

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
tall = 0
ans = 0
for i in A:
    if i < tall:
        ans += tall-i
    tall = max(i,tall)
print(ans)

Submission status: Accepted

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

prev_max = 0
ans = 0
for a in A:
    if a > prev_max:
        prev_max = a
    else:
        ans += (prev_max - a)
print(ans)

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

Submission status: Accepted

Solution code:
def solve(string):
    _, *a = map(int, string.split())
    ans = step = 0
    for a0, a1 in zip(a, a[1:]):
        step = max(0, a0 + step - a1)
        ans += step
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
N = input()

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

highest = 0
ans = 0
for num in A:
    if highest > num:
        ans += highest - num
    else:
        highest = num
print(ans)

Submission status: Accepted

Solution code:
N = input()
A = list(map(int, input().split()))
ans = []
tmax=0
for i in A:
  if tmax<i:
    tmax=i
    ans.append(0)
  else:
    ans.append(tmax-i)
print(sum(ans))

Submission status: Accepted

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

prev = 0
ans = 0
for a in A:
    if a < prev:
       ans += prev - a

    prev = max(prev, a)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):

    if a[i] >= max_height:
        max_height = a[i]
        continue

    result += (max_height - a[i])

print(result)

Submission status: Accepted

Solution code:
N=int(input())
A= list(map(int, input().split()))
takai=A[0]
kot=0
for a in range(N):
    if takai<=A[a]:
        takai=A[a]
    else:
        kot+=takai-A[a]

print(kot)

Submission status: Accepted

Solution code:
n = int(input())

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

answer = 0

for i in range(n - 1):
    d = a[i] - a[i + 1]
    if d > 0:
        answer += d
        a[i + 1] += d

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
mx=l[0];total=0
for it in l:
  if it>mx:mx=it
  total+=mx-it
print(total)

Submission status: Accepted

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

ans = 0

for i in range(n-1):
  if l[i+1]<l[i]:
    ans+= l[i]-l[i+1]
    l[i+1]=l[i]
    
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8

N = int(input())

A = [int(y) for y in input().split()]

ans = 0

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

print("{}".format(ans))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = [int(n) for n in input().split()]

ans = 0

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

Submission status: Accepted

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

for i in range(N - 1):
    step = max(0, A[i] - A[i + 1])
    answer += step
    A[i + 1] += step

print(answer)

Submission status: Accepted

Solution code:
from math import ceil
def main():
    n=int(input())
    aa=list(map(int,input().split()))
    m=aa[0]
    d=0
    for a in aa:
        if a<m:
            d+=m-a
        elif a>=m:
            m=a
    print(d)
main()

Submission status: Accepted

Solution code:
max_height = 0
step_sum = 0

input()
people_height_array = map(int, input().split())

for height in people_height_array:
    if max_height <= height:
        max_height = height
    else:
        step_sum += max_height - height

print(step_sum)

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

Submission status: Accepted

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

Submission status: Accepted

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

max = 0
sum = 0
for a in A:
  if a < max:
    sum += max - a
  if a > max:
    max = a

print(sum)

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] - 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):
    if i < N-1:
        a = A[i] - A[i+1]
        if a >= 0:
            s += a
            A[i+1] = A[i]
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()))
ans=0

for i,s in enumerate(a) :
    if i > n-2:
      break
    if a[i] > a[i+1] :
        ans = 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()))

ans = 0
mx_a = a[0]
for i in range(n):
    if mx_a > a[i]:
        ans += mx_a - a[i]
    
    mx_a = max(mx_a, a[i])

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math

#setrecursionlimit(10**6)
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')
MOD = 10**9 + 7

def main():
	n = int(rl())
	H = list(rli())
	maxx = H[0]
	ans = 0
	for i in range(1, n):
		if H[i] < maxx:
			ans += maxx - H[i]
		maxx = max(H[i], maxx)
	print(ans)
	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
loop = int(input())

tall = list(map(int, input().split()))
front = tall[0]
standard = front
result = 0


for i in range(1, loop):
  back = tall[i]
  
  if back-standard > 0:
    standard = back
  elif back-standard < 0:
    result += standard - back
  front = back

print(result)

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())
list1 = list(map(int, input().split()))
h,c = 0,0
for i in list1:
    if i < h:
        c += h - i
    else:
        h = i
print(c)

Submission status: Accepted

Solution code:
# string
# a, b, c = input().split()
# str_list = list(input().split())

# number
# a, b, c = map(int, input().split())
# num_list = list(map(int, input().split()))

# lows
# str_list = [input() for _ in range(n)]

# many inputs
# num_list = []
# for i in range(n): num_list.append(list(map(int, input().split())))

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()))
ans = 0
for i in range(1, N):
    if A[i-1] > A[i]:
        d = A[i-1] - A[i]
        A[i] += d
        ans += d
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for a in A:
    s += max(m-a,0)
    m = max(m, a)
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

temp = 0
result = 0
for i in range(n):
  if(temp<li[i]):
    temp = li[i]
  else:
    result += temp - li[i]
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    max_height = 0

    for height in A:
        if max_height < height:
            max_height = height
        else:
            count += max_height - height
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int,input().split()))
    front = 0
    ans = 0
    for a in A:
        if front < a:
            front = a
        else:
            ans += front - a
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=input()
A=input().split()
ans=0
j=0
for i in A:
    if j>int(i):
        ans+=j-int(i)
    else:
        j=int(i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))

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

print(ans)

Submission status: Accepted

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

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]

print(ans)

Submission status: Accepted

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

for i in range(N-1):
    if input[i+1] < input[i]:
        ans = ans+(input[i]-input[i+1])
        input[i+1] = input[i]

print(ans)

Submission status: Accepted

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

result = 0
current = A[0]

for i in range(1,N):
    if current <= A[i]:
        current = A[i]
        continue
    else:
        result += current - A[i]

print(result)

Submission status: Accepted

Solution code:
try:
    k = int(input())
    l = list(map(int,input().split(' ')))
    c = 0
    for i in range(len(l)-1):
      x = l[i+1]-l[i]
      if x<0:
        l[i+1]+=abs(x)
        c+=abs(x)
    print(c)
except:
    pass

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = map(int, input().split())
h = []

max_h = 0
for a in A:
    if max_h <= a:
        max_h = a
        h.append(0)
    else:
        h.append(max_h - a)

print(sum(h))

Submission status: Accepted

Solution code:
n = int(input())

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

cnt = 0
for i in range(0,len(A)-1):
  if A[i] > A[i+1]:
    d = (A[i]-A[i+1])
    cnt = cnt + d
    A[i+1] = A[i+1] + d

print(cnt)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    As = list(map(int, input().split()))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
height=0
count=0
for i in l:
    if i<height:
        count+=height-i
    else:
        height=i
print(count)

Submission status: Accepted

Solution code:
n = int(input())
arr=[int(n) for n in input().split()]
ans = 0
m = arr[0]
for i in arr:
    if i < m:
        ans += (m-i)
        m=max(i,m)
    else:
        m = max(i,m)
        continue
print(ans)

Submission status: Accepted

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


def main():
    peopleNum, peopleList = problemInput()

    maxHeight = 0
    totalStepHeight = 0

    for ind in range(peopleNum):
        lookingPerson = peopleList[ind]
        if lookingPerson <= maxHeight:
            totalStepHeight += maxHeight-lookingPerson
        else:
            maxHeight = lookingPerson
    
    print(totalStepHeight)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = list(map(int, input().split()))

    mi = A[0]
    s = 0

    for a in A[1:]:
        if a <= mi:
            s += mi - a
        else:
            mi = a

    print(s)


if __name__ == '__main__':
    main()

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:
# coding: utf-8
# Your code here!

n = int(input())

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

dai = 0

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
m = a[0]
sum = 0
for i in a[1:]:
    if i <= m:
        sum += m - i
    else:
        m = i
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input().rstrip())
a = [int(v) for v in input().rstrip().split()]
r = 0
pre = a[0]
for i in range(1, N):
	d = a[i] - pre
	if d >= 0:
		pre = a[i]
		pass
	else:
		d2 = abs(d)
		r += d2

print(r)

Submission status: Accepted

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

ans=mx=0
for x in a:
    mx=max(mx,x)
    ans+=mx-x

print(ans)

Submission status: Accepted

Solution code:
num = int(input())
line = input().split(" ")
border = 0
step = 0
for i in line:
  if int(i) >= border:
    border = int(i)
  else:
    step += border - int(i)
print(step)

Submission status: Accepted

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

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:
def solve(n, arr):
    ans_arr = [0]
    for i in range(0, n-1):
        if arr[i] > arr[i + 1]:
            diff = arr[i] - arr[i + 1]
            ans_arr.append(diff)
            arr[i + 1] += diff
        else:
            ans_arr.append(0)
    return sum(ans_arr)

if __name__ == "__main__":
    n = int(input())
    arr = list(map(int, input().split(' ')))
    print(solve(n, arr))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
heights = list(map(int, input().split()))
before = 0
answer = 0

for h in heights:
  if h < before:
    answer += before - h
  else:
    before = h
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

for i in range(1,len(L)):
    if L[i-1] > L[i]:
        ans += L[i-1] - L[i]
        L[i] = L[i-1]

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

for a in A:
    ans += max(0, now_max - a)
    now_max = max(now_max, a)

print(ans)

Submission status: Accepted

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

sum = 0

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0
for n in range(N-1):
	if a[n]>a[n+1]:
		dif=a[n]-a[n+1]
		a[n+1]=a[n+1]+dif
		sum=sum+dif
print(sum)

Submission status: Accepted

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

temp = a[0]
count = 0
for i in range(1,n):
    count += max(0,temp-a[i])
    temp = max(temp,a[i])

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
 
h_list = [0]*(n+10)
 
for i in range(1,len(a_list)):
    if a_list[i]  < a_list[i-1] + h_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1] + h_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))

Submission status: Accepted

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

c=0
ans=0
for i in range(n):
	if c<a[i]:
		c=a[i]
	elif c>=a[i]:
		ans+=c-a[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

people_count = int(input())
people = list(map(int, input().split()))

stools, previous = 0, -1
for index in range(people_count):
    if index == 0:
        previous = people[index]
    else:
        current = people[index]

        if previous > current:
            stools += abs(current - previous)
            people[index] = previous
        else:
            previous = current

print(stools)

Submission status: Accepted

Solution code:
def main():
    _ = input()
    a = list(map(int, input().split()))
    max_height = 0
    ans = 0
    for height in a:
        if max_height <= height:
            max_height = height
        else:
            ans += max_height - height
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A = list(map(int, input().split()))
i = 0
sum = 0
while i<N-1:
  if A[i]<=A[i+1]:
    f=0
  else:
    f=A[i]-A[i+1]
  A[i+1]+=f
  sum+=f
  i+=1
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

total = 0
for i in range(1, N):
    d = max(A[i-1] - A[i], 0)
    A[i] += d
    total += d
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

high = 0;
con = 0;

for i in range(n):
    if a[i] > high:
        high = a[i]
    else:
        con += (high - a[i]);
print(con);

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