Codeforces Round 848 (Div. 2)
A. Flip Flop Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. The integers are either $$$1$$$ or $$$-1$$$. You have to perform the following operation exactly once on the array $$$a$$$:

  • Choose an index $$$i$$$ ($$$1 \leq i \lt n$$$) and flip the signs of $$$a_i$$$ and $$$a_{i+1}$$$. Here, flipping the sign means $$$-1$$$ will be $$$1$$$ and $$$1$$$ will be $$$-1$$$.

What is the maximum possible value of $$$a_1 + a_2 + \ldots + a_n$$$ after applying the above operation?

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$), the length of the array $$$a$$$.

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i = 1$$$ or $$$a_i = -1$$$).

The sum of $$$n$$$ over all cases doesn't exceed $$$10^5$$$.

Output

For each test case, print the maximum possible sum of the array $$$a$$$ you can get in a separate line.

Example
Input
4
5
-1 1 1 -1 -1
5
1 1 -1 -1 -1
2
1 1
4
1 -1 -1 1
Output
3
3
-2
4
Note

In the first case, we can choose index $$$4$$$ and flip the signs of $$$a_4$$$ and $$$a_5$$$. After this operation, the sum will be $$$-1+1+1+1+1 = 3$$$. We can't make the sum larger than this.

In the third case, the only option is to choose the index $$$1$$$.

B. The Forbidden Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation $$$p$$$ of length $$$n$$$, an array of $$$m$$$ distinct integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1 \le a_i \le n$$$), and an integer $$$d$$$.

Let $$$\mathrm{pos}(x)$$$ be the index of $$$x$$$ in the permutation $$$p$$$. The array $$$a$$$ is not good if

  • $$$\mathrm{pos}(a_{i}) \lt \mathrm{pos}(a_{i + 1}) \le \mathrm{pos}(a_{i}) + d$$$ for all $$$1 \le i \lt m$$$.

For example, with the permutation $$$p = [4, 2, 1, 3, 6, 5]$$$ and $$$d = 2$$$:

  • $$$a = [2, 3, 6]$$$ is a not good array.
  • $$$a = [2, 6, 5]$$$ is good because $$$\mathrm{pos}(a_1) = 2$$$, $$$\mathrm{pos}(a_2) = 5$$$, so the condition $$$\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d$$$ is not satisfied.
  • $$$a = [1, 6, 3]$$$ is good because $$$\mathrm{pos}(a_2) = 5$$$, $$$\mathrm{pos}(a_3) = 4$$$, so the condition $$$\mathrm{pos}(a_2) \lt \mathrm{pos}(a_3)$$$ is not satisfied.

In one move, you can swap two adjacent elements of the permutation $$$p$$$. What is the minimum number of moves needed such that the array $$$a$$$ becomes good? It can be shown that there always exists a sequence of moves so that the array $$$a$$$ becomes good.

A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$, but there is $$$4$$$ in the array).

Input

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 three integers $$$n$$$, $$$m$$$ and $$$d$$$ ($$$2\leq n \leq 10^5$$$, $$$2\leq m\leq n$$$, $$$1 \le d \le n$$$), the length of the permutation $$$p$$$, the length of the array $$$a$$$ and the value of $$$d$$$.

The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1\leq p_i \leq n$$$, $$$p_i \ne p_j$$$ for $$$i \ne j$$$).

The third line contains $$$m$$$ distinct integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1\leq a_i \leq n$$$, $$$a_i \ne a_j$$$ for $$$i \ne j$$$).

The sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^5$$$.

Output

For each test case, print the minimum number of moves needed such that the array $$$a$$$ becomes good.

Example
Input
5
4 2 2
1 2 3 4
1 3
5 2 4
5 4 3 2 1
5 2
5 3 3
3 4 1 5 2
3 1 2
2 2 1
1 2
2 1
6 2 4
1 2 3 4 5 6
2 5
Output
1
3
2
0
2
Note

In the first case, $$$pos(a_1)=1$$$, $$$pos(a_2)=3$$$. To make the array good, one way is to swap $$$p_3$$$ and $$$p_4$$$. After that, the array $$$a$$$ will be good because the condition $$$\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d$$$ won't be satisfied.

In the second case, $$$pos(a_1)=1$$$, $$$pos(a_2)=4$$$. The $$$3$$$ moves could be:

  1. Swap $$$p_3$$$ and $$$p_4$$$.
  2. Swap $$$p_2$$$ and $$$p_3$$$.
  3. Swap $$$p_1$$$ and $$$p_2$$$.
After these moves, the permutation $$$p$$$ will be $$$[2,5,4,3,1]$$$. The array $$$a$$$ will be good because the condition $$$\mathrm{pos}(a_1) \lt \mathrm{pos}(a_2)$$$ won't be satisfied. It can be shown that you can't make the array $$$a$$$ good with fewer moves.

In the third case, $$$pos(a_1)=1$$$, $$$pos(a_2)=3$$$, $$$pos(a_3)=5$$$. The $$$2$$$ moves can be:

  1. Swap $$$p_4$$$ and $$$p_5$$$.
  2. Swap $$$p_3$$$ and $$$p_4$$$.
After these moves, the permutation $$$p$$$ will be $$$[3,4,2,1,5]$$$. The array $$$a$$$ will be good because the condition $$$\mathrm{pos}(a_2) \lt \mathrm{pos}(a_3)$$$ won't be satisfied. It can be shown that you can't make the array $$$a$$$ good with fewer moves.

In the fourth case, $$$pos(a_1)=2$$$, $$$pos(a_2)=1$$$. The array $$$a$$$ is already good.

In the fifth case, $$$pos(a_1)=2$$$, $$$pos(a_2)=5$$$. The $$$2$$$ moves are:

  1. Swap $$$p_1$$$ and $$$p_2$$$.
  2. Swap $$$p_5$$$ and $$$p_6$$$.

C. Flexible String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a string $$$a$$$ and a string $$$b$$$. Both of the strings have length $$$n$$$. There are at most $$$10$$$ different characters in the string $$$a$$$. You also have a set $$$Q$$$. Initially, the set $$$Q$$$ is empty. You can apply the following operation on the string $$$a$$$ any number of times:

  • Choose an index $$$i$$$ ($$$1\leq i \leq n$$$) and a lowercase English letter $$$c$$$. Add $$$a_i$$$ to the set $$$Q$$$ and then replace $$$a_i$$$ with $$$c$$$.

For example, Let the string $$$a$$$ be "$$$\tt{abecca}$$$". We can do the following operations:

  • In the first operation, if you choose $$$i = 3$$$ and $$$c = \tt{x}$$$, the character $$$a_3 = \tt{e}$$$ will be added to the set $$$Q$$$. So, the set $$$Q$$$ will be $$$\{\tt{e}\}$$$, and the string $$$a$$$ will be "$$$\tt{ab\underline{x}cca}$$$".
  • In the second operation, if you choose $$$i = 6$$$ and $$$c = \tt{s}$$$, the character $$$a_6 = \tt{a}$$$ will be added to the set $$$Q$$$. So, the set $$$Q$$$ will be $$$\{\tt{e}, \tt{a}\}$$$, and the string $$$a$$$ will be "$$$\tt{abxcc\underline{s}}$$$".

You can apply any number of operations on $$$a$$$, but in the end, the set $$$Q$$$ should contain at most $$$k$$$ different characters. Under this constraint, you have to maximize the number of integer pairs $$$(l, r)$$$ ($$$1\leq l\leq r \leq n$$$) such that $$$a[l,r] = b[l,r]$$$. Here, $$$s[l,r]$$$ means the substring of string $$$s$$$ starting at index $$$l$$$ (inclusively) and ending at index $$$r$$$ (inclusively).

Input

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 contains two integers $$$n$$$ and $$$k$$$ ($$$1\leq n \leq 10^5$$$, $$$0\leq k\leq 10$$$) — the length of the two strings and the limit on different characters in the set $$$Q$$$.

The second line contains the string $$$a$$$ of length $$$n$$$. There is at most $$$10$$$ different characters in the string $$$a$$$.

The last line contains the string $$$b$$$ of length $$$n$$$.

Both of the strings $$$a$$$ and $$$b$$$ contain only lowercase English letters. The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output

For each test case, print a single integer in a line, the maximum number of pairs $$$(l, r)$$$ satisfying the constraints.

Example
Input
6
3 1
abc
abd
3 0
abc
abd
3 1
xbb
xcd
4 1
abcd
axcb
3 10
abc
abd
10 3
lkwhbahuqa
qoiujoncjb
Output
6
3
6
6
6
11
Note

In the first case, we can select index $$$i = 3$$$ and replace it with character $$$c = \tt{d}$$$. All possible pairs $$$(l,r)$$$ will be valid.

In the second case, we can't perform any operation. The $$$3$$$ valid pairs $$$(l,r)$$$ are:

  1. $$$a[1,1] = b[1,1] =$$$ "$$$\tt{a}$$$",
  2. $$$a[1,2] = b[1,2] =$$$ "$$$\tt{ab}$$$",
  3. $$$a[2,2] = b[2,2] =$$$ "$$$\tt{b}$$$".

In the third case, we can choose index $$$2$$$ and index $$$3$$$ and replace them with the characters $$$\tt{c}$$$ and $$$\tt{d}$$$ respectively. The final set $$$Q$$$ will be $$$\{\tt{b}\}$$$ having size $$$1$$$ that satisfies the value of $$$k$$$. All possible pairs $$$(l,r)$$$ will be valid.

D. Flexible String Revisit
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. In each move, the string $$$a$$$ is modified in the following way.

  • An index $$$i$$$ ($$$1 \leq i \leq n$$$) is chosen uniformly at random. The character $$$a_i$$$ will be flipped. That is, if $$$a_i$$$ is $$$0$$$, it becomes $$$1$$$, and if $$$a_i$$$ is $$$1$$$, it becomes $$$0$$$.

What is the expected number of moves required to make both strings equal for the first time?

A binary string is a string, in which the character is either $$$\tt{0}$$$ or $$$\tt{1}$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the length of the strings.

The second line of each test case contains the binary string $$$a$$$ of length $$$n$$$.

The third line of each test case contains the binary string $$$b$$$ of length $$$n$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

Output

For each test case, output a single line containing the expected number of moves modulo $$$998\,244\,353$$$.

Formally, let $$$M = 998\,244\,353$$$. It can be shown that the 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}$$$.

Example
Input
4
1
0
1
2
00
00
4
1000
1110
5
01001
10111
Output
1
0
665496254
665496277
Note

In the first test case, index $$$1$$$ is chosen randomly and $$$a_1$$$ is flipped. After the move, the strings $$$a$$$ and $$$b$$$ are equal. The expected number of moves is $$$1$$$.

The strings $$$a$$$ and $$$b$$$ are already equal in the second test case. So, the expected number of moves is $$$0$$$.

The expected number of moves for the third and fourth test cases are $$$\frac{56}{3}$$$ and $$$\frac{125}{3}$$$ respectively.

E. The Tree Has Fallen!
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently, a tree has fallen on Bob's head from the sky. The tree has $$$n$$$ nodes. Each node $$$u$$$ of the tree has an integer number $$$a_u$$$ written on it. But the tree has no fixed root, as it has fallen from the sky.

Bob is currently studying the tree. To add some twist, Alice proposes a game. First, Bob chooses some node $$$r$$$ to be the root of the tree. After that, Alice chooses a node $$$v$$$ and tells him. Bob then can pick one or more nodes from the subtree of $$$v$$$. His score will be the bitwise XOR of all the values written on the nodes picked by him. Bob has to find the maximum score he can achieve for the given $$$r$$$ and $$$v$$$.

As Bob is not a good problem-solver, he asks you to help him find the answer. Can you help him? You need to find the answers for several combinations of $$$r$$$ and $$$v$$$ for the same tree.

Recall that a tree is a connected undirected graph without cycles. The subtree of a node $$$u$$$ is the set of all nodes $$$y$$$ such that the simple path from $$$y$$$ to the root passes through $$$u$$$. Note that $$$u$$$ is in the subtree of $$$u$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line contains an integer $$$n$$$ ($$$2\leq n\leq 2 \cdot 10^5$$$) — the number of nodes of the tree.

The next line contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1\leq a_i\leq 10^9$$$) — the values written on the nodes.

Each of the next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1\leq u, v\leq n$$$; $$$u\neq v$$$), denoting there is an undirected edge between node $$$u$$$ and node $$$v$$$. It is guaranteed that the given edges form a tree.

The next line contains an integer $$$q$$$ ($$$1\leq q\leq 2 \cdot 10^5$$$) — the number of queries.

Each of the next $$$q$$$ lines contains two integers $$$r$$$ and $$$v$$$ ($$$1\leq r, v\leq n$$$) — the root node Bob defines, and the node Alice chooses.

It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases don't exceed $$$2 \cdot 10^5$$$.

Output

For each test case, in each query, print a line containing an integer denoting the maximum score Bob can achieve.

Example
Input
3
6
12 12 8 25 6 1
1 5
1 2
2 6
2 3
2 4
3
4 2
3 5
1 2
2
3 8
1 2
4
2 2
2 1
1 2
1 1
3
3 8 7
1 2
2 3
2
2 2
2 1
Output
15
6
29
11
3
8
11
15
3
Note

In each of the below figures, the green-colored node is the node picked by Bob, and the red-colored node is the node picked by Alice. The values of the nodes are placed in the blue boxes beside the nodes.

Consider the first example. In the first query, if we put the root node $$$4$$$ at the top of the tree, the tree looks like the below figure:

Tree with root node $$$4$$$ in the first query.
The nodes in the subtree of the node $$$2$$$ are $$$[2,1,5,6,3]$$$. Among them, Bob can pick node $$$3$$$, node $$$5$$$, and node $$$6$$$. His score will be $$$a_3 \oplus a_5 \oplus a_6 = 8 \oplus 6 \oplus 1 = 15$$$. He can't achieve any score more than this.

In the second query, if the root node $$$3$$$ is placed at the top of the tree, the tree looks like the below figure:

Tree with root node $$$3$$$ in the second query.
The only node in the subtree of the node $$$5$$$ is $$$5$$$. Bob can only pick the node $$$5$$$. His score will be $$$a_5 = 6$$$.

In the third query, if the root node $$$1$$$ is placed at the top of the tree, the tree looks like this:

Tree with root node $$$1$$$ in the third query.
The nodes in the subtree of the node $$$2$$$ are $$$[2,3,6,4]$$$. Among them, Bob can pick node $$$2$$$, node $$$3$$$, and node $$$4$$$. His score will be $$$a_2 \oplus a_3 \oplus a_4 = 12 \oplus 8 \oplus 25 = 29$$$. He is not able to score higher than that.

F. Maximizing Root
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given a rooted tree consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Vertex $$$1$$$ is the root of the tree. Each vertex has an integer value. The value of $$$i$$$-th vertex is $$$a_i$$$. You can do the following operation at most $$$k$$$ times.

  • Choose a vertex $$$v$$$ that has not been chosen before and an integer $$$x$$$ such that $$$x$$$ is a common divisor of the values of all vertices of the subtree of $$$v$$$. Multiply by $$$x$$$ the value of each vertex in the subtree of $$$v$$$.

What is the maximum possible value of the root node $$$1$$$ after at most $$$k$$$ operations? Formally, you have to maximize the value of $$$a_1$$$.

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The subtree of a node $$$u$$$ is the set of all nodes $$$y$$$ such that the simple path from $$$y$$$ to the root passes through $$$u$$$. Note that $$$u$$$ is in the subtree of $$$u$$$.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 50\,000$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq k \leq n$$$) — the number of vertices in the tree and the number of operations.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 1000$$$), where $$$a_i$$$ denotes the value of vertex $$$i$$$.

Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$, $$$u_i \neq v_i$$$), denoting the edge of the tree between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree.

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

Output

For each test case, output the maximum value of the root after performing at most $$$k$$$ operations.

Example
Input
2
5 2
24 12 24 6 12
1 2
1 3
2 4
2 5
5 3
24 12 24 6 12
1 2
1 3
2 4
2 5
Output
288
576
Note

Both examples have the same tree:

For the first test case, you can do two operations as follows:

  • Choose the subtree of vertex $$$4$$$ and $$$x = 2$$$.
    After this operation, the node values become $$$\{24, 12, 24, 12, 12\}.$$$
  • Choose the subtree of vertex $$$1$$$ and $$$x = 12$$$.
    After this operation, the node values become $$$\{288, 144, 288, 144, 144\}.$$$
The value of the root is $$$288$$$ and it is the maximum.

For the second test case, you can do three operations as follows:

  • Choose the subtree of vertex $$$4$$$ and $$$x = 2$$$.
    After this operation, the node values become $$$\{24, 12, 24, 12, 12\}.$$$
  • Choose the subtree of vertex $$$2$$$ and $$$x = 4$$$.
    After this operation, the node values become $$$\{24, 48, 24, 48, 48\}.$$$
  • Choose the subtree of vertex $$$1$$$ and $$$x = 24$$$.
    After this operation, the node values become $$$\{576, 1152, 576, 1152, 1152\}.$$$
The value of the root is $$$576$$$ and it is the maximum.