inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness.

Initially, there is one store numbered $1$, which sells nothing. You have to process $q$ queries of the following types:

  * $1~x$ — a new store opens, that sells the same types of ice cream as store $x$. It receives the minimum available positive index. The order of the types of ice cream in the new store is the same as in store $x$.    * $2~x~p~t$ — a type of ice cream with price $p$ and tastiness $t$ becomes available in store $x$.    * $3~x$ — a type of ice cream that was available the longest (appeared the earliest) in store $x$ is removed.    * $4~x~p$ — for store $x$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $p$ (each type can be used in the subset no more than once).

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

Each of the following $q$ lines contains a query in the format described in the statement:

  * $1~x$;    * $2~x~p~t$ ($1 \le p, t \le 2000$);    * $3~x$;    * $4~x~p$ ($1 \le p \le 2000$). 

Additional constraints on the input data:

  * $x$ in each query does not exceed the current number of stores (that is, $1$ plus the number of type $1$ queries);    * query type $3$ is not applied to a store that has no types of ice cream;    * there is at least one query of type $4$.

Output Format:
For each query of type $4$, output a single integer — for store $x$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $p$ (each type can be used in the subset no more than once).

Tags:
['data structures', 'dfs and similar', 'divide and conquer', 'dp', 'implementation', 'trees']

Time Limit: 2000 ms
Memory Limit: 1024 MB
You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $n$ stamps. Each stamp is a rectangle of width $w_i$ and height $h_i$.

You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, and for each cell, the stamp must either cover it fully or not cover it at all. You can use the stamp at any position on the grid, even if some or all of the cells covered by the stamping area are already black.

What is the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used?

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 $i$-th of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \le w_i, h_i \le 100$).

Output Format:
For each test case, output a single integer — the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.

Tags:
['geometry', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity.

It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array $[1, 4, 2, 3, 6, 5, 5, 7, 7]$ becomes $[1, 4, 6, 7, 7]$ after a Stalin Sort.

We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays$^{\text{∗}}$, as many times as is needed.

Given an array $a$ of $n$ integers, determine the minimum number of integers which must be removed from the array to make it vulnerable.

$^{\text{∗}}$An array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases. This 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 size 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$).

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 integer — the minimum number of integers which must be removed from the array to make it vulnerable.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
You're given an array $a$ initially containing $n$ integers. In one operation, you must do the following:

  * Choose a position $i$ such that $1 < i \le |a|$ and $a_i = |a| + 1 - i$, where $|a|$ is the current size of the array.    * Append $i - 1$ zeros onto the end of $a$. 

After performing this operation as many times as you want, what is the maximum possible length of the array $a$?

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

The first line of each test case contains $n$ ($1 \le n \le 3 \cdot 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^{12}$).

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 possible length of $a$ after performing some sequence of operations.

Tags:
['brute force', 'data structures', 'dfs and similar', 'dp', 'graphs', 'greedy']

Time Limit: 3000 ms
Memory Limit: 256 MB
This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.

You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:

  * Type $1$ — If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost.    * Type $2$ — You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$. 

You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).

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 third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).

It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.

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

Output Format:
For each test case, if it's possible to make $a$ empty, then output the minimum total cost of the operations.

If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.

You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:

  * Type $1$ — If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost.    * Type $2$ — You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$. 

You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations, and the number of sequences of operations which yield this minimum cost modulo $10^9 + 7$.

Two sequences of operations are considered different if you choose a different type of operation at any step, or the size of the removed prefix is different at any step.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).

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 third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).

It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.

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

Output Format:
For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$.

If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack.

Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:

  * $1 \le d \le a_i$, and    * $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). 

The player who cannot make a move loses, and Alice goes first.

You're given the $a_i$ and $x_i$ values for each pile, please determine who will win the game if both players play optimally.

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

The first line of each test case contains $n$ ($1 \le n \le 10^4$) — the number of piles.

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

The third line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i < 2^{30}$).

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

Output Format:
Print a single line with the winner's name. If Alice wins, print "Alice", otherwise print "Bob" (without quotes).

Tags:
['bitmasks', 'brute force', 'games', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the hard version of this problem. The only difference is that you need to output the number of choices of games where Bob wins in this version, where the number of stones in each pile are not fixed. You must solve both versions to be able to hack.

Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:

  * $1 \le d \le a_i$, and    * $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). 

The player who cannot make a move loses, and Alice goes first.

You're given the $a_i$ values of each pile, but the number of stones in the $i$-th pile has not been determined yet. For the $i$-th pile, $x_i$ can be any integer between $1$ and $b_i$, inclusive. That is, you can choose an array $x_1, x_2, \ldots, x_n$ such that the condition $1 \le x_i \le b_i$ is satisfied for all piles.

Your task is to count the number of games where Bob wins if both players play optimally. Two games are considered different if the number of stones in any pile is different, i.e., the arrays of $x$ differ in at least one position.

Since the answer can be very large, please output the result modulo $10^9 + 7$.

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

The first line of each test case contains $n$ ($1 \le n \le 10^4$) — the number of piles.

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

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i < 2^{30}$).

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

Output Format:
Output a single integer, the number of games where Bob wins, modulo $10^9 + 7$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $(0, 0)$, and the Red Queen is at position $(a, b)$. Alice can only move in the four cardinal directions (north, east, south, west).

More formally, if Alice is at the point $(x, y)$, she will do one of the following:

  * go north (represented by N), moving to $(x, y+1)$;    * go east (represented by E), moving to $(x+1, y)$;    * go south (represented by S), moving to $(x, y-1)$; or    * go west (represented by W), moving to $(x-1, y)$. 

Alice's movements are predetermined. She has a string $s$ representing a sequence of moves that she performs from left to right. Once she reaches the end of the sequence, she repeats the same pattern of moves forever.

Can you help Alice figure out if she will ever meet the Red Queen?

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 three integers $n$, $a$, $b$ ($1 \le n$, $a$, $b \le 10$) — the length of the string and the initial coordinates of the Red Queen.

The second line contains a string $s$ of length $n$ consisting only of the characters N, E, S, or W.

Output Format:
For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen.

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:
['brute force', 'implementation', 'math', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Alice mixed up the words transmutation and permutation! She has an array $a$ specified via three integers $n$, $b$, $c$: the array $a$ has length $n$ and is given via $a_i = b\cdot (i - 1) + c$ for $1\le i\le n$. For example, if $n=3$, $b=2$, and $c=1$, then $a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]$.

Now, Alice really enjoys permutations of $[0, \ldots, n-1]$$^{\text{∗}}$ and would like to transform $a$ into a permutation. In one operation, Alice replaces the maximum element of $a$ with the $\operatorname{MEX}$$^{\text{†}}$ of $a$. If there are multiple maximum elements in $a$, Alice chooses the leftmost one to replace.

Can you help Alice figure out how many operations she has to do for $a$ to become a permutation for the first time? If it is impossible, you should report it.

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $0$ to $n-1$ in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ($1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ($n=3$ but there is $3$ in the array).

$^{\text{†}}$The $\operatorname{MEX}$ of an array is the smallest non- negative integer that does not belong to the array. For example, the $\operatorname{MEX}$ of $[0, 3, 1, 3]$ is $2$ and the $\operatorname{MEX}$ of $[5]$ is $0$.

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

The only line of each test case contains three integers $n$, $b$, $c$ ($1\le n\le 10^{18}$; $0\le b$, $c\le 10^{18}$) — the parameters of the array.

Output Format:
For each test case, if the array can never become a permutation, output $-1$. Otherwise, output the minimum number of operations for the array to become a permutation.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $n$ sections with tastiness values $a_1, a_2, \ldots, a_n$. There are $m$ creatures at the tea party, excluding Alice.

Alice will cut the cake into $m + 1$ pieces. Formally, she will partition the cake into $m + 1$ subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these $m + 1$ pieces up among the $m$ creatures and herself (her piece can be empty). However, each of the $m$ creatures will only be happy when the tastiness of its piece is $v$ or more.

Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output $-1$.

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 three integers $n, m, v$ ($1\le m\le n\le 2\cdot 10^5$; $1\le v\le 10^9$) — the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively.

The next line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the tastinesses of the sections.

The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

Output Format:
For each test case, output the maximum tastiness Alice can achieve for her piece, or $-1$ if there is no way to make sure every creature is happy.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are $n$ different types of cards in their card game. Alice currently has a card of type $1$ and needs a card of type $n$ to escape Wonderland. The other players have one of each kind of card.

In this card game, Alice can trade cards with the three other players. Each player has different preferences for the $n$ types of cards, which can be described by permutations$^{\text{∗}}$ $q$, $k$, and $j$ for the Queen, King, and Jack, respectively.

A player values card $a$ more than card $b$ if for their permutation $p$, $p_a > p_b$. Then, this player is willing to trade card $b$ to Alice in exchange for card $a$. Alice's preferences are straightforward: she values card $a$ more than card $b$ if $a > b$, and she will also only trade according to these preferences.

Determine if Alice can trade up from card $1$ to card $n$ subject to these preferences, and if it is possible, give a possible set of trades to do it.

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

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 an integer $n$ ($2\le n\le 2\cdot 10^5$) — the number of card types.

The next three lines contain the preferences of the Queen, King, and Jack respectively. Each of these lines contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1\le p_i\le n$) — a permutation corresponding to the player's preferences.

The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

Output Format:
For each test case, on the first line output a single string "YES" or "NO" (without the quotes) denoting whether Alice can trade up to card $n$.

If the first line was "YES", then on the next line output $k$ — the number of trades Alice will make. On the next $k$ lines output space separated a character $c\in \\{\texttt{q}, \texttt{k}, \texttt{j}\\}$ and integer $x$, denoting that Alice trades with player $c$ to get card $x$. It must be the case that on the $k$'th line, $x = n$. If there are multiple solutions, print any of them.

You can output this answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. The same goes for the character $c$ denoting the player in the trade ($\texttt{Q}, \texttt{K}, \texttt{J}$ will all be accepted alongside their lowercase variants).

Tags:
['constructive algorithms', 'divide and conquer', 'dp', 'graphs', 'greedy', 'implementation', 'sortings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB