CodeEff/ECCO huggingface dataset:
# Original solution
import os
import string
import sys
from collections import Counter
from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)

lefts = [{c: 0 for c in string.ascii_lowercase} for _ in range(N)]
rights = [{c: 0 for c in string.ascii_lowercase} for _ in range(N)]
for i in range(1, N):
    for c in string.ascii_lowercase:
        lefts[i][c] = lefts[i - 1][c]
    lefts[i][S[i - 1]] += 1
for i in reversed(list(range(N - 1))):
    for c in string.ascii_lowercase:
        rights[i][c] = rights[i + 1][c]
    rights[i][S[i + 1]] += 1
total = Counter(S)


def dump(counter):
    for c in sorted(counter.keys()):
        if counter[c] > 0:
            print((c, counter[c]))
    print()


@lru_cache(maxsize=None)
def count_palindrome(l, r):
    if r <= l:
        assert False
    if l + 1 == r:
        return 1
    counts = total.copy()
    for c in string.ascii_lowercase:
        counts[c] -= lefts[l][c]
        counts[c] -= rights[r - 1][c]
    odds = 0
    for c in list(counts.values()):
        odds += c % 2
    if odds <= 1:
        return 1

    ret = INF
    for i in range(l + 1, r):
        # i で split するときの両側の数
        cnt = count_palindrome(l, i) + count_palindrome(i, r)
        ret = min(cnt, ret)
    return ret


ans = count_palindrome(0, N)
print(ans)


# Optimized solution
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


# 解説
S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)

cs = [0] * N
h = 0
for i, c in enumerate(S):
    h ^= 1 << (ord(c) - ord('a'))
    cs[i] = h


def dump(h):
    import numpy as np
    print((np.binary_repr(h, 26)))


def dumpdp(dp):
    for k in list(dp.keys()):
        if dp[k] < INF:
            dump(k)
            print((dp[k]))
    print()


# dp[h]: それまでのハッシュが h である位置以前の分割方法の最小値
dp = defaultdict(lambda: INF)
dp[0] = 0
dp[cs[0]] = 1
for i in range(1, N):
    # print(S[i - 1])
    # dumpdp(dp)

    h = cs[i]
    if h == 0:
        continue
    d = 1
    while d < (1 << 26):
        # i から h^d の位置までまとめて 1 つ切り出せる
        dp[h] = min(dp[h], dp[h ^ d] + 1)
        d <<= 1

if cs[-1] == 0:
    print((1))
else:
    print((dp[cs[-1]]))
# Original solution
def main():
    import sys
    from collections import defaultdict
    from bisect import bisect_left
    input = sys.stdin.readline

    S = input().rstrip('\n')
    N = len(S)

    A = [ord(s)-97 for s in S]
    H = [0] * (N+1)
    h2i = defaultdict(list)
    for i in range(N):
        H[i+1] = H[i] ^ (1 << A[i])
        h2i[H[i+1]].append(i+1)

    dp = [N+1] * (N+1)
    dp[0] = 0
    for i in range(N):
        h = H[i]
        if h2i[h]:
            ii_list = h2i[h]
            ii = bisect_left(ii_list, i+1)
            if ii < len(ii_list):
                ii = ii_list[ii]
                if i:
                    dp[ii] = min(dp[ii], dp[i])
                else:
                    dp[ii] = 1
        for j in range(26):
            new_h = h ^ (1 << j)
            if h2i[new_h]:
                ii_list = h2i[new_h]
                ii = bisect_left(ii_list, i+1)
                if ii < len(ii_list):
                    ii = ii_list[ii]
                    dp[ii] = min(dp[ii], dp[i] + 1)
    print((dp[-1]))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.readline

    S = input().rstrip('\n')
    N = len(S)

    A = [ord(s)-97 for s in S]
    H = [0] * (N+1)
    h2i = {}
    for i in range(N):
        H[i+1] = H[i] ^ (1 << A[i])
        if H[i+1] in h2i:
            h2i[H[i+1]].append(i+1)
        else:
            h2i[H[i+1]] = [i+1]

    dp = [N+1] * (N+1)
    dp[0] = 0
    pow2 = [2**i for i in range(26)]
    for i in range(N):
        h = H[i]
        if h in h2i:
            ii_list = h2i[h]
            ii = bisect_left(ii_list, i+1)
            if ii < len(ii_list):
                ii = ii_list[ii]
                if i:
                    dp[ii] = min(dp[ii], dp[i])
                else:
                    dp[ii] = 1
        for j in pow2:
            new_h = h ^ j
            if new_h in h2i:
                ii_list = h2i[new_h]
                ii = bisect_left(ii_list, i+1)
                if ii < len(ii_list):
                    ii = ii_list[ii]
                    dp[ii] = min(dp[ii], dp[i] + 1)
    print((dp[-1]))


if __name__ == '__main__':
    main()
# Original solution
while 1:
    n,a,b=list(map(int,input().split()))
    if n==a==b==0:break
    c=0;d=[1]+[0]*2000000
    for i in range(0,n+1):
        if d[i]:d[i+a]=d[i+b]=1
        else:c+=1
    print(c)

# Optimized solution
while 1:
    n,a,b=list(map(int,input().split()))
    if n==a==b==0:break
    c=0;d=[1]*(n+1)
    for x in range(a):
        for i in range(x*b,n+1,a):d[i]=0
    print((sum(d)))
# Original solution
N = int(eval(input()))
array = list(map(int, input().split()))
ans = 0
next = 1
a = True
while (a):
    a = False
    b = False
    for i in range(next,len(array)):
        if array[i] < array[i-1]:
            array[i] = array[i] + 1
            ans += 1
            a = True
            if b == False:
                b= True
                next = i

print(ans)


# Optimized solution
N = int(eval(input()))
array = list(map(int, input().split()))
ans = 0
next = 1
a = True
while (a):
    a = False
    b = False
    for i in range(next,len(array)):
        if array[i] < array[i-1]:
            ans += array[i-1] - array[i]
            array[i] = array[i - 1]
            a = True
            if b == False:
                b= True
                next = i

print(ans)
# Original solution
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, *a = list(map(int, read().split()))
    #a2 = a[::-1]
    r = 0

    for i1 in range(1, n):
        if max(a[:i1]) > a[i1]:
            r += max(a[:i1]) - a[i1]
    print(r)


if __name__ == '__main__':
    main()


# Optimized solution
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = list(map(int, read().split()))
    r = 0
    st = a[0]
    for ae in a[1:]:
        if st > ae:
            r += st - ae
        else:
            st = ae
    print(r)


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
ans = 0
m = a[0]
for i in range(1, n):
    m = max(a[i-1], m)
    if a[i] - m < 0:
        ans += abs(a[i] - m)
print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
m = 1
ans = 0
for i in range(n):
    if m > a[i]:
        ans += m - a[i]
    m = max(a[i], m)
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

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

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

fllor = a[0]
ans = 0
for i in range(n):
    if fllor > a[i]:
        ans += fllor - a[i]
    else:
        fllor = max(fllor, a[i])
print(ans)
# Original solution
# coding: utf-8
# Your code here!

# import math
# from decimal import Decimal
# from collections import deque, Counter
# from itertools import product


def LI():
    return list(map(int,input().split()))
    
def OPEN():
    return list(map(int,open(0).read().split()))
    

n,*a=OPEN()

now=a[0]
ans=[]
for i in range(n):
    if a[i]<now:
        ans+=(now-a[i]),
        a[i]=now
    now=a[i]
    
print((sum(ans)))
        

# Optimized solution
n,*a=list(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)
# Original solution
n = int(eval(input()))
wa = 0
l = list(map(int, input().split()))
for i in range(1, n):
    if max(l[:i]) >= l[i]:
        wa += max(l[:i]) - l[i]
print(wa)

# Optimized solution
n = int(eval(input()))
wa = 0
m = 0
l = list(map(int, input().split()))
for i in range(n):
    m = max(l[i], m)
    if m > l[i]:
        wa += m - l[i]
print(wa)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
s=0
if sorted(a) == a:
    print(s)
    exit()
while len(a)>0:
    x=max(a)
    k=a.index(x)
    s=s+x*(len(a)-1-k)-sum(a[k+1:])
    a=a[:k]
print(s)





# Optimized solution
n=int(eval(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)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
count = 0
for i in range (0,N-1):
    while(A[i]>A[i+1]):
        count += 1
        A[i+1] += 1
    if(A[i]<=A[i+1]):
        continue

print(count)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
count = 0
max = A[0]
for i in range (0,N):
    while(max>A[i]):
        count = count + (max - A[i])
        A[i] = max
       # print(A)
    if(max<=A[i]):
        max = A[i]
      #  print(A)
print(count)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))

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

print(ans)


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))

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

print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

mae = []
stepsum = 0
for i in A:
  if mae and max(mae) > i:
    stepsum += max(mae)-i
  mae.append(i)

print(stepsum)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

heighest = 0
stepsum = 0
for i in A:
  if heighest < i:
    heighest = i
    continue
  else:
    stepsum += heighest-i

print(stepsum)
# Original solution
def main():
    n = int(eval(input()))
    a = [int(x) for x in input().split()]
    cnt = 0
    while n > 1:
        a_max = max(a[:n])
        a_max_i = a.index(a_max)
        if a_max_i != n-1:
            cnt += (n - a_max_i) * a_max - sum(a[a_max_i:n])
        n = a_max_i
    print(cnt)

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    n = int(eval(input()))
    A = [int(x) for x in input().split()]
    maxv = 0
    ans = 0
    for a in A:
        if maxv > a:
            ans += maxv - a
        else:
            maxv = a  
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
a=int(eval(input()))
b=list(map(int,input().split()))
c=0
if a==0:
  print((0))
else:
  for i in range(a-1):
    while b[i]>b[i+1]:
      b[i+1]=b[i+1]+1
      c=c+1
  print(c)

# Optimized solution
a=int(eval(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)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
x = 0
M = a[0]
if n != 1:
    for i in range(n - 1):
        if M > a[i + 1]:
            x += (M - a[i + 1])
        else:
            M = a[i + 1]
print(x)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
x = 0
h = a[0]
for i in range(n):
    if a[i] > h:
        h = a[i]
    else:
        x += (h - a[i])
print(x)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
 
x = 0
for i in range(n-1):
  while True:
    if a[i] > a[i + 1]:
      a[i + 1] += 1
      x += 1
    if a[i] <= a[i + 1]:
      break
 
print(x)

# Optimized solution
N = int(eval(input()))
A = [int(x) for x 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)
# Original solution
n = int(eval(input()))
li = list(map(int,input().split()))
ans = 0
for i in range(1,n):
    while li[i]<li[i-1]:
        li[i] +=1
        ans+=1
print(ans)

# Optimized solution
n = int(eval(input()))
li = list(map(int,input().split()))
ma = 0
ans = 0
for i in range(n):
    if li[i]>ma:
        ma = li[i]
    else:
        ans+=ma-li[i]
print(ans)
# Original solution
import sys
cin = sys.stdin.readline
n = int(cin())
height = list(map(int, cin().split()))
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)


# Optimized solution
import sys
cin = sys.stdin.readline
n = cin()
height = list(map(int, cin().split()))
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)
# Original solution
#!/usr/local/bin/python3
N = int(eval(input()))
A = list(map(int, input().split()))[::-1]

ans = 0
while N != 0:
    m = max(A)
    idx = A.index(m) + 1
    ans += (len(A[:idx]) * m) - sum(A[:idx])
    A = A[idx:]
    N -= idx

print(ans)


# Optimized solution
#!/usr/local/bin/python3
N = int(eval(input()))
A = list(map(int, input().split()))

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

print(ans)
# Original solution
from sys import stdin, setrecursionlimit
#input = stdin.buffer.readline
setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18

N = int(eval(input()))
#N, M = map(int, input().split())
A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
#ABC = [list(map(int, input().split())) for _ in range(N)]
#S = input()
#S = [input() for _ in range(N)]

if N == 1:
    print((0))
    exit()
    
answer = 0
max_a = A[0]

for a in A[1:]:
    max_a = max(max_a, a)
    answer += max_a - a

print(answer)

# Optimized solution
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline

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

answer = 0
max_a = A[0]

for a in A:
    max_a = max(max_a, a)
    answer += max_a - a

print(answer)
# Original solution
def main():
    _ = int(eval(input()))
    A = [int(i) for i in input().split()]
    ma = A[0]
    ans = 0
    for a in A[1:]:
        if ma > a:
            ans += ma - a
        if ma < a:
            ma = a
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = [int(i) for i in input().split()]
    ma = A[0]
    ans = 0
    for a in A:
        ma = max(ma, a)
        ans += ma - a
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
li = list(map(int, input().split()))
a = []
b = 0
for i in li:
  a.append(i)
  c = max(a)
  if i < c:
    b = b + c - i
print(b)
    

# Optimized solution
N = int(eval(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)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
ans = 0

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



# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
ans = 0

for i in range(1,n):
    if a[i] >= a[i-1]:
        continue
    else:
        step = a[i-1] - a[i]
        a[i] += step
        ans += step
print(ans)
# Original solution
n = int(eval(input()))
num_list = [int(i) for i in input().split()]
count = 0

while num_list:
    max_num_index = num_list.index(max(num_list))
    for i in range(max_num_index,len(num_list)):
        count += (num_list[max_num_index]-num_list[i])
        num_list[i] = num_list[max_num_index]
    num_list.remove(num_list[max_num_index])

print(count)

# Optimized solution
n = int(eval(input()))
num_list = [int(i) for i in input().split()]
count = 0

for i in range(1,n):
    if num_list[i] < num_list[i-1]:
        count += (num_list[i-1] - num_list[i])
        num_list[i] = num_list[i-1]

print(count)
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    h = A[0]
    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))

    tallest = A[0]
    ans = 0

    for height in A:
        if height < tallest:
            ans += tallest - height
        elif height > tallest:
            tallest = height

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n=int(eval(input()))
arr=list(map(int,input().split()))
c=0
for i in range(1,n):
    m=0
    for j in range(i,0,-1):
       
        if arr[j-1]>m:
            m=arr[j-1]
        
    if arr[i]<m:
        c+=m-arr[i]
    
print(c)

# Optimized solution
n=int(eval(input()))
arr=list(map(int,input().split()))
c=0
for i in range(1,n):
    if arr[i]<arr[i-1]:
        c+=arr[i-1]-arr[i]
        arr[i]+=arr[i-1]-arr[i]
        
print(c)
# Original solution
# -*- coding: utf-8 -*-
# 標準入力を取得
N = int(eval(input()))
A = list(map(int, input().split()))

# 求解処理
ans = 0
highest = 0
for n in range(N):
    highest = max(highest, A[n])
    ans += max(highest - A[n], 0)

# 結果出力
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(eval(input()))
    A = list(map(int, input().split()))

    return N, A


def main(A: list) -> None:
    """
    メイン処理.

    Args:\n
        A (list): 身長(1 <= A_i <= 10**9)
    """
    # 求解処理
    ans = 0
    highest = 0
    for A_n in A:
        highest = max(highest, A_n)
        ans += max(highest - A_n, 0)

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, A = get_input()

    # メイン処理
    main(A)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
sum = 0
for i in range(1, len(a)):
	if a[i-1] > a[i]:
		while not a[i] == a[i-1]:
			a[i] += 1
			sum += 1
print(sum)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
sum = 0
for i in range(1, len(a)):
	if a[i-1] > a[i]:
		sum += a[i - 1] - a[i]
		a[i] = a[i - 1]
print(sum)
# Original solution
N = int(eval(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)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split(' ')))
ans = 0
gap = 0
for i in range(1,N):
  if ( A[i-1] + gap ) > A[i]:
    gap = ( A[i-1] + gap ) - A[i]
  else:
    gap = 0
  ans += gap
    
print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
ans=sum(A)
s=N+1
j=s

for i in range(100000):
    if  A==sorted(A):
        break
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s

print((sum(A)-ans))

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
ans=sum(A)

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

print((sum(A)-ans))
# Original solution
n=int(eval(input()))
s=list(map(int,input().split()))
m=0
for i in range(n-1):
    if s[i+1] < s[i]:
        m+= s[i] - s[i+1]
        s[i+1] = s[i]
print(m)

# Optimized solution
n=int(eval(input()))
a = list(map(int,input().split()))
p=0
for i in range(1,n):
    if a[i-1] > a[i]:
        p+=a[i-1]-a[i]
        a[i] = a[i-1]
print(p)
# Original solution
n,*a=list(map(int,open(0).read().split()))
count=0
for i in range(1,n):
    if i==1:
        if a[0]>a[1]:
            count+=a[0]-a[1]
    else:
        if max(a[:i])>a[i]:
            count+=max(a[:i])-a[i]
print(count)

# Optimized solution
n,*a=list(map(int,open(0).read().split()))
count,max_n=0,a[0]
for i in range(1,n):
    if max_n>a[i]:
        count+=max_n-a[i]
    else:
        max_n=max(max_n,a[i])
print(count)
# Original solution
n = int(eval(input()))
l = list(map(int,input().split()))

ans = 0

for i in range(n-1):
  if l[i+1]<=max(l[0:i+1]):
    ans+= max(l[0:i+1])-l[i+1]
    
print(ans)

# Optimized solution
n = int(eval(input()))
l = list(map(int,input().split()))

ans = 0

for i in range(n-1):
  if l[i+1]<l[i]:
    ans+= l[i]-l[i+1]
    l[i+1]=l[i]
    
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
tmp = A[0]
sm = A[0]
for i in range(len(A)-1):
    sm += max(tmp, A[i+1])
    tmp = max(tmp, A[i+1])
print((sm-sum(A)))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
tmp = 0
sm = 0
for i in range(len(A)):
    sm += max(tmp, A[i])
    tmp = max(tmp, A[i])
print((sm-sum(A)))
# Original solution
N=int(eval(input()))
A=list(map(int, input().split()))
B=[0 for _ in range(N)]
C=A
for i in range(1,N):
    M=max(C[:i])
    if M>A[i]:
        B[i]=M-A[i]
        C[i]=M
print((sum(B)))

# Optimized solution
N=int(eval(input()))
A=list(map(int, input().split()))
ans=0
M=A[0]
for i in range(1,N):
    if A[i]>M:
        M=A[i]
    else:
        ans+=M-A[i]
print(ans)
# Original solution
n = int(eval(input()))
aN = list(map(int, input().split()))
sum_board = 0
for i in range(1,n):
    if aN[i] < max(aN[:i]):
        sum_board +=  max(aN[:i]) - aN[i]

print(sum_board)

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

print(sum_board)
# Original solution
from collections import defaultdict
N = int(eval(input()))
A = [int(a) for a in input().split()]

ans = 0
memo = defaultdict(lambda: 0)

for i in range(1, len(A))[::-1]:
    memo[A[i]] += 1
    
    for h in list(memo.keys()):
        if (A[i-1] > h):
            diff = A[i-1] - h
            ans += diff * memo[h]
            # print(h, memo[h], diff)
            memo[h+diff] = memo[h]
            memo[h] = 0
            

    # print(memo)

print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(a) for a in input().split()]

ans = 0

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

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


# Optimized solution
_ = int(eval(input()))
s = h = 0
for a in map(int, input().split()):
    h = max(h, a)
    s += h - a
print(s)
# Original solution
n = int(eval(input()))
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    max_height_idx = 0
    continue

  if an >= max_height:
    max_height = an
    max_height_idx = i
    continue

  result += (max_height - an)

print(result)

# Optimized solution
n = int(eval(input()))
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    continue

  if an >= max_height:
    max_height = an
    continue

  result += (max_height - an)

print(result)
# Original solution
n = int(eval(input()))
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    continue

  if an >= max_height:
    max_height = an
    continue

  result += (max_height - an)

print(result)

# Optimized solution
n = int(eval(input()))
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):

    if a[i] >= max_height:
        max_height = a[i]
        continue

    result += (max_height - a[i])

print(result)
# Original solution
N = int(eval(input()))
array = list(map(int,input().split()))

su = sum(array)

def func(c,ar):
    num = max(ar[0:c])
    idx = ar.index(num)
    for i in range(idx+1,c):
        ar[i] = num 
    if idx == 0:
        print((sum(ar)-su))
        return
    func(idx,ar)
    
res = func(N,array)

# Optimized solution
N = int(eval(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))