You are given the sequence $a_1, a_2, \dots, a_n$. You can choose any subset of elements and then reorder them to create a “saw”.
The sequence $b_1, b_2, \dots, b_m$ is called a “saw” if the elements satisfy one of the following series of inequalities: $b_1>b_2<b_3>b_4<\dots$ or $b_1<b_2>b_3<b_4>\dots$.
Find the longest saw which can be obtained from a given array.
Note that both the given sequence $a$ and the required saw $b$ can contain duplicated (non-unique) values.
Input
The first line contains an integer $t$ ($1 \le t \le 10^5$) — the number of test cases in the input. Then the descriptions of the $t$ test cases follow. Each test case begins with a line containing integer $n$ ($1 \le n \le 2\cdot10^5$). Then a line containing $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) follows.
It’s guaranteed that $\sum{n}$ doesn’t exceed $2 \cdot 10^5$.
Output
For each test case, print two lines: print the length of the longest saw in the first line, and the saw itself in the second line. If there are several solutions, print any of them.
Example
input
3 10 10 9 8 7 6 5 4 3 2 1 7 1 2 2 2 3 2 2 3 100 100 100
output
10 1 6 2 7 3 8 4 9 5 10 4 2 1 3 2 1 100
Solution:
fun main(args: Array<String>) { val tt = readLine()!!.toInt() var res = Array<String>(tt, {i -> ""}) for (qq in 0..tt-1) { val n = readLine()!!.toInt() val a = readLine()!!.split(" ").map { it.toInt() } var b: MutableList<Int> = mutableListOf() for (c in a) { b.add(c) } b.sort() var mx = -1 var who = -1 var beg = 0 while (beg < n) { var end = beg while (end + 1 < n && b[end + 1] == b[end]) end++ var occ = end - beg + 1 if (occ > mx) { mx = occ who = b[beg] } beg = end + 1 } var rest = n - mx if (mx > rest) { mx = rest if (who == b[0] || who == b.last()) { mx++ } } else { mx = -1 who = -1 } var d: MutableList<Int> = mutableListOf() if (who != -1) { for (i in 0 until mx) { d.add(who) } } for (c in b) { if (c != who) { d.add(c) } } d.sort() var e: MutableList<Int> = mutableListOf() if (d.size % 2 == 0) { for (i in 0 until (d.size / 2)) { e.add(d[d.size / 2 - 1 - i]) e.add(d[d.size - 1 - i]) } } else { e.add(d[d.size / 2]) if (d[0] == d[d.size / 2]) { for (i in 0 until (d.size / 2)) { e.add(d[d.size - 1 - i]) e.add(d[d.size / 2 - 1 - i]) } } else { for (i in 0 until (d.size / 2)) { e.add(d[i]) e.add(d[d.size / 2 + 1 + i]) } } } res[qq] = (e.size).toString() + "\n" + e.joinToString(" ") } println(res.joinToString("\n")) }