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

dai = 0

for i in range(1, n):
    if a_list[i] < a_list[i-1]:
        dai += a_list[i-1] - a_list[i]
        a_list[i] += a_list[i-1] - a_list[i]
#        print(dai)
        
    else:
        continue

print(dai)

Submission status: Accepted

Solution code:
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

N=II()
A=LI()
L=[0]*N
TM=A[0]
for i in range(N):
  TM=max(TM,A[i])
  L[i]=TM-A[i]
  
  #print(L[i],TM,A[i])
ans=sum(L)
print(ans)

Submission status: Accepted

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
a = lmiinps()

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

Submission status: Accepted

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

for i in a:
  if i <=m:
    ans += m-i
  else:
    m = i

print(ans)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = int(input())
a_list = np.array(list(map(int, input().split())))

a_mx = np.maximum.accumulate(a_list)

ans = (a_mx - a_list).sum()

print(ans)

Submission status: Accepted

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

h = 0
r = 0
for v in A:
	need = 0 if v >= h else h-v
	r += need
	h = need + v
print(r)

Submission status: Accepted

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

Submission status: Accepted

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

steps = 0
steps_list = []
for i in range(1,N):
	if A[i] < A[i-1]:
		steps_list.append(A[i-1] - A[i])
		A[i] += A[i-1] - A[i]
print(sum(steps_list))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=input()
b=input()
c=b.split()
d=map(int,c)
main=list(d)
sum=0
for i in range(len(main)-2):
    stool=main[i]-main[i+1]
    if stool>0:
        main[i+1]+=stool
        sum+=stool

stool=main[len(main)-2]-main[len(main)-1]
if stool>0:
    sum+=stool
    main[len(main)-1]+=stool

print(sum)

Submission status: Accepted

Solution code:

N = int(input())
As = list(map(int, input().split()))
ans = 0

tall = int(As[0])

for A in As:
	if(tall > A):
		ans += tall - A
	else:
		tall = A

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

i = 0
step = []
while i <= len(A) - 2:
    if A[i] > A[i+1]:
        step.append(A[i] - A[i+1])
        A[i+1] = A[i]
        i += 1
    else:
        i += 1

print(sum(step))

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
max=l[0]
ans=0
for i in range(1,len(l)):
    if l[i]<max:
        ans+=(max-l[i])
    else:
        max=l[i]
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]:
        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()))
h = 0
for i in range(1, len(a)):
    if a[i-1] > a[i]:
        h += a[i-1] - a[i]
        a[i] += a[i - 1] - a[i]
print(h)

Submission status: Accepted

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

Submission status: Accepted

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

t=a[0]
total=0

for i in range(n):
  if t>a[i]:
    total=total+t-a[i]
  else:
    t=a[i]
    
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

    ans = 0
    for idx in range(1, N):
        if A[idx] < A[idx - 1]:
            diff = A[idx - 1] - A[idx]
            ans += diff 
            A[idx] = A[idx - 1]

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

if __name__ == '__main__':
    main()

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

print(x)

Submission status: Accepted

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

ans=0
for i in range(n-1):
    ans+=max(s[i]-s[i+1],0)
    s[i+1]=max(s[i+1],s[i])

print(ans)

Submission status: Accepted

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

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

toki=a[0]
shigi=0

for i in range(1,n):
  if toki>a[i]:
    shigi += toki-a[i]
  else:toki=a[i]

print(shigi)

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())
height = [int(i) for i in input().split()]
box = [0]

for x in range(1 ,N):
    if(height[x] >= height[x-1] + box[x-1]):
        box.append(0)
    if(height[x] < height[x-1] + box[x-1]):
        box.append(- height[x] + height[x-1] + box[x-1])

sum = 0
for t in box:
    sum += t
print(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())
l = list(map(int,input().split()))

h = 0
sum = 0
for i in range(n):
    if l[i] <= h:
        sum += h - l[i]
    else:
        h = l[i]

print(sum)

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
max = 0

for i in range(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()))
num_b = 0
cnt = 0
for i in A:
    if num_b > i:
        cnt += num_b - i
    else:
        num_b = i
print(cnt)

Submission status: Accepted

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

tmp = 0
sum = 0
for i in a:
    if tmp > i:
        dif = tmp - i
        sum += dif
    else:
        tmp = i

print(sum)

Submission status: Accepted

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

hight_sum = 0
for i in range(n-1):
    hight = 0
    diff = a[i+1] - a[i]
    if diff < 0:
        hight -= diff
        a[i+1] -= diff
        hight_sum += hight
print(hight_sum)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
res = 0
temp_max = 0
for x in a:
    if temp_max > x:
        res += temp_max - x
    temp_max = max(temp_max,x)

print(res)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A=list(map(int,input().split()))
dansa = []
top = 0

for i in range(N):
    dansa.append(max(0,top-A[i]))
    top = max(top,A[i])

print(sum(dansa))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    s = 0
    ans = 0

    for a in A:
        if a > s:
            s = a
        else:
            ans += s - a

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C - Step

from itertools import accumulate
import operator

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

b = accumulate(a, max)
s = sum(map(operator.sub, b, a))
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    high = 0
    ans = 0
    for a in A:
        if high > a:
            ans += high - a
        elif high < a:
            high = a
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

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

count = 0
temp = a[0]

for i in range(n):
  if a[i] >= temp:
    temp = a[i]
  else:
    count += temp - a[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ret)

Submission status: Accepted

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

max_num = 0
sum_num = 0
for i in A:
    max_num =  max(i, max_num)
    calcu_num = max_num
    sum_num += (calcu_num - i)

print(sum_num)

Submission status: Accepted

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

a=0
result=0

for i in range(n-1):
  if A[i] > A[i+1]:
    a = A[i] - A[i+1]
    A[i+1] += a
    result += a
    
print(result)

Submission status: Accepted

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

MAX = 0
ANS = 0

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

print(ANS)

Submission status: Accepted

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

standard = height[0]
ans = 0
for i in range(n):
  if height[i] > standard:
    standard = height[i]
  elif height[i] < standard:
    ans += standard - height[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n=int(input())
A=input_array()

front=0 # 1つ前の人の身長
ans=0 # 踏み台のたかさの合計
for a in A:
	if a < front:
		ans+=front-a
	else:
		front=a

print(ans)

Submission status: Accepted

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

height = 1
step = 0

for i in range(N):
  if As[i] < height:
    step += height - As[i]
  else:
    height = As[i]
    
print(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]:
        pass
    else:
        ans += a[i] - a[i+1]
        a[i+1] = a[i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import math
inp = lambda : stdin.readline().strip()

n = int(inp())
a = [int(x) for x in inp().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] - a[i]
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from math import ceil
inp = lambda : stdin.readline().strip()

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

curr = 0
ans = 0
for i in a:
    curr = max(curr, i)
    if i < curr:
        ans += curr - i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flag = 0
ans = 0
for i in range(len(A)):
    if A[i] < flag:
        ans += flag - A[i]
    else:
        flag = A[i]
print(ans)

Submission status: Accepted

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

ans = 0

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


print(ans)

Submission status: Accepted

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

max_tmp = 0
ans = 0

for i in range(n):
    max_tmp = max(max_tmp, lst[i])
    ans = ans + (max_tmp - lst[i])

print(ans)

Submission status: Accepted

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

h = 0
ans = 0

for i in range(N):
    h = max(h, As[i])
    ans += h - As[i]

print(ans)

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split(' ')))
mx = float('-inf')
ans = 0

for val in nums:
    if val < mx:
        ans += mx - val
    
    mx = max(val, mx)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for x in A:
    if x < maxBefore:
        steps += maxBefore - x
    else:
        maxBefore = x
print(steps)

Submission status: Accepted

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

r = 0
m = 0
for i in a:
    if m <= i:
        m = i
    else:
        r += m-i
print(r)

Submission status: Accepted

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

print(sum(A)-B)

Submission status: Accepted

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

for i in range(N - 1):
  if A[i] > A[i + 1]:
    dif = A[i] - A[i + 1]
    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())) 
Ad = A.copy()
sum = 0

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

for j in range(N):
    sum += A[j]-Ad[j]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def func(A):
  result = 0
  min = 0
  for a in A:
    if a < min:
      result += min - a
    else:
      min = a
  return result

if __name__ == "__main__":
    N = int(input())
    A = list(map(int,input().split()))
    print(func(A))

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

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]
    else:
        tmp = A[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N = input()
    heights = [int(x) for x in input().split(" ")]

    max_heights = 0
    s = 0

    for h in heights:
        if h < max_heights:
            s += max_heights - h
        else:
            max_heights = h
    print(s)
    
resolve()

Submission status: Accepted

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

a =[]
for i in range(1, N):
  if A[i-1] -A[i] > 0:
    add = A[i-1] -A[i]
    a.append(add)
    A[i] += add
print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
arr=[int(i) for i in input().split()]
so_far=arr[0]
tot=0
for i in range(1,n):
    if arr[i]<so_far:
        tot+=so_far-arr[i]
    else:
        so_far=arr[i]
print(tot)

Submission status: Accepted

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

ma = vals[0]
res = [0 for _ in range(n)]
for i, val in enumerate(vals):
    if val < ma: 
        res[i] = ma - val
    if ma < val:
        ma = val

print(sum(res))

Submission status: Accepted

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

M = 0
S = 0

for a in A:
    if M < a:
        M = a
    if a < M:
        S += M - a

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

Submission status: Accepted

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

print(sum(ans))

Submission status: Accepted

Solution code:
n = int(input())
total = 0
a = [int(v) for v in input().split()]
highest = a[0]
for i in range(1, len(a)):
    first = a[i - 1]
    second = a[i]
    if second < first:
        diff = first - second
        a[i] = second + diff
        total += diff
print(total)

Submission status: Accepted

Solution code:
def sol(lis):
    m = lis[0]
    ans = []
    for i in range(len(lis)):
        if lis[i]<m:
            ans.append(m-lis[i])
            lis[i] = m
        elif  lis[i]!=m:
            m = max(m,lis[i])
    print(sum(ans))
input()
lis = list(map(int,input().split(' ')))
sol(lis)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

for i in range(1,n):
  max_a_list[i] = max(max_a_list[i - 1],a[i])
sum_ = 0
for i in range(n):
  if max_a_list[i] > a[i]:
    sum_ += max_a_list[i] - a[i]
print(sum_)

Submission status: Accepted

Solution code:
import sys

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

h=0
k=0

for i in range(n):
  if h<=li[i]:
    h=max(h,li[i])
  else:
    k=k+(h-li[i])
    h=max(h,li[i])
    
print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
strings = input().split()

needs = 0
thred = int(strings[0])
for s in strings[1:N]:
  now = int(s)
  if now < thred:
    needs = needs + thred - now
  else:
    thred = now
    
print(needs)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(sum_step)

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

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(len(l)-1):
    if l[i] <= l[i+1]:
        pass
    else:
        ans += l[i] - l[i+1] 
        l[i+1] += l[i] - l[i+1] 
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from itertools import combinations
from itertools import combinations_with_replacement
from itertools import product
from collections import defaultdict
from collections import deque
from collections import Counter
import heapq


def input_li():
    return list(map(int, input().split()))


N = int(input())
A_LI = input_li()
max_h = A_LI[0]
ans = 0
for i in range(1, N):
    if A_LI[i] < max_h:
        ans += max_h - A_LI[i]
    else:
        max_h = max(max_h, A_LI[i])
print(ans)

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

print(ans)

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] = max(A[i], A[i + 1])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
answer = 0
prev = 0
for a in A:
    step = max(0, prev - a)
    answer += step
    prev = a + step
print(answer)

Submission status: Accepted

Solution code:
'''
Problem Link - https://atcoder.jp/contests/abc176/tasks/abc176_c
'''
from sys import stdin,stdout
from math import *
from collections import *

mod = 1000000007

def gcd(a,b):
	while b:
		a,b = b,a%b
	return a

def lcm(a,b): return (a*b) // gcd(a,b)

def set_bits(X):
	c = 0
	while X:
		X &= (X-1);c += 1
	return c

def compute_MOD(N,M): return (N%M + M) % M

def get_array(): return list(map(int, stdin.readline().split()))
 
def get_ints(): return map(int, stdin.readline().split())

def get_int(): return int(stdin.readline())

def get_input(): return stdin.readline().strip()

def main():
    tc = get_int()
    arr = get_array()
    ans = 0
    Max = arr[0]
    for i in arr[1:]:
    	Max = max(Max,i)
    	ans += Max-i
    stdout.write(str(ans)+'\n')
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


for i in range(n-1):
    if a[i] >= a[i+1]:
        x = a[i] - a[i+1]
        total += x
        a[i+1] = a[i]
        i = i+1
    else:
        total += 0

print(total)

Submission status: Accepted

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

# max(abs(x-y) for x, y in  zip(A, A[1:]))

stool = A[0]
ans = 0
for i, h in enumerate(A):
    if h < stool:
        ans += stool - h
    stool = max(stool, h)

print(ans)