ACM (TJU) : 2838


#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
    int c, b;
    cin >> c >> b;
    int value [25];
    int a [25];
    int max = 0;

    for  ( int i = 0; i < b; i++ )
    cin >> value [i];

    for ( int i = 0; i < b; i++ ) {

        for ( int j = 0; j < 25; j++ )
        a [j] = 0;

        for ( int j = 0; j <= i; j++ )
        a [j] = 1;

        sort (a, a + b);

        do {
            int sum = 0;
            for ( int i = 0; i < b; i++ ) {
                //printf ("%d", a [i]);
                if ( a [i] == 1 )
                sum += value [i];
            }
            //printf ("\n");

            if ( sum <= c && sum > max )
            max = sum;
        } while ( next_permutation (a, a + b));



    }

    printf ("%d\n", max);
}
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