def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    highest = A[0]

    for height in range(len(A)-1):
        if A[height] > A[height+1]:
            if A[height] > highest:
                highest = A[height]
            count += highest - A[height+1]

    print(count)


if __name__ == '__main__':
    main()