Question:
Sofia had an array of $n$ integers $a_1, a_2, \ldots, a_n$. One day she got bored with it, so she decided to sequentially apply $m$ modification operations to it.

Each modification operation is described by a pair of numbers $\langle c_j, d_j \rangle$ and means that the element of the array with index $c_j$ should be assigned the value $d_j$, i.e., perform the assignment $a_{c_j} = d_j$. After applying all modification operations sequentially, Sofia discarded the resulting array.

Recently, you found an array of $n$ integers $b_1, b_2, \ldots, b_n$. You are interested in whether this array is Sofia's array. You know the values of the original array, as well as the values $d_1, d_2, \ldots, d_m$. The values $c_1, c_2, \ldots, c_m$ turned out to be lost.

Is there a sequence $c_1, c_2, \ldots, c_m$ such that the sequential application of modification operations $\langle c_1, d_1, \rangle, \langle c_2, d_2, \rangle, \ldots, \langle c_m, d_m \rangle$ to the array $a_1, a_2, \ldots, a_n$ transforms it into the array $b_1, b_2, \ldots, b_n$?

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

Then follow the descriptions of the test cases.

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

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

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) — the elements of the found array.

The fourth line contains an integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of modification operations.

The fifth line contains $m$ integers $d_1, d_2, \ldots, d_m$ ($1 \le d_j \le 10^9$) — the preserved value for each modification operation.

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

Output Format:
Output $t$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if there exists a suitable sequence $c_1, c_2, \ldots, c_m$, 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).

Tags:
['constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
GCD (Greatest Common Divisor) of two integers $x$ and $y$ is the maximum integer $z$ by which both $x$ and $y$ are divisible. For example, $GCD(36, 48) = 12$, $GCD(5, 10) = 5$, and $GCD(7,11) = 1$.

Kristina has an array $a$ consisting of exactly $n$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $b$, called GCD-sequence.

So, the elements of the GCD-sequence $b$ will be calculated using the formula $b_i = GCD(a_i, a_{i + 1})$ for $1 \le i \le n - 1$.

Determine whether it is possible to remove exactly one number from the array $a$ so that the GCD sequence $b$ is non-decreasing (i.e., $b_i \le b_{i+1}$ is always true).

For example, let Khristina had an array $a$ = [$20, 6, 12, 3, 48, 36$]. If she removes $a_4 = 3$ from it and counts the GCD-sequence of $b$, she gets:

  * $b_1 = GCD(20, 6) = 2$    * $b_2 = GCD(6, 12) = 6$    * $b_3 = GCD(12, 48) = 12$    * $b_4 = GCD(48, 36) = 12$

The resulting GCD sequence $b$ = [$2,6,12,12$] is non-decreasing because $b_1 \le b_2 \le b_3 \le b_4$.

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

This is followed by the descriptions of the test cases.

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of elements in the array $a$.

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

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

Output Format:
For each test case, output a single line:

  * "YES" if you can remove exactly one number from the array $a$ so that the GCD-sequence of $b$ is non-decreasing;    * "NO" otherwise.

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

Tags:
['greedy', 'implementation', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$.

A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[100]$, $[1, 2, 4, 5]$ are not.

A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $[[1, 2], [3, 4]]$, $[[1]]$, $[[1, 5, 3], [2, 6, 4]]$ contain permutations, while matrices $[[2]]$, $[[1, 1], [2, 2]]$, $[[1, 2], [100, 200]]$ do not.

You can perform one of the following two actions in one operation:

  * choose columns $c$ and $d$ ($1 \le c, d \le m$, $c \ne d$) and swap these columns;    * choose rows $c$ and $d$ ($1 \le c, d \le n$, $c \ne d$) and swap these rows.

You can perform any number of operations.

You are given the original matrix $a$ and the matrix $b$. Your task is to determine whether it is possible to transform matrix $a$ into matrix $b$ using the given operations.

Input Format:
The first line 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 description contains $2$ integers $n$ and $m$ ($1 \le n, m \le n \cdot m \le 2 \cdot 10^5$) — the sizes of the matrix.

The next $n$ lines contain $m$ integers $a_{ij}$ each ($1 \le a_{ij} \le n \cdot m$). It is guaranteed that matrix $a$ is a permutation.

The next $n$ lines contain $m$ integers $b_{ij}$ each ($1 \le b_{ij} \le n \cdot m$). It is guaranteed that matrix $b$ is a permutation.

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

Output Format:
For each test case, output "YES" if the second matrix can be obtained from the first, and "NO" otherwise.

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.

Tags:
['constructive algorithms', 'data structures', 'greedy', 'hashing', 'implementation', 'math', 'matrices', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
This is an easy version of the problem; it differs from the hard version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.

Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$), the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).

Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:

  * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field.    * Alice's path will divide the field into two parts — one part will belong to Alice (this part includes the cells of her path), the other part — to Bob.    * Alice will own the part that includes the cell ($n, 1$).    * Bob will own the part that includes the cell ($1, m$).

Alice wants to divide the field in such a way as to get as many cells as possible.

Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ — the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot). Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where:

  * $a_i=0$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot does not increase (i.e., remains equal to $\alpha$);    * $a_i=1$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot increases (i.e., becomes greater than $\alpha$).

Input Format:
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 three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) — the field sizes and the number of fountains, respectively.

Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) — the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).

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

Output Format:
For each test case, first output $\alpha$ — the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where:

  * $a_i=0$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot does not increase compared to the case when all $k$ fountains belong to Bob;    * $a_i=1$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot increases compared to the case when all $k$ fountains belong to Bob.

If you output any other positive number instead of $1$ that fits into a 64-bit signed integer type, it will also be recognized as $1$. Thus, a solution to the hard version of this problem will also pass the tests for the easy version.

Tags:
['data structures', 'math', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.

Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).

Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:

  * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field.    * Alice's path will divide the field into two parts — one part will belong to Alice (this part includes the cells of her path), the other part — to Bob.    * Alice will own the part that includes the cell ($n, 1$).    * Bob will own the part that includes the cell ($1, m$).

Alice wants to divide the field in such a way as to get as many cells as possible.

Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ — the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot).

Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

Input Format:
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 three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) — the field sizes and the number of fountains, respectively.

Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) — the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).

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

Output Format:
For each test case, first output $\alpha$ — the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

Tags:
['math', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Yasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.

Next to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.

The first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of the weight of that edge and the integer $y$.

The second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \le u \le n$, $u \neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.

Then Yasya finds a simple cycle in the resulting graph and calculates the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \to u \to v$.

Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.

Help Yasya answer all the queries.

Input Format:
The first line 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 integers $n$, $m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of queries.

The next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \le v, u \le n$, $1 \le w \le 10^9$) — the ends of some edge in the tree and its weight.

It is guaranteed that the given set of edges forms a tree.

The next $m$ lines of each test case describe the queries:

  * ^ $y$ ($1 \le y \le 10^9$) — parameter of the first type query;    * ? $v$ $x$ ($1 \le v \le n$, $1 \le x \le 10^9$) — parameters of the second type query.

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

Output Format:
For each test case, output the answers to the queries of the second type.

Tags:
['bitmasks', 'data structures', 'dfs and similar', 'graphs', 'greedy', 'strings', 'trees']

Time Limit: 2500 ms
Memory Limit: 512 MB


Question:
Turtle and Piggy are playing a number game.

First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$.

Then, Piggy will keep doing the following operation until $x$ becomes $1$:

  * Choose an integer $p$ such that $p \ge 2$ and $p \mid x$ (i.e. $x$ is a multiple of $p$).    * Set $x$ to $\frac{x}{p}$, and the score will increase by $1$.

The score is initially $0$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.

Input Format:
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 two integers $l, r$ ($1 \le l \le r \le 10^9, 2l \le r$) — The range where Turtle can choose the integer from.

Output Format:
For each test case, output a single integer — the maximum score.

Tags:
['brute force', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$.

After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \mid a_i \mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \mid a_1$. Here, $|$ denotes [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

Turtle is asked to find the value of $a_n$ after $m$ seconds. In particular, if $m = 0$, then he needs to find the initial value of $a_n$. He is tired of calculating so many values, so please help him!

Input Format:
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 two integers $n, m$ ($0 \le n, m \le 10^9$).

Output Format:
For each test case, output a single integer — the value of $a_n$ after $m$ seconds.

Tags:
['bitmasks', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Turtle was playing with a sequence $a_1, a_2, \ldots, a_n$ consisting of positive integers. Unfortunately, some of the integers went missing while playing.

Now the sequence becomes incomplete. There may exist an arbitrary number of indices $i$ such that $a_i$ becomes $-1$. Let the new sequence be $a'$.

Turtle is sad. But Turtle remembers that for every integer $i$ from $1$ to $n - 1$, either $a_i = \left\lfloor\frac{a_{i + 1}}{2}\right\rfloor$ or $a_{i + 1} = \left\lfloor\frac{a_i}{2}\right\rfloor$ holds for the original sequence $a$.

Turtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can't complete the sequence.

Formally, you need to find another sequence $b_1, b_2, \ldots, b_n$ consisting of positive integers such that:

  * For every integer $i$ from $1$ to $n$, if $a'_i \ne -1$, then $b_i = a'_i$.    * For every integer $i$ from $1$ to $n - 1$, either $b_i = \left\lfloor\frac{b_{i + 1}}{2}\right\rfloor$ or $b_{i + 1} = \left\lfloor\frac{b_i}{2}\right\rfloor$ holds.    * For every integer $i$ from $1$ to $n$, $1 \le b_i \le 10^9$.

If there is no sequence $b_1, b_2, \ldots, b_n$ that satisfies all of the conditions above, you need to report $-1$.

Input Format:
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 of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the sequence.

The second line of each test case contains $n$ integers $a'_1, a'_2, \ldots, a'_n$ ($a'_i = -1$ or $1 \le a'_i \le 10^8$) — the elements of the sequence $a'$.

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

Output Format:
For each test case, if there is no sequence $b_1, b_2, \ldots, b_n$ that satisfies all of the conditions, output a single integer $-1$.

Otherwise, output $n$ integers $b_1, b_2, \ldots, b_n$ — the elements of the sequence $b_1, b_2, \ldots, b_n$ you find. The sequence should satisfy that $1 \le b_i \le 10^9$ for every integer $i$ from $1$ to $n$. If there are multiple answers, print any of them.

Tags:
['bitmasks', 'brute force', 'constructive algorithms', 'greedy', 'implementation', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Turtle just learned how to multiply two integers in his math class, and he was very excited.

Then Piggy gave him an integer $n$, and asked him to construct a sequence $a_1, a_2, \ldots, a_n$ consisting of integers which satisfied the following conditions:

  * For all $1 \le i \le n$, $1 \le a_i \le 3 \cdot 10^5$.    * For all $1 \le i < j \le n - 1$, $a_i \cdot a_{i + 1} \ne a_j \cdot a_{j + 1}$.

Of all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements.

Turtle definitely could not solve the problem, so please help him!

Input Format:
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$ ($2 \le n \le 10^6$) — the length of the sequence $a$.

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

Output Format:
For each test case, output $n$ integers $a_1, a_2, \ldots, a_n$ — the elements of the sequence $a$.

If there are multiple answers, print any of them.

Tags:
['constructive algorithms', 'dfs and similar', 'graphs', 'number theory']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$.

Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$.

Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree.

We say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\max(l_1, l_2) \le \min(r_1, r_2)$.

Input Format:
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 of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) — the number of segments.

The $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) — the $i$-th segment and the $i$-th element of the sequence.

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

Output Format:
For each test case, output a single integer — the sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.

Tags:
['data structures', 'dsu', 'graphs', 'greedy']

Time Limit: 5000 ms
Memory Limit: 512 MB


Question:
Note the unusual definition of $\text{MEX}$ in this problem.

Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$.

If a set of paths $P = \\{(x_i, y_i)\\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more.

Turtle defines the value of a set of paths as $\sum\limits_{(x, y) \in P} f(x, y)$, where $f(x, y)$ denotes the $\text{MEX}^{\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$).

Turtle wonders the minimum value over all good sets of paths. Please help him calculate the answer!

$^{\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons.

$^{\ddagger}\text{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\text{MEX}$ of $[2, 3]$ is $1$.

Input Format:
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$ ($2 \le n \le 2.5 \cdot 10^4$) — the number of vertices in the tree.

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

The third line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$) — the parent of each vertex in the tree.

Additional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons.

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

Output Format:
For each test case, output a single integer — the minimum value over all good sets of paths.

Tags:
['data structures', 'dp', 'trees']

Time Limit: 4000 ms
Memory Limit: 1024 MB


Question:
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $x$ : $(y + 1)$.

While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score?

It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence.

Help Dima and answer the question!

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \le x_{1}, y_{1} \le 10^{9}$, $x_{1} \neq y_{1}$) — the score before Dima was distracted.

The second line of each test case contains two integers $x_{2}, y_{2}$ ($x_{1} \le x_{2} \le 10^{9}$, $y_{1} \le y_{2} \le 10^{9}$, $x_{2} \neq y_{2}$) — the score when Dima returned.

Output Format:
For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes.

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

Tags:
['greedy', 'implementation', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture:

There is a variable $x$ and a constant $y$. The following operation is performed $k$ times:

  * increase $x$ by $1$, then    * while the number $x$ is divisible by $y$, divide it by $y$.

Note that both of these actions are performed sequentially within one operation.

For example, if the number $x = 16$, $y = 3$, and $k = 2$, then after one operation $x$ becomes $17$, and after another operation $x$ becomes $2$, because after adding one, $x = 18$ is divisible by $3$ twice.

Given the initial values of $x$, $y$, and $k$, Maxim wants to know what is the final value of $x$.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. Then follows the description of the test cases.

The only line of each test case contains three integers $x$, $y$, and $k$ ($1 \le x, k \le 10^{9}$, $2 \le y \le 10^{9}$) — the initial variable, constant and the number of operations.

Output Format:
For each test case, output a single integer — the number obtained after applying $k$ operations.

Tags:
['brute force', 'implementation', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play.

Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a non-zero number of cards from the top of the deck and finishes the round. If the sum of the numbers on the cards collected during the round is between $l$ and $r$, inclusive, the round is won; otherwise, it is lost.

Egor knows by heart the order of the cards. Help Egor determine the maximum number of rounds he can win in such a game. Note that Egor is not required to win rounds consecutively.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains three integers $n$, $l$, and $r$ ($1 \le n \le 10^{5}$, $1 \le l \le r \le 10^9$).

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the numbers on the cards from top to bottom.

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

Output Format:
For each test case, output a single number — the maximum number of rounds Egor can win.

Tags:
['binary search', 'data structures', 'dp', 'greedy', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Nikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of $n$ rows and $m$ columns, with each cell containing a non-negative integer representing the height of the mountain.

He also noticed that mountains come in two types:

  * With snowy caps.    * Without snowy caps.

Nikita is a very pragmatic person. He wants the sum of the heights of the mountains with snowy caps to be equal to the sum of the heights of the mountains without them. He has arranged with the mayor of Berlyand, Polikarp Polikarpovich, to allow him to transform the landscape.

Nikita can perform transformations on submatrices of size $k \times k$ as follows: he can add an integer constant $c$ to the heights of the mountains within this area, but the type of the mountain remains unchanged. Nikita can choose the constant $c$ independently for each transformation. Note that $c$ can be negative.

Before making the transformations, Nikita asks you to find out if it is possible to achieve equality of the sums, or if it is impossible. It doesn't matter at what cost, even if the mountains turn into canyons and have negative heights.

If only one type of mountain is represented on the map, then the sum of the heights of the other type of mountain is considered to be zero.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. This is followed by a description of test cases.

The first line of each test case contains three integers $n, m, k$ ($1 \le n, m \le 500, 1 \le k \le min(n, m)$).

The next $n$ lines of each test case contain $m$ integers $a_{i j}$ ($0 \le a_{i j} \le 10^{9}$) — the initial heights of the mountains.

The next $n$ binary strings of length $m$ for each test case determine the type of mountain, '$0$' — with snowy caps, '$1$' — without them.

It is guaranteed that the sum of $n \cdot m$ for all test cases does not exceed $250\,000$.

Output Format:
For each test case, output "YES" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['brute force', 'data structures', 'implementation', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$.

A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \le i \le r$ condition $bit(a_{i}) \le k$ is satisfied.

You are given an array $a$ of length $n$, consisting of consecutive non-negative integers starting from $0$, i.e., $a_{i} = i$ for $0 \le i \le n - 1$ (in $0$-based indexing). You need to count the number of $k$-good subarrays in this array.

As the answer can be very large, output it modulo $10^{9} + 7$.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. The following lines describe the test cases.

The single line of each test case contains two integers $n$, $k$ ($1 \le n \le 10^{18}, 1 \le k \le 60$).

Output Format:
For each test case, output a single integer — the number of $k$-good subarrays modulo $10^{9} + 7$.

Tags:
['bitmasks', 'brute force', 'combinatorics', 'divide and conquer', 'dp', 'math', 'meet-in-the-middle']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:

What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted in non-decreasing order?

More formally, you want to select a subarray of the array $(l, r)$ with the minimum value of $r - l + 1$. After that, you will sort the elements $a_{l}, a_{l + 1}, \ldots, a_{r}$ and want the condition $a_{i} \le a_{i + 1}$ to hold for all $1 \le i < n$. If the array is already sorted in non-decreasing order, then $l$ and $r$ should be considered as equal to $-1$.

Note that finding such $(l, r)$ does not change the array in any way. The modifications themselves take the form: assign $a_{pos} = x$ for given $pos$ and $x$.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) — the number of test cases. Then follows the description of test cases.

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

The second line of each test case contains $n$ integers $a_{i}$ ($0 \le |a_{i}| \le 10^{9}$) — the initial elements of the array $a$.

The third line of each test case contains a number $q$ ($0 \le q \le 5 \cdot 10^{5}$) — the number of modifications to the array.

The following $q$ lines of each test case contain two integers $pos_{i}$ ($1 \le pos_{i} \le n$) and $val_{i}$ ($0 \le |val_{i}| \le 10^{9}$) — this means that for the $i$-th modification, $a_{pos_{i}}$ is assigned the value $val_{i}$.

It is guaranteed that the sum of $n$ and the sum of $q$ for all test cases does not exceed $5 \cdot 10^{5}$.

Output Format:
For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ — the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.

Tags:
['binary search', 'data structures', 'sortings']

Time Limit: 2500 ms
Memory Limit: 256 MB


Question:
An array of integers $a_1,a_2,\cdots,a_n$ is beautiful subject to an integer $k$ if it satisfies the following:

  * The sum of $a_{j}$ over all $j$ such that $j$ is a multiple of $k$ and $1 \le j \le n $, itself, is a multiple of $k$.    * More formally, if $\sum_{k | j} a_{j}$ is divisible by $k$ for all $1 \le j \le n$ then the array $a$ is beautiful subject to $k$. Here, the notation ${k|j}$ means $k$ divides $j$, that is, $j$ is a multiple of $k$.

Given $n$, find an array of positive nonzero integers, with each element less than or equal to $10^5$ that is beautiful subject to all $1 \le k \le n$.

It can be shown that an answer always exists.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

The first and only line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array.

Output Format:
For each test case, print the required array as described in the problem statement.

Tags:
['constructive algorithms', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given two grids of numbers $a$ and $b$, with $n$ rows and $m$ columns. All the values in the grid are $0$, $1$ or $2$.

You can perform the following operation on $a$ any number of times:

  * Pick any subrectangle in the grid with length and width $\ge 2$. You are allowed to choose the entire grid as a subrectangle.    * The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $1$ to their values modulo $3$.    * For the pair of corners not picked, add $2$ to their values modulo $3$.

Note that the operation only changes the values of the corners of the picked subrectangle.

Is it possible to convert the grid $a$ into grid $b$ by applying the above operation any number of times (possibly zero)?

Input Format:
The first line contains an integer $t$, the number of testcases ($1 \le t \le 250$).

For each testcase:

The first line contains two integers $n$ and $m$, the number of rows and columns in the grid ($2 \le n,m \le 500$).

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $a_{i,j}$.

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $b_{i,j}$ ($0 \le a_{i,j}, b_{i,j} \le 2$).

It is guaranteed that the sum of $n$ over all test cases and the sum of $m$ over all test cases do not exceed $500$.

Output Format:
For each test case print "YES" (without quotes) if it is possible to convert grid $a$ into grid $b$ and "NO" (without quotes) otherwise.

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.

Tags:
['constructive algorithms', 'greedy', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Alice, Bob and Charlie want to share a rectangular cake cut into $n$ pieces. Each person considers every piece to be worth a different value. The $i$-th piece is considered to be of value $a_i$ by Alice, $b_i$ by Bob and $c_i$ by Charlie.

The sum over all $a_i$, all $b_i$ and all $c_i$ individually is the same, equal to $tot$.

Given the values of each piece of the cake for each person, you need to give each person a contiguous slice of cake. In other words, the indices at the left and right ends of these subarrays (the slices given to each person) can be represented as $(l_a, r_a)$, $(l_b, r_b)$ and $(l_c, r_c)$ respectively for Alice, Bob and Charlie. The division needs to satisfy the following constraints:

  * No piece is assigned to more than one person, i.e., no two subarrays among $[l_a,\ldots,r_a]$, $[l_b, \ldots, r_b]$ and $[l_c, \ldots, r_c]$ intersect.   * $ \sum_{i = l_a}^{r_a} a_i, \sum_{i = l_b}^{r_b} b_i, \sum_{i = l_c}^{r_c} c_i \geq \lceil \frac{tot}{3} \rceil$.

Here, the notation $\lceil \frac{a}{b} \rceil$ represents ceiling division. It is defined as the smallest integer greater than or equal to the exact division of $a$ by $b$. In other words, it rounds up the division result to the nearest integer. For instance $\lceil \frac{10}{3} \rceil = 4$ and $\lceil \frac{15}{3} \rceil = 5$.

Input Format:
The first line contains an integer $t$, the number of testcases, ($1 \le t \le 10^4$)

For each testcase:

The first line contains the integer $n$ ($3 \le n \le 2 \cdot 10^5$).

The following three lines contain $n$ integers each:

One line with $n$ integers $a_1, a_2, \ldots, a_n$ represents the values for Alice ($1 \le a_i \le 10^6$).

The next line with $n$ integers $b_1, b_2, \ldots, b_n$ represents the values for Bob ($1 \le b_i \le 10^6$).

The next line with $n$ integers $c_1, c_2, \ldots, c_n$ represents the values for Charlie ($1 \le c_i \le 10^6$).

It is guaranteed that $ \sum_{i = 1}^{n} a_i = \sum_{i = 1}^{n} b_i = \sum_{i = 1}^{n} c_i$.

The sum of $n$ over all testcases does not exceed $2 \cdot 10^5$.

Output Format:
For each testcase, output $-1$ if the required condition is impossible.

Otherwise, output six numbers – $l_a, r_a, l_b, r_b, l_c, r_c$, the respective starting and ending indices ($1$-indexed) of the subarrays for Alice, Bob and Charlie, respectively.

Tags:
['binary search', 'brute force', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given two arrays of distinct positive integers $a$ and $b$ of length $n$, we would like to make both the arrays the same. Two arrays $x$ and $y$ of length $k$ are said to be the same when for all $1 \le i \le k$, $x_i = y_i$.

Now in one move, you can choose some index $l$ and $r$ in $a$ ($l \le r$) and swap $a_l$ and $a_r$, then choose some $p$ and $q$ ($p \le q$) in $b$ such that $r-l=q-p$ and swap $b_p$ and $b_q$.

Is it possible to make both arrays the same?

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2 \cdot 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 10^5$) — the length of the arrays $a$ and $b$.

The second line of each test case contains $n$ distinct integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — the integers in the array $a$.

The third line of each test case contains $n$ distinct integers $b_1,b_2,b_3,\ldots,b_n$ ($1 \le b_i \le 2 \cdot 10^5$) — the integers in the array $b$.

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

Output Format:
For each testcase, print "YES" if the arrays $a$ and $b$ can be made the same. Otherwise, print "NO". 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.

Tags:
['constructive algorithms', 'data structures', 'divide and conquer', 'greedy', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it.

The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $0$ at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn.

They play this game until no balls are remaining in the game. Alice plays first.

Find the expected score that both the players have at the end of the game modulo $10^9+7$.

Formally, let $M = 10^9+7$. 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 < M$ and $x \cdot q \equiv p \pmod{M}$.

Input Format:
There are multiple test cases. The first line of the input contains an integer $t$, the number of test cases ($1 \le t \le 2 \cdot 10^5$).

Each test case description is on a new line. The first line of the test case contains two integers $n$ and $k$ in the respective order separated by a space ($1 \le k \le n \le 4 \cdot 10^5$).

The second line of the test case contains $n$ integers: $v_1, v_2, \ldots, v_n$, the value for each ball separated by spaces. The first $k$ balls are special ($1 \le v_i \le 10^7$).

The sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $10^9+7$.

Tags:
['combinatorics', 'math', 'probabilities']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You have an array of non-negative integers $a_1, a_2, \ldots, a_n$.

The value of a sub-array of length $\ge 2$, $a[l, r] = [a_l, a_{l+1}, \ldots, a_r]$ is the minimum value of $a_i \oplus a_j$ such that $l \le i < j \le r$, where $\oplus$ is the xor (exclusive-or) operator.

You have to find the $k$-th smallest value over all sub-arrays of length $\ge 2$.

Input Format:
The first line of the input contains multiple test cases $t$ ($1 \le t \le 2 \cdot 10^4$).

The first line of each test case contains integer numbers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le \frac{n\cdot(n-1)}{2}$).

The second line of the input contains $n$ non-negative integer numbers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the array itself.

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

Output Format:
Print the $k$-th smallest value obtained over all subarrays of length at least $2$.

Tags:
['binary search', 'bitmasks', 'data structures', 'greedy', 'two pointers']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
You are given a tree with $n$ nodes numbered from $1$ to $n$, along with an array of size $n$. The value of $i$-th node is $a_{i}$. There are $q$ queries. In each query, you are given 2 nodes numbered as $x$ and $y$.

Consider the path from the node numbered as $x$ to the node numbered as $y$. Let the path be represented by $x = p_0, p_1, p_2, \ldots, p_r = y$, where $p_i$ are the intermediate nodes. Compute the sum of $a_{p_i}\oplus i$ for each $i$ such that $0 \le i \le r$ where $\oplus$ is the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) operator.

More formally, compute $$\sum_{i =0}^{r} a_{p_i}\oplus i$$.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case contains several sets of input data.

The first line of each set of input data contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of nodes.

The next $n-1$ lines of each set of input data contain $2$ integers, $u$ and $v$ representing an edge between the node numbered $u$ and the node numbered $v$. It is guaranteed that $u \ne v$ and that the edges form a tree.

The next line of each set of input data contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 5 \cdot 10^5$) — values of the nodes.

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

The next $q$ lines describe the queries. The $i$-th query contains $2$ integers $x$ and $y$ ($1 \le x,y \le n$) denoting the starting and the ending node of the path.

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

Output Format:
For each query, output a single number — the sum from the problem statement.

Tags:
['bitmasks', 'brute force', 'dp', 'trees']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Define the range of a non-empty array to be the maximum value minus the minimum value. For example, the range of $[1,4,2]$ is $4-1=3$.

You are given an array $a_1, a_2, \ldots, a_n$ of length $n \geq 3$. It is guaranteed $a$ is sorted.

You have to color each element of $a$ red or blue so that:

  * the range of the red elements does not equal the range of the blue elements, and    * there is at least one element of each color.

If there does not exist any such coloring, you should report it. If there are multiple valid colorings, you can print any of them.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.

The first line of each test case contains an integer $n$ ($3 \leq n \leq 50$) — the length of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$). It is guaranteed $a_1 \leq a_2 \leq \ldots \leq a_{n - 1} \leq a_{n}$.

Output Format:
For each test case, if it is impossible to color $a$ to satisfy all the constraints, output $\texttt{NO}$.

Otherwise, first output $\texttt{YES}$.

Then, output a string $s$ of length $n$. For $1 \leq i \leq n$, if you color $a_i$ red, $s_i$ should be $\texttt{R}$. For $1 \leq i \leq n$, if you color $a_i$ blue, $s_i$ should be $\texttt{B}$.

Tags:
['constructive algorithms']

Time Limit: 1000 ms
Memory Limit: 256 MB

{"inputs": ["7\n4\n1 1 2 2\n5\n1 2 3 4 5\n3\n3 3 3\n4\n1 2 2 2\n3\n1 2 2\n3\n1 1 2\n3\n1 9 84"], "outputs": ["YES\nRBRR\nYES\nBBRBB\nNO\nYES\nRBBR\nYES\nRRB\nYES\nBRR\nYES\nBRB"]}


Question:
A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large.

You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits?

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

The only line of each test case contains a single integer $x$ ($10 \leq x \leq 10^{18}$).

Output Format:
For each test case, output $\texttt{YES}$ if $x$ satisfies the condition, and $\texttt{NO}$ otherwise.

You can output $\texttt{YES}$ and $\texttt{NO}$ in any case (for example, strings $\texttt{yES}$, $\texttt{yes}$, and $\texttt{Yes}$ will be recognized as a positive response).

Tags:
['implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.

You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:

  * Option $1$: set $c$ to $c + a_i$.    * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.

Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find $k$.

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

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

The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$).

The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the value of $k$.

Tags:
['dp', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.

You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:

  * Option $1$: set $c$ to $c + a_i$.    * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.

Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find the number of unique procedures that result in $c = k$. Two procedures are different if at any index $i$, one procedure chose option $1$ and another chose option $2$, even if the value of $c$ is equal for both procedures after that turn.

Since the answer may be large, output it modulo $998\,244\,353$.

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

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

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$).

The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the number of unique procedures that result in $c = k$, modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditions:

  * each substring either equals $t$ or "$\texttt{a}$", and    * at least one substring equals $t$.

$^{\dagger}$ A partition of a string $s$ is an ordered sequence of some $k$ strings $t_1, t_2, \ldots, t_k$ (called substrings) such that $t_1 + t_2 + \ldots + t_k = s$, where $+$ represents the concatenation operation.

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

The only line of each test case contains a string $s$ consisting of lowercase Latin characters ($2 \leq |s| \leq 2 \cdot 10^5$).

The sum of $|s|$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the number of nonempty strings $t \neq$ "$\texttt{a}$" that satisfy all constraints.

Tags:
['brute force', 'hashing', 'implementation', 'math', 'string suffix structures', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old tree.

  1. Choose any node $V$ from the original tree $T$. Create a new tree $T_2$, with $V$ as the root.    2. Remove $V$ from $T$, such that the original tree is split into one or more subtrees (or zero subtrees, if $V$ is the only node in $T$).    3. Shuffle each subtree with the same procedure (again choosing any node as the root), then connect all shuffled subtrees' roots back to $V$ to finish constructing $T_2$.

After this, Oscar and Lura are left with a new tree $T_2$. They can only eat leaves and are very hungry, so please find the maximum number of leaves over all trees that can be created in exactly one shuffle.

Note that leaves are all nodes with degree $1$. Thus, the root may be considered as a leaf if it has only one child.

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

The first line of every test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of nodes within the original tree $T$.

The next $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) — an edge within the original tree $T$. The given edges form a tree.

The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the maximum number of leaves achievable with exactly one shuffle procedure on the whole tree.

Tags:
['dp', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive.

You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

For each $i$ from $1$ to $n$ inclusive, we must have:

  * If $s_i = \texttt{P}$, $b_i$ is the sum of $a_1$ through $a_i$.    * If $s_i = \texttt{S}$, $b_i$ is the sum of $a_i$ through $a_n$.

Output the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ such that there exists an array $a_1, a_2, \ldots, a_n$ with elements not exceeding $m$ by absolute value satisfying the constraints given by the array $b_1, b_2, \ldots, b_n$ and the string $s$.

Since the answer may be large, output it modulo $998\,244\,353$.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) — the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element $a_i$.

The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($|b_i| \leq m \cdot n$).

The sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.

Output Format:
For each test case, output a single integer — the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.

Tags:
['brute force', 'dp', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
The secret behind Oscar's first magic trick has been revealed! Because he still wants to impress Lura, he comes up with a new idea: he still wants to sort a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.

This time, he chooses an integer $k$. He wants to sort the permutation in non-decreasing order using the following operation several times:

  1. Pick a continuous subarray of length $k$ and remove it from $p$.    2. Insert the continuous subarray back into $p$ at any position (perhaps, in the very front or the very back).

To be as impressive as possible, Oscar would like to choose the maximal value of $k$ such that he can sort his permutation. Please help him find the maximal $k$ as well as a sequence of operations that will sort the permutation. You don't need to minimize the number of operations, but you are allowed to use at most $5n^2$ operations.

We have a proof that, for the maximal $k$ such that you can sort the permutation in any number of operations, you can also sort it in at most $5n^2$ operations.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($5 \leq n \leq 10^3$) — the length of the permutation.

The second line of each test case contains a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.

The sum of $n$ over all test cases does not exceed $2 \cdot 10^3$.

Output Format:
For each test case, first output the chosen value of $k$ on a new line ($1 \leq k \leq n$).

Then, output a single integer $m$ — the number of operations used ($0 \leq m \leq 5n^2$).

Then, on each of the next $m$ lines, output the operations denoted by two integers $i$ and $j$ ($1 \leq i, j \leq n - k + 1$), representing an operation where you remove the subarray starting from index $i$ and replace it back into $p$ at index $j$.

Tags:
['constructive algorithms', 'implementation', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them. To do this, you can do the following operation any number of times:

  * Pick two towers $P$ and $Q$ you own and one tower $R$ you don't own, such that the circle through $P$, $Q$, and $R$ contains all $n$ towers inside of it.    * Afterwards, capture all towers in or on triangle $\triangle PQR$, including $R$ itself.

An attack plan is a series of choices of $R$ ($R_1, R_2, \ldots, R_k$) using the above operations after which you capture all towers. Note that two attack plans are considered different only if they differ in their choice of $R$ in some operation; in particular, two attack plans using the same choices of $R$ but different choices of $P$ and $Q$ are considered the same.

Count the number of attack plans of minimal length. Note that it might not be possible to capture all towers, in which case the answer is $0$.

Since the answer may be large, output it modulo $998\,244\,353$.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 250$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($4 \leq n \leq 100$) — the number of towers.

The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$) — the location of the $i$-th tower. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$.

All towers are at distinct locations, no three towers are collinear, and no four towers are concyclic.

The sum of $n$ over all test cases does not exceed $1000$.

Output Format:
For each test case, output a single integer — the number of attack plans of minimal length after which you capture all towers, modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'geometry']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap.

Note that the new words may not necessarily be different.

Input Format:
The first line contains $t$ ($1 \leq t \leq 100$)  — the number of test cases.

The first and only line of each test case contains two space-separated strings, $a$ and $b$, both of length $3$. The strings only contain lowercase Latin letters.

Output Format:
For each test case, after the swap, output $a$ and $b$, separated by a space.

Tags:
['implementation', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB

{"inputs": ["6\nbit set\ncat dog\nhot dog\nuwu owo\ncat cat\nzzz zzz"], "outputs": ["sit bet\ndat cog\ndot hog\nowu uwo\ncat cat\nzzz zzz"]}


Question:
Given an integer $n$, find an integer $x$ such that:

  * $2 \leq x \leq n$.    * The sum of multiples of $x$ that are less than or equal to $n$ is maximized. Formally, $x + 2x + 3x + \dots + kx$ where $kx \leq n$ is maximized over all possible values of $x$.

Input Format:
The first line contains $t$ ($1 \leq t \leq 100$) — the number of test cases.

Each test case contains a single integer $n$ ($2 \leq n \leq 100$).

Output Format:
For each test case, output an integer, the optimal value of $x$. It can be shown there is only one unique answer.

Tags:
['brute force', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Alex thinks some array is good if there exists some element that can be represented as the sum of all other elements (the sum of all other elements is $0$ if there are no other elements). For example, the array $[1,6,3,2]$ is good since $1+3+2=6$. Furthermore, the array $[0]$ is also good. However, the arrays $[1,2,3,4]$ and $[1]$ are not good.

Alex has an array $a_1,a_2,\ldots,a_n$. Help him count the number of good non-empty prefixes of the array $a$. In other words, count the number of integers $i$ ($1 \le i \le n$) such that the length $i$ prefix (i.e. $a_1,a_2,\ldots,a_i$) is good.

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

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

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

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

Output Format:
For each test case, output a single integer — the number of good non-empty prefixes of the array $a$.

Tags:
['greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.

Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$, where $r$ is a positive constant.

On the grid, the set of points that are part of the manhattan circle is marked as '#'. Find the coordinates of the center of the circle.

Input Format:
The first line contains $t$ ($1 \leq t \leq 1000$)  — the number of test cases.

The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) — the height and width of the grid, respectively.

The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point is part of the manhattan circle.

It is guaranteed the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$, and there is a whole manhattan circle on the grid.

Output Format:
For each test case, output the two integers, the coordinates of the center of the circle.

Tags:
['implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Ntarsis has a box $B$ with side lengths $x$, $y$, and $z$. It lies in the 3D coordinate plane, extending from $(0,0,0)$ to $(x,y,z)$.

Ntarsis has a secret box $S$. He wants to choose its dimensions such that all side lengths are positive integers, and the volume of $S$ is $k$. He can place $S$ somewhere within $B$ such that:

  * $S$ is parallel to all axes.    * every corner of $S$ lies on an integer coordinate.

$S$ is magical, so when placed at an integer location inside $B$, it will not fall to the ground.

Among all possible ways to choose the dimensions of $S$, determine the maximum number of distinct locations he can choose to place his secret box $S$ inside $B$. Ntarsis does not rotate $S$ once its side lengths are selected.

Input Format:
The first line consists of an integer $t$, the number of test cases ($1 \leq t \leq 2000$). The description of the test cases follows.

The first and only line of each test case contains four integers $x, y, z$ and $k$ ($1 \leq x, y, z \leq 2000$, $1 \leq k \leq x \cdot y \cdot z$).

It is guaranteed the sum of all $x$, sum of all $y$, and sum of all $z$ do not exceed $2000$ over all test cases.

Note that $k$ may not fit in a standard 32-bit integer data type.

Output Format:
For each test case, output the answer as an integer on a new line. If there is no way to select the dimensions of $S$ so it fits in $B$, output $0$.

Tags:
['brute force', 'combinatorics', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can use all attacks that are not currently on cooldown, all at once. If all attacks are on cooldown, you do nothing for the turn and skip to the next turn.

Initially, all attacks are not on cooldown. How many turns will you take to beat the boss? The boss is beaten when its health is $0$ or less.

Input Format:
The first line contains $t$ ($1 \leq t \leq 10^4$)  – the number of test cases.

The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) – the health of the boss and the number of attacks you have.

The following line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ ($1 \leq a_i \leq 2 \cdot 10^5$) – the damage of your attacks.

The following line of each test case contains $n$ integers $c_1, c_2, ..., c_n$ ($1 \leq c_i \leq 2 \cdot 10^5$) – the cooldown of your attacks.

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

Output Format:
For each test case, output an integer, the minimum number of turns required to beat the boss.

Tags:
['binary search', 'data structures']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Let $D(n)$ represent the sum of digits of $n$. For how many integers $n$ where $10^{l} \leq n < 10^{r}$ satisfy $D(k \cdot n) = k \cdot D(n)$? Output the answer modulo $10^9+7$.

Input Format:
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) – the number of test cases.

Each test case contains three integers $l$, $r$, and $k$ ($0 \leq l < r \leq 10^9$, $1 \leq k \leq 10^9$).

Output Format:
For each test case, output an integer, the answer, modulo $10^9 + 7$.

Tags:
['combinatorics', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.

Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.

In one operation, Alex selects any row $r$ ($1 \le r \le n$) or any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ or column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \cdot m \le 10^6$) — the number of rows and columns of the grid.

The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.

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

Output Format:
For each test case, output a single integer — the maximum possible size of a connected component of '#' cells that Alex can achieve.

Tags:
['brute force', 'data structures', 'dfs and similar', 'dsu', 'graphs', 'implementation']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.

Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.

In one operation, Alex selects any row $r$ ($1 \le r \le n$) and any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ and column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \cdot m \le 10^6$) — the number of rows and columns of the grid.

The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.

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

Output Format:
For each test case, output a single integer — the maximum possible size of a connected component of '#' cells that Alex can achieve.

Tags:
['data structures', 'dfs and similar', 'dp', 'dsu', 'implementation']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given three points with integer coordinates $x_1$, $x_2$, and $x_3$ on the $X$ axis ($1 \leq x_i \leq 10$). You can choose any point with an integer coordinate $a$ on the $X$ axis. Note that the point $a$ may coincide with $x_1$, $x_2$, or $x_3$. Let $f(a)$ be the total distance from the given points to the point $a$. Find the smallest value of $f(a)$.

The distance between points $a$ and $b$ is equal to $|a - b|$. For example, the distance between points $a = 5$ and $b = 2$ is $3$.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. Then follows their descriptions.

The single line of each test case contains three integers $x_1$, $x_2$, and $x_3$ ($1 \leq x_i \leq 10$) — the coordinates of the points.

Output Format:
For each test case, output the smallest value of $f(a)$.

Tags:
['brute force', 'geometry', 'math', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a matrix of size $n \times m$, where the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The element at the intersection of the $i$-th row and the $j$-th column is denoted by $a_{ij}$.

Consider the algorithm for stabilizing matrix $a$:

  1. Find the cell $(i, j)$ such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of $i$, and if there are still multiple cells, choose the one with the smallest value of $j$.   2. Set $a_{ij} = a_{ij} - 1$.    3. Go to step $1$.

In this problem, cells $(a, b)$ and $(c, d)$ are considered neighbors if they share a common side, i.e., $|a - c| + |b - d| = 1$.

Your task is to output the matrix $a$ after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations.

Input Format:
Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. This is followed by their description.

The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 100, n \cdot m > 1$) — the number of rows and columns of matrix $a$.

The next $n$ lines describe the corresponding rows of the matrix. The $i$-th line contains $m$ integers $a_{i1}, a_{i2}, \ldots, a_{im}$ ($1 \leq a_{ij} \leq 10^9$).

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

Output Format:
For each set of input data, output $n$ lines with $m$ numbers in each line — the values of the cells of matrix $a$ after the stabilization algorithm.

Tags:
['brute force', 'data structures', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the $i$-th operation, you set $s_{ind_i} = c_i$. Note that you perform all $m$ operations from the first to the last.

Of course, if you change the order of indices in the array $ind$ and/or the order of letters in the string $c$, you can get different results. Find the lexicographically smallest string $s$ that can be obtained after $m$ update operations, if you can rearrange the indices in the array $ind$ and the letters in the string $c$ as you like.

A string $a$ is lexicographically less than a string $b$ if and only if one of the following conditions is met:

  * $a$ is a prefix of $b$, but $a \neq b$;   * in the first position where $a$ and $b$ differ, the symbol in string $a$ is earlier in the alphabet than the corresponding symbol in string $b$.

Input Format:
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) — the length of the string $s$ and the number of updates.

The second line of each set of input data contains a string $s$ of length $n$, consisting of lowercase Latin letters.

The third line of each set of input data contains $m$ integers $ind_1, ind_2, \ldots, ind_m$ ($1 \leq ind_i \leq n$) — the array of indices $ind$.

The fourth line of each set of input data contains a string $c$ of length $m$, consisting of lowercase Latin letters.

It is guaranteed that the sum of $n$ over all sets of input data does not exceed $2 \cdot 10^5$. Similarly, the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.

Output Format:
For each set of input data, output the lexicographically smallest string $s$ that can be obtained by rearranging the indices in the array $ind$ and the letters in the string $c$ as you like.

Tags:
['data structures', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["4\n1 2\na\n1 1\ncb\n4 4\nmeow\n1 2 1 4\nzcwz\n7 4\nabacaba\n1 3 5 7\ndamn\n7 10\ntraktor\n7 6 5 4 3 2 1 6 4 2\ncodeforces"], "outputs": ["b\ncwoz\nabdcmbn\nccdeefo"]}


Question:
You are given a string $s$ of length $n > 1$, consisting of digits from $0$ to $9$. You must insert exactly $n - 2$ symbols $+$ (addition) or $\times$ (multiplication) into this string to form a valid arithmetic expression.

In this problem, the symbols cannot be placed before the first or after the last character of the string $s$, and two symbols cannot be written consecutively. Also, note that the order of the digits in the string cannot be changed. Let's consider $s = 987009$:

  * From this string, the following arithmetic expressions can be obtained: $9 \times 8 + 70 \times 0 + 9 = 81$, $98 \times 7 \times 0 + 0 \times 9 = 0$, $9 + 8 + 7 + 0 + 09 = 9 + 8 + 7 + 0 + 9 = 33$. Note that the number $09$ is considered valid and is simply transformed into $9$.   * From this string, the following arithmetic expressions cannot be obtained: $+9 \times 8 \times 70 + 09$ (symbols should only be placed between digits), $98 \times 70 + 0 + 9$ (since there are $6$ digits, there must be exactly $4$ symbols).

The result of the arithmetic expression is calculated according to the rules of mathematics — first all multiplication operations are performed, then addition. You need to find the minimum result that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string $s$.

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

The first line of each test case contains a single integer $n$ ($2 \leq n \leq 20$) — the length of the string $s$.

The second line of each test case contains a string $s$ of length $n$, consisting of digits from $0$ to $9$.

Output Format:
For each test case, output the minimum result of the arithmetic expression that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string.

Tags:
['brute force', 'dp', 'greedy', 'implementation', 'math', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations.

Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following:

  * Choose an index $1 \leq i \leq n$,   * Make $a_i = a_i + k$.

The array $b_1, b_2, \ldots, b_n$ is beautiful if $b_i = b_{n - i + 1}$ for all $1 \leq i \leq n$.

Find the minimum number of operations needed to make the array beautiful, or report that it is impossible.

Input Format:
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 10^9$) — the size of the array $a$ and the number $k$ from the problem statement.

The second line of each set of input data contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array $a$.

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

Output Format:
For each set of input data, output the minimum number of operations needed to make the array beautiful, or $-1$ if it is impossible.

Tags:
['greedy', 'math', 'number theory', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph.

Find the smallest number of pairs of vertices!

Input Format:
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $n - 1 \leq m \leq \min(10^5, \frac{n \cdot (n - 1)}{2})$) — the number of vertices in the graph and the number of edges.

Each of the next $m$ lines contains two integers $u$ and $v$ ($1 \leq u, v \leq n, u \neq v$), indicating that there is an undirected edge in the graph between vertices $u$ and $v$.

It is guaranteed that the given graph is connected and without multiple edges.

It is guaranteed that the sum of $n$ and the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.

Output Format:
For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed.

Tags:
['dfs and similar', 'graphs', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is a simple version of the problem. The only difference is that in this version $n \leq 10^5$ and the sum of $n$ for all sets of input data does not exceed $10^5$.

You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.

A permutation is a sequence of $n$ integers, where each integer from $1$ to $n$ occurs exactly once. For example, $[1]$, $[3,5,2,1,4]$, $[1,3,2]$ are permutations, while $[2,3,2]$, $[4,3,1]$, $[0]$ are not.

Input Format:
Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains a single integer $n$ ($1 \leq n \leq 10^5$) — the length of the permutation $p$.

The second line of each set of input data contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) — the permutation $p$.

It is guaranteed that the sum of $n$ for all sets of input data does not exceed $10^5$.

Output Format:
For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.

Tags:
['binary search', 'brute force', 'combinatorics', 'data structures', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 128 MB


Question:
This is the hard version of the problem. The only difference is that in this version $n \leq 5 \cdot 10^5$ and the sum of $n$ for all sets of input data does not exceed $5 \cdot 10^5$.

You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.

A permutation is a sequence of $n$ integers, in which each integer from $1$ to $n$ occurs exactly once. For example, $[1]$, $[3,5,2,1,4]$, $[1,3,2]$ are permutations, while $[2,3,2]$, $[4,3,1]$, $[0]$ are not.

Input Format:
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains a single integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) — the length of the permutation $p$.

The second line of each set of input data contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) — the permutation $p$.

It is guaranteed that the sum of $n$ for all sets of input data does not exceed $5 \cdot 10^5$.

Output Format:
For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.

Tags:
['brute force', 'data structures', 'hashing', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 128 MB


Question:
Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM!

You want to upload $n$ GBs of RAM. Every second, you will upload either $0$ or $1$ GB of RAM. However, there is a restriction on your network speed: in any $k$ consecutive seconds, you can upload only at most $1$ GB of RAM in total.

Find the minimum number of seconds needed to upload $n$ GBs of RAM!

Input Format:
Each test contains multiple test cases. The first line of input 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 and only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 100$) — the number of GBs that you want to upload and the length of the time window respectively.

Output Format:
For each test case, output a single integer — the minimum number of seconds needed to upload $n$ GBs of RAM.

Tags:
['greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array of integers $a$ of length $n$.

You can apply the following operation any number of times (maybe, zero):

  * First, choose an integer $k$ such that $1 \le k \le n$ and pay $k + 1$ coins.    * Then, choose exactly $k$ indices such that $1 \le i_1 < i_2 < \ldots < i_k \le n$.    * Then, for each $x$ from $1$ to $k$, increase $a_{i_x}$ by $1$.

Find the minimum number of coins needed to make $a$ non-decreasing. That is, $a_1 \le a_2 \le \ldots \le a_n$.

Input Format:
Each test contains multiple test cases. The first line of input 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 a single integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.

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

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

Output Format:
For each test case, output a single integer — the minimum number of coins needed to make $a$ non-decreasing.

Tags:
['greedy']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There are $n$ flowers in a row, the $i$-th of them initially has a positive height of $h_i$ meters.

Every second, the wind will blow from the left, causing the height of some flowers to decrease.

Specifically, every second, for each $i$ from $1$ to $n$, in this order, the following happens:

  * If $i = n$ or $h_i > h_{i + 1}$, the value of $h_i$ changes to $\max(0, h_i - 1)$.

How many seconds will pass before $h_i=0$ for all $1 \le i \le n$ for the first time?

Input Format:
Each test contains multiple test cases. The first line of input 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 a single integer $n$ ($1 \le n \le 10^5$) — the number of flowers.

The second line of each test case contains $n$ integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le 10 ^ 9$) — the heights of the flowers.

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

Output Format:
For each test case, output a single integer — the number of seconds that will pass before $h_i=0$ for all $1 \le i \le n$.

Tags:
['dp', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.

Alice and Bob take turns eating them, with Alice starting first:

  * In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of the cakes she's eaten before that. Note that on the first turn, she can choose any cake.    * In his turn, Bob chooses any remaining cake and eats it.

The game ends when the current player can't eat a suitable cake. Let $x$ be the number of cakes that Alice ate. Then, Alice wants to maximize $x$, while Bob wants to minimize $x$.

Find out how many cakes Alice will eat if both players play optimally.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) — 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 \le n \le 5000$) — the number of cakes.

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

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

Output Format:
For each test case, output a single integer — the number of cakes Alice will eat if both players play optimally.

Tags:
['dp', 'games']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
God's Blessing on This ArrayForces!

A Random Pebble

You are given a tree with $n$ vertices, rooted at vertex $1$. The $i$-th vertex has an integer $a_i$ written on it.

Let $L$ be the set of all direct children$^{\text{∗}}$ of $v$. A tree is called wonderful, if for all vertices $v$ where $L$ is not empty, $$a_v \le \sum_{u \in L}{a_u}.$$ In one operation, you choose any vertex $v$ and increase $a_v$ by $1$.

Find the minimum number of operations needed to make the given tree wonderful!

$^{\text{∗}}$ Vertex $u$ is called a direct child of vertex $v$ if:

  * $u$ and $v$ are connected by an edge, and    * $v$ is on the (unique) path from $u$ to the root of the tree.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 5000$) — the number of vertices in the tree.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the values initially written on the vertices.

The third line of each test case contains $n - 1$ integers $p_2, p_3 , \ldots, p_n$ ($1 \le p_i < i$), indicating that there is an edge from vertex $p_i$ to vertex $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 $5000$.

Output Format:
For each test case, output a single integer — the minimum number of operations needed to make the tree wonderful.

Tags:
['brute force', 'data structures', 'dfs and similar', 'dsu', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.

You are given an array of integers $a$ of length $n$.

In one operation, you will perform the following two-step process:

  1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$.    2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.

Find the maximum number of times that you can perform the operation above.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) — 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 \le n \le 100$) — the length of the array $a$.

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

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

Output Format:
For each test case, output a single integer — the maximum number of times that you can perform the operation.

Tags:
['dp']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.

You are given an array of integers $a$ of length $n$.

In one operation, you will perform the following two-step process:

  1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$.    2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.

Find the maximum number of times that you can perform the operation above.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) — 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 \le n \le 800$) — the length of the array $a$.

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

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

Output Format:
For each test case, output a single integer — the maximum number of times that you can perform the operation.

Tags:
['dp']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference between the two versions are the allowed characters in $s$. In the easy version, $s$ only contains the character ?. You can make hacks only if both versions of the problem are solved.

You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, consisting only of the character ?.

For each $i$ from $1$ to $n$:

  * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$.    * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$.

Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph:

  * If $s_i =$ L, add the edge $(i, l_i)$ to the graph.    * If $s_i =$ R, add the edge $(i, r_i)$ to the graph.    * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice.

Find the maximum possible diameter$^{\text{∗}}$ over all connected graphs that you can form. Output $-1$ if it is not possible to form any connected graphs.

$^{\text{∗}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$.

The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$) — the length of the permutation $p$.

The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) — the elements of $p$, which are guaranteed to form a permutation.

The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the character ?.

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

Output Format:
For each test case, output the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.

Tags:
['divide and conquer', 'dp', 'trees']

Time Limit: 7000 ms
Memory Limit: 1024 MB


Question:
This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved.

You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, where each character is either L, R, or ?.

For each $i$ from $1$ to $n$:

  * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$.    * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$.

Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph:

  * If $s_i =$ L, add the edge $(i, l_i)$ to the graph.    * If $s_i =$ R, add the edge $(i, r_i)$ to the graph.    * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice.

Find the maximum possible diameter over all connected$^{\text{∗}}$ graphs that you can form. Output $-1$ if it is not possible to form any connected graphs.

$^{\text{∗}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$.

The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$) — the length of the permutation $p$.

The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) — the elements of $p$, which are guaranteed to form a permutation.

The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the characters L, R, and ?.

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

Output Format:
For each test case, output the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.

Tags:
['divide and conquer', 'dp', 'trees']

Time Limit: 7000 ms
Memory Limit: 1024 MB