TheForces Round #28 (Epic-Forces)
A. Minimum Black Cells
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two integers $$$n$$$ and $$$k$$$.

There's an $$$n\times n$$$ grid. At each step, you can move one cell up, down, left, or right, but you cannot move out of bounds. At first, all cells in the grid are white. You need to color some cells black, such that no matter how you move from the top-left corner to the bottom-right corner, you pass through at least $$$k$$$ number of black cells.

Find the minimum number of black cells required to be colored to satisfy the condition above.

Input

The first line contains a single integer $$$t(1 \le t\le 1000)$$$ — the number of test cases.

The only line of each test case contains two space-separated integers $$$n$$$ and $$$k(2 \le n\le 1000,0\le k\le 2\cdot n-1)$$$.

Output

For each test case, output in a new line  — the minimum number of black cells required to be colored to satisfy the condition above.

Example
Input
5
2 0
2 1
6 7
10 19
300 194
Output
0
1
16
100
9506

B. Sequence Duplication
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given an array $$$a$$$ of size $$$n$$$ and an array $$$b$$$ of size $$$m$$$.

Find the minimum $$$k$$$ such that $$$a$$$ is a subsequence$$$^\dagger$$$ of $$$dup(b, k)$$$. If there's no such $$$k$$$, output $$$-1$$$ instead.

Here $$$dup(b,k)=[b_1,b_2,\ldots,b_m,b_1,b_2,\ldots,b_m,\ldots]$$$($$$k$$$ times). More formally, $$$dup(b,k)$$$ is an array $$$x$$$ of length $$$m \cdot k$$$, where $$$x_i = b _{(i-1)\ mod\ m\ +\ 1}$$$ for all $$$1 \le i \le m \cdot k$$$.

$$$^\dagger$$$A subsequence of a sequence is a sequence that is formed by selecting zero or more elements from the original sequence while maintaining the relative order of the selected elements.

Input

The first line contains a single integer $$$t(1 \le t\le 10^4)$$$ — the number of test cases.

The first line of each test case contains two numbers $$$n,m(1 \le n,m\le 2\times10^5)$$$.

The second line of each test case contains $$$n$$$ numbers $$$a_1,a_2,\ldots,a_n(1 \le a_i\le max(n,m))$$$.

The third line of each test case contains $$$m$$$ numbers $$$b_1,b_2,\ldots,b_m(1 \le b_i\le max(n,m))$$$.

It is guaranteed that the sum of $$$n$$$ for all tests does not exceed $$$2\times 10^5$$$, and the sum of $$$m$$$ for all tests does not exceed $$$2\times 10^5$$$.

Output

For each test case, output the minimum $$$k$$$. If there's no suitable $$$k$$$, output $$$-1$$$ instead.

Example
Input
4
2 1
1 1
2
3 4
2 1 2
1 2 1 1
4 4
1 2 3 4
4 3 2 1
18 17
11 17 1 5 16 17 16 18 16 13 16 2 9 16 16 11 5 1
3 16 18 18 11 5 13 16 1 14 17 9 16 2 17 5 13
Output
-1
2
4
7

C. Perfect Square Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given an $$$n \times n$$$ square matrix. At first, all elements in this matrix are zero.

You need to change $$$n$$$ elements in the matrix so that $$$1,2, \ldots ,n$$$ appears exactly once.

A square matrix of size $$$n \times n$$$ is considered perfect if its number of good square submatrices reaches maximum. A square submatrix of size $$$m \times m (m \le n)$$$ is considered good if it contains the integer $$$m$$$.

Count the number of possible perfect square matrices. Since the answer may be large, output the answer modulo $$$998\,244\,353$$$.

Input

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$  — the number of testcases.

The first line of each testcase contains a single integer $$$n$$$ $$$(2 \le n \le 10^6)$$$  — the size of the matrix.

It's guaranteed that the sum of $$$n$$$ over all testcases doesn't exceed $$$10^6$$$.

Output

For each testcase, output in a new line  — the answer modulo $$$998\,244\,353$$$.

Example
Input
4
2
3
5
999990
Output
12
56
25872
492008352
Note

In the first test case, you can freely select two elements and change them into $$$1$$$ and $$$2$$$. All matrices obtained using this method are perfect. Thus, the answer is $$$A^{4}_{2}=12$$$.

For example,

12
00

is perfect. Its number of good square submatrices reaches the maximum $$$2$$$. It has one $$$1 \times 1$$$ good square submatrix and one $$$2 \times 2$$$ good square submatrix.

In the second test case, for example,

100
020
003

is perfect. Its number of good square submatrices reaches the maximum $$$6$$$. It has one $$$1 \times 1$$$ good square submatrix, four $$$2 \times 2$$$ good square submatrices, and one $$$3 \times 3$$$ good square submatrix.

D. Tree Merger
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree with $$$n$$$ nodes. At first, the value of the $$$i$$$-th node is $$$a_i$$$.

The two nodes can be merged only if the following two conditions hold:

  1. The values of the two nodes are equal;
  2. They are connected by an edge.

In the $$$i$$$-th merging, a new node with number $$$(n+i)$$$ is created. Assume we'll merge node $$$x$$$ and $$$y$$$, we set the value of node $$$(n+i)$$$ to the sum of the value of node $$$x$$$ and $$$y$$$, and we add edges of node $$$(n+i)$$$ and all the nodes connected to node $$$x$$$ and $$$y$$$, except $$$x$$$ and $$$y$$$ themselves. After that, node $$$x$$$ and $$$y$$$ will be removed.

Find if it's possible to merge all nodes into one node.

Input

The first line contains a single integer $$$t(1 \le t\le 10^4)$$$ — the number of test cases.

The first line of each test case contains a single number $$$n$$$($$$2\le n\le 2 \cdot 10^5$$$).

The next line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n(1\le a_i\le 10^9)$$$  — the value on the $$$i$$$-th node.

The next $$$n-1$$$ lines contain two integers $$$u_i$$$,$$$v_i$$$($$$1\le u_i,v_i\le n$$$)  —the numbers of nodes connected by the $$$i$$$-th edge. It is guaranteed that the input forms a tree.

It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output YES if it's possible to merge all nodes into one node.Otherwise, output NO.

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.

Example
Input
5
5
8 1 1 2 4
1 2
2 3
3 4
3 5
4
3 3 3 3
1 2
2 3
2 4
4
3 3 3 3
1 2
2 3
3 4
2
1 2
1 2
5
1 1 1 4 1
1 2
2 3
2 4
1 5
Output
YES
NO
YES
NO
YES
Note

In the first test case, the initial tree is shown in the following picture:

In the first merging, we merge node $$$2$$$ and node $$$3$$$ to obtain the new node $$$6$$$.

In the second merging, we merge node $$$4$$$ and node $$$6$$$ to obtain the new node $$$7$$$.

In the third merging, we merge node $$$5$$$ and node $$$7$$$ to obtain the new node $$$8$$$.

In the fourth merging, we merge node $$$1$$$ and node $$$8$$$ to obtain the new node $$$9$$$.

In the second test case, it's impossible to merge all nodes into one node.

E. RBS Score
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You have been given a bracket sequence $$$s_1 s_2 \dots s_n$$$ of length $$$n$$$. The score of the sequence $$$s$$$ is the number of pairs $$$(l,r)$$$ such that $$$1 \leq l \leq r \leq n$$$ and $$$s_l s_{l+1} \dots s_r$$$ forms a regular bracket sequence$$$^\dagger$$$.

There are $$$q$$$ operations which will be performed on $$$s$$$. The $$$i$$$-th operation is described as follows:

  • Swap the $$$p_i$$$-th character with the $$$(p_i+1)$$$-th character in $$$s$$$. The effect of this operation is permanent, meaning that the sequence $$$s$$$ is permanently modified after this operation.

For each $$$i$$$ such that $$$1 \leq i \leq q$$$, calculate the score of $$$s$$$ after the $$$i$$$-th operation is performed.

$$$^\dagger$$$A regular bracket sequence is a bracket sequence where if we add + and 1 into the sequence, it is possible for us to get a correct mathematical expression. For example, (), ((())), and ()(()) are all regular bracket sequences, while )(, ((), and ())(()() are not.

Input

The first line contains two integers $$$n,q$$$ ($$$2 \leq n \leq 2 \cdot 10^5, 1 \leq q \leq 2 \cdot 10^5$$$) — the length of the bracket sequence and the number of operations performed.

The second line contains a bracket sequence $$$s$$$ of length $$$n$$$. For each $$$i$$$ such that $$$1 \leq i \leq n$$$, $$$s_i$$$ is either ( or ).

The third line contains $$$q$$$ integers $$$p_1,p_2,\dots,p_q$$$ ($$$1 \leq p_i \lt n$$$) — the operations described in the statement.

Output

Output $$$q$$$ integers $$$c_1,c_2,\dots,c_q$$$ separated by spaces. For each $$$i$$$ such that $$$1 \leq i \leq q$$$, $$$c_i$$$ represents the score of $$$s$$$ after the $$$i$$$-th operation is performed.

Example
Input
6 4
((()))
3 2 4 1
Output
4 4 6 3
Note

After the first operation, $$$s=$$$(()()). There are $$$4$$$ number of pairs $$$(l,r)$$$ such that $$$s_l s_{l+1} \dots s_r$$$ forms a regular bracket sequence, which are $$$(1,6), (2,3), (2,5), (4,5)$$$. Thus, the score of $$$s$$$ after the first operation is $$$4$$$.

After the second operation, $$$s=$$$()(()). The score of $$$s$$$ after this operation is $$$4$$$.

After the third operation, $$$s=$$$()()(). The score of $$$s$$$ after this operation is $$$6$$$.

After the fourth operation, $$$s=$$$)(()(). The score of $$$s$$$ after this operation is $$$3$$$.

F. Too Many BSTs
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You're given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$,and an array of $$$q$$$ integers $$$x_1, x_2, \ldots, x_q$$$. It is guaranteed that all integers $$$a_1, a_2, \ldots, a_n, x_1, x_2, \ldots, x_q$$$ are distinct.

You need to answer $$$q$$$ queries. For the $$$i$$$-th query, find the height$$$^\dagger$$$ of the BST(binary search tree) constructed by inserting $$$n + 1$$$ elements in the order of $$$x_i, a_1, a_2, \ldots, a_n$$$.

Formally, for the $$$i$$$-th query, we set the BST $$$T$$$ empty initially. Then, we run the function $$$\texttt{insert}(T, x_i), \texttt{insert}(T, a_1), \texttt{insert}(T, a_2), \ldots , \texttt{insert}(T, a_n)$$$ in order.

function insert(T, x)
if T is null:
newNode = Node(x) // Create a new node with value x
T = newNode // Set newNode as the root of the tree
else if x < T.value:
T.left = insert(T.left, x) // Recursively insert x into the left subtree
else if x > T.value:
T.right = insert(T.right, x) // Recursively insert x into the right subtree
return T // Return the root of the modified tree

$$$^\dagger$$$The height of a tree refers to the length of the longest path from the root node to any leaf node in the tree. It represents the number of edges on the longest downward path between the root node and a leaf.

Input

The first line contains an integer $$$t(1 \leq t \leq 10^5)$$$, the number of test cases.

For each test case:

  • The first line contains two integers $$$n$$$ and $$$q$$$$$$(2 \leq n,q \leq 5 \cdot 10^5)$$$, the number of elements in the array and the number of queries, respectively.
  • The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, representing the elements of the array.
  • The third $$$q$$$ line contains $$$q$$$ space-separated integers $$$x_1, x_2, \ldots, x_q$$$ $$$(1 \leq x_i \leq 10^9)$$$, representing the queries.
  • It is guaranteed that all integers $$$a_1, a_2, \ldots, a_n, x_1, x_2, \ldots, x_q$$$ are distinct.

It is guaranteed the sum of $$$n$$$ and the sum of $$$q$$$ will not exceed $$$5 \cdot 10^5$$$ over all test cases respectively.

Output

For each query, output in a new line  — $$$q$$$ space-separated integers, the answer of the $$$q$$$ queries.

Example
Input
2
3 2
1 4 3
2 5
5 6
5 9 2 999999999 20
16 1 6 4 10 1000000000
Output
2 3
2 4 3 4 2 4
Note

In the first test case, the final BST for the $$$1$$$-st and $$$2$$$-nd queries are shown as follows.

The final BST for the $$$1$$$-st query. The height of this BST is $$$2$$$.
The final BST for the $$$2$$$-nd query. The height of this BST is $$$3$$$.