Title Description
Description
Jinming today is very happy, home purchase of 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 started budgeting, but he wanted to buy too many 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 integers) 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 of input, two positive integers, separated by a space: N m
(where n (<30000) represents the total amount of money, M (<25) 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 2 non-negative integers per line
V P
(where v indicates the price of the item (v<=10000), p indicates the importance of the item ()
Output description
Output Description
The output has only a positive integer, which is the maximum value (<100000000) of the sum of the price and the importance of the item that does not exceed the total amount of money.
Sample input
Sample Input
1000 5
800 2
400 5
300 5
400 3
200 2
Sample output
Sample Output
3900
Data range and Tips
Data Size & Hint
Exercises
Move back to the backpack.
This topic can be done with a search (data water, only 25 things), but easy to time out, so the recommended backpack to do.
F "J" means how much J yuan can be sold, F "J" =max (F "J", F "j-a" I "" +b "I" *a "I"), the answer F "M".
var n,m,i,j:longint;
A,b,f:array[0..30001]of Longint;
function Max (a,b:longint): Longint;
Begin
If A>b then exit (a);
Exit (b);
End
Begin
READLN (M,n);
For I:=1 to n do read (A[i],b[i]);
For I:=1 to n do b[i]:=b[i]*a[i];
For I:=1 to N do
For J:=m Downto A[i] Do
F[j]:=max (F[j],f[j-a[i]]+b[i]);
Write (f[m]);
End.
2006 Happy Jinming