Accepted neck.pdf
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1022 accepted submission (s): 406
Problem descriptioni
Have n precious stones, and plan to use K of them to make a neck.pdf
For my mother, but she won't accept a neck1_which is too heavy. Given
The value and the weight of each precious stone, please help me find
Out of the most valuable necklace my mother will accept.
Inputthe first line of input is the number of instances.
For each case, the first line contains two integers n (n <= 20 ),
The total number of stones, and K (k <= N), the exact number
Stones to make a necktasks.
Then n lines follow, each containing
Two integers: A (a <= 1000), representing the value of each precious
Stone, and B (B <= 1000), its weight.
The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.
Outputfor each case, output the highest possible value of the neck.pdf.
Sample input1
2 1
1 1
1 1
3
Sample output1
Source HDU boys' open session-from whu)
Recommendzty
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Int t, n, k, W;
Struct Node
{
Int V, W;
} P1 [2, 100];
Int sum = 0;
Void DFS (int v, int W, int num, Int J)
{
Int I;
If (W> W)
Return;
If (Num> K)
Return;
If (sum <v)
Sum = V;
For (I = J; I <n; I ++ ){
DFS (V + P1 [I]. V, W + P1 [I]. W, num + 1, I + 1 );
}
}
Int main ()
{
Int I, J;
Scanf ("% d", & T );
While (t --)
{
Sum = 0;
Scanf ("% d", & N, & K );
For (I = 0; I <n; I ++)
Scanf ("% d", & P1 [I]. V, & P1 [I]. W );
Scanf ("% d", & W );
DFS (0, 0, 0, 0); // parameter transfer, current value, value weight, current number, array subscript
Printf ("% d \ n", sum );
}
Return 0;
}