/************************************************
*Author :jibancanyang
*Created Time :日 4/ 3 21:23:36 2016
*File Name :hdu1087.cpp
*題目類型: LIS變形
*很簡單的題,但是開始沒有把轉移方程想好就寫,導緻了浪費時間...
*定義:dp[i]為以i結尾的最好路徑的最大值,然後由它前面的數轉移過來就好了.
*************************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
vector<int> vi;
#define xx first
#define yy second
#define sa(n) scanf("%d", &(n))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++)
#define pr(x) cout << #x << ": " << x << " "
#define prln(x) cout << #x << ": " << x << endl
const int mod = int() + , INF = , maxn = + ;
int a[maxn / ];
ll dp[maxn / ];
int n;
int main(void)
{
#ifdef LOCAL
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
// cin.sync_with_stdio(false);
while (sa(n), n) {
rep (i, , n) {
sa(a[i]), dp[i] = ;
}
dp[] = a[];
rep (i, , n) {
for (int j = i - ; j >= ; j--) {
dp[i] = max(dp[i], a[i] > a[j] ? dp[j] + a[i] : a[i]);
}
}
ll ans = ;
rep (i, , n) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
return ;
}