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

def main(n,a):
    sum=0
    h=a[0]
    for i in range(1,n):
        if h>a[i]:
            sum+=h-a[i]
        else:
            h=a[i]
    return sum

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
la = [int(w) for w in input().split()]

ans = 0
h = la[0]

for a in la[1:]:
    if a <= h:
        ans += h-a
    else:
        h = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

M=list(accumulate(L,max))
ans = 0
for i in range(n):
    ans+=abs(M[i]-L[i])
print(ans)

Submission status: Accepted

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

s = 0
f = ass[0]
for a in ass:
  if f > a:
    s += f-a
  else:
    f = a

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
m = 0
ans = 0
for i in range(len(l)):
    if m < l[i]:
        m = l[i]
    else:
        ans += m-l[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

summ=0
mini=0
for i in range(n):
    if a[i]>=mini:
        mini=a[i]
    else:
        summ += mini-a[i]
print(summ)

Submission status: Accepted

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

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
now = As[0]
res = 0
for i in As:
  if i < now:
    res += now - i
  else:
    now = i
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]:
        ans += a[i] - a[i+1]
        a[i+1] = a[i]
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input=sys.stdin.readline
##import numpy as np
#sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
##def power(x, y):
##    if(y == 0):return 1
##    temp = power(x, int(y / 2))%mo
##    if (y % 2 == 0):return (temp * temp)%mo 
##    else:
##        if(y > 0):return (x * temp * temp)%mo 
##        else:return ((temp * temp)//x )%mo
##
for _ in range(1):
    n=inin()
    l=ain()
    ans=0
    curr=l[0]
    for i in range(1,n):
        if(l[i]<curr):
            ans+=curr-l[i]
        curr=max(curr,l[i])
    print(ans)

Submission status: Accepted

Solution code:
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N = int(input())
A = input().split()

nowTall = int(A[0])
ans = 0

for i in range(N):
    if i == 0:
        continue

    if int(A[i]) < nowTall:
        ans+= nowTall - int(A[i])

    nowTall = max(nowTall, int(A[i]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
highest = 0
ans = 0
for i in range(n):
    if highest > a_ls[i]:
        ans += highest - a_ls[i]
    else:
        highest = a_ls[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:
#!/usr/bin/env python3

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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


n=int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
lens = int(input())
arrs = [int(x) for x in input().split()]
need = 0
for i in range(1, lens):
    if arrs[i] < arrs[i-1]:
        more =  (arrs[i-1] - arrs[i])
        arrs[i] += more
        need += more
print(need)

Submission status: Accepted

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

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    s = 0
    for i, a in enumerate(A):
        if i==0:
            continue
        
        if A[i-1] > A[i]:
            h = A[i-1] - A[i]
            A[i] += h
            s+=h
    print(s)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
p = 0
for a in A:
    if p > a:
        ans = ans + (p - a)
    p = max(p, a)
    # print(a, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

curr_max = 0
for a in A:
    if a > curr_max:
        curr_max = a
    else:
        rslt += curr_max - a

print(rslt)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline
#read = sys.stdin.buffer.read

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

    for i in range(1, N):
        a_left, a_right = A[i-1], A[i]
        if a_left > a_right:
            cost += a_left - a_right
            A[i] = a_left
    print(cost)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import bisect
from sys import stdin,stdout
from math import gcd,floor,sqrt,log
from collections import defaultdict as dd
from bisect import bisect_left as bl,bisect_right as br

sys.setrecursionlimit(100000000)

ii     =lambda: int(input())
si     =lambda: input()
jn     =lambda x,l: x.join(map(str,l))
sl     =lambda: list(map(str,input().strip()))
mi     =lambda: map(int,input().split())
mif    =lambda: map(float,input().split())
lii    =lambda: list(map(int,input().split()))

ceil   =lambda x: int(x) if(x==int(x)) else int(x)+1
ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1

flush  =lambda: stdout.flush()
ssi =lambda: stdin.readline()
sii =lambda: int(stdin.readline())
sp  =lambda x: stdout.write(str(x))

mod=1000000007


#main code
n=ii()
arr=lii()
count=0
for i in range(1,n):
    if arr[i]<arr[i-1]:
        count+=arr[i-1]-arr[i]
        arr[i]=arr[i-1]
print(count)

Submission status: Accepted

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    li = list(map(int, input().split()))
    max = 0
    total = 0
    for i in li:
        if max < i:
            max = i
            continue
        elif max == i:
            continue
        else:
            total += max - i
    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
result = 0
max_a = 0
for x in a:
    if x > max_a:
        max_a = x
    result += max_a - x
print(result)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()

height = 0

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

print(height)

Submission status: Accepted

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

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

print(sum(a)-sum(a_2))

Submission status: Accepted

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

print(sum)

Submission status: Accepted

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

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import functools
n = int(input())
A = list(map(int, input().split(" ")))

res = 0
hist = []
for i in range(n-1):
    if A[i] > A[i+1]:
        sa = A[i] - A[i+1]
        res += sa
        A[i+1] += sa

print(res)

Submission status: Accepted

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

res = val = 0
for i in a:
    res += val - min(val, i)
    val = max(val, i)
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

mae_max = []
max_tall = 0
for i in range(N):
    max_tall = max(max_tall, A[i])
    mae_max.append(max_tall)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-08-22 20:52:00 +0900
# LastModified: 2020-08-22 21:12:19 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    n = int(input())
    A = list(map(int, input().split()))
    base = A[0]
    ans = 0
    for a in A:
        if base > a:
            ans += base-a
        if base < a:
            base = a
    print(ans)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(-ans)

Submission status: Accepted

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

minval = 0
previous = 0
for i in range(len(A)):
    if i == 0:
        previous = A[0]
        continue

    diff = previous - A[i]

    if diff > 0:
        minval += diff
    previous = A[i] + diff if diff > 0 else A[i]

print(minval)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

prev_A = As[0]
total_step_hight = 0
for i in range(1,len(As)):
    if  As[i-1] > As[i]:
        total_step_hight += As[i-1] - As[i]
        As[i] = As[i] + (As[i-1] - As[i])

print(total_step_hight)

Submission status: Accepted

Solution code:
#import numpy as np
#import collections
#import math

#f = open('./input_C.txt')

#f.readline()
#int(f.readline())
#map(int, f.readline().split())
#list(map(int, f.readline().split()))

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

highest = A[0]
dai = 0

for Ai in A[1:]:
    if Ai <= highest:
        dai += highest-Ai
    else:
        highest = Ai

ans = dai
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
AA=list(map(int,input().split()))
s=0
key=0
for i in range(N):
    key=max(key,AA[i])
    s+=key
print(s-sum(AA))

Submission status: Accepted

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


    return ans

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(ans)

resolve()

Submission status: Accepted

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

Submission status: Accepted

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

tmp = A[0]
ans = 0

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

Submission status: Accepted

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

now = A[0]
ans = 0
for a in A:
    if a < now:
        ans += now - a
        continue
    now = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

current = a[0]
steps = 0

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

print(steps)

Submission status: Accepted

Solution code:
import math
import re
#import sympy

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

tempmax=0
retnum=0

tempmax=a[0]

for i in range(len(a)):
    if tempmax>a[i]:
        retnum+=tempmax-a[i]
    else:
        tempmax=a[i]
print(retnum)

Submission status: Accepted

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

prev, tot = a[0], 0
for i in range(1, n):
    b = max(0, prev-a[i])
    prev = a[i] + b
    tot += b

print (tot)

Submission status: Accepted

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

top = A[0]
ans = 0

for i in range(1,N):
  if A[i] < top:
    ans += (top - A[i])
  elif A[i] > top:
    top = A[i]
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = [int(i) for i in input().split()]
maxs = [0]
for a in As:
    maxs.append(max(a, maxs[-1]))
maxs = maxs[1:]
print(sum(m-a for a, m in zip(As, maxs)))

Submission status: Accepted

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

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#from bisect import bisect_left as bl                #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br               #c++ upperbound br(array,element)
 
 
def main():
    n=int(input())
    a=list(map(int,input().split(" ")))
    s=0
    for x in range(1,n):
        if a[x]<a[x-1]:
            s=s+a[x-1]-a[x]
            a[x]=a[x-1]
    print(s)


#-----------------------------BOSS-------------------------------------!
# region fastio

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

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
max_a = A[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.readline
import time
from heapq import heappush, heappop, heapify
import math
import itertools as it
def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))
start_time = time.perf_counter()
# ------------------------------

N = inp()
A = inpl()

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)

# -----------------------------
end_time = time.perf_counter()
print('time:', end_time-start_time, file=sys.stderr)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt_step = 0

for cnt in range(n-1):
    if a[cnt] > a[cnt+1]:
        cnt_step += a[cnt] - a[cnt+1]
        a[cnt+1] = a[cnt]

print(cnt_step)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""

# %% import phase

# %% define phase

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

# %% process phase
ans = 0
f = 0
for ai in a:
    if f <= ai:
        f = ai
    else:
        ans += f - ai


# %%output phase
print(ans)

Submission status: Accepted

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

for i in range(len(a)):
    if i > 0:
        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())
a_list = list(map(int, input().split()))
ans = 0

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

print(ans)

Submission status: Accepted

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

base = 0
before_a = ai[0]

for a in ai[1:]:
    if before_a < a:
        before_a = a

    else:
        base += (before_a - a)

print(base)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys,collections
from sys import stdin, stdout  
#MAXN = 10000001

def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip() 
def input(): return sys.stdin.readline().strip()
#spf = [0 for i in range(MAXN)] 
def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w') 
def is_subsequence(x, y):
    """Test whether x is a subsequence of y"""
    x = list(x)
    for letter in y:
        if x and x[0] == letter:
            x.pop(0)

    return not x    
def sieve(): 
	spf[1] = 1
	for i in range(2, MAXN): 
		spf[i] = i 
	for i in range(4, MAXN, 2): 
		spf[i] = 2

	for i in range(3, mt.ceil(mt.sqrt(MAXN))): 
		if (spf[i] == i): 
			for j in range(i * i, MAXN, i): 
				if (spf[j] == j): 
					spf[j] = i 
def getFactorization(x): 
	ret = list() 
	while (x != 1): 
		ret.append(spf[x]) 
		x = x // spf[x] 

	return ret 
def getFloor(A, x):

    (left, right) = (0, len(A) - 1)

    ind,floor = -1,-1
    while left <= right:
        mid = (left + right) // 2
        '''if A[mid] == x:
            return mid'''
        if x < A[mid]:
            right = mid - 1
        else:
            floor = A[mid]
            ind=mid
            left = mid + 1
            
    return ind
def check(st) : 
  
    # Compute sum of digits 
    n = len(st) 
    digitSum = 0
      
    for i in range(0,n) : 
        digitSum = digitSum + (int)(st[i]) 
  
    # Check if sum of digits 
    # is divisible by 9. 
    return (digitSum % 9 == 0)     
#file()
#sieve() 
mod=(10**9)+7

def main():
	n=int(input())
	l=get_array()
	c=0
	for i in range(1,n):
		if(l[i]>=l[i-1]):
			continue
		else:
			diff=l[i-1]-l[i]
			c+=diff
			l[i]+=diff
	print(c)		

	





















	




















		
			


		

			

		





			

	























				

	















	

	










if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

maxheight = a[0]
addon = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N-1):
    if arr[i]>arr[i+1]:
        dai = arr[i]-arr[i+1]
        arr[i+1] += dai
        ans += dai

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m = a[0]
f = 0

for i in range(1, len(a)):
  if m > a[i]:
    f += m - a[i]
  else:
    m = a[i]

print(f)