Deep in the wilderness, Zhily and Jily discovered a series of gathering places that contain abstract logic. Some of these gathering places harbor inconsistent errors in their logic, which may collapse at any moment. They hope to transmit logic between adjacent gathering places through reasonable transfer arrangements so that as many gathering places as possible can eventually restore logical stability.
You are given an array $$$a$$$ of $$$n$$$ integers. You can perform the following operation any number of times:
Each index can be chosen at most once.
Find the maximum number of positive integers in the final array after all operations.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n \leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-10^9\leq a_i\leq 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.
For each test case, you should output a single line containing an integer $$$k$$$, the number of positive numbers in the final sequence.
450 -1 3 -3 050 -2 1 2 350 1 0 1 021000000000 -1000000000
3541
In the first test case, the array $$$a$$$ is operated on by the following process:
$$$[0,-1,3,-3,0]\to [0,\color{red}{2},3,-3,0]\to [\color{red}{2},2,3,-3,0]$$$.
There are $$$3$$$ positive numbers in the final array, and it can be proven that this is the maximum count of positive integers.
In the second test case, the final array can be:$$$[4,4,6,5,3]$$$, which is operated on by the following process:
$$$[0,-2,1,2,3]\to [0,-2,1,\color{red}{5},3]\to [0,-2,\color{red}{6},5,3]\to [0,\color{red}{4},6,5,3]\to [\color{red}{4},4,6,5,3]$$$.
In the third test case, the final array can be:$$$[1,1,1,1,0]$$$, which is operated on by the following process:
$$$[0,1,0,1,0]\to [\color{red}{1},1,0,1,0]\to [1,1,\color{red}{1},1,0]$$$.
Deeper in the wilderness, Zhily and Jily discovered a mysterious sequence of numbers. Each prefix of the sequence has an important characteristic value, known as the mex and the max. By rearranging the sequence, it can generate a special kind of magic.
You are given an array $$$a$$$ of $$$n$$$ non-negative integers. You can rearrange it arbitrarily.
Find the maximum possible value of the sum of the MEX$$$^{\text{∗}}$$$ and the maximum value over all prefixes. Formally, you need to maximize the following expression: $$$\sum\limits_{i=1}^n (\operatorname{mex}(a_1,a_2,\cdots,a_i)+\max(a_1,a_2,\cdots,a_i))$$$.
$$$^{\text{∗}}$$$The minimum excluded (MEX) of a collection of integers $$$c_1, c_2, \ldots, c_k$$$ is defined as the smallest non-negative integer $$$x$$$ which does not occur in the collection $$$c$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$) — the length of the array $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\cdots,a_n$$$ ($$$0\leq a_i \leq 10^9$$$) — the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output the maximum sum on a new line.
550 0 0 0 020 151 1 1 1 061 1 4 5 1 411000000000
5413301000000000
In the first test case, no matter how you rearrange $$$a$$$, the MEX and the maximum value for each prefix will always be $$$1$$$ and $$$0$$$, respectively.
In the third test case, you can rearrange $$$a$$$ into $$$[0,1,1,1,1]$$$, so that $$$\sum\limits_{i=1}^n (\operatorname{mex}(a_1,a_2,\cdots,a_i)+\max(a_1,a_2,\cdots,a_i))=(1+0)+(2+1)+(2+1)+(2+1)+(2+1)=13$$$. It can be shown that this is the optimal solution.
Deep in the wilderness, Zhily and Jily discovered two sequences composed of brackets. Each sequence carries some logical structure, but neither of them is necessarily a regular bracket sequence on its own. They found that by swapping brackets between the two sequences, they could repair them. They wish to make both sequences regular bracket sequences by swapping brackets between them.
A regular bracket sequence is a sequence consisting of '(' and ')', which can be turned into a valid math expression by inserting 1 and + any number of times into the sequence. For example, the sequence "()(()())" is a regular bracket sequence, while "())((" or "(()" are not regular bracket sequences.
You are given two bracket sequences $$$a$$$ and $$$b$$$ of even length $$$n$$$. You can perform the following operation any number of times:
Determine whether you can turn both $$$a$$$ and $$$b$$$ into regular bracket sequences.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 2\cdot 10^5$$$, $$$n$$$ is even) — the length of the strings $$$a$$$ and $$$b$$$.
The second line of each test case contains a sequence $$$a$$$ of length $$$n$$$ consisting only of characters '(' and ')'.
The third line of each test case contains a sequence $$$b$$$ of length $$$n$$$ consisting only of characters '(' and ')'.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output "YES" if you can turn both $$$a$$$ and $$$b$$$ into a regular bracket sequences, "NO" otherwise. You can output "YES" and "NO" in any case (for example, "yES", "yes", and "Yes" will be recognized as a positive response).
72()()4))(((())4(((())))4()()(())6(((())()()))8()()()()(((())))4((((((((
YESNONOYESYESYESNO
In the first, fourth, and sixth test cases, both $$$a$$$ and $$$b$$$ are initially regular bracket sequences.
In the second and third test cases, either $$$a_1$$$ or $$$b_1$$$ will be ')', which makes it impossible for them to be regular.
In the fifth test case, the final sequences may be $$$\texttt{(((}\color{red}{\texttt{)}}\texttt{))}$$$ and $$$\texttt{()(}\color{red}{\texttt{(}}\texttt{))}$$$, where the brackets to be swapped are colored red.
Zhily developed a game called Barknights, and she is preparing to release a major update on March $$$25$$$. Specifically, she plans to add a Module to each Operator in the game, multiplying their power level by the Module's power.
After the update is released, the famous game streamer Jily will evaluate the Operators' power levels and rank them in a tier list. Whenever an earlier-released Operator is ranked higher than a later-released Operator, it will cause a wave of drama.
Unfortunately, Zhily accidentally knocked over a hot water kettle and broke her computer, which caused all the Modules to be randomly rearranged. Now Zhily wants to know the expected number of waves of drama that will be generated, but since she has to move on to the next problem, she has entrusted this task to you.
You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ positive integers. Let $$$b'$$$ be a permutation of array $$$b$$$ chosen uniformly at random among all $$$n!$$$ possible permutations. Define $$$ c_i = a_i \cdot b'_i$$$ for $$$1\le i \le n$$$.
Find the expected number of inversions$$$^{\text{∗}}$$$ of array $$$c$$$.
$$$^{\text{∗}}$$$An inversion in array $$$c$$$ is a pair of indices $$$(i, j)$$$ such that $$$1 \le i \lt j \le n$$$ and $$$c_i \gt c_j$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the length of the arrays $$$a$$$ and $$$b$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$.
The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the array $$$b$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.
For each test case, output the expected number of inversions in $$$c$$$ modulo $$$998\,244\,353$$$.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the exact answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
351 14 5 1 41 1 1 1 133 2 53 2 51010 72 65 43 73 23 78 13 49 9931 90 45 19 44 18 59 31 48 29
5665496236820778710
In the first test case, since all elements of $$$b$$$ are $$$1$$$, any of the $$$5!$$$ permutations results in $$$b' = (1, 1, 1, 1, 1)$$$. Thus, $$$c = (1, 14, 5, 1, 4)$$$ is always constant. The inversions are $$$(2, 3), (2, 4), (2, 5), (3, 4),$$$ and $$$(3, 5)$$$. The expected number of inversions is $$$5 \equiv 5 \pmod{998\,244\,353}$$$.
In the second test case, there are $$$3! = 6$$$ equally likely permutations $$$b'$$$. The resulting arrays $$$c$$$ and their respective inversion counts are shown below:
| $$$b'$$$ | $$$c = (3b'_1, 2b'_2, 5b'_3)$$$ | Inversion Count |
| $$$(3, 2, 5)$$$ | $$$(9, 4, 25)$$$ | 1 |
| $$$(3, 5, 2)$$$ | $$$(9, 10, 10)$$$ | 0 |
| $$$(2, 3, 5)$$$ | $$$(6, 6, 25)$$$ | 0 |
| $$$(2, 5, 3)$$$ | $$$(6, 10, 15)$$$ | 0 |
| $$$(5, 3, 2)$$$ | $$$(15, 6, 10)$$$ | 2 |
| $$$(5, 2, 3)$$$ | $$$(15, 4, 15)$$$ | 1 |
The expected number of inversions is $$$\frac{1+0+0+0+2+1}{6} = \frac{4}{6} = \frac{2}{3}$$$.
Modulo $$$998\,244\,353$$$, the answer is $$$2 \cdot 3^{-1} \equiv 665\,496\,236 \pmod{998\,244\,353}$$$.
While returning to the base, Jily lost his way. He found that the paths ahead formed a tree. At every fork in the road, there was a signpost, but these signposts were affected by an anomalous magnetic field and kept rotating. Zhily became worried about Jily and wants to ask you several questions. Each time, given a specific moment, he asks which node Jily would eventually stop at if he started from the beginning.
You are given a tree with $$$n$$$ nodes, rooted at $$$1$$$. Node $$$u$$$ has $$$d_u$$$ children, sorted by node index in increasing order, namely $$$s_{u,0},\ldots,s_{u,d_u-1}$$$. It takes some time to cross each edge; specifically, the time needed to cross the edge between node $$$u$$$ and its parent is $$$l_u$$$.
Each non-leaf node in the tree has a signpost pointing to one of its children. When you are at such a node, you must immediately move to the child it points to, continuing in this way until you reach a leaf node, where you stop immediately.
The signposts change over time. At time $$$m$$$, the signpost of node $$$u$$$ points to its $$$(m\bmod d_u+1)$$$-th child, that is, node $$$s_{u,m \bmod d_u}$$$.
There are now $$$q$$$ queries. In each query, you are given $$$m$$$, and you need to determine the index of the leaf node you will eventually reach if you start from the root at time $$$m$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains two positive integers $$$n,q$$$ ($$$1 \le n \le 5 \cdot 10^5,1 \le q \le 10^6$$$).
The second line contains $$$n-1$$$ positive integers $$$f_2,\ldots,f_n$$$ ($$$1 \le f_u \lt u$$$), where $$$f_u$$$ denotes the index of the parent of node $$$u$$$.
The third line contains $$$n-1$$$ non-negative integers $$$l_2,\ldots,l_n$$$ ($$$0 \le l_u \le 10^9$$$), where $$$l_u$$$ denotes the time needed to cross the edge between node $$$u$$$ and its parent.
The fourth line contains $$$q$$$ non-negative integers $$$m_1,\ldots,m_{q}$$$ ($$$0 \le m_i \le 10^{18}$$$), representing the $$$q$$$ queries.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.
It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output one line containing $$$q$$$ positive integers, representing the answer to each query.
23 11 110 20410 51 2 2 2 1 1 3 4 51 2 3 4 5 6 7 8 91 2 3 4 5
26 7 9 6 7
The tree in the first test case is shown below.
The tree in the second test case is shown below.
Zhily and Jily resolved to travel across the entire world to eliminate all chaos. They start from a certain place and wish to visit every region exactly once.
You are given a directed graph with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. For each vertex $$$i$$$ ($$$1 \le i \le n$$$), there are directed edges from $$$i$$$ to all vertices $$$j$$$ such that $$$a_i \le j \le n$$$.
Find a Hamiltonian cycle$$$^{\text{∗}}$$$ of the graph.
$$$^{\text{∗}}$$$A Hamiltonian cycle is a cycle that visits each vertex exactly once.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n\,(1 \leq n \leq 10^5)$$$ — the number of vertices.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\cdots,a_n\,(1 \leq a_i \leq n)$$$ describing the graph.
It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^6$$$.
For each test case, if no Hamiltonian cycle exists, output "No" in one line.
Otherwise, output "Yes" on the first line. On the second line, output a permutation $$$p_1, p_2, \dots, p_n$$$ ($$$1\le p_i\le n$$$) representing the order of vertices visited in the cycle. If there are multiple solutions, print any of them.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
571 3 6 3 2 1 594 2 9 8 5 8 5 7 983 4 4 5 6 1 8 21155 4 2 5 5
Yes7 5 2 4 3 6 1NoYes1 3 7 8 2 4 5 6Yes1No
In the first test case, the figure below illustrates a Hamiltonian cycle: $$$1 \to 7 \to 5 \to 2 \to 4 \to 3 \to 6 \to 1$$$.

In the second test case, no Hamiltonian cycle exists because there are no incoming edges to vertex $$$1$$$ (i.e., no vertex can reach vertex $$$1$$$).