Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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

{"inputs": ["6\n2 2 2\nNE\n3 2 2\nNNE\n6 2 1\nNNEESW\n6 10 10\nNNEESW\n3 4 2\nNEE\n4 5 5\nNEWS"], "outputs": ["YES\nNO\nYES\nYES\nYES\nNO"]}


Question:
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


Question:
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


Question:
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


Question:
Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$^{\text{∗}}$ which has an exit at vertex $1$, and Alice starts at some vertex $v$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.

Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$^{\text{†}}$ of the tree, Alice loses.

Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $1\le v\le n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.

Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

$^{\text{∗}}$A tree is a connected simple graph which has $n$ vertices and $n-1$ edges.

$^{\text{†}}$A leaf is a vertex that is connected to exactly one edge.

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$ ($2\le n\le 2\cdot 10^5$) — the number of vertices in the tree.

The $i$-th of the next $n - 1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$ and $x_i \neq y_i$) — the edges of the tree. 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 $2\cdot 10^5$.

Output Format:
For each test case, output $n$ integers on one line — the probabilities of Alice escaping starting from vertex $1, 2, \ldots, n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.

Tags:
['combinatorics', 'dfs and similar', 'dp', 'games', 'greedy', 'math', 'probabilities', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Note that the memory limit is unusual.

The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations: $\times$ is done before $+$.

Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!

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

The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) — the number of integers and the target, respectively.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\le a_i\le 10^4$) — the elements of the array $a$.

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

Output Format:
For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.

You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['bitmasks', 'brute force', 'dp', 'implementation']

Time Limit: 3000 ms
Memory Limit: 32 MB


Question:
You are given a positive integer $k$ and a set $S$ of all integers from $l$ to $r$ (inclusive).

You can perform the following two-step operation any number of times (possibly zero):

  1. First, choose a number $x$ from the set $S$, such that there are at least $k$ multiples of $x$ in $S$ (including $x$ itself);    2. Then, remove $x$ from $S$ (note that nothing else is removed).

Find the maximum possible number of operations that can be performed.

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

The only line of each test case contains three integers $l$, $r$, and $k$ ($1\le l\le r\leq 10^9$, $1\leq k\le r-l+1$) — the minimum integer in $S$, the maximum integer in $S$, and the parameter $k$.

Output Format:
For each test case, output a single integer — the maximum possible number of operations that can be performed.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You have a binary string$^{\text{∗}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$.

Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \le i \le n-1$):

  * First, you choose an index $k$ such that $1\le k\le |s| - 1$ and $s_{k} \neq s_{k+1}$. If it is impossible to choose such an index, you lose;    * Then, you replace $s_ks_{k+1}$ with $r_i$. Note that this decreases the length of $s$ by $1$.

If all the $n-1$ operations are performed successfully, you win.

Determine whether it is possible for you to win this game.

$^{\text{∗}}$A binary string is a string where each character is either $\mathtt{0}$ or $\mathtt{1}$.

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

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

The second line contains the binary string $s$ of length $n$ ($s_i=\mathtt{0}$ or $\mathtt{1}$).

The third line contains the binary string $r$ of length $n-1$ ($r_i=\mathtt{0}$ or $\mathtt{1}$).

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

Output Format:
For each test case, print "YES" (without quotes) if you can win the game, and "NO" (without quotes) 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:
['games', 'greedy', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Hello, Codeforces Forcescode!

Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode.

Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$.

Now he has hacked into the backend of Forcescode and will select an interval $[l,r]$ ($1\le l\le r\le n$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way:

  * Initially, his rating is $x=0$;    * For each $1\le i\le n$, after the $i$-th contest,      * If $l\le i\le r$, this contest will be skipped, and the rating will remain unchanged;      * Otherwise, his rating will be updated according to the following rules:        * If $a_i>x$, his rating $x$ will increase by $1$;        * If $a_i=x$, his rating $x$ will remain unchanged;        * If $a_i<x$, his rating $x$ will decrease by $1$.

You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $[l,r]$ optimally. Note that Kevin has to skip at least one contest.

Input Format:
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 5\cdot 10^4$) — the number of test cases. The description of 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 contests.

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$) — the performance ratings in the contests.

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 rating after the recalculation if Kevin chooses the interval optimally.

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

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given an undirected graph with $n$ vertices and $m$ edges.

You can perform the following operation at most $2\cdot \max(n,m)$ times:

  * Choose three distinct vertices $a$, $b$, and $c$, then for each of the edges $(a,b)$, $(b,c)$, and $(c,a)$, do the following:      * If the edge does not exist, add it. On the contrary, if it exists, remove it.

A graph is called cool if and only if one of the following holds:

  * The graph has no edges, or    * The graph is a tree.

You have to make the graph cool by performing the above operations. Note that you can use at most $2\cdot \max(n,m)$ operations.

It can be shown that there always exists at least one solution.

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

The first line of each test case contains two integers $n$ and $m$ ($3\le n\le 10^5$, $0\le m\le \min\left(\frac{n(n-1)}{2},2\cdot 10^5\right)$) — the number of vertices and the number of edges.

Then $m$ lines follow, the $i$-th line contains two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$) — the two nodes that the $i$-th edge connects.

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

It is guaranteed that there are no self-loops or multiple-edges in the given graph.

Output Format:
For each test case, in the first line output an integer $k$ ($0\le k\le 2\cdot \max(n, m)$) — the number of operations.

Then output $k$ lines, the $i$-th line containing three distinct integers $a$, $b$, and $c$ ($1\le a,b,c\le n$) — the three integers you choose in the $i$-th operation.

If there are multiple solutions, you can output any of them.

Tags:
['constructive algorithms', 'data structures', 'dfs and similar', 'graphs', 'trees']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
For two integers $x$ and $y$ ($x,y\ge 2$), we will say that $x$ is a generator of $y$ if and only if $x$ can be transformed to $y$ by performing the following operation some number of times (possibly zero):

  * Choose a divisor $d$ ($d\ge 2$) of $x$, then increase $x$ by $d$.

For example,

  * $3$ is a generator of $8$ since we can perform the following operations: $3 \xrightarrow{d = 3} 6 \xrightarrow{d = 2} 8$;    * $4$ is a generator of $10$ since we can perform the following operations: $4 \xrightarrow{d = 4} 8 \xrightarrow{d = 2} 10$;    * $5$ is not a generator of $6$ since we cannot transform $5$ into $6$ with the operation above.

Now, Kevin gives you an array $a$ consisting of $n$ pairwise distinct integers ($a_i\ge 2$).

You have to find an integer $x\ge 2$ such that for each $1\le i\le n$, $x$ is a generator of $a_i$, or determine that such an integer does not exist.

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

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

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($2\le a_i\le 4\cdot 10^5$) — the elements in the array $a$. It is guaranteed that the elements are pairwise distinct.

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

Output Format:
For each test case, output a single integer $x$ — the integer you found. Print $-1$ if there does not exist a valid $x$.

If there are multiple answers, you may output any of them.

Tags:
['brute force', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$).

Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j\le n-1$):

  * There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p=[p_0,p_1,p_2,\ldots,p_m]$ such that:     * $p_0=i$, $p_m=j$;      * For each $0\leq x\le m-1$, either $p_{x+1}=(p_x+1)\bmod n$ or $p_{x+1}=(p_{x}-1)\bmod n$;      * For each $0\le x\le y\le m-1$ satisfying $x+y=m-1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.

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 number of test cases. The description of the test cases follows.

The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) — the number of vertices in the cycle.

The second line contains a string $c$ of length $n$ ($c_i=\texttt{R}$ or $\texttt{B}$) — the color of each edge.

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

Output Format:
For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) 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:
['constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$.

Kevin can perform several operations on the array. Each operation is one of the following two types:

  * Prefix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_j$ by $1$;    * Suffix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $x\le j\le n$, increases $a_j$ by $1$.

In the country of KDOI, people think that the integer $v$ is balanced. Thus, Iris gives Kevin an array $c$ consisting of $n$ integers and defines the beauty of the array $a$ as follows:

  * Initially, set $b=0$;    * For each $1\le i\le n$, if $a_i=v$, add $c_i$ to $b$;    * The beauty of $a$ is the final value of $b$.

Kevin wants to maximize the beauty of $a$ after all the operations. However, he had already performed $m$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations.

You have to help Kevin find the maximum possible beauty if he optimally performs the new operations.

However, to make sure that you are not just rolling the dice, Kevin gives you an integer $V$, and you need to solve the problem for each $1\le v\le V$.

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

The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) — the length of the array $a$, the number of initial operations, and the number that Kevin gives you.

The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1\le c_i\le 10^9$) — the elements in the array $c$.

Then $m$ lines follow, the $i$-th line containing a character $op$ and an integer $x$ ($op=\mathtt{L}$ or $\mathtt{R}$, $1\le x\le n$) — the type of the $i$-th operation and the selected index.

  * If $op=\mathtt{L}$, this operation is a prefix addition on index $x$;    * If $op=\mathtt{R}$, this operation is a suffix addition on index $x$.

It is guaranteed that:

  * the sum of $n$ over all test cases does not exceed $2\cdot 10^5$;    * the sum of $m$ over all test cases does not exceed $2\cdot 10^5$;    * the sum of $V^2$ over all test cases does not exceed $4\cdot 10^6$.

Output Format:
For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$.

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

Time Limit: 3000 ms
Memory Limit: 1024 MB


Question:
Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day.

Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it had the message the day before. Note that each day, each edge chooses its appearance independently.

Calculate the expected number of days before all the vertices have the message, modulo $998\,244\,353$.

Input Format:
The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$).

Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) — there is an undirected edge between $u$ and $v$, and it has a probability of appearance of $\frac{p}{q}$ each day.

It is guaranteed that there are no self-loops or multiple-edges in the graph and that the graph is connected if all of the edges appear.

Additional constraint in the input: Let $g_{i,j}$ be the probability of appearance of the edge between $i$ and $j$ ($g_{i,j}=0$ if there is no edge between $i$ and $j$). It is guaranteed that for any $S\subseteq\\{1,2,\ldots,n\\}$ ($|S|\ge 1$), $$ \prod_{i\in S}\left(\prod_{j\in\\{1,2,\ldots,n\\}\setminus S}(1-g_{i,j})\right)\not\equiv1\pmod{998\,244\,353}. $$

Output Format:
Print a single integer in the only line of the output — the expected number of days, modulo $998\,244\,353$.

Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

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

Time Limit: 12000 ms
Memory Limit: 1024 MB


Question:
Kevin has recently learned the definition of variance. For an array $a$ of length $n$, the variance of $a$ is defined as follows:

  * Let $x=\dfrac{1}{n}\displaystyle\sum_{i=1}^n a_i$, i.e., $x$ is the mean of the array $a$;    * Then, the variance of $a$ is $$ V(a)=\frac{1}{n}\sum_{i=1}^n(a_i-x)^2. $$

Now, Kevin gives you an array $a$ consisting of $n$ integers, as well as an integer $k$. You can perform the following operation on $a$:

  * Select an interval $[l,r]$ ($1\le l\le r\le n$), then for each $l\le i\le r$, increase $a_i$ by $k$.

For each $1\le p\le m$, you have to find the minimum possible variance of $a$ after exactly $p$ operations are performed, independently for each $p$.

For simplicity, you only need to output the answers multiplied by $n^2$. It can be proven that the results are always integers.

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

The first line of each test case contains three integers $n$, $m$, and $k$ ($1\le n,m\le 5000$, $\color{red}{n\cdot m\le 2\cdot 10^4}$, $1\le k\le 10^5$) — the length of the array $a$, the maximum number of operations, and the number you add to $a_i$ each time, respectively.

The second line contains $n$ integers $a_1,a_2,\ldots, a_n$ ($1\le a_i\le 10^5$) — the elements of the array $a$.

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

Output Format:
For each test case, output $m$ integers in a single line, the $p$-th integer denoting the minimum possible variance of $a$ when exactly $p$ operations are performed, multiplied by $n^2$.

Tags:
['flows', 'graphs', 'greedy']

Time Limit: 6000 ms
Memory Limit: 256 MB


Question:
While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...

The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the following manner:

  * $b_i = \min(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.    * $c_i = \max(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.

Define the score of $a$ as $\sum_{i=1}^n c_i - b_i$ (i.e. the sum of $c_i - b_i$ over all $1 \leq i \leq n$). Before you calculate the score, you can shuffle the elements of $a$ however you want.

Find the maximum score that you can get if you shuffle the elements of $a$ optimally.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) — the number of elements in $a$.

The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) — the elements of the array $a$.

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

Output Format:
For each test case, output the maximum score that you can get.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
For an arbitrary binary string $t$$^{\text{∗}}$, let $f(t)$ be the number of non-empty subsequences$^{\text{†}}$ of $t$ that contain only $\mathtt{0}$, and let $g(t)$ be the number of non-empty subsequences of $t$ that contain at least one $\mathtt{1}$.

Note that for $f(t)$ and for $g(t)$, each subsequence is counted as many times as it appears in $t$. E.g., $f(\mathtt{000}) = 7, g(\mathtt{100}) = 4$.

We define the oneness of the binary string $t$ to be $|f(t)-g(t)|$, where for an arbitrary integer $z$, $|z|$ represents the absolute value of $z$.

You are given a positive integer $n$. Find a binary string $s$ of length $n$ such that its oneness is as small as possible. If there are multiple strings, you can print any of them.

$^{\text{∗}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$.

$^{\text{†}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements. For example, subsequences of $\mathtt{1011101}$ are $\mathtt{0}$, $\mathtt{1}$, $\mathtt{11111}$, $\mathtt{0111}$, but not $\mathtt{000}$ nor $\mathtt{11100}$.

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

The only line of each test case contains an integer $n$ ($1 \leq n \leq 2\cdot10^5$) — the length of $s$.

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

Output Format:
For each test case, output $s$ on a new line. If multiple answers exist, output any.

Tags:
['combinatorics', 'constructive algorithms', 'games', 'math']

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{∗}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, there are no operators between the booleans.

Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of $n-1$ turns since there are $n$ booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally.

To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false:

  * If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation.    * Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation.

For example, the expression true or false and false is evaluated as true or (false and false) $=$ true or false $=$ true. It can be shown that the result of any compound statement is unique.

$^{\text{∗}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$

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

The first line of each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the length of the string.

The second line contains a binary string of length $n$, consisting of characters $\texttt{0}$ and $\texttt{1}$ — the list of boolean values.

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

Output Format:
For each testcase, output "YES" (without quotes) if Alice wins, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
QED is given a permutation$^{\text{∗}}$ $p$ of length $n$. He also has a string $s$ of length $n$ containing only characters $\texttt{L}$ and $\texttt{R}$. QED only likes permutations that are sorted in non-decreasing order. To sort $p$, he can select any of the following operations and perform them any number of times:

  * Choose an index $i$ such that $s_i = \texttt{L}$. Then, swap $p_i$ and $p_{i-1}$. It is guaranteed that $s_1 \neq \texttt{L}$.    * Choose an index $i$ such that $s_i = \texttt{R}$. Then, swap $p_i$ and $p_{i+1}$. It is guaranteed that $s_n \neq \texttt{R}$.

He is also given $q$ queries. In each query, he selects an index $i$ and changes $s_i$ from $\texttt{L}$ to $\texttt{R}$ (or from $\texttt{R}$ to $\texttt{L}$). Note that the changes are persistent.

After each query, he asks you if it is possible to sort $p$ in non- decreasing order by performing the aforementioned operations any number of times. Note that before answering each query, the permutation $p$ is reset to its original form.

$^{\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:
The first line contains $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $q$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq q \leq 2 \cdot 10^5$) – the length of the permutation and the number of queries.

The following line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$, $p$ is a permutation).

The following line contains $n$ characters $s_1s_2 \ldots s_n$. It is guaranteed that $s_i$ is either $\texttt{L}$ or $\texttt{R}$, $s_1 = \texttt{R}$, and $s_n = \texttt{L}$.

The following $q$ lines contain an integer $i$ ($2 \leq i \leq n-1$), denoting that $s_i$ is changed from $\texttt{L}$ to $\texttt{R}$ (or from $\texttt{R}$ to $\texttt{L}$).

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

Output Format:
For each query, output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

Tags:
['data structures', 'implementation', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["3\n5 3\n1 4 2 5 3\nRLRLL\n2\n4\n3\n8 5\n1 5 2 4 8 3 6 7\nRRLLRRRL\n4\n3\n5\n3\n4\n6 2\n1 2 3 4 5 6\nRLRLRL\n4\n5"], "outputs": ["YES\nYES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES"]}


Question:
Suppose we partition the elements of an array $b$ into any number $k$ of non-empty multisets $S_1, S_2, \ldots, S_k$, where $k$ is an arbitrary positive integer. Define the score of $b$ as the maximum value of $\operatorname{MEX}(S_1)$$^{\text{∗}}$$ + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}(S_k)$ over all possible partitions of $b$ for any integer $k$.

Envy is given an array $a$ of size $n$. Since he knows that calculating the score of $a$ is too easy for you, he instead asks you to calculate the sum of scores of all $2^n - 1$ non-empty subsequences of $a$.$^{\text{†}}$ Since this answer may be large, please output it modulo $998\,244\,353$.

$^{\text{∗}}$$\operatorname{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ that does not occur in the collection $c$. For example, $\operatorname{MEX}([0,1,2,2]) = 3$ and $\operatorname{MEX}([1,2,2]) = 0$

$^{\text{†}}$A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deleting several (possibly, zero or all) elements.

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

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

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < n$) — the elements of the array $a$.

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 the answer, modulo $998\,244\,353$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Suppose you have an array $b$. Initially, you also have a set $S$ that contains all distinct elements of $b$. The array $b$ is called orangutan-approved if it can be emptied by repeatedly performing the following operation:

  * In one operation, select indices $l$ and $r$ ($1 \leq l \leq r \leq |b|$) such that $v = b_l = b_{l+1} = \ldots = b_r$ and $v$ is present in $S$. Remove $v$ from $S$, and simultaneously remove all $b_i$ such that $l \leq i \leq r$. Then, reindex the elements $b_{r+1}, b_{r+2}, \ldots$ as $b_l, b_{l+1}, \ldots$ accordingly.

You are given an array $a$ of length $n$ and $q$ queries.

Each query consists of two indices $l$ and $r$ ($1 \le l \le r \le n$), and you need to determine whether or not the subarray $a_{l}, a_{l+1}, \ldots, a_r$ is orangutan-approved.

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

The first line of each test case contains integers $n$ and $q$ ($1 \leq n,q \leq 2 \cdot 10^5$) — the size of $a$ and the number of queries, respectively.

The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array $a$.

The following $q$ lines contain two integers $l$ and $r$ — the endpoints of the subarray for each query ($1 \leq l \leq r \leq n$).

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

Output Format:
For each query, output "YES" (without quotes) if the subarray from $l$ to $r$ is orangutan-approved, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

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

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved.

Orangutans are powerful beings—so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!

There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.

Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.

Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.

Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) — the number of planets in the universe.

The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) — the initial interval of vulnerability of the $i$-th planet.

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

Output Format:
For each test case, output an integer — the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved.

Orangutans are powerful beings—so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!

There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.

Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.

Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.

Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^6$) — the number of planets in the universe.

The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) — the initial interval of vulnerability of the $i$-th planet.

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

Output Format:
For each test case, output an integer — the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.

Tags:
['combinatorics', 'math']

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:

  * Each light is connected to exactly two switches.    * Each switch is connected to exactly one light. It's unknown which light each switch is connected to.    * When all switches are off, all lights are also off.    * If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.

Alice brings the circuit, which shows only the states of the $2n$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?

Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — 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 \le n \le 50$) — the number of lights in the circuit.

The second line of each test case contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($0 \le a_i \le 1$) — the states of the switches in the circuit. $a_i = 0$ means the $i$-th switch is off, and $a_i = 1$ means the $i$-th switch is on.

Output Format:
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.

Tags:
['greedy', 'implementation', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array $a = [1, 2, \ldots, n]$, where $n$ is odd, and an integer $k$.

Your task is to choose an odd positive integer $m$ and to split $a$ into $m$ subarrays$^{\dagger}$ $b_1, b_2, \ldots, b_m$ such that:

  * Each element of the array $a$ belongs to exactly one subarray.    * For all $1 \le i \le m$, $|b_i|$ is odd, i.e., the length of each subarray is odd.    * $\operatorname{median}([\operatorname{median}(b_1), \operatorname{median}(b_2), \ldots, \operatorname{median}(b_m)]) = k$, i.e., the median$^{\ddagger}$ of the array of medians of all subarrays must equal $k$. $\operatorname{median}(c)$ denotes the median of the array $c$.

$^{\dagger}$A subarray of the array $a$ of length $n$ is the array $[a_l, a_{l + 1}, \ldots, a_r]$ for some integers $1 \le l \le r \le n$.

$^{\ddagger}$A median of the array of odd length is the middle element after the array is sorted in non-decreasing order. For example: $\operatorname{median}([1,2,5,4,3]) = 3$, $\operatorname{median}([3,2,1]) = 2$, $\operatorname{median}([2,1,2,1,2,2,2]) = 2$.

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

The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n < 2 \cdot 10^5$, $n$ is odd) — the length of array $a$ and the desired median of the array of medians of all subarrays.

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:

  * If there is no suitable partition, output $-1$ in a single line.    * Otherwise, in the first line, output an odd integer $m$ ($1 \le m \le n$), and in the second line, output $m$ distinct integers $p_1, p_2 , p_3 , \ldots, p_m$ ($1 = p_1 < p_2 < p_3 < \ldots < p_m \le n$) — denoting the left borders of each subarray.

In detail, for a valid answer $[p_1, p_2, \ldots, p_m]$:

  * $b_1 = \left[ a_{p_1}, a_{p_1 + 1}, \ldots, a_{p_2 - 1} \right]$    * $b_2 = \left[ a_{p_2}, a_{p_2 + 1}, \ldots, a_{p_3 - 1} \right]$    * $\ldots$    * $b_m = \left[ a_{p_m}, a_{p_m + 1}, \ldots, a_n \right]$.

If there are multiple solutions, you can output any of them.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$.

You can perform the following operation any number (possibly $0$) of times:

  * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$.

Find the minimum number of operations required to make the array $a$ satisfy the condition:

  * For every pairwise distinct triplet of indices $(x, y, z)$ ($1 \le x, y, z \le n$, $x \ne y$, $y \ne z$, $x \ne z$), there exists a non-degenerate triangle with side lengths $a_x$, $a_y$ and $a_z$, i.e. $a_x + a_y > a_z$, $a_y + a_z > a_x$ and $a_z + a_x > a_y$.

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

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 minimum number of operations required.

Tags:
['binary search', 'math', 'sortings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is an interactive problem.

Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.

The monster's nerve system can be structured as a tree$^{\dagger}$ of $n$ nodes (really, everything should stop resembling trees all the time$\ldots$), numbered from $0$ to $n-1$, with node $0$ as the root.

Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values $p_1, p_2, \ldots, p_{n-1}$ of the tree, where $p_i$ ($0 \le p_i < i$) is the direct parent node of node $i$ ($1 \le i \le n - 1$).

She doesn't know exactly how the nodes are placed, but she knows a few convenient facts:

  * If we remove root node $0$ and all adjacent edges, this tree will turn into a forest consisting of only paths$^{\ddagger}$. Each node that was initially adjacent to the node $0$ will be the end of some path.    * The nodes are indexed in a way that if $1 \le x \le y \le n - 1$, then $p_x \le p_y$.    * Node $1$ has exactly two adjacent nodes (including the node $0$).

![](CDN_BASE_URL/9e3efde5fa1c3b92802e71707f427f91)| ![](CDN_BASE_URL/ba6c28030157828dcd682786de7f0527)| ![](CDN_BASE_URL/bdeedf002bf19f7cf8b52abe474df823)   ---|---|---   The tree in this picture does not satisfy the condition, because if we remove node $0$, then node $2$ (which was initially adjacent to the node $0$) will not be the end of the path $4-2-5$.| The tree in this picture does not satisfy the condition, because $p_3 \le p_4$ must hold.| The tree in this picture does not satisfy the condition, because node $1$ has only one adjacent node.      Gretel can make queries to the containment cell:

  * "? a b" ($1 \le a, b < n$, $a \ne b$) — the cell will check if the simple path between nodes $a$ and $b$ contains the node $0$.

However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most $2n - 6$ times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand?

$^{\dagger}$A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them.

$^{\ddagger}$A path is a tree whose vertices can be listed in the order $v_1, v_2, \ldots, v_k$ such that the edges are $(v_i, v_{i+1})$ ($1 \le i < k$).

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

The first line of each test case contains a single integer $n$ ($4 \le n \le 10^4$) — the number of nodes in Genokraken's nerve system.

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

Output Format:

Tags:
['constructive algorithms', 'data structures', 'graphs', 'implementation', 'interactive', 'trees', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a cyclic array $a$ with $n$ elements, where $n$ is odd. In each operation, you can do the following:

  * Choose an index $1 \le i \le n$ and increase $a_{i - 1}$ by $1$, $a_i$ by $2$, and $a_{i + 1}$ by $1$. The element before the first element is the last element because this is a cyclic array.

A cyclic array is called balanced if all its elements are equal to each other.

Find any sequence of operations to make this cyclic array balanced or determine that it is impossible. Please note that you do not have to minimize the number of operations.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 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 \le n < 2 \cdot 10^5$, $n$ is odd) — 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^{6}$) — the elements of the array $a$.

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:

  * If it is impossible to make the cyclic array balanced, output $-1$.    * Otherwise, output $n$ integers $v_1, v_2, \ldots, v_n$ ($0 \leq v_i \leq 10^{18}$) — where $v_i$ denotes the number of operations applied to index $i$. It can be proved that if any solution exists, then there exists a solution under the given constraints. If there are several solutions under the given constraints, output any of them.

Tags:
['constructive algorithms', 'data structures', 'greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Having the magical beanstalk, Jack has been gathering a lot of peanuts lately. Eventually, he has obtained $n$ pockets of peanuts, conveniently numbered $1$ to $n$ from left to right. The $i$-th pocket has $a_i$ peanuts.

Jack and his childhood friend Alice decide to play a game around the peanuts. First, Alice divides the pockets into some boxes; each box will have a non- zero number of consecutive pockets, and each pocket will, obviously, belong to exactly one box. At the same time, Alice does not change the order of the boxes, that is, the boxes are numbered in ascending order of the indices of the pockets in them.

After that, Alice and Jack will take turns alternately, with Alice going first.

At each turn, the current player will remove a positive number of peanuts from exactly one pocket which belongs to the leftmost non-empty box (i.e., the leftmost box containing at least one non-empty pocket). In other words, if we number the boxes from left to right, then each player can only pick peanuts from the pocket in the $j$-th box ($j \ge 2$) only if the $(j - 1)$-th box has no peanuts left. The player who cannot make a valid move loses.

Alice is sure she will win since she has the advantage of dividing the pockets into boxes herself. Thus, she wanted to know how many ways there are for her to divide the peanuts into boxes at the start of the game so that she will win, assuming both players play optimally. Can you help her with the calculation?

As the result can be very large, output it modulo $998\,244\,353$.

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

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 number of peanuts in each pocket.

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

Output Format:
For each test case, output a single integer — the number of ways for Alice to divide the pockets into boxes at the start of the game to guarantee her win, assuming both players play optimally, modulo $998\,244\,353$.

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start.

On the $i$-th move, the current player will move the dot in some direction by $2\cdot i-1$ units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction.

In other words, the following will happen:

  1. Sakurako will change the position of the dot by $-1$, $x = -1$ now    2. Kosuke will change the position of the dot by $3$, $x = 2$ now    3. Sakurako will change the position of the dot by $-5$, $x = -3$ now    4. $\cdots$

They will keep on playing while the absolute value of the coordinate of the dot does not exceed $n$. More formally, the game continues while $-n\le x\le n$. It can be proven that the game will always end.

Your task is to determine who will be the one who makes the last turn.

Input Format:
The first line contains one integer $t$ ($1\le t\le 100$) — the number of games that Sakurako and Kosuke played.

Each game is described by one number $n$ ($1 \le n\le 100$) — the number that defines the condition when the game ends.

Output Format:
For each of the $t$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke".

Tags:
['constructive algorithms', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB

{"inputs": ["4\n1\n6\n3\n98"], "outputs": ["Kosuke\nSakurako\nKosuke\nSakurako"]}


Question:
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there.

Kosuke is very afraid of water, so Sakurako needs to help him:

  * With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one.

More formally, she can choose a submatrix with the upper left corner located at $(i, j)$ and the lower right corner at $(p, q)$, such that $p-i=q-j$. She can then add one to each element at the intersection of the $(i + k)$-th row and the $(j + k)$-th column, for all $k$ such that $0 \le k \le p-i$.

Determine the minimum number of times Sakurako must use her magic so that there are no lakes.

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

Each test case is described as follows:

  * The first line of each test case consists of a single number $n$ ($1 \le n \le 500$).    * Each of the following $n$ lines consists of $n$ integers separated by spaces, which correspond to the heights of the mountains in the valley $a$ ($-10^5 \le a_{i,j} \le 10^5$).

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

Output Format:
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of students.

The disturbance of the line is defined as the number of neighbouring people with the same topic of interest. In other words, disturbance is the number of indices $j$ ($1 \le j < n$) such that $a_j = a_{j + 1}$.

In order to do this, you can choose index $i$ ($1\le i\le n$) and swap students at positions $i$ and $n-i+1$. You can perform any number of swaps.

Your task is to determine the minimal amount of disturbance that you can achieve by doing the operation described above any number of times.

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

Each test case is described by two lines.

  * The first line contains one integer $n$ ($2 \le n \le 10^5$) — the length of the line of students.    * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) — the topics of interest of students in line.

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

Output Format:
For each test case, output the minimal possible disturbance of the line that you can achieve.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segments of the array $a$, such that each segment is considered beautiful.

A segment $[l,r]$ is considered beautiful if $a_l + a_{l+1} + \dots + a_{r-1} + a_r=0$.

For a fixed array $a$, your task is to compute the maximum number of non- overlapping beautiful segments.

Input Format:
The first line of input contains the number $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case consists of $2$ lines.

  * The first line contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array.   * The second line contains $n$ integers $a_i$ ($-10^5 \le a_i \le 10^5$) — the elements of the array $a$.

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

Output Format:
For each test case, output a single integer: the maximum number of non- overlapping beautiful segments.

Tags:
['data structures', 'dp', 'dsu', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple.

A permutation $p$ is considered simple if for every $i$ $(1\le i \le n)$ one of the following conditions holds:

  * $p_i=i$    * $p_{p_i}=i$

For example, the permutations $[1, 2, 3, 4]$, $[5, 2, 4, 3, 1]$, and $[2, 1]$ are simple, while $[2, 3, 1]$ and $[5, 2, 1, 4, 3]$ are not.

In one operation, Kosuke can choose indices $i,j$ $(1\le i,j\le n)$ and swap the elements $p_i$ and $p_j$.

Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple.

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

Each test case is described by two lines.

  * The first line contains one integer $n$ ($1\le n \le 10^6$) — the length of the permutation $p$.    * The second line contains $n$ integers $p_i$ ($1\le p_i\le n$) — the elements of the permutation $p$.

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

It is guaranteed that $p$ is a permutation.

Output Format:
For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Kosuke is too lazy. He will not give you any legend, just the task:

Fibonacci numbers are defined as follows:

  * $f(1)=f(2)=1$.    * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$

We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divisible by $k$. For given $n$ and $k$, compute $G(n,k)$.

As this number can be too big, output it by modulo $10^9+7$.

For example: $G(3,2)=9$ because the $3$-rd Fibonacci number that is divisible by $2$ is $34$. $[1,1,\textbf{2},3,5,\textbf{8},13,21,\textbf{34}]$.

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

The first and only line contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^5$).

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

Output Format:
For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$.

Tags:
['brute force', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away.

To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina.

Also, for each guess, Kosuke assumes that Chefir could move along the edges an arbitrary number of times:

  * from vertex $a$ to vertex $b$, if $a$ is an ancestor$^{\text{∗}}$ of $b$, the stamina will not change;    * from vertex $a$ to vertex $b$, if $a$ is not an ancestor of $b$, then Chefir's stamina decreases by $1$.

If Chefir's stamina is $0$, he cannot make a move of the second type.

For each assumption, your task is to find the distance to the farthest vertex that Chefir could reach from vertex $v_i$, having $k_i$ stamina.

$^{\text{∗}}$Vertex $a$ is an ancestor of vertex $b$ if the shortest path from $b$ to the root passes through $a$.

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

Each test case is described as follows:

  * The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.   * The next $n-1$ lines contain the edges of the tree. It is guaranteed that the given edges form a tree.   * The next line consists of a single integer $q$ $(1\le q\le 2 \cdot 10^5)$, which denotes the number of guesses made by Kosuke.   * The next $q$ lines describe the guesses made by Kosuke, with two integers $v_i$, $k_i$ $(1\le v_i \le n, 0 \le k_i\le n)$.

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

Output Format:
For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina.

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
Red was ejected. They were not the imposter.

There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\cdot m+c$ is initially at $(r,c)$.

The person at $(r, c)$ decides to leave. To fill the gap, let the person who left be numbered $i$. Each person numbered $j>i$ will move to the position where the person numbered $j-1$ is initially at. The following diagram illustrates the case where $n=2$, $m=3$, $r=1$, and $c=2$.

![](CDN_BASE_URL/9b0b8e601446e3410296d7c9b1ff8763)

Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at $(r_0, c_0)$ and then moved to $(r_1, c_1)$, the Manhattan distance is $|r_0-r_1|+|c_0-c_1|$.

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

The only line of each testcase contains $4$ integers $n$, $m$, $r$, and $c$ ($1\le r\le n\le 10^6$, $1 \le c \le m \le 10^6$), where $n$ is the number of rows, $m$ is the number of columns, and $(r,c)$ is the position where the person who left is initially at.

Output Format:
For each test case, output a single integer denoting the sum of the Manhattan distances.

Tags:
['implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There are 3 heroes and 3 villains, so 6 people in total.

Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66$. If no such integer exists, print $-1$.

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

The only line of each test case contains a single integer $n$ ($1\le n\le 500$) — the length of the decimal representation.

Output Format:
For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise.

Tags:
['constructive algorithms', 'greedy', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Alya has been given a hard problem. Unfortunately, she is too busy running for student council. Please solve this problem for her.

Given an integer $n$, construct a permutation $p$ of integers $1, 2, \ldots, n$ that maximizes the value of $k$ (which is initially $0$) after the following process.

Perform $n$ operations, on the $i$-th operation ($i=1, 2, \dots, n$),

  * If $i$ is odd, $k=k\,\&\,p_i$, where $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).    * If $i$ is even, $k=k\,|\,p_i$, where $|$ denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

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

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

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 the maximum value of $k$ in the first line and output the permutation $p_1, p_2,\ldots, p_n$ in the second line.

If there are multiple such permutations, output any.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Three r there are's in strawberry.

You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero):

  * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}$ and $b_i$ is even, divide $b_i$ by $2$ and multiply $b_j$ by $2$.

Your task is to maximize the sum of the array after performing any number of such operations. Since it could be large, output this sum modulo $10^9+7$.

Since this problem is too easy, you are given an array $a$ of length $n$ and need to solve the problem for each prefix of $a$.

In other words, denoting the maximum sum of $b$ after performing any number of such operations as $f(b)$, you need to output $f([a_1])$, $f([a_1,a_2])$, $\ldots$, $f([a_1,a_2,\ldots,a_n])$ modulo $10^9+7$ respectively.

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 length of $a$.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the starting values of array $a$.

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

Output Format:
For each test case, output $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me...

You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations.

  * Increase $d$ — the damage of your weapon by $1$, costing $x$ coins.    * Attack the monster, dealing $d$ damage and costing $y$ coins.

You cannot perform the first operation for more than $k$ times in a row.

Find the minimum number of coins needed to defeat the monster by dealing at least $z$ damage.

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

The only line of each test case contains 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) — the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.

Output Format:
For each test case, output the minimum number of coins needed to defeat the monster.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This really says a lot about our society.

One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$.

You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $u$ as choosing a single node in $u$'s subtree$^{\text{∗}}$ and incrementing or decrementing its value by $1$. The order in which operations are performed on nodes is as follows:

  * For $1 \le i \le n$, the $i$-th operation will be performed on node $i$.    * For $i > n$, the $i$-th operation will be performed on the same node as operation $i - n$.

More formally, the $i$-th operation will be performed on the $(((i - 1) \bmod n) + 1)$-th node.$^{\text{†}}$

Note that you cannot skip over operations; that is, you cannot perform the $i$-th operation without first performing operations $1, 2, \ldots, i - 1$.

Find the minimum number of operations you must perform before you can make the values of all nodes equal to $0$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $0$ after finite operations, output $-1$.

$^{\text{∗}}$The subtree of a node $u$ is the set of nodes for which $u$ lies on the shortest path from this node to the root, including $u$ itself.

$^{\text{†}}$Here, $a \bmod b$ denotes the remainder from dividing $a$ by $b$.

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 two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) — the number of nodes and the root of the tree.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the starting value of each node.

Each of the next $n - 1$ lines of each test case contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) representing an undirected edge from $u$ to $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 $2000$.

Output Format:
For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved.

Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.

Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.

              let l = 1     let h = n          while l < h:       let m = floor((l + h) / 2)            if a[m] < k:         l = m + 1       else:         h = m          return l

Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.

Test $i$ is correct if the following hold:

  1. The $x_i$-th element in the array is $k_i$.    2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$.

It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.

In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.

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 $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests.

The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.

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

Output Format:
For each test case, output two integers, $r$ — the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.

Tags:
['dp', 'trees']

Time Limit: 5000 ms
Memory Limit: 256 MB