#C6183. Minimum Total Time with Distinct Adjacent Speeds

    ID: 49915 Type: Default 1000ms 256MiB

Minimum Total Time with Distinct Adjacent Speeds

Minimum Total Time with Distinct Adjacent Speeds

You are given a set of runners, each with a certain speed. You need to arrange the runners in order such that no two consecutive runners have the same speed. If such an arrangement is possible, the total time taken is defined as the sum of the speeds of the runners, i.e., \(\sum_{i=1}^{N} v_i\), where \(v_i\) is the speed of the \(i^{th}\) runner in the arrangement. Note that the sum of the speeds remains constant regardless of the order, so if a valid permutation exists the answer will always be the sum of the speeds. Otherwise, output inf (infinity) to represent that no valid arrangement exists.

A valid ordering must satisfy the condition:

[ v_i \neq v_{i-1} \quad\text{for all } 2 \le i \le N. ]

Input Format: The input consists of multiple test cases. Each test case begins with an integer \(N\), the number of runners, followed by \(N\) space-separated integers representing the speeds.

Output Format: For each test case, output a single line containing the total time if a valid ordering exists, otherwise output inf.

inputFormat

The first line of input contains an integer \(T\) indicating the number of test cases. For each test case:

  • The first line contains an integer \(N\) denoting the number of runners.
  • The second line contains \(N\) space-separated integers \(v_1, v_2, \dots, v_N\) representing the speeds.

outputFormat

For each test case, output a single line with one number: the sum \(\sum_{i=1}^{N} v_i\) if a valid permutation exists where no two adjacent speeds are equal; otherwise, output inf.

## sample
1
3
3 6 9
18

</p>