代码如下:
#include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #define MAXN 105 using namespace std; int N, M, p[MAXN], v[MAXN], num[MAXN], dp[MAXN]; int zo_bag(int v, int p) { for (int i = N; i >= p; --i) { dp[i] = max(dp[i], dp[i-p]+v); } } int c_bag(int x) { int k = 1; while (num[x]-k > 0) { zo_bag(k*v[x], k*p[x]); num[x] -= k; k <<= 1; } if (num[x]) { zo_bag(num[x]*v[x], num[x]*p[x]); } } int DP() { for (int i = 1; i <= M; ++i) { c_bag(i); } return dp[N]; } int main() { int T; scanf("%d", &T); while (T--) { memset(dp, 0, sizeof (dp)); scanf("%d %d", &N, &M); for (int i = 1; i <= M; ++i) { scanf("%d %d %d", &p[i], &v[i], &num[i]); } printf("%d\n", DP()); } return 0; }