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:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

c = INF
B = []

tmp = A[0]

for i, a in enumerate(A):
    if i == 0:
        tmp = A[0]
        B.append(tmp)
    else:
        if tmp >= A[i]:
            B.append(tmp)
        else:
            tmp = A[i]
            B.append(tmp)

print(sum(B) - sum(A))

    # if a <= c:
    #     c = a
    #     B.append(c)
    # else:
    #     B.append(c)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
A_max = 0
for a in A:
    A_max = max(A_max, a)
    ans += abs(A_max - a)
print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = inpl()

ans = 0
mini = 0
for i in range(n):
    if A[i] < mini:
        ans += mini - A[i]
    mini = max(A[i],mini)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
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()))

ans = 0
prev = a[0]
for e in a:
  if prev - e > 0:
    ans += prev - e
  else:
    prev = e

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

tmp_top = A[0]
ans = 0

for A_i in A[1:]:
    if A_i > tmp_top:
        tmp_top = A_i
    else:
        ans += tmp_top - A_i

print(ans)

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 now > a[i]:
        ans += now - a[i]
    now = max(now, a[i])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
highest = a[0]

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

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

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

    res = 0

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

maxh=A[0]
stepsum=0

for i in range(1, N):
	step = max(maxh - A[i],0)
	maxh = step + A[i]
	stepsum += step
print(stepsum)

Submission status: Accepted

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

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

Submission status: Accepted

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

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    total = 0 # 踏み台の高さ
    pre_a = As[0]
    for i in range(1, N):
        diff = pre_a - As[i]
        if diff > 0:
            # 前の人のほうが高い
            total += diff
            As[i] += diff
        else:
            # 後ろの人のほうが高い
            pass
        pre_a = As[i]

    print(total)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    ans = 0
    h = A[0]
    for a in A:
        if h > a:
            ans += h - a
        h = max(h, a)
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


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


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Accepted

Solution code:
N,*A = map(int,open(0).read().split())
a_max = A[0]
ans = 0
for a in A:
  if a_max > a:
    ans += a_max - a
  elif a_max < a:
    a_max = a
print(ans)

Submission status: Accepted

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

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

ans=0

a0=A[0]
for a in A[1:]:
    if a < a0:
        ans+=a0-a
    else:
        a0=a

print(ans)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
total=0
height=A[0]
for i in A:
  if i<=height:
    total+=(height-i)
  else:
    height=i
print(total)

Submission status: Accepted

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

print(sum_step)

Submission status: Accepted

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

    ans = 0
    for i in range(N-1):
        if A[i] <= A[i+1]:
            continue
        else:
            diff = A[i] - A[i+1]
            ans += diff
            A[i+1] = A[i]
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a = input().split(" ")
lista = [int(n) for n in a]
kotae=0
for i in range(n-1):
  if lista[i]>lista[i+1]:
    kotae+=lista[i]-lista[i+1]
    lista[i+1]=lista[i]
print(kotae)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
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())
def main():
    n = I()
    a = LI()
    bemax = a[0]
    ans = 0
    for i in a[1:]:
        if i > bemax:
            bemax = i
        elif i < bemax:
            ans += bemax - i
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    n=int(input())
    a=list(map(int,input().split()))
    maxheight=0
    total=0
    for i in range(n):
        if a[i]<maxheight:
            total+=maxheight-a[i]
        maxheight=max(a[i],maxheight)
    print(total)




if __name__ == "__main__":

    main()

Submission status: Accepted

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

n = int(input())
A = list(map(int, input().split()))
#n, m = map(int, input().split())
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
#readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

ans = 0
max = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

X = [0]*N
maxtall = 0
total = 0
for i in range(N):
    if A[i] > maxtall:
        maxtall = A[i]
    else:
        total += maxtall - A[i]
print(total)

Submission status: Accepted

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

ans = 0
m = A[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:
import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    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
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

#maincode-------------------------------------------------
n = ini()
a = inl()
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()))

step = 0

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

print(step)

Submission status: Accepted

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

s = 0
hi = a[0]
for ai in a:
    if hi > ai:
        s += (hi - ai)
    else:
        hi = ai
print(s)

Submission status: Accepted

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

ans = 0
before = A[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import ceil
def mlt(): return map(int, input().split())


x = int(input())
s = list(mlt())

res = 0

for n in range(1, x):
    if s[n] < s[n-1]:
        d = s[n-1]-s[n]
        res += d
        s[n] += d

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

    else:
        som = som+(m-l[i])

print(som)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int, input().split())
newa=[]
latest=0
for i in range(n):
    if a[i]>latest:
        latest=a[i]
    newa.append(latest)
ans=[newa[i]-a[i] for i in range(n)]
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

current_max = 0

answer = 0
for a in A:
    if a < current_max:
        answer += current_max - a
    current_max = max(current_max, a)

print(answer)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    arr = list(map(int,input().split()))
    total = 0
    max_val = 0
    for i in arr:
        if i < max_val:
            total += max_val-i

        max_val = max(max_val,i)

    print(total)

main()

Submission status: Accepted

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

for i in range(N-1):
  if a[i] <= a[int(i+1)]:
    pass
  else:
    b = b + (a[i] - a[int(i+1)])
    a[int(i+1)] = a[i]

print(b)

Submission status: Accepted

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

Submission status: Accepted

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

before = A[0]
total = 0
for i in range(1,N):
    dif = A[i] - before
    if dif>=0:
        before = A[i]
    else:
        total -= dif
print(total)

Submission status: Accepted

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

t = a[0]
ans = 0

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

ans=0
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 = [int(i) for i in input().split()]
cur_height = 0
ans = 0
for i in range(n):
    if A[i] <= cur_height:
        ans += cur_height - A[i]
    else:
        cur_height = A[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 m > A[i]:
        ans += m - A[i]
    m = max(m, A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

before=0
ans=[]
for i in A:
  ans.append(max(0,before-i))
  before=i+ans[-1]
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# abc176_d.py
N = int(input())
A = list(map(int,input().split()))
m = 0
ans = 0
for i in A:
    if m < i:
        m = i
    ans += max(0,m-i)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()


NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

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

    highest = 0

    step_sum = 0

    for a in A:
        if highest < a:
            highest = a
        else:
            step_sum += highest - a
    print(step_sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

temp = alist[0]
count = 0
for a in alist[1:]:
    if a < temp:
        count += temp - a
    elif a > temp:
        temp = a

print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

print(s)

Submission status: Accepted

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

tmp = a[0]
ans = 0
for i in a[1:]:
    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()))

M = A[0]
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(p)

Submission status: Accepted

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

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

print(fumifumi)

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
aaa = list(map(int, input().split()))
now = aaa[0]
acc = 0
for a in aaa:
    if a >= now:
        now = a
    else:
        acc += now - a
print(acc)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
    こんにちは。これはクシャルです
    
    Author : thekushalghosh (クシャル)
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
    

    
def solve():
    n = inp()
    a = inlt()
    q = 0
    w = 0
    for i in range(len(a)):
        if a[i] < q:
            w = w + q - a[i]
        else:
            q = a[i]
    print(w)
    
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
        sys.stdout.close()
    
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
    q = [0] * max(a)
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)

def counter_elements(a):
    q = dict()
    for i in range(len(a)):
        if a[i] not in q:
            q[a[i]] = 0
        q[a[i]] = q[a[i]] + 1
    return(q)

def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97],1
    return(q)
 
def factors(n):
    q = []
    for i in range(1,int(n ** 0.5) + 1):
        if n % i == 0: q.append(i); q.append(n // i)
    return(list(sorted(list(set(q)))))
 
def prime_factors(n):
    q = []
    while n % 2 == 0: q.append(2); n = n // 2
    for i in range(3,int(n ** 0.5) + 1,2):
        while n % i == 0: q.append(i); n = n // i
    if n > 2: q.append(n)
    return(list(sorted(q)))

def transpose(a):
    n,m = len(a),len(a[0])
    b = [[0] * n for i in range(m)]
    for i in range(m): 
        for j in range(n): 
            b[i][j] = a[j][i]
    return(b)

#-----------------------------------------------------------------------#
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
main()

Submission status: Accepted

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

stand = 0
minimum = A[0]
for a in A[1:]:
    if a < minimum:
        stand += minimum - a
    else:
        minimum = a
print(stand)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
Created on 2020/08/22

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

  N=int(pin())
  A=list(map(int,pin().split()))
  now=0
  ans=0
  for i in A:
    if now>i:
      ans+=now-i 
    else:
      now=i
  print(ans)
  return 
main()

Submission status: Accepted

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

Submission status: Accepted

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

ma_h=0
g=0

for i in range(n):
  if ma_h>l[i]:
    g=g+ma_h-l[i]
  else:
    ma_h=l[i]
print(g)