Maximum Sum With at Most K Elements - Problem

Imagine you're a treasure hunter with a magical bag that can hold at most k precious gems. You've discovered an ancient temple with a grid of gem chambers, where each chamber contains gems of different values.

However, there's a catch! Each row of chambers has a guardian that limits how many gems you can take from that row. The i-th row's guardian allows you to take at most limits[i] gems from that entire row.

Your mission: Select at most k gems from the entire grid to maximize their total value, while respecting each row's guardian limits.

Input:
• A 2D matrix grid of size n × m containing gem values
• An array limits of length n specifying max gems per row
• An integer k representing your bag capacity

Output: The maximum possible sum of gem values you can collect.

Input & Output

example_1.py — Basic Case
$ Input: grid = [[1,2,3],[4,5,6]], limits = [1,2], k = 3
Output: 15
💡 Note: We can take element 6 from row 1 (1 element), elements 5 and 4 from row 1 (2 elements total from row 1), giving us sum = 6 + 5 + 4 = 15. This respects limits[0]=1 (we took 0 from row 0) and limits[1]=2 (we took 2 from row 1), and we took k=3 elements total.
example_2.py — K Constraint Binding
$ Input: grid = [[7,8,9],[1,2,3]], limits = [3,3], k = 2
Output: 17
💡 Note: Even though we could take up to 3 elements from each row, we're limited by k=2. We take the two highest values: 9 and 8, both from row 0, giving sum = 9 + 8 = 17.
example_3.py — Row Limit Binding
$ Input: grid = [[10,1],[2,3]], limits = [1,1], k = 3
Output: 13
💡 Note: We want to take 3 elements but each row allows only 1. We take the best element from each row: 10 from row 0 and 3 from row 1, giving sum = 10 + 3 = 13. We can only take 2 elements total due to row limits.

Constraints

  • 1 ≤ n, m ≤ 103
  • 1 ≤ grid[i][j] ≤ 104
  • 0 ≤ limits[i] ≤ m
  • 1 ≤ k ≤ n × m
  • Sum of all limits[i] ≥ k is not guaranteed

Visualization

Tap to expand
Treasure Vault OptimizationRow 0: Guardian allows 2 gems975Row 1: Guardian allows 1 gem86Sorted by Value (k=3)9✓ Selected8✓ Selected7✓ SelectedResult: Sum = 24Row 0: 2/2 gems usedRow 1: 1/1 gems usedSort & Select
Understanding the Visualization
1
Survey the Vault
Examine all gems and sort them by value from highest to lowest
2
Greedy Selection
Start with the most valuable gem and work your way down
3
Check Guardian Limits
Before taking each gem, verify the row guardian allows it
4
Monitor Bag Capacity
Stop when your bag reaches capacity k or no more valid gems exist
Key Takeaway
🎯 Key Insight: Greedy selection works because we can always replace a lower-value gem with a higher-value one if constraints allow, making the sorted approach optimal!
Asked in
Google 45 Amazon 38 Microsoft 25 Meta 20
42.4K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen