UVa 10130 SuperSale (0-1 backpack)

Source: Internet
Author: User
Tags integer time limit

10130-supersale

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1071

There is a supersale in a superhipermarket. Every person can take only one of the kind, i.e. one TV, one carrot, but for extra. We are going with a whole family to that superhipermarket. Every can take as many objects, as he/she can carry out from the SuperSale. We have given list of objects with prices and their weight. We also know, what is the maximum weight this every person can stand. What is the maximal value of objects we can buy at SuperSale?

Input specification

The input consists of T test cases. The number of them (1<=t<=1000) is given on the "the" input file.

Each test case begins with a line containing a single integer number N that indicates the number of objects (1 <= n ; = 1000). Then follows Nlines, each containing two integers:p and W. The "1<=p<=100" corresponds to the price of object. The second integer (1<=w<=30) corresponds to the weight of object. Next line contains one integer (1<=g<=100) It's the number of people in our group. Next G lines contains maximal weight (1<=mw<=30) that can stand this i-th person from our family (1<=i<=g).

Output specification

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45377.htm

For every test case your program has to determine one integer. Print out the maximal value of goods which we can-buy with that family.

Sample Input

2

3

72 17

44 23

31 24

1

26

6

64 26

85 22

52 4

99 18

39 13

54 9

4

23

20

20

26

Output for the Sample Input

72

514

Water and water.

Complete code:

/*0.072s*/
    
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
using namespace Std;  
    
int p[1005], w[1005], dp[35];  
    
int main ()  
{  
    int t, n, I, J, G, Sum, MAXW;  
    scanf ("%d", &t);  
    while (t--)  
    {  
        scanf ("%d", &n);  
        for (i = 0; i < n; ++i)  
            scanf ("%d%d", &p[i), &w[i]);  
        scanf ("%d", &g);  
        sum = 0;  
        while (g--)  
        {  
            memset (DP, 0, sizeof (DP));  
            scanf ("%d", &MAXW);  
            for (i = 0; i < n; ++i) for  
                (j = maxw J >= W[i];--j)  
                    dp[j] = max (Dp[j], dp[j-w[i] + p[i]);  
            Sum + + DP[MAXW];  
        }  
        printf ("%d\n", sum);  
    }  
    return 0;  
}

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.