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()))
now = A[0]
ans = 0
for a in A:
    diff = now - a
    if diff > 0:
        ans += diff
    else:
        now = a
print(ans)

Submission status: Accepted

Solution code:

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

# def
def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return x

def str_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

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

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

def print_space(l):
    return print(" ".join([str(x) for x in l]))

# import
import numpy as np



# main code
N = int(input())
A = int_list()

ans = 0

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

now = A[0]

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

print(ans)

Submission status: Accepted

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

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = 0
prev = None
for a in A:
    if prev is None:
        prev = a
        continue
    if prev > a:
        diff = prev - a
    else:
        diff = 0
    ans += diff
    prev = a + diff

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = sum(a)

for i in range(N-1):
  if a[i]>a[i+1]:
    a[i+1]=a[i]
ans_1 = sum(a)
print(ans_1-ans)

Submission status: Accepted

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

mae = a[0]
ans = 0

for i in a:
    if mae > i:
        ans += mae - i
    else:
        mae = i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(len(a_list) - 1):
  if a_list[i] > a_list[i+1]:
    score += a_list[i] - a_list[i+1]
    a_list[i+1] = a_list[i]
    
print(score)

Submission status: Accepted

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

max_height = 0
ans = 0
for i in range(N):
    if A_list[i] > max_height:
        max_height = A_list[i]
    else:
        ans += max_height - A_list[i]
print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
A = map(int, sys.stdin.readline().split())

ans = 0
pre = 0
for a_i in A:
    # print("pre", pre, "a_i", a_i)
    if a_i < pre:
        diff = pre - a_i
        a_i += diff
        ans += diff
    pre = a_i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
from numba import vectorize

readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
a = np.array(readline().split(), dtype=np.int64)

@vectorize("i8(i8,i8)")
def larger (a,b):
    return max(a,b)

b = larger.accumulate(a)
ans = sum(b-a)
print(ans)

Submission status: Accepted

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

print(r)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

humidai = 0

for i in range(1,N):
  if A[i-1] > A[i]:
    humidai_plus = (A[i-1] - A[i])
    A[i] += humidai_plus
    humidai += humidai_plus
    
print(humidai)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    print(sum)

    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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:
import sys
from math import ceil

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    a_s = LI()
    big = 0
    sum_ = 0
    for i in a_s:
        if big > i:
            sum_ += big - i
        else:
            big = i
    print(sum_)

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):
  ans+=max(0,A[i-1]-A[i])
  A[i]=max(A[i-1],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())
a = list(map(int, input().split()))
now = a[0]
res = 0
for i in range(1, n):
    res += max(0, now - a[i])
    now = max(a[i], now)

print(res)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    # 数値取得サンプル
    n = int(input())
    a_s = list(map(int, input().split()))

    if n == 1:
        print(0)
        return

    ans = 0
    work = a_s[0]
    for a in a_s[1:]:
        if work - a > 0:
            ans += work - a
        else:
            work = a

    print(ans)


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターン
    def test_original(self):
        pass


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

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

fumidai=0

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

print(fumidai)

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

now_height = 0
count = 0
for i in range(N):
  if A[i] < now_height:
    dai = now_height - A[i]
    count += dai
  else:
    now_height = A[i]

print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

sum = 0
amax = A[0]

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

print(sum)

Submission status: Accepted

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

ans = 0
for i in range(len(As) - 1):
    if As[i] > As[i+1]:
        ans += As[i] - As[i+1]
        As[i+1] += As[i] - As[i+1]

# print('-----output-----')
print(ans)
# print(As)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
v = A[0]
for a in A:
    if a < v:
        ans += v - a
    v = max(v, a)
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]:
        cnt += A[i]-A[i+1]
        A[i+1] += A[i]-A[i+1]

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    sum = 0
    now = 0
    for a in a_list:
        if now <= a:
            now = a
        else:
            sum += now-a
    print(sum)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

sum = 0

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50



def run():
    N, *A = map(int, read().split())
    ans = 0
    cache = A[0]
    for a in A:
        if cache > a:
            ans += cache - a
        else:
            cache = a

    print(ans)


if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
n = int(input())
#L, R = list(map(int, input().split()))
#n, x, t = list(map(int, input().split()))
a = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
# n = int(input())
#l = list(map(int, input().split()))
#r = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
# data = [list(map(int, input().split())) for i in range(n)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
# for i in range(1, int(n**0.5)+1):
count = 0
#_max = 0
for i in range(1, n):
    if a[i-1] > a[i]:
        #_max = a[i]
        count += a[i-1]-a[i]
        a[i] = a[i-1]

print(count)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    count = 0
    for n in range(1, N):
        if A[n - 1] <= A[n]:
            continue
        need = A[n - 1] - A[n]
        count += need
        A[n] += need 
    print(count)

    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())
l = [int(x) for x in input().split()]
m = l[0]
ans = 0
for i in l:
    if m>i:
        
        ans+=m-i

    m = max(m,i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

total = 0

max_height = 0

for height in heights:
  if height > max_height:
    max_height = height
  else:
    total += max_height - height

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

before_max = A_list[0]
ans = 0

for i in range(N):
    #print(i,before_max)
    if A_list[i] < before_max:
        ans += before_max - A_list[i]
    elif A_list[i] > before_max:
        before_max = A_list[i]
        
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(map(int, input().split()))
now_max = 0
ans = 0
for i in A:
    if now_max < i:
        now_max = i
    elif now_max > i:
        ans += (now_max - 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(1, 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()))
ans = 0
if N==1:
    print(0)
    quit()
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=[int(i) for i in input().split()]
x=min(a)
for i in range(n):
    a[i]-=x
ans=chk=0
chk=a[0]
for i in range(n):
    if a[i]<chk:
        ans+=chk-a[i]
    else:
        chk=a[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))

total = 0
for i in range(1, n):
    if a_list[i - 1] > a_list[i]:
        total += a_list[i - 1] - a_list[i]
        a_list[i] = a_list[i - 1]

print(total)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

print a

Submission status: Accepted

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


ans=[0 for i in range(n)]
ans[0]=0



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

print(sum(ans))

Submission status: Accepted

Solution code:
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

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

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()

######################################

N = In.int()
A = In.ints()

maxYet = 0

ans = 0

for h in A:
    if maxYet>h:
        ans+=maxYet-h
    maxYet = max(h, maxYet)

Out.int(ans)

######################################

if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Submission status: Accepted

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

fumidai = 0
tallest = lst[0]
for num in lst[1:]:
    if num < tallest:
        fumidai += tallest - num
    elif num > tallest:
        tallest = num
print(fumidai)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Accepted

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

res = 0
mi = 0
for aa in A:
    mi = max(mi, aa)
    res += mi - aa 

print(res)

Submission status: Accepted

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

ans = [0]*N
tmp = A[0] #一個前の高さ
for i in range(1, N):
    if A[i] >= tmp:
        tmp = A[i]
    else: #台をおく必要がある
        ans[i] = tmp - A[i] #tmpに合わせたからtmpは変えない

print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

for i in range(1,N) :
    if A[i-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() ))

ret = 0
bafore = A[0]
for a in A:
  if bafore > a:
    ret += bafore - a
  bafore = max( a, bafore )
    
print( ret )

Submission status: Accepted

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

Submission status: Accepted

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

heighest = 0
humi =[]

for i in range(n):
  if a[i] > heighest:
    heighest = a[i]
  elif a[i]<heighest:
    sa = heighest-a[i]
    humi.append(sa)
print(sum(humi))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    step = [0 for i in range(N)]

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

    ans = sum(step)
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

maxh = L[0]
sum1 = 0
for i in range(1,n):
    if L[i] > maxh:
        maxh = L[i]
    else:
        sum1 += maxh-L[i]
print(sum1)

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

current = a[0]

sum_ = 0

for aa in a:
    if current <= aa:
        current = aa
    else:
        sum_ += current - aa


print(sum_)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！
n = I()
a = LI()
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:
import numpy as np

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

r=0
for ii in np.arange(1,n):
  if a[ii]<a[ii-1]:
    r+=a[ii-1]-a[ii]
    a[ii]=a[ii-1]

print(r)

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:
#!/usr/bin/env python3
import sys
import numpy as np


def solve(N: int, A: "List[int]"):
    highest = A[0]
    ans = 0

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

    print(ans)

    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:
def main():
    n = int(input())
    A = list(map(int,input().split()))

    ans = 0
    temp = A[0]
    for a in A[1:]:
        if a < temp:
            ans+=temp-a
        else:
            temp = a
    
    print(ans)
main()

Submission status: Accepted

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

b = 0
ans = 0
for v in A:
    if v < b:
        ans += b - v
    else:
        b = v

print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(1, N):
    front = A[i - 1]
    back = A[i]

    if front > back:
        diff = front-back 
        cnt += diff
        A[i] = A[i] + diff

    # print(A)
print(cnt)