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-----
5
2 1 5 4 3

-----Sample Output-----
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.

Difficulty: EASY

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


def step():
	N = int(input())
	A = list(map(int, input().split()))
	step_height_sum = 0
	for i in range(len(A) - 1):
		if A[i] > A[i + 1]:
			step_height_sum += A[i] - A[i + 1]
			A[i + 1] = A[i]
	return step_height_sum
result = step()
print(result)


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


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


_ = int(input())
s = h = 0
for a in map(int, input().split()):
	h = max(h, a)
	s += h - a
print(s)


from typing import List

def answer(n: int, a: List[int]) -> int:
	height_sum = 0
	current_tallest = 0
	for i in a:
		if i < current_tallest:
			height_sum += current_tallest - i
		elif current_tallest < i:
			current_tallest = i
	return height_sum

def main():
	n = int(input())
	a = list(map(int, input().split()))
	print(answer(n, a))

def __starting_point():
	main()
__starting_point()


n = int(input())
a = list(map(int, input().split()))
mx = 0
res = 0
for i in a:
	res += max(0, mx - i)
	mx = max(mx, i)
print(res)


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


n = int(input())
a = list(map(int, input().split()))
ans = 0
h = a[0]
for x in a:
	ans += max(0, h - x)
	h = max(h, x)
print(ans)


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


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


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]
	else:
		x = a[i]
print(ans)


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


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


def solve(n, a):
	(res, th) = (0, 0)
	for x in a:
		th = max(th, x)
		res += th - x
	return res
n = int(input())
a = list(map(int, input().split()))
print(solve(n, a))


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


n = int(input())
a = map(int, input().split())
m = 0
cut = 0
for i in a:
	if m < i:
		m = i
	elif m >= i:
		cut += m - i
print(cut)


n = int(input())
A = [int(_) for _ in input().split()]
c = A[0]
ans = 0
for a in A:
	if a < c:
		ans += c - a
	else:
		c = a
print(ans)


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


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


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)


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


a = int(input())
lis = list(map(int, input().split()))
ans = 0
for i in range(a - 1):
	if lis[i] > lis[i + 1]:
		j = lis[i] - lis[i + 1]
		ans += j
		lis[i + 1] = lis[i]
print(ans)


(n, *l) = map(int, open(0).read().split())
a = t = 0
for i in l:
	if i > t:
		t = i
	else:
		a += t - i
print(a)


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)


N = int(input())
A = list(map(int, input().split()))
res = 0
last = 0
for a in A:
	res += max(last, a) - a
	last = max(last, a)
print(res)


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


from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
(ans, tmp_max) = (0, 0)
for a in A:
	if a > tmp_max:
		tmp_max = a
	else:
		ans += tmp_max - a
print(ans)


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


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


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)


N = input()
A = map(int, input().split())
sum = 0
pre = 0
for ai in A:
	if pre > ai:
		sum += pre - ai
	else:
		pre = ai
print(sum)


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


n = int(input())
l = list(map(int, input().split()))
mx = l[0]
total = 0
for it in l:
	if it > mx:
		mx = it
	total += mx - it
print(total)


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


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


N = int(input())
A = map(int, input().split())
ans = 0
c_max = 0
for a in A:
	if a > c_max:
		c_max = a
	ans += c_max - a
print(ans)


n = int(input())
alist = list(map(int, input().split()))
ans = 0
b = 0
c = 0
k = 0
for i in range(n):
	b += alist[i]
	c += alist[0 + k]
	if b < c:
		ans += c - b
		b = c
	elif b > c:
		k = i
		c = b
print(ans)


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)


N = int(input())
A = [int(x) for x in input().split()]
res = 0
m = -1
for i in A:
	if m > i:
		res += m - i
	m = max(m, i)
print(res)


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)


n = int(input())
(*a,) = map(int, input().split())
s = 0
for i in range(1, n, 1):
	d = a[i - 1] - a[i]
	if d > 0:
		s += d
		a[i] += d
print(s)


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


n = int(input())
l = [*map(int, input().split())]
t = l[0]
a = 0
for i in l:
	if i > t:
		t = i
	a += t - i
print(a)


n = int(input())
l = map(int, input().split())
ans = 0
maxt = 0
for i in l:
	if i < maxt:
		ans += maxt - i
	maxt = max(i, maxt)
print(ans)


N = int(input())
num_list = list(map(int, input().split()))
ANS = 0
tall = 0
pre = 0
for i in range(N):
	if tall > num_list[i]:
		pre = tall - num_list[i]
		ANS += pre
		tall = num_list[i] + pre
	else:
		tall = num_list[i]
print(ANS)


N = int(input())
nums = [int(i) for i in input().split()]
before = 0
ans = 0
for i in range(N):
	if i == 0:
		before = nums[i]
		continue
	if before > nums[i]:
		ans += before - nums[i]
	else:
		before = nums[i]
print(ans)


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


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)


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


import sys
from sys import stdin

def I():
	return stdin.readline().rstrip()

def MI():
	return map(int, stdin.readline().rstrip().split())

def LI():
	return list(map(int, stdin.readline().rstrip().split()))
n = int(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)


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


n = int(input())
arr = list(map(int, input().split()))
res = 0
m = 0
for a in arr:
	m = max(m, a)
	res += max(m - a, 0)
print(res)


N = int(input())
A = list(map(int, input().split()))
h = 0
tot = 0
for a in A:
	tot += max(h - a, 0)
	h = max(h, a)
print(tot)


n = int(input())
a = list(map(int, input().split()))
p = a[0]
ans = 0
for v in a:
	ans += max(0, p - v)
	p = max(p, v)
print(ans)


n = int(input())
a = list(map(int, input().split()))
cur = 0
ans = 0
for i in a:
	cur = max(cur, i)
	ans += cur - i
print(ans)


N = int(input())
pre = 0
ans = 0
for a in input().split():
	a = int(a)
	ans += max(pre - a, 0)
	pre = max(pre, a)
print(ans)


N = int(input())
A = list(map(int, input().split()))
tot = 0
for (i, a) in enumerate(A):
	if i == 0:
		forward = a
		continue
	if forward >= a:
		tot += forward - a
	else:
		forward = a
print(tot)


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)


def main():
	N = int(input())
	A = list(map(int, input().split()))
	dp = [0] * N
	for i in range(1, N):
		h = A[i - 1] - A[i]
		if h > 0:
			A[i] += h
			dp[i] = dp[i - 1] + h
		else:
			dp[i] = dp[i - 1]
	print(dp[-1])
	return

def __starting_point():
	main()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
A_sub = A[1:]
a_before = A[0]
count = 0
for a in A_sub:
	if a < a_before:
		count += a_before - a
	else:
		a_before = a
print(count)


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


n = int(input())
lst = list(map(int, input().split()))
now = lst[0]
sm = 0
for i in range(1, n):
	if lst[i] >= now:
		now = lst[i]
		continue
	sm += now - lst[i]
print(sm)


n = int(input())
a_max = 0
t = 0
for x in map(int, input().split(' ')):
	a_max = x if x > a_max else a_max
	t += a_max - x
print(t)


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


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)


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)


n = int(input())
a = list(map(int, input().split()))
m = 0
ans = 0
for ai in a:
	m = max(m, ai)
	ans += m - ai
print(ans)


N = int(input())
A = list(map(int, input().split()))
m = A[0]
ans = 0
for i in A:
	ans += abs(m - i) if i <= m else 0
	m = max(m, i)
print(ans)


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


N = input()
A = input().split()
ans = 0
j = 0
for i in A:
	if j > int(i):
		ans += j - int(i)
	else:
		j = int(i)
print(ans)


def main(A):
	ans = 0
	max_num = A[0]
	for a in A:
		max_num = max(max_num, a)
		ans += max_num - a
	return ans

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


n = int(input())
(mn, *a) = map(int, input().split())
ans = 0
for i in a:
	if i < mn:
		ans += mn - i
	else:
		mn = i
print(ans)


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


n = int(input())
A = [*map(int, input().split())]
s = maxA = 0
for a in A:
	maxA = max(maxA, a)
	s += maxA - a
print(s)


N = int(input())
A = list(map(int, input().split()))
p = A[0]
ans = 0
for a in A[1:]:
	ans += max(0, p - a)
	p = max(p, a)
print(ans)


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(A[i + 1], A[i])
print(ans)


n = int(input())
A = list(map(int, input().split()))
h = 0
ans = 0
for a in A:
	if a < h:
		ans += h - a
	h = max(h, a)
print(ans)


N = int(input())
A = input()
Aa = A.split()
i = 0
total = 0
for i in range(N - 1):
	left = int(Aa[i])
	right = int(Aa[i + 1])
	diff = 0
	if left > right:
		diff = abs(right - left)
		total = total + diff
		Aa[i + 1] = right + diff
	else:
		total = total + 0
print(total)


n = int(input())
a = input().split()
a = [int(i) for i in a]
num = 0
maxa = a[0]
for i in range(1, len(a)):
	if maxa < a[i]:
		maxa = a[i]
	elif maxa > a[i]:
		num += maxa - a[i]
	else:
		pass
print(num)


N = int(input())
A = list(map(int, input().split()))
a = 0
s = []
for i in A:
	if i < a:
		s.append(a - i)
	else:
		a = i
print(sum(s))


import math

def cin():
	return list(map(int, input().split()))
N = int(input())
data = cin()
total = 0
for i in range(N - 1):
	if data[i + 1] < data[i]:
		total += data[i] - data[i + 1]
		data[i + 1] = data[i]
print(total)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(rs())

def rs_():
	return [_ for _ in rs().split()]

def ri_():
	return [int(_) for _ in rs().split()]
N = ri()
A = ri_()
ans = 0
tmp = A[0]
for i in range(1, N):
	if tmp > A[i]:
		ans += tmp - A[i]
	else:
		tmp = A[i]
print(ans)


n = int(input())
l = [int(x) for x in input().split(' ')]
m = 0
ans = 0
for i in range(n):
	ans += max(0, m - l[i])
	m = max(m, l[i])
print(ans)


N = int(input())
A = list(map(int, input().split()))
s = 0
x = 0
for i in A:
	if i < x:
		s += x - i
	else:
		x = i
print(s)


input()
r = m = 0
for x in map(int, input().split()):
	r += m - min(m, x)
	m = max(m, x)
print(r)


(n, p, *v) = map(int, open(0).read().split())
a = 0
for t in v:
	if p > t:
		a += p - t
	else:
		p = t
print(a)


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


n = int(input())
a = list(map(int, input().split()))
asum = sum(a)
for i in range(1, n):
	if a[i] < a[i - 1]:
		a[i] = a[i - 1]
print(sum(a) - asum)


n = int(input())
A = tuple(map(int, input().split()))
ans = 0
m = A[0]
for a in A[1:]:
	ans += max(m - a, 0)
	m = max(a, m)
print(ans)


n = int(input())
a = list(map(int, input().split()))
res = val = 0
for i in a:
	res += val - min(val, i)
	val = max(val, i)
print(res)


n = int(input())
alist = list(map(int, input().split()))
res = 0
ma = alist[0]
for a in alist:
	if a > ma:
		ma = a
	else:
		res += ma - a
print(res)


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


N = int(input())
A = list(map(int, input().split()))
before = 0
ans = []
for i in A:
	ans.append(max(0, before - i))
	before = i + ans[-1]
print(sum(ans))


N = input()
A = list(map(int, input().split()))
c = 0
mae = A[0]
for i in A:
	if i < mae:
		c += mae - i
	else:
		mae = i
print(c)


n = int(input())
a = [int(x) for x in input().split()]
ma = 0
ans = 0
for i in a:
	ma = max(ma, i)
	ans += ma - i
print(ans)


input()
A = list(map(int, input().split()))
(h, res) = (A[0], 0)
for i in A:
	if i <= h:
		res += h - i
	else:
		h = i
print(res)


n = int(input())
A = list(map(int, input().split()))
now = 0
ans = 0
for a in A:
	if now > a:
		ans += now - a
	else:
		now = a
print(ans)


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


N = int(input())
a = list(map(int, input().split()))
max_height = -1
s = 0
for i in a:
	max_height = max(max_height, i)
	d = max_height - i
	s += d
print(s)


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)


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


n = int(input())
l = list(map(int, input().split()))
al = []
for i in range(0, n - 1):
	if l[i] <= l[i + 1]:
		al.append(0)
	else:
		al.append(l[i] - l[i + 1])
		l[i + 1] = l[i]
print(sum(al))


N = int(input())
A = list(map(int, input().split()))
(m, s) = (0, 0)
for a in A:
	s += max(m - a, 0)
	m = max(m, a)
print(s)


n = int(input())
L = list(map(int, input().split()))
m = 0
ans = 0
for l in L:
	m = max(m, l)
	ans += m - l
print(ans)


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


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


n = int(input())
a = list(map(int, input().split()))
num = 0
k = a[0]
for i in a:
	if i > k:
		k = i
	if i < k:
		num += k - i
print(num)


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


n = int(input())
people = list(map(int, input().split()))
count = 0
for i in range(n):
	if i == 0 or people[i - 1] <= people[i]:
		continue
	else:
		count += people[i - 1] - people[i]
		people[i] += people[i - 1] - people[i]
print(count)


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)


def input_int():
	return int(input())

def int1(x):
	return int(x) - 1

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

def input_to_int_tuple():
	return tuple(map(int, input().split()))

def input_to_int_tuple_minus1():
	return tuple(map(int1, input().split()))

def main():
	N = input_int()
	A = input_to_int_tuple()
	result = 0
	max_a = 0
	for a in A:
		if max_a > a:
			result += max_a - a
		max_a = max(a, max_a)
	return result

def __starting_point():
	print(main())
__starting_point()


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)


N = int(input())
As = list(map(int, input().split()))
(tlest, total) = (As[0], 0)
for i in range(1, N):
	tlest = max(tlest, As[i])
	if tlest - As[i]:
		total += tlest - As[i]
print(total)


n = int(input())
l = list(map(int, input().split()))
t = 0
ans = 0
for i in l:
	if i < t:
		ans += t - i
	else:
		t = i
print(ans)


N = int(input())
A = list(map(int, input().split()))
ans = 0
mx = 0
for x in A:
	if mx >= x:
		ans += mx - x
	else:
		mx = x
print(ans)


n = int(input())
arr = [int(x) for x in input().split()]
heights = 0
for i in range(1, len(arr)):
	if arr[i - 1] > arr[i]:
		heights += arr[i - 1] - arr[i]
		arr[i] = arr[i - 1]
print(heights)


n = int(input())
m = list(map(int, input().split()))
k = 0
s = 0
for i in range(n):
	s += max(0, k - m[i])
	k = max(k, m[i])
print(s)


N = int(input())
M = map(int, input().split())
Mx = 0
Total = 0
for n in M:
	Mx = max([Mx, n])
	Total += Mx - n
print(Total)


n = int(input())
a = [int(s) for s in input().split()]
allsum = sum(a)
for i in range(1, n):
	if a[i] < a[i - 1]:
		a[i] = a[i - 1]
print(sum(a) - allsum)


n = int(input())
a = list(map(int, input().split()))
memo = 0
ans = 0
for j in a:
	if memo > j:
		ans += memo - j
	else:
		memo = j
print(ans)


N = int(input())
A = list(map(int, input().split()))
H = 0
M = 0
for i in A:
	if i < M:
		H += M - i
	else:
		M = i
print(H)


n = int(input())
a = list(map(int, input().split(' ')))
a_max = 0
step = 0
for i in range(n):
	if a_max < a[i]:
		a_max = a[i]
		a_num = i
	else:
		step += a_max - a[i]
print(step)


input()
(step, height) = (0, 0)
for i in map(int, input().split()):
	if height > i:
		step += height - i
	height = max(height, i)
print(step)


n = input()
x = [int(i) for i in input().split()]
tmp = x[0]
tot = 0
for i in x:
	if i <= tmp:
		tot += tmp - i
	else:
		tmp = i
print(f'{tot}')


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)


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


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


n = int(input())
l = list(map(int, input().split()))
max = l[0]
c = 0
for x in l:
	if max < x:
		max = x
	else:
		c += max - x
print(c)


N = int(input())
li = list(map(int, input().split()))
ans = 0
h = li[0]
for l in li[1:]:
	h = max(h, l)
	ans += h - l
print(ans)


N = int(input())
A = map(int, input().split())
ma = 0
ans = 0
for a in A:
	ans -= max(0, ma - a)
	ma = max(ma, a)
print(-ans)


n = int(input())
s = 0
x = list(map(int, input().split()))
z = x[0]
for i in range(len(x)):
	if z > x[i]:
		s += z - x[i]
	else:
		z = x[i]
print(s)


n = int(input())
a = [int(i) for i in input().split()]
c = 0
t = a[0]
for i in a:
	if t > i:
		b = t - i
		c += b
	else:
		t = i
print(c)


def main():
	N = int(input())
	a = list(map(int, input().split()))
	DP = [0] * N
	max_i = 0
	for i in range(1, N):
		if a[max_i] > a[i]:
			DP[i] += a[max_i] - a[i]
		elif a[max_i] < a[i]:
			max_i = i
	print(sum(DP))

def __starting_point():
	main()
__starting_point()


N = input()
A = [int(_) for _ in input().split()]
x = 0
amax = 0
for a in A:
	if amax > a:
		x += amax - a
	else:
		amax = a
print(x)


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)


(n, *a) = map(int, open(0).read().split())
s = a[0]
ans = 0
for i in range(1, n):
	if s > a[i]:
		ans += s - a[i]
	else:
		s = a[i]
print(ans)


n = int(input())
A = [int(x) for x in input().split()]
(p, t) = (A[0], 0)
for a in A:
	if p > a:
		t += p - a
	else:
		p = a
print(t)


n = int(input())
lst = list(map(int, input().split()))
_max = lst[0]
st = 0
for i in lst:
	if i >= _max:
		_max = i
	else:
		st = st + (_max - i)
print(st)


n = int(input())
a = list(map(int, input().split()))
k = a[0]
s = 0
for i in a[1:]:
	s += max(k - i, 0)
	k = max(i, k)
print(s)


n = int(input())
ans = 0
pre = 0
for i in list(map(int, input().split())):
	if i < pre:
		ans += pre - i
	else:
		pre = i
print(ans)


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


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


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


N = int(input())
A = list(map(int, input().split()))
ans = 0
t = A[0]
for a in A:
	d = max(0, t - a)
	ans += d
	t = max(t, a)
print(ans)


N = int(input())
A = list(map(int, input().split()))
result = 0
minimum = 0
for a in A:
	if a > minimum:
		minimum = a
	elif a < minimum:
		result += minimum - a
print(result)


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


n = int(input())
x = list(map(int, input().split()))
c = 0
tot = 0
for i in range(n - 1):
	if x[i] > x[i + 1]:
		c = x[i] - x[i + 1]
		tot = tot + c
		x[i + 1] = x[i + 1] + c
print(tot)


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


import sys
N = int(input())
A = list(map(int, input().split()))
m = A[0]
count = 0
for a in A:
	if m > a:
		count += m - a
	else:
		m = a
print(count)


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)


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


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


n = int(input())
heights = list(map(int, input().split()))
humidais = []
for i in range(n):
	if i >= 1:
		if heights[i - 1] > heights[i]:
			humidai = heights[i - 1] - heights[i]
			humidais.append(humidai)
			heights[i] = heights[i] + humidai
ans = 0
for i in range(len(humidais)):
	ans += humidais[i]
print(ans)


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


s = int(input())
x = list(map(int, input().split()))
sum = 0
maxi = x[0]
for i in x:
	maxi = max(maxi, i)
	sum += maxi - i
print(sum)


n = int(input())
l = list(map(int, input().split()))
p = l[0]
s = 0
for i in l[1:]:
	s += max(0, p - i)
	p = max(p, i)
print(s)


n = int(input())
A = [int(i) for i in input().split()]
ans = 0
se = 0
for i in A:
	se = max(se, i)
	ans += se - i
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
if a == sorted(a):
	print(ans)
	return
b = a.index(max(a))
for i in range(0, b):
	if a[i] > a[i + 1]:
		ans = ans + a[i] - a[i + 1]
		a[i + 1] = a[i]
ans = ans + max(a) * len(a[b + 1:]) - sum(a[b + 1:])
print(ans)


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


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)


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


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


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


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)


N = int(input())
A = list(map(int, input().split(' ')))
count = 0
m = 0
for i in A:
	if i >= m:
		m = i
	else:
		count += m - i
print(count)


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


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


def steps():
	biggerone = 0
	ans = 0
	list_a = []
	cnt = int(input())
	a = input()
	list_a = [int(x) for x in a.split()]
	if len(list_a) != cnt:
		print('Wrong input')
		if ans == 0:
			ans = None
		steps()
	else:
		for i in range(0, len(list_a) - 1):
			if i == 0:
				biggerone = list_a[i]
			if biggerone > list_a[i + 1]:
				ans += biggerone - list_a[i + 1]
			else:
				biggerone = list_a[i + 1]
	print(ans)
steps()


N = int(input())
total = 0
h = 0
for A in map(int, input().split()):
	if h > A:
		total += h - A
	else:
		h = A
print(total)


input()
A = list(map(int, input().split()))
max_A = 0
sum_A = 0
for Ai in A:
	max_A = max(Ai, max_A)
	if max_A > Ai:
		sum_A += max_A - Ai
print(sum_A)


n = int(input())
A = list(map(int, input().split()))
m = A[0]
s = 0
for a in A[1:]:
	s += max(0, m - a)
	m = max(a, m)
print(s)


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


N = int(input())
a = [int(i) for i in input().split()]
ans = 0
mx = a[0]
for i in a:
	if i < mx:
		ans += mx - i
	else:
		mx = i
print(ans)


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


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


n = int(input())
a = list(map(int, input().split()))
maxa = 0
total = 0
for i in a:
	maxa = max(maxa, i)
	total = total + maxa - i
print(total)


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
maxa = 0
for aa in a:
	maxa = max(maxa, aa)
	ans += maxa - aa
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
max_a = a[0]
for i in range(1, n):
	if max_a > a[i]:
		ans += max_a - a[i]
	elif max_a == a[i]:
		pass
	else:
		max_a = a[i]
print(ans)


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


n = int(input())
ass = list(map(int, input().split()))
s = 0
f = ass[0]
for a in ass:
	if f > a:
		s += f - a
	else:
		f = a
print(s)


N = int(input())
A_l = map(int, input().split())
M = 0
B = 0
for i in A_l:
	if i >= M:
		B += 0
		M = i
	else:
		B += M - i
print(B)


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)


input()
b = list(map(int, input().split()))
ans = 0
tall = 0
for i in b:
	if tall > i:
		ans += tall - i
	else:
		tall = i
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
b = a[0]
c = 0
for i in range(1, n):
	if a[i] < b:
		c += b - a[i]
	else:
		b = a[i]
print(c)


N = int(input())
As = list(map(int, input().split()))
ans_step_sum = 0
current_max_height = As[0]
for Ai in As[1:]:
	diff = current_max_height - Ai
	if diff > 0:
		ans_step_sum += diff
	else:
		current_max_height = Ai
print(ans_step_sum)


N = int(input())
AA = list(map(int, input().split()))
s = 0
key = 0
for i in range(N):
	key = max(key, AA[i])
	s += key
print(s - sum(AA))


n = int(input())
cur = 0
step = 0
for a in map(int, input().split()):
	if a < cur:
		step += cur - a
	else:
		cur = a
print(step)


a = input()
b = list(map(int, input().split()))
x = 0
y = 0
for i in b:
	if i <= x:
		y += x - i
	else:
		x = i
print(y)


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)


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


n = int(input())
A = list(map(int, input().split()))
ans = 0
prev_h = 0
for (i, h) in enumerate(A):
	if i == 0:
		prev_h = h
	elif h < prev_h:
		ans += prev_h - h
	else:
		prev_h = h
print(ans)


(_, A) = open(0)
m = x = 0
for a in map(int, A.split()):
	x += max(m - a, 0)
	m = max(m, a)
print(x)


pre = 0
n = int(input())
ans = 0
for i in map(int, input().split()):
	if i < pre:
		ans += pre - i
	pre = max(pre, i)
print(ans)


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


input()
ans = 0
tmp = 0
for i in map(int, input().split()):
	if tmp < i:
		tmp = i
	else:
		ans += tmp - i
print(ans)


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)


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


MOD = 10 ** 9 + 7
N = int(input())
AN = list(map(int, input().split()))
ans = 0
m = AN[0]
for i in range(1, N):
	if m > AN[i]:
		ans += m - AN[i]
	m = max(m, AN[i])
print(ans)


(n, *a) = map(int, open(0).read().split())
b = 0
for i in range(1, n):
	if a[i] < a[i - 1]:
		b += a[i - 1] - a[i]
		a[i] = a[i - 1]
print(b)


N = int(input())
A = [int(d) for d in input().split()]
S = []
X = 0
for i in range(N):
	if X < A[i]:
		X = A[i]
	else:
		pass
	S.append(X - A[i])
print(sum(S))


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


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


class Person:

	def __init__(self, num, height):
		self.num = num
		self.height = height

	@staticmethod
	def static():
		print('static')

def main():
	number = int(input())
	person_list = list(map(int, input().split()))
	ans = 0
	for (num, person) in enumerate(person_list):
		if num > 0:
			if person < person_list[num - 1]:
				person_list[num] = person_list[num - 1]
				ans += person_list[num - 1] - person
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, S, *A) = map(int, open(0).read().split())
ans = 0
for i in A:
	if i > S:
		S = i
	else:
		ans += S - i
print(ans)


N = int(input())
A = list(map(int, input().split()))
h = 0
r = 0
for a in A:
	if a < h:
		r += h - a
	h = max(h, a)
print(r)


N = int(input())
li = list(map(int, input().split()))
a = 0
b = 0
for i in li:
	if a < i:
		a = i
	if i < a:
		b = b + a - i
print(b)


def c176(n, alist):
	ans = 0
	tmp = alist[0]
	for i in alist:
		if tmp <= i:
			tmp = i
			continue
		ans += tmp - i
	return ans

def main():
	n = int(input())
	alist = list(map(int, input().split()))
	print(c176(n, alist))

def __starting_point():
	main()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
h = 0
r = 0
for v in A:
	need = 0 if v >= h else h - v
	r += need
	h = need + v
print(r)