from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    for i in range(1, N):
        diff = A[i - 1] - A[i]
        if diff < 0:
            count += abs(diff)
    print(count)

if __name__ == '__main__':
    main()
