Problem:


Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.



Sample input:
5
2 1 5 4 3


Sample output:
4

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
M=map(int,input().split())
Mx=0
Total=0
for n in M:
  Mx=max([Mx,n])
  Total+=Mx-n
print (Total)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
flag=a[0]

for i in range(len(a)):
    if a[i] < flag:
        ans+=flag-a[i]
    else:
        flag = 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())
heights = list(map(int, input().split()))
steps = [0] * N
for i in range(1, N):
    if heights[i-1] + steps[i-1] > heights[i]:
        steps[i] = (heights[i-1] + steps[i-1]) - heights[i]
    else:
        steps[i] = 0
        
print (sum(steps))

Submission status: Accepted

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

dailist = [0]*N
Alist.append(0)
dailist.append(0)
max = 0
for i in range(N):
  if Alist[i]>max:
    max=Alist[i]
    #dailist[i+1]=max-Alist[i+1]
    #print("max="+str(max))
  
  if max>Alist[i+1]:
    dailist[i+1]=max-Alist[i+1]
    #print ("dailist="+str(dailist[i+1]))

#print (Alist)
#print (dailist)
del dailist[-1]
print (sum(dailist))

Submission status: Accepted

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

Submission status: Accepted

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

total = 0

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

print(total)

Submission status: Accepted

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

cnt = 0
step = []
step_sum = 0

for i in range(0, N):
    if i == 0:
        step.append(0)
        continue
    diff = (An[i - 1] + step[i - 1]) - An[i]
    if diff <= 0:
        step.append(0)
    else:
        step.append(diff)

for num in step:
    step_sum += num

print(step_sum)

Submission status: Accepted

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           #takes array as input
    return list(map(int,stin().split()))
def matrix(n):
    #matrix input
    return [list(map(int,input().split()))for i in range(n)]

################################################
def calculate_sum(a, N): #sum of a to N
    # Number of multiples 
    m = N / a 
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
    # sum of multiples 
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j
# d = {}
 
"""*******************************************************"""
n = inin()
a = lin()

# m = max(a)
c, d = 0, 0

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = LI()
ans = 0
max_n = 0
for i in A:
    if i > max_n:
        max_n = i
    else:
        ans = ans + (max_n - i)
print(ans)

Submission status: Accepted

Solution code:
from sys import exit
n = int(input())
a = list(map(int, input().split()))
if n==1: print(0); exit()
al = [0 for _ in range(n)]
for index in range(n-1):
	person = a[index]
	person_behind = a[index+1]
	if person_behind < person:
		k = person - person_behind
		a[index+1] += k
		al[index+1] = k
print(sum(al))

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.readline
n = int(readline())
a = list(map(int, readline().split()))
x = 0
ans = 0
for i in a:
    if x > i:
        ans += (x-i)
    else:
        x = i
print(ans)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

p = 0
t = 0
for i in range(len(a)):
    if p > a[i]:
        add = p - a[i]
        t += add
        p = a[i] + add
    else:
        p = a[i]

print(t)

Submission status: Accepted

Solution code:
import math
import sys
#####sorting a dictionary by the values#####
def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))
##### naive method for testing prime or not#####
def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
#####Primes till Nth#####
def seive_primes(n):
    flag=[0]*(n+10)
    flag[1]=flag[0]=1
    i=2
    while i*i<=n+1:
        if flag[i]==0:
            j=i*i
            while j<=n+1:
                flag[j]=1
                j+=i
        i+=1
    return flag
#####greatest common divisor of two numbers#####
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
#####least common multiplyer of two numbers#####
def lcm(a,b):
    return (a*b)//gcd(a,b)
#####function that return all the letters#####
def alphabates():
    return "abcdefghijklmnopqrstuvwxyz"
#####binary search#####
def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1
#####Decimal to Binary(8 bits)#####
def Binary(n):
    if n==0:
        return "00000000"
    binary = "" 
    i = 0
    while n > 0 and i<=8: 
        s1 = str(int(n%2)) 
        binary = binary + s1 
        n /= 2
        i = i+1
        d = binary[::-1] 
    return d
#####quadratic#####
def qdrt(x,a,b,c):
    return a*x*x+b*x+c
#####taking an array/list as input#####
def inp():
    ls=list(map(int,input().split()))
    return ls
#####taking multiple inputs#####
def mult_inp():
    return map(int,input().split())
#####0-1 Knapsack#####
def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W + 1)] for x in range(n + 1)]  
    for i in range(n + 1): 
        for w in range(W + 1): 
            if i == 0 or w == 0: 
                K[i][w] = 0
            elif wt[i-1] <= w: 
                K[i][w] = max(val[i-1]+K[i-1][w-wt[i-1]],  K[i-1][w]) 
            else: 
                K[i][w] = K[i-1][w] 
  
    return K[n][W] 
#####Main function starts from here#####
n=int(input())
ls=inp()
boro=ls[0]
ans=0
for i in range(1,n):
    if ls[i]<boro:
        ans+=boro-ls[i]
    else:
        boro=ls[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

answer = 0
prev = 0
for idx, v in enumerate(li_a):
    if idx == 0:
        prev = v

    if prev > v:
        answer += (prev - v)
    else:
        prev = v
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(sum(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
import numpy as np
import time

# f = open("input.txt", "r")
# sys.stdin = f

n = int(input())
a = list(map(int, input().split()))
tmpMax = a[0]
ans = 0
for num in a:
	ans += max(0, tmpMax-num)
	if tmpMax < num:
		tmpMax = num
print(ans)

Submission status: Accepted

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

ans = 0
m = 0
for a in aa:
    if m > a:
        ans += (m-a)
    else:
        m = a
print(ans)

Submission status: Accepted

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

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

print(sum);

Submission status: Accepted

Solution code:
def step(N, A):
    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]
    return sum
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(step(N, A))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(l)):
    if m > l[i]:
        s += m - l[i]
    else:
        m = l[i]

print(s)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

lb = A[0]
ans = 0

for a in A:
  if lb < a:
    lb = a
  elif lb > a:
    ans += lb - a
    
print(ans)

Submission status: Accepted

Solution code:
# 問題：https://atcoder.jp/contests/abc176/tasks/abc176_c

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
def Ii():return int(sys.stdin.readline().rstrip())
def Mi():return map(int,sys.stdin.readline().split())
def Li():return list(map(int,sys.stdin.readline().split()))

n = Ii()
a = Li()
mx = 0
ans = 0
for i in a:
  if mx > i:
    ans += mx - i
  else:
    mx = i
    
    
print(ans)

Submission status: Accepted

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

tmax = A[0]
humidai = 0
humidai_sum = 0

for a in A :
  if tmax > a:
    humidai = tmax - a
    humidai_sum += humidai
  else :
    tmax = a
    
print(humidai_sum)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
cur = 0

for a in arr:
    if a < cur:
        ans+=cur-a
    else:
        cur = a

print(ans)

Submission status: Accepted

Solution code:
kazu=int(input())
sintyo=list(map(int, input().split())) 
ans=0
takasa=sintyo[0]
for x in range(kazu-1):
    takasa2=sintyo[x+1]
    if(takasa>takasa2):
        sabun=takasa-takasa2
        ans+=sabun
    if(takasa<takasa2):
        takasa=sintyo[x+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):
	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_lst = list(map(int, input().split()))
maximum = a_lst[0]
h_lst = []
for i in range(n):
    maximum = max(maximum, a_lst[i])
    h_lst.append(maximum - a_lst[i])
sum1 = sum(h_lst)
print(sum1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
m = dat[0]
res = 0
for x in dat:
    res += max(0, m - x)
    m = max(m, x)
print(res)

Submission status: Accepted

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

min_h, sum_h = 0, 0
for a in A:
  if min_h < a:
    min_h = a
  sum_h += min_h - a

print(sum_h)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#pylint: disable=missing-docstring,bad-whitespace,invalid-name,len-as-condition,trailing-newlines,multiple-statements,unnecessary-semicolon,trailing-whitespace,superfluous-parens
import math as M

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

num = input()
sum = 0
for ch in num :
    sum += ord(ch)-ord('0')
if sum%9==0 :
    print('Yes')
else :
    print('No')
exit()      

n,x,t = (map(int,input().split()))
print(t*(n//x) + t*(min(n%x,1)))
exit()

n = int(input())
a = list(map(int,input().split()))
cm = 1000
p1 = p2 = a[0]
for i in range(1,n):
    #print(i,a[i],p1,p2)
    if(a[i]>=p2):
        p2 = a[i]
    elif(a[i]<p2):
        cm += (cm//p1)*(p2-p1)
        p1 = p2 = a[i]
cm += (cm//p1)*(p2-p1)
print(cm)
exit()

cm = 1000
for i in range(0,40):
    st = cm//100
    cm -= st*100
    cm += st*200
print(cm)
print(M.log10(cm))
print(M.log2(10**16))
exit()

isPrime = [1]*101
isPrime[0]=isPrime[1]=0
for i in range(2,int(M.sqrt(101)+1)) :
    if not isPrime[i] : continue
    for j in range(i*i,101,i) :
        isPrime[j]=0

pa = 5
pb = 1
ctr= 0
for i in range(2,101) : 
    if not isPrime[i] : continue
    if i==5 : 
        ctr+=1
        continue
    if(ctr&1) : 
        pa *= i
    else : 
        pb *= i
    ctr+=1
print(pa,' ',pa<=2*(10**18))
print(pb,' ',pb<=2*(10**18))


#init pa = 5, pb = 1
#into b  2
#into a  3
#into a  7
#into b  11
#into a  13
#into b  17
#into a  19
#into b  23
#into a  29
#into b  31
#into a  37
#into b  41
#into a  43
#into b  47
#into a  53
#into b  59
#into a  61
#into b  67
#into a  71
#into b  73
#into a  79
#into b  83
#into a  89
#into b  97
#1931237034633205845   True
#1193829614179601806   True

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
 
total = 0
m = 0
 
for x in a:
  if m > x:
    total += m - x
  m = max(m,x)
  
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

prev = 0
sum = 0
for i in a:
    if (prev > i):
       sum += prev - i
    else:
        prev = i

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

ans = 0
cur = A[0]
for a in A[1:]:
    if cur > a:
        ans += cur - a
    else:
        cur = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N = INT()
A = LIST()

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

print(ans)

Submission status: Accepted

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

def main():
  n = int(input())
  la = list(map(int, input().strip().split()))
  taller = -1
  step = 0
  for a in la:
    taller = max(taller, a)
    if taller > a:
      step += taller - a
  print(step)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import sys

import math
def gcd(a,b):
	if a == 0:
		return b
	return gcd(b%a,a)

def solve(n):
	return n%9

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

if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
step,height = 0,0
for i in map(int,input().split()):
    if height>i:
        step+=height-i
    height = max(height,i)
print(step)

Submission status: Accepted

Solution code:
  
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


n = readint()
a = readints()

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

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  A = list(map(int, input().split()))
    
  m = A[0]
  S = 0

  for a in A:
      if m - a > 0:
          S += m - a
      if m < a:
          m = a

  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(1,N):
  diff = A[i-1] - A[i] if A[i-1] - A[i] > 0 else 0
  ans += diff
  A[i] += diff
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
l=list(map(int,input().split()))
s_1=sum(l)
n=len(l)

for i in range(n-1):
  if l[i]>l[i+1]:
    l[i+1]=l[i]
s_2=sum(l)

print(s_2-s_1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))
ans = 0
h = li[0]
for l in li[1:]:
    h = max(h,l)
    ans += h-l
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def maxof(x, y):
    if x > y:
        return x
    return y

n=int(input())
a = list(map(int, input().strip().split()))
total=0
max_num=0
for i in range(len(a)):
    max_num=maxof(max_num,a[i])
    total=total+max_num-a[i]
print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
before=A[0]
ans=0
for a in A[1:]:
    if before>a:#前の値のほうが大きい
        ans+=(before-a)
    else:
        before=a#値更新
print(ans)

Submission status: Accepted

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

total_height = 0

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

print(total_height)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N-1):
    v1, v2 = A[i], A[i+1]
    if v1<=v2:
        continue
    ans += v1-v2
    A[i+1] = v1
print(ans)

Submission status: Accepted

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

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))

    return N, A


def main(A: list) -> None:
    """
    メイン処理.

    Args:\n
        A (list): 身長(1 <= A_i <= 10**9)
    """
    # 求解処理
    ans = 0
    highest = 0
    for A_n in A:
        highest = max(highest, A_n)
        ans += max(highest - A_n, 0)

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, A = get_input()

    # メイン処理
    main(A)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A_list = [int(i) for i in input().split()]
pre = 0
count = 0
for p in A_list:
    if pre > p:
        count += pre - p
    else:
        pre = p
print(count)

Submission status: Accepted

Solution code:
def main():
    from sys import stdin
    readline = stdin.readline
    n = int(readline())
    A = list(map(int, readline().split()))

    tallest = A[0]
    cnt = 0

    for height in A:
        if tallest > height:
            cnt += tallest - height
        else:
            tallest = height

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

    tallest = A[0]
    cnt = 0

    for height in A:
        if tallest > height:
            cnt += tallest - height
        else:
            tallest = height

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

    tallest = 0
    ans = 0

    for height in A:
        tallest = max(tallest, height)
        ans += tallest - height

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import math

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

max_h = -1
ans = 0

for i in range(N):
    # 踏み台を設置する場合
    if max_h > A[i]:
        ans += max_h - A[i]
    elif max_h < A[i]:
        max_h = A[i]

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    tallest = A[0]
    ans = 0

    for height in A:
        if height < tallest:
            ans += tallest - height
        elif height > tallest:
            tallest = height

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0
max_height_now = 0
for index in range(num):
  height_now = int(height[index])
  if height_now >= max_height_now:
    max_height_now = height_now
  else:
    sum += max_height_now - height_now

print(sum)

Submission status: Accepted

Solution code:
# coding:utf-8
n = int(input())
a = list(map(int, input().split()))
ans = 0
tall = 0

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

print(ans)

Submission status: Accepted

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

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]:
        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()))

ol = a[0]
s = 0
for i in a:
  if ol < i:
    ol = i
  else:
    s += ol - i
print (s)

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
A=list(map(int,input().split()))
F=[]
for i in np.arange(len(A)-1):
    if(A[i]>A[i+1]):
        F.append(A[i]-A[i+1])
        A[i+1]=A[i]

print(sum(F))

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    m = A[0]
    S = 0

    for a in A:
        if m - a > 0:
            S += m - a
        if m < a:
            m = a
    
    print(S)


if __name__ == "__main__":
    main()