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 numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


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


def II():
    return int(input())


N = II()
a = acinput()

x = 0

h = a[0]
for i in range(1, N):
    x += max(0, h-a[i])
    h = max(h, a[i])

print(x)

Submission status: Accepted

Solution code:
num = int(input()) - 1
list = list(map(int,input().split()))
len = len(list) 
total = 0
i = 0

while i < num:
    if list[i] > list[i+1]:
        total= total + (list[i+1] -list[i])
        list[i+1] = list[i]
        i = i + 1
    else:
        i = i + 1
        
print(abs(total))

Submission status: Accepted

Solution code:
#中身の数
n = int(input())
#リスト
a = list(map(int,input().split()))
#合計
p = 0
#前が使った土
q = 0

for i in range(n-1):
  #前の人との差
  e = a[i+1]-a[i]
  if e > 0:
    if e > q:
      q = 0
    else:
      q = q-e
  else:
    q = q-e
  p += q
  
print(p)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
s = list(map(int, input().split()))
h = 0

for i in range(1, N):
  if s[i] < s[i-1]:
    h += s[i-1] - s[i]
    s[i] = s[i-1]
  else:
    continue

print(h)

Submission status: Accepted

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

Submission status: Accepted

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

tmp = 0
ans = 0

for i in a:
    if tmp > i:
        ans += tmp - i
    tmp = max(i, tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
x=0
dx=0
for i in A:
    if i>x:
        x=i
    else:
        dx+=(x-i)
print(dx)

Submission status: Accepted

Solution code:
def main():
#    N, X, T = map(int, input().split())
    N = input()
    heights = [int(s) for s in input().split()]

    sum = 0

    max_value = 0

    for i in range(0, len(heights)):
        if max_value < heights[i]:
            max_value = heights[i]
        else:
            sum = sum + max_value - heights[i]


    print(sum)
main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

import numpy as np

B_list = A_list.copy()

for i in range(1, len(A_list)):
    if A_list[i] < A_list[i-1]:
        A_list[i] = A_list[i-1]
        
A_array = np.array(A_list)
B_array = np.array(B_list)

print(np.sum(A_array - B_array))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
members = list(map(int, input().strip().split()))
stands = []
stands.append(0)

for i, member in enumerate(members):
  if i>0:
    if members[i] > members[i-1] + stands[i-1]:
      stands.append(0)
    else:
      stands.append(members[i-1] + stands[i-1] - members[i])
      
print(sum(stands))

Submission status: Accepted

Solution code:

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

high = [0]*n
for i in range(1,len(a)):
    if a[i] <= a[i-1]:
        dai = a[i-1] - a[i]
        a[i] += dai
        high[i] = dai


print(sum(high))

Submission status: Accepted

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

now=int(A[0])
sum=0
for i in range(N):
    if now>=int(A[i]):
        sum+=now-int(A[i])
    else:
        now=int(A[i])

print(sum)

Submission status: Accepted

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

max=0
total=0

for i in range(len(A)):
  if max < A[i]:
    max=A[i]
  else:
    total+=max-A[i]

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = [int(s) for s in input().split(' ')]

max = 0
step = 0

for a in h:
    if a > max:
        max = a
    if a < max:
        step += max - a

print(step)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def abc176_c():
    n = int(input())
    a = list(map(int, input().split(' ')))
    ans = 0
    most_h = 0

    for i in range(1, len(a)):
        if i == 1:
            most_h = max(a[i - 1], a[i])
            if a[i - 1] > a[i]:
                ans += a[i - 1] - a[i]
        else:
            if most_h > a[i]:
                ans += most_h - a[i]

        most_h = max(most_h, a[i])

    print(ans)

if __name__ == '__main__':
    abc176_c()

Submission status: Accepted

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

f = l[0]
t = l[1:]
s = 0
max = f
for i in t:
    diff = max - i
    if diff > 0:
        s += diff
        
    if(max < i):
        max = i 
        
print(s)

Submission status: Accepted

Solution code:
"""
自分が前より小さいとき前の人に合わせる
大きいときそのまま
"""
n = int(input())
a = list(map(int, input().split()))
cnt = 0
m = int(a[0])
for i in range(1, n):
    if m > int(a[i]):
        cnt += m - int(a[i])
    else:
        m = int(a[i])
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
data = [int(i) for i in input().split()]
MAX = data[0]
res = 0
for i in range(1, len(data)):
    if data[i] > MAX:
        MAX = data[i]
    else:
        res += (MAX - data[i])

print(res)

Submission status: Accepted

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

t=0
x=0
z=a[0]

while t<n:
  if a[t]<z:
    x=x+(z-a[t])
  else:  
    z=a[t]
  t=t+1
print(x)

Submission status: Accepted

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

print(total)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = a[0]
c = 0
r = 0
for i in a:
    if c == n:
        break
    if s > i:
        r += s - i
    else:
        s = i

    c += 1

print(r)

Submission status: Accepted

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

keep = 0
cnt =0
x=0


for a in A:
    if keep > a:
        cnt += keep -a
        x = keep -a
        keep = a + x
    else:
      keep = a
    
    
print(cnt)

Submission status: Accepted

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

@author: naoki
"""

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

Submission status: Accepted

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

Submission status: Accepted

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

total = 0
amax = A[0]

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

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

total = 0
ext = 0
prev = an[0]

for a in an:
    if prev+ext > a:
        ext = prev+ext - a
        total += ext
    else:
        ext=0
    prev = a

print(total)

Submission status: Accepted

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

for i in range(len(A)-1):
    if A[i] >= A[i+1]:
        gap = 0
        gap = A[i]-A[i+1]
        A[i+1] = A[i+1] + gap
        sum_gap += gap
print(sum_gap)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

F = 0
H = A[0]

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

print(F)

Submission status: Accepted

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

stand = [0] * n
for i in range(1, len(a)):
    if (a[i-1] + stand[i-1]) > a[i]:
        stand[i] = (a[i-1] + stand[i-1]) - a[i]

print(sum(stand))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()
m = input().split()
m = [int(i) for i in m]
mx = max(m)
ind = m.index(mx)
 
c = 0
j = len(m)
for i in reversed(m[ind:]):
    if i < mx:
        d = mx - i
        c += d
        
ind_i = 0
for i in m[1:ind]:
    #print(i)
    if m[ind_i] > i:
        num = m[ind_i] - i
        m[ind_i+1] += num
        c+= num
    
    ind_i += 1
    
print(c)

Submission status: Accepted

Solution code:
num = input()
height = list(map(int,input().split()))
count = 0

for i in range(len(height) - 1):
    if height[i] > height[i+1]:
        count = count + (height[i]-height[i+1])
        height[i+1] = height[i]

print(count)

Submission status: Accepted

Solution code:
import copy
N = input()
A = []
for val in map(int, input().split()):
    A.append(int(val))

# qwer = copy.deepcopy(A)
def asd(index):
    x = A[index]
    y = A[index+1]
    inheight = 0
    # print('A', A)
    # print(index, x, y)
    if( x > y) :
        # print('height',  x - y)
        inheight +=  x - y
        A[index+1] += inheight
    return inheight

height = 0
for index in range(len(A) - 1):
    # print(index)
    height += asd(index)
print(height)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int, input().split()))
n = int(len(l))

x = 0
y = l[0]
for i in range(1, n):
    z = l[i] - y
    if z >= 0:
        y = l[i]
        x += 0
    else:
        x += abs(z)

print(x)

Submission status: Accepted

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

std = 0
base = int(A[0])
ans = 0

for n in range(1,N):
    std = int(A[n]-1)

    if std < base:
        ans += A[n-1] - A[n]
        std += base - std
        A[n] = std

    base = A[n]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
aN = list(map(int, input().split()))
sum_board = 0
aN_max = aN[0]
for i in range(1,n):
    if aN[i] < aN_max:
        sum_board +=  aN_max - aN[i]
    else:
        aN_max = aN[i]

print(sum_board)

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
mx = -1
ht = 0

for i in range(n):
  mx = max(mx,an[i])
  if mx - an[i] > 0:
    ht += mx - an[i]
print(ht)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

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


print(ans)

Submission status: Accepted

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

A.insert(0, A[0])

stand_h = 0

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

print(stand_h)

Submission status: Accepted

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

count = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit,stdout
from collections import deque, defaultdict,Counter
from math import log2, log, ceil,sqrt
from operator import *
input = stdin.readline
setrecursionlimit(int(2e5))
def getstr(): return input()[:-1]
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x : int(x) - 1, input().split()))
jn = lambda x,l: x.join(map(str,l))
# swap_array function
def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp
 
## gcd function
def gcd(a,b):
    if a == 0:
        return b
    return gcd(b%a, a)
 
## nCr function efficient using Binomial Cofficient
def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return res 
 
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
    hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo
 

# sliding window ---

def maxSlidingWindow(nums,k):
    windowSum,maxSum = 0,0
    windowSum = sum(nums[:k])
    for end in range(k,len(nums)):
        windowSum += nums[end]-nums[end - k]
        maxSum = max(maxSum,windowSum)
    return maxSum


# Two Pointer 
def twoSum(nums, target):
        left = 0
        right = len(nums)-1
        while left <= right:
            if nums[left]+nums[right] == target:
                return [left,right]
            elif nums[left]+nums[right] < target:
                left +=  1
            else:
                right -= 1
        return 

# prefix sum

def prefix(arr):
    a = map(add,arr.insert(0,0))
    return a

# Modular Inverse
# y-1 = y(p-2)%p

# String Matching





## prime factorization
def primefs(n):
    ## if n == 1    ## calculating primes
    primes = {}
    while(n%2 == 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    ## prime factoriazation of n is stored in dictionary
    ## primes and can be accesed. O(sqrt n)
    return primes
 
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 
 
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b
 
# find function with path compression included (recursive)
# def find(x, link):
#     if link[x] == x:
#         return x
#     link[x] = find(link[x], link);
#     return link[x];
 
# find function with path compression (ITERATIVE)
def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;
 
 
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x
 
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime


# Binary Search functions
def binsearch(a, l, r, x):
    while l <= r:
        mid = l + (r-1)//2
        if a[mid]:
            return mid
        elif a[mid] > x:
            l = mid-1
        else:
            r = mid+1
    return -1


# Grafh related queries : 



graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000
 
 
def bfs(d, v):
    q = []
    q.append(v)
    visited[v] = 1
    while len(q) != 0:
        x = q[0]
        q.pop(0)
        for i in d[x]:
            if visited[i] != 1:
                visited[i] = 1
                q.append(i)
        print(x)
 
 
def make_graph(e):
    d = {}
    for i in range(e):
        x, y = mi()
        if x not in d:
            d[x] = [y]
        else:
            d[x].append(y)
        if y not in d:
            d[y] = [x]
        else:
            d[y].append(x)
    return d
 
 
def gr2(n):
    d = defaultdict(list)
    for i in range(n):
        x, y = mi()
        d[x].append(y)
    return d
 
 
def connected_components(graph):
    seen = set()
 
    def dfs(v):
        vs = set([v])
        component = []
        while vs:
            v = vs.pop()
            seen.add(v)
            vs |= set(graph[v]) - seen
            component.append(v)
        return component
 
    ans = []
    for v in graph:
        if v not in seen:
            d = dfs(v)
            ans.append(d)
    return ans

# End graph related queries
 
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    ## function for storing smallest prime factors (spf) in the array
 
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)] 
spf_sieve() 
def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    ## this function is useful for multiple queries only, o/w use
    ## primefs function above. complexity O(log n)
 
## taking integer array input


# LCM of two numbers :
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
 
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
y, n = "Yes", "No"
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12,
       'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24,
       'z': 25}


# used for testing your code 

# inputs=open('input.txt','r') 
# L I S
def solve():
    x = getints()
    count = 0
    for i in range(1,len(x)):
        if x[i-1]<= x[i]:
            count += 0
        else:
            count+=(x[i-1]-x[i])
            x[i] = x[i-1]
    print(count)
if __name__ == "__main__":
    # solve()
    # for t in range(getint()):
    #     print("Case #{}: ".format(t + 1), end="")
    #     solve()
    getint()
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
u=max(l)
d=0
i=0
while l[i]!=u:
  if l[i]>l[i+1]:
    d=d+abs(l[i+1]-l[i])
    l[i+1]=l[i] 
  i=i+1
r=i
for b in range(r,n):
    d=d+(u-l[b])
print(d)

Submission status: Accepted

Solution code:
def resolve():
	N = int(input())
	H = 0
	res = 0
	for A in input().split():
		A = int(A)
		res += max(0, H - A)
		H = max(H, A)
	print(res)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
 
sum_humidai = 0
max_front_person = 0
  
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(max_front_person, As[person_idx - 1])
  if max_front_person > person_height:
    humidai = max_front_person - person_height
    sum_humidai += humidai
 
print(sum_humidai)

Submission status: Accepted

Solution code:
a=[input() for i in range(2)]
a1=int(a[0])
a2=[int(i) for i in a[1].split()]

n=0

for i in range(len(a2)-1):
	sa=a2[i]-a2[i+1]
	if sa>0:
		a2[i+1]+=sa
		n+=sa

print(n)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sum = 0
for i in range(1,N): #2番目から最後尾まで
    lows = A[i]-A[i-1] #1つ前の人との身長差
    if lows < 0:
        sum += -lows
        A[i] += -lows

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Amax = 0
FD = 0

for Ai in AL:
#  print('FD=%d' % FD)
  if Amax > Ai:
#    print('1')
    FD = FD + (Amax - Ai)
  elif Amax < Ai:
#    print('2')
    Amax = Ai

print(FD)

Submission status: Accepted

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

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 = map(int, input().split())
l = list(a)
nl = [0]*n

for i in range(n-1):
    if l[i] > l[i+1]:
        nl[i+1] += l[i] - l[i+1]
        l[i+1] = l[i]
    else:
        nl[i+1] += 0
print(sum(nl))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ｎ = int(input())
a = list(map(int,input().split()))
cnt = 0
mx = a[0]

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

print(cnt)

Submission status: Accepted

Solution code:
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    max = A[0]
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max < A[i]:
            max = A[i]
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

except:
    pass
print(fumidai)

Submission status: Accepted

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

maxi, res = 0, 0
for num in nums:
  maxi = max(maxi, num)
  res += maxi - num
  
print(res)

Submission status: Accepted

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

tmpMax=0
sumHeight=0
for Ai in A:
  if tmpMax>Ai:
    sumHeight+=tmpMax-Ai
  else:
    tmpMax=Ai
print(sumHeight)

Submission status: Accepted

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

prev = A[0]
ans = 0
for i in A[1:]:
    if prev > i:
        ans += prev - i
    else:
        prev = i
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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


highest = 0
ans = 0

for i in a:
    if i > highest:
        highest = i
    ans += highest - i

print(ans)

Submission status: Accepted

Solution code:
N = input()
A = [int(p) for p in input().split(' ')]
stools = list()
stools.append(0)
for k in range(1,len(A)):
    if(A[k-1]>A[k]):
        stools.append(A[k-1]-A[k])
        A[k] = A[k-1]
    else:
        stools.append(0)
print(sum(stools))

Submission status: Accepted

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

Submission status: Accepted

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

S = [0]*N
i = 0

while i != N-1 and i == 0 and i < N-1:
    for j in range(i+1,N):
        if A[i] >= A[j]:
            S[j] = A[i] - A[j]
        else:
            i = j
            continue
    i = j

print(sum(S))

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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

res = 0

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

print(res)

Submission status: Accepted

Solution code:
def main():
  x = 0
  n = int(input())
  a = list(map(int,(input().split())))
  for i in range(n-1):
    a1 = a[i:i+1]
    a2 = a[i+1:i+2]
#    print(a1,a2)
    if a1 > a2:
#      print(a1,a2,a1[0]-a2[0])
      sa = a1[0]-a2[0]
      x +=(sa)
      a[i+1]+=sa
  if a[n-2] > a[n-1]:
    x +=a[n-2][0]-a[n-1][0]
  print(x)  

if __name__ == '__main__':
  main()