#include int n;int a[1001], dp[1001];int main() { while (scanf("%d", &n)) { if (0 == n) break; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); int m = -1000000; for (int i = 1; i <= n; ++i) { dp[i] = a[i]; for (int j = 1; j < i; ++j) { if (a[i] > a[j] && dp[i] < dp[j] + a[i]) dp[i] = dp[j] + a[i]; } if (dp[i] > m) m = dp[i]; } printf("%d\n", m); } return 0;}