inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
This is the easy version of the problem. The only difference between the two versions are the allowed characters in $s$. In the easy version, $s$ only contains the character ?. You can make hacks only if both versions of the problem are solved.

You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, consisting only of the character ?.

For each $i$ from $1$ to $n$:

  * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$.    * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$. 

Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph:

  * If $s_i =$ L, add the edge $(i, l_i)$ to the graph.    * If $s_i =$ R, add the edge $(i, r_i)$ to the graph.    * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice. 

Find the maximum possible diameter$^{\text{∗}}$ over all connected graphs that you can form. Output $-1$ if it is not possible to form any connected graphs.

$^{\text{∗}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$.

The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$.

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

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

The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) — the elements of $p$, which are guaranteed to form a permutation.

The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the character ?.

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 the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.

Tags:
['divide and conquer', 'dp', 'trees']

Time Limit: 7000 ms
Memory Limit: 1024 MB
This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved.

You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, where each character is either L, R, or ?.

For each $i$ from $1$ to $n$:

  * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$.    * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$. 

Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph:

  * If $s_i =$ L, add the edge $(i, l_i)$ to the graph.    * If $s_i =$ R, add the edge $(i, r_i)$ to the graph.    * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice. 

Find the maximum possible diameter over all connected$^{\text{∗}}$ graphs that you can form. Output $-1$ if it is not possible to form any connected graphs.

$^{\text{∗}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$.

The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$.

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

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

The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) — the elements of $p$, which are guaranteed to form a permutation.

The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the characters L, R, and ?.

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 the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.

Tags:
['divide and conquer', 'dp', 'trees']

Time Limit: 7000 ms
Memory Limit: 1024 MB
This temple only magnifies the mountain's power.

Badeline

This is an interactive problem.

You are given two positive integers $n$ and $m$ ($\bf{n \le m}$).

The jury has hidden from you a rectangular matrix $a$ with $n$ rows and $m$ columns, where $a_{i,j} \in \\{ -1, 0, 1 \\}$ for all $1 \le i \le n$ and $1 \le j \le m$. The jury has also selected a cell $(i_0, j_0)$. Your goal is to find $(i_0,j_0)$.

In one query, you give a cell $(i, j)$, then the jury will reply with an integer.

  * If $(i, j) = (i_0, j_0)$, the jury will reply with $0$.    * Else, let $S$ be the sum of $a_{x,y}$ over all $x$ and $y$ such that $\min(i, i_0) \le x \le \max(i, i_0)$ and $\min(j, j_0) \le y \le \max(j, j_0)$. Then, the jury will reply with $|i - i_0| + |j - j_0| + |S|$. 

Find $(i_0, j_0)$ by making at most $n + 225$ queries.

Note: the grader is not adaptive: $a$ and $(i_0,j_0)$ are fixed before any queries are made.

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

The only line of each test case contains two integers $n$ and $m$ ($1 \le n \le m \le 5000$) — the numbers of rows and the number of columns of the hidden matrix $a$ respectively.

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

Output Format:


Tags:
['interactive']

Time Limit: 4000 ms
Memory Limit: 512 MB
A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, $\\{2,2,4\\}$ is a multiset.

You have a multiset $S$. Initially, the multiset contains only one positive integer $n$. That is, $S=\\{n\\}$. Additionally, there is a given positive integer $k$.

In one operation, you can select any positive integer $u$ in $S$ and remove one copy of $u$ from $S$. Then, insert no more than $k$ positive integers into $S$ so that the sum of all inserted integers is equal to $u$.

Find the minimum number of operations to make $S$ contain $n$ ones.

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

The only line of each testcase contains two integers $n,k$ ($1\le n\le 1000,2\le k\le 1000$).

Output Format:
For each testcase, print one integer, which is the required answer.

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

Time Limit: 1000 ms
Memory Limit: 512 MB
You are given a sequence $[a_1,\ldots,a_n]$ where each element $a_i$ is either $0$ or $1$. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers $1\le l\le r\le |a|$ (where $|a|$ is the current length of $a$) and replace $[a_l,\ldots,a_r]$ with a single element $x$, where $x$ is the majority of $[a_l,\ldots,a_r]$.

Here, the majority of a sequence consisting of $0$ and $1$ is defined as follows: suppose there are $c_0$ zeros and $c_1$ ones in the sequence, respectively.

  * If $c_0\ge c_1$, the majority is $0$.    * If $c_0<c_1$, the majority is $1$. 

For example, suppose $a=[1,0,0,0,1,1]$. If we select $l=1,r=2$, the resulting sequence will be $[0,0,0,1,1]$. If we select $l=4,r=6$, the resulting sequence will be $[1,0,0,1]$.

Determine if you can make $a=[1]$ with a finite number of operations.

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

The first line of each testcase contains one integer $n$ ($1\le n\le 2\cdot 10^5$).

The second line of each testcase contains a string consisting of $0$ and $1$, describing the sequence $a$.

It's guaranteed that the sum of $n$ over all testcases does not exceed $2\cdot 10^5$.

Output Format:
For each testcase, if it's possible to make $a=[1]$, print YES. Otherwise, print NO. 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:
['greedy', 'implementation']

Time Limit: 1500 ms
Memory Limit: 512 MB
You are given a positive integer $n$. Find the longest sequence of positive integers $a=[a_1,a_2,\ldots,a_k]$ that satisfies the following conditions, and print the sequence:

  * $a_i\le n$ for all $1\le i\le k$.    * $a$ is strictly increasing. That is, $a_i>a_{i-1}$ for all $2\le i\le k$.    * $a_i\,|\,a_{i-1}=n$ for all $2\le i\le k$, where $|$ denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

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

The only line of each test case contains one integer $n$ ($1\le n\le 10^{18}$).

It's guaranteed that the sum of lengths of the longest valid sequences does not exceed $5\cdot 10^5$.

Output Format:
For each testcase, print two lines. In the first line, print the length of your constructed sequence, $k$. In the second line, print $k$ positive integers, denoting the sequence. If there are multiple longest sequences, you can print any of them.

Tags:
['bitmasks', 'constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 512 MB
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for $10^{100}$ rounds.

In each round, the following happens in order:

  1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters.    2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future. 

There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.

If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?

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

The first line of each test case contains an integer $n$ ($1\le n\le 3\cdot 10^5$).

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

The following $n-1$ lines each contain two integers $x,y$ ($1\le x,y\le n$), denoting an edge on the tree connecting vertex $x$ and $y$.

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, print one integer: the minimum possible health decrement.

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

Time Limit: 3000 ms
Memory Limit: 512 MB
For an array $[a_1,a_2,\ldots,a_n]$ of length $n$, define $f(a)$ as the sum of the minimum element over all subsegments. That is, $$f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.$$

A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. You are given a permutation $[a_1,a_2,\ldots,a_n]$. For each $i$, solve the following problem independently:

  * Erase $a_i$ from $a$, concatenating the remaining parts, resulting in $b = [a_1,a_2,\ldots,a_{i-1},\;a_{i+1},\ldots,a_{n}]$.    * Calculate $f(b)$.

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

The first line of each test case contains an integer $n$ ($1\le n\le 5\cdot 10^5$).

The second line of each test case contains $n$ distinct integers $a_1,\ldots,a_n$ ($1\le a_i\le n$).

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

Output Format:
For each test case, print one line containing $n$ integers. The $i$-th integer should be the answer when erasing $a_i$.

Tags:
['binary search', 'brute force', 'data structures', 'divide and conquer', 'implementation']

Time Limit: 4000 ms
Memory Limit: 512 MB
For an array $u_1, u_2, \ldots, u_n$, define

  * a prefix maximum as an index $i$ such that $u_i>u_j$ for all $j<i$;    * a suffix maximum as an index $i$ such that $u_i>u_j$ for all $j>i$;    * an ascent as an index $i$ ($i>1$) such that $u_i>u_{i-1}$. 

You are given three cost arrays: $[a_1, a_2, \ldots, a_n]$, $[b_1, b_2, \ldots, b_n]$, and $[c_0, c_1, \ldots, c_{n-1}]$.

Define the cost of an array that has $x$ prefix maximums, $y$ suffix maximums, and $z$ ascents as $a_x\cdot b_y\cdot c_z$.

Let the sum of costs of all permutations of $1,2,\ldots,n$ be $f(n)$. Find $f(1)$, $f(2)$, ..., $f(n)$ modulo $998\,244\,353$.

Input Format:
The first line contains an integer $n$ ($1\le n\le 700$).

The second line contains $n$ integers $a_1,\ldots,a_n$ ($0\le a_i<998\,244\,353$).

The third line contains $n$ integers $b_1,\ldots,b_n$ ($0\le b_i<998\,244\,353$).

The fourth line contains $n$ integers $c_0,\ldots,c_{n-1}$ ($0\le c_i<998\,244\,353$).

Output Format:
Print $n$ integers: the $i$-th one is $f(i)$ modulo $998\,244\,353$.

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

Time Limit: 5000 ms
Memory Limit: 512 MB
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet.

The game is pretty simple. The screen represents a coordinate grid such that:

  * the X-axis is directed from left to right;    * the Y-axis is directed from bottom to top;    * the center of the screen has coordinates $(0, 0)$. 

At the beginning of the game, the character is located in the center, and $n$ coins appear on the screen — the $i$-th coin is at coordinates $(x_i, y_i)$. The coordinates of all coins are different and not equal to $(0, 0)$.

In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $(x, y)$, then it can end up at any of the coordinates $(x, y + 1)$, $(x + 1, y + 1)$, $(x + 1, y)$, $(x + 1, y - 1)$, $(x, y - 1)$, $(x - 1, y - 1)$, $(x - 1, y)$, $(x - 1, y + 1)$.

If the character ends up at the coordinates with a coin, then Monocarp collects that coin.

After Monocarp makes a move, all coins fall down by $1$, that is, they move from $(x, y)$ to $(x, y - 1)$. You can assume that the game field is infinite in all directions.

Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 500$) — the number of coins.

In the $i$-th of the next $n$ lines, two integers $x_i$ and $y_i$ ($-50 \le x_i, y_i \le 50$) are written — the coordinates of the $i$-th coin. The coordinates of all coins are different. No coin is located at $(0, 0)$.

Output Format:
For each coin, print "YES" if Monocarp can collect it. Otherwise, print "NO".

Tags:
['implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.

A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.

For example, consider the string abac:

  * a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences;    * a, b, c, ab, ba, ac, aba, bac and abac are its substrings. 

Your task is to calculate the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.

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

The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters.

The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Latin letters.

Output Format:
For each test case, print a single integer — the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.

If a person is asked to leave a review for the movie, then:

  * if that person liked the movie, they will leave a positive review, and the movie's rating will increase by $1$;    * if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by $1$;    * otherwise, they will leave a neutral review, and the movie's rating will not change. 

Every person will review exactly one movie — and for every person, you can choose which movie they will review.

The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.

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 second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-1 \le a_i \le 1$), where $a_i$ is equal to $-1$ if the first movie was disliked by the $i$-th viewer; equal to $1$ if the first movie was liked; and $0$ if the attitude is neutral.

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($-1 \le b_i \le 1$), where $b_i$ is equal to $-1$ if the second movie was disliked by the $i$-th viewer; equal to $1$ if the second movie was liked; and $0$ if the attitude is neutral.

Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, print a single integer — the maximum possible rating of the company, if for each person, choose which movie to leave a review on.

Tags:
['greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get $1$ experience point, and every time you melt an item, you also get $1$ experience point.

There are $n$ classes of weapons you can forge and $m$ types of metal ingots.

You can create one weapon of the $i$-th class, spending $a_i$ ingots of metal of the same type. Melting a weapon of the $i$-th class (which you crafted earlier) returns you $b_i$ ingots of the type of metal it was made of.

You have $c_j$ metal ingots of the $j$-th type, and you know that you can craft a weapon of any class from any metal type. Each combination of a weapon class and a metal type can be used any number of times.

What is the maximum total amount of experience you can earn by crafting and melting weapons?

Input Format:
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) — the number of weapon classes and metal types.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the number of ingots you need to forge one weapon of the $i$-th class.

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < a_i$), where $b_i$ is the number of ingots you return by melting one weapon of the $i$-th class you forged earlier.

The fourth line contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le 10^9$) — the number of ingots you have of the corresponding metal type.

Output Format:
Print one integer — the maximum total experience points you can gain by repeatedly forging and melting weapons.

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

Time Limit: 3000 ms
Memory Limit: 256 MB
Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once.

Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to the closest element in $a$ which is not equal to $a_i$. In other words, $b_i = \min \limits_{j \in [1, n], a_j \ne a_i} |i - j|$.

For example, if $a = [1, 1, 2, 3, 3, 3, 3, 1]$, then $b = [2, 1, 1, 1, 2, 2, 1, 1]$.

Calculate the number of different arrays $b$ you can obtain if you consider all possible arrays $a$, and print it modulo $998244353$.

Input Format:
The only line of the input contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le \min(n, 10)$).

Output Format:
Print one integer — the number of different arrays $b$ you can obtain, taken modulo $998244353$.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given a matrix, consisting of $n$ rows and $m$ columns.

You can perform two types of actions on it:

  * paint the entire column in blue;    * paint the entire row in red. 

Note that you cannot choose which color to paint the row or column.

In one second, you can perform either one action or multiple actions at the same time. If you perform one action, it will be free. If you perform $k > 1$ actions at the same time, it will cost $k^2$ coins. When multiple actions are performed at the same time, for each cell affected by actions of both types, the color can be chosen independently.

You are asked to process $q$ queries. Before each query, all cells become colorless. Initially, there are no restrictions on the color of any cells. In the $i$-th query, a restriction of the following form is added:

  * $x_i~y_i~c_i$ — the cell in row $x_i$ in column $y_i$ should be painted in color $c_i$. 

Thus, after $i$ queries, there are $i$ restrictions on the required colors of the matrix cells. After each query, output the minimum cost of painting the matrix according to the restrictions.

Input Format:
The first line contains three integers $n, m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) — the size of the matrix and the number of queries.

In the $i$-th of the next $q$ lines, two integers $x_i, y_i$ and a character $c_i$ ($1 \le x_i \le n$; $1 \le y_i \le m$; $c_i \in$ {'R', 'B'}, where 'R' means red, and 'B' means blue) — description of the $i$-th restriction. The cells in all queries are pairwise distinct.

Output Format:
Print $q$ integers — after each query, output the minimum cost of painting the matrix according to the restrictions.

Tags:
['dfs and similar', 'divide and conquer', 'graphs']

Time Limit: 6000 ms
Memory Limit: 512 MB