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()))
ans = 0
for i in range(1,N):
    if not 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())
h = [int(i) for i in input().split()]
m = h[0]
s = 0

for i in h:
  if(i > m):
    m = i
  else:
    s += m - i
    
print(s)

Submission status: Accepted

Solution code:
n=int(input())
a=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:
def step():
    n = int(input()) #sinceramente, isso aqui não serve pra nada
    alturas = list(map(int, input().split()))
    height = 0;

    for i in range(0, len(alturas)-1):
        if alturas[i] > alturas[i+1]:
            height += alturas[i] - alturas[i+1]
            alturas[i+1] = alturas[i]

    print(height)

step()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
highest = 0
for i, var in enumerate(A):
  if highest - var > 0:
    ans += highest - var
  else:
    highest = var
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lst = []
chair = []
res = 0
lst = input().split()
for i in range(N):
    lst[i] = int(lst[i])
for index in range(1,N):
    if lst[index] < lst[index-1]:
        chair.append(lst[index-1]-lst[index])
        lst[index] = lst[index-1]
for i in chair:
    res += i
print(res)

Submission status: Accepted

Solution code:
# Python template 
from collections import defaultdict 
import sys
import math 

def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

n = int(input())
a = get_array()

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

Submission status: Accepted

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

now = 0
ans = 0
for a in A:
    if now <= a:
        now = a
    else:
        ans += now - a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


if len(a_L) == 1:
    print(0)
    exit()

ans = 0
for i in range(0,len(a_L)-1):
    if a_L[i] > a_L[i+1]:
        ans += abs(a_L[i+1] - a_L[i])
        a_L[i+1] = a_L[i]


if a_L[-2] > a_L[-1]:
    ans += abs(a_L[-1]-a_L[-2])
print(ans)

Submission status: Accepted

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

stack = [a[0]]
s = 0
for i in range(1,n):
    if a[i]<stack[-1]:
        s = s + abs(a[i] - stack[-1])
    else:
        stack.append(a[i])
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# # Make IO faster
# import sys
# input = sys.stdin.readline

# # get single (or) multiple str
# X = input()

# # get single int
# N = int(input())
# # get multiple int (e.g., 2)
# X, Y = map(int, input().split())
# # get multiple int (e.g., 2) for N lines
# XY = [list(map(int, input().split())) for _ in range(N)]

# from IPython import embed; embed(); exit();

# 全部入り
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = MAP()
prev = -INF
ans = 0
for a in A:
    if a < prev:
        ans += (prev - a)
    else:
        prev = 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:

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


tc()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
ma=a[0]
ans=0
for q in a:
  if ma>q:
    ans+=(ma-q)
  ma=max(ma,q)
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

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

Submission status: Accepted

Solution code:
import array


class Stack:
    def __init__(self):
        self.size = 0
        self.list = list()

    def push(self, el):
        self.size += 1
        self.list.append(el)

    def pop(self):
        self.size -= 1
        return self.list.pop()

    def top(self):
        return self.list[-1]

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        return(self.list.__str__())


def cstep(ar):
    if len(ar)  == 1:
        return 0
    s = Stack()
    d = array.array('i', [0]*len(ar))
    maxima = ar[0]
    for index in range(1, len(ar)):
        el = ar[index]
        if maxima < el:
            maxima = el
        d[index] = maxima - el
    return sum(d)

def main():
    n = int(input())
    ar = list(map(int, input().strip().split()))
    print(cstep(ar))


main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == '__main__':
    N = int(input())

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

    M = max(li)
    # print(M)

    MV = li[0]
    C = 0
    hai = li.index(M)
    #print(hai)
    for i in range(N):
        if i < hai:
            if MV > li[i]:
                C += MV - li[i]
            else:
                MV = li[i]

        else:
            C += M - li[i]
        #print(C)

    print(C)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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:
b=s=0
for a in[*open(0)][1].split():a=int(a);b=max(b,a);s+=b-a
print(s)

Submission status: Accepted

Solution code:
# Author: S Mahesh Raju
# Username: maheshraju2020
# Date: 22/08/2020

from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda: int(stdin.readline().strip())
is1 = lambda: stdin.readline().strip()
iia = lambda: list(map(int, stdin.readline().strip().split()))
isa = lambda: stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007

n = ii1()
arr = iia()
res = 0
for i in range(1, n):
    if arr[i] < arr[i - 1]:
        res += arr[i - 1] - arr[i]
        arr[i] = arr[i - 1]
print(res)

Submission status: Accepted

Solution code:
import math

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

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

res = 0
a = list(a)
max_a = a[0]
for i in range(n):
    if a[i] > max_a:
        max_a = a[i]
    else:
        res += max_a - a[i]

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

    X=A[i]
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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


for i in range(len(a) - 1):
    if a[i + 1] < a[i]:
        l.append(a[i]-a[i + 1])
        a[i + 1] = a[i]


print(sum(l))

Submission status: Accepted

Solution code:
import math

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
count = 0

for i in range(N):
  A[i] = int(A[i])
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:
input()
array = list(map(int, input().split()))
max = array[0]
sum = 0

for i in array:
    if i < max:
        sum += max - i
    else:
        max = i

print(sum)

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

stage_hight = []
for i, ai in enumerate(A):
	if i != 0 and ai < A[i-1]:
		stage_hight.append(A[i-1] - ai)
		A[i] = A[i-1]
print(sum(stage_hight))

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

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=[int(i) for i in input().split()]
e=0
s=0
for i in range(1,len(a)):
    if(a[i]>=a[i-1]):
        pass
    else:
        e=abs(a[i]-a[i-1])
        s=s+e
        a[i]=a[i]+e
print(s)

Submission status: Accepted

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

ans = 0
prev_max=a[0]

for i in a:
    if prev_max > i:
        ans+= prev_max-i
    elif prev_max < i:
        prev_max = i
        
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    A  = [int(i) for i in input().split()]
    ans = 0
    max = A[0]
    for a in A:
        if max > a:
            ans += max-a
        else:
            max = a
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input(''))
A1 = np.array(list(map(int, input('').split(' '))))

def func(A):
    step = 0
    for k in range(0,N-1):
        if A[k] > A[k+1]:
            add = A[k] - A[k+1]
            step += add
            A[k+1] += add
    return step
        
print(func(A1))

Submission status: Accepted

Solution code:
# Step


def main(a):
    s = 0
    m = 0
    for ai in a:
        if m < ai:
            m = ai
        else:
            s += m - ai
    return s


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

result = main(A)
print(result)

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())
As = list(map(int, input().split(" ")))

max_height = As[0]
fumidai = 0
for height in As:
    if height < max_height:
        fumidai += max_height - height
    if height > max_height:
        max_height = height
print(fumidai)

Submission status: Accepted

Solution code:
import sys
N = sys.stdin.readline()
Alst = list(map(int, sys.stdin.readline().split()))
now = 1
x = 0
for i in Alst:
    if now < i:
        now = i
    else:
        x += (now - i)

print(x)

Submission status: Accepted

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

max_a = 0
sum_mod = 0
for a in a_list:
    if max_a > a:
        sum_mod += max_a - a
    else:
        max_a = a
print(sum_mod)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]"):
    ret = 0
    for i in range(1, N):
        tmp = max(0, A[i - 1] - A[i])
        A[i] += tmp
        ret += tmp
    print(ret)
    return

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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

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

N = I()
A = IL()

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

tot = 0

mx = A[0]
for a in A:
    if a < mx:
        tot += mx - a
    mx = max(mx, a)
print(tot)

Submission status: Accepted

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

ans = 0
tem_max = list_A[0]

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

Submission status: Accepted

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

current_highest = heights[0]
steps_sum = 0
for h in heights[1:]:
  if h > current_highest:
    current_highest = h
  else:
    steps_sum += current_highest - h
 
print(steps_sum)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
count,max_n=0,a[0]
for i in range(1,n):
    count+=max(0,max_n-a[i])
    max_n=max(max_n,a[i])
print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


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

    n = int(input())
    a = list(map(int, input().split()))
    height = a[0]
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline

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

answer = 0
max_a = A[0]

for a in A:
    max_a = max(max_a, a)
    answer += max_a - a

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

  
print(ans)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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

#処理内容
def main():
	N = int(input())
	A = getlist()
	ans = 0
	maxsum = A[0]
	for i in range(1, N):
		ans += max(0, maxsum - A[i])
		maxsum = max(maxsum, A[i])

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
N=int(input())
num=map(int, input().split())
list1=[]
list2=[]
for x in num:
    list1.append(x)
for i in range(0,N-1):
    if list1[i+1]<list1[i]:
        list1[i]+=0
        re=list1[i]-list1[i+1]
        list1[i+1]+=re
        list2.append(re)
    elif list1[i+1]>=list1[i]:
        list1[i+1]+=0
        list1[i]+=0
print(sum(list2))

Submission status: Accepted

Solution code:
import sys

n, *aaa = map(int, sys.stdin.buffer.read().split())
cur = aaa[0]
ans = 0
for a in aaa[1:]:
    if cur > a:
        ans += cur - a
    else:
        cur = a
print(ans)

Submission status: Accepted

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

ans=0

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(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 = map(int, input().split())
ans = 0
ma = 0
for a in A:
    if ma < a:
        ma = a
    ans += ma - a
print(ans)

Submission status: Accepted

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

import sys
input=sys.stdin.readline

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

Submission status: Accepted

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

sum = 0
step =0


for i in range(n):
    try:
        if As[i] > As[i + 1]:
            step = As[i] - As[i + 1]
            As[i + 1] += step
            sum += step 
    except IndexError:
        continue



print(sum)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    N = NI()
    A = NLI()
    now = 0
    ans = 0
    for a in A:
        if now <= a:
            now = a
        else:
            ans += now - a
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

max_ = 0
c=0
for i in range(n):
    if max_>=A[i]:
        c +=(max_-A[i])
    else:
        max_ = A[i]
print(c)

Submission status: Accepted

Solution code:
b=0
print(sum((b:=max(c:=int(a),b))-c for a in[*open(0)][1].split()))

Submission status: Accepted

Solution code:
def main():

    n = int(input())
    x = tuple([int(t)for t in input().split()])

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [*map(int, input().split())]
s = 0
pre = A[0]
for now in A[1:]:
    if pre > now: s += pre - now
    else: pre = now
print(s)

Submission status: Accepted

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

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

Submission status: Accepted

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

    max_height = A[0]
    ans = 0
    for i in range(1, N):
        if A[i] >= max_height:
            max_height = A[i]
        else:
            ans += max_height - A[i]
    
    print(f'{ans}')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    print(cnt)
main()

Submission status: Accepted

Solution code:
# maをi-1番目までにでてる最大値としてi番目との差分を足していく

def main():
    # INF = 10 ** 9
    N = int(input())
    A = list(map(int, input().split()))

    # initialize
    ma = A[0]

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

if __name__ == "__main__":
    main()