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:
input()
a_list = [int(x) for x in input().split()]
n = len(a_list)
i=1
mae_height = a_list[0]
fumidai_sum = 0
sa = 0
while i<n:
  if a_list[i] < mae_height:
    sa = mae_height - a_list[i]
    fumidai_sum += sa
    mae_height = a_list[i] + sa
  elif a_list[i] >= mae_height:
    mae_height = a_list[i]
  i+=1
print(fumidai_sum)

Submission status: Accepted

Solution code:
from sys import stdin, stdout
def gc(): return map(int, stdin.readline().strip().split())
def gar(): return list(map(int, stdin.readline().strip().split()))
def gs(): return stdin.readline().strip()
n = int(stdin.readline())
ar = gar()
ans = 0
p = ar[0]
for i in range(n):
    if ar[i] <= p:
        ans += p-ar[i]
        ar[i] = p
    else:
        p = ar[i]
print(ans)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
length = input()
lst = input()
lst = lst.split()
q = 0
for i in range(len(lst)):
    lst[i] = int(lst[i])
for i in range(int(length)-1):
    if lst[i] >= lst[i+1]:
        q += (lst[i]-lst[i+1])
        lst[i + 1]= lst[i]
print(q)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

len = len(A)
i = 1
dai = 0

while(i < len):
    if(A[i-1] > A[i]):
        dai += (A[i-1] - A[i])
        A[i] = A[i-1]
    i += 1

print(dai)

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:
if __name__=="__main__":
  n=int(input())
  a=[int(i) for i in input().split()]
  stools=0
  for i in range(1, n):
    if a[i]<a[i-1]:
      stools+=a[i-1]-a[i]
      a[i] += a[i-1] - a[i]
  print(stools)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = input().split()
ans = 0

current_max = 0

for i in range(len(A)):
  if int(A[i]) > current_max:
    current_max = int(A[i])
  else:
    ans += current_max - int(A[i])

print(ans)

Submission status: Accepted

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

A = 0
for i in range(0,N):
    A = A+L[i]

for i in range(0,N-1):
    if L[i+1]<L[i]:
        L[i+1] = L[i]
    else:
        pass

B = 0
for i in range(0,N):
    B = B+L[i]

print(B-A)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
strNums = input().split()
intNums = [int(i) for i in strNums]
step = []
initialValue = intNums[0]
for i in range(len(intNums)):
    if intNums[i] < initialValue:
        step.append(initialValue - intNums[i])
    else:
        step.append(0)
        initialValue = intNums[i]
print(sum(step))

Submission status: Accepted

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

for i in range(1,N):
  t = A[i-1]-A[i]
  if (t>=0):
    B.append(t)
    A[i] = A[i] + t
r = 0
for i in B:
  r = r + i
print(r)

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(i) for i in input().split()]
h_list = []
A_before = 0
for A in A_list:
    h = 0
    if A < A_before:
        h = A_before - A
    else:
        h = 0
    h_list.append(h)
    A_before = A + h
print(sum(h_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))
S = 0

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

print(S)

Submission status: Accepted

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

print(sum(m))

Submission status: Accepted

Solution code:
N = int(input())
high_list = list(map(int,input().split()))
l = high_list[0]
count = 0
for i in range(N-1):
    i_a = high_list[i]
    i_b = high_list[i+1]
    if(i_a > i_b):
        dai = i_a - i_b
        i_b = i_b + dai
        high_list[i+1] = i_b
        count += dai
print(count)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    A = read_list()
    pre = A[0]
    res = 0
    for a in A:
        res += max(0, pre - a)
        pre = max(a, pre)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input().rstrip())
member_str_list = input().rstrip().split(' ')
member_list = []

for member in member_str_list:
    member_list.append(int(member))

step_list = [0]
for i in range(len(member_list)):
    if i - 1 >= 0:
        front_member = member_list[i-1] + step_list[i-1]
        current_member = member_list[i]
        if front_member > current_member:
            step_list.append(front_member - current_member)
        else:
            step_list.append(0)

print(sum(step_list))

Submission status: Accepted

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

import sys
import math
from decimal import Decimal, ROUND_DOWN
from collections import deque
from itertools import combinations
import copy

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (n, ) = [int(e) for e in lines[0].split(' ')]
    a = [int(e) for e in lines[1].split(' ')]


    return (n, a)


def solve(n, a):

             
    current = a[0]   
    total = 0
    for i in range(n):

        tmp = a[i] - current
        if tmp < 0:
            total = total + (-tmp)
        
        if a[i] > current:
            current = a[i]


    return total
    


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = lr()
ans = 0
for i in range(n-1):
    if a[i+1] < a[i]:
        tmp = a[i]-a[i+1]
        ans += tmp
        a[i+1] = a[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(1000000)
from collections import deque

# 整数の入力
N = int(input())
#配列の入力
A = list(map(int, input().split()))
nmin = A[0]
su = 0
for ai in A:
    if ai < nmin:
        su += nmin - ai
    else:
        nmin = ai

print(su)

Submission status: Accepted

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

Amax = 0
ans = 0

for i in range(len(A)):
  if (Amax - A[i]) >= 0:
    ans += (Amax-A[i])
  elif (Amax - A[i])<0:
    Amax = A[i]
print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    mi = 0
    le = len(a)
    cost = 0
    for i in range(le-1):
        if mi > a[i+1] or a[i] > a[i+1]:
            mi = max(a[i], mi)
            cost += mi - a[i+1]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
l = int(input())
xs = [int(x) for x in input().split()]
step = 0
max = 0
for x in xs:
    # This x must be the max in the preceeding numbers
    if x > max:
        max = x
        continue  # Current max, so no step is needed
    step += max - x
print(step)

Submission status: Accepted

Solution code:
# input n
# input height

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

count = 0
i = 0


for i in range(n-1):
    if height[i] > height[i+1]:

        count += height[i] - height[i+1]
        height[i+1] += height[i] - height[i+1]


print(count)

Submission status: Accepted

Solution code:
x = int(input())
lst = list(map(int, input().split()))
i = 1
cnt = 0
a = lst[0]
if x == 1:
    print(0)
else:
    while True:
        b = lst[i]
        if b < a:
            cnt += abs(b - a)
            a = a
        else:
            a = b
        if i >= x - 1:
            break
        i += 1
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ppl=(input().split())
answer=0
if n==1:
    print(0)
for x in range(0,n-1):
    ppl[x]=int(ppl[x])
    ppl[x+1]=int(ppl[x+1])
    if ppl[x]>ppl[x+1]:
        answer+=ppl[x]-ppl[x+1]
        ppl[x+1]=ppl[x]
if(n!=1):
    print(answer)

Submission status: Accepted

Solution code:
N = int(input())
l = input().split()

cnt = 0
current = int(l[0])
for i in range(1, N):
    now = int(l[i])
    if now >= current:
        current = now
    if now < current:
        cnt += current - now

print(cnt)

Submission status: Accepted

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

for i in range(len(A)):
  if A[i] - old_a < 0:
    diff = (old_a - A[i])
    step += diff
  else:
    diff =0
  old_a = A[i] + diff
  
print(step)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
b=A[0]
c=0
d=len(A)

for i in range(1,d):
    if b>A[i]:
        c+=b-A[i]
    else:
        b=A[i]

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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


for i in range(len(a) - 1):
    if a[i + 1] < a[i]:
        l.append(a[i]-a[i + 1])
        a[i + 1] = a[i]


print(sum(l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
cm=0
for i in range(1,N):
    if A[i]<=A[i-1]:
        cm+=(A[i-1]-A[i])
        A[i]+=(A[i-1]-A[i])
print(cm)

Submission status: Accepted

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

goukei = 0
for i in range((n-1)):
    an01=an[i]-an[i+1]
    if  an01 >= 0:
        an[i+1] = an[i]
        goukei += an01

print(goukei)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s=list([0])
for i in range(N-1):
  if A[i]>A[i+1]:
    s.append(A[i]-A[i+1])
    A[i+1] = A[i]
  else:
    s.append(0)
print(sum(s))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    
    # 踏み台の高さを決める
    B = [0] * N
    
    B[0] = 0
    sum = 0
    for i in range(1, N):
        if A[i-1] + B[i-1] > A[i]:
            B[i] = A[i-1] + B[i-1] - A[i]
        else:
            B[i] = 0
            
        sum = sum + B[i]

    print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

lst = A[:]
B = [0]
for i in range(1, len(A)):
  if lst[i] < lst[i-1]:
	  B.append(lst[i-1] - lst[i])
	  lst[i] = lst[i-1]
  else:
    B.append(0)
    
print(sum(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())

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

sum=0

for i in range(1,len(h)):
  if h[i]-h[i-1]<0:
    sum=sum+abs(h[i]-h[i-1])
    h[i]=h[i]-(h[i]-h[i-1])

print(sum)

Submission status: Accepted

Solution code:
n=int(input())

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

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

Submission status: Accepted

Solution code:
import copy

N = int(input())
A = list(map(int, input().split()))
A2 = copy.copy(A)
B = [A[0]]

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()));ans=0
for i in range(n-1):
    f=a[i];b=a[i+1]
    if b<f:
        ans+=(f-b)
        a[i+1]+=(f-b)
print(ans)

Submission status: Accepted

Solution code:
#
# abc176 c
#
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """4
3 1 1 3"""
        output = """4"""
        self.assertIO(input, output)


def resolve():
    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)


if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
heigt = A[0]
for i in A:
    if heigt > i:
        ans += heigt - i
    else:
        heigt = i

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l1 = [0] * n
l2 = [int(n) for n in input().split()]
b = 0
for p in range(1, n):
    s = l2[b] - l2[p] + l1[b]
    if s > 0:
        l1[p] = s
    b = p
print(sum(l1))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,N):
    if tmp > A[i]:
        result = result + (tmp - A[i])
    else:
        tmp = A[i]

print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
count=0
for i in range(N):
  try:
    a = A[i]
    b = A[i+1]
    if a <= b:
      pass
    else:
      c = abs(a-b)
      A[i+1] = b+c
      count+=c
  except:
    pass
print(count)

Submission status: Accepted

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

sum = 0

for i in range(len(A)-1):
  if A[i+1] < A[i]:
    sum += A[i] - A[i+1]    
    A[i+1] = A[i]

  else:
    pass
print(sum)

Submission status: Accepted

Solution code:
# Q3
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from functools import lru_cache

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

N = I()
A_i = LI()

total_fumidai = 0
prev_num = -1
for num in A_i:
    if prev_num < 0:
        prev_num = num
        continue

    if prev_num <= num:
        prev_num = num
    else:
        total_fumidai += (prev_num - num)
print(total_fumidai)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
step = [0]
for k,i in enumerate(A[1:]):
    if A[k] >= i:
        step.append(A[k] - i)
        A[k+1] = A[k+1] + A[k] - i
print(sum(step))

Submission status: Accepted

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

for i in range(N):
  if A[i] > basic:
    basic = A[i]
  else:    
    count += basic - A[i]

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n=int(input())
a=get_array()
arr=a[0]
ans=0
for i in range(n-1):
    if a[i+1]<arr:
        ans+=(arr-a[i+1])
    else:
        arr=a[i+1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

step = []
sum = 0

for i in range(int(N) -1):
  if A[i] > A[i+1]:
    n = A[i] - A[i+1]
    A[i+1] = A[i+1] + n
    step.append(n)
  
for i in step:
  sum = sum + i

print(sum)

Submission status: Accepted

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

        ans+=arr[i-1]-arr[i]
        arr[i] = arr[i - 1]
print(ans)

Submission status: Accepted

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

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

print(sum_new-sum_org)

Submission status: Accepted

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

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:
def readInt():
    return list(map(int, input().split()))

n = int(input())
a = readInt()
ans = 0
height = a[0]
for i in range(1,len(a)):
    if a[i] <= height:
        ans += height - a[i]
    else:
        height = a[i]
print(ans)

Submission status: Accepted

Solution code:
#-------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

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")
 
# ------------------- fast io --------------------

from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[j - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] =  j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else '0' * (length - len(y)) + y


def solve(N, A):
    ans = 0
    for i in range(1, N):
        if A[i] <= A[i - 1]:
            ans += abs(A[i - 1] - A[i])
            A[i] += abs(A[i - 1] - A[i])
    #print(A)
    return ans


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

Submission status: Accepted

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