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

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)

-# 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 +input = stdin.buffer.readline -# N, M = 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() -for a in A[1:]: +for a in A: