inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
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
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
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
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
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
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
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
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked.

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

You know that:

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

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

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

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

The first line of each test case contains two integers $l$ and $r$ ($1 \le l < r \le 100$) — the bounds of the segment of rooms where Alice is located.

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

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

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

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

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

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

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

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

What is the minimum possible score Bob can achieve?

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

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

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

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$.

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

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

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

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

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

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

Additional constraints on the input:

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Two players, Alice and Bob, are playing a game. They have $n$ piles of stones, with the $i$-th pile initially containing $a_i$ stones.

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

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

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

Determine who will win.

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

Each test case consists of two lines:

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

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

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

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

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given an array $a$ consisting of $n$ integers.

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

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

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

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

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

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

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

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

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

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

Time Limit: 5000 ms
Memory Limit: 512 MB
Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows:

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

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

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

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

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

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

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

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

Time Limit: 2000 ms
Memory Limit: 1024 MB