This is a multi-backpack question ..... but there won't be more... it will only be simple... use DFS here ..... however, if you do not pass the current position down, it will time out... I started to get started with TLE and then passed the current status to.
15 ms
#include
int V [25], W [25]; int des [25], n, k, we, Max, DP [25] [25]; int INF = 0x7fffffff; void DFS (int p, int num, int Val, int Wei) // P is indispensable, otherwise timeout .... tragedy .... {// in the future, you must first consider whether to pass the current status .. to reduce the deep search time, if (num = k | Wei = We) if (Val> MAX) max = val; For (INT I = P; I <= N; ++ I) {If (! Des [I]) if (Num + 1 <= k) & (Wei + W [I] <= We) {des [I] = 1; DFS (I + 1, num + 1, Val + V [I], Wei + W [I]); des [I] = 0 ;}} int main () {int t; scanf ("% d", & T); While (t --) {max = 0; scanf ("% d", & N, & K); For (INT I = 1; I <= N; ++ I) des [I] = 0, scanf ("% d ", & V [I], & W [I]); scanf ("% d", & we); DFS (0, 0, 0, 0); printf ("% d \ n ", max);} return 0;}/* 5 18 31 1 */