UVa : 11517 (Exact Change)



// http://uva.onlinejudge.org/external/115/11517.html

// @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 <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
using namespace std;

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int bill;
        scanf ("%d", &bill);

        int coinNumber;
        scanf ("%d", &coinNumber);

        int coins [100 + 5];
        for ( int i = 0; i < coinNumber; i++ )
            scanf ("%d", &coins [i]);

        int dp [10000 + 10];
        for ( int i = 0; i < 10010; i++ )
            dp [i] = INT_MAX;

        dp [0] = 0;

        for ( int i = 0; i < coinNumber; i++ ) {
            for ( int j = 10000; j >= 0; j-- ) {
                if ( dp [j] != INT_MAX && j + coins [i] <= 10000 && dp [j + coins [i]] > dp [j] + 1 ) dp [j + coins [i]] = dp [j] + 1;
            }
        }

        for ( int i = bill; i <= 10000; i++ ) {
            if ( dp [i] != INT_MAX ) {
                printf ("%d %d\n", i, dp [i]);
                break;
            }
        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

One thought on “UVa : 11517 (Exact Change)

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