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()))
num=0
ans=0

for i in a:
  if num<i:
    num=i
    
  ans+=abs(num-i)
  
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

maxi = 0
ans = 0
for a in As:
    if a<maxi:
        ans += maxi-a
    maxi = max(a, maxi)

print(ans)

Submission status: Accepted

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

sm=0
mx=0
for i in a:
  sm+=max(mx-i,0)
  mx=max(i,mx)
  
print(sm)

Submission status: Accepted

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

hmax = Alist[0]
ans = 0
for n in range(1, N):
    if Alist[n] < hmax:
        ans += hmax - Alist[n]
    else:
        hmax = Alist[n]

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

Submission status: Accepted

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

for i in range(N):
  if a<A[i]:
    a = A[i]
  else:
    res += a-A[i]
    
print(res)

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()))
ans = 0
_a = A[0]
for a in A[1:]:
    tmp = 0
    #print(a, _a, _a - a)
    if a - _a < 0:
        tmp = _a - a
    ans += tmp
    _a = a + tmp
print(ans)

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

Submission status: Accepted

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

ans = 0
m = 0
for Ai in A:
    if m <= Ai:
        m = Ai
    else:
        ans += m - Ai
print(ans)

Submission status: Accepted

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

ans = 0
maximum = 0
for num in nums:
    maximum = max(maximum, num)
    ans += maximum - num

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

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

ans = 0
prev = 0
for a in A:
    if prev <= a:
        prev = a
        continue
    ans += prev - a
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys, math, bisect


if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    N = int( input().strip() )
    Heights = list( map(int, input().strip().split() ) )

    sum = 0
    for i in range(N-1):
        if(Heights[i+1] < Heights[i]):
            plus = Heights[i] - Heights[i+1]
            sum += plus
            Heights[i+1] += plus

    print(sum)

Submission status: Accepted

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

    ma = 0
    ans = 0
    for h in A:
        if h < ma:
            ans += ma - h
        else:
            ma = h

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

val = A[0]
cnt = 0
for a in A:
    if val < a:
        val = a
    else:
        cnt += val - a
print(cnt)

Submission status: Accepted

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

now=a[0]
ans=0
for h in a:
    if now > h:
        ans+=(now-h)
    now=max(now,h)
print(ans)

Submission status: Accepted

Solution code:

n = int(input())
A = list(int(x) for x in input().split())

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    ans = 0
    mx = a[0]

    for x in a[1:]:
        diff = mx - x
        if diff > 0:
            ans += diff
        mx = max(mx, x)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq
import math

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

    res = 0
    max_a = A[0]
    for a in A[1:]:
        if max_a > a:
            res += (max_a - a)
        else:
            max_a = a

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
n,*aa = map(int, open(0).read().split())
aa = np.array(aa)
bb = np.maximum.accumulate(aa)

print((bb-aa).sum())

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

max_height = 0
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:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("## ",*x,file=sys.stderr) if LOCAL else 0
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    tot=0
    cur=0
    for a in A:
        if cur>a:
            tot+=cur-a
        else:
            cur=a
    print(tot)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

h = A[0]
ans = 0
for a in A[1:]:
  ans += max(0, h-a)
  h = max(h, a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    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)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial,sqrt
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    n=I()
    lis=LI()
    ans=0
    for i in range(1,n):
        if lis[i-1]>lis[i]:
            ans+=lis[i-1]-lis[i]
            lis[i]=lis[i-1]
    print(ans)
        
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

now = a[0]
point = 0

for i in a[1:]:
    if i < now:
        point += now-i
    else:
        now = i

print(point)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=input()
*A, = map(int,input().split())
ans = 0
h = A[0]
for a in A:
    ans += max(0,h-a)
    h = max(h,a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

def solve():
    readline = sys.stdin.readline

    N = int(readline())
    A = list(map(int, readline().split()))
    
    ans = 0

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

solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    A = in_nl()

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

maxA = 0
ans = 0
for a in As:
    if a < maxA:
        ans += maxA - a
    else:
        maxA = a
print(ans)

Submission status: Accepted

Solution code:
def cin():  return list(map(int,input().split()))

N = cin()[0]
A = cin()

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

Submission status: Accepted

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = 'ONLINE_JUDGE' not in sys.argv

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    N=ii()
    A=lmis()
    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)









main()

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

n = k()
a = l()
max_h = a[0]

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

print(cnt)

Submission status: Accepted

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

cur=a[0]

ans=0

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
N = int(input())
*A, = 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:
# -*- coding: utf-8 -*-

N = int(input())
A = [0] + list(map(int, input().split()))

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

print(answer)

Submission status: Accepted

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

tmpmax, ans = A[0], 0
for i in A[1:]:
    if i > tmpmax:
        tmpmax = i
    elif i < tmpmax:
        ans += tmpmax - i

print(ans)

Submission status: Accepted

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

maxer = a[0]
sumer = 0
for i in a:
    if i <= maxer:
        sumer += maxer - i
    else:
        maxer = i

print(sumer)

Submission status: Accepted

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

ans = 0
max_num = a[0]
for ai in a[1:]:
    if(max_num < ai ):
        max_num = ai
    else:
        ans += max_num -ai
print(ans)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

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

Submission status: Accepted

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

Submission status: Accepted

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

m=0
K=0
for i in range(N):
    if i==0:
        m=A[i]
    else:
        S=max(0,m-A[i])
        K+=S
        m=A[i]+S
print(K)

Submission status: Accepted

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    n = int(stdin.readline().rstrip())
    A = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    now = 0
    ans = 0
    for i in range(n):
        if now <= A[i]:
            now = A[i]
        else:
            ans += abs(now-A[i])
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
ans = 0
for i in range(1,n):
    if A[i-1] > A[i]:
        sa = A[i-1] - A[i]
        ans += sa
        A[i] += sa
print(ans)

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = LMIIS()
    max_a = 0
    ans = 0
    for a in A:
        max_a = max(max_a,a)
        ans += max_a - a
    print(ans)


    
main()

Submission status: Accepted

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

import sys
input=sys.stdin.readline

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

Submission status: Accepted

Solution code:
def resolve():
    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)
resolve()

Submission status: Accepted

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

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

n = int(input())
a = lmp()
u = []
now= -1
for i in range(n):
    if now < a[i]:
        u.append(a[i])
        now = a[i]
    else:
        u.append(now)
ans = 0
for i in range(n):
    ans += u[i] -a[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math, re
from functools import lru_cache
from collections import defaultdict
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N = ii()
    A = list(mi())

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

    print(ans)

if __name__ == '__main__':
    main()