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())
Als=list(map(int,input().split()))

ans=0

for i in range (1,N):
    if Als[i]>=Als[i-1]:
        pass
    else :
        step=abs(Als[i]-Als[i-1])
        ans+=step
        Als[i]+=step
print(ans)

Solution (Python):

n = int(input())
A = list(map(int, input().split()))
s = 0 
M = 0
for i in range(n-1):
    if A[i] <= A[(i+1)] and M <= A[(i+1)]:
        M = 0
        continue
    else:
        if M == 0:
            s += A[i] - A[(i+1)]
            M = A[(i+1)] + (A[i] - A[(i+1)])
        else:
            s += M - A[(i+1)]
print(s)

Solution (Python):
N = int(input())
array = list(map(int,input().split()))
new_array = array
t = sum(array)

for i in range(N-1):
    if array[i] > array[i+1]:
        array[i+1] = array[i]
print(sum(array) - t)

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

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

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

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

ans = 0

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

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

sum=0
MaxNum=0
for i in range(N):
    MaxNum=max(MaxNum,A[i])
    B=MaxNum
    sum+=MaxNum-A[i]
    
print(sum)

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

ma = 0
ans = 0
for a in A:
    ma = max(ma,a)
    ans += (ma - a)

print(ans)

Solution (Python):
N,*A = map(int,open(0).read().split())
base = A[0]
ans = 0
for i in A[1:]:
    if base > i:
        ans += base - i
    elif base < i:
        base = i
print(ans)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline


def main():
    n = int(input())
    A = np.array(input().split(), dtype=int)
    final = np.maximum.accumulate(A)
    ans = (final - A).sum()
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    l = list(map(int, input().split()))
    fumidai_sum = 0
    last = 0
    for i, ai in enumerate(l):
        if last > ai:
            diff = last - ai
            fumidai_sum += diff
            last = ai + diff
        else:
            last = ai
            continue
    print(fumidai_sum)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi -an[i]
            humi += hu
        maxi = max(maxi, an[i])

    print(humi)
if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    n = int(input())
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi -an[i]
            humi += hu
        maxi = max(maxi, an[i])

    print(humi)
if __name__ == '__main__':
    main()

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

d = 0
ans = 0
for a, b in zip(a[:-1], a[1:]):
    if a + d > b:
        d = a + d - b
        ans += d
    else:
        d = 0

print(ans)

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

print(ans)

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

Solution (Python):



N = int(input())
A = input().split()
pre_a = 0
sum = 0
for i in range(N):
    a = int(A[i])
    if (pre_a != 0 and a < pre_a):
        diff = pre_a - a
        sum += diff
    else:
        pre_a = a
print(sum)

Solution (Python):


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


def main():
    N = int(input())
    a = list(map(int,input().split()))
    print(solve(N,a),end="")
    return


if __name__ == '__main__':
    main()

Solution (Python):


n=int(input())

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


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

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

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

    print(ans)

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

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

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

b = 0

t = 0
for i in range(0, n):
	b = max(b, a[i])
	t += b - a[i]

print(t)

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

ans = 0
max_val = A[0]

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

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

for a in A:
    ans += max(0, mx - a)
    mx = max(mx, a)

print(ans)

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

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

ans = 0
mina = A[0]

for a in A[1:]:
  if mina > a:
    ans += mina - a
  else:
    mina = a
    
print(ans)

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

h_max= h[0]
ans=0

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

print(ans)

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

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

h_sum  = 0
temp_max = ary_A[0]

for i in range(len(ary_A) - 1):
    diff = ary_A[i + 1] - temp_max
    if diff < 0:
        h_sum += abs(diff)
    else:
        temp_max = ary_A[i + 1]

print(h_sum)

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

ans = 0
ma = a[0]

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

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

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

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
pre = 0
for a in A:
    if a < pre:
        ans += pre-a
    else:
        pre = a
print(ans)

Solution (Python):
n = int(input())
lol=[int(n) for n in input().split()]
ans = 0
m = lol[0]
for i in lol:
    if(i < m):
        ans += (m-i)
        m=max(i,m)
    else:
        m = max(i,m)
        continue
print(ans)

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

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)



def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def solve(N: int, A: "List[int]"):
    mx = 0
    ans = 0
    for i in range(N):
        if mx > A[i]:
            ans += mx - A[i]
            A[i] = mx
        
        mx = max(mx,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):
N = int(input())
A = list(map(int, input().split()))
step = 0
tmp_step = 0
for i in range(N-1):
  if A[i] + tmp_step > A[i+1]:
    tmp_step = A[i] + tmp_step - A[i+1]
    step += tmp_step
  else:
    tmp_step = 0
print(step)

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

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

n = int(input())
ans = 0
lis = list(map(int, input().split()))
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)

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

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

print(ans)

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

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

Solution (Python):
N = int(input())
an = list(map(int,input().split()))
solve = []

ans = 0
maxnum = 0

for i in range(N):
    maxnum = max(maxnum,an[i])
    ans += maxnum - an[i]
print(ans)

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

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

    current_max = -1
    ans = 0
    for a in A:
        if a <= current_max:
            ans += current_max - a
        else:
            current_max = a
    return ans

print(c_step())

Solution (Python):
n=int(input())
a=[int(i)for i in input().split()]
res=[]
for i in range(1,n):
    if a[i-1]>a[i]:
        tmp=a[i-1]-a[i]
        a[i]+=tmp
        res.append(tmp)
print(sum(res))

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

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

Solution (Python):
n = int(input())
h = [int(i) for i in input().split()]
m = h[0]
s = 0

for i in h:
  if(i > m):
    m = i
  else:
    s += m - i
    
print(s)

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

ans=0
cur_max=0

for x in a:
  if cur_max>x:
    ans+=cur_max-x
  cur_max=max(cur_max,x)

print(ans)

Solution (Python):
def step():
    n = int(input()) 
    alturas = list(map(int, input().split()))
    height = 0;

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

    print(height)

step()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
highest = 0
for i, var in enumerate(A):
  if highest - var > 0:
    ans += highest - var
  else:
    highest = var
print(ans)

Solution (Python):
N = int(input())
lst = []
chair = []
res = 0
lst = input().split()
for i in range(N):
    lst[i] = int(lst[i])
for index in range(1,N):
    if lst[index] < lst[index-1]:
        chair.append(lst[index-1]-lst[index])
        lst[index] = lst[index-1]
for i in chair:
    res += i
print(res)

Solution (Python):

from collections import defaultdict 
import sys
import math 

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

n = int(input())
a = get_array()

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

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

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

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

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

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_L = list(map(int,input().split()))


if len(a_L) == 1:
    print(0)
    exit()

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


if a_L[-2] > a_L[-1]:
    ans += abs(a_L[-1]-a_L[-2])
print(ans)

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

stack = [a[0]]
s = 0
for i in range(1,n):
    if a[i]<stack[-1]:
        s = s + abs(a[i] - stack[-1])
    else:
        stack.append(a[i])
print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ma=0
res=0
for i in range(n):
    ma=max(ma,a[i])
    res+=ma-a[i]
print(res)

Solution (Python):

















import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = MAP()
prev = -INF
ans = 0
for a in A:
    if a < prev:
        ans += (prev - a)
    else:
        prev = 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]<a[i-1]:
        ans+=a[i-1]-a[i]
        a[i]=a[i-1]
print(ans)

Solution (Python):

def tc():
    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)


tc()

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

Solution (Python):
n,*a=map(int,open(0).read().split())
ma=a[0]
ans=0
for q in a:
  if ma>q:
    ans+=(ma-q)
  ma=max(ma,q)
print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

if __name__ == "__main__":
    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):
import array


class Stack:
    def __init__(self):
        self.size = 0
        self.list = list()

    def push(self, el):
        self.size += 1
        self.list.append(el)

    def pop(self):
        self.size -= 1
        return self.list.pop()

    def top(self):
        return self.list[-1]

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        return(self.list.__str__())


def cstep(ar):
    if len(ar)  == 1:
        return 0
    s = Stack()
    d = array.array('i', [0]*len(ar))
    maxima = ar[0]
    for index in range(1, len(ar)):
        el = ar[index]
        if maxima < el:
            maxima = el
        d[index] = maxima - el
    return sum(d)

def main():
    n = int(input())
    ar = list(map(int, input().strip().split()))
    print(cstep(ar))


main()

Solution (Python):
n,*a=map(int,open(0).read().split())
now=a[0]
ans=0
for i in range(n):
  if a[i]<now:
    ans+=(now-a[i])
  now=max(a[i],now)
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()))

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

print(ans)

Solution (Python):


if __name__ == '__main__':
    N = int(input())

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

    M = max(li)
    

    MV = li[0]
    C = 0
    hai = li.index(M)
    
    for i in range(N):
        if i < hai:
            if MV > li[i]:
                C += MV - li[i]
            else:
                MV = li[i]

        else:
            C += M - li[i]
        

    print(C)

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

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

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

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

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
ans=0
mx=arr[0]
for i in arr:
	ans+=max(0,mx-i)
	mx=max(i,mx)
print(ans)

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

Solution (Python):
b=s=0
for a in[*open(0)][1].split():a=int(a);b=max(b,a);s+=b-a
print(s)

Solution (Python):




from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
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()
setrecursionlimit(100000)
mod = 1000000007

n = ii1()
arr = iia()
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):
import math


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

print(cnt)

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

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

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

Solution (Python):

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

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

print(res)

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

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

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

for i in range(n-1):
  if lis[i] > lis[i+1]:
    x = lis[i] - lis[i+1]
    lis[i+1] += x
    ans += x

print(ans)

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

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

    X=A[i]
    
print(ans)

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

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

Solution (Python):


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


for i in range(len(a) - 1):
    if a[i + 1] < a[i]:
        l.append(a[i]-a[i + 1])
        a[i + 1] = a[i]


print(sum(l))

Solution (Python):
import math

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

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

print(ans)

Solution (Python):
N = int(input())
A = input().split()
count = 0

for i in range(N):
  A[i] = int(A[i])
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):
input()
array = list(map(int, input().split()))
max = array[0]
sum = 0

for i in array:
    if i < max:
        sum += max - i
    else:
        max = i

print(sum)

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

print(count)

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

stage_hight = []
for i, ai in enumerate(A):
	if i != 0 and ai < A[i-1]:
		stage_hight.append(A[i-1] - ai)
		A[i] = A[i-1]
print(sum(stage_hight))

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

sum = 0
for i in range(1, len(A)):
    height = A[i] - A[i-1]
    if height < 0:
        sum -= height
        A[i] -= height

print(sum)

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

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

ans = 0
cur_max = 0

for x in a:
    if cur_max > x:
        ans += cur_max - x
    cur_max = max(cur_max, x)

print(ans)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
e=0
s=0
for i in range(1,len(a)):
    if(a[i]>=a[i-1]):
        pass
    else:
        e=abs(a[i]-a[i-1])
        s=s+e
        a[i]=a[i]+e
print(s)

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

ans = 0
prev_max=a[0]

for i in a:
    if prev_max > i:
        ans+= prev_max-i
    elif prev_max < i:
        prev_max = i
        
print(ans)

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

A = list(map(int, input().split()))
l = [0] * len(A)

ans=0

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

Solution (Python):


def main():
    N = int(input())
    A  = [int(i) for i in input().split()]
    ans = 0
    max = A[0]
    for a in A:
        if max > a:
            ans += max-a
        else:
            max = a
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
N = int(input(''))
A1 = np.array(list(map(int, input('').split(' '))))

def func(A):
    step = 0
    for k in range(0,N-1):
        if A[k] > A[k+1]:
            add = A[k] - A[k+1]
            step += add
            A[k+1] += add
    return step
        
print(func(A1))

Solution (Python):



def main(a):
    s = 0
    m = 0
    for ai in a:
        if m < ai:
            m = ai
        else:
            s += m - ai
    return s


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

result = main(A)
print(result)

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

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

print(ans)

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

max_height = As[0]
fumidai = 0
for height in As:
    if height < max_height:
        fumidai += max_height - height
    if height > max_height:
        max_height = height
print(fumidai)

Solution (Python):
import sys
N = sys.stdin.readline()
Alst = list(map(int, sys.stdin.readline().split()))
now = 1
x = 0
for i in Alst:
    if now < i:
        now = i
    else:
        x += (now - i)

print(x)

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

max_a = 0
sum_mod = 0
for a in a_list:
    if max_a > a:
        sum_mod += max_a - a
    else:
        max_a = a
print(sum_mod)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]"):
    ret = 0
    for i in range(1, N):
        tmp = max(0, A[i - 1] - A[i])
        A[i] += tmp
        ret += tmp
    print(ret)
    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):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]



def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


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


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = IL()

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

Solution (Python):
n = int(input())
a = [int(i) for i in 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):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

tot = 0

mx = A[0]
for a in A:
    if a < mx:
        tot += mx - a
    mx = max(mx, a)
print(tot)

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

ans = 0
tem_max = list_A[0]

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