BACKPACK - Dab of Backpack

no tags 

One day Blue Mary goes to a nearby supermarket to buy some goods. She has a backpack, whose capacity is V-Max. She finds that there are many goods in the market, each has a volume Vi(it will always be a multiple of 10 and less than 10000) and an importance Ci (1<= Ci <=5). Since she has almost unlimited money, the only problem she is to solve is how to choose goods such that the total volume won't exceed the capacity of the backpack and the sum of the product of the volume and the importance of each good is maximum. To be an excellent mathematician, she comes up with the answer quickly, and now she wants you to do a harder task. There are two kinds of goods: main goods and attachments. If you want to buy an attachment you must buy its main good before.


Multiple test cases, the number of them is given in the very first line.

For each test case:

The first line contains two space-separated integers V-Max (1<=V-Max<=32000) and the number of the goods N (1<=N<=60). N lines follow, each contains three space-separated integers Vi, Ci and a integer u. If u is not 0, this good is an attachment of good u(as the order in the input file).

To make the problem not too difficult, Blue Mary tells you that:

(A) An attachment won't have any attachments which belong to it.

(B) A main good will always have less than 3 attachments.


For each test case:

The first and the only line contains a single integer denoted the answer.


1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0


hide comments
shuks: 2015-01-12 12:02:27

Can anyone provide some hard test cases. I can't find the mistake in my code ;(

Abhishek: 2014-12-15 15:14:49

USED Recursion + Memoization , AC in 1 GO (not putting space between nested template gave 1 compilation error :P )

Nice Question Indeed! , worth the time.

P.S the pair template from <utility> is quite handy.

Last edit: 2014-12-15 16:26:25
joud zouzou: 2014-07-27 14:50:45

" (B) A main good will always have less than 3 attachments. "

should be :
" (B) A main good will always have no more than 3 attachments. "

nondescript: 2013-10-30 05:29:39

finally got accepted. many thanks to the problem setter. :D

Last edit: 2013-11-08 22:14:24
natsu: 2013-05-31 14:14:38

Finally got AC, very good problem thanks to the problemsetter
Note: Even if there are attachments,it is not necessary to take it... costed me a lot of WA's

DivineAtheist: 2013-01-08 22:59:45

finally got AC.....after trying too hard...nice problem...:)

Mukesh Yadav: 2012-12-07 21:30:00

@ankit You can buy as many attachments as you want, even no one , but satisfying the condition that you have bought their main good !

Romal Thoppilan: 2012-10-13 12:46:43

Are the goods unbounded or single(limited) , pls mention it

sriankit: 2012-07-25 06:02:39

can i buy a particular attachment and the main good..???

manushree: 2011-10-06 23:29:50

Plz explain the questn properly! Are repetitions allowed?

Added by:Fudan University Problem Setters
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Based on a Problem from Chinese National Olympiad in Informatics in Province 2006