I love sneakers! Group backpack (select at least one for each group)

Source: Internet
Author: User

 

Problem description

 

After months of hard working, iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.


There are several brands of sneakers that iserlohn wants to collect, such as Air Jordan and Nike pro. and each brand has released various products. for the reason that iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, iserlohn sets values for each of them based on his own tendency. with handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. obviusly, as a collector, he
Won't buy the same product twice.
Now, iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.

 

Input

 

Input contains multiple test cases. each test case begins with three integers 1 <= n <= 100 representing the total number of products, 1 <= m <= 10000 the money iserlohn gets, and 1 <= k <= 10 representing the sneaker brands. the following n lines each represents
Product with three positive integers 1 <= A <= K, B and c, 0 <= B, c <100000, meaning the brand's number it belongs, the labeled price, and the value of this product. process to end of file.

 

Output

 

For each test case, print an integer which is the maximum total value of the sneakers that iserlohn purchases. Print "impossible" If iserlohn's demands can't be satisfied.

 

Sample Input

 

5 10000 31 4 62 5 73 4 991 55 772 44 66

 

Sample output

 

255

A man has money. M. He wants to buy some. These shoes have k kinds of trademarks. The price of each trademark product is B, value: C. How can I buy the most valuable products? note that each trademark product has at least one product, which is different from the grouping of backpacks in section 9, for details, refer to Section 9 of backpack. the following explanations and codes are provided:

 

/* The current status comes from two sources: A. Maximum value obtained under the current number of brands; b. Based on a smaller number than the current one, we can easily design the state transition equation: f [J] [v] = max (F [J] [v], F [J] [V-cost] + value ); f [J] [v] = max (F [J] [v], F [J-1] [V-cost] + value ); f [J] [v] indicates the maximum value of placing the first J commodities into V */# include <stdio. h> # include <string. h> # include <stdlib. h> # define max (A, B) A> B? A: bint DP [15] [1, 10010]; int main () {int n, m, I, J, K, P, Q, O, CNT [15], V [15] [1, 110], W [15] [110]; while (scanf ("% d", & N, & M, & K )! = EOF) {memset (CNT, 0, sizeof (CNT); for (I = 0; I <n; I ++) {scanf ("% d", & P, & Q, & O); V [p] [CNT [p] = O; W [p] [CNT [p] ++] = Q;} // Initialization is amazing. memset (DP,-1, sizeof (DP) is very skillful )); // because the product value may be 0, the initialization cannot be 0for (I = 0; I <= m; I ++) // you can understand the meaning of DP [0] [I] = 0; for (I = 1; I <= K; I ++) {for (j = 0; j <CNT [I]; j ++) // 2, 3, the loop is called to select at most one for each group. The current loop is that it is amazing to select at least one for each group. {for (P = m; p> = W [I] [J]; p --) {// if you write the following two statements separately, if W [I] [J] = 0, the DP [I] [p] = max (DP [I] [p], DP [I] [p-W [I] [J] + V [I] [J]); DP [I] [p] = max (DP [I] [p], DP [I-1] [p-W [I] [J] + V [I] [J]); /* or change it to DP [I] [p] = max (DP [I] [p], max (DP [I] [p-W [I] [J] + V [I] [J], DP [I-1] [p-W [I] [J] + V [I] [J]) */}}} if (DP [k] [m] =-1) printf ("impossible \ n"); elseprintf ("% d \ n ", DP [k] [m]);} 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.