Fill Bag with Maximum Value from Identical-Weight Items
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Tests greedy optimization when all items have uniform weight, requiring recognition that standard knapsack DP is unnecessary and handling large counts efficiently.
You have a bag that can hold up to capacity units of weight. You're given n different item types, where each item type i has the following properties:
- Every individual item of this type weighs exactly 1 unit
- Every individual item of this type has value
values[i] - You have
counts[i]identical items of this type available
Your goal is to select items to place in the bag such that the total weight does not exceed capacity and the total value is maximized.
For each item type i, you may choose any number of items x[i] where 0 <= x[i] <= counts[i]. The total weight will be x[0] + x[1] + ... + x[n-1], which must not exceed capacity. The total value will be x[0] * values[0] + x[1] * values[1] + ... + x[n-1] * values[n-1].
Return the maximum total value achievable.
Constraints
1 <= len(values) == len(counts) <= 10^50 <= values[i] <= 10^61 <= counts[i] <= 10^90 <= capacity <= 10^9- The sum of all available items can reach
10^14 - The maximum possible answer can reach
10^15, exceeding 32-bit integer limits
Examples
Example 1
Input: fill_bag_max_value([6, 10, 3], [2, 1, 5], 4)
Output: 25
Example 2
Input: fill_bag_max_value([4, 7], [3, 2], 10)
Output: 26
Example 3
Input: fill_bag_max_value([5, 0, 9], [1000000000, 3, 1], 1000000000)
Output: 5000000004
Hints
Hint 1
Since every item weighs exactly 1 unit, you're simply choosing at most capacity individual items to maximize value. Which items should you prioritize?
Hint 2
A greedy approach works here because uniform weights eliminate the typical knapsack trade-offs between heavy valuable items and light items.
Hint 3
Process item types in order of decreasing value, taking as many copies as possible from each type until capacity runs out.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since every item weighs exactly 1 unit, this problem simplifies dramatically compared to traditional knapsack problems. We need to select at most capacity individual items to maximize total value, regardless of which item types they come from.
The optimal strategy is greedy: always select the most valuable items first. Since all items have the same weight, there's no trade-off between taking one high-value item versus multiple lower-value items—we can simply rank all available items by value and take the top ones.
Rather than enumerating individual items (which could be up to 10^14 items), we work with item types:
- Sort item types by value in descending order
- Iterate through sorted types, taking as many copies as possible from each type
- For each type, take
min(counts[i], remaining_capacity)items - Stop when capacity is exhausted or when remaining types have zero value
This greedy approach is optimal because of an exchange argument: if an optimal solution includes a lower-value item while excluding a higher-value item, we could swap them to get a better solution, contradicting optimality.
Why It Works
The uniform weight eliminates the core difficulty of knapsack problems. Normally, we need dynamic programming because a heavy valuable item might compete with multiple lighter items. Here, every item occupies exactly one capacity unit, so there's no structural difference between items beyond their values.
Any optimal selection must consist of the highest-valued items available, up to the capacity limit. Items with equal value are interchangeable. Processing types from highest to lowest value ensures we collect exactly these items.
Complexity
- Time: O(n log n) for sorting the n item types, plus O(n) for the iteration. The key insight is that we never iterate over individual items—only over types.
- Space: O(n) for the sorting indices.
Edge Cases
- When
capacity = 0, nothing fits, return 0 - When total available items are fewer than capacity, take all items with positive value
- Item types with
values[i] = 0contribute nothing and can be skipped - Large values: products like
counts[i] * values[i]can approach 10^15, fitting in 64-bit integers but exceeding 32-bit limits
Solution
from typing import List
def fill_bag_max_value(values: List[int], counts: List[int], capacity: int) -> int:
# Sort item types by value in descending order
indices = sorted(range(len(values)), key=lambda i: -values[i])
total_value = 0
remaining_capacity = capacity
for i in indices:
# Stop if bag is full or remaining items have no value
if remaining_capacity <= 0 or values[i] <= 0:
break
# Take as many copies as allowed by both availability and capacity
items_to_take = min(counts[i], remaining_capacity)
total_value += items_to_take * values[i]
remaining_capacity -= items_to_take
return total_value
Time Complexity: O(n log n)
Space Complexity: O(n)