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

step = 0
max_a = a[ 0 ]

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

print( step )

Submission status: Accepted

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

Amax = A[0]
ans = 0

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

print(ans)

Submission status: Accepted

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

# In[6]:


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


# In[7]:


f = 0
height = 0
for a in A:
    if height >= a:
        f += height - a
    else:
        height = a
print(f)


# In[ ]:

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] > 0:
        total += a[i]-a[i+1]
        a[i+1] = a[i]
print(total)

Submission status: Accepted

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

step_sum = 0
pre_height = a[0]
for i in a[1:]:
    if pre_height <= i:
        pre_height = i
    else:
        step_sum += pre_height - i

print(step_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

cur_tallest = A[0]

res = 0
for a in A:
    if a <= cur_tallest:
        res += cur_tallest - a
    else:
        cur_tallest = a
        
print(res)

Submission status: Accepted

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

count=0
high=0

for i in range(n):
    if high>=a[i]:
        count+=high-a[i]
    else:
        high=a[i]
        
print(count)

Submission status: Accepted

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

before = 0
result = 0
for i in A:
    if i < before:
        result += before - i
    else:
        before = i

print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = map(int, input().split())
s = 0
m = 0
for e in a:
    if e > m:
        m = e
    else:
        s += m - e
print(s)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(add)

Submission status: Accepted

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

cur_H = 0
ans = 0
for i in range(N):
    Ai = int(A[i])
    if cur_H > Ai:
        ans += cur_H - Ai
    
    cur_H = max(cur_H, Ai)

print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = list(map(int, input().split()))
  tallest = a[0]
  ans = 0
  for i in range(1, n):
    ans +=  max(0, tallest-a[i])
    tallest = max(tallest, a[i])
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

max_a = 0
ans = 0
for a in A:
    if max_a > a:
        ans += max_a-a
    elif max_a <= a:
        max_a = a
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A_L = [int(x) for x in input().split()]

ans = 0
_a = A_L[0]
for i in range(1, N):
    _ca = A_L[i]
    ans += max(_a - _ca, 0)
    _a = max(_ca, _a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
N=int(input())
ans=0
if N==1:
    print(0)
    sys.exit()

As=list(map(int, input().split()))
max_h=As[0]
for i in range(1,N):
    if As[i]<max_h:
        ans+=max_h-As[i]
    elif As[i]>max_h:
        max_h=As[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, As):
    ans = 0
    for i in range(1, N):
        if As[i - 1] > As[i]:
            ans += As[i - 1] - As[i]
            As[i] = As[i - 1]
    print(ans)


if __name__ == '__main__':
    # S = input()
    N = int(input())
    # N, M = map(int, input().split())
    As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(N, As)

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]
    else: continue
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


mx = arr[0]
i = 1
ans = 0
while i < n:
    if arr[i] < mx:
        ans += mx - arr[i]
    mx = max(mx, arr[i])
    i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    # N = 3
    # A = [3, 2, 1]
    count = 0
    for i in range(1, N):
        diff = A[i - 1] - A[i]
        if diff > 0:
            count += abs(diff)
            A[i] += abs(diff)
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = input()
    a = [int(i) for i in input().split()]

    now = a[0]
    ans =0
    for i in a:
        if i < now:
            ans+= now - i
        else:
            now = i

    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
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:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


N = z()
A = zz()

np.argmax(A)
part_max = [0]*N
# 自分より前のMAXを求める
for i in range(1, N):
    part_max[i] = max(A[i-1], part_max[i-1])
ans = [0]*N
for i in range(N):
    ans[i] = max(part_max[i]-A[i], 0)
print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
c = 0
for n in range(1, N):
    if X[n-1] > X[n]:
        c += X[n-1] - X[n]
        X[n] = X[n-1]

print(c)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = 0
mofu = 0
for i in a:
    if mofu <= i:
        mofu = i 
    else:
        ans += mofu-i
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

h=a[0]

ans=0
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:
import sys,math
try:sys.stdin,sys.stdout=open('input.txt','r'),open('out.txt','w')
except:pass
from time import clock
a=clock()
from sys import stdin,stdout;mod=int(1e9 + 7);from statistics import mode
from collections import *;from math import ceil,floor,inf,factorial,gcd,log2,sqrt,log
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
# print('{:.3f}'.format(1),round(1.123456789,4))
# sys.setrecursionlimit(500000)
def lcm(a,b): return (a*b)//gcd(a,b)
def setbits(n):return bin(n).count('1')
def resetbits(n):return bin(n).count('0')
def modinv(n,p):return pow(n,p-2,p)
def ncr(n,r):
    num,den=1,1;r=min(n,n-r)
    for i in range(r):num*=(n-i);den*=(i+1)
    return num//den
def ncr_p(n, r, p):
    num,den=1,1;r=min(r,n-r)
    for i in range(r):num = (num * (n - i)) % p ;den = (den * (i + 1)) % p
    return (num * modinv(den,p)) % p
def isPrime(num) :
    if num<=1:return False
    if num==2 or n==3:return True
    if (num % 2 == 0 or num % 3 == 0) :return False
    m = int(num**0.5)+1
    for i in range(5,m,6):
        if (num % i == 0 or num % (i + 2) == 0) :return False
    return True
def bin_search(s, low, high, val):
    while low <= high:
        mid = low + (high - low) // 2;
        if s[mid] == val:return mid
        elif s[mid] < val:low = mid + 1
        else:high = mid - 1
    return -1
def sumofdigit(num):
    count=0;
    while num : count+=num % 10;num //= 10;
    return count;
def inputmatrix():
    r,c=iia();mat=[0]*r;
    for i in range(r):mat[i]=iia();
    return r,c,mat;
def prefix_sum(n,s):
    for i in range(1,n):s[i]+=s[i-1]
    return s;
def binomial(n, k):
    if 0 <= k <= n:
        ntok = 1;ktok = 1
        for t in range(1, min(k, n - k) + 1):ntok *= n;ktok *= t;n -= 1
        return ntok // ktok
    else:return 0
def divisors(n):
    res = [];
    for i in range(1,ceil(sqrt(n))+1):
        if n%i == 0:
            if i==n//i:res.append(i)
            else:res.append(i);res.append(n//i)
    return res;
# code here---
n = ii1()
arr = iia()
ans=0
for i in range(1,n):
    if arr[i]<arr[i-1]:
        ans+=abs(arr[i-1]-arr[i])
        arr[i]=arr[i-1]
print(ans)

Submission status: Accepted

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

mx = A[0]
humidai = 0
for i in range(1, n):
    if A[i] < mx:
        humidai += mx-A[i]
    elif A[i] > mx:
        mx = A[i]

print(humidai)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_ = 0
ans = 0

for a in A:
    max_ = max(a, max_)
    if a < max_:
        ans += (max_ - a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import itertools
import sys
n = int(input())
a = list(map(int, input().split()))
tmp = a[0]
cnt = 0
for i in range(1, len(a)):
    if(tmp > a[i]):
        cnt += tmp-a[i]
    else:
        tmp = a[i]
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 22 21:05:46 2020

@author: NEC-PCuser
"""

N = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前
sort()
sorted(reverse=true)
配列の初期化
配列名=[a]*n+[b]*m
Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

n = int(stdin.readline().rstrip())
A = [int(s) for s in stdin.readline().rstrip().split()]
num = 0
for i in range(1, n):
    if A[i-1] > A[i]:
        num += A[i-1]-A[i]
        A[i] += A[i-1]-A[i]
print(num)

Submission status: Accepted

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

for p in range(N):
    if a >= A[p]:
        ans += a-A[p]
        A[p] = a
    
    a = A[p]

print(ans)

Submission status: Accepted

Solution code:
import math
# [int(i) for i in input().split(' ')]
n, = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]

cur = 0
ret = 0
for t in a:
    if cur > t:
        ret += cur - t
    else:
        cur = t

print(ret)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())
def LIST1() : return list(MAP1())

def solve():
    n = INT()
    a = LIST()
    ans = 0
    tmp = a[0]
    for i in range(1, n):
        ans += max(0, tmp-a[i])
        tmp = max(tmp, a[i])
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import Counter,deque,defaultdict
from itertools import combinations,permutations,accumulate
from bisect import bisect_left,bisect
sys.setrecursionlimit(10**6)
INF = 10**9+7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
cnt = 0
tmp = A[0]
for i in range(1,N):
    if tmp > A[i]:
        cnt += tmp-A[i]
        A[i]=tmp
    tmp = A[i]
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
temparr = input()
temparr = temparr.split()
arr = []
for i in temparr:
    arr.append(int(i))
ans = 0 
mins = arr[0]
for i in arr:
    if i < mins :
        ans += mins - i  
        continue 
    if i > mins:
        mins = i 
print(ans)

Submission status: Accepted

Solution code:

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

minHeight = Ai[0]
step = 0
for A in Ai:
	minHeight = max(minHeight, A)
	step += max(0, minHeight - A)

print(step)

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:
_ = input()
A = [int(i) for i in input().split()]

ans = 0
for i in range(len(A) - 1):
  h = max(A[i] - A[i + 1], 0)
  if h != 0:
    A[i + 1] += h
  ans += h

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int, input().split()))
cnt = 0
tmp = 0
for a, b in zip(S, S[1:]):
    a = max(a, tmp)
    if a > b:
        cnt += a - b
        tmp = a
print(cnt)

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()))
p=a[0]
d=0
for i in a:
    if(p>i):
        d+=(p-i)
    else:
        p=i
print(d)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    A = list(map(int,input().split()))
    ans = 0
    tmp = 0
    for i in A:
        if tmp < i:
            tmp = i
        else:
            ans += tmp - i
    print(ans)
    return 0

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(_x) for _x in input().split()]
    AA = A.copy()

    total = 0
    for i in range(len(AA)):
        if i == 0:
            continue
        dif = AA[i-1] - AA[i]
        if dif > 0:
            total += dif
            AA[i] = AA[i-1]
    print(total)


main()

Submission status: Accepted

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

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

    print(ans)

if __name__ == '__main__':
    main()

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]>0:
        count+=a[i]-a[i+1]
        a[i+1]=a[i]
print(count)

Submission status: Accepted

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

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

print(s)

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)
t = a[0]
count = 0
for i in a:
    if t < i:
        t = i
    count += t
print(count - s)

Submission status: Accepted

Solution code:
def main():
    n = map(int, list(input()))
    a = list(map(int, input().split()))
    height = 0
    max_height = a[0]
    for _a in a[1:]:
        if _a == max_height:
            continue
        elif _a > max_height:
            max_height = _a
        else:
            height += max_height - _a
    print(height)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
A = [int(x) for x in sys.stdin.readline().rstrip().split()]

sumf = 0
max_ = 0
for a in A:
    if max_ < a:
        max_ = a
        continue
    else:
        sumf += max_ - a

print(sumf)

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()
al = inpl()
ans = 0
tmp = 0
pre = al[0]
for a in al:
    if(pre > a):
        ans += pre - a
    else:
        pre = a
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
ans = 0
a = rl()
tmp = 0
for i in a:
  ans += max(0,tmp-i)
  tmp = max(tmp, i)
print(ans)

Submission status: Accepted

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

ans = 0
h = a[0]
for i in a[1:]:
    if i <= h:
        ans += h - i
    else:
        h = i

print(ans)

Submission status: Accepted

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

tmax = A[0]
ans = 0
for p in range(1, N):
    if A[p] < tmax:
        ans += tmax - A[p]
    elif A[p] > tmax:
        tmax = A[p]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(N, AS):
    ret = 0
    prevStep = 0
    for i in range(1, N):
        if prevStep + AS[i - 1] > AS[i]:
            prevStep = prevStep + AS[i - 1] - AS[i]
            ret += prevStep
        else:
            prevStep = 0
    return ret


def main():
    N = int(input())
    AS = list(map(int, input().split()))
    print(solve(N, AS))


# tests
T1 = """
5
2 1 5 4 3
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
4
"""

T2 = """
5
3 3 3 3 3
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
0
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


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


def main():
    _ = int(input())
    A = [int(i) for i in input().split()]

    ans = 0
    cur = A[0]
    for a in A:
        if a >= cur:
            cur = a
        else:
            ans += cur - a

    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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