04.05 · Problem
0/1 Knapsack
A bag holds at most W kg. Item i weighs wt[i] kg and is worth val[i]. Take each item at most once. Return the most value that fits.
Examples
Example 1
Input: wt = [1, 2, 4, 5], val = [5, 4, 8, 6], W = 5
Output: 13
Items 0 and 2: weight 1 + 4 = 5, value 5 + 8 = 13.
solulu Pro
Continue this problem with Pro
Inside: step-by-step hints · 11 hidden tests · reference solutions · code editor.
Lifetime ₹1,999 ₹999 · Launch offer
Item 31 of 44 · updated 2026-10-13