Question:
Sparkle gives you two arrays $a$ and $b$ of length $n$. Initially, your score is $0$. In one operation, you can choose an integer $i$ and add $a_i$ to your score. Then, you must set $a_i$ = $\max(0, a_i - b_i)$.

You only have time to perform $k$ operations before Sparkle sets off a nuclear bomb! What is the maximum score you can acquire after $k$ operations?

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

The first line of each test case contains $n$ and $k$ ($1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9$) — the length of the arrays and the number of operations you can perform.

The following line contains $n$ integers $a_1, a_2, ... a_n$ ($1 \leq a_i \leq 10^9$).

The following line contains $n$ integers $b_1, b_2, ... b_n$ ($1 \leq b_i \leq 10^9$).

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

Output Format:
For each test case, output an integer, the maximum score you can acquire after $k$ operations.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
On Penacony, The Land of the Dreams, there exists $n$ houses and $n$ roads. There exists a road between house $i$ and $i+1$ for all $1 \leq i \leq n-1$ and a road between house $n$ and house $1$. All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads.

There are $m$ pairs of friendships between the residents of Penacony. If the resident living in house $a$ is friends with the resident living in house $b$, there must be a path between houses $a$ and $b$ through maintained roads.

What is the minimum number of roads that must be maintained?

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

The first line of each test case contains two integers $n$ and $m$ ($3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$) – the number of houses and the number of friendships.

The next $m$ lines contain two integers $a$ and $b$ ($1 \leq a < b \leq n$) – the resident in house $a$ is friends with the resident in house $b$. It is guaranteed all ($a, b$) are distinct.

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

Output Format:
For each test case, output an integer, the minimum number of roads that must be maintained.

Tags:
['brute force', 'data structures', 'graphs', 'greedy', 'hashing']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Monocarp's current password on Codeforces is a string $s$, consisting of lowercase Latin letters. Monocarp thinks that his current password is too weak, so he wants to insert exactly one lowercase Latin letter into the password to make it stronger. Monocarp can choose any letter and insert it anywhere, even before the first character or after the last character.

Monocarp thinks that the password's strength is proportional to the time it takes him to type the password. The time it takes to type the password is calculated as follows:

  * the time to type the first character is $2$ seconds;    * for each character other than the first, the time it takes to type it is $1$ second if it is the same as the previous character, or $2$ seconds otherwise.

For example, the time it takes to type the password abacaba is $14$; the time it takes to type the password a is $2$; the time it takes to type the password aaabacc is $11$.

You have to help Monocarp — insert a lowercase Latin letter into his password so that the resulting password takes the maximum possible amount of time to type.

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

Each test case consists of one line containing the string $s$ ($1 \le |s| \le 10$), consisting of lowercase Latin letters.

Output Format:
For each test case, print one line containing the new password — a string which can be obtained from $s$ by inserting one lowercase Latin letter. The string you print should have the maximum possible required time to type it. If there are multiple answers, print any of them.

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

Time Limit: 2000 ms
Memory Limit: 512 MB

{"inputs": ["4\na\naaa\nabb\npassword"], "outputs": ["wa\naada\nabcb\npastsword"]}


Question:
There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked.

A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds:

  * $x$ and $y$ share a side;    * there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$.

A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.

For example, consider the following layout, where white cells are free, and dark grey cells are blocked:

![](CDN_BASE_URL/35b42e4e3c64eee3071df3d7b48861e8)

There are $3$ regions in it, denoted with red, green and blue color respectively:

![](CDN_BASE_URL/b2528153b76de41b1afcd49c1578a191)

The given grid contains at most $1$ connected region. Your task is to calculate the number of free cells meeting the following constraint:

  * if this cell is blocked, the number of connected regions becomes exactly $3$.

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 columns.

The $i$-th of the next two lines contains a description of the $i$-th row of the grid — the string $s_i$, consisting of $n$ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell).

Additional constraint on the input:

  * the given grid contains at most $1$ connected region;    * 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 number of cells such that the number of connected regions becomes $3$ if this cell is blocked.

Tags:
['constructive algorithms', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Monocarp had a regular bracket sequence $s$ of length $n$ ($n$ is even). He even came up with his own way to calculate its cost.

He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs.

For example, let's look at RBS (())(). It has three pairs of brackets:

  * (__)__: the distance between brackets at position $1$ and at $4$ is $4 - 1 = 3$;    * _()___: the distance is $3 - 2 = 1$;    * ____(): the distance is $6 - 5 = 1$.

So the cost of (())() is $3 + 1 + 1 = 5$.

Unfortunately, due to data corruption, Monocarp lost all characters on odd positions $s_1, s_3, \dots, s_{n-1}$. Only characters on even positions ($s_2, s_4, \dots, s_{n}$) remain. For example, (())() turned to _(_)_).

Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him?

Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not.

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

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$; $n$ is even) — the length of string $s$.

The second line of each test case contains a string $s$ of length $n$, where all characters on the odd positions are '_' and all characters on the even positions are either '(' or ')'.

Additional constraints:

  * $s$ can be restored to at least one regular bracket sequence;    * the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, print one integer — the minimum cost of the regular bracket sequence that can be obtained from $s$ by replacing '_'-s with brackets.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a rooted tree, consisting of $n$ vertices. The vertices in the tree are numbered from $1$ to $n$, and the root is the vertex $1$. The value $a_i$ is written at the $i$-th vertex.

You can perform the following operation any number of times (possibly zero): choose a vertex $v$ which has at least one child; increase $a_v$ by $1$; and decrease $a_u$ by $1$ for all vertices $u$ that are in the subtree of $v$ (except $v$ itself). However, after each operation, the values on all vertices should be non-negative.

Your task is to calculate the maximum possible value written at the root using the aforementioned operation.

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 vertices in the tree.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$) — the initial values written at vertices.

The third line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the parent of the $i$-th vertex in the tree. Vertex $1$ is the root.

Additional constraint on the input: 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 maximum possible value written at the root using the aforementioned operation.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Monocarp is playing a computer game. He starts the game being level $1$. He is about to fight $n$ monsters, in order from $1$ to $n$. The level of the $i$-th monster is $a_i$.

For each monster in the given order, Monocarp's encounter goes as follows:

  * if Monocarp's level is strictly higher than the monster's level, the monster flees (runs away);    * otherwise, Monocarp fights the monster.

After every $k$-th fight with a monster (fleeing monsters do not count), Monocarp's level increases by $1$. So, his level becomes $2$ after $k$ monsters he fights, $3$ after $2k$ monsters, $4$ after $3k$ monsters, and so on.

You need to process $q$ queries of the following form:

  * $i~x$: will Monocarp fight the $i$-th monster (or will this monster flee) if the parameter $k$ is equal to $x$?

Input Format:
The first line contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) — the number of monsters and the number of queries.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — the levels of the monsters.

In the $j$-th of the following $q$ lines, two integers $i$ and $x$ ($1 \le i, x \le n$) — the index of the monster and the number of fights required for a level up in the $j$-th query.

Output Format:
For each query, output "YES", if Monocarp will fight the $i$-th monster in this query, and "NO", if the $i$-th monster flees.

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
You have $n$ chips, and you are going to place all of them in one of $x$ points, numbered from $1$ to $x$. There can be multiple chips in each point.

After placing the chips, you can perform the following four operations (in any order, any number of times):

  * choose a chip in point $i \ge 3$, remove it and place two chips: one in $i-1$, one in $i-2$;    * choose two chips in adjacent points $i$ and $i+1$, remove them and place a new chip in $i+2$;    * choose a chip in point $1$ and move it to $2$;    * choose a chip in point $2$ and move it to $1$.

Note that the coordinates of the chips you place during the operations cannot be less than $1$, but can be greater than $x$.

Denote the cost of chip placement as the minimum number of chips which can be present on the line after you perform these operations, starting from the placement you've chosen.

For example, the cost of placing two chips in points $3$ and one chip in point $5$ is $2$, because you can reduce the number of chips to $2$ as follows:

  * choose a chip in point $3$, remove it, place a chip in $1$ and another chip in $2$;    * choose the chips in points $2$ and $3$, remove them and place a chip in $4$;    * choose the chips in points $4$ and $5$, remove them and place a chip in $6$.

You are given three integers $n$, $x$ and $m$. Calculate the number of placements of exactly $n$ chips in points from $1$ to $x$ having cost equal to $m$, and print it modulo $998244353$. Two placements are considered different if the number of chips in some point differs between these placements.

Input Format:
The only line contains three integers $n$, $x$ and $m$ ($1 \le m \le n \le 1000$; $2 \le x \le 10$).

Output Format:
Print one integer — the number of placements with cost equal to $m$, taken modulo $998244353$.

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

Time Limit: 5000 ms
Memory Limit: 512 MB


Question:
I couldn't think of a good title for this problem, so I decided to learn from LeetCode.

— Sun Tzu, The Art of War

You are given three integers $x_c$, $y_c$, and $k$ ($-100 \leq x_c, y_c \leq 100$, $1 \leq k \leq 1000$).

You need to find $k$ distinct points ($x_1, y_1$), ($x_2, y_2$), $\ldots$, ($x_k, y_k$), having integer coordinates, on the 2D coordinate plane such that:

  * their center$^{\text{∗}}$ is ($x_c, y_c$)    * $-10^9 \leq x_i, y_i \leq 10^9$ for all $i$ from $1$ to $k$

It can be proven that at least one set of $k$ distinct points always exists that satisfies these conditions.

$^{\text{∗}}$The center of $k$ points ($x_1, y_1$), ($x_2, y_2$), $\ldots$, ($x_k, y_k$) is $\left( \frac{x_1 + x_2 + \ldots + x_k}{k}, \frac{y_1 + y_2 + \ldots + y_k}{k} \right)$.

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

Each test case contains three integers $x_c$, $y_c$, and $k$ ($-100 \leq x_c, y_c \leq 100$, $1 \leq k \leq 1000$) — the coordinates of the center and the number of distinct points you must output.

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

Output Format:
For each test case, output $k$ lines, the $i$-th line containing two space separated integers, $x_i$ and $y_i$, ($-10^9 \leq x_i, y_i \leq 10^9$) — denoting the position of the $i$-th point.

If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constraints.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
It is known that [Farmer John likes Permutations](https://usaco.org/index.php?page=viewproblem2&cpid=1421), but I like them too!

— Sun Tzu, The Art of Constructing Permutations

You are given a permutation$^{\text{∗}}$ $p$ of length $n$.

Find a permutation $q$ of length $n$ that minimizes the number of pairs ($i, j$) ($1 \leq i \leq j \leq n$) such that $p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$.

$^{\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 ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

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

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

The following line contains $n$ space-separated integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) — denoting the permutation $p$ of length $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 one line containing any permutation of length $n$ (the permutation $q$) such that $q$ minimizes the number of pairs.

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

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you.

— satyam343's biggest fan

You are given an array $a$ of length $n$ and an integer $k$. You are also given a binary array $b$ of length $n$.

You can perform the following operation at most $k$ times:

  * Select an index $i$ ($1 \leq i \leq n$) such that $b_i = 1$. Set $a_i = a_i + 1$ (i.e., increase $a_i$ by $1$).

Your score is defined to be $\max\limits_{i = 1}^{n} \left( a_i + \operatorname{median}(c_i) \right)$, where $c_i$ denotes the array of length $n-1$ that you get by deleting $a_i$ from $a$. In other words, your score is the maximum value of $a_i + \operatorname{median}(c_i)$ over all $i$ from $1$ to $n$.

Find the maximum score that you can achieve if you perform the operations optimally.

For an arbitrary array $p$, $\operatorname{median}(p)$ is defined as the $\left\lfloor \frac{|p|+1}{2} \right\rfloor$-th smallest element of $p$. For example, $\operatorname{median} \left( [3,2,1,3] \right) = 2$ and $\operatorname{median} \left( [6,2,4,5,1] \right) = 4$.

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

Each test case begins with two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^9$) — the length of the $a$ and the number of operations you can perform.

The following line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — denoting the array $a$.

The following line contains $n$ space separated integers $b_1, b_2, \ldots, b_n$ ($b_i$ is $0$ or $1$) — denoting the array $b$.

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 the maximum value of score you can get on a new line.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
MOOOOOOOOOOOOOOOOO

— Bessie the Cow, The Art of Racing on Islands

Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for all $1 \leq i \leq n - 1$. Additionally, there are $m$ alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index.

Initially, Elsie starts on island $1$, and Bessie starts on island $s$. The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island $i$. During a cow's turn, if there are any bridges connecting island $i$ to island $j$, then the cow can move to island $j$. Then, island $i$ collapses, and all bridges connecting to island $i$ also collapse. Also, note the following:

  * If there are no bridges connecting island $i$ to another island, then island $i$ collapses, and this cow is eliminated from the race.    * If the other cow is also on island $i$, then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race.    * After an island or bridge collapses, no cows may use them.    * If a cow is eliminated, their turn is skipped for the rest of the race.

The race ends once either cow reaches island $n$. It can be shown that regardless of the cows' strategies, at least one cow reaches island $n$. Bessie wins if and only if she reaches island $n$ first.

For each $1 \leq s \leq n - 1$, determine whether Bessie wins if she starts the race on island $s$. Assume both cows follow optimal strategies to ensure their own respective victories.

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

The first line of each test case contains $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq 2 \cdot 10^5$) – the number of islands and the number of alternative bridges.

The next $m$ lines of each test case contain $u$ and $v$ ($1 \leq u < v \leq n$) – the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges.

It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^5$.

Output Format:
For each test case, output a binary string of length $n - 1$ on a new line. The $i$'th character is $1$ if it is possible for Bessie to win if she starts on island $i$. Otherwise, it is $0$.

Tags:
['data structures', 'dp', 'graphs', 'greedy', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Drink water.

— Sun Tzu, The Art of Becoming a Healthy Programmer

This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack.

You are given two integers $n$ and $x$ ($x=n$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball.

For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows:

  * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$.

  * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$.

    * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.      * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.      * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$:        * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.        * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.

  * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times.

For each integer $i$ from $x$ to $n$, you need to find $f(i)$.

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

The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = n$) — the number of balls and the smallest index $i$ for which you need to find $f(i)$.

The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the initial number written on each ball.

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 $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.

Tags:
['binary search', 'brute force', 'data structures', 'divide and conquer', 'greedy']

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
Drink water.

— Sun Tzu, The Art of Becoming a Healthy Programmer

This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack.

You are given two integers $n$ and $x$ ($x=1$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball.

For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows:

  * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$.

  * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$.

    * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.      * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.      * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$:        * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.        * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.

  * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times.

For each integer $i$ from $x$ to $n$, you need to find $f(i)$.

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

The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = 1$) — the number of balls and the smallest index $i$ for which you need to find $f(i)$.

The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the initial number written on each ball.

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 $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
Given a two-digit positive integer $n$, find the sum of its digits.

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

The only line of each test case contains a single two-digit positive integer $n$ ($10 \leq n \leq 99$).

Output Format:
For each test case, output a single integer — the sum of the digits of $n$.

Tags:
['implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Suneet and Slavic play a card game. The rules of the game are as follows:

  * Each card has an integer value between $1$ and $10$.    * Each player receives $2$ cards which are face-down (so a player doesn't know their cards).    * The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round.    * A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game.

Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner.

For a better understanding, please check the notes section.

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

The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic has, respectively.

Output Format:
For each test case, output a single integer — the number of games Suneet would win considering all possible games.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
As a computer science student, Alex faces a hard challenge — showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes!

He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $l_i$ and $r_i$). No two tasks overlap.

Given all $n$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $s$?

![](CDN_BASE_URL/5d5195053b99e5c6936ccefadc239679)

In the first test case, Alex can shower for the first $3$ minutes of the day and not miss any of the tasks.

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

The first line of each test case contains three integers $n$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) — the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.

Then $n$ lines follow, the $i$-th of which contains two integers $l_i$ and $r_i$ ($0 \leq l_i < r_i \leq m$) — the time interval of the $i$-th task. No two tasks overlap.

Additional constraint on the input: $l_i > r_{i-1}$ for every $i > 1$.

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

Output Format:
For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise.

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

Tags:
['greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Slavic has a very tough exam and needs your help in order to pass it. Here is the question he is struggling with:

There exists a string $s$, which consists of lowercase English letters and possibly zero or more "?".

Slavic is asked to change each "?" to a lowercase English letter such that string $t$ becomes a subsequence (not necessarily continuous) of the string $s$.

Output any such string, or say that it is impossible in case no string that respects the conditions exists.

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

The first line of each test case contains a single string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$, and $s$ consists only of lowercase English letters and "?"-s)  – the original string you have.

The second line of each test case contains a single string $t$ ($1 \leq |t| \leq |s|$, and $t$ consists only of lowercase English letters)  – the string that should be a subsequence of string $s$.

The sum of $|s|$ over all test cases doesn't exceed $2 \cdot 10^5$, where $|x|$ denotes the length of the string $x$.

Output Format:
For each test case, if no such string exists as described in the statement, output "NO" (without quotes).

Otherwise, output "YES" (without quotes). Then, output one line — the string that respects all conditions.

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

If multiple answers are possible, you can output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
On the board Ivy wrote down all integers from $l$ to $r$, inclusive.

In an operation, she does the following:

  * pick two numbers $x$ and $y$ on the board, erase them, and in their place write the numbers $3x$ and $\lfloor \frac{y}{3} \rfloor$. (Here $\lfloor \bullet \rfloor$ denotes rounding down to the nearest integer).

What is the minimum number of operations Ivy needs to make all numbers on the board equal $0$? We have a proof that this is always possible.

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

The only line of each test case contains two integers $l$ and $r$ ($1 \leq l < r \leq 2 \cdot 10^5$).

Output Format:
For each test case, output a single integer — the minimum number of operations needed to make all numbers on the board equal $0$.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Arul has a binary array$^{\text{∗}}$ $a$ of length $n$.

He will take all subsequences$^{\text{†}}$ of length $k$ ($k$ is odd) of this array and find their median.$^{\text{‡}}$

What is the sum of all these values?

As this sum can be very large, output it modulo $10^9 + 7$. In other words, print the remainder of this sum when divided by $10^9 + 7$.

$^{\text{∗}}$A binary array is an array consisting only of zeros and ones.

$^{\text{†}}$An array $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements. Subsequences don't have to be contiguous.

$^{\text{‡}}$The median of an array of odd length $k$ is the $\frac{k+1}{2}$-th element when sorted.

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

The first line of each test case contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$, $k$ is odd) — the length of the array and the length of the subsequence, respectively.

The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 1$) — the elements of the array.

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

Output Format:
For each test case, print the sum modulo $10^9 + 7$.

Tags:
['combinatorics', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{10}$ queries.

This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307).

We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values:

  * If $y < x$, the ruler (correctly) measures the object as having length $y$.    * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$.

![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8)

The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$.

You need to find the value of $x$. To do that, you can make queries of the following form:

  * $\texttt{?}~a~b$ — in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you.

Find the value of $x$. You can ask at most $\mathbf{10}$ queries.

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

Output Format:

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{7}$ queries.

This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307).

We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values:

  * If $y < x$, the ruler (correctly) measures the object as having length $y$.    * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$.

![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8)

The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$.

You need to find the value of $x$. To do that, you can make queries of the following form:

  * $\texttt{?}~a~b$ — in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you.

Find the value of $x$. You can ask at most $\mathbf{7}$ queries.

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

Output Format:

Tags:
['binary search', 'interactive', 'ternary search']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad.

The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' when writing the important integer. For example, instead of the integer $10^5$, he would have written $105$, and instead of $10^{19}$, he would have written $1019$.

Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.

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

The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) — the next integer from the board.

Output Format:
For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise.

You may 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:
['implementation', 'math', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules:

  * If there are no occupied seats in the bus, a passenger can sit in any free seat;   * Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index $i$ ($1 \le i \le n$) only if at least one of the seats with indices $i-1$ or $i+1$ is occupied.

Today, $n$ passengers boarded the bus. The array $a$ chronologically records the seat numbers they occupied. That is, $a_1$ contains the seat number where the first passenger sat, $a_2$ — the seat number where the second passenger sat, and so on.

You know the contents of the array $a$. Determine whether all passengers followed the recommendations.

For example, if $n = 5$, and $a$ = [$5, 4, 2, 1, 3$], then the recommendations were not followed, as the $3$-rd passenger sat in seat number $2$, while the neighboring seats with numbers $1$ and $3$ were free.

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

The following describes the input test cases.

The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of seats in the bus and the number of passengers who boarded the bus.

The second line of each test case contains $n$ distinct integers $a_i$ ($1 \le a_i \le n$) — the seats that the passengers occupied in chronological order.

It is guaranteed that the sum of $n$ values across all test cases does not exceed $2 \cdot 10^5$, and that no passenger sits in an already occupied seat.

Output Format:
For each test case, output on a separate line:

  * "YES", if all passengers followed the recommendations;    * "NO" otherwise.

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

Tags:
['two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Kristina has an array $a$, called a template, consisting of $n$ integers. She also has $m$ strings, each consisting only of lowercase Latin letters. The strings are numbered from $1$ to $m$. She wants to check which strings match the template.

A string $s$ is considered to match the template if all of the following conditions are simultaneously satisfied:

  * The length of the string $s$ is equal to the number of elements in the array $a$.   * The same numbers from $a$ correspond to the same symbols from $s$. So, if $a_i = a_j$, then $s_i = s_j$ for ($1 \le i, j \le n$).   * The same symbols from $s$ correspond to the same numbers from $a$. So, if $s_i = s_j$, then $a_i = a_j$ for ($1 \le i, j \le n$).

In other words, there must be a one-to-one correspondence between the characters of the string and the elements of the array.

For example, if $a$ = [$3, 5, 2, 1, 3$], then the string "abfda" matches the template, while the string "afbfa" does not, since the character "f" corresponds to both numbers $1$ and $5$.

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

The following descriptions are for the 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 $a$.

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

The third line of each test case contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of strings to check for template matching.

Following are $m$ strings, each containing a non-empty string $s_j$ ($1 \le |s_j| \le 2 \cdot 10^5$), consisting of lowercase Latin letters.

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

Output Format:
For each test case, output $m$ lines. On the $i$-th line ($1 \le i \le m$) output:

  * "YES", if the string with index $i$ matches the template;    * "NO" otherwise.

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["3\n5\n3 5 2 1 3\n2\nabfda\nafbfa\n2\n1 2\n3\nab\nabc\naa\n4\n5 -3 5 -3\n4\naaaa\nbcbc\naba\ncbcb"], "outputs": ["YES\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nYES"]}


Question:
Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'.

Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.

In one operation, you can choose two indices $l$ and $r$ ($1 \le l < r \le n$) such that $s_l$ = 'L' and $s_r$ = 'R' and do the following:

  * add $a_l + a_{l + 1} + \dots + a_{r - 1} + a_r$ points to the current score;    * replace $s_i$ with '.' for all $l \le i \le r$, meaning you can no longer choose these indices.

For example, consider the following strip:

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   L| R| L| L| L| R      You can first choose $l = 1$, $r = 2$ and add $3 + 5 = 8$ to your score.

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   .| .| L| L| L| R      Then choose $l = 3$, $r = 6$ and add $1 + 4 + 3 + 2 = 10$ to your score.

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   .| .| .| .| .| .      As a result, it is impossible to perform another operation, and the final score is $18$.

What is the maximum score that can be achieved?

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

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) — the numbers written on the strip.

The third line of each test case contains a string $s$ of $n$ characters 'L' and 'R'.

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

Output Format:
For each test case, output one integer — the maximum possible number of points that can be scored.

Tags:
['greedy', 'implementation', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You really love gorillas, so you decided to organize a photoshoot for them. Gorillas live in the jungle. The jungle is represented as a grid of $n$ rows and $m$ columns. $w$ gorillas agreed to participate in the photoshoot, and the gorilla with index $i$ ($1 \le i \le w$) has a height of $a_i$. You want to place all the gorillas in the cells of the grid such that there is no more than one gorilla in each cell.

The spectacle of the arrangement is equal to the sum of the spectacles of all sub-squares of the grid with a side length of $k$.

The spectacle of a sub-square is equal to the sum of the heights of the gorillas in it.

From all suitable arrangements, choose the arrangement with the maximum spectacle.

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

The descriptions of the test cases follow.

The first line contains integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le n \cdot m \le 2 \cdot 10^5$, $1 \le k \le \min(n, m)$) — the dimensions of the grid and the side length of the square.

The second line contains an integer $w$ ($1 \le w \le n \cdot m$) — the number of gorillas.

The third line contains $w$ integers $a_1, a_2, \ldots, a_w$ ($1 \le a_i \le 10^9$) — the heights of the gorillas.

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

Output Format:
For each test case, output a single integer — the maximum spectacle of a suitable arrangement.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You have $n$ rectangles, the $i$-th of which has a width of $a_i$ and a height of $b_i$.

You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.

Each time you completely color any row or column, you earn $1$ point. Your task is to score at least $k$ points with as few operations as possible.

Suppose you have a rectangle with a width of $6$ and a height of $3$. You can score $4$ points by coloring all the cells in any $4$ columns, thus performing $12$ operations.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 100$) — the number of test cases. The following are the descriptions of the test cases.

The first line of each test case description contains two integers $n$ and $k$ ($1 \le n \le 1000, 1 \le k \le 100$) — the number of rectangles in the case and the required number of points.

The next $n$ lines contain the descriptions of the rectangles. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 100$) — the width and height of the $i$-th rectangle.

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

Output Format:
For each test case, output a single integer — the minimum number of operations required to score at least $k$ points. If it is impossible to score at least $k$ points, output -1.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections.

Every minute, you can board a bus at intersection $u_i$ and travel for $l_{i1}$ minutes to intersection $v_i$. Conversely, you can travel from intersection $v_i$ to intersection $u_i$ in $l_{i1}$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there.

You can also walk along each street, which takes $l_{i2} > l_{i1}$ minutes.

You can make stops at intersections.

You live at intersection number $1$. Today you woke up at time $0$, and you have an important event scheduled at intersection number $n$, which you must reach no later than time $t_0$. You also have a phone call planned that will last from $t_1$ to $t_2$ minutes ($t_1 < t_2 < t_0$).

During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $t_1$ and board the bus again at minute $t_2$.

Since you want to get enough sleep, you became curious — how late can you leave home to have time to talk on the phone and still not be late for the event?

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

The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) — the number of intersections and streets in the city.

The second line of each test case contains three integers $t_0$, $t_1$, $t_2$ ($1 \le t_1 < t_2 < t_0 \le 10^9$) — the start time of the event, the start time of the phone call, and its end time, respectively.

The next $m$ lines of each test case contain descriptions of the streets.

The $i$-th line contains four integers $u_i$, $v_i$, $l_{i1}$, $l_{i2}$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le l_{i1} < l_{i2} \le 10^9$) — the numbers of the intersections connected by the $i$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other.

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

Output Format:
For each test case, output a single integer — the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $n$ distinct integers $a_1, a_2, \ldots, a_n$.

The set will undergo $m$ operations sequentially. The operations can be of the following types:

  * Insert element $x$ into the set;    * Remove element $x$ from the set;    * Report the $k$-load of the set.

The $k$-load of the set is defined as the minimum positive integer $d$ such that the integers $d, d + 1, \ldots, d + (k - 1)$ do not appear in this set. For example, the $3$-load of the set $\\{3, 4, 6, 11\\}$ is $7$, since the integers $7, 8, 9$ are absent from the set, and no smaller value fits.

Ksyusha is busy with management tasks, so you will have to write the engine. Implement efficient support for the described operations.

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

The following lines describe the test cases.

The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the initial size of the set.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 2 \cdot 10^6$) — the initial state of the set.

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

The next $m$ lines contain the operations. The operations are given in the following format:

  * + $x$ ($1 \le x \le 2 \cdot 10^6$) — insert element $x$ into the set (it is guaranteed that $x$ is not in the set);    * - $x$ ($1 \le x \le 2 \cdot 10^6$) — remove element $x$ from the set (it is guaranteed that $x$ is in the set);    * ? $k$ ($1 \le k \le 2 \cdot 10^6$) — output the value of the $k$-load of the set.

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

Output Format:
For each test case, output the answers to the operations of type "?".

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

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
You are given a cyclic array $a_1, a_2, \ldots, a_n$.

You can perform the following operation on $a$ at most $n - 1$ times:

  * Let $m$ be the current size of $a$, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, $a_m$ and $a_1$ are adjacent and $a_m$ is the previous one), and delete exactly one of them. In other words, choose an integer $i$ ($1 \leq i \leq m$) where $a_i \leq a_{(i \bmod m) + 1}$ holds, and delete exactly one of $a_i$ or $a_{(i \bmod m) + 1}$ from $a$.

Your goal is to find the minimum number of operations needed to make all elements in $a$ equal.

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

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

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

Output Format:
For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in $a$ equal.

Tags:
['greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There is an integer sequence $a$ of length $n$, where each element is initially $-1$.

Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to $1$, and another writes letters from right to left with a pointer initially pointing to $n$.

Misuki would choose one of the typewriters and use it to perform the following operations until $a$ becomes a permutation of $[1, 2, \ldots, n]$

  * write number: write the minimum positive integer that isn't present in the array $a$ to the element $a_i$, $i$ is the position where the pointer points at. Such operation can be performed only when $a_i = -1$.    * carriage return: return the pointer to its initial position (i.e. $1$ for the first typewriter, $n$ for the second)    * move pointer: move the pointer to the next position, let $i$ be the position the pointer points at before this operation, if Misuki is using the first typewriter, $i := i + 1$ would happen, and $i := i - 1$ otherwise. Such operation can be performed only if after the operation, $1 \le i \le n$ holds.

Your task is to construct any permutation $p$ of length $n$, such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using.

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

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

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 line of $n$ integers, representing the permutation $p$ of length $n$ such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using, or $-1$ if it is impossible to do so.

If there are multiple valid permutations, you can output any of them.

Tags:
['constructive algorithms']

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
This is an interactive problem.

Misuki has chosen a secret tree with $n$ nodes, indexed from $1$ to $n$, and asked you to guess it by using queries of the following type:

  * "? a b" — Misuki will tell you which node $x$ minimizes $|d(a,x) - d(b,x)|$, where $d(x,y)$ is the distance between nodes $x$ and $y$. If more than one such node exists, Misuki will tell you the one which minimizes $d(a,x)$.

Find out the structure of Misuki's secret tree using at most $15n$ queries!

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

Each test case consists of a single line with an integer $n$ ($2 \le n \le 1000$), the number of nodes in the tree.

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

Output Format:

Tags:
['binary search', 'brute force', 'dfs and similar', 'divide and conquer', 'dsu', 'greedy', 'interactive', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given an integer sequence $a_1, a_2, \ldots, a_n$. Let $S$ be the set of all possible non-empty subsequences of $a$ without duplicate elements. Your goal is to find the longest sequence in $S$. If there are multiple of them, find the one that minimizes lexicographical order after multiplying terms at odd positions by $-1$.

For example, given $a = [3, 2, 3, 1]$, $S = \\{[1], [2], [3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 3, 1], [3, 2, 1]\\}$. Then $[2, 3, 1]$ and $[3, 2, 1]$ would be the longest, and $[3, 2, 1]$ would be the answer since $[-3, 2, -1]$ is lexicographically smaller than $[-2, 3, -1]$.

A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements.

A sequence $c$ is lexicographically smaller than a sequence $d$ if and only if one of the following holds:

  * $c$ is a prefix of $d$, but $c \ne d$;   * in the first position where $c$ and $d$ differ, the sequence $c$ has a smaller element than the corresponding element in $d$.

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

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

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

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 the answer in the following format:

Output an integer $m$ in the first line — the length of $b$.

Then output $m$ integers $b_1, b_2, \ldots, b_m$ in the second line — the sequence $b$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.

Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.

Define the operation $\mathrm{pop}$ as follows:

  1. initialize variable $v$ as $1$;    2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$);      1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them;      2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$);      3. assign $x$ to $v$ (i.e. $v := x$);    3. assign $-1$ to $a_v$ (i.e. $a_v := -1$).

Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.

A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.

A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first time.

Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:

  * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$.

Two heaps are considered different if there is a node which has different values in the heaps.

Since the answer might be large, print it modulo $p$.

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

The first line of each test case contains three integers $n, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).

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

Output Format:
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.

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

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.

Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.

Define the operation $\mathrm{pop}$ as follows:

  1. initialize variable $v$ as $1$;    2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$);      1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them;      2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$);      3. assign $x$ to $v$ (i.e. $v := x$);    3. assign $-1$ to $a_v$ (i.e. $a_v := -1$).

Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.

A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.

A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first and the second time.

Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:

  * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$.

Two heaps are considered different if there is a node which has different values in the heaps.

Since the answer might be large, print it modulo $p$.

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

The first line of each test case contains three integers $n, k, p$ ($2 \le n \le 100$, $1 \le k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).

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

Output Format:
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
You received an $n\times m$ grid from a mysterious source. The source also gave you a magic positive integer constant $k$.

The source told you to color the grid with some colors, satisfying the following condition:

  * If $(x_1,y_1)$, $(x_2,y_2)$ are two distinct cells with the same color, then $\max(|x_1-x_2|,|y_1-y_2|)\ge k$.

You don't like using too many colors. Please find the minimum number of colors needed to color the grid.

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

The only line of each test case consists of three positive integers $n$, $m$, $k$ ($1\le n,m,k\le10^4$) — the dimensions of the grid and the magic constant.

Output Format:
For each test case, print a single integer — the minimum number of colors needed to color the grid.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays.

In each turn, the following events happen in order:

  * Alice chooses either the first or the last element of her array and removes it from the array;    * Bob chooses either the first or the last element of his array and removes it from the array.

The game continues for $n-1$ turns, after which both arrays will have exactly one remaining element: $x$ in the array $a$ and $y$ in the array $b$.

If $x=y$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). 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 next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$ are distinct) — the permutation of Alice.

The next line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_i\le n$, all $b_i$ are distinct) — the permutation of Bob.

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

Output Format:
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$.

The circles' radii increase at a rate of $1$ unit per second.

You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circle (including the moment you reach $(x_t,y_t)$). You can move in any direction you want. However, your speed is limited to $1$ unit per second.

Please determine whether this is possible.

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

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

The next $n$ lines each contain two integers $x_i$, $y_i$ ($1\le x_i,y_i\le10^9$) — the center of each circle.

The final line contains four integers $x_s$, $y_s$, $x_t$, $y_t$ ($1\le x_s,y_s,x_t,y_t\le10^9$) — the coordinates of the starting point and the goal, respectively.

It is guaranteed that these $n+2$ points are distinct.

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

Output Format:
For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, and output $\texttt{NO}$ otherwise.

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved.

You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$.

You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\ddagger$ of the given tree.

Please note that the swaps are persistent through queries.

$^\dagger$ A perfect binary tree is a tree with vertex $1$ as its root, with size $n=2^k-1$ for a positive integer $k$, and where the parent of each vertex $i$ ($1<i\le n$) is $\left\lfloor\frac{i}{2}\right\rfloor$. Thus, all leaves of this tree are at a distance $k - 1$ from the root.

$^\ddagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree.

              dfs_order = []          function dfs(v):         append v to the back of dfs_order         pick an arbitrary permutation s of children of v         for child in s:             dfs(child)     dfs(1)

Note that the DFS order is not unique.

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

The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) — the number of vertices in the tree and the number of queries. It is guaranteed that $n=2^k-1$ for a positive integer $k$.

The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) — the parent of each vertex in the given tree. It is guaranteed that $a_i=\left\lfloor\frac{i}{2}\right\rfloor$.

The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) — the initial permutation $p$.

The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) — the positions of the elements to swap in the permutation.

It is guaranteed that the sum of all $n$ does not exceed $65\,535$, and the sum of all $q$ does not exceed $5 \cdot 10^4$.

Output Format:
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise.

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

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

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved.

You are given a rooted tree consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$.

You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\dagger$ of the given tree.

Please note that the swaps are persistent through queries.

$^\dagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree.

              dfs_order = []          function dfs(v):         append v to the back of dfs_order         pick an arbitrary permutation s of children of v         for child in s:             dfs(child)     dfs(1)

Note that the DFS order is not unique.

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

The first line of each test case contains two integers $n$, $q$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) — the number of vertices in the tree and the number of queries.

The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) — the parent of each vertex in the given tree.

The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) — the initial permutation $p$.

The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) — the positions of the elements to swap in the permutation.

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

Output Format:
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise.

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

Tags:
['binary search', 'data structures', 'dfs and similar', 'graphs', 'hashing', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$.

Define the strength of an array as the number of seconds it takes to become empty.

You are given an array of integers compressed in the form of $n$ pairs that describe the array left to right. Each pair $(a_i,b_i)$ represents $a_i$ copies of $b_i$, i.e. $\underbrace{b_i,b_i,\cdots,b_i}_{a_i\textrm{ times}}$.

For each $i=1,2,\dots,n$, please find the strength of the sequence described by the first $i$ pairs.

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

The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) — the length of sequence $a$.

The next $n$ lines contain two integers each $a_i$, $b_i$ ($1\le a_i\le10^9,0\le b_i\le n$) — the pairs which describe the sequence.

It is guaranteed that the sum of all $n$ does not exceed $3\cdot10^5$.

It is guaranteed that for all $1\le i<n$, $b_i\neq b_{i+1}$ holds.

Output Format:
For each test case, print one line containing $n$ integers — the answer for each prefix of pairs.

Tags:
['brute force', 'data structures', 'dp']

Time Limit: 2000 ms
Memory Limit: 512 MB


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

In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.

Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:

  * after every round, $\gcd(W_L,W_F)\le 1$;    * at the end of the match, $W_L\le n, W_F\le m$.

Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.

Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.

Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.

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

The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.

Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.

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

Tags:
['brute force', 'dfs and similar', 'dp', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved.

In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.

Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:

  * after every round, $\gcd(W_L,W_F)\le 1$;    * at the end of the match, $W_L\le n, W_F\le m$.

Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.

Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.

Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.

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

The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$): $n$, $m$ give the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.

Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.

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

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
Consider a grid graph with $n$ rows and $n$ columns. Let the cell in row $x$ and column $y$ be $(x,y)$. There exists a directed edge from $(x,y)$ to $(x+1,y)$, with non-negative integer value $d_{x,y}$, for all $1\le x < n, 1\le y \le n$, and there also exists a directed edge from $(x,y)$ to $(x,y+1)$, with non-negative integer value $r_{x,y}$, for all $1\le x \le n, 1\le y < n$.

Initially, you are at $(1,1)$, with an empty set $S$. You need to walk along the edges and eventually reach $(n,n)$. Whenever you pass an edge, its value will be inserted into $S$. Please maximize the MEX$^{\text{∗}}$ of $S$ when you reach $(n,n)$.

$^{\text{∗}}$The MEX (minimum excluded) of an array is the smallest non- negative integer that does not belong to the array. For instance:

  * The MEX of $[2,2,1]$ is $0$, because $0$ does not belong to the array.    * The MEX of $[3,1,0,1]$ is $2$, because $0$ and $1$ belong to the array, but $2$ does not.    * The MEX of $[0,3,1,2]$ is $4$, because $0, 1, 2$, and $3$ belong to the array, but $4$ does not.

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

The first line of each test case contains a single integer $n$ ($2\le n\le20$) — the number of rows and columns.

Each of the next $n-1$ lines contains $n$ integers separated by single spaces — the matrix $d$ ($0\le d_{x,y}\le 2n-2$).

Each of the next $n$ lines contains $n-1$ integers separated by single spaces — the matrix $r$ ($0\le r_{x,y}\le 2n-2$).

It is guaranteed that the sum of all $n^3$ does not exceed $8000$.

Output Format:
For each test case, print a single integer — the maximum MEX of $S$ when you reach $(n,n)$.

Tags:
['bitmasks', 'brute force', 'hashing', 'meet-in-the-middle']

Time Limit: 7000 ms
Memory Limit: 1024 MB


Question:
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind.

Your predecessors had taken their final bow; it's your turn to take the stage.

Sorting through some notes that were left behind, you found a curious statement named Problem 101:

  * Given a positive integer sequence $a_1,a_2,\ldots,a_n$, you can operate on it any number of times. In an operation, you choose three consecutive elements $a_i,a_{i+1},a_{i+2}$, and merge them into one element $\max(a_i+1,a_{i+1},a_{i+2}+1)$. Please calculate the maximum number of operations you can do without creating an element greater than $m$.

After some thought, you decided to propose the following problem, named Counting 101:

  * Given $n$ and $m$. For each $k=0,1,\ldots,\left\lfloor\frac{n-1}{2}\right\rfloor$, please find the number of integer sequences $a_1,a_2,\ldots,a_n$ with elements in $[1, m]$, such that when used as input for Problem 101, the answer is $k$. As the answer can be very large, please print it modulo $10^9+7$.

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

The only line of each test case contains two integers $n$, $m$ ($1\le n\le 130$, $1\le m\le 30$).

Output Format:
For each test case, output $\left\lfloor\frac{n+1}{2}\right\rfloor$ numbers. The $i$-th number is the number of valid sequences such that when used as input for Problem 101, the answer is $i-1$, modulo $10^9+7$.

Tags:
['greedy']

Time Limit: 10100 ms
Memory Limit: 1010 MB


Question:
Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that:

  * $k \ge 2$.    * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$.    * For all $1 \le i < j \le k$, the first character of $t_i$ isn't equal to the last character of $t_j$.

Turtle is given a string $s$ consisting of lowercase Latin letters. Please tell him whether the string $s$ is a good string!

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

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

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

Output Format:
For each test case, output "YES" if the string $s$ is a good string, 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:
['greedy', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB

{"inputs": ["4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc"], "outputs": ["No\nnO\nYes\nYES"]}


Question:
Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).

The game goes as follows:

  * Let the current length of the sequence be $m$. If $m = 1$, the game ends.    * If the game does not end and it's Turtle's turn, then Turtle must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\max(a_i, a_{i + 1})$, and remove $a_{i + 1}$.    * If the game does not end and it's Piggy's turn, then Piggy must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\min(a_i, a_{i + 1})$, and remove $a_{i + 1}$.

Turtle wants to maximize the value of $a_1$ in the end, while Piggy wants to minimize the value of $a_1$ in the end. Find the value of $a_1$ in the end if both players play optimally.

You can refer to notes for further clarification.

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

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

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

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

Output Format:
For each test case, output a single integer — the value of $a_1$ in the end if both players play optimally.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Turtle gives you a string $s$, consisting of lowercase Latin letters.

Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a pleasant pair if and only if there exists an integer $k$ such that $i \le k < j$ and both of the following two conditions hold:

  * $s_k \ne s_{k + 1}$;    * $s_k \ne s_i$ or $s_{k + 1} \ne s_j$.

Besides, Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a good pair if and only if $s_i = s_j$ or $(i, j)$ is a pleasant pair.

Turtle wants to reorder the string $s$ so that the number of good pairs is maximized. Please help him!

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

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

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

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 the string $s$ after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved.

One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$.

Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:

  * There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer.    * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$.    * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations.

Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$.

Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him!

$^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.

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

The first line of each test case contains two integers $n, m$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$).

Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence.

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

Output Format:
For each test case, output a single integer — the value of $\sum\limits_{i = 0}^m f(i)$.

Tags:
['greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
The two versions are different problems. In this version of the problem, you can't choose the same integer twice or more. You can make hacks only if both versions are solved.

One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$.

Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:

  * There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer.    * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$ and $i$ wasn't chosen before, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$.    * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations.

Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$.

Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him!

$^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.

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

The first line of each test case contains two integers $n, m$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$).

Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence.

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

Output Format:
For each test case, output a single integer — the value of $\sum\limits_{i = 0}^m f(i)$.

Tags:
['dfs and similar', 'dp', 'graphs', 'greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.

Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds:

$$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$

Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation.

An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.

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 two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le \frac{n}{2}$) — the length of the permutation and the number of intervals.

The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) — the $i$-th interval.

Additional constraint on the input in this version: $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$.

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

Output Format:
For each test case, if there is no interesting permutation, output a single integer $-1$.

Otherwise, output a single integer — the maximum number of inversions.

Tags:
['brute force', 'divide and conquer', 'dp', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
This is a hard version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.

Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds:

$$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$

Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation.

An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.

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 two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le 5 \cdot 10^3$) — the length of the permutation and the number of intervals.

The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) — the $i$-th interval. Note that there may exist identical intervals (i.e., there may exist two different indices $i, j$ such that $l_i = l_j$ and $r_i = r_j$).

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

Output Format:
For each test case, if there is no interesting permutation, output a single integer $-1$.

Otherwise, output a single integer — the maximum number of inversions.

Tags:
['brute force', 'data structures', 'divide and conquer', 'dp', 'greedy', 'math', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
Piggy gives Turtle three sequences $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$, and $c_1, c_2, \ldots, c_n$.

Turtle will choose a subsequence of $1, 2, \ldots, n$ of length $m$, let it be $p_1, p_2, \ldots, p_m$. The subsequence should satisfy the following conditions:

  * $a_{p_1} \le a_{p_2} \le \cdots \le a_{p_m}$;    * All $b_{p_i}$ for all indices $i$ are pairwise distinct, i.e., there don't exist two different indices $i$, $j$ such that $b_{p_i} = b_{p_j}$.

Help him find the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$, or tell him that it is impossible to choose a subsequence of length $m$ that satisfies the conditions above.

Recall that a sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements.

Input Format:
The first line contains two integers $n$ and $m$ ($1 \le n \le 3000$, $1 \le m \le 5$) — the lengths of the three sequences and the required length of the subsequence.

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

The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$) — the elements of the sequence $b$.

The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^4$) — the elements of the sequence $c$.

Output Format:
Output a single integer — the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$. If it is impossible to choose a subsequence of length $m$ that satisfies the conditions above, output $-1$.

Tags:
['brute force', 'data structures', 'dp', 'greedy', 'math', 'probabilities', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$.

The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all other points from the set have distance to $j$ greater or equal to $|i - j|$.

For example, consider a set of points $\\{1, 3, 5, 8\\}$:

  * for the point $1$, the closest point is $3$ (other points have distance greater than $|1-3| = 2$);    * for the point $3$, there are two closest points: $1$ and $5$;    * for the point $5$, the closest point is $3$ (but not $8$, since its distance is greater than $|3-5|$);    * for the point $8$, the closest point is $5$.

You are given a set of points. You have to add an integer point into this set in such a way that it is different from every existing point in the set, and it becomes the closest point to every point in the set. Is it possible?

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

Each test case consists of two lines:

  * the first line contains one integer $n$ ($2 \le n \le 40$) — the number of points in the set;    * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots < x_n \le 100$) — the points from the set.

Output Format:
For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO.

Tags:
['implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB