def resolve():
    N = input()
    string = input()
    max_height = 0
    s = 0
    for c in string:
        if c == " ":
            continue
        h = int(c)
        if h < max_height:
            s += max_height - h
        elif h > max_height:
            max_height = h

    print(s)
resolve()