int N, M; int tcost[NMAX + 5]; int mget[NMAX + 5]; int ans[MMAX + 5]; intmain() { cin >> N >> M; for (int i = 1; i <= N; i++) cin >> tcost[i] >> mget[i]; for (int i = 1; i <= N; i++) { for (int j = M; j >= tcost[i]; j--) ans[j] = max(ans[j - tcost[i]] + mget[i], ans[j]); } cout << ans[M] << endl; return0; }
for (int k = 1; k <= ts; k++) // 循环每一组 for (int i = m; i >= 0; i--) // 循环背包容量 for (int j = 1; j <= cnt[k]; j++) // 循环该组的每一个物品 if (i >= w[t[k][j]]) // 背包容量充足 dp[i] = max(dp[i], dp[i - w[t[k][j]]] + c[t[k][j]]); // 像0-1背包一样状态转移
memset(dp, 0, sizeof(dp)); int i, j, p, x, y, z; scanf("%d%d%d", &n, &m, &K); for (i = 0; i < n; i++) scanf("%d", &w[i]); for (i = 0; i < n; i++) scanf("%d", &c[i]); for (i = 0; i < n; i++) { for (j = m; j >= c[i]; j--) { for (p = 1; p <= K; p++) { a[p] = dp[j - c[i]][p] + w[i]; b[p] = dp[j][p]; } a[p] = b[p] = -1; x = y = z = 1; while (z <= K && (a[x] != -1 || b[y] != -1)) { if (a[x] > b[y]) dp[j][z] = a[x++]; else dp[j][z] = b[y++]; if (dp[j][z] != dp[j][z - 1]) z++; } } } printf("%d\n", dp[m][K]);