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:
import sys
import math 
# sys.stdin = open('input.txt', 'r')  
# sys.stdout = open('output.txt', 'w')
n = int(input())
height = list(map(int, input().split()))
x = height[0]
ans = 0
for i in range(1, n):
	if x >  height[i]:
		ans += (x - height[i])
	x = max(x, height[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import copy
from collections import deque
import collections
import itertools
stdin = sys.stdin
import math

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()

a = na()

prev = 0
cnt = 0
for ai in a:
    if ai >= prev:
        prev = ai
    else:
        cnt += (prev-ai)

print(cnt)

Submission status: Accepted

Solution code:
from collections import deque

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

max_A = 0
ans = 0
while A_deq:
    anum = A_deq.popleft()
    if max_A <= anum:
        max_A = anum
    else:
        ans += max_A - anum

print(ans)

Submission status: Accepted

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

maxA = 0
count = 0
for a in As:
    # print(a)
    if a >= maxA:
        pass
    else:
        # print("count up : ", maxA - a)
        count += maxA - a
    maxA = max(a, maxA)
    # print("max : ", maxA)
    
print(count)

Submission status: Accepted

Solution code:

def li():
    return [int(x) for x in input().split()]


N = int(input())
A = [0] + li()
total = 0
for i in range(1, N+1):
    diff = A[i] - A[i - 1]
    if diff < 0:
        total -= diff
        A[i] = A[i - 1]

print(total)

Submission status: Accepted

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

B = []
S = 0
for i in range(N):
    #print(A[i], B)
    if i == 0:
        B.append(A[i])
        continue
    last_B = B[i - 1]
    humidai = last_B - A[i]
    if humidai <= 0:
        humidai = 0
    now_B = A[i] + humidai
    #print(humidai)
    S += humidai
    B.append(now_B)

print(S)

Submission status: Accepted

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

total_step = 0
for i in range(N):
    if i == 0:
        prev_max_height = heights[i]
        continue
    height = heights[i]
    if height > prev_max_height:
        prev_max_height = height
    else:
        total_step += prev_max_height - height

print(total_step)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
ma = 0
ans = 0
for i in a:
    ma = max(ma, i)
    ans += (ma - 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] < 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()]

offset=0
now=0
for i in a:
    if i<=now:
        offset+=now-i
    else:
        now=i

print(offset)

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

Submission status: Accepted

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

for a in A:
    if pre > a:
        ans += pre - a
        a = pre
    pre = a

print(ans)

Submission status: Accepted

Solution code:
#C
n = int(input())
a = list(map(int,input().split()))
base = a[0]
Sum = 0

for i in a:
    if base > i:
        Sum += (base - i)
    
    if base < i:
        base = i
        
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] > 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()))
cnt = 0
tmp = a[0]
for i in range(1,n):
    if a[i] <= tmp:
        cnt += tmp - a[i]
    else:
        tmp = a[i]
print(cnt)

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

maxi = 0
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)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in 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:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

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


main()

Submission status: Accepted

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

for i in range(1, n):
  ai = arr[i]
  ai_ = arr[i-1]
  if ai >= ai_:
    pass
  else:
    cnt += ai_ - ai
    arr[i] = ai_
    
print(cnt)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ms = c[0]
ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

_max = 0
ans = 0
for v in a:
    _max = max(_max, v)
    ans += (_max - v)

print(ans)

Submission status: Accepted

Solution code:
input()
a = input().split(" ")

a = list(map(int, a))

count = 0
for i in range(1, len(a)):
  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
x = a[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

maxa = 0
box = 0
for value in A:
    if maxa < value:
        maxa = value
    if maxa > value:
        box += maxa - value

print(box)

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

Submission status: Accepted

Solution code:
def C():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    a_max = 0
    for t in a:
        if a_max > t:
            ret += a_max-t
        else:
            a_max = t
    print(ret)
C()

Submission status: Accepted

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
import sys
from decimal import *
from collections import deque


def get_int():
    return int(input())

def get_string():
    return input()

def get_int_list():
    return [int(x) for x in input().split()]

def get_string_list():
    return input().split()

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

def get_string_char_list():
    return list(str(input()))

# print("{} {}".format(a, b))
# a_list = [0] * a
sys.setrecursionlimit(10 ** 6)

def main():
    start = time.time()

    n = get_int()
    a = get_int_list()

    ans = 0
    wk = 0
    for i in range(n):
        wk = max(wk, a[i])
        if a[i] < wk:
            ans += wk - a[i]






    print(ans)

    #print(time.time() - start)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# [] = list(map(int, input().split()))

n = int(input())
aList = list(map(int, input().split()))
hList = aList.copy()
header = aList[0]
for index, item in enumerate(aList[1:]):
    if header > item:
        hList[index+1] = header
    else:
        header = item
dif = 0
for i in range(n):
    dif += hList[i] - aList[i]
print(dif)

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

for l in ll:
  if l < temp:
    ans +=  temp - l
  elif l >= temp:
    temp = l

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ssum = 0
pr = 0
for i in A:
    dai = max(0, pr - i)
    ssum += dai
    pr = i + dai
print(ssum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in 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:
import sys,bisect

from sys  import stdin,stdout

from bisect import bisect_left,bisect_right,bisect,insort,insort_left,insort_right

from math import gcd,ceil,floor,sqrt

from collections import Counter,defaultdict,deque,OrderedDict

from queue import Queue,PriorityQueue

from string import ascii_lowercase

sys.setrecursionlimit(10**6)
INF = float('inf')
MOD = 998244353
mod = 10**9+7

def isPrime(n):
    
    if (n <= 1) :return False
    if (n <= 3) :return True
    if (n%2 == 0 or n%3 == 0):return False
    for i in range(5,ceil(sqrt(n))+1,6):
        if (n%i==0 or n%(i+2)==0):
            return False
    return True


def st():
    return list(stdin.readline().strip())

def inp():
    return int(stdin.readline())

def li():
    return list(map(int,stdin.readline().split()))

def mp():
    return map(int,stdin.readline().split())

def pr(n):
    stdout.write(str(n)+"\n")
    
def DFS(dictionary,vertex,visited):
    visited[vertex]=True
    stack=[vertex]
    print(vertex)
    while stack:
        a=stack.pop()
        for i in dictionary[a]:
            if not visited[i]:
                print(i)
                visited[i]=True
                stack.append(i)


def BFS(dictionary, vertex,visited):
    visited[vertex]=True
    q=deque()
    q.append(vertex)
    while q:
        a=q.popleft()
        for i in dictionary[a]:
            if not visited[i]:
                visited[i]=True
                q.append(i)
                print(i)

                


def soe(limit):
    l=[1]*(limit+1)
    l[0]=0
    l[1]=0
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0
    
    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)
                
def gcd(a,b):
    while b:
        a=a%b
        b,a=a,b
    return a

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
        a*=a
        n=n>>1
    return r
    
def solve():
    n=inp()
    l=li()
    ans=0
    for i in range(1,n):
        if l[i-1]>l[i]:
            v=l[i-1]-l[i]
            ans+=v
            l[i]+=v
    pr(ans)
    




for _ in range(1):
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

max = A[0]
ans = 0
for a in A:
    if (m := max - a) > 0:
        ans += m
    else:
        max = a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(cnt)

Submission status: Accepted

Solution code:
from collections import deque
import sys, copy, itertools
input = sys.stdin.readline

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

ans = 0
x = 0

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

#print(A)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

step_sum=0
h=line[0]

for i in line:
    if i<h:
        step_sum+=h-i
    elif i>h:
        h=i
print(step_sum)

Submission status: Accepted

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

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

count = 0
tmp = 0
for idx, i in enumerate(A):
  if idx == 0:
    tmp = i
    continue
  if tmp > i: 
    count += tmp - i
  else:
    tmp = i
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N=int(input())
#A="a"
A=list(map(int,input().split()))
ans=0
tmp=A[0]

for i in A:
    if i<tmp:
        ans+=tmp-i
    else:
        tmp=i
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
#N,X,T=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))

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

Submission status: Accepted

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

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

    print(ans)

main()

Submission status: Accepted

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


def main():
    n = int(si())
    l = [int(e) for e in si().split()]
    prv, ans = l[0], 0
    for e in l:
        if prv > e:
            ans += prv-e
        else:
            prv = e
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(fumidai_total)

Submission status: Accepted

Solution code:
# cook your dish here
n=int(input())
ans=0
v=list(map(int,input().split()))

for i in range(1,n):
    if v[i]<v[i-1]:
        ans+=(v[i-1]-v[i])
        v[i]=v[i-1]
        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

mini = A[0]
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())

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

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

Submission status: Accepted

Solution code:
def main():
    n, *a = map(int, open(0).read().split())
    ans = 0
    t = 0
    for p, q in zip(a, a[1:]):
        p += t
        if p > q:
            ans += (p - q)
            t = (p - q)
        else:
            t = 0
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

fumidai = 0
great = A[0]

for i in range(n):
    if great > A[i]:
        fumidai += great - A[i]
    else:
        great = A[i]

print(fumidai)

Submission status: Accepted

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

ans = 0
now = a[0]

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

rev, bef = 0, None
for n in A:
    if bef is None:
        bef = n
        continue

    if n < bef:    
        rev += (bef - n)
    else:
        bef = n
    
print(rev)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

count = 0

for i in range(len(A)):
    if i > 0:
        buf = max(A[i-1] - A[i], 0)
        count += buf
        A[i] += buf
        
print(count)

Submission status: Accepted

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

max_a = 0
n_steps = 0
for a in A:
    max_a = max(a, max_a)
    n_steps += max(max_a - a, 0)
print(n_steps)

Submission status: Accepted

Solution code:
# coding: utf-8

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
h = A[0]
res = 0 #sum
for i in range(N):
    if A[i] >= h:
        h = A[i]
    else:
        res += h - A[i]
        
print(res)

Submission status: Accepted

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

print(fumidaisum)

Submission status: Accepted

Solution code:
import math
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

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

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

N = I()
A = LI()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, input().split())
ans = 0
c_max = 0
for a in A:
  if a > c_max:
    c_max = a
  ans += c_max - a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = 0

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

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0
for i in range(1, n, 1):
    if heights[i] < heights[i-1]:
        sum += (heights[i-1] - heights[i])
        heights[i] += (heights[i-1] - heights[i])

print(sum)

Submission status: Accepted

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

print(ct)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

step_sum = 0
height_front = A_list[0]
for idx in range(1, len(A_list)):
    A = A_list[idx]
    if A < height_front:
        step_sum += height_front - A
    else:
        height_front = A

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#
import math
import sys
import itertools
import numpy as np

n =int(input())

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

now = 0
answer = 0
for i in range(n):
    if now > a[i]:
        answer += now - a[i] #fumidai
    else:
        now = a[i]

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(str(sum))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
h = a[0]
ans = 0
for x in a:
  if h > x:
    ans = ans + (h - x)
  elif h<= x:
    h = x
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):
  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())

N_list = list(map(int,input().split()))
max_n = 0
cnt = 0
for i in range(len(N_list)-1):
  if max_n < N_list[i]:
    max_n = N_list[i]
  if max_n > N_list[i+1]:
    cnt += max_n - N_list[i+1]
    
print(cnt)