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:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    A = read_ints()
    front = 0
    total = 0
    for a in A:
        if a < front:
            total += front-a
        else:
            front = a
    return total


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

Submission status: Accepted

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

h,res=A[0],0
for i in A:
    if i<=h:
        res+=(h-i)
    else:
        h=i
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    a = LI()
    ans = 0
    for i in range(1, N):
        if a[i] >= a[i - 1]:
            continue
        ans += a[i - 1] - a[i]
        a[i] = a[i - 1]

    print(ans)


main()

Submission status: Accepted

Solution code:
from math import ceil
from collections import defaultdict as dd
import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
mx=0
ans=0
for i in l:
    if(i<mx):
        ans+=(mx-i)
        
    else:
       mx=i
print(ans)

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

s = 0

for i in range(1, N):
    s += - min(0, A[i] - A[i - 1])
    A[i] += - min(0, A[i] - A[i - 1])

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-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()))
pre = 0
ans = 0
for ai in a:
    if ai < pre:
        ans += pre - ai
    else:
        pre = ai
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N == 1:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
ans=0
now=A[0]
i=1
while i<N:
    if A[i]>now:
        now=A[i]
    else:
        ans+=now-A[i]
    i+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A= list(map(int,input().split()))
a = A[0]
ans = 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:
import math
n = int(input())
alist = list(map(int,input().split()))

maxmin = -math.inf

board = 0
for i in range(n):
    a = alist[i]

    if a > maxmin:
        maxmin = a
    else:
        board += maxmin - a

print(board)

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
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)
INF = 10**9
mod = 10**9+7

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math
import sys

n = int(input())
alist = list(map(int, input().split()))
num = 0
ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
now = A[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:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()
A = getList()
now = A[0]
ans = 0
for i in range(1, N):
    if now <= A[i]:
        now = A[i]
    ans += now - A[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

max = An[0]
count = 0

for i in range(n):
    if An[i]<max:
        count += max - An[i]
    else:
        max = An[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# by the authority of GOD    author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase

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

#Fast IO Region
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()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
check = A[0]
for i in range(1,N):
    if A[i] < check:
        ans += check-A[i]
        A[i] =check
    check = 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:
"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from collections import defaultdict as dd, deque, Counter as C
from bisect import bisect_left as bl, bisect_right as br, bisect
from heapq import heapify, heappop, heappush
from math import ceil, log, floor, sqrt
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


n = int(input())
arr = L()
answer = 0
for i in range(1, n):
    if arr[i] < arr[i-1]:
        answer += arr[i-1]-arr[i]
        arr[i] = arr[i-1]
out(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(T) for T in input().split()]
Step = 0
for TN in range(1,N):
    if A[TN-1]>A[TN]:
        Step += A[TN-1]-A[TN]
        A[TN] = A[TN-1]
print(Step)

Submission status: Accepted

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

res = 0
m = 0
for a in arr:
    m = max(m, a)
    res += max(m-a, 0)

print(res)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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())
arr = [int(j) for j in input().split()]

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

print(ans)

Submission status: Accepted

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

lmax = a[0]
ans = 0
for i in a:
    ans += max(0, lmax-i)
    lmax = max(lmax, i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

before = 0
sum = 0
for a in a_array:
    if before > a:
        sum += before - a
    else:
        before = a

print(sum)

Submission status: Accepted

Solution code:
import sys
import math
# sys.stdin  = open("input.txt")
n=int(input())
a=[int(o) for o in input().split()]
s=0
for i in range(n-1):
    if a[i+1]<a[i]:
        s+=(a[i]-a[i+1])
        a[i+1]=a[i]
print(s)

Submission status: Accepted

Solution code:
N = int(input())

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

sum = 0
tallest = 0
for t in l:
    if t < tallest:
        sum += tallest - t
    else:
        tallest = t
print(sum)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
tmp = a[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 = int(input())
As = list(map(int, input().split()))
mx = 0
ans = 0
for a in As:
    if mx < a:
        mx = a
    else:
        ans += mx - a
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
n = int(input())
#n, x, t = map(int, sys.stdin.readline().split())
#n = input()
a = list(map(int, sys.stdin.readline().split()))

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

ans = 0
current_max = 0
for ai in a:
    current_max = max(current_max, ai)
    ans += current_max - ai

print(ans)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    h = A[0]
    ans = 0
    for a in A:
        h = max(h, a)
        ans += h - a
    return ans

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(N, A))

def test():
    import doctest
    doctest.testmod()

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in 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
input = lambda: sys.stdin.readline().rstrip()

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

ans = 0
minstep = a[0]

for i in a[1:]:
    if i < minstep:
        ans += minstep - i
    else:
        minstep = i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

prev = p[0]
ans = 0
for p in p[1:]:
    diff = max(prev - p, 0)
    # print(diff)
    ans += diff
    prev = p + diff

print(ans)

Submission status: Accepted

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

pre_max = A_list[0]
res = 0
for a in A_list:
    if a > pre_max:
        pre_max = a
    else:
        res += pre_max - a

print(res)

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

pre = A[0]

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

print(res)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    ret, max_h = 0, A[0]
    for n in range(N):
        if max_h > A[n]:
            ret += max_h - A[n]
            max_h = A[n] + (max_h - A[n])
        else:
            max_h = max(max_h, A[n])
    print(ret)


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sm)

Submission status: Accepted

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

curmax = 0
ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import pprint
import fractions
import collections
import itertools
from decimal import *
from collections import deque
from bisect import *
N=int(input())
A=list(map(int,input().split()))
cnt=0
for i in range(1,N):
    if A[i-1]>A[i]:
        cnt=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()))
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()))

ans = 0
l = A[0]

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

Submission status: Accepted

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

c = 0
for i in range(1, N):
    d = A[i] - A[i - 1]

    if d >= 0:
        continue
    else:
        c += abs(d)
        A[i] = A[i - 1]

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=0
_=A[0]
for a in A:
  if _<a:
    _=a
  else:
    ans+=(_-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:
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    n = ri()
    A = ril()
    h = 0
    ans = 0
    for a in A:
        dh = max(h-a, 0)
        h = a + dh
        ans += dh
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
height_need = 0

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

mx = None
ans = 0
for a in As:
    if mx is None:
        mx = a
    else:
        if a >= mx:
            mx = a
        else:
            ans += mx - a
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, -1, mod)


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

main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

p = a[0]
ans = 0
for v in a:
    ans += max(0, p - v)
    p = max(p, v)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def _i(): return int(sys.stdin.readline().strip())


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    n = _i()
    cur, *a = _ia()
    ans = 0
    for ai in a:
        if ai < cur:
            ans += (cur - ai)
        else:
            cur = ai
    return ans


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#create date: 2020-08-22 21:03

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    m = 0
    ans = 0
    for ai in a:
        ans += max(0, m - ai)
        m = max(m, ai)
        #print(m, ans)
    print(ans)

if __name__ == "__main__":
    main()