import numpy as np
import math
import scipy as sp
import queue
import collections

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

def main(kwargs):
    n = kwargs["n"]
    A = kwargs["A"]
    B = [0 for i in range(n)]
    for i in range(n - 1):
        B[i+1] = max(0, A[i] - A[i+1])
    if n > 1:
        B[-1] = max(0, A[-2] + B[-2] - A[-1])
    res = sum(B)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]

    cin = input().split()
    kwargs["A"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)