A fleet of n drones is lined up on the tarmac, every rotor at rest and every altimeter reading exactly 0 meters. You control the fleet with a handset that issues two kinds of commands. A Boost command targets a single drone and raises its altitude by exactly 1 meter; it costs one command. A Surge command fires simultaneously on the whole fleet, instantly doubling the current altitude of every drone at once (a drone still at 0 stays at 0); it also costs one command. Commands can be issued in any order and interleaved freely. You are handed the mission's required altitude for each drone. Determine the minimum total number of commands (Boosts plus Surges, in any combination and order) needed to bring every drone from 0 up to its own required altitude at the same time.
Print a single integer: the minimum number of commands required.
Example 1
Input
2 1 5
Expected
5
Explanation
Targets are 1 (binary 1) and 5 (binary 101). Boosting to set each drone's bits directly needs 1 + 2 = 3 Boosts total, and since 5 needs bits up through position 2, exactly 2 Surges are needed to shift earlier increments up to that level (e.g. Boost drone2 to 1, Surge to [0,2], Boost drone2 to 3... one valid optimal sequence: Boost the second drone to 1, Surge -> [0,2], Boost the first drone to 1 and the second to 5's next bit, Surge -> ..., ending after 5 total commands). The minimum achievable total is 3 Boosts + 2 Surges = 5.
Example 2
Input
2 2 2
Expected
3
Explanation
Both targets are 2 (binary 10), one set bit each, so 2 Boosts are needed in total. The highest target's bit-length is 2, requiring 1 Surge to shift the boosted bit from position 0 to position 1. Total commands = 2 + 1 = 3.
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 →