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()))
stepsum = 0
for i in range(n):
  if i and a[i-1] > a[i]:
    stepsum += a[i-1] - a[i]
    a[i] = a[i] + (a[i-1] - a[i])
print(stepsum)

Submission status: Accepted

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

Submission status: Accepted

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

for y in range(1, N):
    if lists[y - 1] <=  lists[y]:
        continue
    else:
        s = abs(lists[y] - lists[y -1])
        lists[y] += s
        step_sum += s
        continue
        
print(step_sum)

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

Submission status: Accepted

Solution code:
N=int(input())
A = list(map(int, input().split()))
#print(A)
count=0
count2=0

for i in range(len(A)-1):
    #print(A[i])
    if A[i]>A[i+1]:
        count=A[i]-A[i+1]
        #print(count)
        A[i+1]=A[i]
        count2+=count

print(count2)

Submission status: Accepted

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

Submission status: Accepted

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

# if N == 1:
#     print(0)
#     exit()

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

print(step)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

total = 0

front = hs[0]

for i in range(1, n):
  if front < hs[i]:
    front = hs[i]
  else:
    total = total + front - hs[i]
    
print(total)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans=mx=0
for x in A:
    mx=max(mx,x)
    ans+=mx-x

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

Ai = input().split()

count = 0
A = int(Ai[0])

for i in Ai:
    i = int(i)
    if A > i:
        count += A - i
    else:
        A = i
print(count)

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

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

Submission status: Accepted

Solution code:
#9の倍数の判定
n = int(input())
a = list(map(int, input().split( )))
s = 0
hi = 0
for i in range(n-1):
  dis = a[i] +  hi - a[i+1]
  if dis >= 0:
    hi = dis
    s += dis
  else:
    hi = 0

print(s)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#current height
now = 0
total = 0

for a in A:
    if a >= now:
        now = a
        pass
    else:
        total += now - a

print(total)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

#current height
now = 0
total = 0

for a in A:
    #print("a", a)

    if a >= now:
        now = a
        pass
    else:
        total += now - a
    #print("now", now)
    #print("total", total)

print(total)

Submission status: Accepted

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

for i in range(1,N):
    #print(A[i-1],A[i])
    if A[i] < A[i-1]:
        count += A[i-1]-A[i]
        A[i] += A[i-1]-A[i]
print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

p,t=A[0],0
for a in A:
  if p>a:
    t+=p-a
  else:
    p=a

print(t)

Submission status: Accepted

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

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

print(step)

Submission status: Accepted

Solution code:
num = int(input())
item = input().split()
res = 0

for i in range(num):
  if i == 0:
    continue
  b = int(item[i - 1])
  t = int(item[i])
  
  if b > t:
    res += (b - t)
    item[i] = b
    
      
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

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

res = 0
max_size = A[0]
for i in range(N):
  max_size = max(max_size, A[i])
  res += max_size - A[i]
print(res)

Submission status: Accepted

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

res = 0
max_size = A[0]
for i in range(N):
  max_size = max(max_size, A[i])
  d = max_size - A[i]
  res += d
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
prev = A[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)


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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int,input().split()))
sum1 = 0
now  = Alist[0]

for a in Alist:
    if a < now:
        sum1 += (now-a)
    else:
        now = a

print(sum1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
heights = [int(x) for x in input().split()]
result = 0
maxSeen = float("-inf")
for h in heights:
    if h < maxSeen:
        result += maxSeen - h
    else:
        maxSeen = max(maxSeen, h)
print(result)

Submission status: Accepted

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

sum_num = 0
b_max = 0
for a in A:
    # print(b_max, a)
    sum_num += max(b_max-a, 0)
    b_max = max(b_max, a)

print(sum_num)

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

for i in range(1, n):
    if ls[i-1] > ls[i]:
        ans += ls[i-1] - ls[i]
        ls[i] = ls[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 = [int(x) for x in input().split()]

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

Submission status: Accepted

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

ans = 0
m = lst[0]

for i in lst:
    ans += max(0, m-i)
    m = max(m, i)
    # print(ans,m)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
arr=[int(i) for i in input().split()]
min=arr[0];su=0
for i in range(1,n):
  if (min-arr[i]<0):
    min=arr[i]
  else:
    su+=min-arr[i]
print(su)

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, input().split()))
answer = 0
for i in range(N-1):
  if(A[i]>=A[i+1]):
    answer += A[i]-A[i+1]
    A[i+1] = A[i]
print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
res = 0
pre = a[0]
for i,x in enumerate(a):
    if i == 0: continue
    if x < pre:
        res += pre-x
    else:
        pre = x
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
max_a = 0
sum_s = 0
for ai in a:
    max_a = max(max_a, ai)
    sum_s += max_a - ai
print(sum_s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def solve():
    count = 0
    tmp = A[0]
    for i in range(1, N):
        if A[i] >= tmp:
            tmp = A[i]
        else:
            count += tmp - A[i]
    return count

if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

h_sum=0
max_h=A[0]
for n in range(1,N):
    if A[n]<=A[n-1]:
        h_sum+=A[n-1]-A[n]
        A[n]=A[n-1]
    else:
        max_h=A[n]
print(h_sum)

Submission status: Accepted

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

sum = 0
m = lis[0]
for i in lis:
    if m > i:
        sum += (m-i)
    else:
        m = i

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
A = map(int, input().split())
sum = 0
pre = 0
for ai in A:
  if pre > ai:
    sum += pre - ai
  else:
    pre = ai
print(sum)

Submission status: Accepted

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

Max_num = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 176
# C - Step

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

ans=0

for i in range (1,N):
    if Als[i]>=Als[i-1]:
        pass
    else :
        step=abs(Als[i]-Als[i-1])
        ans+=step
        Als[i]+=step
print(ans)

Submission status: Accepted

Solution code:
# 自分の改造
n = int(input())
A = list(map(int, input().split()))
s = 0 
M = 0
for i in range(n-1):
    if A[i] <= A[(i+1)] and M <= A[(i+1)]:
        M = 0
        continue
    else:
        if M == 0:
            s += A[i] - A[(i+1)]
            M = A[(i+1)] + (A[i] - A[(i+1)])
        else:
            s += M - A[(i+1)]
print(s)

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int,input().split()))
new_array = array
t = sum(array)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

sum=0
MaxNum=0
for i in range(N):
    MaxNum=max(MaxNum,A[i])
    B=MaxNum
    sum+=MaxNum-A[i]
    
print(sum)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,*A = map(int,open(0).read().split())
base = A[0]
ans = 0
for i in A[1:]:
    if base > i:
        ans += base - i
    elif base < i:
        base = i
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline


def main():
    n = int(input())
    A = np.array(input().split(), dtype=int)
    final = np.maximum.accumulate(A)
    ans = (final - A).sum()
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    l = list(map(int, input().split()))
    fumidai_sum = 0
    last = 0
    for i, ai in enumerate(l):
        if last > ai:
            diff = last - ai
            fumidai_sum += diff
            last = ai + diff
        else:
            last = ai
            continue
    print(fumidai_sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi -an[i]
            humi += hu
        maxi = max(maxi, an[i])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    n = int(input())
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi -an[i]
            humi += hu
        maxi = max(maxi, an[i])

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

Submission status: Accepted

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

d = 0
ans = 0
for a, b in zip(a[:-1], a[1:]):
    if a + d > b:
        d = a + d - b
        ans += d
    else:
        d = 0

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = input().split()
pre_a = 0
sum = 0
for i in range(N):
    a = int(A[i])
    if (pre_a != 0 and a < pre_a):
        diff = pre_a - a
        sum += diff
    else:
        pre_a = a
print(sum)

Submission status: Accepted

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

def solve(n,a):
    ans = 0
    tallest = 0
    for i in range(n):
        if a[i] >= tallest:
            tallest = a[i]
        else:
            ans += abs(a[i] - tallest)
    return ans


def main():
    N = int(input())
    a = list(map(int,input().split()))
    print(solve(N,a),end="")
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import 
#=input()
n=int(input())
#=map(int,input().split())
a=list(map(int,input().split()))
#=[list(map(int,input().split())) for _ in range()]

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

Submission status: Accepted

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

ans = 0
if N == 1:
    print(ans)
else:
    tmp_max = A[0]
    for a in A[1:]:
        if a < tmp_max:
            ans += tmp_max-a
        else:
            tmp_max = a

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

b = 0

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

print(t)

Submission status: Accepted

Solution code:
N = int(input())#整数
A = list(map(int, input().split()))#配列Int型

ans = 0
max_val = A[0]

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

ans = 0
mina = A[0]

for a in A[1:]:
  if mina > a:
    ans += mina - a
  else:
    mina = a
    
print(ans)

Submission status: Accepted

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

h_max= h[0]
ans=0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

h_sum  = 0
temp_max = ary_A[0]

for i in range(len(ary_A) - 1):
    diff = ary_A[i + 1] - temp_max
    if diff < 0:
        h_sum += abs(diff)
    else:
        temp_max = ary_A[i + 1]

print(h_sum)

Submission status: Accepted

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

ans = 0
ma = a[0]

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

Submission status: Accepted

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

top = A[0]
score = 0
for a in A[1:]:
  if a <= top:
    score += (top - a)
  else:
    top = a
print(score)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

height = A[0]
cnt = 0
for a in A:
    if a < height:
        cnt += (height - a)
    else:
        height = a
print(cnt)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
import sys
sys.setrecursionlimit(10**9)



def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def solve(N: int, A: "List[int]"):
    mx = 0
    ans = 0
    for i in range(N):
        if mx > A[i]:
            ans += mx - A[i]
            A[i] = mx
        
        mx = max(mx,A[i])
 
    print(ans)
 
    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:
N = int(input())
A = list(map(int, input().split()))
step = 0
tmp_step = 0
for i in range(N-1):
  if A[i] + tmp_step > A[i+1]:
    tmp_step = A[i] + tmp_step - A[i+1]
    step += tmp_step
  else:
    tmp_step = 0
print(step)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 0
m = a[0]
for i in range(1, n):
    ans += max(m - a[i], 0)
    m = max(m, 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):
  if(i == 0):
    pass
  else:
    if(A[i] - A[i-1] >= 0):
      pass
    else:
      tmp = A[i-1] - A[i]
      ans += tmp
      A[i] += tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int,input().split()))
solve = []

ans = 0
maxnum = 0

for i in range(N):
    maxnum = max(maxnum,an[i])
    ans += maxnum - an[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    current_max = -1
    ans = 0
    for a in A:
        if a <= current_max:
            ans += current_max - a
        else:
            current_max = a
    return ans

print(c_step())

Submission status: Accepted

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

Submission status: Accepted

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