10656 - Maximum Sum (II)

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1597

In a given a sequence of non-negative integers you will have to find such a sub-sequence in it whose summation is maximum.

 

Input

The input file contains several input sets. The description of each set is given below:

 

Each set starts with an integer N (N<1000) that indicates how many numbers are in that set. Each of the next N lines contains a single non-negative integer. All these numbers are less than 10000.

 

Input is terminated by a set where N=0. This set should not be processed.

 

Output

For each set of input produce one line of output. This line contains one or more integers which is are taken from the input sequence and whose summation is maximum. If there is more than one such sub-sequence print the one that has minimum length. If there is more than one sub-sequence of minimum length, output the one that occurs first in the given sequence of numbers. A valid sub-sequence must have a single number in it. Two consecutive numbers in the output are separated by a single space.

 

Sample Input     Output for Sample Input

2

3

4


0

3 4                                                            


注意子序列可以不连续(有些题没说连续但是样例是连续的,有些题看上去像是连续的但WA了后才知道是可以不连续的)


完整代码:

/*0.029s*/

#include<cstdio>

int main()
{
	int n, a;
	bool vis;
	while (scanf("%d", &n), n)
	{
		vis = false;
		while (n--)
		{
			scanf("%d", &a);
			if (a)
			{
				if (vis) putchar(' ');
				vis = true;
				printf("%d", a);
			}
		}
		if (!vis) putchar('0');
		putchar(10);
	}
	return 0;
}