#K62257. Minimum Time to Complete Tasks Without Consecutive Repetitions
Minimum Time to Complete Tasks Without Consecutive Repetitions
Minimum Time to Complete Tasks Without Consecutive Repetitions
In this problem, you are given n tasks, each with an associated positive integer representing the time to complete the task. Your goal is to rearrange these tasks so that no two consecutive tasks are the same. If such an arrangement is possible, output the minimum total time required, which is simply the sum of the time values of all tasks. Otherwise, if it is impossible to avoid consecutive repetitions, output "Impossible".
Formally, let (a_1, a_2, \dots, a_n) be the time values of the tasks. An arrangement is valid if for every (1 \leq i < n), (a_i \neq a_{i+1}).
inputFormat
The input is given via standard input. The first line contains an integer (n), which is the number of tasks. The second line contains (n) space-separated integers, where each integer represents the time cost of the corresponding task.
outputFormat
Output a single line to standard output with the minimum possible total time (i.e. the sum of all task time values) if it is possible to rearrange the tasks so that no two consecutive tasks are the same. Otherwise, output the string "Impossible".## sample
4
1 2 1 2
6
</p>