Parametric MST
Contest: CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
Difficulty rating: 2600
Tags: binary search, constructive algorithms, graphs, greedy, math, sortings

You are given $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. For any real number $$$t$$$, consider the complete weighted graph on $$$n$$$ vertices $$$K_n(t)$$$ with weight of the edge between vertices $$$i$$$ and $$$j$$$ equal to $$$w_{ij}(t) = a_i \cdot a_j + t \cdot (a_i + a_j)$$$.

Let $$$f(t)$$$ be the cost of the minimum spanning tree of $$$K_n(t)$$$. Determine whether $$$f(t)$$$ is bounded above and, if so, output the maximum value it attains.

Input format:
The input consists of multiple test cases. The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10^4$$$) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of vertices of the graph.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^6 \leq a_i \leq 10^6$$$).

The sum of $$$n$$$ for all test cases is at most $$$2 \cdot 10^5$$$.

Output format:
For each test case, print a single line with the maximum value of $$$f(t)$$$ (it can be shown that it is an integer), or INF if $$$f(t)$$$ is not bounded above.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
5
2
1 0
2
-1 1
3
1 -1 -2
3
3 -1 -2
4
1 2 3 -4

Example output:
INF
-1
INF
-6
-18

Editorial:
Assume $$$a_1 \leq \ldots \leq a_n$$$. We will try to connect each node $$$u$$$ to the neighbour $$$v$$$ that minimizes the cost function $$$c(u, v) = a_u a_v + t(a_u + a_v)$$$. If by doing this we obtain a tree which is connected, it will clearly be an MST.
Let $$$b_i(t) = a_i + t$$$. We can rewrite $$$c(u, v)$$$ as $$$c(u, v) = b_u(t)b_v(t) - t^2$$$. So, if we fix $$$t$$$ and $$$u$$$, this value will be minimized if $$$v = n$$$ when $$$b_u(t) \leq 0$$$ or $$$v = 1$$$ when $$$b_u(t) \geq 0$$$.
We have three cases: If there are positive and negative values of $$$b_i(t)$$$, connect all $$$i$$$ with $$$b_i(t) < 0$$$ to $$$v = n$$$, and connect the rest to $$$v = 1$$$. We see that we are adding $$$n - 1$$$ edges (since we are counting the edge $$$\{1, n\}$$$ twice), and that the resulting graph is connected since every node is connected to either $$$1$$$ or $$$n$$$.
If all $$$b_i(t)$$$ are positive, connect all $$$u \neq 1$$$ to $$$v = 1$$$; and if all are negative, connect all $$$u \neq n$$$ to $$$v = n$$$.
Now it is immediate to see that the MST will only change when some $$$b_i(t)$$$ changes its sign, that is, when $$$t = -a_k$$$ for some $$$k$$$, and that the total cost function will be piecewise affine. Furthermore, updating the total cost at each $$$t = -a_k$$$ can be done in $$$O(1)$$$ time if we process nodes from $$$1$$$ to $$$n$$$ and we mantain some cumulative sums.
We are left with checking whether the MST total cost function goes to $$$+\infty$$$ when $$$t \to \pm\infty$$$, which can be done by computing the slope of the MST total cost function at the limiting values $$$t = -a_1$$$ and $$$t = -a_n$$$ (which can be computed by adding the slopes of the cost functions of the edges, the construction of which we have previously mentioned).