**the budget proposal of Jinming**2006 NOIP National League Improvement Group

Title Description Description

Kim is very happy today, the home purchase of the new house on the key, the new house has a very spacious room of his own dedicated. To his delight, the mother said to him yesterday: "Your room needs to buy what items, how to decorate, you decide, as long as not more than n yuan money on the line." Early this morning, he began to make a budget, he wanted to buy items divided into two categories: the main parts and accessories, attachments are subordinate to a certain main parts, the following table is some examples of the main parts and accessories:

If you want to buy an item that is classified as an accessory, you must first buy the main part that the attachment belongs to. Each main piece can have 0, one, or 2 attachments. Attachments no longer have attachments that belong to them. Jinming want to buy a lot of things, will certainly exceed the mother limit of N yuan. So, he set an important degree of each item, divided into 5, and so on: With an integer 1**~**5, the 5th is the most important. He also found the price of each item (all multiples of 10 yuan) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than n yuan (which could equal N).

The price of the article J is v[j], the importance of w[j], a total of K items selected, the number is J1,J2,......,JK, then the sum is:

v[j1]*w[j1]+v[j2]*w[j2]+. +V[JK]*W[JK]. (where * is multiplication sign)

Please help Jinming to design a shopping list that satisfies the requirements.

Enter a description

Input Description
The 1th line, which is two positive integers, is separated by a space:

Nm

(where n (<32000) represents the total amount of money, M (<60) is the number of items you wish to purchase. ）

From line 2nd to line m+1, line J gives basic data for items numbered J-1, with 3 non-negative integers per line

V P q

(where v indicates the price of the item (v<10000), p indicates the item's importance (1**~**5), q Indicates whether the item is a main part or an attachment. If q=0, indicates that the item is a main piece, if q>0, indicates that the item is an attachment, q is the number of the main part of the product)

Output description

Output Description
Only a positive integer, the maximum value of the sum of the product of the price and the importance of the item that does not exceed the total amount of money (<200000)

Sample input

Sample Input
1000 5

2 0

400 5 1

300 5 1

400 3 0

500 2 0

Sample output Sample outputs

2200

Just read the backpack nine talk, find a problem to practice this problem belongs to have a dependency on the backpack a total of M items, can be seen as a total of K set {by the main parts and accessories of choice decision composition} If a main piece has 1 attachments, there are 2 decision options {Main, Main, and accessory} if---------2 attachments, There are 4 decision options (main parts, main parts and accessories 1, main parts and accessories 2, main parts and accessories 1 and Accessories 2}F[i,j] Represents the first set of I, at the cost of the maximum benefit obtained by Jtransfer equation: f[i,j]=max{f[i-1,j], f[i-1,j-w[i][k]]+v[i][k] | 0<=k<= decision number of the I-set}Core code:For I:=1 to set number of doFor j:=v Downto 0 dobegin F[I,J]:=F[I-1,J]; {If this collection is not selected}for K:=1 to this set of decision number doF[i,j]:=max (F[i,j], f[i-1,j-w[i][k]{the cost of the decision}+v[i][k]{the benefit of the decision)end;

var n,m:longint; Jhn:longint;

Jh:array[0..60]of Record

V,w:array[1..60]of Longint;

End

Jk:array[0..60]of Longint;

P,t,w,w1,v,v1:array[1..60]of Longint;

F:array[0..60,0..32000]of Longint;

Son:array[0..60,0..60]of Longint;

Findmax:longint;

I,j,k:longint;procedure choose (x:longint);

var i,j:longint;

Begin IF Son[x,0]=1

Then BEGIN Inc (jk[JHN]);

jh[jhn].v[jk[jhn]]:=v1[x]+v1[son[x,1]];

jh[jhn].w[jk[jhn]]:=w1[x]+w1[son[x,1]];

End

If son[x,0]=2

Then BEGIN Inc (jk[JHN]);

jh[jhn].v[jk[jhn]]:=v1[x]+v1[son[x,1]];

jh[jhn].w[jk[jhn]]:=w1[x]+w1[son[x,1]];

Inc (jk[JHN]);

jh[jhn].v[jk[jhn]]:=v1[x]+v1[son[x,2]];

jh[jhn].w[jk[jhn]]:=w1[x]+w1[son[x,2]];

Inc (jk[JHN]);

jh[jhn].v[jk[jhn]]:=v1[x]+v1[son[x,1]]+v1[son[x,2];

jh[jhn].w[jk[jhn]]:=w1[x]+w1[son[x,1]]+w1[son[x,2];

End

End;function Max (x,y:longint): Longint;

Begin IF X>y

Then exit (x)

else exit (y);

End;begin Fillchar (son,sizeof (son), 0);

Fillchar (jk,sizeof (JK), 0);

READLN (N,M);

For I:=1 to M do

Begin READLN (W1[i],t[i],p[i]);

V1[i]:=w1[i]*t[i];

If p[i]<>0

Then BEGIN Inc (son[p[i],0]);

son[P[i], son[p[i],0]]:=i;

End

End

For I:=1 to M do

If p[i]=0

Then BEGIN Inc. (JHN); jk[Jhn]:=1;

jh[jhn].v[1]: =v1[i];

jh[jhn].w[1]: =w1[i];

If son[i,0]<>0

Then choose (i);

End

For I:=1 to Jhn do

For j:=n Downto 0 do

Begin F[I,J]:=F[I-1,J];

For K:=1 to Jk[i] do

If Jh[i].w[k]<=j

Then F[i,j]:=max (F[i,j],f[i-1,j-jh[i].w[k]]+jh[i].v[k]);

End

Writeln (F[jhn,n]);

End.

Backpack DP: Jinming's budget plan