UVa 10656 Maximum Sum (II) (Water ver.)

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.