Ascending Buys Three Copies
0/1 knapsack, space-optimised to one array. Two items, capacity 6.
int w[] = {0, 2, 3}; // 1-indexed
int v[] = {0, 3, 4};
int W = 6;
int f[7] = {0};
for (int i = 1; i <= 2; i++)
for (int j = w[i]; j <= W; j++) // ascending
f[j] = max(f[j], f[j - w[i]] + v[i]);
cout << f[W];
What does this print?
Sign in to answer questions and track your progress
Sign In