#include<iostream>
#include <stdio.h>
#define MAXN 1000
using namespace std;
int a[MAXN+10];
int dp[MAXN+10];
int main()
{
intn,i,j,maxsum;
while(scanf("%d",&n),n){
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
dp[i]=a[i];
}
maxsum=0;
for(i=1;i<=n;i++){
for(j=1;j<i;j++){
if(a[j]<a[i]&&dp[j]+a[i]>dp[i])
dp[i]=dp[j]+a[i];
if(dp[i]>maxsum)
maxsum=dp[i];
}
}
printf("%d\n",maxsum);
}
return0;
}
03-09