/* Multiple backpacks, which are converted into 01 backpacks using the binary splitting method */# include <cstdio> # include <cstring> # define maxn 99999 # define max (, b) (a)> (B )? (A) :( B) int DP [maxn], C [maxn], V [maxn]; int main () {int Z, V, M, I, A, B, x, J; scanf ("% d", & Z); While (Z --) {scanf ("% d", & V, & M ); int CNT = 1; for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B, & X); int T = 1; while (x> = T) {v [CNT] = A * t; C [CNT ++] = B * t; x-= T; t <1;} If (x) {v [CNT] = A * X; C [CNT ++] = B * X ;}} memset (DP, 0, sizeof (DP); for (I = 1; I <CNT; I ++) for (j = V; j> = V [I]; J --) DP [J] = max (DP [J], DP [J-V [I] + C [I]); printf ("% d \ n ", DP [v]);} return 0 ;}
/* Completely converted from a backpack to a repeated 01 backpack */# include <cstdio> # include <cstring> # define max (a, B) (a)> (B )? (A) :( B) # define maxn 100010int DP [100010]; int main () {int N, I, j, V; int C [200], V [200]; while (scanf ("% d", & N )! = EOF) {memset (DP, 0, sizeof (DP); for (I = 1; I <= N; I ++) scanf ("% d ", & C [I], & V [I]); scanf ("% d", & V); for (I = 1; I <= N; I ++) for (j = V [I]; j <= V; j ++) DP [J] = max (DP [J], DP [J-V [I] + C [I]); printf ("% d \ n", DP [v]);} return 0 ;}
/* The two-dimensional cost is much deformed. The triple cycle is hard to grasp. */# Include <cstdio> # include <cstring> # define maxn 150 # define max (a, B) (a)> (B )? (A) :( B) int main () {int C [maxn], V [maxn], DP [maxn] [maxn]; int ex, Pa, KI, mm, i, J, K; while (scanf ("% d", & Ex, & PA, & Ki, & mm )! = EOF) // experience, patience, kinds, mostmonserts {int F = 1; memset (DP, 0, sizeof (DP); for (I = 1; I <= Ki; I ++) scanf ("% d", & C [I], & V [I]); for (I = 1; I <= Ki; I ++) for (j = V [I]; j <= PA; j ++) for (k = 1; k <= mm; k ++) DP [J] [k] = max (DP [J] [K], DP [J-V [I] [k-1] + C [I]); for (I = 1; I <= PA; I ++) for (k = 1; k <= mm; k ++) if (DP [I] [k]> = ex) {f = 0; printf ("% d \ n ", pa-I); goto a;} If (f) printf ("-1 \ n"); A:;} return 0 ;}