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

Submission status: Accepted

Solution code:
import sys
import math

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()

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

cnt = 0

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readlines
def main():
    lines = input()

    N = int(lines[0].strip())
    A = list(map(int, lines[1].split()))

    max_ = 0
    total = 0
    for i, x in enumerate(A):
        if x <= max_:
            total += (max_ - x)
        else:
            max_ = x
    print(total)

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

Submission status: Accepted

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

for i in range(N):
    if i==0:
        B.append(0)
    else:
        if A[i-1]>A[i]:
            temp = A[i-1]-A[i]
            B.append(temp)
            A[i] += temp

ans = sum(B)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

x = 0

before_a = 0

for a in A:
    if a < before_a:
        x += (before_a - a)
    else:
        before_a = a
    
print(x)

Submission status: Accepted

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


mod = 1000000007


def combinations_count_mod(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x, y: x*y % mod, range(n, n - r, -1), 1)
    denom = pow(reduce(lambda x, y: x*y % mod, range(1, r + 1), 1), mod-2, mod)
    return numer * denom % mod


def solve():
    pass


def main():
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    max_x = a[0]
    ans = 0
    for i in range(1,n):
        ans += max(max_x - a[i], 0)
        max_x = max(max_x, a[i])
        # eprint('minx, a[i], ans ',end=':\n')
        # eprint(minx, a[i], ans)
    print(ans)


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
total = 0
cnt = x[0]
for i in range(n):
    if x[i] < cnt:
        total += cnt - x[i]
    elif x[i] > cnt:
        cnt = x[i]

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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]
    i+=1

print(step)

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
b=copy.deepcopy(a)
for i in range(1,n):
  if a[i-1] > a[i]:
    a[i] = a[i-1]
print(sum([a[i]-b[i] for i in range(n)]))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

x = A[0]
y = 0
for a in A:
    if a >= x:
        x = a
    else:
        y += x - a
        
print(y)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

mm = 0
ans = 0
for i in range(len(a)):
    mm = max(mm, a[i])
    q = mm - a[i]
    ans += q

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = map(int, input().split())
    A = list(map(int, input().split()))
    max_list = [0]*(len(A))
    max_list[0] = A[0]
    for i in range(1, len(A)):
        max_list[i] = max(max_list[i - 1], A[i])
    diff_sum = 0
    for i in range(len(A)):
        diff_sum += max_list[i] - A[i]
    return diff_sum

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solution(data):
    mh = float("-inf")
    res = 0
    for x in data:
        mh = max(mh, x)
        if mh > x:
            res += (mh - x)
    return res

n = input()
data = list(map(int, input().split()))
print(solution(data))

Submission status: Accepted

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

Submission status: Accepted

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

# from typing import Optional
# from typing import List
# from typing import Dict
# from typing import Tuple
# from typing import Sequence


def main():
    N = int(input())
    A = map(lambda x: int(x), input().split())

    currentMax = 0
    result = 0
    for item in A:
        if item > currentMax:
            currentMax = item
        else:
            result += currentMax - item

    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

    hsum = 0
    hmax = a[0]
    for i in range(len(a)-1) :
        if hmax >= a[i+1] :
            hsum += hmax - a[i+1]

        else :
            hmax = a[i+1]

    print(hsum)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
listA = [int(i) for i in input().split()]
maxH = listA[0]
ans = 0
for a in listA[1:]:
    if maxH > a:
        ans = ans + (maxH - a)
    elif maxH < a:
        maxH = a
print(ans)

Submission status: Accepted

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

step = 0
dif = 0
maxHeight = A[0]
for n in range(1, N):
    if maxHeight > A[n]:
        step += (maxHeight - A[n])
    elif maxHeight < A[n]:
        maxHeight = A[n]
    else:
        pass

print(step)

Submission status: Accepted

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

for i in range(1,len(arr)):
  cmp = max(arr[i],cmp)
  add = cmp - arr[i]
  ans = ans + add

print(ans)

Submission status: Accepted

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

print(diff)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

l = A[0]
b = 0
for i in A[1:]:
    if l > i:
        b = l - i + b
    elif l < i:
        l = i
print(b)

Submission status: Accepted

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

biggest = 0
ans = 0
A_new = []

for i in range(N):
    biggest = max(biggest, A[i])
    A_new.append(biggest)
    ans += A_new[i] - A[i]

print(ans)

Submission status: Accepted

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

max_ = 0
result = 0
for i in range(N):
    if max_ < A[i]:
        max_ = A[i]
    elif A[i] < max_:
        result += (max_ - A[i])
print(result)

Submission status: Accepted

Solution code:
N=input()
A=[int(_) for _ in input().split()]
x = 0
amax = 0
for a in A:
  if amax > a: x += (amax-a)
  else: amax = a
print(x)

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    most = A[0]
    res = 0

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

    print(res)



main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
ans = 0
highest = 0
for n in range(N):
    highest = max(highest, A[n])
    ans += max(highest - A[n], 0)

# 結果出力
print(ans)

Submission status: Accepted

Solution code:
import math

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    tmp=0
    Max=0
    for i in range(1,N):
        Max=max(Max,A[i-1])
        tmp+=max(0, Max - A[i])

    print(tmp)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
sum=0
for i in range(len(a)-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()))
c = 0
for i in range(1,n):
    if a[i-1]>a[i]:
        q = (a[i-1]-a[i])
        c += q
        a[i] = a[i]+q
print(c)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
#A.reverse()
sin = A[0]
dai = 0
ans = 0
for i in range(1,N):
    sin = max(sin,A[i])
    ans += sin-A[i]
print(ans)

Submission status: Accepted

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

def main():
    f = 0
    p = s[0]
    for ss in range(1, n):
        if s[ss] <= p:
            f += p - s[ss]
        else:
            p = s[ss]
    
    print(f)
    return
main()

Submission status: Accepted

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

sum = 0
max = 0

for i in range(n):
    if a[i] < max:
        sum += max - a[i]
    else:
        max = a[i]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

length = 0
step_sum = 0

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

print(step_sum)

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
max_val = -1
for a in A:
    if a >= max_val:
        max_val = a
    else:
        res += max_val - a

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

t = 0 ;
for idx , x in enumerate ( A ) :
	if idx==0 : continue ;

	if A[idx] < A[idx-1] :
		r = A[idx-1] - A[idx] ; 
		t += r ; 
		A[idx] += r ; 

print ( t ) ;

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

max=0
sum=0

for i in list:
    if i>max:
        max=i
    else:
        sum+=(max-i)

print(sum)

Submission status: Accepted

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

t = arr[0]

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(sum)

Submission status: Accepted

Solution code:
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    n=int(input())
    arr=inar()
    ms=arr[0]
    ans=0
    for i in range(1,n):
        if arr[i]<ms:
            ans+=ms-arr[i]
        else:
            ms=max(arr[i],ms)
    print(ans)


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threadin.Thread(target=main).start()

Submission status: Accepted

Solution code:
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:
nn = int(input())
aa = list(map(int, input().split()))

mymax = aa[0]
steps = 0
for i in range(1,nn):
    if mymax >= aa[i]:
        steps += mymax - aa[i]
    else:
        mymax = aa[i]

print(steps)

Submission status: Accepted

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

step = 0
mx = 0
for i in range(len(A)-1):
    if A[i] > mx:
        mx = A[i]
    if mx > A[i+1]:
        step += mx-A[i+1]

print(step)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(len(a)):
    if i == 0:
        height = a[i]
        continue
    if a[i] >= height:
        height = a[i]
        continue
    ans += height - a[i]
print(ans)

Submission status: Accepted

Solution code:
num_p = int(input())
pre_height = 1
step_sum = 0
li= input().split()
for i in range(num_p):
    height = int(li[i])
    if height > pre_height:
        step_sum += 0
        pre_height = height
    else:
        step_sum += pre_height - height
        pre_height += 0
        
print(step_sum)

Submission status: Accepted

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
#if l[i] == l[j]: continue
n = int(input())
a = list(map(int, input().split()))
ans = 0
num = a[0]

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

    else:
        ans += num - a[i+1]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

    else:
        dekai=a[i]

print(humidai)

Submission status: Accepted

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

ans = 0
dai = 0

for i in range(1, n):
    dai = min(0, a[i]-a[i-1])*(-1)

    if dai != 0:
        a[i] += dai
        ans += dai

print(ans)

Submission status: Accepted

Solution code:
import math

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

    count = 0
    currentHeight = A[0]

    for i in range(1, len(A)):
        if A[i] < A[i-1]:
            dai = A[i-1] - A[i]
            A[i] += dai
            count += dai

    print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
now = AList[0]
for a in AList:
  if a < now:
    ans += (now - a)
  elif a > now:
    now = a
print(ans)

Submission status: Accepted

Solution code:


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

goukei=0

bar=S[0]

for i in range(N-1):
    if S[i+1]<bar:
        goukei+=bar-S[i+1]
    elif S[i+1]>bar:
        bar=S[i+1]

print(goukei)

Submission status: Accepted

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

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

print(sam)

Submission status: Accepted

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

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:
from sys import stdin
readline = stdin.buffer.readline

def main():
    N = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    ans = 0
    h = A[0]
    for a in A:
        if a > h:
            h = a
        elif a < h:
            ans += h - a
    print(ans)

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

print(ans)

Submission status: Accepted

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

pre = A[0]
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:

import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        N = int(self.input_data())
        A = list(map(int, self.input_data().split()))

        a_len = len(A)
        max = A[0]
        result = 0
        for i in range(1, a_len):
            if A[i] < max:
                result += (max - A[i])
            else:
                max = A[i]
        print(str(result))

solver = Solver(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

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

before = 0
d = [0] * N
for i in range(N):
    if i == 0:
        before = i
        continue

    if (A[before] + d[before]) > A[i]:
        d[i] = (A[before] + d[before]) - A[i]
        before = i
    else:
        before = i

print(sum(d))

Submission status: Accepted

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

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

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]:
        continue
    else:
        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()))
highest = 0
ans = 0
for a in A:
    if a >= highest:
        highest = a
    else:
        ans += highest - a
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:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    As = list(map(int, input().split()))

    s = 0
    max_n = As[0]
    for i in range(len(As)):
        if i == 0: continue
        if max_n > As[i]:
            s += max_n - As[i]
        elif max_n < As[i]:
            max_n = As[i]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, n):
    step[i] = max(0, a[i-1] + step[i-1] - a[i])
print(str(sum(step)))

Submission status: Accepted

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

daisum = 0
minh = 0
for i in a:
  dai = 0
  if i < minh:
    dai = minh - i
    daisum += dai
  elif i > minh:
    minh = i

print(daisum)

Submission status: Accepted

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

    current = A[0]
    answer = 0
    for i in range(N):
        if A[i] < current:
            answer += current - A[i]
        elif A[i] > current:
            current = A[i]

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding: utf-8
from collections import defaultdict
import sys

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

totalStep = 0

prev = 0

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

    totalStep += step
#    print(a, step, totalStep)
    prev = a
    totalStep

print(totalStep)

Submission status: Accepted

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