inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).

Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:

  * They can submit the problem and receive $a_i$ points;    * They can skip the problem, in which case they will never be able to submit it. 

Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:

  * If he submitted the $i$-th problem, it looks at problems with indices $j < i$;    * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$. 

Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.

Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — 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 \leq n \leq 4 \cdot 10^5$) — the number of problems in the olympiad.

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

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) — the parameters of the problems.

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

Output Format:
For each test case, output a single integer — the maximum number of points that Prokhor can achieve.

Tags:
['constructive algorithms', 'data structures', 'divide and conquer', 'dp', 'flows', 'graphs', 'greedy', 'implementation', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB
Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity.

The conditions in this casino are as follows:

  * There are a total of $n$ games in the casino.    * You can play each game at most once.    * Each game is characterized by two parameters: $p_i$ ($1 \le p_i \le 100$) and $w_i$ — the probability of winning the game in percentage and the winnings for a win.    * If you lose in any game you decide to play, you will receive nothing at all (even for the games you won). 

You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings.

In this case, if you choose to play the games with indices $i_1 < i_2 < \ldots < i_k$, you will win in all of them with a probability of $\prod\limits_{j=1}^k \frac{p_{i_j}}{100}$, and in that case, your winnings will be equal to $\sum\limits_{j=1}^k w_{i_j}$.

That is, the expected value of your winnings will be $\left(\prod\limits_{j=1}^k \frac{p_{i_j}}{100}\right) \cdot \left(\sum\limits_{j=1}^k w_{i_j}\right)$.

To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $i$ ($1 \le i \le n$), it holds that $w_i \cdot p_i \le 2 \cdot 10^5$.

Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of games offered to play.

The $i$-th of the following $n$ lines contains two integers $p_i$ and $w_i$ ($1 \leq p_i \leq 100$, $1 \leq w_i, p_i \cdot w_i \leq 2 \cdot 10^5$) — the probability of winning and the size of the winnings in the $i$-th game.

Output Format:
Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games.

Your answer will be accepted if the relative or absolute error does not exceed $10^{-6}$. Formally, if $a$ is your answer and $b$ is the jury's answer, it will be accepted if $\frac{|a-b|}{\max(b, 1)} \le 10^{-6}$.

Tags:
['dp', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.

In one second, you can do one of the following two actions:

  * choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen;    * choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen. 

You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.

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

Each test case consists of two lines. The first line contains the string $s$, and the second line contains the string $t$ ($1 \le |s|, |t| \le 100$). Both strings consist of uppercase Latin letters.

Output Format:
For each test case, print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.

Tags:
['binary search', 'greedy', 'strings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 512 MB
Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:

                  for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive)               C[n][0] = 1;               C[n][n] = 1;               for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive)                   C[n][k] = C[n][k - 1] + C[n - 1][k - 1];           }       

Unfortunately, he made an error, since the right formula is the following:

                          C[n][k] = C[n - 1][k] + C[n - 1][k - 1]       

But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $t$ various pairs $(n_i, k_i)$. Note that they should be calculated according to the first (wrong) formula.

Since values $C[n_i][k_i]$ may be too large, print them modulo $10^9 + 7$.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of pairs. Next, $t$ pairs are written in two lines.

The second line contains $t$ integers $n_1, n_2, \dots, n_t$ ($2 \le n_i \le 10^5$).

The third line contains $t$ integers $k_1, k_2, \dots, k_t$ ($1 \le k_i < n_i$).

Output Format:
Print $t$ integers $C[n_i][k_i]$ modulo $10^9 + 7$.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it.

At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.

In other words, if on the previous turn Monocarp took a card with the number $x$, then on the current turn he can take either a card with the number $x$ or a card with the number $x + 1$. Monocarp can take any card which meets that condition, regardless of its position in the deck.

After Monocarp takes a card on the current turn, it is removed from the deck.

According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $k$.

If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.

Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

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 $k$ ($1 \le k \le n \le 200\,000$) — the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.

The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$), where $a_i$ is the number written on the $i$-th card.

Additional constraint of the input: the sum of $n$ over all test cases doesn't exceed $200\,000$.

Output Format:
For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

Tags:
['binary search', 'brute force', 'greedy', 'implementation', 'sortings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 512 MB
Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.

During the game, you'll acquire $m$ attribute points that allow you to increase your attribute levels — one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.

Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?

Note that you can't change the order of records.

Input Format:
The first line contains two integers $n$ and $m$ ($1 \le m \le 5000$; $m < n \le 2 \cdot 10^6$) — the number of records in the list and the total number of points you'll get during the game.

The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-m \le r_i \le m$), where $r_i$ encodes the $i$-th record:

  * If $r_i = 0$, then the $i$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence;    * If $r_i > 0$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $|r_i|$, you pass.    * If $r_i < 0$, then it's a Strength check: if your Strength level is greater than or equal to $|r_i|$, you pass. 

Additional constraint on the input: the sequence $r_1, r_2, \dots, r_n$ contains exactly $m$ elements equal to $0$.

Output Format:
Print one integer — the maximum number of checks you can pass.

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

Time Limit: 2500 ms
Memory Limit: 512 MB
In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.

A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:

  * $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit);    * $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank). 

Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.

Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.

Input Format:
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).

Additional constraint on the input: $m$ is even.

Output Format:
Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given an array $a$, consisting of $n$ integers (numbered from $1$ to $n$). Initially, they are all zeroes.

You have to process $q$ queries. The $i$-th query consists of two different integers $x_i$ and $y_i$. During the $i$-th query, you have to choose an integer $p$ (which is either $x_i$ or $y_i$) and an integer $d$ (which is either $1$ or $-1$), and assign $a_p = a_p + d$.

After each query, every element of $a$ should be a non-negative integer.

Process all queries in such a way that the sum of all elements of $a$ after the last query is the minimum possible.

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

Then $q$ lines follow. The $i$-th of these lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$) — the description of the $i$-th query.

Output Format:
For each query, print a line containing two characters:

  * the first character should be x if you choose $p=x_i$, or y if you choose $p=y_i$;    * the second character should be + if you choose $d=1$, or - if you choose $d=-1$. 

If there are multiple answers, print any of them.

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

Time Limit: 3000 ms
Memory Limit: 512 MB
Monocarp is gathering an army to fight a dragon in a videogame.

The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter — his health. Each defensive artifact also has one parameter — its durability.

Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.

The battle consists of rounds that proceed as follows:

  * first, the dragon deals damage equal to $\frac{1}{a + b}$ (a real number without rounding) to each hero, where $a$ is the number of heroes alive and $b$ is the number of active artifacts;    * after that, all heroes with health $0$ or less die;    * finally, some artifacts are deactivated. An artifact with durability $x$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $x$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle. 

The battle ends when there are no heroes left alive.

Initially, the army is empty. There are $q$ queries: add a hero with health $x$ or an artifact with durability $y$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

Input Format:
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.

In the $i$-th of the following $q$ lines, there are two integers $t_i$ and $v_i$ ($t_i \in \\{1, 2\\}$; $1 \le v_i \le 10^9$) — the type of the query and the value of the query parameter. If the type is $1$, a hero with health $v_i$ is added. If the type is $2$, an artifact with durability $v_i$ is added.

Output Format:
Print $q$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

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

Time Limit: 5000 ms
Memory Limit: 512 MB
You are given a coordinate plane and three integers $X$, $Y$, and $K$. Find two line segments $AB$ and $CD$ such that

  1. the coordinates of points $A$, $B$, $C$, and $D$ are integers;    2. $0 \le A_x, B_x, C_x, D_x \le X$ and $0 \le A_y, B_y, C_y, D_y \le Y$;    3. the length of segment $AB$ is at least $K$;    4. the length of segment $CD$ is at least $K$;    5. segments $AB$ and $CD$ are perpendicular: if you draw lines that contain $AB$ and $CD$, they will cross at a right angle. 

Note that it's not necessary for segments to intersect. Segments are perpendicular as long as the lines they induce are perpendicular.

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 and only line of each test case contains three integers $X$, $Y$, and $K$ ($1 \le X, Y \le 1000$; $1 \le K \le 1414$).

Additional constraint on the input: the values of $X$, $Y$, and $K$ are chosen in such a way that the answer exists.

Output Format:
For each test case, print two lines. The first line should contain $4$ integers $A_x$, $A_y$, $B_x$, and $B_y$ — the coordinates of the first segment.

The second line should also contain $4$ integers $C_x$, $C_y$, $D_x$, and $D_y$ — the coordinates of the second segment.

If there are multiple answers, print any of them.

Tags:
['constructive algorithms', 'geometry', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white.

You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black.

A list $a$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $k$ for which this is possible.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 500$) — 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$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$).

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

Output Format:
For each test case, print a single integer — the minimum value of $k$ for which it is possible to paint all the given cells black.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains $n$ figures, the $i$-th figure costs $i$ coins and is available for purchase from day $i$ to day $n$.

For each of the $n$ days, Monocarp knows whether he can visit the shop.

Every time Monocarp visits the shop, he can buy any number of action figures which are sold in the shop (of course, he cannot buy an action figure that is not yet available for purchase). If Monocarp buys at least two figures during the same day, he gets a discount equal to the cost of the most expensive figure he buys (in other words, he gets the most expensive of the figures he buys for free).

Monocarp wants to buy exactly one $1$-st figure, one $2$-nd figure, ..., one $n$-th figure from the set. He cannot buy the same figure twice. What is the minimum amount of money he has to spend?

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

Each test case consists of two lines:

  * the first line contains one integer $n$ ($1 \le n \le 4 \cdot 10^5$) — the number of figures in the set (and the number of days);    * the second line contains a string $s$ ($|s| = n$, each $s_i$ is either 0 or 1). If Monocarp can visit the shop on the $i$-th day, then $s_i$ is 1; otherwise, $s_i$ is 0. 

Additional constraints on the input:

  * in each test case, $s_n$ is 1, so Monocarp is always able to buy all figures during the $n$-th day;    * the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.

Output Format:
For each test case, print one integer — the minimum amount of money Monocarp has to spend.

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

Time Limit: 2500 ms
Memory Limit: 512 MB
You are given a sequence of integers $[a_1, a_2, \dots, a_n]$. Let $s(l,r)$ be the sum of elements from $a_l$ to $a_r$ (i. e. $s(l,r) = \sum\limits_{i=l}^{r} a_i$).

Let's construct another sequence $b$ of size $\frac{n(n+1)}{2}$ as follows: $b = [s(1,1), s(1,2), \dots, s(1,n), s(2,2), s(2,3), \dots, s(2,n), s(3,3), \dots, s(n,n)]$.

For example, if $a = [1, 2, 5, 10]$, then $b = [1, 3, 8, 18, 2, 7, 17, 5, 15, 10]$.

You are given $q$ queries. During the $i$-th query, you are given two integers $l_i$ and $r_i$, and you have to calculate $\sum \limits_{j=l_i}^{r_i} b_j$.

Input Format:
The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$).

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

The third line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$).

Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le \frac{n(n+1)}{2}$).

Output Format:
Print $q$ integers, the $i$-th of which should be equal to $\sum \limits_{j=l_i}^{r_i} b_j$.

Tags:
['binary search', 'data structures', 'dp', 'implementation', 'math']

Time Limit: 4000 ms
Memory Limit: 512 MB
Given an integer array $a$ of size $n$.

Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array.

For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which contains $2$ set bits), and the value of the array is $4-2=2$.

Your task is to calculate the maximum possible value of some subsequence of the given array.

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

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

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{60}$).

Output Format:
For each test case, print the maximum possible value of some subsequence of the given array.

Tags:
['bitmasks', 'dfs and similar', 'flows', 'graph matchings', 'graphs']

Time Limit: 2000 ms
Memory Limit: 256 MB