An adventurer enters a gauntlet containing n monsters, each with a toughness value, and starts with R stamina. The adventurer repeatedly performs moves, each targeting one monster that hasn't been the target of any move yet:
Moves can be performed in any order, any number of times, as long as each move is legal at the moment it is made, and every monster is the target of at most one move overall. The adventurer wants to leave the gauntlet with as many trophies as possible. Determine that maximum.
A single integer: the maximum number of trophies obtainable.
Example 1
Input
4 4 1 2 3 4
Expected
3
Explanation
Sorted toughness: [1,2,3,4], starting stamina 4. Battle 1 (stamina 4->3, trophy 1). Battle 2 (stamina 3->1, trophy 2). Battle 3 is unaffordable (stamina 1 < 3), but a trophy has been earned, so salvage the largest remaining monster (toughness 4): stamina becomes 1+4=5, that monster is spent. Now battle 3 (stamina 5->2, trophy 3). No monsters remain. Total trophies: 3.
Example 2
Input
5 10 2 7 4 3 9
Expected
4
Explanation
Sorted toughness: [2,3,4,7,9], starting stamina 10. Battle 2 (stamina 8, trophy 1), battle 3 (stamina 5, trophy 2), battle 4 (stamina 1, trophy 3). Battle 7 is unaffordable, so salvage the largest remaining monster (toughness 9): stamina becomes 1+9=10. Now battle 7 (stamina 3, trophy 4). No monsters remain. Total trophies: 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →