#include<iostream>
using namespace std;
int dp[1005];/**/////存放第i之前的最大的递增数列和int a[1005];
int main()
{
int n;
while(cin>>n)
{
if(n == 0)
break;
int i,j;
for(i = 0;i < n;i++)
{
cin>>a[i];
}
//dp[0] = 1;
int sum = 0;
int max;
int result = 0;
for(i = 0;i < n;i++)
{
max = 0;
for(j = 0;j < i;j++)
{
if(a[i] > a[j])
if(dp[j] > max)
{
max = dp[j];
// sum += a[j];
}
}
dp[i] = max + a[i];
if(result < dp[i])
{
result = dp[i];
}
}
cout<<result<<endl;
}
return 0;
}