Happy Xiao Ming (Nanyang oj49) (01 backpack), Nanyang oj4901 backpack
Happy James time limit: 1000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
James 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 ". James 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.
-
Input
-
Enter an integer N (0 <N <= 101) in the first row to indicate the number of test data groups.
The 1st rows of test data input in each group are two positive integers separated by a space:
N m
(N (<30000) indicates the total amount of money, and m (<25) indicates the number of items to be purchased .)
From row 2nd to row m + 1, row j gives the number of J-1
The basic data of an item. 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 ))
-
Output
-
Each group of test data outputs only one positive integer, which is the sum of the product price and importance of an item that does not exceed the total amount of money
Maximum value (<100000000)
-
Sample Input
-
11000 5800 2400 5300 5400 3200 2
-
Sample output
-
3900
-
/* It is still 01. When doing this, you only need to multiply the item price and importance, and then set the template. */# include <stdio. h> # include <string. h ># include <algorithm> using namespace std; int main () {int test, I, j, n, m; int a [28], B [28], dp [30002]; scanf ("% d", & test); while (test --) {scanf ("% d", & n, & m ); memset (dp, 0, sizeof (dp); for (I = 1; I <= m; I ++) {scanf ("% d ", & a [I], & B [I]) ;}for (I = 1; I <= m; I ++) {for (j = n; j> = a [I]; j --) // note that j> = a [I] in this loop; dp [j] = max (dp [j], dp [j-a [I] + a [I] * B [I]);} printf ("% d \ n", dp [n]);} return 0 ;}