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

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

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(a)
print(c)

Submission status: Accepted

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

dai = 0

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

print(dai)

Submission status: Accepted

Solution code:

n = int(input())
A = list(map(int,input().split()))
ans = 0 
temp = 0
for i in range(n):
    if A[i]<temp:
        ans += temp-A[i]
    else:
        temp = max(temp,A[i])

print(ans)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
max_a = 0
for ai in A:
    max_a = max(ai, max_a)
    ans += (max_a-ai)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

if __name__ == '__main__':
    N = int(sys.stdin.readline())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    
    n = 0
    for i in range(1, len(A)):
        if A[i-1] > A[i]:
            n += A[i-1] - A[i]
            A[i] = A[i-1]
    print(n)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

m = 0
ans = 0
for ai in a:
    m = max(m, ai)
    ans += m - ai

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
# 明らかに背の高い人が前に存在しない
total = 0
now = A[0]  # 基準
for i in range(1, N):
    # 基準の方が高い場合
    if now >= A[i]:
        total += now - A[i]
        # この場合基準はそのまま
    else:
        # 基準の方が低いなら基準を更新
        now = A[i]
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def i():
	return int(input())
def s():
	return input()
def mp():
	return map(int,input().split())

a = input()
b = list(mp())
mx = b[0]
ans =0
for i in range(1,len(b)):
	if b[i]<mx:ans+=mx-b[i]
	else:mx = b[i]
print(ans)

Submission status: Accepted

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    n = int(input())
    ts = map(int, input().split())

    total = 0
    pre = 0
    for t in ts:
        d = max(pre - t, 0)
        total += d
        pre = t + d

    return total


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

max_h=a[0]
ans=0

for i in range(1,n):
    a1 = a[i]
    if a1 >= max_h:
        max_h = a1
    else:
        ans += max_h - a1

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
aL = list(map(int, input().split(" ")))

mx = aL[0]
ans = 0
for a in aL[1:]:
    if mx > a:
        ans += mx - a
    else:
        mx = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

#n = int(input())
#l = list(map(int, input().split()))

'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''
n=int(input())
a=list(map(int, input().split()))

cnt=0
for i in range(1,n):
    if a[i-1]>a[i]:
        cnt+=a[i-1]-a[i]
        a[i]+=a[i-1]-a[i]
    #print(a)
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] < a[i-1]:
        dum = a[i-1]-a[i]
        a[i] = a[i-1]
        ans += dum
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#abc176-c
import sys,math,collections,itertools
input = sys.stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

print(out)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
nums = map(int, input().split(" "))

out = 0
for i, num in enumerate(nums):
    if i == 0:
        cur_max = num
    else:
        if cur_max > num:
            out += cur_max - num
        else:
            cur_max = num

print(out)

Submission status: Accepted

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

Submission status: Accepted

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

L = 0
ans = 0
for a in A:
    if L > a:
        ans += L - a
    else:
        L = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase


"""def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)"""

"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""



def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    ar=inpt()
    mx=ar[0]
    ans=0
    for i in range(1,n):
        if(ar[i]<mx):
            ans+=mx-ar[i]
        mx=max(ar[i],mx)
    print(ans)

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

Submission status: Accepted

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

maxh = 0
ans = 0
for h in a:
    if maxh > h:
        ans += maxh - h
    maxh = max(maxh, h)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cur_max = a[0]
ret = 0
for i in a[1::]:
    if i < cur_max:
        ret += cur_max - i
    else:
        cur_max = i

print(ret)

Submission status: Accepted

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

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())
l=list(map(int,input().split()))
def solve(n,l):
     if n==1:
        return 0
     ans=0
     m=0
     for i in range(n):
         if l[i]>=m:
            m=l[i]
         else:
            ans+=max(0,m-l[i])
     return ans
print(solve(n,l))

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]:
#        print("i=",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 = [int(x) for x 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:
N = int(input())
A = list(map(int, input().split()))

sum_dai = 0
for i in range(1, N):
    diff = A[i] - A[i - 1]
    if diff < 0:
        sum_dai += -diff  # 前の人と高さを一緒にする
        A[i] = A[i - 1]
print(sum_dai)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in 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:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    n = II()
    aa = [0] + MII()
    sumv = 0
    for i in range(n):
        if aa[i] > aa[i+1]:
            delta = aa[i] - aa[i+1]
            sumv += delta
            aa[i+1] += delta
    print(sumv)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
ma = A[0]
for i in range(1, n):
    if A[i] < ma:
        res += (ma - A[i])
    ma = max(ma, A[i])

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N-1):

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

print(ans)

Submission status: Accepted

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

ans = 0
bfr = a[0]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    currentH = 0
    add = 0
    for i, a in enumerate(A):
        if a >= currentH:
            currentH = a
        else:
            add += currentH - a
    print(add)

    return 0

if __name__ == "__main__":
    solve()

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

Submission status: Accepted

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret
######################################################################################################
N=int(input())
A=inputintlist()
mmax=A[0]
ans=0
for a in A[1:]:
    if mmax>a:
        ans+=mmax-a
    mmax=max(mmax,a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    a_max=a[0]
    ans=0
    for ai in a[1:]:
        if ai<a_max:
            ans+=a_max-ai
        a_max=max(a_max,ai)
    return ans

if __name__=='__main__':
    ans=main()
    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:
ans = 0
komi_max = 0

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

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

print(ans)

Submission status: Accepted

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

mmax = 0
ans = 0
for a in A:
  if mmax >= a:
    ans += mmax-a
  else:
    mmax = a

print(ans)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    
    t_max = 1
    h = 0
    for ai in A:
        if ai >= t_max:
            t_max = ai
        else:
            h += t_max-ai

    print(h)

        
    

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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()))
ans = 0

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

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    ans=0
    for i in range(1,n):
        diff = arr[i-1]-arr[i]
        if diff > 0:
            ans+=diff
            arr[i]+=diff
    print(ans)
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
import sys
import  math
import fractions
from collections import defaultdict
import copy
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
ans=0
now=A[0]
for i in range(1,N):
    if(now<A[i]):
        now=A[i]
        continue
    else:
        ans+=now-A[i]

print(ans)

Submission status: Accepted

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

min_value = a[0]
result = 0

for x in a:
  diff = min_value - x
  if x < min_value:
    result += diff
    min_value = x + diff
  else:
    min_value = x
    
print(result)

Submission status: Accepted

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

h = 0
tot = 0

for a in A:
    tot += max(h-a, 0)
    h = max(h, a)
print(tot)