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

hmax=A[0]
hsum=0

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

print(hsum)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
list1 = list(map(int,input().split()))
Sum = 0
M = 0
for i in range(N):
    if M > list1[i]:
        Sum += abs(M - list1[i])
    M = max(M, list1[i])
print(Sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import math
import itertools


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()
    AArray = LI()
    ans = 0
    max_sintyou = 0

    for A in AArray:
        if max_sintyou > A:
            sa = max_sintyou-A
            ans += sa
            A += sa

        max_sintyou = max(A, max_sintyou)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
m = A[0]
preMax = A[0]
ans = 0

for a in A:
    if preMax < a:
        preMax = a
        m = a 
    if a < m:
        ans += abs(a-m)
    preMax = max(a, preMax)
print(ans)

Submission status: Accepted

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

prev = A[0]
sum_ = 0
for a in A[1:]:
    if a >= prev:
        d = 0
    else:
        d = prev - a

    sum_ += d
    prev = a + d

print(sum_)

Submission status: Accepted

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

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

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, gcd
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 functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    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]

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

ans = 0

if n==1:
	print(ans)
	exit()

for i in range(1,len(a)):
	t = max(a[i],a[i-1])
	ans += max(0, t-a[i])
	a[i] = t

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
tmpmax = A[0]
for a in A:
    if a < tmpmax:
        ans += tmpmax - a
    elif a > tmpmax:
        tmpmax = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 整数1個だけ
N = int(input().strip())

# 別変数の整数複数個
A = [int(x) for x in input().strip().split(" ")]

cnt = 0

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N = int(input())
A = [int(i) for i in input().split()]
ans = 0
temp = A[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
before = sum(A)
former = A[0]
for i in range(n):
    if A[i] < former:
        A[i] = former
    else:
        former= A[i]
print(sum(A) - before)

Submission status: Accepted

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

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    N = readint()
    A = list(readnums())
    m = A[0]
    ans = 0
    for i in range(1, N):
        if m < A[i]:
            m = A[i]
        else:
            ans += m - A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
sum=0
for i in range(len(a)-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())
A = list(map(int, input().split()))

cnt = 0
m = A[0]
for i in range(1, N):
    sub = max(A[i-1], m) - A[i]
    if sub > 0:
        cnt += sub
    else:
        m = A[i]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

a_tmp = a_list[0]

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

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
maxNum = 0
lis2 = []
for i in range(n):
    a = lis[i]
    maxNum = max(maxNum,a)
    lis2.append(maxNum)
print(sum(lis2)-sum(lis))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2020/08/22
# AtCoder Beginner Contest 176 - C

# Input
n = int(input())
a = list(map(int,input().split()))
alen = len(a)

# Calc
hmax = 0
fsum = 0
for i in range(alen):
    if hmax < a[i]:
        hmax = a[i]
    fsum = fsum + (hmax - a[i])

# Output
print(fsum)

Submission status: Accepted

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

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

Submission status: Accepted

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

s = 0
x = 0
for i in A:
    if i < x:
        s += x - i
    else:
        x = i
print(s)

Submission status: Accepted

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

tallest= 0
ans = 0
for person in a_s:
	if person>tallest:
		tallest= person
	else:
		ans+= tallest-person
print(ans)

Submission status: Accepted

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

ans = 0

if n == 1:
    print(0)
if n != 1:
    for i in range(1,n):
        if c[i] < c[i-1]:
            ans += abs(c[i-1]-c[i])
            c[i] = c[i-1]
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
ans=0
maxheight=0
for i in range(N):
    #print(maxheight-A[i])
    ans+=max(0,maxheight-A[i])
    maxheight=max(maxheight,A[i])

print(ans)

Submission status: Accepted

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

n = 0
count = 0

for a in A:
  if n > a:
    count += n-a
  else:
    n = a
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    _ = int(input())
    A = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans_list=[]
a_max=0

for a_idx,a in enumerate(A):
    if a > a_max:
        a_max = a
        ans_list.append(0)
    else:
        ans_list.append(a_max-a)
        
print(sum(ans_list))

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:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = LI()
ans = 0
if n == 1:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
n = int(input())
a =[int(x) for x in input().split()]
num = 0
min_num = a[0]
for i in a[1:]:
    if i < min_num:
        num += min_num-i
    else:
        min_num = i
print(num)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
data = list(map(int, stdin.readline().rstrip().split()))

min = data[0]
count = 0
for i in data:
    if i < min :
        count += min - i
    else:
        min = i
print(count)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(_) for _ in input().split()]
c=A[0]
ans=0
for a in A:
    if a<c:
        ans+=c-a
    else:
        c=a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
n=int(input(""))
lisa=input("").split(" ")
t=0
s=int(lisa[0])
for i in lisa:
    if(s<int(i)):
        s=int(i)
    else:
        t+=s-int(i)
print(t)

Submission status: Accepted

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

total = 0
th = 0

for i in range(n):
  if th < a[i]:
    th = a[i]
  else:
    total += th - a[i]

print(total)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(item) for item in input().split()]

    res = 0
    if N >= 2:
        prev = As[0]
        for item in As[1:]:
            if item < prev:
                res += prev - item
            else:
                prev = item

    else:
        res = 0

    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


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


n = I()
A = list(MI())

ans = 0
prev = 0
for a in A:
    if a < prev:
        d = prev - a
        ans += d
    else:
        prev = a

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

out = 0
t_h = A[0]

for i in A:
    a = max(t_h - i, 0)
    out += a
    t_h = i + a

print(out)

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

res =0
temp = al[0]

for i in range(n):
    x = temp - al[i]
    if x >0:
        res += x
    else:
        temp = al[i]

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
As = list(map(int, input().split()))
res = 0
for i in range(N-1):
    if As[i] > As[i+1]:
        res += As[i] - As[i+1]
        As[i+1] = As[i]
print(res)

Submission status: Accepted

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

Submission status: Accepted

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



def calculate(n, arr):
    currentHeight = arr[0]
    result = 0
    for i in range(1,n):
        if arr[i] < currentHeight:
            result += (currentHeight - arr[i])
        else:
            currentHeight = arr[i]

    return result

res = calculate(N, ARR)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


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



    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

    max = a[0]
    count = 0
    for i in a[1:]:
        if max < i:
            max = i
        else:
            count += max - i
    print(count)

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    summation = 0
    tallest = A[0]
    for a in A[1:]:
        if a > tallest:
            tallest = a
        else:
            diff = tallest - a
            summation += diff

    print(summation)
    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())
arr = [int(n) for n in input().split()]
l, res, prev = len(arr), 0, arr[0]
for i in range(1, l):
  now = arr[i]
  if now > prev:
    prev = now
  else:
    res += prev - now
print(res)

Submission status: Accepted

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

goukei=0

saidai=a[0]

for i in range(1,len(a)):
    if a[i]<saidai:
        goukei+=saidai-a[i]
    else:
        saidai=a[i]

print(goukei)

Submission status: Accepted

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

m = a[0]
res = 0

for v in a:
    if m < v:
        m = v
    else:
        res += m-v
print(res)

Submission status: Accepted

Solution code:


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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now_h = A[0]
ans = 0

for a_i in A:
  if now_h>a_i:
    ans += now_h-a_i
  else:
    now_h = a_i
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,*A = map(int, open(0).read().split())
ans = 0
temp = A[0]
for x in A:
    if x < temp:
        ans += temp - x
    else:
        temp = x
print(ans)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

ans = 0
prev_height = A[0]
for current_height in A[1:]:
    if prev_height>current_height:
        ans+=prev_height-current_height
    else:
        prev_height=current_height


print(ans)

Submission status: Accepted

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

ans = 0
prev = 0
for a in A:
    now = a
    if prev<=now:
        prev = now
        continue
    ans += (prev-now)

print(ans)

Submission status: Accepted

Solution code:
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    max = 0
    ans = 0
    for i in range(n):
        if max > a[i]:
            ans += max - a[i]
        else:
            max = a[i]
    print(ans)
    return






n = RN()
a = RA()
solve()

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase


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

# region fastio

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

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

h_sum = 0

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

print(h_sum)

Submission status: Accepted

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

import sys


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def std_in():
    return sys.stdin.readline().strip()


def main():
    n = int(std_in())
    a = list(get_ints())

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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


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

print(humidai)

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:
# coding: utf-8
# Your code here!
# map(int, input().split())

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
N=sys.stdin.readline().strip()
A=map(int, sys.stdin.readline().split())
a_max=None
ans=0
for i,a in enumerate(A):
    if i==0:
        a_max=a
    else:
        if a<a_max:
            ans+=(a_max-a)
        elif a_max<a:
            a_max=a
print ans