Problem:
Maximum Sum Sequence

Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence.

Input

The input consists of multiple datasets. Each data set consists of:

n
a1
a2
.
.
an

You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.

The input end with a line consisting of a single 0.

Output

For each dataset, print the maximum sum in a line.

Sample Input

7
-5
-1
6
4
9
-6
-7
13
1
2
3
2
-2
-1
1
2
3
2
1
-2
1
3
1000
-200
201
0

Output for the Sample Input

19
14
1001

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class Main {
	private	static	ArrayList<Integer>	list = null;
	private	static	BufferedReader		br   = null;

	static {
		list = new ArrayList<Integer>();
		br   = new BufferedReader(new InputStreamReader(System.in));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int	nNum = 0;
		int	nTmp = 0;
		int	nSum = 0;
		int	nMax = 0;

		while((nNum = getNum()) != 0) {
			// 連続する正の数と負の数の和を求める
			for (int n = 0; n < nNum; n++) {
				nTmp = getNum();
				if (nTmp >= 0) {
					nSum = (n == 0) ? nTmp : nSum + nTmp;
					if (n == nNum - 1) {
						list.add(nSum);
					}
				}
				else {
					if (nSum > 0) {
						list.add(nSum);
					}
					nSum = nTmp;
					list.add(nSum);
					nSum = 0;
				}
			}

			// 和の最大値を求める
			nSum = list.get(0);
			nMax = nSum;
			for (int n = 1; n < list.size(); n++) {
				if (nSum < 0) {
					nSum = 0;
				}
				nSum += list.get(n);
				nMax  = Math.max(nMax, nSum);
			}
			System.out.println(nMax);
			list.clear();
		}
	}

	private static int getSign(int nNum) {
		return (nNum >= 0) ? 1 : 0;
	}

	private static int getNum() {
		return Integer.parseInt(parseStdin());
	}

	private static String parseStdin() {
		String	strNum = null;

		try {
			String line = br.readLine();
			if (line != null) {
				if (!line.isEmpty()) {
					strNum = line;
				}
			}
		}
		catch (IOException e) {}

		return strNum;
	}
}