inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
This is the easy version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.

Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.

Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.

However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. It is followed by descriptions of the test cases.

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

The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) — the intelligence values of the knights.

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

Output Format:
For each test case, output a single line containing one integer — the minimal difference Arthur can achieve.

Tags:
['2-sat', 'data structures', 'dp', 'matrices', 'two pointers']

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

Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.

Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.

However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. It is followed by descriptions of the test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 100\,000$) — the number of desks.

The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) — the intelligence values of the knights.

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

Output Format:
For each test case, output a single line containing one integer — the minimal difference Arthur can achieve.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
It's another beautiful day on Farmer John's farm.

After Farmer John arrived at his farm, he counted $n$ legs. It is known only chickens and cows live on the farm, and a chicken has $2$ legs while a cow has $4$.

What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?

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

Each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^3$, $n$ is even).

Output Format:
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$.

Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of cells such that every cell belongs to exactly one block.

Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value.

For example, the following demonstration shows a grid being reduced by factor of $3$.

Original grid $0$| $0$| $0$| $1$| $1$| $1$   ---|---|---|---|---|---   $0$| $0$| $0$| $1$| $1$| $1$   $0$| $0$| $0$| $1$| $1$| $1$   $1$| $1$| $1$| $0$| $0$| $0$   $1$| $1$| $1$| $0$| $0$| $0$   $1$| $1$| $1$| $0$| $0$| $0$   Reduced grid $0$| $1$   ---|---   $1$| $0$      Help Tina reduce the grid by a factor of $k$.

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

The first line of each test case contains two integers $n$ and $k$ ($1 \leq n \leq 1000$, $1 \le k \le n$, $k$ is a divisor of $n$) — the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by.

Each of the following $n$ lines contain $n$ characters describing the cells of the grid. Each character is either $0$ or $1$. It is guaranteed every $k$ by $k$ block has the same value.

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

Output Format:
For each test case, output the grid reduced by a factor of $k$ on a new line.

Tags:
['greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries.

For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and set $a_i = x$ where $x$ is any character you desire. Output the minimum number of operations you must perform such that $\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}$. The operations you perform on one query does not affect other queries.

For an arbitrary string $c$, $\texttt{sorted(c[l..r])}$ denotes the substring consisting of characters $c_l, c_{l+1}, ... , c_r$ sorted in lexicographical order.

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 two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^5$) – the length of both strings and the number of queries.

The following line contains $a$ of length $n$. It is guaranteed $a$ only contains lowercase latin letters.

The following line contains $b$ of length $n$. It is guaranteed $b$ only contains lowercase latin letters.

The following $q$ lines contain two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) – the range of the query.

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

Output Format:
For each query, output an integer, the minimum number of operations you need to perform in a new line.

Tags:
['dp', 'greedy', 'sortings', 'strings']

Time Limit: 5000 ms
Memory Limit: 256 MB
Counting is Fun!

— satyam343

Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.

Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as different) and $a$, $b$, $c$ must be strictly greater than $0$.

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

Each test case contains two integers $n$ and $x$ ($1 \leq n,x \leq 10^6$).

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

Output Format:
Output a single integer — the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
In a desperate attempt to obtain your waifu favorite character, you have hacked into the source code of the game. After days of struggling, you finally find the binary string that encodes the gacha system of the game. In order to decode it, you must first solve the following problem.

You are given a binary string $s$ of length $n$. For each pair of integers $(l, r)$ $(1 \leq l \leq r \leq n)$, count the number of pairs $(x, y)$ $(l \leq x \leq y \leq r)$ such that the amount of $\mathtt{0}$ equals the amount of $\mathtt{1}$ in the substring $s_xs_{x+1}...s_y$.

Output the sum of counts over all possible $(l, r)$ modulo $10^9+7$.

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

Each test case contains a binary string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$). It is guaranteed $s$ only contains characters $\mathtt{0}$ and $\mathtt{1}$.

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
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
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
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
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
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
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
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
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