Codeforces Round 1062 (Div. 4)
A. Square?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$4$$$ sticks of lengths $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. You can not break or bend them.

Determine whether it is possible to form a square$$$^{\text{∗}}$$$ using the given sticks.

$$$^{\text{∗}}$$$A square is defined as a polygon consisting of $$$4$$$ vertices, of which all sides have equal length and all inner angles are equal. No two edges of the polygon may intersect each other.

Input

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

The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$ ($$$1 \le a, b, c, d \le 10$$$) — the lengths of the sticks.

Output

For each test case, print "YES" if it is possible to form a square using the given sticks, and "NO" otherwise.

You may print each letter in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer.

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

In the first test case, we can prove that we can't make a square.

In the second, third, and sixth test cases, we can make a square like this:

B. Your Name
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

khba is writing his girlfriend's name. He has $$$n$$$ cubes, each with one lowercase Latin letter written on it. They are arranged in a row, forming a string $$$s$$$. His girlfriend's name is also a string $$$t$$$, consisting of $$$n$$$ lowercase Latin letters.

To prove his love, he must check whether it is possible to rearrange the letters of string $$$s$$$ so that it becomes her name $$$t$$$.

Input

The first line contains an integer $$$q$$$ ($$$1 \le q \le 1000$$$) — the number of test cases.

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 20$$$).

The second line of each test case contains two distinct strings $$$s$$$ and $$$t$$$, each consisting of $$$n$$$ lowercase Latin letters.

Output

For each test case, output "YES" if the letters of $$$s$$$ can be arranged to form $$$t$$$; 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
7
humitsa mitsuha
4
orhi hori
6
aakima makima
6
nezuqo nezuko
6
misaka mikasa
Output
YES
YES
NO
NO
YES
Note

In the first example, the initial string is "humitsa", and the following operations can be performed:

  • swap the first and third characters, resulting in "muhitsa"
  • swap the second and fourth characters, resulting in "mihutsa"
  • swap the third and fifth characters, resulting in "mithusa"
  • swap the fourth and sixth characters, resulting in "mitsuha"

In the second example, the initial string is "orhi", and the following operations can be performed:

  • swap the second and third characters, resulting in "ohri"
  • swap the first and second characters, resulting in "hori"

C. Isamatdin and His Magic Wand!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Isamatdin has $$$n$$$ toys arranged in a row. The $$$i$$$-th toy has an integer $$$a_i$$$. He wanted to sort them because otherwise, his mother would scold him.

However, Isamatdin never liked arranging toys in order, so his friend JahonaliX gave him a magic wand to help. Unfortunately, JahonaliX made a small mistake while creating the wand.

But Isamatdin couldn't wait any longer and decided to use the broken wand anyway. The wand can only swap two toys if their integers have different parity (one is even, the other is odd). In other words, you can swap toys in positions $$$(i, j)$$$ only if $$$a_i \bmod 2 \neq a_j \bmod 2$$$, where $$$\bmod$$$ — is the remainder of integer division.

Now he wants to know the lexicographically smallest$$$^{\text{∗}}$$$ arrangement he can achieve using this broken wand.

$$$^{\text{∗}}$$$A sequence $$$p$$$ is lexicographically smaller than a sequence $$$q$$$ if there exists an index $$$i$$$ such that $$$p_j = q_j$$$ for all $$$j \lt i$$$, and $$$p_i \lt q_i$$$.

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 a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of toys.

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

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 $$$n$$$ integers — the lexicographically smallest sequence that can be obtained using the described operation.

Example
Input
7
4
2 3 1 4
5
3 2 1 3 4
4
3 7 5 1
2
1000000000 2
3
1 3 5
5
2 5 3 1 7
4
2 4 8 6
Output
1 2 3 4 
1 2 3 3 4 
3 7 5 1 
1000000000 2 
1 3 5 
1 2 3 5 7 
2 4 8 6 
Note

In the first test case, we can swap positions $$$(1, 3)$$$ and then $$$(2, 3)$$$.

In the second test case, we can swap positions $$$(1, 2)$$$, $$$(1, 3)$$$, and then $$$(2, 3)$$$.

In the third and fourth test cases, we can't swap any positions because all toy integers have the same parity.

D. Yet Another Array Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer $$$n$$$ and an array $$$a$$$ of length $$$n$$$.

Find the smallest integer $$$x$$$ ($$$2 \le x \le 10^{18}$$$) such that there exists an index $$$i$$$ ($$$1 \le i \le n$$$) with $$$\gcd$$$$$$^{\text{∗}}$$$$$$(a_i, x) = 1$$$. If no such $$$x$$$ exists within the range $$$[2,10^{18}]$$$, output $$$-1$$$.

$$$^{\text{∗}}$$$$$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

Input

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

Each of the following $$$t$$$ test cases consists of two lines:

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the length of the array.

The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$).

It is guaranteed that the total sum of $$$n$$$ across all test cases does not exceed $$$10^{5}$$$.

Output

For each test case, output a single integer: the smallest $$$x$$$ ($$$2 \le x \le 10^{18}$$$) such that there exists an index $$$i$$$ with $$$\gcd(a_i, x) = 1$$$. If there is no such $$$x$$$ in the range $$$[2,10^{18}]$$$, print $$$-1$$$.

Example
Input
4
1
1
4
6 6 12 12
3
24 120 210
4
2 4 6 10
Output
2
5
5
3
Note

In the first test case, $$$\gcd(2,1)=1$$$, which is the smallest number satisfying the condition.

In the second test case:

  • $$$\gcd(2,6)=2$$$, $$$\gcd(2,12)=2$$$, so $$$2$$$ cannot be the answer.
  • $$$\gcd(3,6)=3$$$, $$$\gcd(3,12)=3$$$, so $$$3$$$ cannot be the answer.
  • $$$\gcd(4,6)=2$$$, $$$\gcd(4,12)=4$$$, so $$$4$$$ cannot be the answer.
  • $$$\gcd(5,6)=1$$$, so the answer is $$$5$$$.

In the third test case:

  • $$$\gcd(2,24)=2$$$, $$$\gcd(2,120)=2$$$, $$$\gcd(2,210)=2$$$, so $$$2$$$ cannot be the answer.
  • $$$\gcd(3,24)=3$$$, $$$\gcd(3,120)=3$$$, $$$\gcd(3,210)=3$$$, so $$$3$$$ cannot be the answer.
  • $$$\gcd(4,24)=4$$$, $$$\gcd(4,120)=4$$$, $$$\gcd(4,210)=2$$$, so $$$4$$$ cannot be the answer.
  • $$$\gcd(5,24)=1$$$, so the answer is $$$5$$$.

In the fourth test case:

  • $$$\gcd(2,2)=2$$$, $$$\gcd(2,4)=2$$$, $$$\gcd(2,6)=2$$$, $$$\gcd(2,10)=2$$$, so $$$2$$$ cannot be the answer.
  • $$$\gcd(3,2)=1$$$, so the answer is $$$3$$$.

E. khba Loves to Sleep!
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

khba has $$$n$$$ friends, each standing on a line at position $$$a_i$$$, and each of them is in the range $$$[0, x]$$$.

They all want to come to him. One of his friends, Isamatdin, gave him $$$k$$$ teleports. Each friend will walk to the nearest teleport (choosing the shortest distance). Once a friend reaches a teleport, khba and the friend can instantly meet.

But khba is so tired that he'll be sleeping while his friends are walking toward him. Now he wants to choose $$$k$$$ teleport positions so that their positions are distinct and lie within the range $$$[0, x]$$$, in order to maximize the time it takes for the first friend who reaches a teleport to reach it. Assume that friends move at the same speed.

Since khba isn't good at calculations, you should output the $$$k$$$ chosen teleport positions.

Input

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

The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$x$$$ ($$$1 \leq n, k \leq 2 \cdot 10^5$$$, $$$k - 1 \leq x \leq 10^9$$$) — the number of friends, the number of teleports, and the range of possible positions for the teleports.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq x$$$) — the positions of khba's friends.

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

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

Output

For each test case, output a single line containing $$$k$$$ integers — the $$$k$$$ chosen teleport positions. The positions must be distinct and lie within the range $$$[0, x]$$$. The positions may be output in any order.

If there are multiple optimal choices, output any of them.

Example
Input
10
4 1 4
1 0 2 4
5 5 4
0 1 2 3 4
2 1 4
4 0
3 4 6
2 4 3
3 2 12
6 12 0
4 3 12
8 12 0 4
1 1 1000000000
0
1 1 1000000000
1000000000
3 4 9
8 7 9
3 4 9
2 0 1
Output
3 
0 1 2 3 4 
2 
0 1 5 6 
3 9 
2 6 10 
1000000000 
0 
0 1 2 3 
6 7 8 9 
Note

Sample 1. Friends at positions $$$[1,0,2,4]$$$. Chosen teleport position: $$$[3]$$$.

Nearest teleport for each friend is in $$$3$$$, minimal times for friends are $$$[2, 3, 1, 1]$$$, respectively.

Sample 2. Friends at positions $$$[0,1,2,3,4]$$$. Chosen teleport positions: $$$[0,1,2,3,4]$$$.

Minimal times for friends are $$$[0, 0, 0, 0, 0]$$$, respectively.

Sample 3. Friends at positions $$$[4,0]$$$. Chosen teleport position: $$$[2]$$$.

Minimal times for friends are $$$[2, 2]$$$, respectively.

Sample 4. Friends at positions $$$[2,4,3]$$$. Chosen teleport positions: $$$[0,1,5,6]$$$.

Minimal times for friends are $$$[1, 1, 2]$$$, respectively.

Sample 5. Friends at positions $$$[6,12,0]$$$. Chosen teleport positions: $$$[3,9]$$$.

Minimal times for friends are $$$[3, 3, 3]$$$, respectively.

F. Tree, TREE!!!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Roots change, but the tree stands strong — so should your logic.

Behruzbek received a tree$$$^{\text{∗}}$$$ with $$$n$$$ nodes. For a chosen root$$$^{\text{†}}$$$ $$$r$$$, Behruzbek wants to find cuteness of the tree.

Consider every set of $$$k$$$ distinct nodes of the tree. For each such set, compute its lowest common ancestor (LCA) in the tree when it is rooted at $$$r$$$. Let $$$S_r$$$ be the set of all distinct nodes obtained this way; then cuteness of the tree is $$$|S_r|$$$, where $$$|S|$$$ means the number of distinct elements.

After discovering the cuteness of trees, Behruzbek became interested in finding the kawaiiness of the tree! Kawaiiness is defined as:

$$$$$$ \sum_{r = 1}^{n} |S_r| = |S_1| + |S_2| + \dots + |S_n| $$$$$$

Unfortunately, Behruzbek is feeling sleepy now. Please help Behruzbek by finding the kawaiiness of the tree!

$$$^{\text{∗}}$$$A tree is a connected graph without cycles.

$$$^{\text{†}}$$$A rooted tree is a tree where one vertex is special and called the root.

Input

The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^{4}$$$).

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq k \leq n \leq 2\cdot 10^{5}$$$) — the number of vertices in the tree and the number of distinct integers to be chosen.

The following $$$n-1$$$ lines of each test case describe the tree. Each of the lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u,v \leq n$$$, $$$u \ne v$$$) that indicate an edge between vertex $$$u$$$ and $$$v$$$. It is guaranteed that these 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 one integer — the value of $$$\sum\limits_{r=1}^n |S_r|$$$.

Example
Input
4
2 2
1 2
5 3
1 2
1 3
1 4
1 5
6 3
1 2
1 3
2 4
2 5
3 6
10 5
5 6
4 9
3 9
2 6
2 8
8 9
6 10
1 6
4 7
Output
2
9
17
35
Note

Let $$$f(i) = |S_i|$$$

For the third example:

  • Root is $$$1$$$, only $$$1$$$ and $$$2$$$ nodes can be obtained. For example, we can choose: $$$LCA(4, 5, 6) = 1$$$ and $$$LCA(2, 4, 5) = 2$$$. As a result, $$$f(1) = 2$$$.

  • Root is $$$2$$$, only $$$1$$$ and $$$2$$$ nodes can be obtained. For example, we can choose: $$$LCA(1, 3, 6) = 1$$$ and $$$LCA(1, 4, 5) = 2$$$. As a result, $$$f(2) = 2$$$.

  • Root is $$$3$$$, $$$f(3) = 3$$$. For example, node $$$3$$$ can be obtained by choosing: $$$LCA(2, 4, 6) = 3$$$.

  • Root is $$$4$$$, $$$f(4) = 3$$$. For example, node $$$2$$$ can be obtained by choosing: $$$LCA(1, 3, 5) = 2$$$.

  • Root is $$$5$$$, $$$f(5) = 3$$$. For example, node $$$2$$$ can be obtained by choosing: $$$LCA(3, 4, 6) = 2$$$.

  • Root is $$$6$$$, $$$f(6) = 4$$$. For example, node $$$3$$$ can be obtained by choosing: $$$LCA(3, 4, 5) = 2$$$.

Overall, $$$2 + 2 + 3 + 3 + 3 + 4 = 17$$$.

G. Mukhammadali and the Smooth Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Muhammadali has an integer array $$$a_1,\dots,a_n$$$. He can change (replace) any subset of positions; changing position $$$i$$$ costs $$$c_i$$$ and replaces $$$a_i$$$ with any integer of his choice. The positions that he does not change must retain their original values.

After all changes, we call an index $$$i$$$ ($$$1 \le i \lt n$$$) a drop if the final value at position $$$i$$$ is strictly greater than the final value at position $$$i+1$$$. Muhammadali wants the final array to contain no drops.

Find the minimum cost of changes required to ensure that there are no drops in the array.

Input

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

Each test case consists of three lines:

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 8000$$$) — the length of the arrays.

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

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

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$8000$$$.

Output

For each test case, output a single integer — the minimum possible total cost required to eliminate all drops.

Example
Input
10
1
10
5
4
1 2 2 3
5 6 7 8
4
4 3 2 1
1 1 1 1
3
3 1 2
100 1 1
5
5 5 5 5 5
10 1 10 1 10
5
1 3 2 2 4
100 1 1 1 100
6
10 9 8 7 6 5
1 100 1 100 1 100
5
100 1 100 100 100
1 100 1 1 1
4
2 1 2 1
5 4 3 2
7
1 5 3 4 2 6 7
10 1 10 1 10 1 10
Output
0
0
3
2
0
1
203
1
6
11
Note

In the first and second examples, the array already has no drops, so no changes are needed.

In the third example, one of the optimal arrays is: $$$[2,3,5,6]$$$; to achieve this, all elements except the second need to be replaced, so the answer is $$$c_1 + c_3 + c_4 = 3$$$.