Access KK values for each capacity, sort values after adding new values, and obtain the previous KK values (from large to small ).
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <set> 6 # include <vector> 7 # include <map> 8 # include <algorithm> 9 # include <cmath> 10 # include <stdlib. h> 11 using namespace STD; 12 INT main () 13 {14 int t, n, V, KK, I, j; 15 int DP [1010] [75]; 16 int va [1010], Vo [1010]; 17 CIN> T; 18 while (t --) 19 {20 CIN> N> V> KK; 21 memset (DP, 0, sizeof (DP); 22 for (I = 0; I <= V; I ++) // DP [I] [0] indicates capacity Number of value types for I 23 DP [I] [0] = 1; 24 for (I = 1; I <= N; I ++) 25 CIN> va [I]; 26 for (I = 1; I <= N; I ++) 27 Cin> VO [I]; 28 29 for (I = 1; I <= N; I ++) 30 for (j = V; j> = Vo [I]; j --) {31 int CNT = DP [J] [0]; 32 for (int K = 1; k <= DP [J-vo [I] [0]; k ++) 33 DP [J] [++ CNT] = DP [J-vo [I] [k] + va [I]; 34 int TMP [70], num = 1; 35 for (int K = 1; k <= CNT; k ++) 36 TMP [k] = DP [J] [k]; 37 sort (TMP + 1, TMP + 1 + CNT); // sort the values, and then obtain the values of the first KK, if not, take 38 DP [J] [1] = TMP [CNT]; 39 for (int K = cnt-1; k> = 1; k --) // obtain the value of the first KK. If not, take 40 if (TMP [k]! = TMP [k + 1]) 41 {42 if (Num + 1> KK) 43 break; 44 DP [J] [++ num] = TMP [k]; 45} 46 DP [J] [0] = num; 47} 48 if (DP [v] [0] <KK) 49 cout <0 <Endl; 50 else51 cout <DP [v] [Kk] <Endl; 52} 53}