n = int(input())
array = list(map(int, input().split()))
dp = [k for k in array]
for i in range(n):
    for j in range(i):
        if array[i] > array[j]:
            dp[i] = max(dp[i], dp[j] + array[i])


print(max(dp))
