def step(N, A):
    sum = 0
    for i in range(1, N):
        if A[i-1] < A[i]:
            sum += A[i] - A[i-1]
    return sum
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(step(N, A))