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()))
MAX = 0
ans = 0
for i in range(N):
  ans += max(0,MAX-A[i])
  MAX = max(MAX,A[i])
print(ans)

Submission status: Accepted

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

N = int(input().split()[0])
a_list = list(map(int, input().split()))
before_a = 0
total = 0
for i, a in enumerate(a_list):
    if i == 0:
        before_a = a
        continue
    total += max(before_a - a, 0)
    before_a = a + max(before_a - a, 0)
ans = total
print(ans)

Submission status: Accepted

Solution code:
# C - Step
import numpy as np

N = int(input())
A = np.array([int(a) for a in input().split()])
Amax = np.maximum.accumulate(A)

print(sum(Amax-A))

Submission status: Accepted

Solution code:
# C - Step

N = int(input())
A = list(int(a) for a in input().split())

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().strip().split()))
befmax=0
sumlong=0
for i in range(0,n):
    if(befmax>s[i]):
        sumlong += befmax-s[i]
    else:
        befmax=s[i]

print(sumlong)

Submission status: Accepted

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

Submission status: Accepted

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

    current = a_list[0]
    answer = 0
    for a in a_list:
        if a < current:
            answer += current - a
        current = max(a, current)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data)

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()
xs = Is()

ans = 0
now = xs[0]
for x in xs[1:]:
    if x < now:
        ans += (now - x)
    now = max(x, now)

print(ans)

Submission status: Accepted

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

m = l[0]
for i in l:
    if m <=i:
        m = i
    else:
        ans +=m-i
print(ans)

Submission status: Accepted

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

result = 0
tmp = a[0]

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

print(result)

Submission status: Accepted

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

acm = 0
for i in range(1,n):
    acm += max(-arr[i]+arr[i-1],0)
    arr[i] = max(arr[i],arr[i-1])
    
print(acm)

Submission status: Accepted

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

step = 0
highest = 0
for a in A:
  if a >= highest:
    highest = a
  else:
    step += highest-a
    
print(step)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [0] * N # 踏み台の高さ
ans = 0
h = 0
for i in range(1,N):
    if A[i] < A[i-1]+B[i-1]:
        h = B[i-1]+A[i-1] - A[i]
    else:
        h = 0
    B[i] = h

for i in range(N):
    ans += B[i]

# print(B)
print(ans)
# 一個前しか見てない

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
smallest = A[0]
ans = 0
for x in range(len(A)-1):
	if A[x] > A[x+1]:
		ans += A[x] - A[x+1] 
		A[x+1] = A[x]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m = A[0]
for a in A:
    if m <= a: m = a
    else: ans += m-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:
# ===============================================================================================
# importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb  # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
# If the element is already present in the list,

# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect

# If the element is already present in the list,
# the right most position where element has to be inserted is returned

# ==============================================================================================
# fast I/O 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")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

# ===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType


def iterative(f, stack=[]):
    def wrapped_func(*args, **kwargs):
        if stack: return f(*args, **kwargs)
        to = f(*args, **kwargs)
        while True:
            if type(to) is GeneratorType:
                stack.append(to)
                to = next(to)
                continue
            stack.pop()
            if not stack: break
            to = stack[-1].send(to)
        return to

    return wrapped_func


#### END ITERATE RECURSION ####

# ===============================================================================================
# some shortcuts

mod = 1000000007


def inp(): return sys.stdin.readline().rstrip("\r\n")  # for fast input

def out(var): sys.stdout.write(str(var))  # for fast output, always take string

def lis(): return list(map(int, inp().split()))

def stringlis(): return list(map(str, inp().split()))

def sep(): return map(int, inp().split())

def strsep(): return map(str, inp().split())


def zerolist(n): return [0] * n

def nextline(): out("\n")  # as stdout.write always print sring.

def testcase(t):
    for p in range(t):
        solve()

def printlist(a):
    for p in range(0, len(a)):
        out(str(a[p]) + ' ')


def solve():
    m=0
    n=int(inp())
    ar=lis()
    ans=0
    for i in range(n):
        if ar[i]<m:
            ans+=(m-ar[i])
        else:
            m=ar[i]
    print(ans)

    
    
    
solve()

Submission status: Accepted

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

ans = []
maxim = A[0]
for i in range(1,n):
    if A[i] < maxim:
        ans.append(maxim-A[i])
    else:
        ans.append(0)
        maxim = A[i]
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-1):
    if a[i]>a[i+1]:
        takasa+=a[i]-a[i+1]
        a[i+1]+=a[i]-a[i+1]
print(takasa)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split(" ")]
s = [0]

for x in range(1, n):
    stool = max(a[x - 1] - a[x], 0)
    s.append(stool)
    a[x] += stool
    
print(sum(s))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s = 0

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

print(s)

Submission status: Accepted

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

sum = 0

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

Submission status: Accepted

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

print(s)

Submission status: Accepted

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

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    CNT = 0
    for i in range(1,N):
        if A[i] <= A[i-1]:
            CNT += A[i-1] - A[i]
            A[i] += A[i-1] - A[i]
    print(CNT)
        

if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

print(sum_value)

Submission status: Accepted

Solution code:
##C - Step(TLE)
N = int(input())
A = list(map(int,input().split()))
ans = 0
maxi = 0
for i in A:
    ans += max(0,maxi-i)
    maxi = max(maxi,i)
print(ans)

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

    ans=0
    maximum=0
    
    for a in A:
        ans += max(0,maximum-a)
        maximum=max(maximum,a)

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

Submission status: Accepted

Solution code:
import sys


def resolve(in_):
    N = int(next(in_))
    A = tuple(map(int, next(in_).split()))

    stool = 0
    height = A[0]
    for h in A[1:]:
        if height <= h:
            height = h
        else:
            stool += height - h

    return stool

def main():
    answer = resolve(sys.stdin)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
number = int(input())
tall = list(map(int, input().split()))
addition = 0
for each in range(1, number):
    difference = tall[each - 1] - tall[each]
    if difference >= 0:
        addition += difference
        tall[each] += difference
print(addition)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
for idx in range(1, n):
    if nums[idx] - nums[idx-1] < 0:
        ans += abs(nums[idx] - nums[idx-1])
        nums[idx] = nums[idx-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = 0
for i in range(n-1):

    if a[i] > a[i+1]:
        c += a[i]-a[i+1]
        a[i+1] = a[i]

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

for count_a in range(len(A)):
    fumidai = 0

    if(A[count_a] >= A_after):
        A_after = A[count_a]
        fumidai_list.append(fumidai)

    elif(A[count_a] < A_after):
        fumidai = A_after - A[count_a]
        fumidai_list.append(fumidai)

print(sum(fumidai_list))

Submission status: Accepted

Solution code:

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

x = 0
for a in range(n-1):
    if l[a]>l[a+1] :
        x=l[a] - l[a+1] + x
        l[a+1]=l[a]
    #print("l[a],x,l[a+1]=", l[a], x, l[a+1])
print(x)

Submission status: Accepted

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

count = 0
current_max = nums[0]
for i in range(len(nums)-1):
    nv = nums[i+1]
    if nv > current_max:
        current_max = nv
    else:
        count += (current_max - nv)

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class height():

    def __init__(self):
        self.human = 0
        self.step = 0

    def Sum(self):
        return self.human + self.step
N = int(input())
A = [height() for i in range(N)]

s = str(input()).split()

for i,v in enumerate(s):
    A[i].human = int(v)
    # print(i,v)

sum, tmp = 0,0
for i in range(len(A)-1):
    if A[i+1].human >= A[i].Sum():
        continue
    tmp = A[i].Sum() - A[i+1].human
    A[i+1].step = tmp
    sum += tmp

print(sum)
# for a in A:
#     print("human",a.human)

Submission status: Accepted

Solution code:
N = int(input())
height = [int(i) for i in input().split()]
step = 0
for i, h in enumerate(height):
    if i == 0:
        pre = h
    else:
        if h < pre:
            step += (pre - h)
            pre = pre
        else:
            step += 0
            pre = h
print(step)

Submission status: Accepted

Solution code:

n = int(input())
l = list(map(int,input().split()))
height_stool = 0

temp = [i for i in l]
for i in range(n-1):
    if l[i] > l[i+1]:
        diff = l[i]-l[i+1]
        height_stool += diff
        l[i+1] += diff
print(height_stool)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().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]-A[i]
print(F)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ma = A[0]
    ans = 0
    for a in A:
        ma = max(ma, a)
        ans += ma - a
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

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

print(ans)

Submission status: Accepted

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

max_num = a[0]
ans = 0
for i in range(1,n):
    max_num = max(max_num, a[i])
    if max_num != a[i]:
        ans += (max_num - a[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
array=[int(x) for x in input().split()]
diff=0
for i in range(1,N):
	if array[i]<array[i-1]:
		diff+=array[i-1]-array[i]
		array[i]+=array[i-1]-array[i]
print(diff)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
a=t=0
for i in l:
  if i>t: t=i
  a+=t-i
print(a)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
a=t=0
for i in l:
  if i>t: t=i
  else: a+=t-i
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
cur_max=0

for x in a:
  if cur_max > x:
    ans += cur_max - x
  cur_max = max(cur_max, x)
print(ans)

Submission status: Accepted

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

print(answer)

Submission status: Accepted

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

min_ = A[0]

for i in range(n):
    if A[i] > min_:
        min_ = A[i]
    else:
        total += (min_ - A[i])
print(total)

Submission status: Accepted

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

c = 0
p = 0
for a in A:
  if p < a:
    p = a
  else:
    c += p - a
    p = max(p, a)
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

height = 0
base = 0
for j in range(0, n - 1):
    if a_int[j] + base > a_int[j + 1] + base:
        base = a_int[j] - a_int[j + 1]
        height += base
        a_int[j + 1] += base
        base = 0
print(height)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,n):
    if height>alist[i]:
        ans+=height-alist[i]
    
    else:
        height=alist[i]

print(ans)

Submission status: Accepted

Solution code:
def solve(n,arr):
    maxh = 0
    h = 0
    for i in range(1,n):
        curr = arr[i]
        curh = arr[i-1]
        f = max(curr,curh)
        maxh = max(f,maxh)

        if curr < maxh:
            h += maxh - curr

        # print(f"curr:{curr},curh:{curh},maxh:{maxh}")
        # print(f"h:{h}")

    return h
        



n = int(input())
arr = list(map(int, input().strip().split()))

print(solve(n,arr))

Submission status: Accepted

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

std=heights.pop(0)
ans=0
if n==1:
  ans=0
else:
  for height in heights:
    if std>height:
      ans+=std-height
    else:
      std=height
print(ans)

Submission status: Accepted

Solution code:
from typing import List

def step(n: int, a: List[int])-> int:
  result = 0
  dai = 0
  for i in range(1,n):
    sub = a[i-1] + dai - a[i]
    if sub > 0:
      dai = sub      
      result += sub
      continue
    dai = 0
  return result

if __name__ == "__main__":
  n = int(input())
  a = list(map(int, input().split()))
  print(step(n, a))

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

print(step)

Submission status: Accepted

Solution code:
n=int(input())

A = list(map(int, input().split()))
l = [0] * len(A)

ans=0
m=A[0]

for i in A:
  if m>i:
    ans = ans +(m-i)
  
  else:
    m=i
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

A = list(map(int, input().split()))
l = [0] * len(A)

ans=0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = 0
height = 0
for a in A:
    height = max(height, a)
    s += height - a
print(s)

Submission status: Accepted

Solution code:
n = int(input())
an = [int(num) for num in input().split()]

sum = 0
for i in range(len(an)-1):
  if an[i] > an[i+1]:
    sum += an[i] - an[i+1]
    an[i+1] += an[i] - an[i+1]
    
print(sum)

Submission status: Accepted

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

rate = 0
count = 0
for i, v in enumerate(s):
    if i == 0:
        rate = v
        continue

    if rate > v:
        tmp = rate - v
        count += tmp
    else:
        rate = v

print(count)

Submission status: Accepted

Solution code:
import sys
import math

lines = sys.stdin.readlines()
# print(lines)
n = int(lines[0].rstrip())
heights = [int(x) for x in lines[1].rstrip().split()]
# print(heights)
tot = 0
for i in range(n-1):
    if heights[i] <= heights[i+1]:
        continue
    else:
        diff = heights[i] - heights[i+1]
        heights[i+1] += diff
        tot += diff

print(tot)

Submission status: Accepted

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

for i in range(1,n):
    if num_list[i] < num_list[i-1]:
        count += (num_list[i-1] - num_list[i])
        num_list[i] = num_list[i-1]

print(count)

Submission status: Accepted

Solution code:
num = input()
list = input().split()

ans = 0

for i in range(int(num) - 1):
    n1 = int(list[i])
    n2 = int(list[i+1])

    if  n1 > n2 :
        ans += (n1 - n2)
        list[i+1] = list[i]
    else:
        ans += 0

print(ans)

Submission status: Accepted

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

S = 0
a = A[0]
for i in range(len(A)-1):
    if a > A[i+1]:
        S += a-A[i+1]
    else:
        a = A[i+1]
print(S)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
arr = map(int, input().split())
tmp = 0
ans = 0
for item in arr:
    if item < tmp:
        ans += tmp - item
    else:
        tmp = item
print(ans)

Submission status: Accepted

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

ans = 0
mx = arr[0]

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[int(x) for x in input().split()]

ans=0
for i in range(N-1):
    if s[i]>s[i+1]:
        ans+=(s[i]-s[i+1])
        s[i+1]=s[i+1]+(s[i]-s[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()))

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

print(sum)

Submission status: Accepted

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

#print(int_n, list_a)

int_tmp_highet = 0
int_count = 0
for i in list_a:
  if i > int_tmp_highet:
    int_tmp_highet = i
  else:
    int_count = int_count + int_tmp_highet - i
  #print(int_count)

print(int_count)

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


m = a[0]
f = 0
for i in a[1:]:
    if i < m:
        f += m-i
    else:
        m = i

print(f)

Submission status: Accepted

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

max = A[0]
cnt = 0

for i in range(N):
  if A[i]>max:
    max = A[i]
  elif A[i] == max:
    continue
  else:
    cnt += max - A[i]

print(cnt)