Question:
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked.

We say that room $x$ is reachable from room $y$ if all doors between them are unlocked.

You know that:

  * Alice is in some room from the segment $[l, r]$;    * Bob is in some room from the segment $[L, R]$;    * Alice and Bob are in different rooms.

However, you don't know the exact rooms they are in.

You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments?

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 two integers $l$ and $r$ ($1 \le l < r \le 100$) — the bounds of the segment of rooms where Alice is located.

The second line of each test case contains two integers $L$ and $R$ ($1 \le L < R \le 100$) — the bounds of the segment of rooms where Bob is located.

Output Format:
For each test case, print a single integer — the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice.

In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left.

Let's say that $A$ is the total cost of items taken by Alice and $B$ is the total cost of Bob's items. The resulting score of the game then will be equal to $A - B$.

Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally.

But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs $a_i$ of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to $k$. Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase.

What is the minimum possible score Bob can achieve?

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 5000$) — the number of test cases. Then $t$ cases follow.

The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le 10^9$) — the number of items and the maximum total increase Bob can make.

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

It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, print a single integer — the minimum possible score $A - B$ after Bob increases the costs of several (possibly none or all) items.

Tags:
['games', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$.

Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city $i$ to city $j$ if they have portals of the same color (for example, you can move between a "blue-red" city and a "blue- green" city). This movement costs $|i-j|$ coins.

Your task is to answer $q$ independent queries: calculate the minimum cost to move from city $x$ to city $y$.

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 two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) — the number of cities and the number of queries, respectively.

The second line contains $n$ strings of the following types: BG, BR, BY, GR, GY, or RY; the $i$-th of them describes the portals located in the $i$-th city; the symbol B indicates that there is a blue portal in the city, G — green, R — red, and Y — yellow.

The $j$-th of the next $q$ lines contains two integers $x_j$ and $y_j$ ($1 \le x_j, y_j \le n$) — the description of the $j$-th query.

Additional constraints on the input:

  * the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$;    * the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each query, print a single integer — the minimum cost to move from city $x$ to city $y$ (or $-1$ if it is impossible).

Tags:
['binary search', 'brute force', 'data structures', 'graphs', 'greedy', 'implementation', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Two players, Alice and Bob, are playing a game. They have $n$ piles of stones, with the $i$-th pile initially containing $a_i$ stones.

On their turn, a player can choose any pile of stones and take any positive number of stones from it, with one condition:

  * let the current number of stones in the pile be $x$. It is not allowed to take from the pile a number of stones $y$ such that the greatest common divisor of $x$ and $y$ is not equal to $1$.

The player who cannot make a move loses. Both players play optimally (that is, if a player has a strategy that allows them to win, no matter how the opponent responds, they will win). Alice goes first.

Determine who will win.

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

Each test case consists of two lines:

  * the first line contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$);    * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^7$).

Additional constraint on the input: the sum of $n$ across all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output Alice if Alice wins, or Bob if Bob wins.

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

Time Limit: 2000 ms
Memory Limit: 512 MB


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

Let the function $f(b)$ return the minimum number of operations needed to make an array $b$ a palindrome. The operations you can make are:

  * choose two adjacent elements $b_i$ and $b_{i+1}$, remove them, and replace them with a single element equal to $(b_i + b_{i + 1})$;    * or choose an element $b_i > 1$, remove it, and replace it with two positive integers $x$ and $y$ ($x > 0$ and $y > 0$) such that $x + y = b_i$.

For example, from an array $b=[2, 1, 3]$, you can obtain the following arrays in one operation: $[1, 1, 1, 3]$, $[2, 1, 1, 2]$, $[3, 3]$, $[2, 4]$, or $[2, 1, 2, 1]$.

Calculate $\displaystyle \left(\sum_{1 \le l \le r \le n}{f(a[l..r])}\right)$, where $a[l..r]$ is the subarray of $a$ from index $l$ to index $r$, inclusive. In other words, find the sum of the values of the function $f$ for all subarrays of the array $a$.

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

The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$).

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$).

Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2000$.

Output Format:
For each test case, print a single integer — the sum of the values of the function $f$ for all subarrays of the array $a$.

Tags:
['binary search', 'brute force', 'data structures', 'greedy', 'math']

Time Limit: 5000 ms
Memory Limit: 512 MB


Question:
Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows:

  * split it into an even number of non-empty substrings — let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = t_1 + t_2 + \dots + t_m$, where $+$ is the concatenation operation);    * write the string $t_2$ $t_1$ times, then the string $t_4$ $t_3$ times, and so on.

For example, for a string "12345", one could do the following: split it into ("1", "23", "4", "5"), and write "235555".

Let the function $f(t)$ for a string $t$ return the minimum length of the string that can be obtained as a result of that process.

You are given a string $s$, consisting of $n$ digits from $1$ to $9$, and an integer $k$. Calculate the value of the function $f$ for all contiguous substrings of $s$ of length exactly $k$.

Input Format:
The first line contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$).

The second line contains the string $s$ ($|s| = n$), consisting only of digits from $1$ to $9$.

Output Format:
Output $n - k + 1$ integers — $f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})$.

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

Time Limit: 2000 ms
Memory Limit: 1024 MB


Question:
Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes.

Narek found out that the kids only know the vowels of the English alphabet (the letters $\mathtt{a}$, $\mathtt{e}$, $\mathtt{i}$, $\mathtt{o}$, and $\mathtt{u}$), so Narek needs to make a string that consists of vowels only. After making the string, he'll ask the kids to count the number of subsequences that are palindromes. Narek wants to keep it simple, so he's looking for a string such that the amount of palindrome subsequences is minimal.

Help Narek find a string of length $n$, consisting of lowercase English vowels only (letters $\mathtt{a}$, $\mathtt{e}$, $\mathtt{i}$, $\mathtt{o}$, and $\mathtt{u}$), which minimizes the amount of palindrome$^{\dagger}$ subsequences$^{\ddagger}$ in it.

$^{\dagger}$ A string is called a palindrome if it reads the same from left to right and from right to left.

$^{\ddagger}$ String $t$ is a subsequence of string $s$ if $t$ can be obtained from $s$ by removing several (possibly, zero or all) characters from $s$ and concatenating the remaining ones, without changing their order. For example, $\mathtt{odocs}$ is a subsequence of $\texttt{c}{\color{red}{\texttt{od}}}\texttt{ef}{\color{red}{\texttt{o}}}\texttt{r}{\color{red}{\texttt{c}}}\texttt{e}{\color{red}{\texttt{s}}}$.

Input Format:
The first line of the input contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Subsequently, the description of each test case follows.

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

Output Format:
For each test case, output any string of length $n$ that satisfies the above conditions.

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

Time Limit: 1000 ms
Memory Limit: 256 MB

{"inputs": ["3\n2\n3\n6"], "outputs": ["uo\niae\noeiiua"]}


Question:
This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved.

Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide.

The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive.

At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move

  * David goes to an adjacent cell or stays at the current one.    * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one.

This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally.

Your task is to find how many moves it will take for the teachers to catch David if they all act optimally.

Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student.

Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position. Note: this is the easy version, and you are given only one query.

Input Format:
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of each test case follows.

In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $m=2$, $q=1$) — the number of cells on the line, the number of teachers, and the number of queries.

In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the cell numbers of the teachers.

In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) — David's cell number for every query.

It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$.

Output Format:
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.

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

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m, q \le 10^5$. You can make hacks only if both versions of the problem are solved.

Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide.

The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive.

At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move

  * David goes to an adjacent cell or stays at the current one.    * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one.

This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally.

Your task is to find how many moves it will take for the teachers to catch David if they all act optimally.

Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student.

Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position.

Input Format:
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of each test case follows.

In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $1 \le m, q \le 10^5$) — the number of cells on the line, the number of teachers, and the number of queries.

In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the cell numbers of the teachers.

In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) — David's cell number for every query.

It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$.

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

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

Output Format:
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.

Tags:
['binary search', 'greedy', 'math', 'sortings']

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $score_n - score_c$, where $score_n$ is Narek's score and $score_c$ is ChatGPT's score.

Narek calculates $score_n$ by examining the selected string (he moves from left to right). He initially searches for the letter $\texttt{"n"}$, followed by $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, and $\texttt{"k"}$. Upon finding all occurrences of these letters, he increments $score_n$ by $5$ and resumes searching for $\texttt{"n"}$ again (he doesn't go back, and he just continues from where he left off).

After Narek finishes, ChatGPT scans through the array and increments $score_c$ by $1$ for each letter $\texttt{"n"}$, $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, or $\texttt{"k"}$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $5$ letters, then all of the letters he used are counted in ChatGPT's score $score_c$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters).

Narek aims to maximize the value of $score_n - score_c$ by selecting the most optimal subset of the initial strings.

Input Format:
In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows.

In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string.

In the next $n$ lines, you're given $n$ strings, each having a length of $m$. The strings only contain lowercase letters of the English alphabet.

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

Output Format:
For each test case, output a single integer: the maximal possible value of $score_n - score_c$.

Tags:
['dp', 'implementation', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["4\n5 2\nnn\naa\nrr\nee\nkk\n1 5\nnarek\n1 4\nnare\n5 7\nnrrarek\nnrnekan\nuuuuuuu\nppppppp\nnkarekz"], "outputs": ["0\n5\n0\n7"]}


Question:
You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$.

You must perform the following operation exactly once:

  * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$;    * swap $a_i$ and $b_i$ for all $i$ such that $l \leq i \leq r$.

Find the maximum possible value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once. Also find the number of distinct pairs $(l, r)$ which achieve the maximum value.

Input Format:
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows.

In the first line of each test case, you are given a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), representing the number of integers in each array.

In the next line, you are given $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

In the last line, you are given $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) — the elements of the array $b$.

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

Output Format:
For each test case, output a line with two integers: the maximum value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once, and the number of ways.

Tags:
['binary search', 'brute force', 'data structures', 'divide and conquer', 'implementation', 'number theory']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved.

Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$.

They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses.

Your task is to determine the winner if the players play optimally.

Input Format:
The first line of the input contains $t$ ($1 \le t \le 300$) – the number of test cases.

The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 300$) – the size of the array and the sizes of the matrix.

The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le \min(7, n \cdot m)$) – the elements of the array $a$.

The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le \min(7, n \cdot m)$) – representing the $i$-th row of the matrix.

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

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

Output Format:
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).

Tags:
['dp', 'games', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved.

Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$.

They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses.

Your task is to determine the winner if the players play optimally.

Note: since the input is large, you may need to optimize input/output for this problem.

For example, in C++, it is enough to use the following lines at the start of the main() function:

              int main() {         ios_base::sync_with_stdio(false);         cin.tie(NULL); cout.tie(NULL);     }

Input Format:
The first line of the input contains $t$ ($1 \le t \le 1500$) – the number of test cases.

The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 1500$) – the size of the array and the sizes of the matrix.

The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le n \cdot m$) – the elements of the array $a$.

The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le n \cdot m$) – representing the $i$-th row of the matrix.

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

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

Output Format:
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).

Tags:
['data structures', 'dp', 'games', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.

Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it.

Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$.

![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5)

  * Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$.   * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$.

The score of a tree is defined as the number of leaves with non-zero weight in the tree.

But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.

Assuming that both girls play optimally, please determine the final score of the tree.

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

The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) — denoting an edge between vertices $u$ and $v$.

It's guaranteed that the given edges form a tree.

The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$.

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

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

Tags:
['constructive algorithms', 'dfs and similar', 'games', 'graphs', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.

Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer.

The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.

![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers.

We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree.

Next, there will be $n - 1$ events:

  * Iris is given integers $x$ and $y$, indicating that $t_x = y$.

After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers.

Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.

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. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) — the number of vertices in the tree and the sum of the edge weights.

The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) — the description of the edges of the tree.

Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$.

It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$.

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 one line containing $n-1$ integers, each representing the answer after each event.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set.

Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not.

Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant.

Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.

Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.

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. The description of the test cases follows.

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

The second line of each test case 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 test cases doesn't exceed $4 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the number of brilliant subarrays.

Tags:
['data structures', 'divide and conquer', 'math', 'number theory', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode!

Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $a$ consisting of $n$ elements, and she needs to calculate the product of each two adjacent elements (that is, $a_1 \cdot a_2$, $a_2 \cdot a_3$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $a$ in such a way that $a_i \cdot a_{i + 1} \leq k$ holds for every $1 \leq i < n$. There are two types of operations she can perform:

  1. She can rearrange the elements of the array $a$ in an arbitrary way.   2. She can select an arbitrary element of the array $a$ and change its value to an arbitrary integer from $1$ to $k$.

Iris wants to minimize the number of operations of type $2$ that she uses.

However, that's completely not the end of the summer holiday! Summer holiday lasts for $q$ days, and on the $i$-th day, Iris is asked to solve the Math homework for the subarray $b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}$. Help Iris and tell her the minimum number of type $2$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $b$ is not changed.

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

The first line of each test case contains three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \leq 10^6$) — the length of array $b$, the number of days, and the upper bound for the multiplication calculation.

The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq k$) — the elements of the array $b$.

Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$) — the boundaries of the subarray on the $i$-th day.

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

Output Format:
For each test, output a single line containing $q$ integers — the minimum number of operations of type $2$ needed for each day.

Tags:
['data structures', 'greedy', 'implementation', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Iris likes full binary trees.

Let's define the depth of a rooted tree as the maximum number of vertices on the simple paths from some vertex to the root. A full binary tree of depth $d$ is a binary tree of depth $d$ with exactly $2^d - 1$ vertices.

Iris calls a tree a $d$-binary tree if some vertices and edges can be added to it to make it a full binary tree of depth $d$. Note that any vertex can be chosen as the root of a full binary tree.

Since performing operations on large trees is difficult, she defines the binary depth of a tree as the minimum $d$ satisfying that the tree is $d$-binary. Specifically, if there is no integer $d \ge 1$ such that the tree is $d$-binary, the binary depth of the tree is $-1$.

Iris now has a tree consisting of only vertex $1$. She wants to add $n - 1$ more vertices to form a larger tree. She will add the vertices one by one. When she adds vertex $i$ ($2 \leq i \leq n$), she'll give you an integer $p_i$ ($1 \leq p_i < i$), and add a new edge connecting vertices $i$ and $p_i$.

Iris wants to ask you the binary depth of the tree formed by the first $i$ vertices for each $1 \le i \le n$. Can you tell her the answer?

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. The description of the test cases follows.

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

The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) — descriptions of all edges of the tree.

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 $n$ integers, $i$-th of them representing the binary depth of the tree formed by the first $i$ vertices.

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

Time Limit: 4000 ms
Memory Limit: 1024 MB


Question:
Sadly, Dora poured the paint when painting the class mural. Dora considers the mural as the matrix $b$ of size $n \times n$. Initially, $b_{i,j} = 0$ for all $1 \le i, j \le n$.

Dora has only two brushes which have two different colors. In one operation, she can paint the matrix with one of two brushes:

  * The first brush has color $1$ on it and can paint one column of the matrix. That is, Dora chooses $1 \leq j \leq n$ and makes $b_{i,j} := 1$ for all $1 \leq i \leq n$;    * The second brush has color $2$ on it and can paint one row of the matrix. That is, Dora chooses $1 \leq i \leq n$ and makes $b_{i,j} := 2$ for all $1 \leq j \leq n$.

Dora paints the matrix so that the resulting matrix $b$ contains only $1$ and $2$.

For a matrix $b$, let $f(b)$ denote the minimum number of operations needed to turn the initial matrix (containing only $0$) into $b$. The beauty of a matrix $b$ is the number of ways to paint the initial matrix in exactly $f(b)$ operations to turn it into $b$. If there's no way to turn the initial matrix into $b$, the beauty of $b$ is $0$.

However, Dora made a uniformly random mistake; there's exactly one element different in the matrix $a$ given to you from the real matrix $b$. That is, there is exactly one pair $(i, j)$ such that $a_{i, j} = 3 - b_{i, j}$.

Please help Dora compute the expected beauty of the real matrix $b$ modulo $998\,244\,353$ (all possible $n^2$ mistakes have equal probability).

Since the size of the matrix is too large, Dora will only tell you the positions of $m$ elements of color $1$, and the remaining $n^2-m$ elements have color $2$.

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. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq \min(10^6, n^2)$) — the size of the matrix and the number of elements of color $1$.

Then $m$ lines follow, each containing two positive integers $x_i$ and $y_i$ ($1 \leq x_i, y_i \leq n$) — denoting that $a_{x_i, y_i} = 1$.

It is guaranteed that if $i \neq j$, then $(x_i, y_i) \neq (x_j, y_j)$.

It is also guaranteed that the sum of $n$ over all test cases does not exceed $4\cdot10^5$, and the sum of $m$ over all test cases does not exceed $10^6$.

Output Format:
For each test case, output a single integer — the expected beauty of the real matrix $b$, modulo $998\,244\,353$.

Tags:
['brute force', 'combinatorics', 'constructive algorithms', 'graphs', 'implementation']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Dora has a set $s$ containing integers. In the beginning, she will put all integers in $[l, r]$ into the set $s$. That is, an integer $x$ is initially contained in the set if and only if $l \leq x \leq r$. Then she allows you to perform the following operations:

  * Select three distinct integers $a$, $b$, and $c$ from the set $s$, such that $\gcd(a, b) = \gcd(b, c) = \gcd(a, c) = 1^\dagger$.    * Then, remove these three integers from the set $s$.

What is the maximum number of operations you can perform?

$^\dagger$Recall that $\gcd(x, y)$ means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$.

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

The only line of each test case contains two integers $l$ and $r$ ($1 \leq l \leq r \leq 1000$) — the range of integers in the initial set.

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

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it.

Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types:

  * $\texttt{+ l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i + 1$.    * $\texttt{- l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i - 1$.

For example, if the initial array $a = [7, 1, 3, 4, 3]$, after performing the operation $\texttt{+} \space 2 \space 4$, the array $a = [7, 1, 4, 5, 4]$. Then, after performing the operation $\texttt{-} \space 1 \space 10$, the array $a = [6, 0, 3, 4, 3]$.

Index is curious about the maximum value in the array $a$. Please help her find it after each of the $m$ operations.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1 \leq m \leq 10^5$) — the length of the array and the number of operations.

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

Then $m$ lines follow, each line corresponds to the operation, in the following format: $\texttt{c l r}$ ($c \in \\{\texttt +, \texttt -\\}$, $l$ and $r$ are integers, $1 \leq l \leq r \leq 10^9$) — the description of the operation.

Note that the elements $a_i$ may not satisfy $1\le a_i\le 10^9$ after some operations, as it is shown in the example.

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

Output Format:
For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Dora has just learned the programming language C++!

However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array $c$ with $n$ elements. Dora has two integers $a$ and $b$. In one operation, she can choose one of the following things to do.

  * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $a$.    * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $b$.

Note that $a$ and $b$ are constants, and they can be the same.

Let's define a range of array $d$ as $\max(d_i) - \min(d_i)$. For instance, the range of the array $[1, 2, 3, 4]$ is $4 - 1 = 3$, the range of the array $[5, 2, 8, 2, 2, 1]$ is $8 - 1 = 7$, and the range of the array $[3, 3, 3]$ is $3 - 3 = 0$.

After any number of operations (possibly, $0$), Dora calculates the range of the new array. You need to help Dora minimize this value, but since Dora loves exploring all by herself, you only need to tell her the minimized value.

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. The description of test cases follows.

The first line of each test case contains three integers $n$, $a$, and $b$ ($1 \leq n \leq 10^5$, $1 \leq a, b \leq 10^9$) — the length of the array $c$ and the constant values, respectively.

The second line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$) — the initial elements of the array $c$.

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 possible range of the array after any number of operations.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.

Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it.

Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$.

![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5)

  * Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$.   * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$.

The score of a tree is defined as the number of leaves with non-zero weight in the tree.

But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.

Assuming that both girls play optimally, please determine the final score of the tree.

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

The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) — denoting an edge between vertices $u$ and $v$.

It's guaranteed that the given edges form a tree.

The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$.

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

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

Tags:
['games', 'graphs', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.

Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer.

The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.

![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers.

We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree.

Next, there will be $n - 1$ events:

  * Iris is given integers $x$ and $y$, indicating that $t_x = y$.

After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers.

Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.

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. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) — the number of vertices in the tree and the sum of the edge weights.

The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) — the description of the edges of the tree.

Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$.

It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$.

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 one line containing $n-1$ integers, each representing the answer after each event.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set.

Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not.

Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant.

Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.

Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.

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. The description of the test cases follows.

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

The second line of each test case 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 test cases doesn't exceed $4 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the number of brilliant subarrays.

Tags:
['binary search', 'data structures', 'number theory', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos.

In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$.

Sakurako is not sure if it is possible to solve this problem, so determine whether there is a way to assign signs such that the sum of all elements in the array equals $0$.

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

The only line of each test case contains two integers $a$ and $b$ ($0\le a,b<10$)  — the number of '1's and the number of '2's in the array.

Output Format:
For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, 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:
['brute force', 'constructive algorithms', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
A beautiful binary matrix is a matrix that has ones on its edges and zeros inside.

![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices.

Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows of the matrix, starting from the first and ending with the $r$-th. More formally, the element from the matrix in the $i$-th row and $j$-th column corresponds to the $((i-1)*c+j)$-th element of the string.

You need to check whether the beautiful matrix from which the string $s$ was obtained could be squared. In other words, you need to check whether the string $s$ could have been build from a square beautiful binary matrix (i.e., one where $r=c$).

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

The second line of each test case contains the string $s$ of length $n$. The string is always the result of writing out the strings of a beautiful matrix.

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

Output Format:
Print "Yes", if the original matrix could have been square, and "No" otherwise.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if:

  * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$;    * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$.

Sakurako has come up with boundaries $l$ and $r$ and wants to construct a good array of maximum length, where $l \le a_i \le r$ for all $a_i$.

Help Sakurako find the maximum length of a good array for the given $l$ and $r$.

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

The only line of each test case contains two integers $l$ and $r$ ($1\le l\le r\le 10^9$).

Output Format:
For each test case, output a single integer  — the length of the longest good array Sakurako can form given $l$ and $r$.

Tags:
['binary search', 'brute force', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
For a certain permutation $p$$^{\text{∗}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times.

If $p=[3,5,6,1,2,4]$, then, for example, $4$ is reachable from $1$, because: $i=1$ $\rightarrow$ $i=p_1=3$ $\rightarrow$ $i=p_3=6$ $\rightarrow$ $i=p_6=4$. Now $i=4$, so $4$ is reachable from $1$.

Each number in the permutation is colored either black or white.

Sakurako defines the function $F(i)$ as the number of black integers that are reachable from $i$.

Sakurako is interested in $F(i)$ for each $1\le i\le n$, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this.

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation (the number $2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$, but the array contains $4$).

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 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 $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$)  — the elements of the permutation.

The third line of each test case contains a string $s$ of length $n$, consisting of '0' and '1'. If $s_i=0$, then the number $p_i$ is colored black; if $s_i=1$, then the number $p_i$ is colored white.

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

Output Format:
For each test case, output $n$ integers $F(1), F(2), \dots, F(n)$.

Tags:
['dp', 'dsu', 'graphs', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Sakurako really loves alternating strings. She calls a string $s$ of lowercase Latin letters an alternating string if characters in the even positions are the same, if characters in the odd positions are the same, and the length of the string is even.

For example, the strings 'abab' and 'gg' are alternating, while the strings 'aba' and 'ggwp' are not.

As a good friend, you decided to gift such a string, but you couldn't find one. Luckily, you can perform two types of operations on the string:

  1. Choose an index $i$ and delete the $i$-th character from the string, which will reduce the length of the string by $1$. This type of operation can be performed no more than $1$ time;    2. Choose an index $i$ and replace $s_i$ with any other letter.

Since you are in a hurry, you need to determine the minimum number of operations required to make the string an alternating one.

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

The second line of each test case contains a string $s$, consisting of lowercase Latin letters.

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

Output Format:
For each test case, output a single integer — the minimum number of operations required to turn the string $s$ into an alternating one.

Tags:
['brute force', 'data structures', 'dp', 'greedy', 'implementation', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed.

Since Sakurako has a PhD in probability, she knows that the best number to pick is [the expected value](http://tiny.cc/matozh_en), but she forgot how to calculate it. Help Sakurako and find the expected value of the product of two elements from the array.

It can be shown that the expected value has the form $\frac{P}{Q}$, where $P$ and $Q$ are non-negative integers, and $Q \ne 0$. Report the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.

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 a single integer $n$ ($2\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, \dots, a_n$ ($0\le a_i\le 10^9$)  — the elements of the array.

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

Output Format:
For each test case, output the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Sakurako has prepared a task for you:

She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation any number of times for any $i$ and $j$, as long as they satisfy the conditions.

Sakurako asks you what is the maximum possible value of $mex_k$$^{\text{∗}}$ of the array after any number of operations.

$^{\text{∗}}$$mex_k$ is the $k$-th non-negative integer that is absent in the array. For example, $mex_1(\\{1,2,3 \\})=0$, since $0$ is the first element that is not in the array, and $mex_2(\\{0,2,4 \\})=3$, since $3$ is the second element that is not in the array.

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 two integers $n$ and $k$ ($1\le n\le 2\cdot 10^5,1\le k\le 10^9$)  — the number of elements in the array and the value $k$ for $mex_k$.

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

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

Output Format:
For each test case, output the maximum $mex_k$ that can be achieved through the operations.

Tags:
['binary search', 'greedy', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Sakurako will soon take a test. The test can be described as an array of integers $n$ and a task on it:

Given an integer $x$, Sakurako can perform the following operation any number of times:

  * Choose an integer $i$ ($1\le i\le n$) such that $a_i\ge x$;    * Change the value of $a_i$ to $a_i-x$.

Using this operation any number of times, she must find the minimum possible median$^{\text{∗}}$ of the array $a$.

Sakurako knows the array but does not know the integer $x$. Someone let it slip that one of the $q$ values of $x$ will be in the next test, so Sakurako is asking you what the answer is for each such $x$.

$^{\text{∗}}$The median of an array of length $n$ is the element that stands in the middle of the sorted array (at the $\frac{n+2}{2}$-th position for even $n$, and at the $\frac{n+1}{2}$-th for odd)

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

The first line of each test case contains two integers $n$ and $q$ ($1\le n,q\le 10^5$)  — the number of elements in the array and the number of queries.

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

The following $q$ lines each contain one integer $x$ ($1\le x\le n$).

It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. The same guarantee applies to the sum of $q$ across all test cases.

Output Format:
For each test case, output $q$ integers  — the answer for each query.

Tags:
['binary search', 'brute force', 'greedy', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender.

To demonstrate the power of his blender, Zhan has $n$ fruits.

The blender can mix up to $x$ fruits per second.

In each second, Zhan can put up to $y$ fruits into the blender. After that, the blender will blend $\min(x, c)$ fruits, where $c$ is the number of fruits inside the blender. After blending, blended fruits are removed from the blender.

Help Zhan determine the minimum amount of time required for Zhan to blend all fruits.

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

The first line of each test case contains one integer $n$ ($0 \le n \le 10^9$) — the number of fruits Zhan has.

The second line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^9$) — the number of fruits the blender can blend per second and the number of fruits Zhan can put into the blender per second.

Output Format:
For each testcase, output a single integer — the minimum number of seconds to blend all fruits.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Eralim, being the mafia boss, manages a group of $n$ fighters. Fighter $i$ has a rating of $a_i$.

Eralim arranges a tournament of $n - 1$ battles, in each of which two not yet eliminated fighters $i$ and $j$ ($1 \le i < j \le n$) are chosen, and as a result of the battle, fighter $i$ is eliminated from the tournament, and the rating of fighter $j$ is reduced by the rating of fighter $i$. That is, $a_j$ is decreased by $a_i$. Note that fighter $j$'s rating can become negative. The fighters indexes do not change.

Eralim wants to know what maximum rating the last remaining fighter can preserve if he chooses the battles optimally.

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 \cdot 10^5$) — the number of fighters.

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 ratings of the fighters.

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

Output Format:
For each testcase, output a single integer — the maximum rating that the last remaining fighter can preserve.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Dimash learned that Mansur wrote something very unpleasant about him to a friend, so he decided to find out his password at all costs and discover what exactly he wrote.

Believing in the strength of his password, Mansur stated that his password — is a binary string of length $n$. He is also ready to answer Dimash's questions of the following type:

Dimash says a binary string $t$, and Mansur replies whether it is true that $t$ is a substring of his password.

Help Dimash find out the password in no more than $2n$ operations; otherwise, Mansur will understand the trick and stop communicating with him.

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.

Output Format:

Tags:
['constructive algorithms', 'interactive', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem.

You are given an array $a_1, a_2, \ldots, a_n$ of length $n$. We can perform any number (possibly, zero) of operations on the array.

In one operation, we choose a position $i$ ($1 \leq i \leq n - 1$) and perform the following action:

  * $a_i := a_i - 1$, and $a_{i+1} := a_{i+1} + 1$.

Find the minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.

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 an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).

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

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 minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.

Tags:
['binary search', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Since Mansur is tired of making legends, there will be no legends for this task.

You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest possible value of the expression $$\gcd(a_1) + \gcd(a_1, a_2) + \ldots + \gcd(a_1, a_2, \ldots, a_n),$$ where $\gcd(a_1, a_2, \ldots, a_n)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of $a_1, a_2, \ldots, a_n$.

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 number $n$ ($1 \le n \le 10^5$) — the size of the array.

The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) — the initial array.

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

The sum of $\max(a_1, a_2, \ldots, a_n)$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single number on a separate line — the answer to the problem.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. In this version, $\mathbf{u = v}$. You can make hacks only if both versions of the problem are solved.

Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges.

Alice and Bob take turns, with Alice going first. Each player starts at some vertex.

On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses.

You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$).

You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).

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 \cdot 10^5$) — the number of vertices in the tree.

Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree.

The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$, $\mathbf{u = v}$).

It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$.

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 $m$ lines.

In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.

Tags:
['binary search', 'brute force', 'data structures', 'dp', 'games', 'greedy', 'implementation', 'trees']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved.

Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges.

Alice and Bob take turns, with Alice going first. Each player starts at some vertex.

On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses.

You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$).

You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).

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 \cdot 10^5$) — the number of vertices in the tree.

Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree.

The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$).

It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$.

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 $m$ lines.

In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
There is a little bit of the outlaw in everyone, and a little bit of the hero too.

The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor.

Robin encounters $n$ people starting from the $1$-st and ending with the $n$-th. The $i$-th person has $a_i$ gold. If $a_i \ge k$, Robin will take all $a_i$ gold, and if $a_i=0$, Robin will give $1$ gold if he has any. Robin starts with $0$ gold.

Find out how many people Robin gives gold to.

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$, $k$ ($1 \le n \le 50, 1 \le k \le 100$) — the number of people and the threshold at which Robin Hood takes the gold.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$) — the gold of each person.

Output Format:
For each test case, output a single integer, the number of people that will get gold from Robin Hood.

Tags:
['greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
In Sherwood, the trees are our shelter, and we are all children of the forest.

The Major Oak in Sherwood is known for its majestic foliage, which provided shelter to Robin Hood and his band of merry men and women.

The Major Oak grows $i^i$ new leaves in the $i$-th year. It starts with $1$ leaf in year $1$.

Leaves last for $k$ years on the tree. In other words, leaves grown in year $i$ last between years $i$ and $i+k-1$ inclusive.

Robin considers even numbers lucky. Help Robin determine whether the Major Oak will have an even number of leaves in year $n$.

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

Each test case consists of two integers $n$, $k$ ($1 \le n \le 10^9$, $1 \le k \le n$) — the requested year and the number of years during which the leaves remain.

Output Format:
For each test case, output one line, "YES" if in year $n$ the Major Oak will have an even number of leaves and "NO" 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:
['math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
In Sherwood, we judge a man not by his wealth, but by his merit.

Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood!

There are $n$ people living in the town. Just now, the wealth of the $i$-th person was $a_i$ gold. But guess what? The richest person has found an extra pot of gold!

More formally, find an $a_j=max(a_1, a_2, \dots, a_n)$, change $a_j$ to $a_j+x$, where $x$ is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.

A person is unhappy if their wealth is strictly less than half of the average wealth$^{\text{∗}}$.

If strictly more than half of the total population $n$ are unhappy, Robin Hood will appear by popular demand.

Determine the minimum value of $x$ for Robin Hood to appear, or output $-1$ if it is impossible.

$^{\text{∗}}$The average wealth is defined as the total wealth divided by the total population $n$, that is, $\frac{\sum a_i}{n}$, the result is a real number.

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

The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot10^5$) — the total population.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$) — the wealth of each person.

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

Output Format:
For each test case, output one integer — the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output $-1$ instead.

Tags:
['binary search', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Impress thy brother, yet fret not thy mother.

Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor.

All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of those $d$ days must be between day $1$ and $n$ inclusive.

Robin has a total of $k$ risky 'jobs' planned. The $i$-th job takes place between days $l_i$ and $r_i$ inclusive, for $1 \le i \le k$. If a job takes place on any of the $d$ days, the visit overlaps with this job (the length of overlap is unimportant).

Robin wants his brother's visit to overlap with the maximum number of distinct jobs, and his mother's the minimum.

Find suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one.

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 consists of three integers $n$, $d$, $k$ ($1 \le n \le 10^5, 1 \le d, k \le n$) — the number of total days, duration of the visits, and the number of jobs.

Then follow $k$ lines of each test case, each with two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the start and end day of each job.

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 two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $1$ and $n$ inclusive.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
In the humble act of meeting, joy doth unfold like a flower in bloom.

Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn't wait to meet, so they both start without delay.

The travel network is represented as $n$ vertices numbered from $1$ to $n$ and $m$ edges. The $i$-th edge connects vertices $u_i$ and $v_i$, and takes $w_i$ seconds to travel (all $w_i$ are even). Marian starts at vertex $1$ (Market) and Robin starts at vertex $n$ (Major Oak).

In addition, $h$ of the $n$ vertices each has a single horse available. Both Marian and Robin are capable riders, and could mount horses in no time (i.e. in $0$ seconds). Travel times are halved when riding. Once mounted, a horse lasts the remainder of the travel. Meeting must take place on a vertex (i.e. not on an edge). Either could choose to wait on any vertex.

Output the earliest time Robin and Marian can meet. If vertices $1$ and $n$ are disconnected, output $-1$ as the meeting is cancelled.

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 consists of three integers $n$, $m$, $h$ ($2 \le n \le 2 \cdot 10^5, \;1 \le m \le 2 \cdot 10^5, \; 1 \le h \le n$) — the number of vertices, the number of edges and the number of vertices that have a single horse.

The second line of each test case contains $h$ distinct integers $a_1, a_2, \ldots, a_h$ ($1 \le a_i \le n$) — the vertices that have a single horse available.

Then follow $m$ lines of each test case, each with three integers $u_i$, $v_i$, $w_i$ ($1\le u_i,v_i \le n, \; 2\le w_i \le 10^6$) — meaning that there is an edge between vertices $u_i$ and $v_i$ with travel cost $w_i$ seconds without a horse.

There are no self loops or multiple edges. By good fortune, all $w_i$ are even integers.

It is guaranteed that the sums of both $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer, the earliest time Robin and Marian can meet. If it is impossible for them to meet, output $-1$.

Tags:
['dfs and similar', 'graphs', 'shortest paths']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold."

The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared.

Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $n$ camps numbered from $1$ to $n$ and $n-1$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $i$ has initially $a_i$ gold.

As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $c$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold.

After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed.

What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally?

Camp $a$ is neighboring camp $b$ if and only if there exists a trail connecting $a$ and $b$. Only strengthened camps count towards the answer, as others are destroyed.

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

Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) — the number of camps and the gold taken from each neighboring camp for strengthening.

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

Then follow $n-1$ lines, each with integers $u$, $v$ ($1 \le u, v \le n$, $u \ne v$) — meaning that there is a trail between $u$ and $v$.

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

It is guaranteed that any camp is reachable from any other camp.

Output Format:
Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack.

Tags:
['dfs and similar', 'dp', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
What is done is done, and the spoilt milk cannot be helped.

Little John is as little as night is day — he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk.

His dairy diary has $n$ entries, showing that he acquired $a_i$ pints of fresh milk on day $d_i$. Milk declines in freshness with time and stays drinkable for a maximum of $k$ days. In other words, fresh milk acquired on day $d_i$ will be drinkable between days $d_i$ and $d_i+k-1$ inclusive.

Every day, Little John drinks drinkable milk, up to a maximum of $m$ pints. In other words, if there are less than $m$ pints of milk, he will drink them all and not be satisfied; if there are at least $m$ pints of milk, he will drink exactly $m$ pints and be satisfied, and it's a milk satisfaction day.

Little John always drinks the freshest drinkable milk first.

Determine the number of milk satisfaction days for Little John.

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 consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness.

Then follow $n$ lines of each test case, each with two integers $d_i$ and $a_i$ ($1\le d_i$, $a_i \le 10^6$), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of $d_i$, and all values of $d_i$ are distinct.

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 milk satisfaction days.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
At such times archery was always the main sport of the day, for the Nottinghamshire yeomen were the best hand at the longbow in all merry England, but this year the Sheriff hesitated...

Sheriff of Nottingham has organized a tournament in archery. It's the final round and Robin Hood is playing against Sheriff!

There are $n$ targets in a row numbered from $1$ to $n$. When a player shoots target $i$, their score increases by $a_i$ and the target $i$ is destroyed. The game consists of turns and players alternate between whose turn it is. Robin Hood always starts the game, then Sheriff and so on. The game continues until all targets are destroyed. Both players start with score $0$.

At the end of the game, the player with most score wins and the other player loses. If both players have the same score, it's a tie and no one wins or loses. In each turn, the player can shoot any target that wasn't shot before. Both play optimally to get the most score possible.

Sheriff of Nottingham has a suspicion that he might lose the game! This cannot happen, you must help Sheriff. Sheriff will pose $q$ queries, each specifying $l$ and $r$. This means that the game would be played only with targets $l, l+1, \dots, r$, as others would be removed by Sheriff before the game starts.

For each query $l$, $r$, determine whether the Sheriff can not lose the game when only considering the targets $l, l+1, \dots, r$.

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

The first line of each test case contains two integers $n$, $q$ ($1 \le n,q \le 2\cdot10^5$) — the number of targets and the queries Sheriff will pose.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$) — the points for hitting each target.

Then follow $q$ lines, each with two integers $l$ and $r$ ($1 \le l \le r \le n$) — the range of the targets that is considered for each query.

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

Output Format:
For each query, output "YES", if the Sheriff does not lose the game when only considering the targets $l, l+1, \dots, r$, and "NO" 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:
['data structures', 'divide and conquer', 'greedy', 'hashing']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)

You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.

There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive.

After buying the new cards, you must partition all your cards into decks, according to the following rules:

  * all the decks must have the same size;    * there are no pairs of cards with the same value in the same deck.

Find the maximum possible size of a deck after buying cards and partitioning them optimally.

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$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) — the number of distinct types of cards and the number of coins.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) — the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$.

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 maximum possible size of a deck if you operate optimally.

Tags:
['2-sat', 'brute force', 'greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)

There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.

  * At time $1$, you conquer exactly one city, called the starting city.    * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.

You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?

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

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the deadlines for conquering the cities.

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 starting cities that allow you to win.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)

You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.

In one operation, you can remove a leaf and the edge adjacent to it (possibly, new leaves appear). What is the minimum number of operations that you have to perform to get a tree, also rooted at node $1$, where all the leaves are at the same distance from the root?

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$ ($3 \leq n \leq 5 \cdot 10^5$) — the number of nodes.

Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $u$ and $v$. 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 $5 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the minimum number of operations needed to achieve your goal.

Tags:
['brute force', 'dfs and similar', 'greedy', 'sortings', 'trees']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)

You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.

You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red).

Your score is the maximum value of a red element, plus the minimum value of a red element, plus the number of red elements. Find the maximum score you can get.

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$ ($1 \le n \le 2 \cdot 10^5$) — the length 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 given 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 maximum possible score you can get after coloring some elements red according to the statement.

Tags:
['data structures', 'dp', 'dsu', 'greedy', 'implementation', 'matrices', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex)

This is the easy version of the problem. In this version, the constraints on $n$ and the time limit are lower. You can make hacks only if both versions of the problem are solved.

A set of (closed) segments is complex if it can be partitioned into some subsets such that

  * all the subsets have the same size; and    * a pair of segments intersects if and only if the two segments are in the same subset.

You are given $n$ segments $[l_1, r_1], [l_2, r_2], \ldots, [l_n, r_n]$. Find the maximum size of a complex subset of these segments.

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

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

The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2n$) — the left endpoints of the segments.

The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i \leq r_i \le 2n$) — the right endpoints of the segments.

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

Output Format:
For each test case, output a single integer: the maximum size of a complex subset of the given segments.

Tags:
['binary search', 'data structures', 'divide and conquer', 'dsu', 'greedy', 'math', 'sortings']

Time Limit: 6000 ms
Memory Limit: 256 MB


Question:
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex)

This is the hard version of the problem. In this version, the constraints on $n$ and the time limit are higher. You can make hacks only if both versions of the problem are solved.

A set of (closed) segments is complex if it can be partitioned into some subsets such that

  * all the subsets have the same size; and    * a pair of segments intersects if and only if the two segments are in the same subset.

You are given $n$ segments $[l_1, r_1], [l_2, r_2], \ldots, [l_n, r_n]$. Find the maximum size of a complex subset of these segments.

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

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

The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2n$) — the left endpoints of the segments.

The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i \leq r_i \le 2n$) — the right endpoints of the segments.

It is guaranteed that 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 size of a complex subset of the given segments.

Tags:
['binary search', 'data structures', 'divide and conquer', 'dsu', 'greedy', 'math', 'sortings']

Time Limit: 13000 ms
Memory Limit: 256 MB