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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
A_list = input().split()
pre = 0
counter = 0

for i in A_list:
  if i == A_list[0]:
    pre = int(i)
    continue
  if pre > int(i):
    dist = pre - int(i)
    counter += dist
    pre = int(i) + dist
    continue
  pre = int(i)

print(counter)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in 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()))

l = [0]*N

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

print(sum(l))

Submission status: Accepted

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

ans = 0
max = a[0]

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

    if max < a[i]:
        max = a[i]

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split( )))
#print(a)
x = 0
front = 0
step = 0
for height in a :
    #print('ooo', height)
    if front > height :
        x = front - height
        #print('ppp',x)
        step += x
    if front < height:
        front = height
print(step)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt, high = 0, 0
for ai in a:
    cnt += max(0, high - ai)
    high = max(high, ai)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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()))
iter_A = iter(A)
ans = 0
for i in range(1, len(A)):
    if A[i-1] > A[i]:
        ans += A[i-1] - A[i]
        A[i] = A[i-1]
print(ans)

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n = int(input())
    a = list(map(int, input().split()))

    #output
    answer = 0
    for i in range(1, n):
        if a[i] < a[i-1]:
            answer += a[i-1]-a[i]
            a[i] = a[i-1]

    print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
array = []
array+=map(int,input().split())
ans=0
premax=0
for n in range(N):
    premax=max(array[n],premax)
    ans+=premax-array[n]
print(ans)

Submission status: Accepted

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

def tako(An):
    stool = 0
    Anstool = [An[0]]
    for n in range(len(An)-1):
        pers = An[n+1]
        fr = Anstool[n]
        if fr > pers:
            stool += fr - pers
            pers = fr
        Anstool.append(pers)
    return stool

print(tako(An))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
maxa=0
total=0
for i in a:
  maxa=max(maxa,i)
  total=total+maxa-i
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

sum=0

for i in range(0,len(length)-1):
  if length[i]>length[i+1]:
    dai=length[i]-length[i+1]
    length[i+1]+=dai
    sum+=dai
  else:
    continue

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
a =list(map(int,input().split()))
maxVal = 0
total = 0
for num in a:
    maxVal = max(maxVal,num)
    total += maxVal - num
print(total)

Submission status: Accepted

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

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

print(step)

Submission status: Accepted

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

for i in range(1, len(A)):
  if A[i] < h:
    S += (h - A[i])
    h = max(h, A[i-1])
  else:
    h = A[i]

print(S)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def INT():
    return int(input())

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

def LI():
    return list(map(int, input().split()))

N = INT()
A = LI()

max_now = A[0]
ans = 0

for Ai in A:
    max_now = max(max_now, Ai)
    ans += max_now - Ai
    
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    m = 0
    ans = 0
    for i in range(N) :
        m = max(m,A[i])
        if(A[i] < m):
            ans += m - A[i]
    print(ans)


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:
def solve(n, a_list):
    cum_max = 0
    res = 0
    for i in range(n):
        a = a_list[i]
        if a < cum_max:
            res += cum_max - a
        cum_max = max(a, cum_max)
    return res


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    res = solve(n, a_list)
    print(res)


def test():
    assert solve(5, [2, 1, 5, 4, 3]) == 4
    assert solve(5, [3, 3, 3, 3, 3]) == 0


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

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


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

    arr = A[0]
    ans = 0
    for i in A:
        ans += abs(arr - i) if i <= arr else 0
        arr = max(arr, i)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_ = int(input())
s = h = 0
for a in map(int, input().split()):
    h = max(h, a)
    s += h - a
print(s)

Submission status: Accepted

Solution code:
a = int(input())
b = input().split()

now_max = 0
score = 0
for i in list(b):
  now = int(i)
  if now > now_max :
    now_max = now
  else:
    score += now_max - now

print(score)

Submission status: Accepted

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

hight_now = a[0]
ans = 0

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

print(ans)

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

Submission status: Accepted

Solution code:
N = int(input())

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

res = 0

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

for x in a:
    ans += max(0,h-x)
    h = max(h,x)
print(ans)

Submission status: Accepted

Solution code:
import sys

# C - Step
N = int(input())
A = list(map(int, input().split()))
height = A[0]
ans = 0

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

print(ans)

Submission status: Accepted

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

ans = 0
x = a[0]
for v in a[1:]:
    if v < x:
        ans += x - v
    else:
        x = v
print(ans)

Submission status: Accepted

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

x = A[0]
ct = 0
for a in A:
    if x < a:
        x = a
    elif x > a:
        ct += (x-a)
print(ct)

Submission status: Accepted

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

step_ttl = 0
step = 0
for i, a in enumerate(a_list):
  if i == 0:
    continue
  if a_list[i-1] + step < a:
    step = 0
    continue
  if a < step + a_list[i-1]:
    step = (step + a_list[i-1]) - a
  step_ttl += step

print(step_ttl)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
z=a[0]
b=0
for q in a:
  if z>q:
    b+=(z-q)
  z=max(z,q)
print(b)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-08-23 22:55
# @url:https://atcoder.jp/contests/abc176/tasks/abc176_c
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio

BUFSIZE = 8192

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")
# ------------------------------
def main():
    n=int(input())
    A=list(map(int,input().split()))
    mx=A[0]
    ans=0
    for a in A:
        mx=max(a,mx)
        ans+=(mx-a)
    print (ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n= int(input())
num=[ int (x) for x in input().split()]
pp= num[0]
eq=0
for x in range(1,n):
    if pp> num[x]:
        eq+= pp-num[x]

    else:
        pp=num[x]

print(eq)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n = ini()
a = inl()
ans = 0
tall = 0
for i in range(n):
    ans += max(0, tall - a[i])
    tall = max(tall, a[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

#front_numの初期値は最初の人
front_num = A[0]

for i in range(1,n):
  if front_num > A[i]:
    dif = front_num - A[i]
    ans += dif
  else:
    #もしA[i]が既存の中で最高だったら、front_numを更新する。
    front_num = A[i] 
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(c_step())

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    N = int(input())
    nums = list(map(int, input().split(' ')))
    n_max = nums[0]
    total = 0
    for i in nums:
        diff = n_max-i
        if diff<0:
            n_max=i
            diff=0
        total += diff
    print(total)

if __name__=="__main__":
  main()

Submission status: Accepted

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

ans = 0
cur_max = 0

for x in a:
    if cur_max > x:
        ans += cur_max - x
    cur_max = max(cur_max, x)

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:
tool=0
n=int(input())
arr=list(map(int,input().split()))
maxi=arr[0]
for i in range(1,len(arr)):
  if arr[i]<maxi:
    tool+=(maxi-arr[i])
  else:
    maxi=arr[i]
print (tool)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = [list(map(int, input().split())) for i in range(2)]
num = 0
for i in range(0, s[0][0]-1):
  if s[1][i]>s[1][i+1]:
    num += abs(s[1][i]-s[1][i+1])
    s[1][i+1]=s[1][i]
print(num)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N-1):
    if sintyo_list[i+1] < sintyo_list[i]:
        ans += sintyo_list[i] - sintyo_list[i+1]
        sintyo_list[i+1] = sintyo_list[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
max = A[0]

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = prev = 0
for a in map(int, input().split()):
    if prev <= a:
        prev = a
        continue
    ans += prev-a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
height = 0

for i in range(N):
    if A[i] < height:
        ans += height - A[i]
    else:
        height = A[i]

print(ans)

Submission status: Accepted

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

dai=[0 for _ in range(A)]

for i, l in enumerate(B):
  if i==0:
    continue
  if B[i-1]-B[i]>0:
    dai[i]=B[i-1]-B[i]
    B[i]+=B[i-1]-B[i]

print(sum(dai))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
if __name__=="__main__":
  N=int(input())
  x=[int(i) for i in input().split()]
  ans=0
  for i in range(1,len(x)):
    if x[i]<x[i-1]:
      #print(x[i-1]-x[i])
      ans+=(x[i-1]-x[i])
      x[i]+=(x[i-1]-x[i])
  print(ans)

Submission status: Accepted

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

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

print(step)

Submission status: Accepted

Solution code:
import sys
cin = sys.stdin.readline
n = cin()
height = map(int, cin().split())
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)

Submission status: Accepted

Solution code:
import sys
cin = sys.stdin.readline
n = int(cin())
height = map(int, cin().split())
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)

Submission status: Accepted

Solution code:
import sys
cin = sys.stdin.readline
n = int(cin())
height = cin().split()
last, ans = 0, 0
for i in height:
    diff = last - int(i)
    if diff > 0:
        ans += diff
        continue
    last = int(i)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
height = 0
for i in range(n-1):
  height = max(height,A[i])
  ans += max(0,height-A[i+1])
  
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()))
S = 0
n = A[0]
for i in A[1:]:
    if n < i:
        n = i
    elif n == i:
        continue
    else:
        S += n - i
print(S)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
heights = list(map(int, input().split()))
m = heights[0]
total = 0
for i in range(1,n):
  if heights[i] < m:
    total += (m-heights[i])
  elif heights[i] > m:
    m = heights[i]

print(total)

Submission status: Accepted

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

ans=0
chighest=0
for Ai in As:
    if Ai > chighest:
        chighest = Ai
    else:
        ans += chighest-Ai
print(ans)

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n = int(input())
seq = [int(x) for x in input().split()]
ans = 0
curmin = seq[0]
for i in xrange(1,n):
    if seq[i] < curmin:
        ans += curmin - seq[i]
    else:
        curmin = seq[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = input().split(" ")
A = [int(n) for n in A]

ans = 0

for i in range(N): 
  if i == 0:
    max = A[i]
  elif i > 0 and max >A[i]:
    dif = max - A[i]
    ans += dif
  elif i > 0 and max < A[i]:
    max = A[i]

print(ans)

Submission status: Accepted

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

max_A = 0
ans = 0

for i in range(n):
    max_A = max(A[i],max_A)
    ans += max_A - A[i]

print(ans)

Submission status: Accepted

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

base = a[0]
ans = 0;

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

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().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)


if __name__ == '__main__':
    solve()

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)