inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Consider the algorithm for stabilizing matrix $a$:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations.

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

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

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

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

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

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

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

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

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

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

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

Find the smallest number of pairs of vertices!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Time Limit: 3000 ms
Memory Limit: 128 MB
Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM!

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

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

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

The first and only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 100$) — the number of GBs that you want to upload and the length of the time window respectively.

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

Tags:
['greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given an array of integers $a$ of length $n$.

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

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

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

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

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

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

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

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

Tags:
['greedy']

Time Limit: 1000 ms
Memory Limit: 256 MB
There are $n$ flowers in a row, the $i$-th of them initially has a positive height of $h_i$ meters.

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

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

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

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

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

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

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

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

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

Tags:
['dp', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB
Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.

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

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

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

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

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

The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) — the number of cakes.

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

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

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

Tags:
['dp', 'games']

Time Limit: 2000 ms
Memory Limit: 256 MB
God's Blessing on This ArrayForces!

A Random Pebble

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

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

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

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

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

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

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

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

The third line of each test case contains $n - 1$ integers $p_2, p_3 , \ldots, p_n$ ($1 \le p_i < i$), indicating that there is an edge from vertex $p_i$ to vertex $i$. It is guaranteed that the given edges form a tree.

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.

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

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

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

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

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

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

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

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

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

Tags:
['dp']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the hard version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.

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

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

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

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

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

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

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

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

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

Tags:
['dp']

Time Limit: 2000 ms
Memory Limit: 256 MB