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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
input()
a = list(map(int, input().split()))
r=0
l=a[0]
for i in range(1,len(a)):
  if l > a[i]:
    r=r+(l-a[i])
  else:
    l=a[i]
print(r)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math

N = int(input())

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

front = 0
base = 0
basesum = 0
for i in range(N):
    if front > A[i]:
        base = front - A[i]
        basesum = basesum + base
        front = A[i] + base
    else:
        base = 0
        front = A[i]

print(basesum)

Submission status: Accepted

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

mx = a[0]
for i in a:
	if i < mx:
		ans += mx - i
	else:
		mx = i

print(ans)

Submission status: Accepted

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

A_tobi=[0]*N
A_max=A[0]
for i in range(len(A)):
    x_sum=A[i]
    if A[i]<A_max:
        x_sum=A_max
        A_max=x_sum
        A_tobi[i]=(x_sum-A[i])
    else :
        A_max=A[i]

print(sum(A_tobi))

Submission status: Accepted

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

sum = 0

for i in range(1, n):
    stoolHeight = max(0, (A[i-1] - A[i]))
    A[i] += stoolHeight
    sum += stoolHeight

print(sum)

Submission status: Accepted

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

count = 0

for i, j in enumerate(a):
    try:
        if j > a[i + 1]:
            b = j - a[i + 1]
            count += b
            a[i + 1] = j
        else:
            pass
    except IndexError:
        pass

print(count)

Submission status: Accepted

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

total = 0
for i in range(1, n):
    if heights[i] < heights[i - 1]:
        diff = heights[i - 1] - heights[i]
        heights[i] += diff
        total += diff

print(total)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
fumidai_list = [0] * N

for i in range(1, N):
    tmp = A_list[i] - (A_list[i-1] + fumidai_list[i-1])
    if tmp < 1:
        fumidai_list[i] = -tmp

print(sum(fumidai_list))

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    N = I()
    A = LI()
    a_max = A[0]
    ans = 0
    for i in range(1,len(A)):
        if A[i]<a_max:
            ans += a_max-A[i]
        else:
            a_max = A[i]
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(count - S)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
A = input().split()

counter = 0
result = 0

for i in A:
    if counter > int(i):
        add = counter - int(i)
        result = result + add
    else:
        counter = int(i)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import stdin,stdout
import math
import time
from functools import lru_cache
from collections import Counter
import heapq
#@lru_cache(maxsize=None) #for optimizing the execution time of callable objects/functions(placed above callable functions)
try:
    for _ in range(1):
        n=int(input())
        arr=[int(i) for i in input().split()]
        ans=0
        mx=arr[0]
        for i in range(n):
            if arr[i]<mx:
                ans+=mx-arr[i]
                mx=max(arr[i],mx)
            else:
                mx=arr[i]
        print(ans)
except EOFError as e:
    print(e)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    N, = map(int, input().split())
    As = list(map(int, input().split()))
    ans = 0
    last = 0
    for a in As:
        dif = max(0,last-a)
        last = a+dif
        ans = ans+dif


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

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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


# 整数の入力
n = int(input())

# スペース区切りの整数の入力
a_list = list(map(int, input().split()))

# print(a_list)

result = 0
before_a = 0
for a in a_list:
  # print("---")
  # print(before_a, a)
  diff = before_a - a
  # print(diff)
  if diff < 0:
    pass
    # print("マイナス")
  else:
    result += diff
    a = a + diff

  before_a = a

print(result)

Submission status: Accepted

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

tot=0
for i in range(N-1):
  if A[i]>A[i+1]:
    table=A[i]-A[i+1]
    A[i+1]+=table
    tot+=table
  else:
    pass

print(tot)

Submission status: Accepted

Solution code:
N = input()
A = input().split()

summation = 0
M = int(A[0])
for i in range(int(N)):
    if int(A[i]) < M:
        summation += M - int(A[i])
    else: M = int(A[i])

print(summation)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

steps = 0
human_mae = -1
for a in As:
    if human_mae == -1:
        human_mae = a
        continue
    if a < human_mae:
        step = human_mae - a
        steps += step
        human_mae = a + step
        continue
    human_mae = a

print(steps)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    a = A[i]
    x = (ap+xp)-a
    if x <= 0:
        xp = 0
    else:
        xp = x
    xcount = xcount + xp
    ap = a
print(xcount)

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

Submission status: Accepted

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

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    n = ii()
    h = lmi()
    step = 0
    for i in range(1, n):
        diff = h[i-1] - h[i]
        if diff > 0:
            # 前の人の方が大きい
            step += diff
            h[i] += diff
    print(step)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

maxh = A_n[0]
ans =0
for i  in range(1,N):
    if A_n[i] < maxh:
        sa = maxh - A_n[i]
        ans += sa
        maxh = A_n[i] +sa
    else:
        maxh = A_n[i]


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(0,len(A)-1):
    if(A[i]>A[i+1]):
        s = A[i]-A[i+1]
        ans += s
        A[i+1]=A[i]
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
humidais = [0]
for i in range(N-1):
    humidais.append(max(A[i]+humidais[i]-A[i+1], 0))

print(sum(humidais))

Submission status: Accepted

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

prev = 0
ans = 0
for i in range(0, N):
    cur = A[i]
    if cur >= prev:
        prev = cur
        continue

    ans += prev - cur

print(ans)

Submission status: Accepted

Solution code:
__DEUBG__ = 0

import math
N = int(input())
high = list(map(int, input().split()))
stack = []
#total = sum(high)
for idx, h in enumerate(high):
    if idx == 0:
        stack.append((idx, h))
    elif h > stack[-1][-1]:
        stack.append((idx, h))

if __DEUBG__:
    print(N, stack)
    #estack.append((N, stack[-1][-1]))

prev = 3
prev = N
total = 0
for idx, h in stack[::-1]:
    if __DEUBG__:
        print(prev, idx, h)
        print((prev - idx) * h)
    total += (prev - idx) * h
    prev = idx

if __DEUBG__:
    print(total, sum(high))
print(total - sum(high))

Submission status: Accepted

Solution code:

num = int(input())
strnum_line = input().split(" ")

num_line = [int(n) for n in strnum_line]
#print(num_line)
fumidai = 0 

for i in range(1,len(num_line)):
    if(num_line[i] < num_line[i-1]):
        #print(i)
        a = num_line[i-1] - num_line[i]
        #print(a)
        fumidai += a
        num_line[i] = num_line[i-1]
        #print(num_line)
        
print(fumidai)

Submission status: Accepted

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

count = 0
s = {}

for i in range(N - 1):
    d = (A[i] + s.get(i, 0)) - A[i + 1]
    if d > 0:
        count += d
        s[i+1] = d
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C
N=int(input())
A_arr=list(map(int,input().split()))
ans=0
h=0
for i in A_arr:
    if i<h:
        ans += h-i
    else:
        h=i
print(ans)

Submission status: Accepted

Solution code:
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())
high = list(map(int, input().split()))
dai = []
est = high[0]
for i in range(N):
  if est > high[i]:
    
    
    dai.append(est-high[i])
  elif est <= high[i]:
    est = high[i]
print(sum(dai))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
sum = 0
max = 0
for a_i in li:
    # 最大値判定
    if max <= a_i:  # あたらしく最大値になるパターン
        max = a_i
    else:  # 踏み台をおくパターン
        sum += max - a_i
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int, input().split()))
now = 0
front = 0
step = 0
for ai in an:
    now = ai
    if now < front:
        step += (front - now)
    else:
        front = now
print(step)

Submission status: Accepted

Solution code:
if __name__ == '__main__':

    n = int(input())

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

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

Submission status: Accepted

Solution code:
input()
r = m = 0
for x in map(int, input().split()):
  r += m - min(m, x)
  m = max(m, x)
print(r)

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


max = a[0]
wa = 0

for i in range(ninzu) : 
  if a[i] > max:
    max = a[i]
  else:
    wa += max - a[i]

print(wa)

Submission status: Accepted

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

count = 0
for i in range(n-1):
    if lis[i] > lis[i+1]:
        dif = lis[i] - lis[i+1]
        count += dif
        lis[i+1] += dif
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))
li_sum = sum(li)

for i in range(len(li)):
    if i == 0:
        continue
    j = i - 1
    if li[i] < li[j]:
        li[i] += li[j] - li[i]

print(sum(li) - li_sum)

Submission status: Accepted

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
ans = 0
m = A[0]
for a in A[1:]:
  ans += max(m-a, 0)
  m = max(a, m)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# t=int(input())
# for _ in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
maxa=arr[0]
count=0
for i in range(n):
    if arr[i] >= maxa:
        maxa=arr[i]
    else:
        count+=(maxa-arr[i])
print(count)

Submission status: Accepted

Solution code:
import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())

n=N()
l=L()
ans=0
for i in range(n-1):
    if l[i]>l[i+1]:
        ans+=l[i]-l[i+1]
        l[i+1]=l[i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
l=0
s=0
for c in range(n):
    if l>a[c]:
        s+=l-a[c]
    else:
        l=a[c]
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

pre_num, sum_stand = 0, 0

for i in A:
    if pre_num > i:
        sum_stand += pre_num - i
    else:
        pre_num = i

print(sum_stand)

Submission status: Accepted

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

sum = 0
for n in range(1, N):
    if A[n] < A[n-1]:
        sum += A[n-1] - A[n]
        A[n] = A[n-1]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flg = True
before = 0
for a in A:
    if flg:
        flg = False
        B.append(0)
        before = a
    else:
        add = max(before - a, 0)
        B.append(add)
        before = a + add

print(sum(B))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
c = 0
if N == 1:
  print(0)
else:
  for i in range(N-1):
    if a[i+1] < a[i]:
      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(len(A)):
    if i == len(A)-1:
        break
    else:
        front = A[i]
        behind = A[i+1]
        if front <= behind:
            pass
        else:
            ans += (front - behind)
            A[i+1] = front

print(ans)

Submission status: Accepted

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

B = []

for i in range(len(A)):
    if i == 0:
        B.append(A[i])
    else:
        if A[i] < B[i-1]:
            B.append(B[i-1])
        else:
            B.append(A[i])

diff = map(lambda a,b: a - b, B,A)
print(sum(list(diff)))

Submission status: Accepted

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

Submission status: Accepted

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

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

th = 0
ph = 0
for a in A:
    if ph > a:
        th += ph - a
    else:
        ph = a
print(th)

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

print(step)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from functools import lru_cache
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def list_t(l): return vfunc(lambda *args:args)(*l)

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

n,a = rvfunc(int)(reader())
tmax=0
f=0
for i in range(n[0]):
    ai=a[i]
    if a[i]>tmax:tmax=a[i]
    f+=tmax-a[i]
print(f)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。

count = 0
height = 0

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

print(count)

Submission status: Accepted

Solution code:
N = input()
tab = []
data = input().split()
res = 0
last = -1
for i in data:
    val = int(i)
    if (val < last):
        res += (last - val)
    else:
        last = val
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

    s = 0

    i = 1
    while i < n:
        while True:
            if (a[i-1] <= a[i]) :
                i+=1
                if i == n:
                    break
                continue
            else:
                s += a[i-1] - a[i]
                a[i]  = a[i-1]
                break
        i += 1
        
    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(n-1):
  if a[i]>=a[i+1]: 
    ans +=a[i]-a[i+1]
    a[i+1]+=a[i]-a[i+1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

max_height = A[0]
step = [0] * N
ans = 0

for a in A:
    if a < max_height:
        ans += max_height - a
    else:
        max_height = a
print(ans)

Submission status: Accepted

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

res = 0

for i in range(len(A)-1):
  if A[i] > A[i+1]:
    res += abs(A[i] - A[i+1])
    A[i+1] = A[i+1] + abs(A[i] - A[i+1])
    
print(res)

Submission status: Accepted

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

total = 0
for i in range(1, n):
    if p[i-1] > p[i]:
        diff = p[i-1] - p[i]
        total += diff
        p[i] = p[i-1]
        
print(total)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global a
    ans = 0
    i = 1
    piv = a[0]
    while i<n:
        while a[i]<piv:
            ans += piv-a[i]
            i += 1
            if n<=i:
                break
        if i<n:
            piv = a[i]
            i += 1
    print(ans)
    return

if __name__=='__main__':
    n = I()
    a = list(IL())
    solve()

Submission status: Accepted

Solution code:
#ABC177 C
N = int(input())
A = list(map(int,input().split()))
max = A[0]
F = [0]

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

print(sum(F))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


prev=A[0]
dai=0
count=0

for i in range(0,len(A)):

    # print(prev - A[i])
    if prev>A[i]:
        # print(prev-A[i])
        # print(i)
        dai=prev-A[i]
        count=count+dai
        A[i]=A[i]+dai
    prev=A[i]
# print(A)

print(count)

Submission status: Accepted

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


max_a = 0
result = 0
for i in A_list:
    
    if i >= max_a:
        max_a = i
    
    else: 
        diff = max_a - i
        result += diff

print(result)

Submission status: Accepted

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


ans = 0
naw = A[0]

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

print(ans)

Submission status: Accepted

Solution code:
n = input()
a_list = list(map(int, input().split())) 
cnt = 0
tmp = 0

for a in range(len(a_list)-1):
    if (a_list[a] > a_list[a+1]):
        cnt += a_list[a] -a_list[a+1]
        a_list[a+1] += a_list[a] -a_list[a+1]
        # print(cnt)

print(cnt)

Submission status: Accepted

Solution code:
x=int(input())
l=list(map(int,input().split(" ")))
sum=0
mx=-1000
for i in range(len(l)-1):
    mx=max(mx,l[i])
    if l[i+1]<mx:
        sum+=mx-l[i+1]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

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