Title 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:
Main parts Accessories
Computer printers, scanners
Bookcase Books
Desk lamp, stationery
Work Chair No
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. As a result, he set an important degree of each article, divided into 5, such as: The whole number of the 5th, and so 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]. (which is multiplication sign)
Please help Jinming to design a shopping list that satisfies the requirements.
Input/output format
Input format:
The 1th line of input, which is two positive integers, is separated by a space:
N m (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 importance of the item ("the"), 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 format:
The output has only a positive integer, which is the maximum value (<200000) of the sum of the price and the importance of the item that does not exceed the total amount of money.
Input/Output sample
Input Sample # #:
1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0
Sample # # of output:
2200
Analysis:
Look at the main piece as an item, and the main piece plus an accessory for another item. is to add the value and cost of the attachment plus the value and cost of the main piece, then DP.
Code:
var m,n,x,b,t1,flag,a1,i,j,k,l:longint;
A:ARRAY[0..2000,0..4] of Longint;
F,C,W,T:ARRAY[0..32000] of Longint;
Begin READLN (M,N);
For I:=1 to n do begin READLN (X,b,c[i]);
If C[i]=0 then BEGIN Inc (T1);
Inc (A1);
A[t1,0]:=1;
A[T1,1]:=A1;
W[a1]:=x;
T[a1]:=x*b;
End
If c[i]>0 then begin k:=0;
For J:=1 to C[i] does if C[j]=0 then Inc (K);
L:=A[K,0];
For j:=1 to L DO BEGIN Inc (a[k,0]);
Inc (A1);
A[K,A[K,0]]:=A1;
W[a1]:=w[a[k,j]]+x;
T[a1]:=t[a[k,j]]+x*b;
End
End
End For k:=1 to T1 does for j:=m Downto 0 does for i:=1 and a[k,0] do if j>=w[a[k,i] and then if f[j-w[
A[K,I]]]+T[A[K,I]]>F[J] then f[j]:=f[j-w[a[k,i]]]+t[a[k,i]];
Write (f[m]); End.