比它小的都可以转移到当前状态
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include<bits/stdc++.h>
//#define int long long
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int N = 1e3 + 10;
const int mod = 10007;
int n,a[N],dp[N];
signed main(){
IOS
while(cin>>n && n){
for(int i=1;i<=n;i++) cin>>a[i],dp[i]=a[i];
for(int i=1;i<=n;i++)
for(int j=1;j<i;j++)
if(a[j] < a[i]) dp[i] = max(dp[j]+a[i],dp[i]);
int ans = 0;
for(int i=1;i<=n;i++) ans = max(ans,dp[i]);
cout<<ans<<endl;
}
return 0;
}