Codeforces Round 888 (Div. 3)
A. Escalator Conversations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of $$$n$$$ passengers. The escalator has a total of $$$m$$$ steps, all steps indexed from $$$1$$$ to $$$m$$$ and $$$i$$$-th step has height $$$i \cdot k$$$.

Vlad's height is $$$H$$$ centimeters. Two people with heights $$$a$$$ and $$$b$$$ can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps.

For example, if two people have heights $$$170$$$ and $$$180$$$ centimeters, and $$$m = 10, k = 5$$$, then they can stand on steps numbered $$$7$$$ and $$$5$$$, where the height difference between the steps is equal to the height difference between the two people: $$$k \cdot 2 = 5 \cdot 2 = 10 = 180 - 170$$$. There are other possible ways.

Given an array $$$h$$$ of size $$$n$$$, where $$$h_i$$$ represents the height of the $$$i$$$-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually.

For example, if $$$n = 5, m = 3, k = 3, H = 11$$$, and $$$h = [5, 4, 14, 18, 2]$$$, Vlad can have a conversation with the person with height $$$5$$$ (Vlad will stand on step $$$1$$$, and the other person will stand on step $$$3$$$) and with the person with height $$$14$$$ (for example, Vlad can stand on step $$$3$$$, and the other person will stand on step $$$2$$$). Vlad cannot have a conversation with the person with height $$$2$$$ because even if they stand on the extreme steps of the escalator, the height difference between them will be $$$6$$$, while their height difference is $$$9$$$. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is $$$2$$$.

Input

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

Then the descriptions of the test cases follow.

The first line of each test case contains integers: $$$n, m, k, H$$$ ($$$1 \le n,m \le 50$$$, $$$1 \le k,H \le 10^6$$$). Here, $$$n$$$ is the number of people, $$$m$$$ is the number of steps, $$$k$$$ is the height difference between neighboring steps, and $$$H$$$ is Vlad's height.

The second line contains $$$n$$$ integers: $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le 10^6$$$). Here, $$$h_i$$$ represents the height of the $$$i$$$-th person.

Output

For each test case, output a single integer — the number of people Vlad can have a conversation with on the escalator individually.

Example
Input
7
5 3 3 11
5 4 14 18 2
2 9 5 6
11 9
10 50 3 11
43 44 74 98 62 60 99 4 11 73
4 8 8 49
68 58 82 73
7 1 4 66
18 66 39 83 48 99 79
9 1 1 13
26 23 84 6 60 87 40 41 25
6 13 3 28
30 70 85 13 1 55
Output
2
1
4
1
0
0
3
Note

The first example is explained in the problem statement.

In the second example, Vlad can have a conversation with the person with height $$$11$$$.

In the third example, Vlad can have a conversation with people with heights: $$$44, 74, 98, 62$$$. Therefore, the answer is $$$4$$$.

In the fourth example, Vlad can have a conversation with the person with height $$$73$$$.

B. Parity Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have an array of integers $$$a$$$ of length $$$n$$$. You can apply the following operation to the given array:

  • Swap two elements $$$a_i$$$ and $$$a_j$$$ such that $$$i \neq j$$$, $$$a_i$$$ and $$$a_j$$$ are either both even or both odd.

Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).

For example, let $$$a$$$ = [$$$7, 10, 1, 3, 2$$$]. Then we can perform $$$3$$$ operations to sort the array:

  1. Swap $$$a_3 = 1$$$ and $$$a_1 = 7$$$, since $$$1$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 10, 7, 3, 2$$$];
  2. Swap $$$a_2 = 10$$$ and $$$a_5 = 2$$$, since $$$10$$$ and $$$2$$$ are even. We get $$$a$$$ = [$$$1, 2, 7, 3, 10$$$];
  3. Swap $$$a_4 = 3$$$ and $$$a_3 = 7$$$, since $$$3$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 2, 3, 7, 10$$$].
Input

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

The description of the test cases follows.

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

The second line of each test case contains exactly $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of array $$$a$$$.

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 on a separate line:

  • YES if the array can be sorted by applying the operation to it some number of times;
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

Example
Input
6
5
7 10 1 3 2
4
11 9 3 5
5
11 3 15 3 2
6
10 7 8 1 2 3
1
10
5
6 6 4 1 6
Output
YES
YES
NO
NO
YES
NO
Note

The first test case is explained in the problem statement.

C. Tiles Comeback
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad remembered that he had a series of $$$n$$$ tiles and a number $$$k$$$. The tiles were numbered from left to right, and the $$$i$$$-th tile had colour $$$c_i$$$.

If you stand on the first tile and start jumping any number of tiles right, you can get a path of length $$$p$$$. The length of the path is the number of tiles you stood on.

Vlad wants to see if it is possible to get a path of length $$$p$$$ such that:

  • it ends at tile with index $$$n$$$;
  • $$$p$$$ is divisible by $$$k$$$
  • the path is divided into blocks of length exactly $$$k$$$ each;
  • tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different.

For example, let $$$n = 14$$$, $$$k = 3$$$.

The colours of the tiles are contained in the array $$$c$$$ = [$$$\color{red}{1}, \color{violet}{2}, \color{red}{1}, \color{red}{1}, \color{violet}{2}, \color{violet}{2}, \color{green}{3}, \color{green}{3}, \color{red}{1}, \color{green}{3}, \color{blue}{4}, \color{blue}{4}, \color{violet}{2}, \color{blue}{4}$$$]. Then we can construct a path of length $$$6$$$ consisting of $$$2$$$ blocks:

$$$\color{red}{c_1} \rightarrow \color{red}{c_3} \rightarrow \color{red}{c_4} \rightarrow \color{blue}{c_{11}} \rightarrow \color{blue}{c_{12}} \rightarrow \color{blue}{c_{14}}$$$

All tiles from the $$$1$$$-st block will have colour $$$\color{red}{\textbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$\color{blue}{\textbf{4}}$$$.

It is also possible to construct a path of length $$$9$$$ in this example, in which all tiles from the $$$1$$$-st block will have colour $$$\color{red}{\textbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$\color{green}{\textbf{3}}$$$, and from the $$$3$$$-rd block will have colour $$$\color{blue}{\textbf{4}}$$$.

Input

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

The description of the test cases follows.

The first line of each test case ontains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$)—the number of tiles in the series and the length of the block.

The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, \dots, c_n$$$ ($$$1 \le c_i \le n$$$) — the colours of the tiles.

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 on a separate line:

  • YES if you can get a path that satisfies these conditions;
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

Example
Input
10
4 2
1 1 1 1
14 3
1 2 1 1 7 5 3 3 1 3 4 4 2 4
3 3
3 1 3
10 4
1 2 1 2 1 2 1 2 1 2
6 2
1 3 4 1 6 6
2 2
1 1
4 2
2 1 1 1
2 1
1 2
3 2
2 2 2
4 1
1 1 2 2
Output
YES
YES
NO
NO
YES
YES
NO
YES
YES
YES
Note

In the first test case, you can jump from the first tile to the last tile;

The second test case is explained in the problem statement.

D. Prefix Permutation Sums
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Your friends have an array of $$$n$$$ elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.

A permutation of $$$n$$$ elements is an array of $$$n$$$ numbers from $$$1$$$ to $$$n$$$ such that each number occurs exactly one times in it.

The array of prefix sums of the array $$$a$$$ — is such an array $$$b$$$ that $$$b_i = \sum_{j=1}^i a_j, 1 \le i \le n$$$.

For example, the original permutation was $$$[1, 5, 2, 4, 3]$$$. Its array of prefix sums — $$$[1, 6, 8, 12, 15]$$$. Having lost one element, you can get, for example, arrays $$$[6, 8, 12, 15]$$$ or $$$[1, 6, 8, 15]$$$.

It can also be shown that the array $$$[1, 2, 100]$$$ does not correspond to any permutation.

Input

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

The first line of the description of each test case contains a positive number $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the size of the initial array.

The second line of the description of each test case contains $$$n - 1$$$ positive number $$$a_i$$$ ($$$1 \le a_i \le 10^{18}$$$), $$$a_{i-1} < a_i$$$ — elements of the array of prefix sums.

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 "YES" if such a permutation exists, and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Example
Input
12
5
6 8 12 15
5
1 6 8 15
4
1 2 100
4
1 3 6
2
2
3
1 2
4
3 7 10
5
5 44 46 50
4
1 9 10
5
13 21 36 42
5
1 2 3 1000000000000000000
9
9 11 12 20 25 28 30 33
Output
YES
YES
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO
Note

In the fourth example, for example, the permutation $$$[1, 2, 3, 4]$$$ is suitable. In the fifth example, for example, the permutation $$$[1, 2]$$$ is suitable. In the seventh example, for example, the permutation $$$[1, 2, 4, 3]$$$ is suitable.

E. Nastya and Potions
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alchemist Nastya loves mixing potions. There are a total of $$$n$$$ types of potions, and one potion of type $$$i$$$ can be bought for $$$c_i$$$ coins.

Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.

As an experienced alchemist, Nastya has an unlimited supply of $$$k$$$ types of potions $$$p_1, p_2, \dots, p_k$$$, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each $$$1 \le i \le n$$$, the minimum number of coins she needs to spend to obtain a potion of type $$$i$$$ next.

Input

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

Each test case is described as follows:

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2 \cdot 10^5$$$) — the total number of potion types and the number of potion types Nastya already has.

The second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^9$$$) — the costs of buying the potions.

The third line contains $$$k$$$ distinct integers $$$p_1, p_2, \dots, p_k$$$ ($$$1 \le p_i \le n$$$) — the indices of potions Nastya already has an unlimited supply of.

This is followed by $$$n$$$ lines describing ways to obtain potions by mixing.

Each line starts with the integer $$$m_i$$$ ($$$0 \le m_i < n$$$) — the number of potions required to mix a potion of the type $$$i$$$ ($$$1 \le i \le n$$$).

Then line contains $$$m_i$$$ distinct integers $$$e_1, e_2, \dots, e_{m_i}$$$ ($$$1 \le e_j \le n$$$, $$$e_j \ne i$$$) — the indices of potions needed to mix a potion of the type $$$i$$$. If this list is empty, then a potion of the type $$$i$$$ can only be bought.

It is guaranteed that no potion can be obtained from itself through one or more mixing processes.

It is guaranteed that the sum of all $$$n$$$ values across all test cases does not exceed $$$2 \cdot 10^5$$$. Similarly, it is guaranteed that the sum of all $$$m_i$$$ values across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output $$$n$$$ integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.

Examples
Input
4
5 1
30 8 3 5 10
3
3 2 4 5
0
0
2 3 5
0
3 2
5 143 3
1 3
1 2
0
2 1 2
5 1
5 4 1 3 4
2
2 4 5
3 3 5 4
2 1 4
1 5
0
4 2
1 1 5 4
2 4
3 2 4 3
0
2 2 4
1 2
Output
23 8 0 5 10 
0 143 0 
5 0 1 3 4 
0 0 0 0 
Input
3
6 3
5 5 4 5 2 2
3 4 5
2 2 5
1 5
3 4 1 6
4 2 6 1 5
0
0
6 2
1 4 4 1 5 2
3 6
4 6 3 4 5
4 6 5 3 4
0
1 5
1 6
0
2 1
4 3
1
0
1 1
Output
0 0 0 0 0 2 
0 0 0 0 0 0 
0 0 
Note

In the first test case of the first sample, it is optimal:

  • Get a potion of the first type by buying and mixing $$$2$$$, $$$3$$$ and $$$4$$$;
  • a potion of the second type can only be obtained by purchasing it;
  • Nastya already has an unlimited number of potions of the third type;
  • a potion of the fourth type is more profitable to buy than to buy and mix other potions;
  • a potion of the fifth type can only be obtained by purchasing it.

F. Lisa and the Martians
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than $$$2^k$$$, for example, when $$$k = 12$$$, the numbers $$$51$$$, $$$1960$$$, $$$0$$$ are martian, and the numbers $$$\pi$$$, $$$-1$$$, $$$\frac{21}{8}$$$, $$$4096$$$ are not.

The aliens will give Lisa $$$n$$$ martian numbers $$$a_1, a_2, \ldots, a_n$$$. Then they will ask her to name any martian number $$$x$$$. After that, Lisa will select a pair of numbers $$$a_i, a_j$$$ ($$$i \neq j$$$) in the given sequence and count $$$(a_i \oplus x) \& (a_j \oplus x)$$$. The operation $$$\oplus$$$ means Bitwise exclusive OR, the operation $$$\&$$$ means Bitwise And. For example, $$$(5 \oplus 17) \& (23 \oplus 17) = (00101_2 \oplus 10001_2) \& (10111_2 \oplus 10001_2) = 10100_2 \& 00110_2 = 00100_2 = 4$$$.

Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such $$$i, j, x$$$ that maximize the calculated value.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of testcases.

Each testcase is described by two lines.

The first line contains integers $$$n, k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 30$$$) — the length of the sequence of martian numbers and the value of $$$k$$$.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^k$$$) — a sequence of martian numbers.

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

Output

For each testcase, print three integers $$$i, j, x$$$ ($$$1 \le i, j \le n$$$, $$$i \neq j$$$, $$$0 \le x < 2^k$$$). The value of $$$(a_i \oplus x) \& (a_j \oplus x)$$$ should be the maximum possible.

If there are several solutions, you can print any one.

Example
Input
10
5 4
3 9 1 4 13
3 1
1 0 1
6 12
144 1580 1024 100 9 13
4 3
7 3 0 4
3 2
0 0 1
2 4
12 2
9 4
6 14 9 4 4 4 5 10 2
2 1
1 0
2 4
11 4
9 4
2 11 10 1 6 9 11 0 5
Output
1 3 14
1 3 0
5 6 4082
2 3 7
1 2 3
1 2 15
4 5 11
1 2 0
1 2 0
2 7 4
Note

First testcase: $$$(3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13$$$.

Second testcase: $$$(1 \oplus 0) \& (1 \oplus 0) = 1$$$.

Third testcase: $$$(9 \oplus 4082) \& (13 \oplus 4082) = 4091$$$.

Fourth testcase: $$$(3 \oplus 7) \& (0 \oplus 7) = 4$$$.

G. Vlad and the Mountains
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad decided to go on a trip to the mountains. He plans to move between $$$n$$$ mountains, some of which are connected by roads. The $$$i$$$-th mountain has a height of $$$h_i$$$.

If there is a road between mountains $$$i$$$ and $$$j$$$, Vlad can move from mountain $$$i$$$ to mountain $$$j$$$ by spending $$$h_j - h_i$$$ units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain $$$i$$$ to mountain $$$j$$$. Note that $$$h_j - h_i$$$ can be negative and then the energy will be restored.

Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain $$$a$$$ and ending at mountain $$$b$$$, given that he initially has $$$e$$$ units of energy?

Input

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

The descriptions of the test cases follow.

The first line of each test case contains two numbers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)$$$) — the number of mountains and the number of roads between them, respectively.

The second line contains $$$n$$$ integers $$$h_1, h_2, h_3, \dots, h_n$$$ ($$$1 \le h_i \le 10^9$$$) — the heights of the mountains.

The next $$$m$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.

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

The following $$$q$$$ lines contain three numbers $$$a$$$, $$$b$$$, and $$$e$$$ ($$$1 \le a, b \le n$$$, $$$0 \le e \le 10^9$$$) — the initial and final mountains of the route, and the amount of energy, respectively.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. The same guarantee applies to $$$m$$$ and $$$q$$$.

Output

For each query, output "YES" if Vlad can construct a route from mountain $$$a$$$ to mountain $$$b$$$, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.

Examples
Input
2
7 7
1 5 3 4 2 4 1
1 4
4 3
3 6
3 2
2 5
5 6
5 7
5
1 1 3
6 2 0
4 7 0
1 7 4
1 7 2
6 5
4 7 6 2 5 1
1 3
5 3
1 5
2 4
6 2
5
1 5 1
1 3 1
1 2 1000
6 2 6
6 2 5
Output
YES
NO
YES
YES
NO

YES
NO
NO
YES
NO
Input
2
3 2
1 3 9
1 2
2 3
5
1 1 1
3 2 2
1 1 2
3 3 0
1 2 1
3 3
1 4 1
1 2
2 3
1 3
5
3 3 9
1 3 6
1 1 2
3 3 6
3 3 4
Output
YES
YES
YES
YES
NO

YES
YES
YES
YES
YES
Input
1
6 10
7 9 2 10 8 6
4 2
6 1
4 5
3 5
6 4
1 3
2 6
6 5
1 2
3 6
5
4 4 8
3 3 1
5 5 9
2 1 7
6 6 10
Output
YES
YES
YES
YES
YES