Jin Ming was very happy today. The new house purchased at home had the key. There was a very spacious room dedicated to him in the new house. Even more pleased, his mother said to him yesterday: "You have the final say about the items you need to purchase and how to arrange in your room, as long as the price does not exceed N yuan ". Jin Ming started to make a budget early this morning, but he wants to buy too many things and will definitely exceed his mother's limit of N yuan. Therefore, he defined an importance for each item and divided it into five equal values: an integer of 1 ~ 5 indicates that 5th is the most important. He also found the price of each item on the Internet (all in integer yuan ). He hopes that the sum of the product of the price and importance of each item will be maximized without exceeding N yuan (which can be equal to N yuan. Set the price of item J to V [J], and the importance to W [J]. K items are selected and numbered as J1... JK, then the sum is: V [J1] * W [J1] + .. + V [JK] * W [JK] Please help Jin Ming design a shopping order meeting the requirements. the 1st rows entered in the input format are 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 want to purchase .) From row 2nd to row m + 1, row J gives the basic data of the item numbered J-1, each row has two non-negative integers v P (V indicates the price of the item (v ≤ 10000), and p indicates the importance of the item (1 ~ 5) the output format outputs only one positive integer, which is the maximum value of the product of price and importance of an item that does not exceed the total amount of money (<100000000) sample input 1000 5 800 2 400 5 300 5 400 3 200 2 sample output 3900
A simple backpack.
If this problem is carried out in the form of a backpack, it will take more time.
The normalization method is to find the most common number of all cells, and then divide all cells by this common number, which can greatly reduce the time-space compression.
Accode:
# Include <cstdio> # include <cstdlib> const char fi [] = "rqnoj02.in"; const char fo [] = "rqnoj02.out"; const int maxn = 30; const int maxm = 50000; const int max = 0x3fffff00; const int min =-0x3fffff00; int f [maxm]; // pay attention to the quota of this data set. Int W [maxn]; int V [maxn]; int n, m, G; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", stdout);} int gcd (int A, int B) {If (B = 0) return a; return gcd (B, A % B );} void readdata () {scanf ("% d", & M, & N); G = m; For (INT I = 1; I <n + 1; ++ I) {scanf ("% d", W + I, V + I); V [I] * = W [I]; G = gcd (G, W [I]);} m/= g; For (INT I = 1; I <n + 1; ++ I) W [I]/= g ;} void work () {for (INT I = 1; I <n + 1; ++ I) for (Int J = m; j> W [I]-1; -- j) if (F [J-W [I] + V [I]> F [J]) f [J] = f [J-W [I] + V [I]; printf ("% d", F [m]);} int main () {init_file (); readdata (); Work (); exit (0 );}