n = int(input())
num = list(map(int, input().split(" ")))
dp = list(num)#记住,一定要加个list(),否则就是同一个内存地址
for i in range(n):
    for j in range(i):
        if num[j] < num[i]:
            dp[i] = max(dp[i], num[i] + dp[j])
print(max(dp))