n = int(input()) A = list(map(int, input().split())) cnt = 0 kaisa = [0] * (n - 1) for i in range(n - 1): kaisa[i] = A[i + 1] - A[i] for i in range(n - 1): if kaisa[i] >= 0: continue cnt += -kaisa[i] if i >= 1: if kaisa[i - 1] > 0: kaisa[i] = min(kaisa[i] + kaisa[i - 1], 0) if kaisa[i] >= 0: continue if i != n - 2: kaisa[i + 1] += kaisa[i] kaisa[i] = 0 print(cnt)