import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, *a = 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()
