solulubeta
Sign in
Recursion & Memo / DP patterns for coding tests
04.05 · Problem

0/1 Knapsack

Medium Take or skip with a capacity Pro

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
Mark this problem as solved when you are finished.

Item 31 of 44 · updated 2026-10-13