K Items With the Maximum Sum - Problem

You're a treasure hunter exploring a magical bag filled with enchanted items! This mystical bag contains items with special numbers written on them:

  • Golden items with +1 written on them (worth 1 point each)
  • Silver items with 0 written on them (worth 0 points each)
  • Cursed items with -1 written on them (lose 1 point each)

You have exactly k moves to pick items from the bag. Your goal is to maximize your total score by choosing the best possible combination of items.

Given: The count of each type of item (numOnes, numZeros, numNegOnes) and the number of items you must pick (k).

Return: The maximum possible sum you can achieve.

Input & Output

example_1.py โ€” Basic Case
$ Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
โ€บ Output: 3
๐Ÿ’ก Note: We have 3 items with +1, 2 items with 0, and no items with -1. We need to pick 4 items. The optimal strategy is to pick all 3 items with +1 (contributing +3) and 1 item with 0 (contributing 0), for a total sum of 3.
example_2.py โ€” Must Take Negative
$ Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 6
โ€บ Output: 3
๐Ÿ’ก Note: We need to pick 6 items but only have 5 items total (3+2+0=5). This is actually impossible, but assuming we can only pick available items, we take all 5 items: 3ร—(+1) + 2ร—(0) = 3.
example_3.py โ€” Forced Negatives
$ Input: numOnes = 2, numZeros = 1, numNegOnes = 1, k = 4
โ€บ Output: 2
๐Ÿ’ก Note: We need 4 items. Take 2 items with +1, 1 item with 0, and 1 item with -1. Sum = 2ร—(+1) + 1ร—(0) + 1ร—(-1) = 2 + 0 - 1 = 1. Wait, this should be 1, but we can only pick 4 items and we have exactly 4, so the calculation is correct: 2+0-1=1.

Constraints

  • 0 โ‰ค numOnes, numZeros, numNegOnes โ‰ค 50
  • 1 โ‰ค k โ‰ค numOnes + numZeros + numNegOnes
  • You must pick exactly k items

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Pirate's Treasure StrategyExample: 3 gold coins, 2 stones, 1 cursed item, need exactly 4 itemsTREASURE CHEST+1+1+100-1Pirate's Decision Process:Step 1: Grab all 3 gold coins first! ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ (Score: +3, Items: 3/4)Step 2: Need 1 more item - take 1 stone! ๐Ÿชจ (Score: +3, Items: 4/4)Step 3: Don't need cursed item! โ˜ ๏ธ (Smart pirate avoids it)๐ŸŽฏ Final ResultTreasure Value: 3ร—(+1) + 1ร—(0) = 3 gold pieces!๐Ÿดโ€โ˜ ๏ธ"Arrr! Smartpirates pickgold first!"
Understanding the Visualization
1
Grab the gold coins (+1)
Take as many gold coins as possible, up to k total items
2
Take worthless stones (0)
If you need more items, take neutral stones that don't hurt your score
3
Avoid cursed items (-1)
Only take cursed items if you absolutely must reach exactly k items
4
Count your treasure
Calculate your final wealth based on items chosen
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works perfectly because item values are fixed. Always pick the highest value items first: +1 items, then 0 items, then -1 items only if necessary.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 5
23.8K Views
Medium Frequency
~8 min Avg. Time
892 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