Problem Description
I 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 the most valuable neckpolicmy mother will accept.
Input
The 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 of 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.
Output
For each case, output the highest possible value of the neck.pdf.
Sample Input
1
2 1
1 1
1 1
3
Sample Output
1
Obtain the sum of the maximum values of K gems, but the weight cannot exceed W;
# Include <stdio. h> struct ston {int sa, sw ;}; struct ston s [25], tem; int su, N, K, W; void DFS (int I, int suma, int w, int k) {int j; if (su <suma) // compare the total value su = suma; if (k = K) // The number of gems cannot exceed K return; for (j = I + 1; j <= N; j ++) if (s [j]. sw + w <= W & k + 1 <= K) DFS (j, s [j]. sa + suma, s [j]. sw + w, k + 1);} int main () {int t, I, j, e, sum; scanf ("% d", & t ); while (t --) {scanf ("% d", & N, & K); for (I = 1; I <= N; I ++) scanf ("% d", & s [I]. sa, & s [I]. sw); scanf ("% d", & W); for (I = 1; I <= N; I ++) // first sort by value from large to small {e = I; for (j = I + 1; j <= N; j ++) if (s [e]. sa <s [j]. sa) e = j; tem = s [I]; s [I] = s [e]; s [e] = tem;} sum = 0; for (I = 1; I <= N; I ++) // you can view the largest if (s [I] with the total value starting with that. sw <= W & K> 0) {su = s [I]. sa; DFS (I, s [I]. sa, s [I]. sw, 1); if (su> sum) sum = su;} printf ("% d \ n", sum) ;}# include <stdio. h> struct ston {int sa, sw ;}; struct ston s [25], tem; int su, N, K, W; void DFS (int I, int suma, int w, int k) {int j; if (su <suma) // compare the total value su = suma; if (k = K) // The number of gems cannot exceed K return; for (j = I + 1; j <= N; j ++) if (s [j]. sw + w <= W & k + 1 <= K) DFS (j, s [j]. sa + suma, s [j]. sw + w, k + 1);} int main () {int t, I, j, e, sum; scanf ("% d", & t ); while (t --) {scanf ("% d", & N, & K); for (I = 1; I <= N; I ++) scanf ("% d", & s [I]. sa, & s [I]. sw); scanf ("% d", & W); for (I = 1; I <= N; I ++) // first sort by value from large to small {e = I; for (j = I + 1; j <= N; j ++) if (s [e]. sa <s [j]. sa) e = j; tem = s [I]; s [I] = s [e]; s [e] = tem;} sum = 0; for (I = 1; I <= N; I ++) // you can view the largest if (s [I] with the total value starting with that. sw <= W & K> 0) {su = s [I]. sa; DFS (I, s [I]. sa, s [I]. sw, 1); if (su> sum) sum = su;} printf ("% d \ n", sum );}}