A city museum is expanding and has twelve unfinished wings, numbered 0 through 11 (a wing's number doubles as its naming-rights prestige value). A rival donor has already locked in a pledge for every wing, and you know exactly how much they committed to each one; their twelve pledges add up to a fixed total budget T.
You have been handed that same total budget T to distribute across the twelve wings: choose a non-negative integer pledge for every wing so that all twelve pledges sum to exactly T (any money you don't want to spend chasing a wing must still be assigned somewhere, so it is always safe to park leftover funds on wing 0, which is worthless anyway). For every wing where your pledge is strictly greater than the rival's pledge, you win that wing's naming rights and add its wing number to your score. Wing 0 never contributes to your score even if you win it.
Determine the maximum total score you can achieve.
A single integer: the maximum achievable score.
Example 1
Input
5 5 0 0 0 0 0 0 0 0 0 0 0
Expected
45
Explanation
The rival parked their entire budget on wing 0, so overtaking any other wing costs just 1 dollar (riv[i]+1 = 1 for i = 1..11). With 5 dollars you can overtake exactly five wings; choosing the five highest-numbered ones -- 7, 8, 9, 10, and 11 -- gives the maximum score 7+8+9+10+11 = 45.
Example 2
Input
10 0 1 2 3 0 0 0 0 0 0 0 4
Expected
51
Explanation
Spending 1 dollar on each of wings 4 through 10 (7 wings, total cost 7) wins 4+5+6+7+8+9+10 = 49 points. The remaining 3 dollars are exactly enough to also overtake wing 2, whose cost is riv[2]+1 = 3, adding 2 more points for a total of 51. No other affordable combination of wings scores higher, so the answer is 51.
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 →