def main():
    N = input()
    if int(N) < 1 or int(N) >= 2*10**5:
        raise ValueError
    A = []
    for i in input().split():
        if int(i) < 1 or int(i) >= 10**9:
            raise ValueError
        A.append(int(i))
    temp = A[0]
    su = 0
    for i in A:
        if i >= temp:
            height = 0
        else:
            height = temp-i
        temp = i + height
        su += height
    print(su)


if __name__ == '__main__':
    main()
