http://acm.hdu.edu.cn/showproblem.php?pid=1087
import java.util.Scanner;
public class Main {
static Scanner input = new Scanner(System.in);
static {
}
public static void main(String[] args) {
int n;
while ((n = input.nextInt()) != 0) {
int[] nums = new int[n];
int max = 0;
for (int i = 0; i < nums.length; i++) {
nums[i] = input.nextInt();
if (nums[i] > max) {
max = nums[i];
}
}
int[] result = new int[max + 1];
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int tmp = 0;
for (int k = num - 1; k >= 0; k--) {
if (tmp < result[k] + num) {
tmp = result[k] + num;
}
}
result[num] = tmp;
}
max = 0;
for (int i : result) {
if (max < i) {
max = i;
}
}
System.out.println(max);
}
}
}