Data Structures & AlgorithmsHardGATE CS · ICPC regional

Items (weight, value) are [(5, 60), (4, 40), (6, 50), (3, 30), (2, 15), (7, 70)]. Each item can be taken at most once, and the total weight must not exceed 8. What is the maximum total value?

  1. A. 90
  2. B. 70
  3. C. 100
  4. D. 105

Correct answer: A. 90

Standard 0/1 knapsack: dp[c] = max(dp[c], dp[c − w] + v) with c scanned downwards so each item is used once. Greedy by value, or by value/weight ratio (fractional knapsack), can be beaten by a combination that packs the capacity exactly.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic