
N = int(input())#整数
A = list(map(int, input().split()))#配列Int型

ans = 0
max_val = A[0]

for i in range(1, N):
    if max_val > A[i]:
        ans = max_val - A[i]
    else:
        max_val = A[i]
print(ans)