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 the non-negative integers you'll have to find such a sub-sequence into 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 a integer N (n<1000) that indicates, and many numbers are in that set. Each of the next N lines contains a single non-negative integer. All of these numbers are less than 10000.
The Input is terminated by a set where n=0. This set should is processed.
Output
For each set of input produce one line of output. This line contains one or more integers which is are taken to 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 a sub-sequence of minimum length, output the one that occurs the the given of the S. A valid sub-sequence must have a single number in it. Two consecutive numbers in the output are separated by a.
Sample Input
2
3
4
0
Output for Sample Input
3 4
Note that the subsequence can be discontinuous (some problems do not say continuous but the sample is continuous, some problems appear to be continuous, but WA after the know is not continuous)
Complete code:
/*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 (ten);
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/