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:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


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


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(f'{solve(N, A)}')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

result = 0
p = arr[0]

for i in range(1, len(arr)):
  if p >= arr[i]:
    result += p - arr[i]
  else:
    p = arr[i]

print(result)

Submission status: Accepted

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

for a in A[1:]:
  if a < max:
    ans += max - a
  else:
    max = a

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
str_in = input()
num = [int(n) for n in str_in.split()]
num = list(map(int, str_in.strip().split()))

hight = num[0]
stools = 0

for x in range(1,len(num)):
    if hight>num[x]:
        stools+=hight-num[x]
    elif num[x] > hight:
        hight = num[x]       
print (stools)

Submission status: Accepted

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

sum = 0
k = a[0]

for i in range(n - 1):
    if k >= a[i + 1]:
        sum += k - a[i + 1]
    else:
        k = a[i + 1]

print(sum)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

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

print(step_sum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    
    for i in range(1, len(a)):
        humidai = max(0, a[i-1] - a[i])
        ans +=  humidai
        a[i] += humidai
    print(ans)
        
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
m=ls[0]
ans=0
for i in range(1,N):
  if ls[i]>m:
    m=ls[i]
  else:
    ans+=(m-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):
    if i == 0:
        continue

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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:
s = int(input())
arr = list(map(int, input().split()))

ans = 0

for i in range(1,s):
    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())
a = list(map(int, input().split()))

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

print(step)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    top = a[0]
    ans = 0

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

    print(ans)


main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
m = 0
ans = 0
for l in L:
    m = max(m, l)
    ans += m - l
print(ans)

Submission status: Accepted

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

for l in L:
  if m == l: 
    continue
  elif m > l: 
    ans += (m - l)
  elif m < l:
    m = l
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 176 C
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)-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())

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

l = []

t = li[0]
for i in range(1,len(li)):
    if t >= li[i]:
        l.append(t - li[i])
    else:
        l.append(0)
        t =li[i]

print(sum(l))

Submission status: Accepted

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

total, large = 0,0

for i in A:
  
  if large > i:
    total += (large - i)
  
  if i > large: large = i
    
print(total)

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template
N = int(input())
A = inputlist()
maxa = A[0]
ans= 0
for i in range(1,N):
    if A[i] < maxa:
        ans += maxa - A[i]
    else:
        maxa = A[i]
print(ans)

Submission status: Accepted

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

n = int(input())
a = list(map(int,input().split()))
ma = -1
res = 0
for i in a:
    res += max(ma - i,0)
    ma = max(ma,i)
print(res)

Submission status: Accepted

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

print (total)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(N) :
    if i==0 :
        continue
    if A[i]<A[i-1] :
        count += abs(A[i]-A[i-1])
        A[i] += abs(A[i]-A[i-1])

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for a in A[1:]:
  ans += max(0, pre_a - a)
  pre_a = max(pre_a, a)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
people = [int(i) for i in input().split()]
step = 0
front = people[0]
for i in range(N):
    if front > people[i]:
        step += front - people[i]
    else:
        front = people[i]
print(step)

Submission status: Accepted

Solution code:
N = int(input())

A = (int(x) for x in input().split())

A = list(A)

step = 0

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



print(step)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

for i in range(0, 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()))
amax = a[0]
asum = 0
acount = 0
result = 0

for i in range(0,n):
  if amax > a[i]:
    acount+=1
    asum+=a[i]
    if i == n-1:
      result += amax*acount-asum
  else:
    result += amax*acount-asum
    amax = a[i]
    asum = 0
    acount = 0

    
print(result)

Submission status: Accepted

Solution code:
def solver(arr):
    stool = 0
    for i in range(1, len(arr)):
        
        if arr[i]<arr[i-1]:
            stool += -arr[i]+arr[i-1]
            arr[i] = arr[i-1]
    return stool


if __name__=='__main__':
    n = int(input())
    arr = list(map(int,input().split()))
    print(solver(arr))

Submission status: Accepted

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

Submission status: Accepted

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

answer = 0
maximum = arr[0]
for index in range(1, len(arr)):
    maximum = max(maximum, arr[index])
    answer += abs(maximum - arr[index])

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[int(i) for i in sys.stdin.readline().split()]
m=0
sums=0
for i in a:
    m=max(m,i)
    if i<m:
        sums+=m-i
    
print(sums)

Submission status: Accepted

Solution code:
n=int(input())

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


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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    s = 0
    for i in range(1, N):
        pre = A[i-1] #前の人の身長
        now = A[i] #今見ている身長
        if pre > now:
            step = pre-now
            s += step
            A[i] += step
    print(s) 
if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = 0 
t = 0
for e,(i , j) in enumerate(zip(A,A[1:])):
    if i + t > j : 
        ans += t + i-j
        t= t + i-j
    else:t = 0
    # print(t,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
tall = input().split(" ")

step = 0
for i in range(n-1):
  c = int(tall[i+1]) - int(tall[i])
  if c < 0:
    step -= c
    tall[i+1] = int(tall[i+1]) - c
print(step)

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

Submission status: Accepted

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

stands = 0
for i in range(1,n): 
  stand = member[i-1] - member[i]
  if stand > 0:
    stands += stand
    member[i] += stand
print(stands)

Submission status: Accepted

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

humidai = [0]*N
s = A[0]

for i in range(1, N):
    if A[i]<s:
        humidai[i]=s-A[i]
    else:
        s = A[i]

print(sum(humidai))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
ma = A[0]
total = 0
for a in A:
  if a < ma:
    total += ma - a
  ma = max(ma, a)
print(total)

Submission status: Accepted

Solution code:

n = int(input())

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

ans = []

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

print(sum(ans))

Submission status: Accepted

Solution code:
#176-C

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

sum = 0

for i in range(0, N):
  if i == 0:
    continue
  elif (shincho[i] - shincho[i-1]) < 0:
    sum += shincho[i-1] - shincho[i]
    shincho[i] = shincho[i-1]

print(sum)

Submission status: Accepted

Solution code:
N=int(input())
*A,=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()))
ans = 0
for i in range(N-1):
    next = i + 1
    if A[i] > A[next]:
        a =A[i] - A[next]
        ans += a
        A[next] += a
    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] < a[i - 1]:
        ans += a[i - 1] - a[i]
        a[i] = a[i - 1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C問題

# 難しく考えず、入力された配列の先頭の要素から順に見に行く
# cur_maxという、①最大身長格納変数を用意、②①を更新して行く　この2ステップの考え方が大事
N = int(input())
cur_max = 0
ans = 0
# 配列を入力   
A = list(map(int, input().split()))

for i in A:
    if cur_max > i:
        ans += cur_max - i
# どんどん高いものに更新→max関数で処理
    cur_max = max(i, cur_max)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    # a, b, c, d = map(int, input().split())
    a = list(map(int, input().split()))
    # s = input()
    mx = 0
    result = 0
    for num in a:
        mx = max(mx, num)
        if mx > num:
            result += mx - num
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

x = 0
m = a[0]

for i in range(n):
    if a[i] < m:
        x += m - a[i]
    else:
        m = a[i]
    
print(x)

Submission status: Accepted

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

ans = A[0]
a = A[0]

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

print(ans - sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

print(s)

Submission status: Accepted

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

b = [0] * n

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# cording:utf-8

# Nの入力
N = int(input())

# 要素数Nのリストを作成
N_i = [0] * N

# N個の整数の入力
N_i = list(map(int, input().split()))

# 踏み台高さ合計Sumを定義
Sum = 0

# N回の繰り返し
for i in range(1, N):
    # Sum_iを初期化
    Sum_i = 0

    # 直前の人の高さが自分より高ければ
    if N_i[i-1] > N_i[i]:
        # 仮のSum_iに踏み台の高さを代入
        Sum_i = N_i[i-1] - N_i[i]
        # N_iに上書き
        N_i[i] = N_i[i] + Sum_i
        # 合計値を算出
        Sum = Sum + Sum_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] < a[i - 1]:
        ans += a[i - 1] - a[i]
        a[i] = a[i - 1]

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


n = int(input())
A = ip()
k = A[0]
ans = 0
for i in range(len(A)):
    k = max(k, A[i])
    ans += (k - A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=str(input())


def ans176(N:int, A:str):
    A=list(map(int,A.split()))
    step_count=0
    max=A[0]
    for i in range(1,N):
        if A[i]<max:
            step_count=step_count+max-A[i]
        else:
            max=A[i]
    return(step_count)

print(ans176(N,A))

Submission status: Accepted

Solution code:
#####
# C #
#####

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

key = A[0]
ans = 0

for a in A:
    if  a < key:
        ans += key - a
        # print(a,key-a,ans,'debug')
    else:
        key = a

print(ans)

Submission status: Accepted

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

max_len = A[0]
sum = 0
# 前から順に調整
for ai in A:
    if ai < max_len:
        sum += max_len - ai
    else:
        max_len = ai

print(sum)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
h = A[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(str(s))

Submission status: Accepted

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

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

print(str(s))

Submission status: Accepted

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

height_list = [0] * N

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

print(sum(height_list))

Submission status: Accepted

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

sum=0;
max=a[0];
for aa in a:
    if aa<max:
        sum=sum+max-aa
    else:
        max=aa

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
maxa = 0
for aa in a:
    maxa = max(maxa, aa)
    ans += maxa - aa
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
ans = 0
now = A[0]
for a in A:
    if now > a:
        ans += now -a
    if now < a:
        now = a
print(ans)

Submission status: Accepted

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

for i in range(n):
    b = max(b,a[i])
    c += (b-a[i])

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
max = A[0]

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

print(ans)

Submission status: Accepted

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

for i in range(N-1):
    Dff = A[i] - A[i+1]
    if Dff > 0:
        A[i+1] += Dff
        Sum += Dff
    Dff = 0

print(Sum)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
max = 0
sum = 0
for i in range(N):
    if max < As[i]:
        max = As[i]
    else:
        sum += (max-As[i])

print(sum)

Submission status: Accepted

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

now = 0
cost = 0
for A in As:
    if A > now:
        now = A
    else:
        cost += now-A
print(cost)

Submission status: Accepted

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

max_list = []
m = 0
ans = 0
for a in A:
    ans += max(m - a, 0)
    max_list.append(m)
    m = max(m, a)

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

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

    s = 0
    high = 0
    for ai in A:
        if ai < high:
            s += high - ai
        else:
            high = ai

    print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def judge(x, y):
  if x > y:
    return(x-y)
  else:
    return(0)
ans = 0
for i in range(n - 1):
  xy = judge(a[i], a[i+1])
  a[i+1] += xy
  ans += xy
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
height = 0
res = 0
for i in A:
    if i > height:
        height = i
    elif i < height:
        res += height - i
print(res)

Submission status: Accepted

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

if len(A) == 1:
    print(0)
    
else:
    for i in A:
        if A[con] > A[con+1]:
            A[con+1] =  A[con]
        
        if A[con+1] == A[-1]:
            break

        con += 1

    ans = sum(A) - raw
    print(ans)

Submission status: Accepted

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

print(fumidai)

Submission status: Accepted

Solution code:
n = int(input())
a = map(int,input().split(' '))
tallest = 0
step = 0
for i in a:
    if i < tallest:
        step = step + tallest - i
    else:
        tallest = i
print(step)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

prev_height = A_list[0]
result = 0

for i in range(1, N):
    cur_height = A_list[i]
    if prev_height > cur_height:
        result += prev_height - cur_height
    else:
        prev_height = cur_height

print(result)

Submission status: Accepted

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