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.

Input

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.

Output

For each test case:

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

Example

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

Output:
2200

hide comments
Anmol: 2021-08-16 00:31:07

nice problem!!
can be solved without below constraint :
(B) A main good will always have less than 3 attachments.

Last edit: 2021-08-16 00:33:28
harshh3010: 2021-03-29 01:41:57

Not using memset caused 5 WA... T_T

sharjeel_spoj: 2020-08-30 16:47:16

Solve knapsack once again before going at it.

blackrise: 2020-04-20 15:46:34

This was so easy... took me just 5 minutes...

prabhat7298: 2019-09-07 14:03:52

problem statement has got this line wrong "A main good will always have less than 3 attachments." It should be no more than 3 attachments

sky_scraper: 2019-06-04 13:37:04

Before solving this problem make sure you understand that, If u is non-zero then it means current good is an attachment of the main good on the line u, not the uth main good.
Test Case :
1
1000 2
100 1 2
100 3 0
Correct output : 400
Thanks @screw_1011

computer_argha: 2019-02-21 07:32:10

AC in one go :)

surajkumar_cse: 2018-12-26 15:02:47

They must provide other test cases which includes attachments first and then its main product.
Finally done, because I was missing this test case.

hello_world123: 2018-06-15 10:23:27

Use memoization

Last edit: 2018-06-15 10:23:47
vengatesh15: 2017-02-14 09:52:05

Took me 2 hours but easy one...

Last edit: 2017-02-14 09:52:53

Added by:Fudan University Problem Setters
Date:2007-11-03
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