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

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

print(int(count))

Submission status: Accepted

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

S=0
for i in range(1,N):
  if A[i]<A2[i-1]:
    A2[i]=A2[i-1]
    S+=(A2[i]-A[i])
# print(A2)
print(S)

Submission status: Accepted

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

ki = 0
s = 0
for i in range(0,n):
    if ki < li[i]:
        ki = li[i]
    else:
        s += ki-li[i]
print(s)

Submission status: Accepted

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

max = A[0]

for length in A[1:]:
    if length < max:
        length_sum += (max - length)

    elif length > max:
        max = length

print(length_sum)

Submission status: Accepted

Solution code:
n=input()
A = input().split()
top_sum=0
top=int(A[0])
for i in range(1,int(n)):
    if int(A[i])>top:
        top = int(A[i])
    else:
        top_sum += top - int(A[i])

print(top_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def step(arr):
    res = 0
    for i in range(1,len(arr)):
        if arr[i] >= arr[i-1]:
            continue
        else:
            res += abs(arr[i-1] - arr[i])
            arr[i] = arr[i - 1]
    return res

def start():
    n = int(input())
    arr = [int(x) for x in input().split(" ")]
    print(step(arr))
start()

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

Submission status: Accepted

Solution code:
_ = input()
a_n = list(map(int, input().split()))

h = a_n[0]
s = 0
for a in a_n[1:]:
    if h > a:
        s += h -a
    if a > h:
        h = a
print(s)

Submission status: Accepted

Solution code:
N =int(input())
li = list(map(int,input().split()))
min = li[0]
h = 0

for a in li:
  if min > a:
    h += min - a
  elif min < a:
    min = a

print(h)

Submission status: Accepted

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

max_height = heights[0]
total_step = 0

for h in range(1, len(heights)):
    if max_height >= heights[h]:
        total_step = total_step + max_height - heights[h]
    else:
        max_height = heights[h]

print(total_step)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sum = 0
for i in range(len(A)):
    if i == len(A)-1:
        break
    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())

As = [int(i) for i in input().split(" ")]


current_max_height = 0


need_height = 0
for i in range(N):

    if As[i] > current_max_height:
        current_max_height = As[i]

    need_height += current_max_height - As[i]

print(need_height)

Submission status: Accepted

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

num = int(input())
tool = list(map(int, input().split()))

def tool_set(num, tool):
  count = 0
  for i in range(num - 1):
    if tool[i] > tool[i + 1]:
      count += tool[i] - tool[i + 1]
      tool[i + 1] = tool[i]
  print(count)

tool_set(num, tool)

Submission status: Accepted

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

sum = 0

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

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
arr=[int (x) for x in input().split()]
q=arr[:]
i,j=0,1
ans=0
while i<n and j<n:
    if arr[i]>arr[j]:
        arr[j]=arr[i]
    i+=1
    j+=1
print(sum(arr)-sum(q))

Submission status: Accepted

Solution code:
N = int(input())
line = input()
A = line.split()
hei = 0
for i in range(N-1):
    d = int(A[i+1]) - int(A[i])
    if d < 0:
        hei = hei - d
        A[i+1] = str(int(A[i+1]) - d)
print(hei)

Submission status: Accepted

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

result = 0

for idx in range(len(A)):

    if idx == 0:
        continue
    elif A[idx-1] > A[idx]:
        result += A[idx-1] - A[idx]
        A[idx] = A[idx-1]

print(result)

Submission status: Accepted

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

for i in range(1, n):
  if lst[i] < num:
    res += lst[i - 1] - lst[i]
    lst[i] = lst[i - 1]
  else:
    num = lst[i]
    
print(res)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))

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

print(count)

Submission status: Accepted

Solution code:

N = int(input())
B = list(map(int,input().split()))
D = [0] * N

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

print(sum(D))

Submission status: Accepted

Solution code:
_ = input()
As = map(int, input().strip().split())

total_steps_height = 0
current_height = 0

for Ai in As:
	if current_height > Ai:
		total_steps_height += (current_height - Ai)
	if current_height < Ai:
		current_height = Ai

print(total_steps_height)

Submission status: Accepted

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

Submission status: Accepted

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

a = row[0]
b =0
fumi = 0
for i in row:
  b = i
  if a <= b:
    a = b
    next
  else:
    fumi += (a - b)
    a = max(a, b)
print(fumi)

Submission status: Accepted

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


def main():
    N = int( sys.stdin.readline() )
    A_list = list(map(int, sys.stdin.readline().split()))

    prev = A_list[0]
    sum_diff = 0

    for i in range(1, len(A_list)):
        if prev > A_list[i]:
            sum_diff += (prev - A_list[i])
        else:
            prev = A_list[i]
    
    print(sum_diff)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == "__main__":
  n = int(input())
  ai = list(map(int, input().split()))
  li = []
  for i in range(n - 1):
    if ai[i] > ai[i + 1]:
      li.append(ai[i] - ai[i + 1])
      ai[i+1]=ai[i]
    else:
      pass
  print(sum(li))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
ma = max(L)
mi = L.index(ma)
S = 0
nma = L[0]

for i in range(1, len(L)):
  if mi <= i:
    S += ma - L[i]
  else:
    nma = max(nma, L[i])
    S += nma - L[i]
print(S)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int, input().split()))
sum = 0

for num in range(1,n):
  if lis[num] < lis[num - 1]:
    sum += lis[num - 1] - lis[num]
    lis[num] = lis[num - 1]
    
print(sum)

Submission status: Accepted

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

answer = 0
m = a[0]

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
count,max_n=0,a[0]
for i in range(1,n):
    if max_n>a[i]:
        count+=max_n-a[i]
    else:
        max_n=max(max_n,a[i])
print(count)

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
l = 0
count = 0
max_h = 0
for i in range(1,N):
    if A[i-1]>max_h:
        max_h = A[i-1]
    if A[i]<max_h:
        k = max_h - A[i]
        count += k
        if k>l: l=k
        A[i] = max_h
print(count)

Submission status: Accepted

Solution code:
N, S, *A = map(int,open(0).read().split())
ans = 0

for i in A:
    if i > S:
        S = i
    else:
        ans += S -i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x.strip()) for x in input().split()]
b = [0]
for i in range(n):
  if i == n-1:
    break
  elif a[i]+b[i] > a[i+1]:
    b.append(a[i]+b[i]-a[i+1])
  else:
    b.append(0)
print(sum(b))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)-1):
    a_before = A[i]
    a_after = A[i+1]

    # print(a_before, a_after)
    diff = a_before-a_after
    # print(diff)
    if diff > 0:
        A[i+1] = a_before
        ans += diff

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
res = 0

for i in range(1, N):
    before = A_list[i - 1]
    after = A_list[i]
    if before > after:
        res += before - after
        A_list[i] += before - after

print(res)

Submission status: Accepted

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

c = 0

for i in range(n):
  if i == 0:
    continue
  if m[i-1]>m[i]:
    res = m[i-1] - m[i]
    c += res
    m[i] = res+m[i]
  
print(c)

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

Submission status: Accepted

Solution code:
N = int(input())
l = map(int,input().split())
two, count = 0, 0
for i in l:
  one = i
  if one < two:
    count += two - one
  else:
    two = one
    continue
    
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    a = LI()
    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(a[i+1],a[i])
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
c = 0
S = [0 for i in range(N)]

for i in range(N):
    if A[i] < c :
        S[i] = c - A[i]
    elif A[i] > c :
        c = A[i]
  
ans = sum(S)

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

Submission status: Accepted

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

n = int(input())

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

l_max = max(lengths)
i_l_max = lengths.index(l_max)

ans = 0
_max = lengths[0]

for i in range(len(lengths)):
    if i < i_l_max:
        if _max > lengths[i]:
            ans += _max - lengths[i]
        else:
            _max = lengths[i]
    else:
        ans += l_max - lengths[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main(n, lst):
	cnt = 0
	for i in range(n-1):
		if lst[i] > lst[i+1]:
			cnt += lst[i] - lst[i+1]
			lst[i+1] = lst[i]
	return cnt

n = int(input())
lst = list(map(int, input().split()))
print(main(n, lst))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
N = int(input())
numbers = input().split()
numbers = list(map(int,numbers))
m = 0
gross_count = 0
for n in numbers:
    if m > n:
        gross_count += (m-n)
    else:
        m = n
print(gross_count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

    return ans

print(main())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
stool = 0
for i in range(N):
    for j in range(i+1,i+2):
        if j >= N:
            break
        if A[i] > A[j]:
            stool += A[i] - A[j]
            A[j] += A[i] - A[j]

print(stool)

Submission status: Accepted

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


pre_max_cnt = arr[0]
total_cnt = 0
for i in arr:
    if pre_max_cnt > i:
        total_cnt += abs(pre_max_cnt - i)
    else:
        pre_max_cnt = i
print(total_cnt)

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

Submission status: Accepted

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

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())
Alist = list(map(int, input().split()))
Sum = 0
for n in range(1, N):
  if Alist[n] < Alist[n-1]:
    Sum += Alist[n-1] - Alist[n]
    Alist[n] = Alist[n-1]
print(Sum)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    a = LI()
    ans = 0
    for i in range(1, n):
        temp = max(a[i-1]-a[i], 0)
        ans += temp
        a[i] += temp
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(D)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))

MOD = 10**9 + 7


def main():
	# ans_ = []
	# ans_.append(ans)
	# print(("\n").join(map(str, ans_)))
	n = int(input()); arr = read()
	s = 0
	for i in range(1, n):
		s += max(0, arr[i-1]-arr[i])
		arr[i] += max(0, arr[i-1]-arr[i])

	print(s)






if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input().rstrip())
A = [int(i) for i in input().rstrip().split()]
B = A.copy()

for i in range(N-1):
  if B[i+1] < B[i]:
    B[i+1] = B[i]
    
A = np.array(A)
B = np.array(B)
C = B - A

print(np.sum(C))

Submission status: Accepted

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

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

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
old_h = 0
for i, h in enumerate(A):
    if (diff:=h-old_h) < 0: 
        res -= diff
    else:
        old_h = h

print(res)

Submission status: Accepted

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

#print(alist)
h=0
pre=0
for a in alist:
    if pre>0 and pre>a:
        h+=(pre-a)
#        print('{}->{}[{}]'.format(a,pre,h))
    if a > pre:
        pre=a

print(h)

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):
  ans += max(M - A[i], 0)
  M = max(M, A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-1):
    if l[i] >= a[i+1]:
        l.append(l[i])
    else:
        l.append(a[i+1])

print(sum(l) -sum(a))

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
a = intl()

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math

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

Max = 0
dai = 0
dai_sum = 0

for a in A:

    if Max > a:
        dai = Max - a
        dai_sum += dai

    if Max < a:
        Max = a

print(dai_sum)