Codeforces Round 1076 (Div. 3)
A. DBMB and the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DBMB had a birthday yesterday. He was gifted an array $$$a$$$ of $$$n$$$ elements and a number $$$x$$$. But there is one problem: he only likes arrays where the sum of the elements equals $$$s$$$. To make the array appealing to him, you can perform the following operation any number of times:

  • Choose an index $$$i$$$ ($$$1 \le i \le n$$$) and add $$$x$$$ to the number $$$a_i$$$.
For example, if he was given the array $$$[1, 2, 3, 5]$$$ and $$$x = 2$$$, you can choose index $$$3$$$ and get the array $$$[1, 2, 5, 5]$$$. Your task is to determine whether the array can appeal to DBMB after any number of operations.
Input

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

The first line of each test case contains three integers $$$n$$$, $$$s$$$, $$$x$$$ ($$$1 \le n, x \le 10$$$, $$$1 \le s \le 100$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots a_n$$$ ($$$1 \le a_i \le 10$$$) — the elements of the array gifted to DBMB.

Output

For each test case, output "YES" if the array can appeal to DBMB. Otherwise, output "NO".

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

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

In the second test case, $$$a = [1, 2, 3]$$$, applying the operation on $$$a_2$$$ gives us $$$a = [1, 4, 3]$$$. The sum of the array equals $$$s$$$.

B. Reverse a Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation of length $$$n$$$ 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]$$$ and $$$[1,3,4]$$$ are not permutations.

You are given a permutation $$$p$$$ of length $$$n$$$. You can perform the following operation exactly once:

  • Choose two integers $$$l,$$$ $$$r$$$ ($$$1\le l\le r\le n$$$).
  • Reverse the segment $$$[l, r]$$$ in the permutation $$$p$$$.
Your task is to output the lexicographically maximum permutation that can be obtained by performing this operation. A permutation $$$a$$$ is lexicographically greater than a permutation $$$b$$$ if for the first position $$$i$$$ where they differ, it holds that $$$a_i\gt b_i$$$.
Input

Each test consists of several test cases. The first line 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 the number $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$).

The second line of each test case contains $$$n$$$ distinct integers $$$p_1, p_2,...,p_n$$$ $$$(1\le p_i\le n)$$$.

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

Output

For each test case, output the lexicographically maximum permutation that can be obtained with one operation.

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

For the first test case, the best segment is $$$[1, 4]$$$. After reversing, $$$a = [4, 1, 2, 3]$$$. For the second test case, the best segment is $$$[2, 3]$$$. After reversing, $$$a = [3, 2, 1]$$$.

C. Replace and Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, KQ has an exam at the Grail Academy. A strict teacher gave a task that KQ could not solve. He was given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. KQ is allowed to perform the following operations on the arrays:

  • Choose an index $$$i$$$ ($$$1\le i\lt n$$$) and replace $$$a_i$$$ with $$$a_{i+1}$$$.
  • Choose an index $$$i$$$ ($$$1\le i\le n$$$) and replace $$$a_i$$$ with $$$b_i$$$.
Now he has $$$q$$$ queries. Each query is described by two numbers $$$l$$$ and $$$r$$$. His task is to find the maximum value of the sum $$$(a_l + a_{l+1} + a_{l+2} + \dots + a_r)$$$ for each query, if he can perform any number of operations on any elements of the array. Since he is not skilled enough for this, he asks for your help.
Input

Each test consists of several test cases. The first line contains one 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 two integers $$$n$$$, $$$q$$$ $$$(1\le n, q\le 2 \cdot 10^5)$$$.

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

The third line of each test case contains $$$n$$$ integers $$$b_1, b_2,...,b_n$$$ $$$(1\le b_i\le 10^4)$$$.

The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ $$$(1\le l\le r\le n)$$$.

It is guaranteed that the sum of the values of $$$n$$$ and the sum of the values of $$$q$$$ across all test cases do not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output $$$q$$$ numbers separated by spaces — the maximum values of the sums $$$(a_l + a_{l+1} + a_{l+2} + \dots + a_r)$$$.

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

Consider the first test case. Replace $$$a_3$$$ with $$$b_3$$$, $$$a = [3, 2, 3]$$$. Replace $$$a_2$$$ with $$$a_3$$$, $$$a = [3, 3, 3]$$$. The sum $$$a_1 + a_2 + a_3 = 9$$$.

D. Monster Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have been gifted a new game called "Elatyh". In the game, you are given $$$n$$$ swords, each with its own strength. In particular, the sword numbered $$$i$$$ has a strength of $$$a_i$$$. The game consists of $$$n$$$ levels, each of which features a monster.

You start at level $$$1$$$ and progress further. To pass level $$$i$$$ and move on to level $$$i + 1$$$, you need to defeat the monster at level $$$i$$$. To defeat the monster at level $$$i$$$, you need to deal it $$$b_i$$$ sword strikes. The swords in the game are very fragile, so they can only deal one strike before breaking. If you complete level $$$n$$$ or run out of swords, you can finish the game and proceed to score calculation.

Before the game, you are allowed to choose the difficulty level. If you choose difficulty $$$x$$$, swords with a strength less than $$$x$$$ will not affect the monsters. The game score in this case is equal to $$$x$$$ multiplied by the number of levels completed. Your task is to choose the game difficulty in such a way as to maximize the game score.

Input

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

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

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 third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ $$$(1\le b_i\le n)$$$.

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

Output

For each test case, output a single integer — the maximum game score.

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

Consider the first test case. Optimal difficulty to choose is $$$3$$$. If difficulty is $$$3$$$, you can deal strikes with swords number $$$2$$$ and $$$3$$$. With $$$2$$$ swords you can complete $$$1$$$ level, so the game score is $$$3\cdot 1 = 3$$$.

E. Product Queries
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, Sabyrzhan was called to the board with an array $$$a$$$ of length $$$n$$$ and was assigned an officer's task — to answer $$$n$$$ questions.

In the $$$i$$$-th question, it is required to determine the minimum number of elements from the array that need to be selected from the board (it is allowed to use the same element multiple times) so that their product is exactly equal to $$$i$$$, or to report that it is impossible to achieve such a product.

Note that at least one element must be selected.

Input

Each test consists of several test cases. The first line contains one 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 3 \cdot 10 ^ 5$$$).

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

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

Output

For the $$$i$$$-th question, output one integer — the minimum number of elements from the array required to obtain a product equal to $$$i$$$, or $$$−1$$$ if it is impossible to achieve such a product.

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

Consider the first test case. The products can be obtained as follows:

  • $$$1$$$ cannot be obtained.
  • $$$2$$$ can be obtained by selecting $$$a_2$$$.
  • $$$3$$$ can be obtained by selecting $$$a_1$$$.
  • $$$4$$$ can be obtained by selecting $$$a_2$$$ twice.
  • $$$5$$$ cannot be obtained.
  • $$$6$$$ can be obtained by selecting $$$a_7$$$.
  • $$$7$$$ can be obtained by selecting $$$a_5$$$.
  • $$$8$$$ can be obtained by selecting $$$a_2$$$ three times.

F. Pizza Delivery
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Courier YF has earned the title of the best pizza delivery person GR. The manager does not like him, so he decided to give him a very difficult task. The manager provided him with $$$n$$$ coordinates of houses $$$(x_i, y_i)$$$ where he needs to deliver the pizza. He will deliver the pizza in the following way:

  • GR pizza is prepared at the point ($$$Ax, Ay$$$) and YF starts the delivery from this point.
  • To deliver the pizza, he can move from the point ($$$x, y$$$) to three points ($$$x+1, y$$$), ($$$x, y+1$$$), ($$$x, y-1$$$).
  • After he has delivered all the pizzas, he returns home to the point ($$$Bx, By$$$).

Each move takes him exactly one second, and handing over the pizza to the customer takes $$$0$$$ seconds. The manager wants the delivery to be as fast as possible. You need to find the minimum delivery time for all GR pizzas. It is guaranteed that delivery is always possible.

Input

Each test consists of several test cases. The first line contains one 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 five integers $$$n$$$, $$$Ax$$$, $$$Ay$$$, $$$Bx$$$, $$$By$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le Ax, Ay, Bx, By \le 10^9$$$) — the number of houses for delivery, as well as the coordinates of the start and end points.

The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$Ax \lt x_i \lt Bx$$$).

The third line of each test case contains $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$).

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

Output

For each test case, output a single integer on a separate line — the minimum time required for pizza delivery.

Example
Input
4
1 2 3 5 2
4
4
3 1 3 5 2
3 4 3
5 4 1
6 1 2 7 3
5 2 3 5 5 3
6 4 3 1 4 1
5 6 9 8 6
7 7 7 7 7
3 1 8 8 3
Output
6
13
19
15
Note

Consider the second test case:

  • Move from the point ($$$Ax, Ay$$$) to the point ($$$x_3, y_3$$$) in $$$4$$$ seconds.
  • Move from the point ($$$x_3, y_3$$$) to the point ($$$x_1, y_1$$$) in $$$4$$$ seconds.
  • Move from the point ($$$x_1, y_1$$$) to the point ($$$x_2, y_2$$$) in $$$2$$$ seconds.
  • Move from the point ($$$x_2, y_2$$$) to the point ($$$Bx, By$$$) in $$$3$$$ seconds.
In total, the delivery takes $$$4 + 4 + 2 + 3 = 13$$$ seconds. It can be proven that it is impossible to deliver the pizza faster.

G. Paths in a Tree
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

In this interactive problem, you are given an acyclic, connected, undirected graph consisting of $$$n$$$ vertices. We define a path between two vertices $$$v$$$ and $$$u$$$ as a sequence of distinct vertices $$$p_1, p_2,\dots p_k$$$ such that $$$p_1 = v$$$, $$$p_k = u$$$, and for all $$$i$$$ ($$$1 \le i \lt k$$$), there exists an edge between vertices $$$p_i$$$ and $$$p_{i+1}$$$.

There are hidden vertices $$$x$$$ and $$$y$$$ (they may coincide). You can make the following queries:

  • Choose two vertices $$$a$$$, $$$b$$$ ($$$1\le a, b\le n$$$). The jury will respond with $$$1$$$ if the path between vertices $$$x$$$, $$$y$$$ and the path between vertices $$$a$$$, $$$b$$$ share at least one common vertex, and will respond with $$$0$$$ otherwise.
Your task is to find at least one vertex on the path between $$$x$$$ and $$$y$$$ in no more than $$$\lfloor\frac{n}{2}\rfloor + 1$$$ queries.

Note that the interactor is adaptive, which means that the hidden vertices may change depending on your queries, but will not contradict previous queries.

Input

Each test consists of several test cases. The first line contains one integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The following lines describe the test cases.

The first line of each test case contains one integer $$$n$$$ ($$$2\le n\le 2\cdot 10^5$$$) — the number of vertices in the graph.

Next, there are $$$n - 1$$$ lines, each containing two integers $$$v$$$, $$$u$$$ ($$$1\le v, u\le n$$$), indicating that vertices $$$v$$$ and $$$u$$$ are connected by an edge in the graph.

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

Interaction

To find any vertex on the path, you can use no more than $$$\lfloor\frac{n}{2}\rfloor + 1$$$ queries. For this, use queries of the form "? $$$a$$$ $$$b$$$".

After each query, read one number, either $$$0$$$ or $$$1$$$ — the response to the query.

When you find one of the required vertices, output one line in the following format: "! $$$v$$$" ($$$1\le v\le n$$$), where $$$v$$$ is the vertex you found.

If your program makes more than $$$\lfloor\frac{n}{2}\rfloor + 1$$$ queries for one test case, the response to the query will be $$$-1$$$, and after receiving such a response, your program should terminate immediately to receive a verdict of Incorrect answer. Otherwise, it may receive any other verdict.

After outputting a query, do not forget to output a newline and flush the output buffer. Otherwise, you will receive a verdict of Solution "timed out". To flush the buffer, use:

  • $$$\tt{fflush(stdout)}$$$ or $$$\tt{cout.flush()}$$$ in C++;
  • $$$\tt{System.out.flush()}$$$ in Java;
  • $$$\tt{flush(output)}$$$ in Pascal;
  • $$$\tt{stdout.flush()}$$$ in Python;
  • refer to the documentation for other languages.
Example
Input
3

2
1 2

1


3
1 2
1 3

0

0


4
1 2
2 3
2 4

0

1

Output
? 1 1

! 1


? 1 1

? 2 2

! 3


? 1 3

? 4 4

! 4

H. Remove the Grail Tree
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Great Grail Tree has stood in the kingdom for $$$315$$$ years. It takes up a lot of space, so King Ila decided to get rid of it as soon as possible. The tree itself is an acyclic, connected, undirected graph with $$$n$$$ vertices, each having its own value $$$a_v$$$. The tree can be removed in the following way:

  • Let $$$S_v$$$ be the sum of the values of all remaining neighbors of $$$v$$$. If $$$v$$$ has no remaining neighbors, then $$$S_v$$$ is $$$0$$$. Choose a vertex $$$v$$$ such that $$$a_v$$$ and $$$S_v$$$ differ in parity (i.e., either $$$a_v$$$ is even and $$$S_v$$$ is odd, or $$$a_v$$$ is odd and $$$S_v$$$ is even). If there are no such vertices, stop the process.
  • Remove vertex $$$v$$$ and all edges connected to it from the tree.
Your task is to determine whether there exists a sequence of removals that will lead to the complete removal of the Grail Tree (i.e., no vertices will remain in the tree). If such a sequence exists, output the sequence of $$$n$$$ vertices in the order of their removal. If there are multiple answers, output any of them.
Input

Each test consists of several test case. The first line contains one integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line contains the number $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of vertices in the Grail Tree.

The second line describes the array $$$a$$$ ($$$1\le a_i\le 10^9$$$) — the values of the vertices in the tree.

Next, there are $$$n - 1$$$ lines, each containing 2 numbers $$$v$$$ and $$$u$$$ ($$$1\le v, u\le n, v\neq u$$$), indicating that vertices $$$v$$$ and $$$u$$$ are connected by an edge in the tree.

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

Output

For each test case, output "YES" if it is possible to completely remove the Grail Tree. Otherwise, output "NO". If the answer is "YES", output any sequence of removals.

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

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