Skip to main content

Practice · 4 of 5

Simple Knapsack

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** n items, item i has weight w[i] and value v[i]. Choose a subset with total weight at most W maximizing total value. **Input:** Line 1: n W (1 <= n <= 100, 1 <= W <= 100000). Next n lines: w[i] v[i] (1 <= w[i] <= W, 1 <= v[i] <= 10^9). **Output:** One integer — the maximum total value. **Example:** 2 5 / 3 10 / 4 15 -> 15 (take item 2; both would weigh 7 > 5).

Back to lesson: Practice: Dynamic Programming Problem Set