UVa : 10130 (SuperSale)


Notes :
1. 0/1 Knapsack problem

2. u can’t take an object more than one time.
suppose, u can stand with maximum 30 kg weight
an object has a weight of 10 kg
but u can take the object only once, not 3 times, though u still have 20 kg space

3. calculate all the values up to 30 and save in an array dp [30]
if ur maxWeightLimit = i, thn dp [i] = maxValue u can get


// http://uva.onlinejudge.org/external/101/10130.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 objects;
        int values [1000 + 5];
        int weight [1000 + 5];

        scanf ("%d", &objects);

        for ( int i = 0; i < objects; i++ )
            scanf ("%d %d", &values [i], &weight [i]);

        int dp [30 + 5];
        memset (dp, 0, sizeof (dp));


        for ( int j = 0; j < objects; j++ ) {
            for ( int i = 32; i >= 0; i-- ) {
                if ( weight [j] <= i && dp [i] < dp [i - weight [j]] + values [j] )
                    dp [i] = dp [i - weight [j]] + values [j];
            }
        }

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

        int maxPrice = 0;

        while ( g-- ) {
            int volume;
            scanf ("%d", &volume);
            maxPrice += dp [volume];
        }

        printf ("%d\n", maxPrice);
    }

	return 0;
}

// @END_OF_SOURCE_CODE

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