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

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

Solution (Python):
n=input()
arr=map(int, raw_input().split())
if n==1:
  print(0)
  exit(0)
a=0
m=arr[0]
for i in xrange(1, n):
  a+=max(0, m-arr[i])
  m=max(m, arr[i])

print a

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


ans=[0 for i in range(n)]
ans[0]=0



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

print(sum(ans))

Solution (Python):
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

def lcm(a, b):  
    return (a*b)/gcd(a, b)

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()



N = In.int()
A = In.ints()

maxYet = 0

ans = 0

for h in A:
    if maxYet>h:
        ans+=maxYet-h
    maxYet = max(h, maxYet)

Out.int(ans)



if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

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

fumidai = 0
tallest = lst[0]
for num in lst[1:]:
    if num < tallest:
        fumidai += tallest - num
    elif num > tallest:
        tallest = num
print(fumidai)

Solution (Python):
n = int(input())

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

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

print(ans)

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

res = 0
mi = 0
for aa in A:
    mi = max(mi, aa)
    res += mi - aa 

print(res)

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

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

print(sum(ans))

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

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

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

for num in range(1, N):
    if A[num - 1] > A[num]:
        ans = ans + A[num - 1] - A[num]
        A[num] = A[num - 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] :
        ans += A[i-1] - A[i]
        A[i] = A[i-1]
print(ans)

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

ret = 0
bafore = A[0]
for a in A:
  if bafore > a:
    ret += bafore - a
  bafore = max( a, bafore )
    
print( ret )

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

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

heighest = 0
humi =[]

for i in range(n):
  if a[i] > heighest:
    heighest = a[i]
  elif a[i]<heighest:
    sa = heighest-a[i]
    humi.append(sa)
print(sum(humi))

Solution (Python):
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    step = [0 for i in range(N)]

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

    ans = sum(step)
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
c=0
for i in range(1,n):
    c+=max(l[i-1]-l[i],0)
    l[i]+=max(l[i-1]-l[i],0)
print(c)

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

maxh = L[0]
sum1 = 0
for i in range(1,n):
    if L[i] > maxh:
        maxh = L[i]
    else:
        sum1 += maxh-L[i]
print(sum1)

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

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

print(n)

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

Solution (Python):

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

current = a[0]

sum_ = 0

for aa in a:
    if current <= aa:
        current = aa
    else:
        sum_ += current - aa


print(sum_)

Solution (Python):

import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]









n = I()
a = LI()
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)

Solution (Python):
import numpy as np

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

r=0
for ii in np.arange(1,n):
  if a[ii]<a[ii-1]:
    r+=a[ii-1]-a[ii]
    a[ii]=a[ii-1]

print(r)

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
import numpy as np


def solve(N: int, A: "List[int]"):
    highest = A[0]
    ans = 0

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

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

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

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

b = 0
ans = 0
for v in A:
    if v < b:
        ans += b - v
    else:
        b = v

print(ans)

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

cnt = 0
for i in range(1, N):
    front = A[i - 1]
    back = A[i]

    if front > back:
        diff = front-back 
        cnt += diff
        A[i] = A[i] + diff

    
print(cnt)

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

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

Solution (Python):






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





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

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

Solution (Python):

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):








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__':
    
    N = int(input())
    
    As = [int(i) for i in input().split()]
    
    solve(N, As)

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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)




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

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

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

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

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

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

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


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;

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)

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

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

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

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

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

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

Solution (Python):

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

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

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

Solution (Python):
import math

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)

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

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

Solution (Python):

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)

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

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

Solution (Python):

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)

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

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

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

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

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

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

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

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

Solution (Python):
import sys
import numpy as np
import math  
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()

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

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

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

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

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

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

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

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