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())
l = list(map(int,input().split()))
ans = 0

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


print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
height = input().split()

suma = 0
highest = int(height[0])
for i in range(1,n):
  if int(height[i]) > highest:
    highest = int(height[i])
  if int(height[i]) < highest:
    suma += highest-int(height[i])
print(suma)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = [int(x) for x in input().split()]
B = A.copy()
max =-1
for i in range(len(A)):
    if(A[i] > max):
        max = A[i]
        B[i] = A[i]
    else:
        B[i] = max

C = [B[i] - A[i] for i in range(len(A))]

print(sum(C))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
maxNum = l[0]
total = 0

for i in range(1,len(l)):
    if maxNum <= l[i]:
        maxNum = l[i]
    else:
        total += maxNum - l[i]
print(total)

Submission status: Accepted

Solution code:

n = int(input())
tolls = [int(i) for i in input().split()]


max_n = 0
height_s = 0


for i in range(len(tolls)):
    if max_n < tolls[i]:
        max_n = tolls[i]
    height_s += max_n - tolls[i]


print(height_s)

Submission status: Accepted

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

cur_max = ls_a[0]
sum = 0
for i in range(1, n):
  cur_h = ls_a[i]
  diff = cur_max - cur_h
  if diff > 0:
    sum += diff
  elif diff < 0:
    cur_max = cur_h

print(sum)

Submission status: Accepted

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

for i in A:
    if i >= max:
        max = i

    else:
        sum += max-i

print(sum)

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
m = -1
for i in A:
  if(m>i):
    res += m-i
  m = max(m,i)
print(res)

Submission status: Accepted

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

first_flg = True
before_tall = 0
add_dai = 0
for now_tall in a_list:
    if first_flg:
        before_tall = now_tall
        first_flg = False
    else:
        if before_tall > now_tall:
            add_dai += before_tall - now_tall
            before_tall = now_tall + (before_tall - now_tall)
        else:
            before_tall = now_tall
print(add_dai)

Submission status: Accepted

Solution code:
n=int(input())
m=0
ans=0
a=[int(x) for x in input().split()]
for i in range(len(a)):
    if a[i]>m:
        m=a[i]
    elif m>=a[i]:
        ans+=m-a[i]

print(ans)

Submission status: Accepted

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

    answer = 0

    for i in range(1, N):
        diff = AList[i-1] - AList[i]

        if(diff > 0):
            answer = answer + diff
            AList[i] = AList[i - 1]

    print(answer)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(s) for s in stdin.readline().rstrip().split()]
ans = 0

for i in range(len(A)):
    if i == 0:
        continue

    if A[i-1] > A[i]:
        tmp = A[i-1]-A[i]
        ans = ans + tmp
        A[i] = A[i] + tmp

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def Qc():
    n = int(input())
    l = list(map(int, input().split()))
    ans = 0
    last = l[0]
    for current in l:
        if last < current:
            last = current
        if current < last:
            stand = last - current
            ans += stand
            last = current + stand
    print(ans)


if __name__ == '__main__':
    Qc()

Submission status: Accepted

Solution code:

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

_sum = 0

for e in range(len(a_list)):
    # print('aaa')
    if e != 0 :
        # print('bbb')
        if a_list[e] < a_list[e - 1] :
            # print('ccc')
            _sum = _sum + (a_list[e - 1] - a_list[e])
            a_list[e] = a_list[e] + (a_list[e - 1] - a_list[e])

print(_sum)

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

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
dai = 0
for i in range(N-1):
    delta = As[i+1] - As[i]
    dai_i = 0
    if delta < 0:
        dai_i = -delta
    dai += dai_i
    As[i+1] += dai_i
print(dai)

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key
from bisect import bisect_left


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

def bindex(a,x):
    i = bisect_left(a,x)
    if i != len(a) and a[i] == x:
        return i
    return None

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N = intinput()
    indatas = list(mulinputs())

    i = 0
    diffs = [0]
    l, r = 0, 1
    total = 0
    while r < N:
        while r < N and indatas[r] < indatas[l]:
            total += indatas[l] - indatas[r]
            r += 1
        l = r
        r += 1
    # 出力
    print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

length = len(A)
before = A[0]
for num in range(1, length):
    if before > A[num]:
        diff = before - A[num]
        ans += diff
        A[num] = before
    before = A[num]

print(ans)

Submission status: Accepted

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

ext = [0] * n
ans = 0
for i in range(1,n):
    if ary[i] < (ary[i - 1] + ext[i - 1]):
        ext[i] = (ary[i - 1] + ext[i - 1]) - ary[i]
print(sum(ext))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
s = [0]*N
for i,n in enumerate(A):
    if i == 0:
        continue
    s[i] = max(0, A[i-1] + s[i-1] - A[i])

print(sum(s))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    height = max(height, h)


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+1] < a[i]:
        dif = a[i] - a[i+1]
        a[i+1] += dif 
        ans += dif
        
print(ans)

Submission status: Accepted

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

user1 = int(input())
user2 = list(map(int, input().split()))

befHeight = -1
result = 0

for i in user2:
    if befHeight > i:
        result += befHeight - i
        i = befHeight
    befHeight = i

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

prev = 0
sum = 0
for A in input().split():
    a = int(A)
    if prev > a:
        sum += prev-a
    prev = max(prev, a)
print(sum)

Submission status: Accepted

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

    step = 0
    max_height = 0
    for i in A:
        if max_height < i:
            max_height = i
        else:
            step += max_height - i

    print(step)


def main():
    probC()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 初期入力
import sys
#input = sys.stdin.readline  #文字列では使わない
N = int(input())
A = list(map(int, input().split()))
ans =0
for i in range(1,N):
    a =A[i-1] -A[i]
    if a >0:
        A[i] +=a
        ans +=a
print(ans)

Submission status: Accepted

Solution code:
import sys
li  =   lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw  =   lambda : sys.stdin.readline().strip().split()
ni  =   lambda : int(sys.stdin.readline().strip())
nsi =   lambda : sys.stdin.readline().strip()

from collections import defaultdict as df
import math

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

Submission status: Accepted

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    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)
resolve()

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sm = 0
now = A[0]
for num in A:
    if num < now:
        sm += now-num
    elif num > now:
        now = num
print(sm)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

number_of_line = int(sys.stdin.readline())

sum = 0
height = 0
line = sys.stdin.readline()
humans = line.split(' ')
steps = 0
for human in humans:
    human_height = int(human)
    if human_height < height:
        steps += (height - human_height)
    height = max(human_height, height)

print(steps)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(len(li)):
    if i > 0:
        diff = li[i] - li[i - 1]
        if li[i] - li[i - 1] < 0:
            li[i] = li[i] + abs(diff)
            sum_diff += abs(diff)
print(sum_diff)

Submission status: Accepted

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

maximum = 0
step_sum = 0
for i in a:
    maximum = max(maximum, i)
    step_sum += maximum - i
print(step_sum)

Submission status: Accepted

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

    if i < ma:
        ans += ma-i
    else :
        ma = i
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
An = [int(val) for val in input().split()]

# 各踏み台を初期化
Fn = [0 for _ in range(N)]

an_1 = An[0]
fn_1 = 0

for i in range(1, N):
    an = An[i]
    fn =  max(0, an_1 + fn_1 - an)

    Fn[i] = fn
    fn_1 = fn
    an_1 = an

sum = 0
for fi in Fn:
    sum += fi

print(sum)

Submission status: Accepted

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

for i in range(N-1):
  large = max(large,A[i+1])
  if large != A[i+1]:
    sum += (large-A[i+1])

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
listA=list(map(int,input().split()))
listB=[0]

for i in range(len(listA)-1):
  if listA[i+1]<listA[i]:
    listB.append(listA[i]-listA[i+1])
    listA[i+1]=listA[i]
  else:
    listB.append(0)
    
print(sum(listB))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = raw_input()
ais = map(int, raw_input().split())
m = ais[0]
q = 0
for i in range(1, len(ais)):
	q += max(0,m - ais[i])
	m = max(m, ais[i])
print q

Submission status: Accepted

Solution code:
n = int(input())
ary = [int(v) for v in input().split()]

max_tmp = -1
steps = 0

for a in ary:
    if max_tmp > a:
        steps += max_tmp - a
    else:
        max_tmp = a

print(steps)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

tall = 0
ans = 0
for i in A:
    if i < tall:
        ans += (tall - i)

    if i > tall:
        tall = i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

humi = [0,]

for i in range(1, n):
    if (hight[i] >= hight[i-1]):
        humi.append(0)
    else:
        dif = hight[i-1] - hight[i]
        humi.append(dif)
        hight[i] = hight[i] + dif

ans = sum(humi)
print(ans)

Submission status: Accepted

Solution code:
n = input()
heights = [int(cur) for cur in input().split()]
curr_max = heights[0]
total = 0
for height in heights:
    if height > curr_max:
        curr_max = height
    else:
        total += curr_max - height

print(total)

Submission status: Accepted

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

for i in range(n):
    if tmp < c[i]:
        tmp = c[i]
    elif tmp >= c[i]:
        ans += tmp - c[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
heights = list(map(int, input().split()))
humidai = []
for i in range(len(heights)):
    if humidai == []:
        humidai.append(0)
    else:
        if heights[i] < heights[i-1]:
            h_height = heights[i-1] - heights[i]
            heights[i] += h_height
            humidai.append(h_height)
        else:
            humidai.append(0)
print(sum(humidai))

Submission status: Accepted

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

    ans = 0

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 0
max_a = a[0]
for i in range(len(a)-1):
    if max_a<a[i+1]:
        max_a = a[i+1]
    if max_a>a[i+1]:
        ans += max_a - a[i+1]

print(ans)

Submission status: Accepted

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

count = 0
for n,a in enumerate(A):
  if(n != 0):
    if(A[n] <= A[n-1]):
      count = count + (A[n-1] - A[n])
      A[n] = A[n-1]
      
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

highest=L[0]
val=0
for A in L[1:]:
    if A<highest:
        val+=(highest-A)
    elif A>highest:
        highest=A
print(val)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    answer = solve(N, A)

    print(answer)


def solve(N, A):
    result = 0
    max_height = A[0]
    for i in range(1, N):
        max_height = max(max_height, A[i])
        if max_height > A[i]:
            result += max_height - A[i]

    return result


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

max = 0
humidai_count = 0

for i in a:
    if max <= i:
        max = i
    else:
        humidai_count += max-i

print(humidai_count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
l2=[l[0]]
#print(l2)

for i in range(1,n):
    if l2[i-1] <= l[i]:
        l2+=[l[i]]
        continue
    else:
        l2+=[l2[i-1]]
        ans+=(l2[i]-l[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

data = input().split()

sum = 0

for i in range(N-1) :
    if(int(data[i]) > int(data[i+1])) :
        sum += int(data[i]) - int(data[i+1])
        data [i+1] = data[i]
        
print(sum)

Submission status: Accepted

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

sumF = 0
maxa = A[0]
fumidai = 0
for i in range(N-1):
  if A[i] > A[i+1]:
    sumF += A[i] - A[i+1]
    A[i+1] = A[i]
print(sumF)

Submission status: Accepted

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

for i in range(n):
    if highest < a_s[i]:
        highest = a_s[i]
    elif highest > a_s[i]:
        ans += (highest - a_s[i])

print(ans)

Submission status: Accepted

Solution code:
##a = int(stdin.readline().rstrip())
##b, c = [int(x) for x in stdin.readline().rstrip().split()]
##s = stdin.readline().rstrip()
##a = list(set(b))  重複を削除
##a = list(map(int,[int(x) for x in stdin.readline().rstrip().split()])) a[0, 1, 2, ...]
##a = [[0] * 2 for _ in range(n)]  a[0,0]
##v = [s[i: i+2] for i in range(0, len(s), 2)]
##  s="12345678" を v=["12","34","56","78"] にするイメージ
##s = "".join(list)リストを文字列に変換[""]は区切り文字
##a.sort(key=itemgetter(1))リストをソートできるよ(from operator import itemgetterが必要)
##sort_p = sorted(p) 昇順ソート

# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy
# stdin = open("sample.txt")


MOD = int(1e9+7)
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): # 入力されたintを桁ごとに分解，リストで出力
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): # 最小公倍数
    return reduce(math.gcd, numbers)

def combination(m,n): # mCn
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): # nの素因数分解の結果を辞書で出力 fuct(24) → {2: 3, 3: 1}
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    A = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    ans = 0
    
    for i in range(1,N):
        if A[i] < A[i-1]:
            ans += abs(A[i-1]-A[i])
            A[i] = A[i-1]
        
    print(ans)
            
main()

Submission status: Accepted

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

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

print(ans)