inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
In the humble act of meeting, joy doth unfold like a flower in bloom.

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

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

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

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

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

The first line of each test case consists of three integers $n$, $m$, $h$ ($2 \le n \le 2 \cdot 10^5, \;1 \le m \le 2 \cdot 10^5, \; 1 \le h \le n$) — the number of vertices, the number of edges and the number of vertices that have a single horse.

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

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

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

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

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

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

Time Limit: 5000 ms
Memory Limit: 256 MB
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold."

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
What is done is done, and the spoilt milk cannot be helped.

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

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

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

Little John always drinks the freshest drinkable milk first.

Determine the number of milk satisfaction days for Little John.

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

The first line of each test case consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness.

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

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

Output Format:
For each test case, output a single integer, the number of milk satisfaction days.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
At such times archery was always the main sport of the day, for the Nottinghamshire yeomen were the best hand at the longbow in all merry England, but this year the Sheriff hesitated...

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

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

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

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

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

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

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

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

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

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

Output Format:
For each query, output "YES", if the Sheriff does not lose the game when only considering the targets $l, l+1, \dots, r$, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Tags:
['data structures', 'divide and conquer', 'greedy', 'hashing']

Time Limit: 3000 ms
Memory Limit: 256 MB
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)

⠀

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

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

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

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

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

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

The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) — the number of distinct types of cards and the number of coins.

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

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

Output Format:
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)

⠀

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

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

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

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

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

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

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

Output Format:
For each test case, output a single integer: the number of starting cities that allow you to win.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)

⠀

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

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

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

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

Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $u$ and $v$. It is guaranteed that the given edges form a tree.

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

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

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

Time Limit: 3000 ms
Memory Limit: 256 MB
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)

⠀

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

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

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

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

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

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

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

Output Format:
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex)

⠀

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

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

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

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

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

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

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

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

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

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

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

Time Limit: 6000 ms
Memory Limit: 256 MB
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex)

⠀

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

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

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

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

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

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

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

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

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

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

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

Time Limit: 13000 ms
Memory Limit: 256 MB
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

⠀

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

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. 

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

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$. 

The answer can be very large, so you have to calculate it modulo a given prime $p$.

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

The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 80$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

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

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

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

Time Limit: 2000 ms
Memory Limit: 1024 MB
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

⠀

This is the medium version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved.

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. 

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

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$. 

The answer can be very large, so you have to calculate it modulo a given prime $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 only line of each test case contains two integers $n$, $p$ ($1 \le n \le 500$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

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

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

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

Time Limit: 10000 ms
Memory Limit: 1024 MB
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

⠀

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

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. 

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

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$. 

The answer can be very large, so you have to calculate it modulo a given prime $p$.

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

The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 3000$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

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

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

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

Time Limit: 10000 ms
Memory Limit: 1024 MB
[EnV - Dynasty](https://soundcloud.com/envyofficial/env-dynasty)

⠀

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

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

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

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 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.

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

Output Format:
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.

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

Time Limit: 1000 ms
Memory Limit: 256 MB