Maximum Score From Removing Stones - Problem

Imagine you're playing a strategic stone removal game with three piles of stones containing a, b, and c stones respectively.

The rules are simple but require strategy:

  • Each turn, you must choose two different non-empty piles
  • Remove exactly one stone from each of the chosen piles
  • Earn 1 point for each successful move
  • The game ends when you have fewer than two non-empty piles remaining

Your goal is to maximize your score by making optimal moves. The key insight is that you want to keep playing as long as possible, which means balancing the piles strategically.

Example: With piles [2, 4, 6], you could potentially make up to 6 moves by always picking stones from the two largest piles, resulting in a maximum score of 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: a = 2, b = 4, c = 6
โ€บ Output: 6
๐Ÿ’ก Note: Total stones = 12. Max pile = 6, others = 6. Since 6 โ‰ค 6, we can make 12/2 = 6 moves. Optimal strategy: always pick from the two largest piles.
example_2.py โ€” Imbalanced Case
$ Input: a = 4, b = 4, c = 2
โ€บ Output: 4
๐Ÿ’ก Note: Total stones = 10. Max pile = 4, others = 6. Since 4 โ‰ค 6, we can make 10/2 = 5 moves... wait, let's verify: (4,4,2) โ†’ (3,3,2) โ†’ (2,2,2) โ†’ (1,1,2) โ†’ (0,0,2). Actually 4 moves, limited by balance.
example_3.py โ€” Edge Case
$ Input: a = 1, b = 8, c = 8
โ€บ Output: 8
๐Ÿ’ก Note: Total stones = 17. Max pile = 8, others = 9. Since 8 โ‰ค 9, we can make 17/2 = 8 moves (integer division). The large pile doesn't dominate completely.

Constraints

  • 0 โ‰ค a, b, c โ‰ค 105
  • At least one pile must be non-empty
  • Time limit: 1 second per test case

Visualization

Tap to expand
Pile A: 3Pile B: 4Pile C: 5Game AnalysisTotal stones: 12Largest pile: 5Sum of others: 7Strategy: GreedyMax Score: 6
Understanding the Visualization
1
Identify the Strategy
Always remove from the two largest piles to maximize game duration
2
Check for Limitations
Determine if one pile is so large it limits our total moves
3
Calculate Maximum
Use the mathematical formula to get the answer instantly
Key Takeaway
๐ŸŽฏ Key Insight: Always pick from the two largest piles to maximize the number of moves. The mathematical formula gives us the answer instantly without simulation.
Asked in
Google 25 Amazon 18 Apple 15 Microsoft 12
23.5K Views
Medium Frequency
~15 min Avg. Time
847 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