HDU : 1709 (The Balance)


// http://acm.hdu.edu.cn/showproblem.php?pid=1709

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;


int main ()
{
    int coins;
    int c [100 + 10];

    while ( scanf ("%d", &coins) != EOF ) {
        int S = 0;

        for ( int i = 0; i < coins; i++ ) {
            scanf ("%d", &c [i]);
            S += c [i];
        }

        sort (c, c + coins);

        bool dp [10000 + 10];
        memset (dp, false, sizeof (dp));
        dp [0] = true;

        for ( int j = 0; j < coins; j++ ) {
            for ( int i = S; i >= 0; i-- ) {
                if ( dp [i] && i + c [j] <= S )
                    dp [i + c [j]] = true;
            }
        }

        for ( int j = 0; j < coins; j++ ) {
            for ( int i = 0; i <= S; i++ ) {
                if ( dp [i] && i - c [j] >= 0 )
                    dp [i - c [j]] = true;
            }
        }

        vector <int> falseCnt;

        for ( int i = 0; i <= S; i++ ) {
            if ( dp [i] == false )
                falseCnt.push_back (i);
        }

        if ( falseCnt.size () == 0 ) { printf ("0\n");}
        else {
            printf ("%d\n", falseCnt.size ());
            printf ("%d", falseCnt [0]);
            for ( size_t i = 1; i < falseCnt.size (); i++ )
                printf (" %d", falseCnt [i]);
            printf ("\n");
        }

    }

    return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s