1. 01 backpack:
# Define N .. /// N this value is determined based on the specific question int v; // v is the total capacity int dp [N]; void ZeroOnePack (int cost, int weight) {for (int j = v; j> = cost; j --) // note the reverse dp [j] = max (dp [j], dp [j-cost] + weight );}
Ii. Complete backpack:
# Define N .. /// N this value is determined based on the specific question int v; // v is the total capacity int dp [N]; void CompletePack (int cost, int weight) {for (int j = cost; j <= v; j ++) // note the order of dp [j] = max (dp [j], dp [j-cost] + weight );}
Iii. Multiple backpacks:
# Define N .. /// N this value is determined based on the specific question int v; // v is the total capacity int dp [N]; void MultiplePack (int cost, int weight, int amount) {if (cost * amount> = v) CompletePack (cost, weight); else {int k = 1; while (k <amount) {ZeroOnePack (k * cost, k * weight); all-= k; k + = k;} ZeroOnePack (amount * cost, amount * weight );}}