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.

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

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mx = a[0]
s = 0
for i in a:
    if i < mx:
        s += mx-i
    else:
        mx = i
print(s)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
last=l[0]
ans=0
for i in range(1,n):
    if l[i]>=last:
        last=l[i]
    else:
        ans+=abs(last-l[i])
print(ans)

Solution (Python):
n = int(input())
a = tuple(map(int,input().split()))

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

Solution (Python):

import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
x = ip()
mx = 0
ans = 0
for i in x:
    mx = max(mx,i)
    ans += mx-i
print(ans)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    cur_max = a[0]
    total = 0
    for ai in a:
        if ai<cur_max:
            total+=cur_max - ai
        else:
            cur_max = ai
    print(total)

if __name__=="__main__":
    main()

Solution (Python):




import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict


MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    n = read_int()
    a = read_int_list()
    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)


if __name__ ==  '__main__':
    Main()

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
num=0
ans=0

for i in a:
  if num<i:
    num=i
    
  ans+=abs(num-i)
  
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

maxi = 0
ans = 0
for a in As:
    if a<maxi:
        ans += maxi-a
    maxi = max(a, maxi)

print(ans)

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

sm=0
mx=0
for i in a:
  sm+=max(mx-i,0)
  mx=max(i,mx)
  
print(sm)

Solution (Python):
N = int(input())
Alist = list(map(int, input().split()))

hmax = Alist[0]
ans = 0
for n in range(1, N):
    if Alist[n] < hmax:
        ans += hmax - Alist[n]
    else:
        hmax = Alist[n]

print(ans)

Solution (Python):
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)

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

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

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


main()

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

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

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

ans = 0
pre = 0
for a in A:
    if a < pre:
        ans += pre-a
    else:
        pre = a
print(ans)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
_a = A[0]
for a in A[1:]:
    tmp = 0
    
    if a - _a < 0:
        tmp = _a - a
    ans += tmp
    _a = a + tmp
print(ans)

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

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

ans = 0
m = 0
for Ai in A:
    if m <= Ai:
        m = Ai
    else:
        ans += m - Ai
print(ans)

Solution (Python):
n = int(input())
nums = list(map(int, input().split()))

ans = 0
maximum = 0
for num in nums:
    maximum = max(maximum, num)
    ans += maximum - num

print(ans)

Solution (Python):
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]
print(cnt)

Solution (Python):
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)

Solution (Python):
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)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

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

print(ans)

Solution (Python):
import sys, math, bisect


if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    N = int( input().strip() )
    Heights = list( map(int, input().strip().split() ) )

    sum = 0
    for i in range(N-1):
        if(Heights[i+1] < Heights[i]):
            plus = Heights[i] - Heights[i+1]
            sum += plus
            Heights[i+1] += plus

    print(sum)

Solution (Python):
def main():
    N = int(input())
    *A, = map(int, input().split())

    ma = 0
    ans = 0
    for h in A:
        if h < ma:
            ans += ma - h
        else:
            ma = h

    print(ans)


if __name__ == '__main__':
    main()

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

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

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

val = A[0]
cnt = 0
for a in A:
    if val < a:
        val = a
    else:
        cnt += val - a
print(cnt)

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

now=a[0]
ans=0
for h in a:
    if now > h:
        ans+=(now-h)
    now=max(now,h)
print(ans)

Solution (Python):

n = int(input())
A = list(int(x) for x in input().split())

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    ans = 0
    mx = a[0]

    for x in a[1:]:
        diff = mx - x
        if diff > 0:
            ans += diff
        mx = max(mx, x)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq
import math

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

    res = 0
    max_a = A[0]
    for a in A[1:]:
        if max_a > a:
            res += (max_a - a)
        else:
            max_a = a

    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
n,*aa = map(int, open(0).read().split())
aa = np.array(aa)
bb = np.maximum.accumulate(aa)

print((bb-aa).sum())

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

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

print(ans)

Solution (Python):
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)

Solution (Python):

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
MAX = A[0]
ans = 0
for i in range(N-1):
    ans += max(0, MAX - A[i+1])
    MAX = max(MAX, A[i+1])
print(ans)

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

Solution (Python):
from sys import stdin
input = stdin.readline

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

max_height = 0
ans = 0

for a in A:
    if a < max_height:
        ans += max_height - a
    else:
        max_height = a

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

    N = int(input())
    
    A = tuple(map(int, input().split())) 
    
    
    tot=0
    cur=0
    for a in A:
        if cur>a:
            tot+=cur-a
        else:
            cur=a
    print(tot)



if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    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)


if __name__ == '__main__':
    main()

Solution (Python):
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)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial,sqrt
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7

    n=I()
    lis=LI()
    ans=0
    for i in range(1,n):
        if lis[i-1]>lis[i]:
            ans+=lis[i-1]-lis[i]
            lis[i]=lis[i-1]
    print(ans)
        
    
if __name__=="__main__":
    main()

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

ans = 0
v_max = a[0]
for v in a:
    if v < v_max:
        ans += (v_max - v)
    else:
        v_max = v
print(ans)

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

now = a[0]
point = 0

for i in a[1:]:
    if i < now:
        point += now-i
    else:
        now = i

print(point)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
mx = a[0]
ans = 0
for i in a:
    if mx > i:
        ans += mx - i
    elif mx < i:
        mx = i
print(ans)

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

Solution (Python):
N=input()
*A, = map(int,input().split())
ans = 0
h = A[0]
for a in A:
    ans += max(0,h-a)
    h = max(h,a)

print(ans)

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

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

Solution (Python):
n = int(input())
A = [int(i) for i in input().split()]
now = A[0]
ans = 0
for i in range(1, n):
    if now > A[i]:
        ans += now - A[i]
    now = max(A[i], now)
print(ans)

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

print(ans)

Solution (Python):
import sys

def solve():
    readline = sys.stdin.readline

    N = int(readline())
    A = list(map(int, readline().split()))
    
    ans = 0

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

solve()

Solution (Python):
N=int(input())
alist=list(map(int,input().split()))

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

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

Solution (Python):
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)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    A = in_nl()

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

print(ans)

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

maxA = 0
ans = 0
for a in As:
    if a < maxA:
        ans += maxA - a
    else:
        maxA = a
print(ans)

Solution (Python):
def cin():  return list(map(int,input().split()))

N = cin()[0]
A = cin()

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

Solution (Python):
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

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

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = 'ONLINE_JUDGE' not in sys.argv

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    N=ii()
    A=lmis()
    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)









main()

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

n = k()
a = l()
max_h = a[0]

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

print(cnt)

Solution (Python):
n=int(input())
*a,=map(int,input().split())

cur=a[0]

ans=0

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = [0]*N
M = A[0]
for i in range(1,N):
    if A[i]<M:
        ans[i] = M-A[i]
    M = max(M,A[i])
print(sum(ans))

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

Solution (Python):
N = int(input())
arr = list(map(int,input().split(" ")))
res = 0

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

print(res)

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

Solution (Python):
N = int(input())
*A, = 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)

Solution (Python):


N = int(input())
A = [0] + list(map(int, input().split()))

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

print(answer)

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

tmpmax, ans = A[0], 0
for i in A[1:]:
    if i > tmpmax:
        tmpmax = i
    elif i < tmpmax:
        ans += tmpmax - i

print(ans)

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

maxer = a[0]
sumer = 0
for i in a:
    if i <= maxer:
        sumer += maxer - i
    else:
        maxer = i

print(sumer)

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

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

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

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

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

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

m=0
K=0
for i in range(N):
    if i==0:
        m=A[i]
    else:
        S=max(0,m-A[i])
        K+=S
        m=A[i]+S
print(K)

Solution (Python):
import sys




import math







stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    n = int(stdin.readline().rstrip())
    A = list(map(int, stdin.readline().rstrip().split()))
    
    
    
    
    now = 0
    ans = 0
    for i in range(n):
        if now <= A[i]:
            now = A[i]
        else:
            ans += abs(now-A[i])
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
ans = 0
for i in range(1,n):
    if A[i-1] > A[i]:
        sa = A[i-1] - A[i]
        ans += sa
        A[i] += sa
print(ans)

Solution (Python):
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = LMIIS()
    max_a = 0
    ans = 0
    for a in A:
        max_a = max(max_a,a)
        ans += max_a - a
    print(ans)


    
main()

Solution (Python):


import sys
input=sys.stdin.readline

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

Solution (Python):
def resolve():
    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)
resolve()

Solution (Python):



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

n = int(input())
a = lmp()
u = []
now= -1
for i in range(n):
    if now < a[i]:
        u.append(a[i])
        now = a[i]
    else:
        u.append(now)
ans = 0
for i in range(n):
    ans += u[i] -a[i]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
M = A[0]
out = 0
for i in range(1,N):
    if A[i]<M:
        out += M-A[i]
    if M<A[i]:
        M=A[i]
print(out)

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

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

Solution (Python):
n=int(input())
A=[int(i) for i in input().split()]
now=A[0]
ans=0
for i in range(1,n):
    if A[i]<now:
        ans+=now-A[i]
    else:
        now=A[i]
print(ans)

Solution (Python):
import sys, math, re
from functools import lru_cache
from collections import defaultdict
sys.setrecursionlimit(500000)
MOD = 10**9+7

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

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N = ii()
    A = list(mi())

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
m = A[0]

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

print(ans)

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

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

Solution (Python):


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

n,*a = map(int,read().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)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
now=a[0]
ans=0
for x in a[1:]:
  if now>x:
    ans+=now-x
  now=max(now,x)
print(ans)

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

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