Black and White Chef
Time limit1sMemory limit1024 MB
Given N steaks with thicknesses up to 25, find the minimum total grilling time so that both sides of every steak are cooked the same number of times and for the same total time.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Problem

Seonghyeon joined a cooking competition and received this harsh review:
This sirloin was grilled badly. The meat was not cooked even.
He entered the competition again the next season and decided to cook the meat even this time. But he misunderstood the meaning of even, and he plans to grill each steak an even number of times. In other words, both sides of each steak must be grilled the same number of times, at least once. Both sides must also be grilled for the same amount of time.
To grill a steak of thickness , one side must be grilled for exactly minutes and then flipped. The steak cannot be flipped before minutes have passed, and it cannot be left unflipped after minutes have passed.
At minute , Seonghyeon puts all steaks on the grill and starts grilling them at the same time. He cannot remove any steak from the grill during cooking, and when he finishes, all steaks must be cooked evenly.
Overcooked meat gets tough, so Seonghyeon wants to finish grilling all the steaks as quickly as possible while meeting these conditions. How many minutes must he grill the steaks?
Input
The first line contains the number of steaks .
The second line contains the thickness of each steak, a positive integer , separated by spaces.
Output
Print the minimum time, in minutes, that Seonghyeon needs to grill all the steaks evenly.
Hint
For C/C++, use the 64-bit integer type long long, since the answer can exceed the range of the 32-bit int type.