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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
last = 0
for a in A:
    res += max(last,a)-a
    last = max(last,a)

print(res)

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
mx=0
ans=0
for i in s:
    ans+=max(0,mx-i)
    mx=max(mx,i)
print(ans)

Submission status: Accepted

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

ans = 0
prev = 0
for a in al:
    if prev <= a:
        prev = a
    else:
        rem = prev-a
        ans += rem

print(ans)

Submission status: Accepted

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

ans = 0

re = A[0]

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

print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    count = 0
    for i in range(1,N):
        if A[i]<A[i-1]:
            count += A[i-1] - A[i]
            A[i] = A[i-1]
    print(count)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    ma = 0
    for a in A:
        ans += max(0, ma-a)
        ma = max(ma, a)
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

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

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

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


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y
from math import ceil

for _ in range(int(input()) if not True else 1):
    n = int(input())
    a = list(map(int, input().split()))
    cur = a[0]
    ans=0
    for i in range(1, n):
        if a[i] >= cur:
            cur = a[i]
            continue
        ans += cur - a[i]
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
a = [0] + a
c = 0
for i in range(n):
    t = a[i] - a[i + 1]
    if t > 0:
        a[i + 1] += t
        c += t
print(c)

Submission status: Accepted

Solution code:
import sys
from math import ceil
def input(): return sys.stdin.readline().strip()

def main():
    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)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()

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

print(ans)

Submission status: Accepted

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

ans = 0
m = 0
for i in a:
    ans += max(0,m-i)
    m = max(m,i)
print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    m = A[0]
    ans = 0
    for a in A[1:]:
        if a < m:
            ans += m - a
        if m < a:
            m = a

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

res = 0
cur = X[0]
for v in X:
    res += max(0, cur - v)
    cur = max(cur, v)

print(res)

Submission status: Accepted

Solution code:
N = int(input())
pre = 0
ans = 0
for a in input().split():
    a = int(a)
    ans += max(pre-a, 0)
    pre = max(pre, a)
print(ans)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

summ=0
before=height[0]

for i in range(N-1):
    diff=height[i+1]-before

    if(diff<0):
        summ+=abs(diff)
    else:
        before=height[i+1]

print(summ)

Submission status: Accepted

Solution code:
input()
ans = 0
mx = 0
for x in map(int, input().split()):
    if x < mx:
        ans += mx - x
    else:
        mx = x
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())
L=list(map(int,input().split()))
if N==1:
  print(0)
  exit()
ans=0
for i in range(1,N):
  if L[i-1]>L[i]:
    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):
    dif = a[i] - a[i-1]
    if dif<0:
        a[i] -= dif
        ans -= dif

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cur = 0
s = 0
for A in As:
    if cur > A:
        s += (cur - A)
    elif cur < A:
        cur = A

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
m = h[0]

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

ans = 0
cur = A[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

# import math
# from decimal import Decimal
# from collections import deque, Counter
# from itertools import product


def LI():
    return list(map(int,input().split()))
    
def OPEN():
    return map(int,open(0).read().split())
    

n,*a=OPEN()

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

Submission status: Accepted

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

ans = 0
max_ = 0
for i in range(n):
    ans += max(max_ - a[i], 0)
    max_ = max(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(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()))
now =A[0]
res = 0
for i in range(1,N):
    if now>=A[i]:
        res +=now-A[i]
    elif now<A[i]:
        now = A[i]
print(res)

Submission status: Accepted

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

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

print(ret)

Submission status: Accepted

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

ans = 0
M = A[0]
for a in A[1:]:
    if M > a:
        ans += M - a
    else:
        M = a
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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 a>=prev:
        prev=a
    else:
        ans+=prev-a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
mx = 0
res = 0
for i in l:
    if i < mx:
        res += mx-i
    mx = max(i,mx)
print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(N - 1):
  if a[i] > a[i + 1]:
    res += a[i] - a[i + 1]
    a[i + 1] = a[i]
print(res)

Submission status: Accepted

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

tmp = 0
ans = 0
for i in range(n):
    if a[i] < tmp:
        ans += (tmp - a[i])
    tmp = max(tmp, a[i])

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
mx = 0
res = 0
for i in l:
    if i < mx:
        res += mx-i
    mx = max(i,mx)
    # print(res, mx)

print(res)

Submission status: Accepted

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

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
def input(): return sys.stdin.readline().rstrip()


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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    ma = 0
    ans = 0
    for i in a:
        if i > ma:
            ma = i
        else:
            ans += ma-i

    print(ans) 

    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
bef_max = 0
ans = 0
for a in alst:
    ans += max(0, bef_max - a)
    bef_max = max(bef_max, a)
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    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)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n = ni()
a = na()

m = 0
ans = 0
for v in a:
    m = max(m, v)
    ans += m - v
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
A = LI()


def main():
    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)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
ans=0
tmp=A[0]
for x in A[1:]:
    if x<tmp:
        ans+=tmp-x
    tmp=max(tmp,x)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mx = 0
res = 0
for i in a:
    res += max(0, mx - i)
    mx = max(mx, i)
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mx=0
s=0
for i in a:
  if mx<i:
    mx=i
  s+=mx-i
print(s)

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

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    ans=0
    ceil=A[0]
    for i in range(N):
        if A[i]>ceil:
            ceil=A[i]
        else:
            ans+=ceil-A[i]
            
    print(ans)

main()

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
a=0
s=[]
for i in A:
  if i<a:
    s.append(a-i)
    print(s)
  else:
    a=i
print(sum(s))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
cnt = 0
for i in A:
    if i == 0:
        pass
    if A[i-1] > A[i]:
        cnt += 1
    else:
        pass
print(cnt)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG

int main() {
    int N; cin >> N;
    vector<int> height(N);
    for (int i = 0; i < N; i++) cin >> height[i];

    int ma = 0, su  = 0;
    for (int i = 0; i < N; i++) {
        if (height[i] > ma) ma = height[i];
        su += ma - height[i];
    }
    cout << su << endl;
}

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
ans=0
N=int(input())
times=N-1
A=int(input())
for i in range(times):
  AA=int(input())
  if A>AA:
    s=A-AA
    ans+=s
    AA+=s
    A=AA
  else:
    A=AA
print(ans)

Submission status: Runtime Error

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

for i in A:
    if A[con] > A[con+1]:
        A[con+1] =  A[con]
        
    if A[con+1] == A[-1]:
        break

    con += 1

ans = sum(A) - raw
print(ans)

Submission status: Runtime Error

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

for i in A:
    if A[con] > A[con+1]:
        A[con+1] =  A[con]
        print(A)
        
    if A[con+1] == A[-1]:
        break

    con += 1

ans = sum(A) - raw
print(ans)

Submission status: Runtime Error

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

for i in A:
    if A[con] > A[con+1]:
        A[con+1] =  A[con]
        
    if A[con+1] == A[-1]:
        break

    con += 1

print(A)
ans = sum(A) - raw
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
res = 0
temp_max = 0
for x in a:
    if temp_max > x:
        res += temp_max - x
    temp_max = max(temp_max,x)

print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
res = 0
for i in range(n-1):
    res += max(0,max(a[:-(i+1)])-a[-(i+1)])
print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
MAX = max(a)
ans = 0
maxflg = False
minflg = False
for i in range(n):
    if a[i] == MAX:
        maxflg = True
        if i == 0:
            index = i + 1
        leftMAX = max(a[:index])
    if maxflg:
        ans += MAX - a[i]

for i in range(n):
    if a[i] == MAX:
        break
    else:
        ans += leftMAX - a[i] 
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        int sl=0;
        Scanner sc=new Scanner(System.in);
        int n= sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
        {
            a[i]=sc.nextInt();
        }
        for(int j=0;j<n-1;j++)
        {
            if(a[j]>=a[j+1])
            {
                sl=sl+(a[j]-a[j+1]);
                a[j+1]=a[j+1]+(a[j]-a[j+1]);
            }
            
        }
        System.out.println(sl);
    }
}

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list[int(input().split()) for i in range(N)]
dif = 0
sum = 0

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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


if A[1] - A[0] >= 0:
    ans = 0
else:
    ans = A[0] - A[1]
y = 0

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

print(ans)

Submission status: Runtime Error

Solution code:
input()
tmp = [for i in input().split(" ")]
ans  = 0
for i in range(1,len(tmp)):
  x = tmp[i-1] - tmp[i]
  if x>0:
  ans += x
print(ans)