problem_id
string | problem_title
string | difficulty
string | problem_statement
string | platform
string | link
string |
---|---|---|---|---|---|
2053B | B. Outstanding Impressionist | easy | If it was so, then let's make it a deal...
— MayDay, [Gentleness](https://www.youtube.com/watch?v=mtAc_bMYBsM&list=PLj6NQzHFCvkHKIm0Vnk9LH3odqTIBRZ1Q&index=15)
Even after copying the paintings from famous artists for ten years, unfortunately, Eric is still unable to become a skillful impressionist painter. He wants to forget something, but the white bear phenomenon just keeps hanging over him.
Eric still remembers $n$ pieces of impressions in the form of an integer array. He records them as $w_1, w_2, \ldots, w_n$. However, he has a poor memory of the impressions. For each $1 \leq i \leq n$, he can only remember that $l_i \leq w_i \leq r_i$.
Eric believes that impression $i$ is unique if and only if there exists a possible array $w_1, w_2, \ldots, w_n$ such that $w_i \neq w_j$ holds for all $1 \leq j \leq n$ with $j \neq i$.
Please help Eric determine whether impression $i$ is unique for every $1 \leq i \leq n$, independently for each $i$. Perhaps your judgment can help rewrite the final story.
### Input
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 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 \leq n \leq 2\cdot 10^5$) — the number of impressions.
Then $n$ lines follow, the $i$-th containing two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq 2\cdot n$) — the minimum possible value and the maximum possible value of $w_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, output a binary string $s$ of length $n$: for each $1 \leq i \leq n$, if impression $i$ is unique, $s_i=\texttt{1}$; otherwise, $s_i=\texttt{0}$. Do not output spaces.
### Example
#### Input #1
```
5
2
1 1
1 1
4
1 3
1 3
1 3
1 3
6
3 6
2 2
1 2
1 1
3 4
2 2
7
3 4
4 4
4 4
1 3
2 5
1 4
2 2
3
4 5
4 4
5 5
```
#### Output #1
```
00
1111
100110
1001111
011
```
### Note
In the first test case, the only possible array $w$ is $[1, 1]$, making neither impression $1$ nor $2$ unique (since $w_1 = w_2$).
In the second test case, all impressions can be made unique:
- For $i = 1$, we can set $w$ to $[1, 3, 2, 3]$, in which $w_1 \neq w_2$, $w_1 \neq w_3$, and $w_1 \neq w_4$;
- For $i = 2$, we can set $w$ to $[2, 3, 1, 2]$, in which $w_2 \neq w_1$, $w_2 \neq w_3$, and $w_2 \neq w_4$;
- For $i = 3$, we can set $w$ to $[1, 1, 3, 1]$;
- For $i = 4$, we can set $w$ to $[2, 3, 3, 1]$.
In the third test case, for $i = 4$, we can set $w$ to $[3, 2, 2, 1, 3, 2]$. Thus, impression $4$ is unique. | codeforces | https://codeforces.com/problemset/problem/2053/B |
2053A | A. Tender Carpenter | easy | I would use a firework to announce, a wave to bid farewell, and a bow to say thanks: bygones are bygones; not only on the following path will I be walking leisurely and joyfully, but also the footsteps won't halt as time never leaves out flowing; for in the next year, we will meet again.
— Cocoly1990, [Goodbye 2022](https://www.luogu.com.cn/problem/P8941)
In his dream, Cocoly would go on a long holiday with no worries around him. So he would try out for many new things, such as... being a carpenter. To learn it well, Cocoly decides to become an apprentice of Master, but in front of him lies a hard task waiting for him to solve.
Cocoly is given an array $a_1, a_2,\ldots, a_n$. Master calls a set of integers $S$ stable if and only if, for any possible $u$, $v$, and $w$ from the set $S$ (note that $u$, $v$, and $w$ do not necessarily have to be pairwise distinct), sticks of length $u$, $v$, and $w$ can form a non-degenerate triangle$^{\text{∗}}$.
Cocoly is asked to partition the array $a$ into several (possibly, $1$ or $n$) non-empty continuous subsegments$^{\text{†}}$, such that: for each of the subsegments, the set containing all the elements in it is stable.
Master wants Cocoly to partition $a$ in at least two different$^{\text{‡}}$ ways. You have to help him determine whether it is possible.
$^{\text{∗}}$A triangle with side lengths $x$, $y$, and $z$ is called non-degenerate if and only if:
- $x + y > z$,
- $y + z > x$, and
- $z + x > y$.
$^{\text{†}}$A sequence $b$ is a subsegment of a sequence $c$ if $b$ can be obtained from $c$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
$^{\text{‡}}$Two partitions are considered different if and only if at least one of the following holds:
- the numbers of continuous subsegments split in two partitions are different;
- there is an integer $k$ such that the lengths of the $k$-th subsegment in two partitions are different.
### Input
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 200$) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 200$) — the length of the array $a$.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \leq 10^5$) — the elements in the array $a$.
### Output
For each test case, print $\texttt{YES}$ if there are at least two ways to partition $a$, and $\texttt{NO}$ otherwise.
You can output the answer in any case (upper or lower). For example, the strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as positive responses.
### Example
#### Input #1
```
5
4
2 3 5 7
4
115 9 2 28
5
8 4 1 6 2
6
1 5 4 1 4 7
2
100000 100000
```
#### Output #1
```
YES
NO
NO
YES
YES
```
### Note
In the first test case, here are two possible partitions:
- $[2, 3], [5, 7]$, since
- $[2, 3]$ is stable because sticks of lengths $(2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)$ respectively can all form non-degenerate triangles.
- $[5, 7]$ is stable because sticks of lengths $(5, 5, 5), (5, 5, 7), (5, 7, 7), (7, 7, 7)$ respectively can all form non-degenerate triangles.
- and $[2], [3, 5], [7]$, since
- $[2]$ is stable because sticks of lengths $(2, 2, 2)$ respectively can form a non-degenerate triangle.
- $[3, 5]$ is stable because sticks of lengths $(3, 3, 3), (3, 3, 5), (3, 5, 5), (5, 5, 5)$ respectively can all form non-degenerate triangles.
- $[7]$ is stable because sticks of lengths $(7, 7, 7)$ respectively can form a non-degenerate triangle.
Note that some other partitions also satisfy the constraints, such as $[2], [3], [5], [7]$ and $[2], [3], [5, 7]$.
In the second test case, Cocoly can only partition each element as a single subsegment, resulting in $[115], [9], [2], [28]$. Since we only have one possible partition, the answer is $\texttt{NO}$.
In the third test case, please note that the partition $[8, 4], [1], [6], [2]$ does not satisfy the constraints, because $\{8, 4\}$ is not a stable set: sticks of lengths $4$, $4$, and $8$ cannot form a non-degenerate triangle. | codeforces | https://codeforces.com/problemset/problem/2053/A |
2053C | C. Bewitching Stargazer | easy | I'm praying for owning a transparent heart; as well as eyes with tears more than enough...
— Escape Plan, [Brightest Star in the Dark](https://www.youtube.com/watch?v=GPnymcrXgX0)
Iris looked at the stars and a beautiful problem emerged in her mind. She is inviting you to solve it so that a meteor shower is believed to form.
There are $n$ stars in the sky, arranged in a row. Iris has a telescope, which she uses to look at the stars.
Initially, Iris observes stars in the segment $[1, n]$, and she has a lucky value of $0$. Iris wants to look for the star in the middle position for each segment $[l, r]$ that she observes. So the following recursive procedure is used:
- First, she will calculate $m = \left\lfloor \frac{l+r}{2} \right\rfloor$.
- If the length of the segment (i.e. $r - l + 1$) is even, Iris will divide it into two equally long segments $[l, m]$ and $[m+1, r]$ for further observation.
- Otherwise, Iris will aim the telescope at star $m$, and her lucky value will increase by $m$; subsequently, if $l \neq r$, Iris will continue to observe two segments $[l, m-1]$ and $[m+1, r]$.
Iris is a bit lazy. She defines her laziness by an integer $k$: as the observation progresses, she will not continue to observe any segment $[l, r]$ with a length strictly less than $k$. In this case, please predict her final lucky value.
### Input
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of test cases follows.
The only line of each test case contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2\cdot 10^9$).
### Output
For each test case, output a single integer — the final lucky value.
### Example
#### Input #1
```
6
7 2
11 3
55 13
5801 6
8919 64
8765432 1
```
#### Output #1
```
12
18
196
1975581
958900
38416403456028
```
### Note
In the first test case, at the beginning, Iris observes $[1, 7]$. Since $[1, 7]$ has an odd length, she aims at star $4$ and therefore increases her lucky value by $4$. Then it is split into $2$ new segments: $[1, 3]$ and $[5, 7]$. The segment $[1, 3]$ again has an odd length, so Iris aims at star $2$ and increases her lucky value by $2$. Then it is split into $2$ new segments: $[1, 1]$ and $[3, 3]$, both having a length less than $2$, so no further observation is conducted. For range $[5, 7]$, the progress is similar and the lucky value eventually increases by $6$. Therefore, the final lucky value is $4 + 2 + 6 = 12$.
In the last test case, Iris finally observes all the stars and the final lucky value is $1 + 2 + \cdots + 8\,765\,432 = 38\,416\,403\,456\,028$. | codeforces | https://codeforces.com/problemset/problem/2053/C |
2053D | D. Refined Product Optimality | easy | As a tester, when my solution has a different output from the example during testing, I suspect the author first.
— Chris, [a comment](https://codeforces.com/blog/entry/133116?#comment-1190579)
Although Iris occasionally sets a problem where the solution is possibly wrong, she still insists on creating problems with her imagination; after all, everyone has always been on the road with their stubbornness... And like ever before, Iris has set a problem to which she gave a wrong solution, but Chris is always supposed to save it! You are going to play the role of Chris now:
- Chris is given two arrays $a$ and $b$, both consisting of $n$ integers.
- Iris is interested in the largest possible value of $P = \prod\limits_{i=1}^n \min(a_i, b_i)$ after an arbitrary rearrangement of $b$. Note that she only wants to know the maximum value of $P$, and no actual rearrangement is performed on $b$.
- There will be $q$ modifications. Each modification can be denoted by two integers $o$ and $x$ ($o$ is either $1$ or $2$, $1 \leq x \leq n$). If $o = 1$, then Iris will increase $a_x$ by $1$; otherwise, she will increase $b_x$ by $1$.
- Iris asks Chris the maximum value of $P$ for $q + 1$ times: once before any modification, then after every modification.
- Since $P$ might be huge, Chris only needs to calculate it modulo $998\,244\,353$.
Chris soon worked out this problem, but he was so tired that he fell asleep. Besides saying thanks to Chris, now it is your turn to write a program to calculate the answers for given input data.
Note: since the input and output are large, you may need to optimize them for this problem.
For example, in C++, it is enough to use the following lines at the start of the main() function:
```
`int main() {<br/> std::ios::sync_with_stdio(false);<br/> std::cin.tie(nullptr); std::cout.tie(nullptr);<br/>}<br/>````
### Input
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \leq t \leq 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 $q$ ($1 \leq n \leq 2\cdot 10^5$, $1 \leq q \leq 2\cdot 10^5$) — the length of the array and the number of operations.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 5\cdot 10^8$) — the array $a$.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 5\cdot 10^8$) — the array $b$.
Then $q$ lines follow, each line contains two integers $o$ and $x$ ($o \in \{1, 2\}$, $1 \leq x \leq n$), representing an operation.
It's guaranteed that the sum of $n$ and the sum of $q$ over all test cases does not exceed $4\cdot 10^5$, respectively.
### Output
For each test case, output $q + 1$ integers in a line, representing the answers that Chris will calculate, modulo $998\,244\,353$.
### Example
#### Input #1
```
4
3 4
1 1 2
3 2 1
1 3
2 3
1 1
2 1
6 8
1 4 2 7 3 5
7 6 5 6 3 3
2 5
1 6
1 5
1 5
1 5
2 3
2 3
1 6
13 8
7 7 6 6 5 5 5 2 2 3 4 5 1
1 4 1 9 6 6 9 1 5 1 3 8 4
2 2
2 11
2 4
2 4
1 7
1 1
2 12
1 5
5 3
10000000 20000000 30000000 40000000 50000000
10000000 20000000 30000000 40000000 50000000
1 1
2 2
2 1
```
#### Output #1
```
2 3 3 6 6
840 840 1008 1344 1680 2016 2016 2016 2352
2116800 2646000 3528000 3528000 3528000 4233600 4838400 4838400 4838400
205272023 205272023 205272023 264129429
```
### Note
In the first test case:
- Before the modifications, Chris can rearrange $b$ to $[1, 2, 3]$ so that $P = \prod\limits_{i=1}^n \min(a_i, b_i) = 1 \cdot 1 \cdot 2 = 2$. We can prove that this is the maximum possible value. For example, if Chris rearranges $b = [2, 3, 1]$, $P$ will be equal $1 \cdot 1 \cdot 1 = 1 < 2$, which is not optimal.
- After the first modification, Chris can rearrange $b$ to $[1, 2, 3]$ so that $P = 1 \cdot 1 \cdot 3 = 3$, which is maximized.
- After the second modification, Chris can rearrange $b$ to $[2, 2, 3]$ so that $P = 1 \cdot 1 \cdot 3 = 3$, which is maximized.
- After the third modification, Chris can rearrange $b$ to $[2, 2, 3]$ so that $P = 6$, which is maximized.
- After the fourth modification, Chris can rearrange $b$ to $[2, 2, 4]$ so that $P = 6$, which is maximized. | codeforces | https://codeforces.com/problemset/problem/2053/D |
2043B | B. Digits | easy | Artem wrote the digit $d$ on the board exactly $n!$ times in a row. So, he got the number $dddddd \dots ddd$ (exactly $n!$ digits).
Now he is curious about which odd digits from $1$ to $9$ divide the number written on the board.
### Input
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The next $t$ test cases follow.
Each test case consists of a single line containing two integers $n$ and $d$ ($2 \le n \le 10^9$, $1 \le d \le 9$).
### Output
For each test case, output the odd digits in ascending order that divide the number written on the board.
### Example
#### Input #1
```
3
2 6
7 1
8 5
```
#### Output #1
```
1 3
1 3 7 9
1 3 5 7 9
```
### Note
The factorial of a positive integer $n$ ($n!$) is the product of all integers from $1$ to $n$. For example, the factorial of $5$ is $1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120$. | codeforces | https://codeforces.com/problemset/problem/2043/B |
2043C | C. Sums on Segments | easy | You are given an array $a$ of $n$ integers, where all elements except for at most one are equal to $-1$ or $1$. The remaining element $x$ satisfies $-10^9 \le x \le 10^9$.
Find all possible sums of subarrays of $a$, including the empty subarray, whose sum is defined as $0$. In other words, find all integers $x$ such that the array $a$ has at least one subarray (possibly empty) with sum equal to $x$. A subarray is a contiguous subsegment of an array.
Output these sums in ascending order. Each sum should be printed only once, even if it is achieved by multiple subarrays.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then, $t$ test cases follow.
Each test case consists of two lines:
- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the array.
- The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the elements of the array $a$. In the array $a$, there is at most one element that is neither $1$ nor $-1$.
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output two lines:
- In the first line, print a single integer — the number of distinct subarray sums.
- In the second line, print these sums in ascending order.
Each sum should be printed only once, even if it is produced by multiple subarrays.
### Example
#### Input #1
```
5
5
1 -1 10 1 1
5
-1 -1 -1 -1 -1
2
-1 2
2
7 1
3
1 4 -1
```
#### Output #1
```
8
-1 0 1 2 9 10 11 12
6
-5 -4 -3 -2 -1 0
4
-1 0 1 2
4
0 1 7 8
6
-1 0 1 3 4 5
```
### Note
Let's define $a[i,j]$ as the subarray of $a$ from position $i$ to position $j$.
Consider the first test case of the example:
- $-1$ is produced by $a[2,2]$;
- $0$ is produced by the empty subarray;
- $1$ is produced by $a[4,4]$;
- $2$ is produced by $a[4,5]$;
- $9$ is produced by $a[2,3]$;
- $10$ is produced by $a[1,3]$;
- $11$ is produced by $a[3,4]$;
- $12$ is produced by $a[3,5]$. | codeforces | https://codeforces.com/problemset/problem/2043/C |
2043A | A. Coin Transformation | easy | Initially, you have a coin with value $n$. You can perform the following operation any number of times (possibly zero):
- transform one coin with value $x$, where $x$ is greater than $3$ ($x>3$), into two coins with value $\lfloor \frac{x}{4} \rfloor$.
What is the maximum number of coins you can have after performing this operation any number of times?
### Input
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of one line containing one integer $n$ ($1 \le n \le 10^{18}$).
### Output
For each test case, print one integer — the maximum number of coins you can have after performing the operation any number of times.
### Example
#### Input #1
```
4
1
5
16
1000000000000000000
```
#### Output #1
```
1
2
4
536870912
```
### Note
In the first example, you have a coin of value $1$, and you can't do anything with it. So, the answer is $1$.
In the second example, you can transform a coin of value $5$ into two coins with value $1$.
In the third example, you can transform a coin of value $16$ into two coins with value $4$. Each of the resulting coins can be transformed into two coins with value $1$. | codeforces | https://codeforces.com/problemset/problem/2043/A |
2043D | D. Problem about GCD | easy | Given three integers $l$, $r$, and $G$, find two integers $A$ and $B$ ($l \le A \le B \le r$) such that their greatest common divisor (GCD) equals $G$ and the distance $|A - B|$ is maximized.
If there are multiple such pairs, choose the one where $A$ is minimized. If no such pairs exist, output "-1 -1".
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of test cases. Then, $t$ test cases follow.
Each test case consists of a single line containing three integers $l, r, G$ ($1 \le l \le r \le 10^{18}$; $1 \le G \le 10^{18}$) — the range boundaries and the required GCD.
### Output
For each test case, output two integers $A$ and $B$ — the solution to the problem, or "-1 -1" if no such pair exists.
### Example
#### Input #1
```
4
4 8 2
4 8 3
4 8 4
5 7 6
```
#### Output #1
```
4 6
-1 -1
4 8
6 6
``` | codeforces | https://codeforces.com/problemset/problem/2043/D |
2051B | B. Journey | easy | Monocarp decided to embark on a long hiking journey.
He decided that on the first day he would walk $a$ kilometers, on the second day he would walk $b$ kilometers, on the third day he would walk $c$ kilometers, on the fourth day, just like on the first, he would walk $a$ kilometers, on the fifth day, just like on the second, he would walk $b$ kilometers, on the sixth day, just like on the third, he would walk $c$ kilometers, and so on.
Monocarp will complete his journey on the day when he has walked at least $n$ kilometers in total. Your task is to determine the day on which Monocarp will complete his journey.
### Input
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of one line containing four integers $n$, $a$, $b$, $c$ ($1 \le n \le 10^9$; $1 \le a, b, c \le 10^6$).
### Output
For each test case, output one integer — the day on which Monocarp will have walked at least $n$ kilometers in total and will complete his journey.
### Example
#### Input #1
```
4
12 1 5 3
6 6 7 4
16 3 4 1
1000000000 1 1 1
```
#### Output #1
```
5
1
6
1000000000
```
### Note
In the first example, over the first four days, Monocarp will cover $1 + 5 + 3 + 1 = 10$ kilometers. On the fifth day, he will cover another $5$ kilometers, meaning that in total over five days he will have covered $10 + 5 = 15$ kilometers. Since $n = 12$, Monocarp will complete his journey on the fifth day.
In the second example, Monocarp will cover $6$ kilometers on the first day. Since $n = 6$, Monocarp will complete his journey on the very first day.
In the third example, Monocarp will cover $3 + 4 + 1 + 3 + 4 + 1 = 16$ kilometers over the first six days. Since $n = 16$, Monocarp will complete his journey on the sixth day. | codeforces | https://codeforces.com/problemset/problem/2051/B |
2051D | D. Counting Pairs | easy | You are given a sequence $a$, consisting of $n$ integers, where the $i$-th element of the sequence is equal to $a_i$. You are also given two integers $x$ and $y$ ($x \le y$).
A pair of integers $(i, j)$ is considered interesting if the following conditions are met:
- $1 \le i < j \le n$;
- if you simultaneously remove the elements at positions $i$ and $j$ from the sequence $a$, the sum of the remaining elements is at least $x$ and at most $y$.
Your task is to determine the number of interesting pairs of integers for the given sequence $a$.
### Input
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 three integers $n, x, y$ ($3 \le n \le 2 \cdot 10^5$, $1 \le x \le y \le 2 \cdot 10^{14}$);
- The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$).
Additional constraint on the input: the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output one integer — the number of interesting pairs of integers for the given sequence $a$.
### Example
#### Input #1
```
7
4 8 10
4 6 3 6
6 22 27
4 9 6 3 4 5
3 8 10
3 2 1
3 1 1
2 3 4
3 3 6
3 2 1
4 4 12
3 3 2 1
6 8 8
1 1 2 2 2 3
```
#### Output #1
```
4
7
0
0
1
5
6
```
### Note
In the first example, there are $4$ interesting pairs of integers:
1. $(1, 2)$;
2. $(1, 4)$;
3. $(2, 3)$;
4. $(3, 4)$. | codeforces | https://codeforces.com/problemset/problem/2051/D |
2051C | C. Preparing for the Exam | easy | Monocarp is preparing for his first exam at the university. There are $n$ different questions which can be asked during the exam, numbered from $1$ to $n$. There are $m$ different lists of questions; each list consists of exactly $n-1$ different questions. Each list $i$ is characterized by one integer $a_i$, which is the index of the only question which is not present in the $i$-th list. For example, if $n = 4$ and $a_i = 3$, the $i$-th list contains questions $[1, 2, 4]$.
During the exam, Monocarp will receive one of these $m$ lists of questions. Then, the professor will make Monocarp answer all questions from the list. So, Monocarp will pass only if he knows all questions from the list.
Monocarp knows the answers for $k$ questions $q_1, q_2, \dots, q_k$. For each list, determine if Monocarp will pass the exam if he receives that list.
### Input
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of three lines:
- the first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m, k \le n$);
- the second line contains $m$ distinct integers $a_1, a_2, \dots, a_m$ ($1 \le a_i \le n$; $a_i < a_{i+1}$);
- the third line contains $k$ distinct integers $q_1, q_2, \dots, q_k$ ($1 \le q_i \le n$; $q_i < q_{i+1}$).
Additional constraints on the input:
- the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
### Output
For each test case, print a string of $m$ characters. The $i$-th character should be 1 if Monocarp passes the exam if he receives the $i$-th question list, 0 if Monocarp won't pass.
### Example
#### Input #1
```
4
4 4 3
1 2 3 4
1 3 4
5 4 3
1 2 3 4
1 3 4
4 4 4
1 2 3 4
1 2 3 4
2 2 1
1 2
2
```
#### Output #1
```
0100
0000
1111
10
```
### Note
In the first test case, Monocarp knows the questions $[1, 3, 4]$. Let's consider all the question lists:
- the first list consists of questions $[2, 3, 4]$. Monocarp doesn't know the $2$-nd question, so he won't pass;
- the second list consists of questions $[1, 3, 4]$. Monocarp knows all these questions, so he will pass;
- the third list consists of questions $[1, 2, 4]$. Monocarp doesn't know the $2$-nd question, so he won't pass;
- the fourth list consists of questions $[1, 2, 3]$. Monocarp doesn't know the $2$-nd question, so he won't pass. | codeforces | https://codeforces.com/problemset/problem/2051/C |
2051E | E. Best Price | easy | A batch of Christmas trees has arrived at the largest store in Berland. $n$ customers have already come to the store, wanting to buy them.
Before the sales begin, the store needs to determine the price for one tree (the price is the same for all customers). To do this, the store has some information about each customer.
For the $i$-th customer, two integers $a_i$ and $b_i$ are known, which define their behavior:
- if the price of the product is at most $a_i$, the customer will buy a tree and leave a positive review;
- otherwise, if the price of the product is at most $b_i$, the customer will buy a tree but leave a negative review;
- otherwise, the customer will not buy a tree at all.
Your task is to calculate the maximum possible earnings for the store, given that it can receive no more than $k$ negative reviews.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $0 \le k \le n$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^9$).
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 2 \cdot 10^9$; $a_i < b_i$).
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, print a single integer — the maximum possible earnings for the store, given that it can receive no more than $k$ negative reviews.
### Example
#### Input #1
```
5
2 0
2 1
3 4
1 1
2
5
3 3
1 5 2
3 6 4
4 3
2 3 2 8
3 7 3 9
3 1
2 9 5
12 14 9
```
#### Output #1
```
2
5
9
14
15
```
### Note
Consider the example from the statement:
- In the first test case, the price should be set to $1$. Then both customers will buy one tree each and leave no negative reviews;
- In the second test case, the price should be set to $5$. Then the only customer will buy a tree and leave a negative review;
- In the third test case, the price should be set to $3$. Then all customers will buy one tree each, and the store will receive two negative reviews.
- In the fourth test case, the price should be set to $7$. Then two customers will buy one tree each, and the store will receive one negative review. | codeforces | https://codeforces.com/problemset/problem/2051/E |
2051A | A. Preparing for the Olympiad | easy | Monocarp and Stereocarp are preparing for the Olympiad. There are $n$ days left until the Olympiad. On the $i$-th day, if Monocarp plans to practice, he will solve $a_i$ problems. Similarly, if Stereocarp plans to practice on the same day, he will solve $b_i$ problems.
Monocarp can train on any day he wants. However, Stereocarp watches Monocarp and follows a different schedule: if Monocarp trained on day $i$ and $i < n$, then Stereocarp will train on day $(i+1)$.
Monocarp wants to organize his training process in a way that the difference between the number of problems he solves and the number of problems Stereocarp solves is as large as possible. Formally, Monocarp wants to maximize the value of $(m-s)$, where $m$ is the number of problems he solves, and $s$ is the number of problems Stereocarp solves. Help Monocarp determine the maximum possible difference in the number of solved problems between them.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$).
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 100$).
### Output
For each test case, print a single integer — the maximum possible difference between the number of problems Monocarp solves and the number of problems Stereocarp solves.
### Example
#### Input #1
```
4
2
3 2
2 1
1
5
8
3
1 1 1
2 2 2
6
8 2 5 6 2 6
8 2 7 4 3 4
```
#### Output #1
```
4
5
1
16
```
### Note
Let's analyze the example from the statement:
- In the first test case, it is optimal for Monocarp to train both days; then Stereocarp will train on day $2$.
- In the second test case, it is optimal for Monocarp to train on the only day, and Stereocarp will not train at all.
- In the third test case, it is optimal for Monocarp to train on the last day (and only on that day).
- In the fourth test case, it is optimal for Monocarp to train on days $1, 3, 4, 6$; then Stereocarp will train on days $2, 4, 5$. | codeforces | https://codeforces.com/problemset/problem/2051/A |
2049B | B. pspspsps | easy | Cats are attracted to pspspsps, but Evirir, being a dignified dragon, is only attracted to pspspsps with oddly specific requirements...
Given a string $s = s_1s_2\ldots s_n$ of length $n$ consisting of characters p, s, and . (dot), determine whether a permutation$^{\text{∗}}$ $p$ of length $n$ exists, such that for all integers $i$ ($1 \le i \le n$):
- If $s_i$ is p, then $[p_1, p_2, \ldots, p_i]$ forms a permutation (of length $i$);
- If $s_i$ is s, then $[p_i, p_{i+1}, \ldots, p_{n}]$ forms a permutation (of length $n-i+1$);
- If $s_i$ is ., then there is no additional restriction.
$^{\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
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 500$), the length of $s$.
The second line of each test case contains a string $s$ of length $n$ that consists of the characters p, s, and ..
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.
### Output
For each test case, output YES or NO on a line. Output YES if there is such a permutation and NO otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
### Example
#### Input #1
```
9
4
s.sp
6
pss..s
5
ppppp
2
sp
4
.sp.
8
psss....
1
.
8
pspspsps
20
....................
```
#### Output #1
```
YES
NO
YES
YES
NO
NO
YES
NO
YES
```
### Note
For the first test case, one permutation that works is $p = [3, 4, 1, 2]$. The restrictions are as follows:
- $s_1 =$ s: $[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$ forms a permutation.
- $s_2 =$ .: No additional restriction.
- $s_3 =$ s: $[p_3, p_4] = [1, 2]$ forms a permutation.
- $s_4 =$ p: $[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$ forms a permutation.
For the second test case, it can be proven that there is no permutation that satisfies all restrictions.
For the third test case, one permutation that satisfies the constraints is $p = [1, 2, 3, 4, 5]$. | codeforces | https://codeforces.com/problemset/problem/2049/B |
2049A | A. MEX Destruction | easy | Evirir the dragon snuck into a wizard's castle and found a mysterious contraption, and their playful instincts caused them to play with (destroy) it...
Evirir the dragon found an array $a_1, a_2, \ldots, a_n$ of $n$ non-negative integers.
In one operation, they can choose a non-empty subarray$^{\\text{∗}}$ $b$ of $a$ and replace it with the integer $\\operatorname{mex}(b)$$^{\\text{†}}$. They want to use this operation any number of times to make $a$ only contain zeros. It can be proven that this is always possible under the problem constraints.
What is the minimum number of operations needed?
$^{\text{∗}}$An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
$^{\text{†}}$The minimum excluded (MEX) of a collection of integers $f_1, f_2, \ldots, f_k$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $f$.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 200$). 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 length of $a$.
The second line of each test case contains $n$ space-separated integers, $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $500$.
### Output
For each test case, output a single integer on a line, the minimum number of operations needed to make $a$ contain only zeros.
### Example
#### Input #1
```
10
4
0 1 2 3
6
0 0 0 0 0 0
5
1 0 1 0 1
5
3 1 4 1 5
4
3 2 1 0
7
9 100 0 89 12 2 3
4
0 3 9 0
7
0 7 0 2 0 7 0
1
0
2
0 1
```
#### Output #1
```
1
0
2
1
1
2
1
2
0
1
```
### Note
In the first test case, Evirir can choose the subarray $b = [1, 2, 3]$ and replace it with $\operatorname{mex}(1, 2, 3) = 0$, changing $a$ from $[0, \underline{1, 2, 3}]$ to $[0, 0]$ (where the chosen subarray is underlined). Therefore, the answer is $1$.
In the second test case, $a$ already contains only $0$s, so no operation is needed.
In the third test case, Evirir can change $a$ as follows: $[1, \underline{0, 1, 0, 1}] \to [\underline{1, 2}] \to [0]$. Here, $\operatorname{mex}(0, 1, 0, 1) = 2$ and $\operatorname{mex}(1, 2) = 0$.
In the fourth test case, Evirir can choose $b$ to be the entire array $a$, changing $a$ from $[\underline{3, 1, 4, 1, 5}]$ to $[0]$. | codeforces | https://codeforces.com/problemset/problem/2049/A |
2049C | C. MEX Cycle | easy | Evirir the dragon has many friends. They have 3 friends! That is one more than the average dragon.
You are given integers $n$, $x$, and $y$. There are $n$ dragons sitting in a circle. The dragons are numbered $1, 2, \ldots, n$. For each $i$ ($1 \le i \le n$), dragon $i$ is friends with dragon $i - 1$ and $i + 1$, where dragon $0$ is defined to be dragon $n$ and dragon $n + 1$ is defined to be dragon $1$. Additionally, dragons $x$ and $y$ are friends with each other (if they are already friends, this changes nothing). Note that all friendships are mutual.
Output $n$ non-negative integers $a_1, a_2, \ldots, a_n$ such that for each dragon $i$ ($1 \le i \le n$), the following holds:
- Let $f_1, f_2, \ldots, f_k$ be the friends of dragon $i$. Then $a_i = \operatorname{mex}(a_{f_1}, a_{f_2}, \ldots, a_{f_k})$.$^{\text{∗}}$
$^{\text{∗}}$The minimum excluded (MEX) of a collection of integers $c_1, c_2, \ldots, c_m$ is defined as the smallest non-negative integer $t$ which does not occur in the collection $c$.
### Input
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 and only line of each test case contains three integers $n$, $x$, $y$ ($3 \le n \le 2 \cdot 10^5$, $1 \le x < y \le n$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output $n$ space-separated non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) on a line that satisfy the condition in the statement. If there are multiple solutions, print any of them. It can be proven that under the problem constraints, a solution with $0 \le a_i \le 10^9$ always exists.
### Example
#### Input #1
```
7
5 1 3
4 2 4
6 3 5
7 3 6
3 2 3
5 1 5
6 2 5
```
#### Output #1
```
0 2 1 0 1
1 2 1 0
1 2 0 1 2 0
0 1 2 0 1 0 1
2 0 1
1 0 2 1 0
0 1 2 0 2 1
```
### Note
For the first test case:
- $i = 1$: Dragon $1$'s friends are dragons $2, 3, 5$. $\operatorname{mex}(a_2, a_3, a_5) = \operatorname{mex}(2, 1, 1) = 0 = a_1$, so the condition for dragon $1$ is satisfied.
- $i = 2$: Dragon $2$'s friends are dragons $1, 3$. $\operatorname{mex}(a_1, a_3) = \operatorname{mex}(0, 1) = 2 = a_2$.
- $i = 3$: Dragon $3$'s friends are dragons $1, 2, 4$. $\operatorname{mex}(a_1, a_2, a_4) = \operatorname{mex}(0, 2, 0) = 1 = a_3$.
- $i = 4$: Dragon $4$'s friends are dragons $3, 5$. $\operatorname{mex}(a_3, a_5) = \operatorname{mex}(1, 1) = 0 = a_4$.
- $i = 5$: Dragon $5$'s friends are dragons $1, 4$. $\operatorname{mex}(a_1, a_4) = \operatorname{mex}(0, 0) = 1 = a_5$. | codeforces | https://codeforces.com/problemset/problem/2049/C |
2048D | D. Kevin and Competition Memories | easy | Kevin used to get into Rio's Memories, and in Rio's Memories, a series of contests was once held. Kevin remembers all the participants and all the contest problems from that time, but he has forgotten the specific rounds, the distribution of problems, and the exact rankings.
There are $ m $ problems in total, with the $ i $-th problem having a difficulty of $ b_i $. Let each contest consist of $ k $ problems, resulting in a total of $ \lfloor \frac{m}{k} \rfloor $ contests. This means that you select exactly $ \lfloor \frac{m}{k} \rfloor \cdot k $ problems for the contests in any combination you want, with each problem being selected at most once, and the remaining $m\bmod k$ problems are left unused. For example, if $m = 17$ and $k = 3$, you should create exactly $5$ contests consisting of $3$ problems each, and exactly $2$ problems will be left unused.
There are $ n $ participants in the contests, with Kevin being the $1$-st participant. The $ i $-th participant has a rating of $ a_i $. During the contests, each participant solves all problems with a difficulty not exceeding their rating, meaning the $ i $-th participant solves the $ j $-th problem if and only if $ a_i \geq b_j $. In each contest, Kevin's rank is one plus the number of participants who solve more problems than he does.
For each $ k = 1, 2, \ldots, m $, Kevin wants to know the minimum sum of his ranks across all $ \lfloor \frac{m}{k} \rfloor $ contests. In other words, for some value of $k$, after selecting the problems for each contest, you calculate the rank of Kevin in each contest and sum up these ranks over all $ \lfloor \frac{m}{k} \rfloor $ contests. Your goal is to minimize this value.
Note that contests for different values of $k$ are independent. It means that for different values of $k$, you can select the distribution of problems into the contests independently.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ($ 1 \le t \le 5\cdot 10^4 $).
The first line of each test case contains two integers $ n $ and $ m $ ($ 1 \le n, m \leq 3\cdot 10^5 $) — the number of participants and the number of problems.
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 rating of each participant.
The third line of each test case contains $ m $ integers $ b_1, b_2, \ldots, b_m $ ($ 0 \le b_i \le 10^9 $) — the difficulty of each problem.
It is guaranteed that both the sum of $ n $ and the sum of $ m $ over all test cases do not exceed $ 3 \cdot 10^5 $.
### Output
For each test case, output $m$ integers — the minimum sum of Kevin's ranks for each $ k = 1, 2, \ldots, m$.
### Example
#### Input #1
```
4
4 4
4 3 7 5
2 5 4 6
5 5
5 0 4 8 6
1 3 9 2 7
6 7
1 1 4 5 1 4
1 9 1 9 8 1 0
7 6
1 9 1 9 8 1 0
1 1 4 5 1 4
```
#### Output #1
```
7 4 2 3
6 2 1 1 2
7 3 2 1 1 1 1
15 9 5 4 4 4
```
### Note
For the first test case:
When $k=1$, since each contest only contains one problem, the distribution is in fact unique. For example, in the contest which only includes the third problem (which has a difficulty of $4$), all participants except the $2$-nd can solve it. Since no one solves strictly more problems than Kevin, his ranking in this contest is $1$. Similarly, in all $4$ contests, Kevin's rankings are $1,3,1,2$, and the sum is $7$.
When $k=2$, one optimal way is to choose the $1$-st and the $3$-rd problem to form a contest, while the $2$-nd and $4$-th for another. In the former contest, $4$ participants respectively solve $2,1,2,2$ problems, so Kevin's ranking is $1$; in the latter one, they respectively solve $0,0,2,1$, since there are $2$ participants ($3$-rd and $4$-th) solve more problems than Kevin, his ranking is $1+2=3$. Thus the answer is $1+3=4$. It can be proven that there's no way to achieve a lower sum.
When $k=3$, we can simply choose the $1$-st, the $3$-rd, and the $4$-th problem to make a contest, and Kevin has a ranking of $2$, which is optimal.
When $k=4$, since there's only one contest, the distribution is also unique, and Kevin's ranking is $3$. | codeforces | https://codeforces.com/problemset/problem/2048/D |
2048A | A. Kevin and Combination Lock | easy | Kevin is trapped in Lakeside Village by Grace. At the exit of the village, there is a combination lock that can only be unlocked if Kevin solves it.
The combination lock starts with an integer $ x $. Kevin can perform one of the following two operations zero or more times:
1. If $ x \neq 33 $, he can select two consecutive digits $ 3 $ from $ x $ and remove them simultaneously. For example, if $ x = 13\,323 $, he can remove the second and third $ 3 $, changing $ x $ to $ 123 $.
2. If $ x \geq 33 $, he can change $ x $ to $ x - 33 $. For example, if $ x = 99 $, he can choose this operation to change $ x $ to $ 99 - 33 = 66 $.
When the value of $ x $ on the combination lock becomes $ 0 $, Kevin can unlock the lock and escape from Lakeside Village. Please determine whether it is possible for Kevin to unlock the combination lock and escape.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$).
The only line of each test case contains a positive integer $x$ ($1\leq x\leq 10^9$).
### Output
For each test case, output "YES" or "NO" (without quotes) in one line, representing whether Kevin can unlock the combination lock and escape. 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.
### Example
#### Input #1
```
5
165
6369
666
114514
133333332
```
#### Output #1
```
YES
YES
NO
NO
YES
```
### Note
For the first test case, $165\xrightarrow{-33}132\xrightarrow{-33}99\xrightarrow{-33}66\xrightarrow{-33}33\xrightarrow{-33}0$.
For the second test case, $6369\xrightarrow{-33}6{\color{red}{33}}6\xrightarrow{\text{remove "33"}}66\xrightarrow{-33}33\xrightarrow{-33}0$.
For the third test case, it can be proven that, regardless of the operations performed, $666$ cannot be transformed into $0$. | codeforces | https://codeforces.com/problemset/problem/2048/A |
2048B | B. Kevin and Permutation | easy | Kevin is a master of permutation-related problems. You are taking a walk with Kevin in Darkwoods, and during your leisure time, he wants to ask you the following question.
Given two positive integers $ n $ and $ k $, construct a permutation$^{\text{∗}}$ $ p $ of length $ n $ to minimize the sum of the minimum values of all subarrays$^{\text{†}}$ of length $ k $. Formally, you need to minimize
$$ \sum_{i=1}^{n-k+1}\left( \min_{j=i}^{i+k-1} p_j\right). $$
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).
$^{\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. Two subarrays are considered different if the sets of positions of the deleted elements are different.
### Input
Each test consists of multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$).
The only line of each test case contains two integers $ n $ and $ k $ ($ 1\le k\le n\le 10^5 $).
It is guaranteed that the sum of $ n $ over all test cases doesn't exceed $ 10^5 $.
### Output
For each test case, output $ n $ integers on a single line — the permutation $ p $ you constructed.
If there are multiple answers, you can print any of them.
### Example
#### Input #1
```
3
4 2
6 1
8 3
```
#### Output #1
```
3 1 2 4
5 2 1 6 4 3
4 6 2 8 3 1 5 7
```
### Note
In the first test case, with $ k=2 $, consider all subarrays of length $ 2 $: the minimum value of $ p_1,p_2 $ is $ 1 $, the minimum value of $ p_2,p_3 $ is $ 1 $, and the minimum value of $ p_3,p_4 $ is $ 2 $. The sum $ 1+1+2=4 $ is the smallest among all possible permutations.
In the second test case, all subarrays of length $ 1 $ have minimum values of $ 5, 2, 1, 6, 4, 3 $, and the sum $ 5+2+1+6+4+3=21 $ is proven to be the smallest. | codeforces | https://codeforces.com/problemset/problem/2048/B |
2048C | C. Kevin and Binary Strings | easy | Kevin discovered a binary string $s$ that starts with 1 in the river at Moonlit River Park and handed it over to you. Your task is to select two non-empty substrings$^{\text{∗}}$ of $s$ (which can be overlapped) to maximize the XOR value of these two substrings.
The XOR of two binary strings $a$ and $b$ is defined as the result of the $\oplus$ operation applied to the two numbers obtained by interpreting $a$ and $b$ as binary numbers, with the leftmost bit representing the highest value. Here, $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
The strings you choose may have leading zeros.
$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$).
The only line of each test case contains a binary string $s$ that starts with 1 ($1\le\lvert s\rvert\le 5000$).
It is guaranteed that the sum of $\lvert s\rvert$ over all test cases doesn't exceed $5000$.
### Output
For each test case, output four integers $l_1, r_1, l_2, r_2$ ($1 \le l_1 \le r_1 \le |s|$, $1 \le l_2 \le r_2 \le |s|$) — in the case the two substrings you selected are $s_{l_1} s_{l_1 + 1} \ldots s_{r_1}$ and $s_{l_2} s_{l_2 + 1} \ldots s_{r_2}$.
If there are multiple solutions, print any of them.
### Example
#### Input #1
```
5
111
1000
10111
11101
1100010001101
```
#### Output #1
```
2 2 1 3
1 3 1 4
1 5 1 4
3 4 1 5
1 13 1 11
```
### Note
In the first test case, we can choose $ s_2=\texttt{1} $ and $ s_1 s_2 s_3=\texttt{111} $, and $ \texttt{1}\oplus\texttt{111}=\texttt{110} $. It can be proven that it is impossible to obtain a larger result. Additionally, $ l_1=3$, $r_1=3$, $l_2=1$, $r_2=3 $ is also a valid solution.
In the second test case, $ s_1 s_2 s_3=\texttt{100} $, $ s_1 s_2 s_3 s_4=\texttt{1000} $, the result is $ \texttt{100}\oplus\texttt{1000}=\texttt{1100} $, which is the maximum. | codeforces | https://codeforces.com/problemset/problem/2048/C |
2044E | E. Insane Problem | easy | Wave is given five integers $k$, $l_1$, $r_1$, $l_2$, and $r_2$. Wave wants you to help her count the number of ordered pairs $(x, y)$ such that all of the following are satisfied:
- $l_1 \leq x \leq r_1$.
- $l_2 \leq y \leq r_2$.
- There exists a non-negative integer $n$ such that $\frac{y}{x} = k^n$.
### Input
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 five integers $k$, $l_1$, $r_1$, $l_2$, and $r_2$ ($2 \leq k \leq 10^9, 1 \leq l_1 \leq r_1 \leq 10^9, 1 \leq l_2 \leq r_2 \leq 10^9$).
### Output
For each test case, output the number of matching ordered pairs $(x, y)$ on a new line.
### Example
#### Input #1
```
5
2 2 6 2 12
2 1 1000000000 1 1000000000
3 5 7 15 63
1000000000 1 5 6 1000000000
15 17 78 2596 20914861
```
#### Output #1
```
12
1999999987
6
1
197
```
### Note
In the third test case, the matching ordered pairs are the following:
- $(5,15)$
- $(5,45)$
- $(6,18)$
- $(6,54)$
- $(7,21)$
- $(7,63)$
In the fourth test case, the only valid ordered pair is $(1,1\,000\,000\,000)$ | codeforces | https://codeforces.com/problemset/problem/2044/E |
2044A | A. Easy Problem | easy | Cube is given an integer $n$. She wants to know how many ordered pairs of positive integers $(a,b)$ there are such that $a=n-b$. Since Cube is not very good at math, please help her!
### Input
The first line contains an integer $t$ ($1 \leq t \leq 99$) — the number of test cases.
The only line of each test case contains an integer $n$ ($2 \leq n \leq 100$).
### Output
For each test case, output the number of ordered pairs $(a, b)$ on a new line.
### Example
#### Input #1
```
3
2
4
6
```
#### Output #1
```
1
3
5
```
### Note
In the first test case, the only ordered pair that works is $(a,b)=(1,1)$.
In the second test case, the three ordered pairs of $(a,b)$ that work are $(3,1), (2,2), (1,3)$. | codeforces | https://codeforces.com/problemset/problem/2044/A |
2044D | D. Harder Problem | easy | Given a sequence of positive integers, a positive integer is called a mode of the sequence if it occurs the maximum number of times that any positive integer occurs. For example, the mode of $[2,2,3]$ is $2$. Any of $9$, $8$, or $7$ can be considered to be a mode of the sequence $[9,9,8,8,7,7]$.
You gave UFO an array $a$ of length $n$. To thank you, UFO decides to construct another array $b$ of length $n$ such that $a_i$ is a mode of the sequence $[b_1, b_2, \ldots, b_i]$ for all $1 \leq i \leq n$.
However, UFO doesn't know how to construct array $b$, so you must help her. Note that $1 \leq b_i \leq n$ must hold for your array for all $1 \leq i \leq n$.
### Input
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$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of $a$.
The following line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output $n$ numbers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) on a new line. It can be shown that $b$ can always be constructed. If there are multiple possible arrays, you may print any.
### Example
#### Input #1
```
4
2
1 2
4
1 1 1 2
8
4 5 5 5 1 1 2 1
10
1 1 2 2 1 1 3 3 1 1
```
#### Output #1
```
1 2
1 1 2 2
4 5 5 1 1 2 2 3
1 8 2 2 1 3 3 9 1 1
```
### Note
Let's verify the correctness for our sample output in test case $2$.
- At $i = 1$, $1$ is the only possible mode of $[1]$.
- At $i = 2$, $1$ is the only possible mode of $[1, 1]$.
- At $i = 3$, $1$ is the only possible mode of $[1, 1, 2]$.
- At $i = 4$, $1$ or $2$ are both modes of $[1, 1, 2, 2]$. Since $a_i = 2$, this array is valid. | codeforces | https://codeforces.com/problemset/problem/2044/D |
2044B | B. Normal Problem | easy | A string consisting of only characters 'p', 'q', and 'w' is painted on a glass window of a store. Ship walks past the store, standing directly in front of the glass window, and observes string $a$. Ship then heads inside the store, looks directly at the same glass window, and observes string $b$.
Ship gives you string $a$. Your job is to find and output $b$.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases.
The only line of each test case contains a string $a$ ($1 \leq |a| \leq 100$) — the string Ship observes from outside the store. It is guaranteed that $a$ only contains characters 'p', 'q', and 'w'.
### Output
For each test case, output string $b$, the string Ship observes from inside the store, on a new line.
### Example
#### Input #1
```
5
qwq
ppppp
pppwwwqqq
wqpqwpqwwqp
pqpqpqpq
```
#### Output #1
```
pwp
qqqqq
pppwwwqqq
qpwwpqwpqpw
pqpqpqpq
``` | codeforces | https://codeforces.com/problemset/problem/2044/B |
2044G1 | G1. Medium Demon Problem (easy version) | easy | This is the easy version of the problem. The key difference between the two versions is highlighted in bold.
A group of $n$ spiders has come together to exchange plushies. Initially, each spider has $1$ plushie. Every year, if spider $i$ has at least one plushie, he will give exactly one plushie to spider $r_i$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, if any spider has more than $1$ plushie at any point in time, they will throw all but $1$ away.
The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $1$ can never be stable.
Find the first year in which the process becomes stable.
### Input
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$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of spiders.
The following line contains $n$ integers $r_1, r_2, \ldots, r_n$ ($1 \leq r_i \leq n, r_i \neq i$) — the recipient of the plushie of each spider.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output an integer on a new line, the first year in which the process becomes stable.
### Example
#### Input #1
```
5
2
2 1
5
2 3 4 5 1
5
2 1 4 2 3
5
4 1 1 5 4
10
4 3 9 1 6 7 9 10 10 3
```
#### Output #1
```
2
2
5
4
5
```
### Note
For the second test case:
- At year $1$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Then, year $1$'s exchange happens.
- At year $2$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Since this array is the same as the previous year, this year is stable.
For the third test case:
- At year $1$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Then, year $1$'s exchange happens.
- At year $2$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 0]$. Then, year $2$'s exchange happens. Note that even though two spiders gave spider $2$ plushies, spider $2$ may only keep one plushie.
- At year $3$, the following array shows the number of plushies each spider has: $[1, 1, 0, 1, 0]$. Then, year $3$'s exchange happens.
- At year $4$, the following array shows the number of plushies each spider has: $[1, 1, 0, 0, 0]$. Then, year $4$'s exchange happens.
- At year $5$, the following array shows the number of plushies each spider has: $[1, 1, 0, 0, 0]$. Since this array is the same as the previous year, this year is stable. | codeforces | https://codeforces.com/problemset/problem/2044/G1 |
2044C | C. Hard Problem | easy | Ball is the teacher in Paperfold University. The seats of his classroom are arranged in $2$ rows with $m$ seats each.
Ball is teaching $a + b + c$ monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that $a$ of them only want to sit in row $1$, $b$ of them only want to sit in row $2$, and $c$ of them have no preference. Only one monkey may sit in each seat, and each monkey's preference must be followed if it is seated.
What is the maximum number of monkeys that Ball can seat?
### Input
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
Each test case contains four integers $m$, $a$, $b$, and $c$ ($1 \leq m, a, b, c \leq 10^8$).
### Output
For each test case, output the maximum number of monkeys you can seat.
### Example
#### Input #1
```
5
10 5 5 10
3 6 1 1
15 14 12 4
1 1 1 1
420 6 9 69
```
#### Output #1
```
20
5
30
2
84
```
### Note
In the second test case, $6$ monkeys want to sit in the front row, but only $3$ seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is $3+2=5$. | codeforces | https://codeforces.com/problemset/problem/2044/C |
2040A | A. Game of Division | easy | You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$ and an integer $k$.
Two players are playing a game. The first player chooses an index $1 \le i \le n$. Then the second player chooses a different index $1 \le j \le n, i \neq j$. The first player wins if $|a_i - a_j|$ is not divisible by $k$. Otherwise, the second player wins.
We play as the first player. Determine whether it is possible to win, and if so, which index $i$ should be chosen.
The absolute value of a number $x$ is denoted by $|x|$ and is equal to $x$ if $x \ge 0$, and $-x$ otherwise.
### Input
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) — 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 n \le 100$; $1 \le k \le 100$) — the length of the array and the number $k$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 100$) — the elements of the array $a$.
### Output
For each test case, if it is impossible for the first player to win, print "NO" (without quotes).
Otherwise, print "YES" (without quotes) and on the next line the appropriate index $1 \le i \le n$. If there are multiple solutions, print any of them.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer.
### Example
#### Input #1
```
7
3 2
1 2 3
4 2
1 2 4 5
5 3
10 7 3 4 5
5 3
1 31 15 55 36
2 1
17 17
2 2
17 18
1 3
6
```
#### Output #1
```
YES
2
NO
YES
3
NO
NO
YES
2
YES
1
```
### Note
In the first test case, the first player can choose $a_2 = 2$. Then:
- If the second player chooses $a_1 = 1$, the resulting difference is $|2 - 1| = 1$ which is not divisible by $k = 2$.
- If the second player chooses $a_3 = 3$, the resulting difference is $|2 - 3| = 1$ which is not divisible by $k = 2$.
In the second test case:
- If the first player chooses $a_1 = 1$ and then the second player chooses $a_4 = 5$, the resulting difference is $|1 - 5| = 4$ which is divisible by $k = 2$.
- If the first player chooses $a_2 = 2$ and then the second player chooses $a_3 = 4$, the resulting difference is $|2 - 4| = 2$ which is divisible by $k = 2$.
- If the first player chooses $a_3 = 4$ and then the second player chooses $a_2 = 2$, the resulting difference is $|4 - 2| = 2$ which is divisible by $k = 2$.
- If the first player chooses $a_4 = 5$ and then the second player chooses $a_1 = 1$, the resulting difference is $|5 - 1| = 4$ which is divisible by $k = 2$.
In any case, the second player wins. | codeforces | https://codeforces.com/problemset/problem/2040/A |
2040B | B. Paint a Strip | easy | You have an array of zeros $a_1, a_2, \ldots, a_n$ of length $n$.
You can perform two types of operations on it:
1. Choose an index $i$ such that $1 \le i \le n$ and $a_i = 0$, and assign $1$ to $a_i$;
2. Choose a pair of indices $l$ and $r$ such that $1 \le l \le r \le n$, $a_l = 1$, $a_r = 1$, $a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil$, and assign $1$ to $a_i$ for all $l \le i \le r$.
What is the minimum number of operations of the first type needed to make all elements of the array equal to one?
### Input
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 only line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array.
Note that there is no limit on the sum of $n$ over all test cases.
### Output
For each test case, print one integer — the minimum number of needed operations of first type.
### Example
#### Input #1
```
4
1
2
4
20
```
#### Output #1
```
1
2
2
4
```
### Note
In the first test case, you can perform an operation of the $1$st type with $i = 1$.
In the second test case, you can perform the following sequence of operations:
1. Operation of $1$st type, $i = 1$. After performing this operation, the array will look like this: $[1, 0]$.
2. Operation of $1$st type, $i = 2$. After performing this operation, the array will look like this: $[1, 1]$.
The sequence of operations in the second test case
In the third test case, you can perform the following sequence of operations:
1. Operation of $1$st type, $i = 1$. After performing this operation, the array will look like this: $[1, 0, 0, 0]$.
2. Operation of $1$st type, $i = 4$. After performing this operation, the array will look like this: $[1, 0, 0, 1]$.
3. Operation of $2$nd type, $l = 1$, $r = 4$. On this segment, $a_l + \ldots + a_r = a_1 + a_2 + a_3 + a_4 = 2$, which is not less than $\lceil\frac{r - l + 1}{2}\rceil = 2$. After performing this operation, the array will look like this: $[1, 1, 1, 1]$.
The sequence of operations in the third test case | codeforces | https://codeforces.com/problemset/problem/2040/B |
2040C | C. Ordered Permutations | easy | Consider a permutation$^{\text{∗}}$ $p_1, p_2, \ldots, p_n$ of integers from $1$ to $n$. We can introduce the following sum for it$^{\text{†}}$:
$$S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)$$
Let us consider all permutations of length $n$ with the maximum possible value of $S(p)$. Output the $k$-th of them in lexicographical$^{\text{‡}}$order, or report that there are less than $k$ of them.
$^{\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).
$^{\text{†}}$For example:
- For the permutation $[1, 2, 3]$ the value of $S(p)$ is equal to $\min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) =$ $1 + 1 + 1 + 2 + 2 + 3 = 10$
- For the permutation $[2, 4, 1, 3]$ the value of $S(p)$ is equal to $\min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ +$ $ \min(4) + \min(4, 1) + \min(4, 1, 3) \ +$ $\min(1) + \min(1, 3) \ +$ $\min(3) =$ $2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17$.
$^{\text{‡}}$An array $a$ is lexicographically smaller than an array $b$ if and only if one of the following holds:
- $a$ is a prefix of $b$, but $a \ne b$; or
- in the first position where $a$ and $b$ differ, the array $a$ has a smaller element than the corresponding element in $b$.
### Input
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 only line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{12}$) — the length of the permutation and the index number of the desired permutation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$.
### Output
For each test case, if there are less than $k$ suitable permutations, print $-1$.
Otherwise, print the $k$-th suitable permutation.
### Example
#### Input #1
```
6
3 2
3 3
4 11
4 6
6 39
7 34
```
#### Output #1
```
1 3 2
2 3 1
-1
2 4 3 1
-1
2 3 4 5 7 6 1
```
### Note
Let us calculate the required sum for all permutations of length $3$ (ordered lexicographically):
PermutationValue of $S(p)
$$[1, 2, 3]$$
10
$$[1, 3, 2]$$
10
$$[2, 1, 3]$$
9
$$[2, 3, 1]$$
10
$$[3, 1, 2]$$
9
$$[3, 2, 1]$$
10$
In the first test case, you have to print the second suitable permutation of length $3$. Looking at the table, we see that it is the permutation $[1, 3, 2]$.
In the second test case, you have to print the third suitable permutation of length $3$. Looking at the table, we see that it is the permutation $[2, 3, 1]$. | codeforces | https://codeforces.com/problemset/problem/2040/C |
2050E | E. Three Strings | easy | You are given three strings: $a$, $b$, and $c$, consisting of lowercase Latin letters. The string $c$ was obtained in the following way:
1. At each step, either string $a$ or string $b$ was randomly chosen, and the first character of the chosen string was removed from it and appended to the end of string $c$, until one of the strings ran out. After that, the remaining characters of the non-empty string were added to the end of $c$.
2. Then, a certain number of characters in string $c$ were randomly changed.
For example, from the strings $a=\color{red}{\text{abra}}$ and $b=\color{blue}{\text{cada}}$, without character replacements, the strings $\color{blue}{\text{ca}}\color{red}{\text{ab}}\color{blue}{\text{d}}\color{red}{\text{ra}}\color{blue}{\text{a}}$, $\color{red}{\text{abra}}\color{blue}{\text{cada}}$, $\color{red}{\text{a}}\color{blue}{\text{cada}}\color{red}{\text{bra}}$ could be obtained.
Find the minimum number of characters that could have been changed in string $c$.
### Input
The first line of the input contains a single integer $t$ ($1 \le t \le 10^3$) — the number of test cases.
The first line of each test case contains one string of lowercase Latin letters $a$ ($1 \leq |a| \leq 10^3$) — the first string, where $|a|$ denotes the length of string $a$.
The second line of each test case contains one string of lowercase Latin letters $b$ ($1 \leq |b| \leq 10^3$) — the second string, where $|b|$ denotes the length of string $b$.
The third line of each test case contains one string of lowercase Latin letters $c$ ($|c| = |a| + |b|$) — the third string.
It is guaranteed that the sum of $|a|$ across all test cases does not exceed $2 \cdot 10^3$. Also, the sum of $|b|$ across all test cases does not exceed $2 \cdot 10^3$.
### Output
For each test case, output a single integer — the minimum number of characters that could have been changed in string $c$.
### Example
#### Input #1
```
7
a
b
cb
ab
cd
acbd
ab
ba
aabb
xxx
yyy
xyxyxy
a
bcd
decf
codes
horse
codeforces
egg
annie
egaegaeg
```
#### Output #1
```
1
0
2
0
3
2
3
``` | codeforces | https://codeforces.com/problemset/problem/2050/E |
2050B | B. Transfusion | easy | You are given an array $a$ of length $n$. In one operation, you can pick an index $i$ from $2$ to $n-1$ inclusive, and do one of the following actions:
- Decrease $a_{i-1}$ by $1$, then increase $a_{i+1}$ by $1$.
- Decrease $a_{i+1}$ by $1$, then increase $a_{i-1}$ by $1$.
After each operation, all the values must be non-negative. Can you make all the elements equal after any number of operations?
### Input
First line of input consists of one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
First line of each test case consists of one integer $n$ ($3 \le n \le 2\cdot 10^5$).
Second line of each test case consists of $n$ integers $a_i$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ of all test cases doesn't exceed $2\cdot 10^5$.
### Output
For each test case, print "YES" without quotation marks if it is possible to make all the elements equal after any number of operations; otherwise, print "NO" without quotation marks.
You can print answers in any register: "yes", "YeS", "nO" — will also be considered correct.
### Example
#### Input #1
```
8
3
3 2 1
3
1 1 3
4
1 2 5 4
4
1 6 6 1
5
6 2 1 4 2
4
1 4 2 1
5
3 1 2 1 3
3
2 4 2
```
#### Output #1
```
YES
NO
YES
NO
YES
NO
NO
NO
``` | codeforces | https://codeforces.com/problemset/problem/2050/B |
2050D | D. Digital string maximization | easy | You are given a string $s$, consisting of digits from $0$ to $9$. In one operation, you can pick any digit in this string, except for $0$ or the leftmost digit, decrease it by $1$, and then swap it with the digit left to the picked.
For example, in one operation from the string $1023$, you can get $1103$ or $1022$.
Find the lexicographically maximum string you can obtain after any number of operations.
### Input
The first line of the input consists of an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of a single line consisting of a digital string $s$ ($1 \le |s| \le 2\cdot 10^5$), where $|s|$ denotes the length of $s$. The string does not contain leading zeroes.
It is guaranteed that the sum of $|s|$ of all test cases doesn't exceed $2\cdot 10^5$.
### Output
For each test case, print the answer on a separate line.
### Example
#### Input #1
```
6
19
1709
11555
51476
9876543210
5891917899
```
#### Output #1
```
81
6710
33311
55431
9876543210
7875567711
```
### Note
In the first example, the following sequence of operations is suitable: $19 \rightarrow 81$.
In the second example, the following sequence of operations is suitable: $1709 \rightarrow 1780 \rightarrow 6180 \rightarrow 6710$.
In the fourth example, the following sequence of operations is suitable: $51476 \rightarrow 53176 \rightarrow 53616 \rightarrow 53651 \rightarrow 55351 \rightarrow 55431$. | codeforces | https://codeforces.com/problemset/problem/2050/D |
2050A | A. Line Breaks | easy | Kostya has a text $s$ consisting of $n$ words made up of Latin alphabet letters. He also has two strips on which he must write the text. The first strip can hold $m$ characters, while the second can hold as many as needed.
Kostya must choose a number $x$ and write the first $x$ words from $s$ on the first strip, while all the remaining words are written on the second strip. To save space, the words are written without gaps, but each word must be entirely on one strip.
Since space on the second strip is very valuable, Kostya asks you to choose the maximum possible number $x$ such that all words $s_1, s_2, \dots, s_x$ fit on the first strip of length $m$.
### Input
The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$; $1 \le m \le 500$) — the number of words in the list and the maximum number of characters that can be on the first strip.
The next $n$ lines contain one word $s_i$ of lowercase Latin letters, where the length of $s_i$ does not exceed $10$.
### Output
For each test case, output the maximum number of words $x$ such that the first $x$ words have a total length of no more than $m$.
### Example
#### Input #1
```
5
3 1
a
b
c
2 9
alpha
beta
4 12
hello
world
and
codeforces
3 2
ab
c
d
3 2
abc
ab
a
```
#### Output #1
```
1
2
2
1
0
``` | codeforces | https://codeforces.com/problemset/problem/2050/A |
2050C | C. Uninteresting Number | easy | You are given a number $n$ with a length of no more than $10^5$.
You can perform the following operation any number of times: choose one of its digits, square it, and replace the original digit with the result. The result must be a digit (that is, if you choose the digit $x$, then the value of $x^2$ must be less than $10$).
Is it possible to obtain a number that is divisible by $9$ through these operations?
### Input
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The only line of each test case contains the number $n$, without leading zeros. The length of the number does not exceed $10^5$.
It is guaranteed that the sum of the lengths of the numbers across all test cases does not exceed $10^5$.
### Output
For each test case, output "YES" if it is possible to obtain a number divisible by $9$ using the described operations, and "NO" otherwise.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
### Example
#### Input #1
```
9
123
322
333333333333
9997
5472778912773
1234567890
23
33
52254522632
```
#### Output #1
```
NO
YES
YES
NO
NO
YES
NO
YES
YES
```
### Note
In the first example, from the integer $123$, it is possible to obtain only $123$, $143$, $129$, and $149$, none of which are divisible by $9$.
In the second example, you need to replace the second digit with its square; then $n$ will equal $342 = 38 \cdot 9$.
In the third example, the integer is already divisible by $9$. | codeforces | https://codeforces.com/problemset/problem/2050/C |
2050F | F. Maximum modulo equality | easy | You are given an array $a$ of length $n$ and $q$ queries $l$, $r$.
For each query, find the maximum possible $m$, such that all elements $a_l$, $a_{l+1}$, ..., $a_r$ are equal modulo $m$. In other words, $a_l \bmod m = a_{l+1} \bmod m = \dots = a_r \bmod m$, where $a \bmod b$ — is the remainder of division $a$ by $b$. In particular, when $m$ can be infinite, print $0$.
### Input
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$, $q$ ($1 \le n, q \le 2\cdot 10^5$) — the length of the array and the number of queries.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^9$) — the elements of the array.
In the following $q$ lines of each test case, two integers $l$, $r$ are provided ($1 \le l \le r \le n$) — the range of the query.
It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$, and the sum of $q$ does not exceed $2\cdot 10^5$.
### Output
For each query, output the maximum value $m$ described in the statement.
### Example
#### Input #1
```
3
5 5
5 14 2 6 3
4 5
1 4
2 4
3 5
1 1
1 1
7
1 1
3 2
1 7 8
2 3
1 2
```
#### Output #1
```
3 1 4 1 0
0
1 6
```
### Note
In the first query of the first sample, $6 \bmod 3 = 3 \bmod 3 = 0$. It can be shown that for greater $m$, the required condition will not be fulfilled.
In the third query of the first sample, $14 \bmod 4 = 2 \bmod 4 = 6 \bmod 4 = 2$. It can be shown that for greater $m$, the required condition will not be fulfilled. | codeforces | https://codeforces.com/problemset/problem/2050/F |
2046A | A. Swap Columns and Find a Path | easy | There is a matrix consisting of $2$ rows and $n$ columns. The rows are numbered from $1$ to $2$ from top to bottom; the columns are numbered from $1$ to $n$ from left to right. Let's denote the cell on the intersection of the $i$-th row and the $j$-th column as $(i,j)$. Each cell contains an integer; initially, the integer in the cell $(i,j)$ is $a_{i,j}$.
You can perform the following operation any number of times (possibly zero):
- choose two columns and swap them (i. e. choose two integers $x$ and $y$ such that $1 \le x < y \le n$, then swap $a_{1,x}$ with $a_{1,y}$, and then swap $a_{2,x}$ with $a_{2,y}$).
After performing the operations, you have to choose a path from the cell $(1,1)$ to the cell $(2,n)$. For every cell $(i,j)$ in the path except for the last, the next cell should be either $(i+1,j)$ or $(i,j+1)$. Obviously, the path cannot go outside the matrix.
The cost of the path is the sum of all integers in all $(n+1)$ cells belonging to the path. You have to perform the operations and choose a path so that its cost is maximum possible.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
Each test case consists of three lines:
- the first line contains one integer $n$ ($1 \le n \le 5000$) — the number of columns in the matrix;
- the second line contains $n$ integers $a_{1,1}, a_{1,2}, \ldots, a_{1,n}$ ($-10^5 \le a_{i,j} \le 10^5$) — the first row of the matrix;
- the third line contains $n$ integers $a_{2,1}, a_{2,2}, \ldots, a_{2,n}$ ($-10^5 \le a_{i,j} \le 10^5$) — the second row of the matrix.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.
### Output
For each test case, print one integer — the maximum cost of a path you can obtain.
### Example
#### Input #1
```
3
1
-10
5
3
1 2 3
10 -5 -3
4
2 8 5 3
1 10 3 4
```
#### Output #1
```
-5
16
29
```
### Note
Here are the explanations of the first three test cases of the example. The left matrix is the matrix given in the input, the right one is the state of the matrix after several column swaps (possibly zero). The optimal path is highlighted in green.
 | codeforces | https://codeforces.com/problemset/problem/2046/A |
2047A | A. Alyona and a Square Jigsaw Puzzle | easy | Alyona assembles an unusual square Jigsaw Puzzle. She does so in $n$ days in the following manner:
- On the first day, she starts by placing the central piece in the center of the table.
- On each day after the first one, she places a certain number of pieces around the central piece in clockwise order, always finishing each square layer completely before starting a new one.
For example, she places the first $14$ pieces in the following order:
The colors denote the layers. The third layer is still unfinished.
Alyona is happy if at the end of the day the assembled part of the puzzle does not have any started but unfinished layers. Given the number of pieces she assembles on each day, find the number of days Alyona is happy on.
### Input
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 contains a single integer $n$ ($1 \le n \le 100$), the number of days.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 100$, $a_1 = 1$), where $a_i$ is the number of pieces Alyona assembles on the $i$-th day.
It is guaranteed in each test case that at the end of the $n$ days, there are no unfinished layers.
### Output
For each test case, print a single integer: the number of days when Alyona is happy.
### Example
#### Input #1
```
5
1
1
2
1 8
5
1 3 2 1 2
7
1 2 1 10 2 7 2
14
1 10 10 100 1 1 10 1 10 2 10 2 10 1
```
#### Output #1
```
1
2
2
2
3
```
### Note
In the first test case, in the only day Alyona finishes the only layer.
In the second test case, on the first day, Alyona finishes the first layer, and on the second day, she finishes the second layer.
In the third test case, she finishes the second layer in a few days.
In the fourth test case, she finishes the second layer and immediately starts the next one on the same day, therefore, she is not happy on that day. She is only happy on the first and last days.
In the fifth test case, Alyona is happy on the first, fourth, and last days. | codeforces | https://codeforces.com/problemset/problem/2047/A |
2047B | B. Replace Character | easy | You're given a string $s$ of length $n$, consisting of only lowercase English letters.
You must do the following operation exactly once:
- Choose any two indices $i$ and $j$ ($1 \le i, j \le n$). You can choose $i = j$.
- Set $s_i := s_j$.
You need to minimize the number of distinct permutations$^\dagger$ of $s$. Output any string with the smallest number of distinct permutations after performing exactly one operation.
$^\dagger$ A permutation of the string is an arrangement of its characters into any order. For example, "bac" is a permutation of "abc" but "bcc" is not.
### Input
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 $n$ ($1 \le n \le 10$) — the length of string $s$.
The second line of each test case contains $s$ of length $n$. The string contains only lowercase English letters.
### Output
For each test case, output the required $s$ after applying exactly one operation. If there are multiple solutions, print any of them.
### Example
#### Input #1
```
6
3
abc
4
xyyx
8
alphabet
1
k
10
aabbccddee
6
ttbddq
```
#### Output #1
```
cbc
yyyx
alphaaet
k
eabbccddee
tttddq
```
### Note
In the first test case, we can obtain the following strings in one operation: "abc", "bbc", "cbc", "aac", "acc", "aba", and "abb".
The string "abc" has $6$ distinct permutations: "abc", "acb", "bac", "bca", "cab", and "cba".
The string "cbc" has $3$ distinct permutations: "bcc", "cbc", and "ccb", which is the lowest of all the obtainable strings. In fact, all obtainable strings except "abc" have $3$ permutations, so any of them would be accepted. | codeforces | https://codeforces.com/problemset/problem/2047/B |
2046B | B. Move Back at a Cost | easy | You are given an array of integers $a$ of length $n$. You can perform the following operation zero or more times:
- In one operation choose an index $i$ ($1 \le i \le n$), assign $a_i := a_i + 1$, and then move $a_i$ to the back of the array (to the rightmost position). For example, if $a = [3, 5, 1, 9]$, and you choose $i = 2$, the array becomes $[3, 1, 9, 6]$.
Find the lexicographically smallest$^{\text{∗}}$ array you can get by performing these operations.
$^{\text{∗}}$An array $c$ is lexicographically smaller than an array $d$ if and only if one of the following holds:
- $c$ is a prefix of $d$, but $c \ne d$; or
- in the first position where $c$ and $d$ differ, the array $c$ has a smaller element than the corresponding element in $d$.
### Input
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 contains a single integer $n$ ($1 \le n \le 10^5$), the length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, print the lexicographically smallest array you can get.
### Example
#### Input #1
```
3
3
2 1 3
5
1 2 2 1 4
6
1 2 3 6 5 4
```
#### Output #1
```
1 3 3
1 1 3 3 5
1 2 3 4 6 7
``` | codeforces | https://codeforces.com/problemset/problem/2046/B |
2042C | C. Competitive Fishing | easy | Alice and Bob participate in a fishing contest! In total, they caught $n$ fishes, numbered from $1$ to $n$ (the bigger the fish, the greater its index). Some of these fishes were caught by Alice, others — by Bob.
Their performance will be evaluated as follows. First, an integer $m$ will be chosen, and all fish will be split into $m$ non-empty groups. The first group should contain several (at least one) smallest fishes, the second group — several (at least one) next smallest fishes, and so on. Each fish should belong to exactly one group, and each group should be a contiguous subsegment of fishes. Note that the groups are numbered in exactly that order; for example, the fishes from the second group cannot be smaller than the fishes from the first group, since the first group contains the smallest fishes.
Then, each fish will be assigned a value according to its group index: each fish in the first group gets value equal to $0$, each fish in the second group gets value equal to $1$, and so on. So, each fish in the $i$-th group gets value equal to $(i-1)$.
The score of each contestant is simply the total value of all fishes that contestant caught.
You want Bob's score to exceed Alice's score by at least $k$ points. What is the minimum number of groups ($m$) you have to split the fishes into? If it is impossible, you should report that.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$).
The second line contains a string, consisting of exactly $n$ characters. The $i$-th character is either 0 (denoting that the $i$-th fish was caught by Alice) or 1 (denoting that the $i$-th fish was caught by Bob).
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, print a single integer — the minimum number of groups you have to split the fishes into; or -1 if it's impossible.
### Example
#### Input #1
```
7
4 1
1001
4 1
1010
4 1
0110
4 2
0110
6 3
001110
10 20
1111111111
5 11
11111
```
#### Output #1
```
2
-1
2
-1
3
4
-1
```
### Note
In the first test case of the example, you can split the fishes into groups as follows: the first three fishes form the $1$-st group, the last fish forms the $2$-nd group. Then, Bob's score will be $1$, and Alice's score will be $0$.
In the third test case of the example, you can split the fishes into groups as follows: the first fish forms the $1$-st group, the last three fishes form the $2$-nd group. Then, Bob's score will be $2$, and Alice's score will be $1$. | codeforces | https://codeforces.com/problemset/problem/2042/C |
2042B | B. Game with Colored Marbles | easy | Alice and Bob play a game. There are $n$ marbles, the $i$-th of them has color $c_i$. The players take turns; Alice goes first, then Bob, then Alice again, then Bob again, and so on.
During their turn, a player must take one of the remaining marbles and remove it from the game. If there are no marbles left (all $n$ marbles have been taken), the game ends.
Alice's score at the end of the game is calculated as follows:
- she receives $1$ point for every color $x$ such that she has taken at least one marble of that color;
- additionally, she receives $1$ point for every color $x$ such that she has taken all marbles of that color (of course, only colors present in the game are considered).
For example, suppose there are $5$ marbles, their colors are $[1, 3, 1, 3, 4]$, and the game goes as follows: Alice takes the $1$-st marble, then Bob takes the $3$-rd marble, then Alice takes the $5$-th marble, then Bob takes the $2$-nd marble, and finally, Alice takes the $4$-th marble. Then, Alice receives $4$ points: $3$ points for having at least one marble for colors $1$, $3$ and $4$, and $1$ point for having all marbles of color $4$. Note that this strategy is not necessarily optimal for both players.
Alice wants to maximize her score at the end of the game. Bob wants to minimize it. Both players play optimally (i. e. Alice chooses a strategy which allows her to get as many points as possible, and Bob chooses a strategy which minimizes the amount of points Alice can get).
Calculate Alice's score at the end of the game.
### Input
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines:
- the first line contains one integer $n$ ($1 \le n \le 1000$) — the number of marbles;
- the second line contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le n$) — the colors of the marbles.
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $1000$.
### Output
For each test case, print one integer — Alice's score at the end of the game, assuming that both players play optimally.
### Example
#### Input #1
```
3
5
1 3 1 3 4
3
1 2 3
4
4 4 4 4
```
#### Output #1
```
4
4
1
```
### Note
In the second test case of the example, the colors of all marbles are distinct, so, no matter how the players act, Alice receives $4$ points for having all marbles of two colors, and no marbles of the third color.
In the third test case of the example, the colors of all marbles are the same, so, no matter how the players act, Alice receives $1$ point for having at least one (but not all) marble of color $4$. | codeforces | https://codeforces.com/problemset/problem/2042/B |
2042A | A. Greedy Monocarp | easy | There are $n$ chests; the $i$-th chest initially contains $a_i$ coins. For each chest, you can choose any non-negative ($0$ or greater) number of coins to add to that chest, with one constraint: the total number of coins in all chests must become at least $k$.
After you've finished adding coins to the chests, greedy Monocarp comes, who wants the coins. He will take the chests one by one, and since he is greedy, he will always choose the chest with the maximum number of coins. Monocarp will stop as soon as the total number of coins in chests he takes is at least $k$.
You want Monocarp to take as few coins as possible, so you have to add coins to the chests in such a way that, when Monocarp stops taking chests, he will have exactly $k$ coins. Calculate the minimum number of coins you have to add.
### Input
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines:
- the first line contains two integers $n$ and $k$ ($1 \le n \le 50$; $1 \le k \le 10^7$);
- the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$).
### Output
For each test case, print one integer — the minimum number of coins you have to add so that, when Monocarp stops taking the chests, he has exactly $k$ coins. It can be shown that under the constraints of the problem, it is always possible.
### Example
#### Input #1
```
4
5 4
4 1 2 3 2
5 10
4 1 2 3 2
2 10
1 1
3 8
3 3 3
```
#### Output #1
```
0
1
8
2
```
### Note
In the first test case of the example, you don't have to add any coins. When Monocarp arrives, he will take the chest with $4$ coins, so he will have exactly $4$ coins.
In the second test case of the example, you can add $1$ coin to the $4$-th chest, so, when Monocarp arrives, he will take a chest with $4$ coins, then another chest with $4$ coins, and a chest with $2$ coins.
In the third test case of the example, you can add $3$ coins to the $1$-st chest and $5$ coins to the $2$-nd chest.
In the fourth test case of the example, you can add $1$ coin to the $1$-st chest and $1$ coin to the $3$-rd chest. | codeforces | https://codeforces.com/problemset/problem/2042/A |
2034C | C. Trapped in the Witch's Labyrinth | easy | In the [fourth labor of Rostam](https://www.gathertales.com/story/the-tale-of-the-haft-khan-seven-labors-of-rostam/sid-604), the legendary hero from the [Shahnameh](https://en.wikipedia.org/wiki/Shahnameh), an old witch has created a magical maze to trap him. The maze is a rectangular grid consisting of $n$ rows and $m$ columns. Each cell in the maze points in a specific direction: up, down, left, or right. The witch has enchanted Rostam so that whenever he is in a cell, he will move to the next cell in the direction indicated by that cell.

If Rostam eventually exits the maze, he will be freed from the witch's enchantment and will defeat her. However, if he remains trapped within the maze forever, he will never escape.
The witch has not yet determined the directions for all the cells. She wants to assign directions to the unspecified cells in such a way that the number of starting cells from which Rostam will be trapped forever is maximized. Your task is to find the maximum number of starting cells which make Rostam trapped.
### Input
The first line of the input contains an integer $t$ ($1 \leq t \leq 10^4$), the number of test cases.
For each test case:
- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 1000$), representing the number of rows and columns in the maze.
- Each of the next $n$ lines contains a string of $m$ characters representing the directions in the maze. Each character is one of the following:
- U (up)
- D (down)
- L (left)
- R (right)
- ? (unspecified direction)
It's guaranteed that the sum of $n \cdot m$ over all test cases is at most $10^6$.
### Output
For each test case, print a single integer, the maximum number of starting cells from which Rostam will be trapped forever after assigning directions to the unspecified cells optimally.
### Example
#### Input #1
```
3
3 3
UUU
L?R
DDD
2 3
???
???
3 3
?U?
R?L
RDL
```
#### Output #1
```
0
6
5
```
### Note
In the first test case, all of the cells will be good no matter what you do.
In the second test case, if you assign the ?s like the picture below, all of the cells will be bad:

In the third test case, if you assign the ?s like the picture below, you will have $5$ bad cells (red-shaded cells):
 | codeforces | https://codeforces.com/problemset/problem/2034/C |
2034B | B. Rakhsh's Revival | easy | [Rostam](https://en.wikipedia.org/wiki/Rostam)'s loyal horse, [Rakhsh](https://en.wikipedia.org/wiki/Rakhsh), has seen better days. Once powerful and fast, Rakhsh has grown weaker over time, struggling to even move. Rostam worries that if too many parts of Rakhsh's body lose strength at once, Rakhsh might stop entirely. To keep his companion going, Rostam decides to strengthen Rakhsh, bit by bit, so no part of his body is too frail for too long.

Imagine Rakhsh's body as a line of spots represented by a binary string $s$ of length $n$, where each $0$ means a weak spot and each $1$ means a strong one. Rostam's goal is to make sure that no interval of $m$ consecutive spots is entirely weak (all $0$s).
Luckily, Rostam has a special ability called Timar, inherited from his mother [Rudabeh](https://en.wikipedia.org/wiki/Rudaba) at birth. With Timar, he can select any segment of length $k$ and instantly strengthen all of it (changing every character in that segment to $1$). The challenge is to figure out the minimum number of times Rostam needs to use Timar to keep Rakhsh moving, ensuring there are no consecutive entirely weak spots of length $m$.
### Input
The first line contains an integer $t$ ($1 \le t \le 10^4$), the number of test cases.
The first line of each test case contains three numbers $n$, $m$, $k$ ($1 \le m, k \le n \le 2 \cdot 10^5$). The second line of each test case contains a binary string $s$ of $n$ characters $s_1s_2 \ldots s_n$. ($s_i \in \{$0,1$\}$ for $1 \le i \le n$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output the minimum number of times Rostam needs to use Timar to keep Rakhsh moving, ensuring there are no consecutive entirely weak spots of length $m$.
### Example
#### Input #1
```
3
5 1 1
10101
5 2 1
10101
6 3 2
000000
```
#### Output #1
```
2
0
1
```
### Note
In the first test case, we should apply an operation on each 0.
In the second test case, $s$ is already ok.
In the third test case, we can perform an operation on interval $[3,4]$ to get 001100. | codeforces | https://codeforces.com/problemset/problem/2034/B |
2034D | D. Darius' Wisdom | easy | [Darius the Great](https://en.wikipedia.org/wiki/Darius_the_Great) is constructing $n$ stone columns, each consisting of a base and between $0$, $1$, or $2$ inscription pieces stacked on top.
In each move, Darius can choose two columns $u$ and $v$ such that the difference in the number of inscriptions between these columns is exactly $1$, and transfer one inscription from the column with more inscriptions to the other one. It is guaranteed that at least one column contains exactly $1$ inscription.

Since beauty is the main pillar of historical buildings, Darius wants the columns to have ascending heights. To avoid excessive workers' efforts, he asks you to plan a sequence of at most $n$ moves to arrange the columns in non-decreasing order based on the number of inscriptions. Minimizing the number of moves is not required.
### Input
The first line contains an integer $t$ — the number of test cases. ($1 \leq t \leq 3000$)
The first line of each test case contains an integer $n$ — the number of stone columns. ($1 \leq n \leq 2 \cdot 10^5$)
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$, where $a_i \in \{0,1,2\}$ represents the initial number of inscriptions in the $i$-th column. It is guaranteed that at least one column has exactly $1$ inscription.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output an integer $k$ — the number of moves used to sort the columns. ($0 \leq k \leq n$)
Then, output $k$ lines, each containing two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$), representing the indices of the columns involved in the $i$-th move. During each move, it must hold that $|a_{u_i} - a_{v_i}| = 1$, and one inscription is transferred from the column with more inscriptions to the other.
It can be proven that a valid solution always exists under the given constraints.
### Example
#### Input #1
```
3
4
0 2 0 1
3
1 2 0
6
0 1 1 2 2 2
```
#### Output #1
```
2
2 4
2 3
2
3 1
2 3
0
```
### Note
Columns state in the first test case:
- Initial: $0, 2, 0, 1$
- After the first move: $0, 1, 0, 2$
- After the second move: $0, 0, 1, 2$
Columns state in the second test case:
- Initial: $1, 2, 0$
- After the first move: $0, 2, 1$
- After the second move: $0, 1, 2$
In the third test case, the column heights are already sorted in ascending order. | codeforces | https://codeforces.com/problemset/problem/2034/D |
2034A | A. King Keykhosrow's Mystery | easy | There is a tale about the wise [King Keykhosrow](https://en.wikipedia.org/wiki/Kay_Khosrow) who owned a grand treasury filled with treasures from across the Persian Empire. However, to prevent theft and ensure the safety of his wealth, King Keykhosrow's vault was sealed with a magical lock that could only be opened by solving a riddle.

The riddle involves two sacred numbers $a$ and $b$. To unlock the vault, the challenger must determine the smallest key number $m$ that satisfies two conditions:
- $m$ must be greater than or equal to at least one of $a$ and $b$.
- The remainder when $m$ is divided by $a$ must be equal to the remainder when $m$ is divided by $b$.
Only by finding the smallest correct value of $m$ can one unlock the vault and access the legendary treasures!
### Input
The first line of the input contains an integer $t$ ($1 \leq t \leq 100$), the number of test cases.
Each test case consists of a single line containing two integers $a$ and $b$ ($1 \leq a, b \leq 1000$).
### Output
For each test case, print the smallest integer $m$ that satisfies the conditions above.
### Example
#### Input #1
```
2
4 6
472 896
```
#### Output #1
```
12
52864
```
### Note
In the first test case, you can see that:
- $4 \bmod 4 = 0$ but $4 \bmod 6 = 4$
- $5 \bmod 4 = 1$ but $5 \bmod 6 = 5$
- $6 \bmod 4 = 2$ but $6 \bmod 6 = 0$
- $7 \bmod 4 = 3$ but $7 \bmod 6 = 1$
- $8 \bmod 4 = 0$ but $8 \bmod 6 = 2$
- $9 \bmod 4 = 1$ but $9 \bmod 6 = 3$
- $10 \bmod 4 = 2$ but $10 \bmod 6 = 4$
- $11 \bmod 4 = 3$ but $11 \bmod 6 = 5$
so no integer less than $12$ satisfies the desired properties. | codeforces | https://codeforces.com/problemset/problem/2034/A |
2039C2 | C2. Shohag Loves XOR (Hard Version) | easy | This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers $x$ and $m$. Help him count the number of integers $1 \le y \le m$ such that $x \oplus y$ is divisible$^{\text{∗}}$ by either $x$, $y$, or both. Here $\oplus$ is the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operator.
$^{\text{∗}}$The number $a$ is divisible by the number $b$ if there exists an integer $c$ such that $a = b \cdot c$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first and only line of each test case contains two space-separated integers $x$ and $m$ ($1 \le x \le 10^6$, $1 \le m \le 10^{18}$).
It is guaranteed that the sum of $x$ over all test cases does not exceed $10^7$.
### Output
For each test case, print a single integer — the number of suitable $y$.
### Example
#### Input #1
```
5
7 10
2 3
6 4
1 6
4 1
```
#### Output #1
```
3
2
2
6
1
```
### Note
In the first test case, for $x = 7$, there are $3$ valid values for $y$ among the integers from $1$ to $m = 10$, and they are $1$, $7$, and $9$.
- $y = 1$ is valid because $x \oplus y = 7 \oplus 1 = 6$ and $6$ is divisible by $y = 1$.
- $y = 7$ is valid because $x \oplus y = 7 \oplus 7 = 0$ and $0$ is divisible by both $x = 7$ and $y = 7$.
- $y = 9$ is valid because $x \oplus y = 7 \oplus 9 = 14$ and $14$ is divisible by $x = 7$. | codeforces | https://codeforces.com/problemset/problem/2039/C2 |
2039B | B. Shohag Loves Strings | easy | For a string $p$, let $f(p)$ be the number of distinct non-empty substrings$^{\text{∗}}$ of $p$.
Shohag has a string $s$. Help him find a non-empty string $p$ such that $p$ is a substring of $s$ and $f(p)$ is even or state that no such string exists.
$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first and only line of each test case contains a string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase English letters.
It is guaranteed that the sum of the length of $s$ over all test cases doesn't exceed $3 \cdot 10^5$.
### Output
For each test case, print a non-empty string that satisfies the conditions mentioned in the statement, or $-1$ if no such string exists. If there are multiple solutions, output any.
### Example
#### Input #1
```
5
dcabaac
a
youknowwho
codeforces
bangladesh
```
#### Output #1
```
abaa
-1
youknowwho
eforce
bang
```
### Note
In the first test case, we can set $p = $ abaa because it is a substring of $s$ and the distinct non-empty substrings of $p$ are a, b, aa, ab, ba, aba, baa and abaa, so it has a total of $8$ distinct substrings which is even.
In the second test case, we can only set $p = $ a but it has one distinct non-empty substring but this number is odd, so not valid.
In the third test case, the whole string contains $52$ distinct non-empty substrings, so the string itself is a valid solution. | codeforces | https://codeforces.com/problemset/problem/2039/B |
2039C1 | C1. Shohag Loves XOR (Easy Version) | easy | This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers $x$ and $m$. Help him count the number of integers $1 \le y \le m$ such that $\mathbf{x \neq y}$ and $x \oplus y$ is a divisor$^{\text{∗}}$ of either $x$, $y$, or both. Here $\oplus$ is the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operator.
$^{\text{∗}}$The number $b$ is a divisor of the number $a$ if there exists an integer $c$ such that $a = b \cdot c$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first and only line of each test case contains two space-separated integers $x$ and $m$ ($1 \le x \le 10^6$, $1 \le m \le 10^{18}$).
It is guaranteed that the sum of $x$ over all test cases does not exceed $10^7$.
### Output
For each test case, print a single integer — the number of suitable $y$.
### Example
#### Input #1
```
5
6 9
5 7
2 3
6 4
4 1
```
#### Output #1
```
3
2
1
1
0
```
### Note
In the first test case, for $x = 6$, there are $3$ valid values for $y$ among the integers from $1$ to $m = 9$, and they are $4$, $5$, and $7$.
- $y = 4$ is valid because $x \oplus y = 6 \oplus 4 = 2$ and $2$ is a divisor of both $x = 6$ and $y = 4$.
- $y = 5$ is valid because $x \oplus y = 6 \oplus 5 = 3$ and $3$ is a divisor of $x = 6$.
- $y = 7$ is valid because $x \oplus y = 6 \oplus 7 = 1$ and $1$ is a divisor of both $x = 6$ and $y = 7$.
In the second test case, for $x = 5$, there are $2$ valid values for $y$ among the integers from $1$ to $m = 7$, and they are $4$ and $6$.
- $y = 4$ is valid because $x \oplus y = 5 \oplus 4 = 1$ and $1$ is a divisor of both $x = 5$ and $y = 4$.
- $y = 6$ is valid because $x \oplus y = 5 \oplus 6 = 3$ and $3$ is a divisor of $y = 6$. | codeforces | https://codeforces.com/problemset/problem/2039/C1 |
2039D | D. Shohag Loves GCD | easy | Shohag has an integer $n$ and a set $S$ of $m$ unique integers. Help him find the lexicographically largest$^{\\text{∗}}$ integer array $a\_1, a\_2, \\ldots, a\_n$ such that $a\_i \\in S$ for each $1 \\le i \\le n$ and $a\_{\\operatorname{gcd}(i, j)} \\neq \\operatorname{gcd}(a\_i, a\_j)$$^{\\text{†}}$ is satisfied over all pairs $1 \\le i \\lt j \\le n$, or state that no such array exists.
$^{\text{∗}}$An array $a$ is lexicographically larger than an array $b$ of the same length if $a \ne b$, and in the first position where $a$ and $b$ differ, the array $a$ has a larger element than the corresponding element in $b$.
$^{\\text{†}}$$\\gcd(x, y)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$.
### Input
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 \le m \le n \le 10^5$).
The second line contains $m$ unique integers in increasing order, representing the elements of the set $S$ ($1 \le x \le n$ for each $x \in S$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
### Output
For each test case, if there is no solution print $-1$, otherwise print $n$ integers — the lexicographically largest integer array that satisfies the conditions.
### Example
#### Input #1
```
3
6 3
3 4 6
1 1
1
2 1
2
```
#### Output #1
```
6 4 4 3 4 3
1
-1
```
### Note
In the first test case, every element in the array belongs to the given set $S = \{3, 4, 6\}$, and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair $(2, 3)$, $a_{\operatorname{gcd}(2, 3)} = a_1 = 6$ and $\operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4$, so they are not equal. There are other arrays that satisfy the conditions as well but this one is the lexicographically largest among them.
In the third test case, there is no solution possible because we are only allowed to use $a = [2, 2]$ but for this array, for pair $(1, 2)$, $a_{\operatorname{gcd}(1, 2)} = a_1 = 2$ and $\operatorname{gcd}(a_1, a_2) = \operatorname{gcd}(2, 2) = 2$, so they are equal which is not allowed! | codeforces | https://codeforces.com/problemset/problem/2039/D |
2039A | A. Shohag Loves Mod | easy | Shohag has an integer $n$. Please help him find an increasing integer sequence $1 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 100$ such that $a_i \bmod i \neq a_j \bmod j$ $^{\text{∗}}$ is satisfied over all pairs $1 \le i \lt j \le n$.
It can be shown that such a sequence always exists under the given constraints.
$^{\\text{∗}}$$a \\bmod b$ denotes the remainder of $a$ after division by $b$. For example, $7 \\bmod 3 = 1, 8 \\bmod 4 = 0$ and $69 \\bmod 10 = 9$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 50$) — the number of test cases.
The first and only line of each test case contains an integer $n$ ($2 \le n \le 50$).
### Output
For each test case, print $n$ integers — the integer sequence that satisfies the conditions mentioned in the statement. If there are multiple such sequences, output any.
### Example
#### Input #1
```
2
3
6
```
#### Output #1
```
2 7 8
2 3 32 35 69 95
```
### Note
In the first test case, the sequence is increasing, values are from $1$ to $100$ and each pair of indices satisfies the condition mentioned in the statement:
- For pair $(1, 2)$, $a_1 \bmod 1 = 2 \bmod 1 = 0$, and $a_2 \bmod 2 = 7 \bmod 2 = 1$. So they are different.
- For pair $(1, 3)$, $a_1 \bmod 1 = 2 \bmod 1 = 0$, and $a_3 \bmod 3 = 8 \bmod 3 = 2$. So they are different.
- For pair $(2, 3)$, $a_2 \bmod 2 = 7 \bmod 2 = 1$, and $a_3 \bmod 3 = 8 \bmod 3 = 2$. So they are different.
Note that you do not necessarily have to print the exact same sequence, you can print any other sequence as long as it satisfies the necessary conditions. | codeforces | https://codeforces.com/problemset/problem/2039/A |
2037C | C. Superultra's Favorite Permutation | easy | Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra!
Construct a permutation$^{\text{∗}}$ $p$ of length $n$ such that $p_i + p_{i+1}$ is composite$^{\text{†}}$ over all $1 \leq i \leq n - 1$. If it's not possible, output $-1$.
$^{\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).
$^{\text{†}}$An integer $x$ is composite if it has at least one other divisor besides $1$ and $x$. For example, $4$ is composite because $2$ is a divisor.
### Input
The first line contains $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
Each test case contains an integer $n$ ($2 \leq n \leq 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
For each test case, if it's not possible to construct $p$, output $-1$ on a new line. Otherwise, output $n$ integers $p_1, p_2, \ldots, p_n$ on a new line.
### Example
#### Input #1
```
2
3
8
```
#### Output #1
```
-1
1 8 7 3 6 2 4 5```
### Note
In the first example, it can be shown that all permutation of size $3$ contain two adjacent elements whose sum is prime. For example, in the permutation $[2,3,1]$ the sum $2+3=5$ is prime.
In the second example, we can verify that the sample output is correct because $1+8$, $8+7$, $7+3$, $3+6$, $6+2$, $2+4$, and $4+5$ are all composite. There may be other constructions that are correct. | codeforces | https://codeforces.com/problemset/problem/2037/C |
2037B | B. Intercepted Inputs | easy | To help you prepare for your upcoming Codeforces contest, Citlali set a grid problem and is trying to give you a $n$ by $m$ grid through your input stream. Specifically, your input stream should contain the following:
- The first line contains two integers $n$ and $m$ — the dimensions of the grid.
- The following $n$ lines contain $m$ integers each — the values of the grid.
However, someone has intercepted your input stream, shuffled all given integers, and put them all on one line! Now, there are $k$ integers all on one line, and you don't know where each integer originally belongs. Instead of asking Citlali to resend the input, you decide to determine the values of $n$ and $m$ yourself.
Output any possible value of $n$ and $m$ that Citlali could have provided.
### Input
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 $k$ ($3 \leq k \leq 2 \cdot 10^5$) — the total number of inputs in your input stream.
The following line of each test case contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \leq a_i \leq k$) — the shuffled inputs of your input stream. It is guaranteed that $n$ and $m$ are contained within the $k$ integers.
It is guaranteed that the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output two integers, one possible value of $n$ and $m$. If multiple possible answers exist, output any.
### Example
#### Input #1
```
5
3
1 1 2
11
3 3 4 5 6 7 8 9 9 10 11
8
8 4 8 3 8 2 8 1
6
2 1 4 5 3 3
8
1 2 6 3 8 5 5 3
```
#### Output #1
```
1 1
3 3
2 3
4 1
1 6```
### Note
In the first test case, the initial input could have been the following:
1 1
2
In the second test case, the initial input could have been the following:
3 3
4 5 6
7 8 9
9 10 11 | codeforces | https://codeforces.com/problemset/problem/2037/B |
2037A | A. Twice | easy | Kinich wakes up to the start of a new day. He turns on his phone, checks his mailbox, and finds a mysterious present. He decides to unbox the present.
Kinich unboxes an array $a$ with $n$ integers. Initially, Kinich's score is $0$. He will perform the following operation any number of times:
- Select two indices $i$ and $j$ $(1 \leq i < j \leq n)$ such that neither $i$ nor $j$ has been chosen in any previous operation and $a_i = a_j$. Then, add $1$ to his score.
Output the maximum score Kinich can achieve after performing the aforementioned operation any number of times.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 500$) — the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 20$) — the length of $a$.
The following line of each test case contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$).
### Output
For each test case, output the maximum score achievable on a new line.
### Example
#### Input #1
```
5
1
1
2
2 2
2
1 2
4
1 2 3 1
6
1 2 3 1 2 3
```
#### Output #1
```
0
1
0
1
3
```
### Note
In the first and third testcases, Kinich cannot perform any operations.
In the second testcase, Kinich can perform one operation with $i=1$ and $j=2$.
In the fourth testcase, Kinich can perform one operation with $i=1$ and $j=4$. | codeforces | https://codeforces.com/problemset/problem/2037/A |
2037D | D. Sharky Surfing | easy | Mualani loves surfing on her sharky surfboard!
Mualani's surf path can be modeled by a number line. She starts at position $1$, and the path ends at position $L$. When she is at position $x$ with a jump power of $k$, she can jump to any integer position in the interval $[x, x+k]$. Initially, her jump power is $1$.
However, her surf path isn't completely smooth. There are $n$ hurdles on her path. Each hurdle is represented by an interval $[l, r]$, meaning she cannot jump to any position in the interval $[l, r]$.
There are also $m$ power-ups at certain positions on the path. Power-up $i$ is located at position $x_i$ and has a value of $v_i$. When Mualani is at position $x_i$, she has the option to collect the power-up to increase her jump power by $v_i$. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle.
What is the minimum number of power-ups she must collect to reach position $L$ to finish the path? If it is not possible to finish the surf path, output $-1$.
### Input
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 three integers $n$, $m$, and $L$ ($1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9$) — the number of hurdles, the number of power-ups, and the position of the end.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($2 \leq l_i \leq r_i \leq L-1$) — the bounds of the interval for the $i$'th hurdle. It is guaranteed that $r_i + 1 < l_{i+1}$ for all $1 \leq i < n$ (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle).
The following $m$ lines contain two integers $x_i$ and $v_i$ ($1 \leq x_i, v_i \leq L$) — the position and the value for the $i$'th power-up. There may be multiple power-ups with the same $x$. It is guaranteed that $x_i \leq x_{i+1}$ for all $1 \leq i < m$ (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle.
It is guaranteed the sum of $n$ and the sum of $m$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output the minimum number of power-ups she must collect to reach position $L$. If it is not possible, output $-1$.
### Example
#### Input #1
```
4
2 5 50
7 14
30 40
2 2
3 1
3 5
18 2
22 32
4 3 50
4 6
15 18
20 26
34 38
1 2
8 2
10 2
1 4 17
10 14
1 6
1 2
1 2
16 9
1 2 10
5 9
2 3
2 2
```
#### Output #1
```
4
-1
1
2
```
### Note
In the first test case, she can collect power-ups $1$, $2$, $3$, and $5$ to clear all hurdles.
In the second test case, she cannot jump over the first hurdle.
In the fourth test case, by collecting both power-ups, she can jump over the hurdle. | codeforces | https://codeforces.com/problemset/problem/2037/D |
2037E | E. Kachina's Favorite Binary String | easy | This is an interactive problem.
Kachina challenges you to guess her favorite binary string$^{\text{∗}}$ $s$ of length $n$. She defines $f(l, r)$ as the number of subsequences$^{\text{†}}$ of $\texttt{01}$ in $s_l s_{l+1} \ldots s_r$. Two subsequences are considered different if they are formed by deleting characters from different positions in the original string, even if the resulting subsequences consist of the same characters.
To determine $s$, you can ask her some questions. In each question, you can choose two indices $l$ and $r$ ($1 \leq l < r \leq n$) and ask her for the value of $f(l, r)$.
Determine and output $s$ after asking Kachina no more than $n$ questions. However, it may be the case that $s$ is impossible to be determined. In this case, you would need to report $\texttt{IMPOSSIBLE}$ instead.
Formally, $s$ is impossible to be determined if after asking $n$ questions, there are always multiple possible strings for $s$, regardless of what questions are asked. Note that if you report $\texttt{IMPOSSIBLE}$ when there exists a sequence of at most $n$ queries that will uniquely determine the binary string, you will get the Wrong Answer verdict.
$^{\text{∗}}$A binary string only contains 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
The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^4$) — the length of $s$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
### Interaction
To ask a question, output a line in the following format (do not include quotes)
- "$\texttt{? l r}$" ($1 \leq l < r \leq n$)
The jury will return an integer $f(l, r)$.
When you are ready to print the answer, output a single line in the following format
- If $s$ is impossible to be determined, output "$\texttt{! IMPOSSIBLE}$"
- Otherwise, output "$\texttt{! s}$"
After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query.
The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant.
If your program makes more than $n$ queries for one test case, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see the documentation for other languages.
Hacks
To make a hack, use the following format.
The first line should contain a single integer $t$ ($1 \leq t \leq 10^3$) – the number of test cases.
The first line of each test case should contain an integer $n$ ($2 \leq n \leq 10^4$) — the length of $s$.
The following line should contain $s$, a binary string of length $n$.
The sum of $n$ over all test cases should not exceed $10^4$.
### Example
#### Input #1
```
2
5
4
0
1
2
2
0```
#### Output #1
```
? 1 5
? 2 4
? 4 5
? 3 5
! 01001
? 1 2
! IMPOSSIBLE```
### Note
In the first test case:
In the first query, you ask Kachina for the value of $f(1, 5)$, and she responds with $4$ in the input stream.
In the second query, you ask Kachina for the value of $f(2, 4)$. Because there are no subsequences of $\texttt{01}$ in the string $\texttt{100}$, she responds with $0$ in the input stream.
After asking $4$ questions, you report $\texttt{01001}$ as $s$, and it is correct.
In the second test case:
In the first query, you ask Kachina for the value of $f(1, 2)$, and she responds with $0$ in the input stream. Notice that this is the only distinct question you can ask.
However, notice that the strings $00$ and $11$ both have an answer of $0$, and it is impossible to differentiate between the two. Therefore, we report IMPOSSIBLE.
Please note that this example only serves to demonstrate the interaction format. It is not guaranteed the queries provided are optimal or uniquely determine the answer. However, it can be shown there exists a sequence of at most $5$ queries that does uniquely determine sample test case $1$. | codeforces | https://codeforces.com/problemset/problem/2037/E |
2031D | D. Penchick and Desert Rabbit | easy | Dedicated to pushing himself to his limits, Penchick challenged himself to survive the midday sun in the Arabian Desert!
While trekking along a linear oasis, Penchick spots a desert rabbit preparing to jump along a line of palm trees. There are $n$ trees, each with a height denoted by $a_i$.
The rabbit can jump from the $i$-th tree to the $j$-th tree if exactly one of the following conditions is true:
- $j < i$ and $a_j > a_i$: the rabbit can jump backward to a taller tree.
- $j > i$ and $a_j < a_i$: the rabbit can jump forward to a shorter tree.
For each $i$ from $1$ to $n$, determine the maximum height among all trees that the rabbit can reach if it starts from the $i$-th tree.
### Input
The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^5$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) — the number of trees.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the height of the trees.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
### Output
For each test case, output $n$ integers. The $i$-th integer should contain the maximum height among all trees that the rabbit can reach if it starts from the $i$-th tree.
### Example
#### Input #1
```
5
4
2 3 1 4
5
5 4 3 2 1
4
2 1 1 3
4
1 1 3 1
8
2 4 1 6 3 8 5 7
```
#### Output #1
```
3 3 3 4
5 5 5 5 5
2 2 2 3
1 1 3 3
8 8 8 8 8 8 8 8
```
### Note
In the first test case, the initial heights of trees are $a = [2, 3, 1, 4]$.
- If the rabbit starts from the first tree, it can jump to the third tree as $3 > 1$ and $1 < 2$. Then, the rabbit can jump to the second tree as $2 < 3$ and $3 > 1$. It can be proved that the rabbit cannot reach the fourth tree; hence, the maximum height of the tree that the rabbit can reach is $a_2 = 3$.
- If the rabbit starts from the fourth tree, it does not need to jump anywhere as it is already at the highest tree.
In the second test case, the rabbit can jump to the first tree regardless of which tree it starts from.
In the fifth test case, if the rabbit starts from the fifth tree, it can jump to the fourth tree. Then the rabbit can jump to the seventh tree and finally reach the sixth tree. Therefore, the maximum height of the tree that the rabbit can reach is $8$. | codeforces | https://codeforces.com/problemset/problem/2031/D |
2031C | C. Penchick and BBQ Buns | easy | Penchick loves two things: square numbers and Hong Kong-style BBQ buns! For his birthday, Kohane wants to combine them with a gift: $n$ BBQ buns arranged from left to right. There are $10^6$ available fillings of BBQ buns, numbered from $1$ to $10^6$. To ensure that Penchick would love this gift, Kohane has a few goals:
- No filling is used exactly once; that is, each filling must either not appear at all or appear at least twice.
- For any two buns $i$ and $j$ that have the same filling, the distance between them, which is $|i-j|$, must be a perfect square$^{\text{∗}}$.
Help Kohane find a valid way to choose the filling of the buns, or determine if it is impossible to satisfy her goals! If there are multiple solutions, print any of them.
$^{\text{∗}}$A positive integer $x$ is a perfect square if there exists a positive integer $y$ such that $x = y^2$. For example, $49$ and $1$ are perfect squares because $49 = 7^2$ and $1 = 1^2$ respectively. On the other hand, $5$ is not a perfect square as no integer squared equals $5$
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot 10^5$). The description of the test cases follows.
The only line of each test case contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of BBQ buns.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, if no valid choice of fillings exists, output $-1$. Otherwise, output $n$ integers, where the $i$-th integer represents the filling of the $i$-th BBQ bun. If there are multiple solutions, print any of them.
### Example
#### Input #1
```
2
3
12
```
#### Output #1
```
-1
1 2 3 6 10 2 7 6 10 1 7 3
```
### Note
In the first test case, the choice of fillings "1 1 1" is not allowed because buns $1$ and $3$ have the same filling, but are distance $2$ apart, which is not a perfect square. The choice of fillings "1 1 2" is also not allowed as filling $2$ is only used once.
In the second test case, the solution is valid because no filling is used exactly once, and any two buns with the same filling are spaced at a distance equal to a perfect square. For example, buns $1$ and $10$ both have filling $1$ and are spaced at a distance of $9=3^2$. Similarly, buns $5$ and $9$ both have filling $10$ and are spaced at a distance of $4=2^2$. | codeforces | https://codeforces.com/problemset/problem/2031/C |
2031A | A. Penchick and Modern Monument | easy | Amidst skyscrapers in the bustling metropolis of Metro Manila, the newest Noiph mall in the Philippines has just been completed! The construction manager, Penchick, ordered a state-of-the-art monument to be built with $n$ pillars.
The heights of the monument's pillars can be represented as an array $h$ of $n$ positive integers, where $h_i$ represents the height of the $i$-th pillar for all $i$ between $1$ and $n$.
Penchick wants the heights of the pillars to be in non-decreasing order, i.e. $h_i \le h_{i + 1}$ for all $i$ between $1$ and $n - 1$. However, due to confusion, the monument was built such that the heights of the pillars are in non-increasing order instead, i.e. $h_i \ge h_{i + 1}$ for all $i$ between $1$ and $n - 1$.
Luckily, Penchick can modify the monument and do the following operation on the pillars as many times as necessary:
- Modify the height of a pillar to any positive integer. Formally, choose an index $1\le i\le n$ and a positive integer $x$. Then, assign $h_i := x$.
Help Penchick determine the minimum number of operations needed to make the heights of the monument's pillars non-decreasing.
### Input
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 a single integer $n$ ($1 \leq n \leq 50$) — the number of pillars.
The second line of each test case contains $n$ integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le n$ and $h_i\ge h_{i+1}$) — the height of the pillars.
Please take note that the given array $h$ is non-increasing.
Note that there are no constraints on the sum of $n$ over all test cases.
### Output
For each test case, output a single integer representing the minimum number of operations needed to make the heights of the pillars non-decreasing.
### Example
#### Input #1
```
3
5
5 4 3 2 1
3
2 2 1
1
1
```
#### Output #1
```
4
1
0
```
### Note
In the first test case, the initial heights of pillars are $h = [5, 4, 3, 2, 1]$.
- In the first operation, Penchick changes the height of pillar $1$ to $h_1 := 2$.
- In the second operation, he changes the height of pillar $2$ to $h_2 := 2$.
- In the third operation, he changes the height of pillar $4$ to $h_4 := 4$.
- In the fourth operation, he changes the height of pillar $5$ to $h_5 := 4$.
After the operation, the heights of the pillars are $h = [2, 2, 3, 4, 4]$, which is non-decreasing. It can be proven that it is not possible for Penchick to make the heights of the pillars non-decreasing in fewer than $4$ operations.
In the second test case, Penchick can make the heights of the pillars non-decreasing by modifying the height of pillar $3$ to $h_3 := 2$.
In the third test case, the heights of pillars are already non-decreasing, so no operations are required. | codeforces | https://codeforces.com/problemset/problem/2031/A |
2031B | B. Penchick and Satay Sticks | easy | Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya!
In the bustling food stalls of Surabaya, Kohane bought $n$ satay sticks and arranged them in a line, with the $i$-th satay stick having length $p_i$. It is given that $p$ is a permutation$^{\text{∗}}$ of length $n$.
Penchick wants to sort the satay sticks in increasing order of length, so that $p_i=i$ for each $1\le i\le n$. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly $1$. Formally, they can perform the following operation any number of times (including zero):
- Select an index $i$ ($1\le i\le n-1$) such that $|p_{i+1}-p_i|=1$;
- Swap $p_i$ and $p_{i+1}$.
Determine whether it is possible to sort the permutation $p$, thus the satay sticks, by performing the above operation.
$^{\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
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot 10^5$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot 10^5$) — the number of satay sticks.
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — the permutation $p$ representing the length of the satay sticks.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, output "YES" if it is possible to sort permutation $p$ by performing the operation. Otherwise, output "NO".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
### Example
#### Input #1
```
2
4
2 1 3 4
4
4 2 3 1
```
#### Output #1
```
YES
NO
```
### Note
In the first test case, we can sort permutation $p = [2, 1, 3, 4]$ by performing an operation on index $1$ ($|p_2 - p_1| = |1 - 2| = 1$), resulting in $p = [1, 2, 3, 4]$.
In the second test case, it can be proven that it is impossible to sort permutation $p = [4, 2, 3, 1]$ by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation:
- Select $i = 2$ ($|p_3 - p_2| = |3 - 2| = 1$). This results in $p = [4, 3, 2, 1]$.
- Select $i = 1$ ($|p_2 - p_1| = |3 - 4| = 1$). This results in $p = [3, 4, 2, 1]$.
- Select $i = 3$ ($|p_4 - p_3| = |1 - 2| = 1$). This results in $p = [3, 4, 1, 2]$.
Unfortunately, permutation $p$ remains unsorted after performing the operations. | codeforces | https://codeforces.com/problemset/problem/2031/B |
2028C | C. Alice's Adventures in Cutting Cake | easy | 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
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
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.
### Example
#### Input #1
```
7
6 2 1
1 1 10 1 1 10
6 2 2
1 1 10 1 1 10
6 2 3
1 1 10 1 1 10
6 2 10
1 1 10 1 1 10
6 2 11
1 1 10 1 1 10
6 2 12
1 1 10 1 1 10
6 2 12
1 1 1 1 10 10
```
#### Output #1
```
22
12
2
2
2
0
-1
```
### Note
For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $10 + 1 + 1 + 10 = 22$ tastiness for herself. We can show that she cannot do any better.
For the second test case, Alice could give the first and second section as one piece, and the sixth section as one piece. She can then take the remaining $10 + 1 + 1 = 12$ tastiness for herself. We can show that she cannot do any better.
For the seventh test case, Alice cannot give each creature a piece of at least $12$ tastiness. | codeforces | https://codeforces.com/problemset/problem/2028/C |
2028B | B. Alice's Adventures in Permuting | easy | 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
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
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.
### Example
#### Input #1
```
7
10 1 0
1 2 3
100 2 1
3 0 1
3 0 0
1000000000000000000 0 0
1000000000000000000 1000000000000000000 1000000000000000000
```
#### Output #1
```
0
1
50
2
-1
-1
1000000000000000000
```
### Note
In the first test case, the array is already $[0, 1, \ldots, 9]$, so no operations are required.
In the third test case, the starting array is $[1, 3, 5, \ldots, 199]$. After the first operation, the $199$ gets transformed into a $0$. In the second operation, the $197$ gets transformed into a $2$. If we continue this, it will take exactly $50$ operations to get the array $[0, 1, 2, 3, \ldots, 99]$.
In the fourth test case, two operations are needed: $[1,1,1] \to [0,1,1] \to [0,2,1]$.
In the fifth test case, the process is $[0,0,0] \to [1,0,0] \to [2,0,0] \to [1,0,0] \to [2,0,0]$. This process repeats forever, so the array is never a permutation and the answer is $-1$. | codeforces | https://codeforces.com/problemset/problem/2028/B |
2028A | A. Alice's Adventures in "Chess" | easy | 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
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
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.
### Example
#### Input #1
```
6
2 2 2
NE
3 2 2
NNE
6 2 1
NNEESW
6 10 10
NNEESW
3 4 2
NEE
4 5 5
NEWS
```
#### Output #1
```
YES
NO
YES
YES
YES
NO
```
### Note
In the first test case, Alice follows the path $(0,0) \xrightarrow[\texttt{N}]{} (0,1) \xrightarrow[\texttt{E}]{} (1,1) \xrightarrow[\texttt{N}]{} (1,2) \xrightarrow[\texttt{E}]{} (2,2)$.
In the second test case, Alice can never reach the Red Queen. | codeforces | https://codeforces.com/problemset/problem/2028/A |
2029C | C. New Rating | easy | 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
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
For each test case, output a single integer — the maximum possible rating after the recalculation if Kevin chooses the interval optimally.
### Example
#### Input #1
```
5
6
1 2 3 4 5 6
7
1 2 1 1 1 3 4
1
1
9
9 9 8 2 4 4 3 5 3
10
1 2 3 4 1 3 2 1 1 10
```
#### Output #1
```
5
4
0
4
5
```
### Note
In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $1$, his rating after the recalculation will be equal to $5$.
In the second test case, Kevin's optimal choice is to select the interval $[3,5]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=1} 1 \xrightarrow{a_2=2} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{a_6=3} 3 \xrightarrow{a_7=4} 4 $$
In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of $0$.
In the fourth test case, Kevin's optimal choice is to select the interval $[7,9]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=9} 1 \xrightarrow{a_2=9} 2 \xrightarrow{a_3=8} 3 \xrightarrow{a_4=2} 2 \xrightarrow{a_5=4} 3 \xrightarrow{a_6=4} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 $$
In the fifth test case, Kevin's optimal choice is to select the interval $[5,9]$. | codeforces | https://codeforces.com/problemset/problem/2029/C |
2029A | A. Set | easy | 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
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
For each test case, output a single integer — the maximum possible number of operations that can be performed.
### Example
#### Input #1
```
8
3 9 2
4 9 1
7 9 2
2 10 2
154 220 2
147 294 2
998 24435 3
1 1000000000 2
```
#### Output #1
```
2
6
0
4
0
1
7148
500000000
```
### Note
In the first test case, initially, $S = \{3,4,5,6,7,8,9\}$. One possible optimal sequence of operations is:
1. Choose $x = 4$ for the first operation, since there are two multiples of $4$ in $S$: $4$ and $8$. $S$ becomes equal to $\{3,5,6,7,8,9\}$;
2. Choose $x = 3$ for the second operation, since there are three multiples of $3$ in $S$: $3$, $6$, and $9$. $S$ becomes equal to $\{5,6,7,8,9\}$.
In the second test case, initially, $S=\{4,5,6,7,8,9\}$. One possible optimal sequence of operations is:
1. Choose $x = 5$, $S$ becomes equal to $\{4,6,7,8,9\}$;
2. Choose $x = 6$, $S$ becomes equal to $\{4,7,8,9\}$;
3. Choose $x = 4$, $S$ becomes equal to $\{7,8,9\}$;
4. Choose $x = 8$, $S$ becomes equal to $\{7,9\}$;
5. Choose $x = 7$, $S$ becomes equal to $\{9\}$;
6. Choose $x = 9$, $S$ becomes equal to $\{\}$.
In the third test case, initially, $S=\{7,8,9\}$. For each $x$ in $S$, no multiple of $x$ other than $x$ itself can be found in $S$. Since $k = 2$, you can perform no operations.
In the fourth test case, initially, $S=\{2,3,4,5,6,7,8,9,10\}$. One possible optimal sequence of operations is:
1. Choose $x = 2$, $S$ becomes equal to $\{3,4,5,6,7,8,9,10\}$;
2. Choose $x = 4$, $S$ becomes equal to $\{3,5,6,7,8,9,10\}$;
3. Choose $x = 3$, $S$ becomes equal to $\{5,6,7,8,9,10\}$;
4. Choose $x = 5$, $S$ becomes equal to $\{6,7,8,9,10\}$. | codeforces | https://codeforces.com/problemset/problem/2029/A |
2029B | B. Replacement | easy | 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
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
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.
### Example
#### Input #1
```
6
2
11
0
2
01
1
4
1101
001
6
111110
10000
6
010010
11010
8
10010010
0010010
```
#### Output #1
```
NO
YES
YES
NO
YES
NO
```
### Note
In the first test case, you cannot perform the first operation. Thus, you lose the game.
In the second test case, you can choose $k=1$ in the only operation, and after that, $s$ becomes equal to $\mathtt{1}$. Thus, you win the game.
In the third test case, you can perform the following operations: $\mathtt{1}\underline{\mathtt{10}}\mathtt{1}\xrightarrow{r_1=\mathtt{0}} \mathtt{1}\underline{\mathtt{01}} \xrightarrow{r_2=\mathtt{0}} \underline{\mathtt{10}} \xrightarrow{r_3=\mathtt{1}} \mathtt{1}$. | codeforces | https://codeforces.com/problemset/problem/2029/B |
2036C | C. Anya and 1100 | easy | While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones.
Now she wants to make it even more beautiful by performing $q$ operations on it.
Each operation is described by two integers $i$ ($1 \le i \le |s|$) and $v$ ($v \in \{0, 1\}$) and means that the $i$-th character of the string is assigned the value $v$ (that is, the assignment $s_i = v$ is performed).
But Anya loves the number $1100$, so after each query, she asks you to tell her whether the substring "1100" is present in her string (i.e. there exist such $1 \le i \le |s| - 3$ that $s_{i}s_{i + 1}s_{i + 2}s_{i + 3} = \texttt{1100}$).
### Input
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of the test case contains the string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$), consisting only of the characters "0" and "1". Here $|s|$ denotes the length of the string $s$.
The next line contains an integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries.
The following $q$ lines contain two integers $i$ ($1 \leq i \leq |s|$) and $v$ ($v \in \{0, 1\}$), describing the query.
It is guaranteed that the sum of $|s|$ across all test cases does not exceed $2 \cdot 10^5$. It is also guaranteed that the sum of $q$ across all test cases does not exceed $2 \cdot 10^5$.
### Output
For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
### Example
#### Input #1
```
4
100
4
1 1
2 0
2 0
3 1
1100000
3
6 1
7 1
4 1
111010
4
1 1
5 0
4 1
5 0
0100
4
3 1
1 1
2 0
2 1
```
#### Output #1
```
NO
NO
NO
NO
YES
YES
NO
NO
YES
YES
YES
NO
NO
NO
NO
``` | codeforces | https://codeforces.com/problemset/problem/2036/C |
2036B | B. Startup | easy | Arseniy came up with another business plan — to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$.
You can place any number of bottles on each shelf, but all bottles on the same shelf must be of the same brand.
Arseniy knows that all the bottles he puts on the shelves of the machine will be sold. Therefore, he asked you to calculate the maximum amount he can earn.
### Input
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$), where $n$ is the number of shelves in the machine, and $k$ is the number of bottles available to Arseniy.
The next $k$ lines contain two integers $b_i$ and $c_i$ ($1 \le b_i \le k, 1 \le c_i \le 1000$) — the brand and cost of the $i$-th bottle.
It is also guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$ and that the sum of $k$ across all test cases also does not exceed $2 \cdot 10^5$.
### Output
For each test case, output one integer — the maximum amount that Arseniy can earn.
### Example
#### Input #1
```
4
3 3
2 6
2 7
1 15
1 3
2 6
2 7
1 15
6 2
1 7
2 5
190000 1
1 1000
```
#### Output #1
```
28
15
12
1000
```
### Note
In the first test case, Arseniy has $3$ shelves in the vending machine. He can place, for example, two bottles of the brand $2$ on the first shelf and a bottle of the brand $1$ on the second shelf. Then the total cost of the bottles would be $6 + 7 + 15 = 28$.
In the second test case, he has only one shelf. It is not difficult to show that the optimal option is to place a bottle of the brand $1$ on it. Then the total cost will be $15$.
In the third test case, he has as many as $6$ shelves, so he can place all available bottles with a total cost of $7 + 5 = 12$. | codeforces | https://codeforces.com/problemset/problem/2036/B |
2036A | A. Quintomania | easy | Boris Notkin composes melodies. He represents them as a sequence of notes, where each note is encoded as an integer from $0$ to $127$ inclusive. The interval between two notes $a$ and $b$ is equal to $|a - b|$ semitones.
Boris considers a melody perfect if the interval between each two adjacent notes is either $5$ semitones or $7$ semitones.
After composing his latest melodies, he enthusiastically shows you his collection of works. Help Boris Notkin understand whether his melodies are perfect.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of melodies.
Each melody is described by two lines.
The first line contains an integer $n$ ($2 \leq n \leq 50$) — the number of notes in the melody.
The second line contains $n$ integers $a_{1}, a_{2}, \dots, a_{n}$ ($0 \leq a_{i} \leq 127$) — the notes of the melody.
### Output
For each melody, output "YES", if it is perfect; otherwise, output "NO".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
### Example
#### Input #1
```
8
2
114 109
2
17 10
3
76 83 88
8
38 45 38 80 85 92 99 106
5
63 58 65 58 65
8
117 124 48 53 48 43 54 49
5
95 102 107 114 121
10
72 77 82 75 70 75 68 75 68 75
```
#### Output #1
```
YES
YES
YES
NO
YES
NO
YES
YES
``` | codeforces | https://codeforces.com/problemset/problem/2036/A |
2036E | E. Reverse the Rivers | easy | A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy — that's what sages do.
There are $n$ countries, each with exactly $k$ regions. For the $j$-th region of the $i$-th country, they calculated the value $a_{i,j}$, which reflects the amount of water in it.
The sages intend to create channels between the $j$-th region of the $i$-th country and the $j$-th region of the $(i + 1)$-th country for all $1 \leq i \leq (n - 1)$ and for all $1 \leq j \leq k$.
Since all $n$ countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the $j$-th region of the $i$-th country will be $b_{i,j} = a_{1,j} | a_{2,j} | ... | a_{i,j}$, where $|$ denotes the [bitwise "OR"](http://tiny.cc/bitwise_or) operation.
After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you $q$ queries for consideration.
Each query will contain $m$ requirements.
Each requirement contains three parameters: the region number $r$, the sign $o$ (either "$<$" or "$>$"), and the value $c$. If $o$ = "$<$", then in the $r$-th region of the country you choose, the new value must be strictly less than the limit $c$, and if $o$ = "$>$", it must be strictly greater.
In other words, the chosen country $i$ must satisfy all $m$ requirements. If in the current requirement $o$ = "$<$", then it must hold that $b_{i,r} < c$, and if $o$ = "$>$", then $b_{i,r} > c$.
In response to each query, you should output a single integer — the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output $-1$.
### Input
The first line contains three integers $n$, $k$, and $q$ ($1 \leq n, k, q \leq 10^5$) — the number of countries, regions, and queries, respectively.
Next, there are $n$ lines, where the $i$-th line contains $k$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,k}$ ($1 \leq a_{i,j} \leq 10^9$), where $a_{i,j}$ is the value of the $j$-th region of the $i$-th country.
Then, $q$ queries are described.
The first line of each query contains a single integer $m$ ($1 \leq m \leq 10^5$) — the number of requirements.
Then follow $m$ lines, each containing an integer $r$, a character $o$, and an integer $c$ ($1 \leq r \leq k$, $0 \leq c \leq 2 \cdot 10^9$), where $r$ and $c$ are the region number and the value, and $o$ is either "$<$" or "$>$" — the sign.
It is guaranteed that $n \cdot k$ does not exceed $10^5$ and that the sum of $m$ across all queries also does not exceed $10^5$.
### Output
For each query, output a single integer on a new line — the smallest number of the suitable country, or $-1$ if no such country exists.
### Example
#### Input #1
```
3 4 4
1 3 5 9
4 6 5 3
2 1 2 7
3
1 > 4
2 < 8
1 < 6
2
1 < 8
2 > 8
1
3 > 5
2
4 > 8
1 < 8
```
#### Output #1
```
2
-1
3
1
```
### Note
In the example, the initial values of the regions are as follows:
$1
$$3$$
5
$$9$$
4
$$6$$
5
$$3$$
2
$$1$$
2$$7$
After creating the channels, the new values will look like this:
$1
$$3$$
5
$$9$$
1 | 4
$$3 | 6$$
5 | 5
$$9 | 3$$
1 | 4 | 2
$$3 | 6 | 1$$
5 | 5 | 2
$$9 | 3 | 7$ $\downarrow$ $1$$
3
$$5$$
9
$$5$$
7
$$5$$
11
$$7$$
7
$$7$$
15$
In the first query, it is necessary to output the minimum country number (i.e., row) where, after the redistribution of water in the first region (i.e., column), the new value will be greater than four and less than six, and in the second region it will be less than eight. Only the country with number $2$ meets these requirements.
In the second query, there are no countries that meet the specified requirements.
In the third query, only the country with number $3$ is suitable.
In the fourth query, all three countries meet the conditions, so the answer is the smallest number $1$. | codeforces | https://codeforces.com/problemset/problem/2036/E |
2036D | D. I Love 1543 | easy | One morning, Polycarp woke up and realized that $1543$ is the most favorite number in his life.
The first thing that Polycarp saw that day as soon as he opened his eyes was a large wall carpet of size $n$ by $m$ cells; $n$ and $m$ are even integers. Each cell contains one of the digits from $0$ to $9$.
Polycarp became curious about how many times the number $1543$ would appear in all layers$^{\text{∗}}$ of the carpet when traversed clockwise.
$^{\text{∗}}$The first layer of a carpet of size $n \times m$ is defined as a closed strip of length $2 \cdot (n+m-2)$ and thickness of $1$ element, surrounding its outer part. Each subsequent layer is defined as the first layer of the carpet obtained by removing all previous layers from the original carpet.
### Input
The first line of the input contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The following lines describe the test cases.
The first line of each test case contains a pair of numbers $n$ and $m$ ($2 \leq n, m \leq 10^3$, $n, m$ — even integers).
This is followed by $n$ lines of length $m$, consisting of digits from $0$ to $9$ — the description of the carpet.
It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $10^6$.
### Output
For each test case, output a single number — the total number of times $1543$ appears in all layers of the carpet in the order of traversal clockwise.
### Example
#### Input #1
```
8
2 4
1543
7777
2 4
7154
8903
2 4
3451
8888
2 2
54
13
2 2
51
43
2 6
432015
512034
4 4
5431
1435
5518
7634
6 4
5432
1152
4542
2432
2302
5942
```
#### Output #1
```
1
1
0
1
0
2
2
2
```
### Note
Occurrences of $1543$ in the seventh example. Different layers are colored in different colors. | codeforces | https://codeforces.com/problemset/problem/2036/D |
2032D | D. Genokraken | easy | 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$).
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
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$.
### Interaction
For each test case, interaction starts by reading the integer $n$.
Then you can make queries of the following type:
- "? a b" (without quotes) ($1 \le a, b < n$, $a \ne b$).
After the query, read an integer $r$ — the answer to your query. You are allowed to use at most $2n - 6$ queries of this type.
- If the simple path between nodes $a$ and $b$ does not contain node $0$, you will get $r = 0$.
- If the simple path between nodes $a$ and $b$ contains node $0$, you will get $r = 1$.
- In case you make more than $2n-6$ queries or make an invalid query, you will get $r = -1$. You will need to terminate after this to get the "Wrong answer" verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out the structure, output a line in the format "! $p_1 \space p_2 \ldots p_{n-1}$" (without quotes), where $p_i$ ($0 \le p_i < i$) denotes the index of the direct parent of node $i$. This query is not counted towards the $2n - 6$ queries limit.
After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately.
After printing any query do not forget to output an end of line and flush the output buffer. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
The interactor is non-adaptive. The tree does not change during the interaction.
Hacks
For hack, use the following format:
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.
The second line of each test case contains $n-1$ integers $p_1, p_2, \ldots, p_{n-1}$ ($0 \le p_1 \le p_2 \le \ldots \le p_{n-1} \le n - 2$, $0 \le p_i < i$) — the direct parents of node $1$, $2$, ..., $n-1$ in the system, respectively.
In each test case, the values $p_1, p_2, \ldots, p_{n-1}$ must ensure the following in the tree:
- If we remove root node $0$ and all adjacent edges, this tree will turn into a forest consisting of only paths. Each node that was initially adjacent to the node $0$ will be the end of some path.
- Node $1$ has exactly two adjacent nodes (including the node $0$).
The sum of $n$ over all test cases must not exceed $10^4$.
### Example
#### Input #1
```
3
4
1
5
1
0
9
```
#### Output #1
```
? 2 3
! 0 0 1
? 2 3
? 2 4
! 0 0 1 2
! 0 0 0 1 3 5 6 7```
### Note
In the first test case, Genokraken's nerve system forms the following tree:

- The answer to "? 2 3" is $1$. This means that the simple path between nodes $2$ and $3$ contains node $0$.
In the second test case, Genokraken's nerve system forms the following tree:

- The answer to "? 2 3" is $1$. This means that the simple path between nodes $2$ and $3$ contains node $0$.
- The answer to "? 2 4" is $0$. This means that the simple path between nodes $2$ and $4$ doesn't contain node $0$.
In the third test case, Genokraken's nerve system forms the following tree:
 | codeforces | https://codeforces.com/problemset/problem/2032/D |
2032B | B. Medians | easy | 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
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
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.
### Example
#### Input #1
```
4
1 1
3 2
3 3
15 8
```
#### Output #1
```
1
1
3
1 2 3
-1
5
1 4 7 10 13
```
### Note
In the first test case, the given partition has $m = 1$ and $b_1 = [1]$. It is obvious that $\operatorname{median}([\operatorname{median}([1])]) = \operatorname{median}([1]) = 1$.
In the second test case, the given partition has $m = 3$ and:
- $b_1 = [1]$
- $b_2 = [2]$
- $b_3 = [3]$
Therefore, $\operatorname{median}([\operatorname{median}([1]), \operatorname{median}([2]), \operatorname{median}([3])]) = \operatorname{median}([1, 2, 3]) = 2$.
In the third test case, there is no valid partition for $k = 3$.
In the fourth test case, the given partition has $m = 5$ and:
- $b_1 = [1, 2, 3]$
- $b_2 = [4, 5, 6]$
- $b_3 = [7, 8, 9]$
- $b_4 = [10, 11, 12]$
- $b_5 = [13, 14, 15]$
Therefore, $\operatorname{median}([\operatorname{median}([1, 2, 3]), \operatorname{median}([4, 5, 6]), \operatorname{median}([7, 8, 9]), \operatorname{median}([10, 11, 12]), \operatorname{median}([13, 14, 15])]) = \operatorname{median}([2, 5, 8, 11, 14]) = 8$. | codeforces | https://codeforces.com/problemset/problem/2032/B |
2032A | A. Circuit | easy | 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
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
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.
### Example
#### Input #1
```
5
1
0 0
1
0 1
1
1 1
3
0 0 1 0 1 0
3
0 1 1 1 0 0
```
#### Output #1
```
0 0
1 1
0 0
0 2
1 3
```
### Note
In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off.
In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on.
In the third test case, there is only one light in the circuit, and both switches are on, so the light is off as it was toggled twice.
In the fourth test case, to have no lights on, the switches can be arranged in this way:
- Switch $1$ and switch $4$ are connected to light $1$. Since both switches are off, light $1$ is also off.
- Switch $2$ and switch $6$ are connected to light $2$. Since both switches are off, light $2$ is also off.
- Switch $3$ and switch $5$ are connected to light $3$. Both switches are on, so light $3$ is toggled twice from its initial off state, and thus also stays off.
And to have $2$ lights on, the switches can be arranged in this way:
- Switch $1$ and switch $2$ are connected to light $1$. Since both switches are off, light $1$ is also off.
- Switch $3$ and switch $4$ are connected to light $2$. Since switch $3$ is on and switch $4$ is off, light $2$ is toggled once from its initial off state, so it is on.
- Switch $5$ and switch $6$ are connected to light $3$. Since switch $5$ is on and switch $6$ is off, light $3$ is toggled once from its initial off state, so it is on. | codeforces | https://codeforces.com/problemset/problem/2032/A |
2032C | C. Trinity | easy | 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
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
For each test case, output a single integer — the minimum number of operations required.
### Example
#### Input #1
```
4
7
1 2 3 4 5 6 7
3
1 3 2
3
4 5 3
15
9 3 8 1 6 5 3 8 2 1 4 2 9 4 7
```
#### Output #1
```
3
1
0
8
```
### Note
In the first test case, one of the possible series of operations would be:
- Assign $a_1 := a_4 = 4$. The array will become $[4, 2, 3, 4, 5, 6, 7]$.
- Assign $a_2 := a_5 = 5$. The array will become $[4, 5, 3, 4, 5, 6, 7]$.
- Assign $a_7 := a_1 = 4$. The array will become $[4, 5, 3, 4, 5, 6, 4]$.
It can be proven that any triplet of elements with pairwise distinct indices in the final array forms a non-degenerate triangle, and there is no possible answer using less than $3$ operations.
In the second test case, we can assign $a_1 := a_2 = 3$ to make the array $a = [3, 3, 2]$.
In the third test case, since $3$, $4$ and $5$ are valid side lengths of a triangle, we don't need to perform any operation to the array. | codeforces | https://codeforces.com/problemset/problem/2032/C |
2026C | C. Action Figures | easy | 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
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
For each test case, print one integer — the minimum amount of money Monocarp has to spend.
### Example
#### Input #1
```
4
1
1
6
101101
7
1110001
5
11111
```
#### Output #1
```
1
8
18
6
```
### Note
In the first test case, Monocarp buys the $1$-st figure on the $1$-st day and spends $1$ coin.
In the second test case, Monocarp can buy the $1$-st and the $3$-rd figure on the $3$-rd day, the $2$-nd and the $4$-th figure on the $4$-th day, and the $5$-th and the $6$-th figure on the $6$-th day. Then, he will spend $1+2+5=8$ coins.
In the third test case, Monocarp can buy the $2$-nd and the $3$-rd figure on the $3$-rd day, and all other figures on the $7$-th day. Then, he will spend $1+2+4+5+6 = 18$ coins. | codeforces | https://codeforces.com/problemset/problem/2026/C |
2026A | A. Perpendicular Segments | easy | You are given a coordinate plane and three integers $X$, $Y$, and $K$. Find two line segments $AB$ and $CD$ such that
1. the coordinates of points $A$, $B$, $C$, and $D$ are integers;
2. $0 \le A_x, B_x, C_x, D_x \le X$ and $0 \le A_y, B_y, C_y, D_y \le Y$;
3. the length of segment $AB$ is at least $K$;
4. the length of segment $CD$ is at least $K$;
5. segments $AB$ and $CD$ are perpendicular: if you draw lines that contain $AB$ and $CD$, they will cross at a right angle.
Note that it's not necessary for segments to intersect. Segments are perpendicular as long as the lines they induce are perpendicular.
### Input
The first line contains a single integer $t$ ($1 \le t \le 5000$) — the number of test cases. Next, $t$ cases follow.
The first and only line of each test case contains three integers $X$, $Y$, and $K$ ($1 \le X, Y \le 1000$; $1 \le K \le 1414$).
Additional constraint on the input: the values of $X$, $Y$, and $K$ are chosen in such a way that the answer exists.
### Output
For each test case, print two lines. The first line should contain $4$ integers $A_x$, $A_y$, $B_x$, and $B_y$ — the coordinates of the first segment.
The second line should also contain $4$ integers $C_x$, $C_y$, $D_x$, and $D_y$ — the coordinates of the second segment.
If there are multiple answers, print any of them.
### Example
#### Input #1
```
4
1 1 1
3 4 1
4 3 3
3 4 4
```
#### Output #1
```
0 0 1 0
0 0 0 1
2 4 2 2
0 1 1 1
0 0 1 3
1 2 4 1
0 1 3 4
0 3 3 0
```
### Note
The answer for the first test case is shown below:
 The answer for the second test case:  The answer for the third test case:  The answer for the fourth test case:  | codeforces | https://codeforces.com/problemset/problem/2026/A |
2026B | B. Black Cells | easy | You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white.
You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black.
A list $a$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $k$ for which this is possible.
### Input
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$).
Additional constraint on the input: the sum of $n$ across all test cases does not exceed $2000$.
### Output
For each test case, print a single integer — the minimum value of $k$ for which it is possible to paint all the given cells black.
### Example
#### Input #1
```
4
2
1 2
1
7
3
2 4 9
5
1 5 8 10 13
```
#### Output #1
```
1
1
2
3
```
### Note
In the first example, with $k=1$, it is possible to paint the cells $(1, 2)$.
In the second example, with $k=1$, it is possible to paint the cells $(7, 8)$.
In the third example, with $k=2$, it is possible to paint the cells $(2, 4)$ and $(8, 9)$.
In the fourth example, with $k=3$, it is possible to paint the cells $(0, 1)$, $(5, 8)$ and $(10, 13)$. | codeforces | https://codeforces.com/problemset/problem/2026/B |
2035D | D. Yet Another Real Number Problem | easy | 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
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
For each test case, output $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$.
### Example
#### Input #1
```
3
10
1 2 3 4 5 6 7 8 9 10
11
1 6 9 4 7 4 4 10 3 2 3
4
527792568 502211460 850237282 374773208
```
#### Output #1
```
1 3 8 13 46 59 126 149 1174 1311
1 7 22 26 70 74 150 1303 1306 1308 1568
527792568 83665723 399119771 773892979
```
### Note
For each prefix in the first example, a possible array after operations is:
- $[1]$ and the sum is $1$;
- $[1, 2]$ and the sum is $3$;
- $[1, 1, 6]$ and the sum is $8$;
- $[1, 1, 3, 8]$ and the sum is $13$;
- $[1, 1, 3, 1, 40]$ and the sum is $46$;
- $[1, 1, 3, 1, 5, 48]$ and the sum is $59$;
- $[1, 1, 3, 1, 5, 3, 112]$ and the sum is $126$;
- $[1, 1, 3, 1, 5, 3, 7, 128]$ and the sum is $149$;
- $[1, 1, 3, 1, 5, 3, 7, 1, 1152]$ and the sum is $1174$;
- $[1, 1, 3, 1, 5, 3, 7, 1, 9, 1280]$ and the sum is $1311$. | codeforces | https://codeforces.com/problemset/problem/2035/D |
2035A | A. Sliding | easy | 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$.

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
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
For each test case, output a single integer denoting the sum of the Manhattan distances.
### Example
#### Input #1
```
4
2 3 1 2
2 2 2 1
1 1 1 1
1000000 1000000 1 1
```
#### Output #1
```
6
1
0
1999998000000
```
### Note
For the first test case, the person numbered $2$ leaves, and the distances of the movements of the person numbered $3$, $4$, $5$, and $6$ are $1$, $3$, $1$, and $1$, respectively. So the answer is $1+3+1+1=6$.
For the second test case, the person numbered $3$ leaves, and the person numbered $4$ moves. The answer is $1$. | codeforces | https://codeforces.com/problemset/problem/2035/A |
2035B | B. Everyone Loves Tres | easy | 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
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
For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise.
### Example
#### Input #1
```
6
1
2
3
4
5
7
```
#### Output #1
```
-1
66
-1
3366
36366
3336366
```
### Note
For $n=1$, no such integer exists as neither $3$ nor $6$ is divisible by $33$.
For $n=2$, $66$ consists only of $6$s and it is divisible by both $33$ and $66$.
For $n=3$, no such integer exists. Only $363$ is divisible by $33$, but it is not divisible by $66$.
For $n=4$, $3366$ and $6666$ are divisible by both $33$ and $66$, and $3366$ is the smallest. | codeforces | https://codeforces.com/problemset/problem/2035/B |
2035C | C. Alya and Permutation | easy | 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
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
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.
### Example
#### Input #1
```
6
5
6
7
8
9
10
```
#### Output #1
```
5
2 1 3 4 5
7
1 2 4 6 5 3
7
2 4 5 1 3 6 7
15
2 4 5 1 3 6 7 8
9
2 4 5 6 7 1 3 8 9
15
1 2 3 4 5 6 8 10 9 7
```
### Note
For the first test case, the value of $k$ is determined as follows:
$k = 0$ initially.
- On the $1$st operation, $1$ is odd, so Alya sets $k$ to be $k\&p_1 = 0\&2 = 0$.
- On the $2$nd operation, $2$ is even, so Alya sets $k$ to be $k|p_2 = 0|1 = 1$.
- On the $3$rd operation, $3$ is odd, so Alya sets $k$ to be $k\&p_3 = 1\&3 = 1$.
- On the $4$th operation, $4$ is even, so Alya sets $k$ to be $k|p_4 = 1|4 = 5$.
- On the $5$th operation, $5$ is odd, so Alya sets $k$ to be $k\&p_5 = 5\&5 = 5$.
The final value of $k$ is $5$. It can be shown that the final value of $k$ is at most $5$ for all permutations of length $5$. Another valid output is $[2, 3, 1, 4, 5]$.
For the second test case, the final value of $k$ is $7$. It can be shown that the final value of $k$ is at most $7$ for all permutations of length $6$. Other valid outputs include $[2, 4, 1, 6, 3, 5]$ and $[5, 2, 6, 1, 3, 4]$. | codeforces | https://codeforces.com/problemset/problem/2035/C |
2027C | C. Add Zeros | easy | 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
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
For each test case, output a single integer — the maximum possible length of $a$ after performing some sequence of operations.
### Example
#### Input #1
```
4
5
2 4 6 2 5
5
5 4 4 5 1
4
6 8 2 3
1
1
```
#### Output #1
```
10
11
10
1
```
### Note
In the first test case, we can first choose $i = 4$, since $a_4 = 5 + 1 - 4 = 2$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0]$. We can then choose $i = 3$ since $a_3 = 8 + 1 - 3 = 6$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0, 0, 0]$, which has a length of $10$. It can be shown that no sequence of operations will make the final array longer.
In the second test case, we can choose $i=2$, then $i=3$, then $i=4$. The final array will be $[5, 4, 4, 5, 1, 0, 0, 0, 0, 0, 0]$, with a length of $11$. | codeforces | https://codeforces.com/problemset/problem/2027/C |
2027A | A. Rectangle Arrangement | easy | 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
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
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.
### Example
#### Input #1
```
5
5
1 5
2 4
3 3
4 2
5 1
3
2 2
1 1
1 2
1
3 2
3
100 100
100 100
100 100
4
1 4
2 3
1 5
3 2
```
#### Output #1
```
20
8
10
400
16
```
### Note
In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity.

After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $20$. It can be shown that there is no way of using the stamps that yields a lower total perimeter.
In the second test case, the second and third stamps can be used entirely inside the first one, so the minimum perimeter is equal to $8$. | codeforces | https://codeforces.com/problemset/problem/2027/A |
2027B | B. Stalin Sort | easy | 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
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
For each test case, output a single integer — the minimum number of integers which must be removed from the array to make it vulnerable.
### Example
#### Input #1
```
6
7
3 6 4 9 2 5 2
5
5 4 4 2 2
8
2 2 4 4 6 6 10 10
1
1000
9
6 8 9 10 12 9 7 5 4
7
300000000 600000000 400000000 900000000 200000000 400000000 200000000
```
#### Output #1
```
2
0
6
0
4
2
```
### Note
In the first test case, the optimal answer is to remove the numbers $3$ and $9$. Then we are left with $a = [6, 4, 2, 5, 2]$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $[4, 2, 5]$ to get $a = [6, 4, 5, 2]$ and then apply a Stalin Sort on the subarray $[6, 4, 5]$ to get $a = [6, 2]$, which is non-increasing.
In the second test case, the array is already non-increasing, so we don't have to remove any integers. | codeforces | https://codeforces.com/problemset/problem/2027/B |
2027D1 | D1. The Endspeaker (Easy Version) | easy | 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
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
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$.
### Example
#### Input #1
```
5
4 2
9 3 4 3
11 7
1 2
20
19 18
10 2
2 5 2 1 10 3 2 9 9 6
17 9
10 11
2 2 2 2 2 2 2 2 2 2
20 18 16 14 12 10 8 6 4 2 1
1 6
10
32 16 8 4 2 1
```
#### Output #1
```
1
-1
2
10
4
```
### Note
In the first test case, one optimal sequence of operations which yields a total cost of $1$ is as follows:
- Perform an operation of type $2$. Choose the prefix to be $[9]$. This incurs a cost of $1$.
- Perform an operation of type $1$. The value of $k$ is now $2$. This incurs no cost.
- Perform an operation of type $2$. Choose the prefix to be $[3, 4]$. This incurs a cost of $0$.
- Perform an operation of type $2$. Choose the prefix to be $[3]$. This incurs a cost of $0$.
- The array $a$ is now empty, and the total cost of all operations is $1$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations. | codeforces | https://codeforces.com/problemset/problem/2027/D1 |
2033E | E. Sakurako, Kosuke, and the Permutation | easy | 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
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
For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple.
### Example
#### Input #1
```
6
5
1 2 3 4 5
5
5 4 3 2 1
5
2 3 4 5 1
4
2 3 4 1
3
1 3 2
7
2 3 1 5 6 7 4
```
#### Output #1
```
0
0
2
1
0
2
```
### Note
In the first and second examples, the permutations are already simple.
In the fourth example, it is sufficient to swap $p_2$ and $p_4$. Thus, the permutation will become $[2, 1, 4, 3]$ in $1$ operation. | codeforces | https://codeforces.com/problemset/problem/2033/E |
2033B | B. Sakurako and Water | easy | 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
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
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.
### Example
#### Input #1
```
4
1
1
2
-1 2
3 0
3
1 2 3
-2 1 -1
0 0 -1
5
1 1 -1 -1 3
-3 1 4 4 -4
-1 -1 3 0 -5
4 5 3 -3 -1
3 1 -3 -1 5
```
#### Output #1
```
0
1
4
19
``` | codeforces | https://codeforces.com/problemset/problem/2033/B |
2033A | A. Sakurako and Kosuke | easy | 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
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
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".
### Example
#### Input #1
```
4
1
6
3
98
```
#### Output #1
```
Kosuke
Sakurako
Kosuke
Sakurako
``` | codeforces | https://codeforces.com/problemset/problem/2033/A |
2033F | F. Kosuke's Sloth | easy | 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
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
For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$.
### Example
#### Input #1
```
3
3 2
100 1
1000000000000 1377
```
#### Output #1
```
9
100
999244007
``` | codeforces | https://codeforces.com/problemset/problem/2033/F |
2033C | C. Sakurako's Field Trip | easy | 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
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
For each test case, output the minimal possible disturbance of the line that you can achieve.
### Example
#### Input #1
```
9
5
1 1 1 2 3
6
2 1 2 2 1 1
4
1 2 1 1
6
2 1 1 2 2 4
4
2 1 2 3
6
1 2 2 1 2 1
5
4 5 5 1 5
7
1 4 3 5 1 1 3
7
3 1 3 2 2 3 3
```
#### Output #1
```
1
2
1
0
0
1
1
0
2
```
### Note
In the first example, it is necessary to apply the operation to $i=2$, thus the array will become $[1, \textbf{2}, 1, \textbf{1}, 3]$, with the bold elements indicating those that have swapped places. The disturbance of this array is equal to $1$.
In the fourth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[2, 1, \textbf{2}, \textbf{1}, 2, 4]$. The disturbance of this array is equal to $0$.
In the eighth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[1, 4, \textbf{1}, 5, \textbf{3}, 1, 3]$. The disturbance of this array is equal to $0$. | codeforces | https://codeforces.com/problemset/problem/2033/C |
2033D | D. Kousuke's Assignment | easy | 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
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
For each test case, output a single integer: the maximum number of non-overlapping beautiful segments.
### Example
#### Input #1
```
3
5
2 1 -3 2 1
7
12 -4 4 43 -3 -5 8
6
0 -4 0 3 0 1
```
#### Output #1
```
1
2
3
``` | codeforces | https://codeforces.com/problemset/problem/2033/D |
2023B | B. Skipping | easy | It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).
Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:
- They can submit the problem and receive $a_i$ points;
- They can skip the problem, in which case they will never be able to submit it.
Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:
- If he submitted the $i$-th problem, it looks at problems with indices $j < i$;
- If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$.
Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.
Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.
### Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) — the number of problems in the olympiad.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the scores of the problems.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) — the parameters of the problems.
It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
### Output
For each test case, output a single integer — the maximum number of points that Prokhor can achieve.
### Example
#### Input #1
```
4
2
15 16
2 1
5
10 10 100 100 1000
3 4 1 1 1
3
100 49 50
3 2 2
4
100 200 300 1000
2 3 4 1
```
#### Output #1
```
16
200
100
1000
```
### Note
In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 2$. Prokhor can submit it and receive $a_2 = 16$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive $a_1 = 15$ points, but the competition will end immediately.
In the second test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 3$. Prokhor can submit it and receive $a_3 = 100$ points. After that, Prokhor will receive the second problem, which he can skip to receive the problem with index $b_2 = 4$. Prokhor can submit the fourth problem and receive another $a_4 = 100$ points. After that, the competition ends because Prokhor has already received all problems with indices not exceeding $4$. Thus, Prokhor will receive a total of $200$ points.
In the third test case, Prokhor can submit the first problem and receive $100$ points, after which the competition will end immediately. | codeforces | https://codeforces.com/problemset/problem/2023/B |
2024B | B. Buying Lemonade | easy | There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot.
When you press the button corresponding to the $i$-th slot, one of two events occurs:
- If there is a can of lemonade in the $i$-th slot, it will drop out and you will take it. At this point, the number of cans in the $i$-th slot decreases by $1$.
- If there are no cans of lemonade left in the $i$-th slot, nothing will drop out.
After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: $a_1, a_2, \ldots, a_n$.
Determine the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.
Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least $k$ cans of lemonade in total in the machine. In other words, $k \leq a_1 + a_2 + \ldots + a_n$.
### Input
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 two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) — the number of slots in the machine and the required number of cans of lemonade.
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 number of cans in the slots.
It is guaranteed that $k \leq a_1 + a_2 + \ldots + a_n$, meaning there are at least $k$ cans of lemonade in the machine.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output a single integer — the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.
### Example
#### Input #1
```
5
2 1
1 1
2 2
1 2
3 4
2 1 3
10 50
1 1 3 8 8 9 12 13 27 27
2 1000000000
1000000000 500000000
```
#### Output #1
```
1
2
5
53
1000000000
```
### Note
In the first test case, we can simply press the first button and receive one can of lemonade.
In the second test case, we can press each button once and guarantee that we receive $2$ cans of lemonade. Note that if we simply press one button twice, we might not be lucky, and that button could correspond to the first slot, in which case we would only receive $1$ can of lemonade for two presses.
In the third test case, one of the optimal strategies is as follows:
Press the first button twice. After the first press, a can of lemonade will definitely drop out. Then there are two options:
- If no can of lemonade drops after the second press, we know that this button must correspond to the second slot, since $a_2 = 1$ and $a_1, a_3 > 1$. Then we can press the second button twice and the third button once. Since $a_1, a_3 \geq 2$, we will definitely receive three cans of lemonade for these three presses. Thus, after $5$ presses, we will have $4$ cans of lemonade.
- If a can of lemonade drops after the second press, we can make one press on the second button and one press on the third button. After each of these presses, we will definitely receive a can of lemonade. Thus, after $4$ presses, we will have $4$ cans of lemonade.
It can be shown that it is impossible to guarantee receiving $4$ cans of lemonade with only $4$ presses, so the answer is $5$. | codeforces | https://codeforces.com/problemset/problem/2024/B |
2023A | A. Concatenation of Arrays | easy | You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.
More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.
$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.
$^{\ddagger}$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
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 \le n \le 10^5$) — the number of arrays.
Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.
### Example
#### Input #1
```
4
2
1 4
2 3
3
3 2
4 3
2 1
5
5 10
2 3
9 6
4 1
8 7
1
10 20
```
#### Output #1
```
2 3 1 4
2 1 3 2 4 3
4 1 2 3 5 10 8 7 9 6
10 20
```
### Note
In the first test case, we concatenated the arrays in the order $2, 1$. Let's consider the inversions in the resulting array $b = [2, 3, 1, 4]$:
- $i = 1$, $j = 3$, since $b_1 = 2 > 1 = b_3$;
- $i = 2$, $j = 3$, since $b_2 = 3 > 1 = b_3$.
Thus, the number of inversions is $2$. It can be proven that this is the minimum possible number of inversions.
In the second test case, we concatenated the arrays in the order $3, 1, 2$. Let's consider the inversions in the resulting array $b = [2, 1, 3, 2, 4, 3]$:
- $i = 1$, $j = 2$, since $b_1 = 2 > 1 = b_2$;
- $i = 3$, $j = 4$, since $b_3 = 3 > 2 = b_4$;
- $i = 5$, $j = 6$, since $b_5 = 4 > 3 = b_6$.
Thus, the number of inversions is $3$. It can be proven that this is the minimum possible number of inversions.
In the third test case, we concatenated the arrays in the order $4, 2, 1, 5, 3$. | codeforces | https://codeforces.com/problemset/problem/2023/A |
2024A | A. Profitable Interest Rate | easy | Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins.
There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum amount required to open the "Profitable" deposit decreases by $2x$ coins. However, these coins cannot later be deposited into the "Profitable" deposit.
Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $0$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
### Input
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.
A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) — the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit.
### Output
For each test case, output a single integer — the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
### Example
#### Input #1
```
5
10 5
7 9
5 100
1 1
1 2
```
#### Output #1
```
10
5
0
1
0
```
### Note
In the first test case, $a \ge b$, so Alice can immediately open the "Profitable" deposit with all $10$ coins.
In the second test case, Alice can open the "Unprofitable" deposit with $2$ coins. Then she will have $5$ coins left, but the minimum amount required to open the "Profitable" deposit will decrease by $4$ coins, making it equal to $5$ coins. Thus, Alice will be able to open the "Profitable" deposit with $5$ coins.
In the third test case, Alice will not be able to open the "Profitable" deposit. | codeforces | https://codeforces.com/problemset/problem/2024/A |
2030C | C. A TRUE Battle | easy | 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
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
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).
### Example
#### Input #1
```
5
2
11
3
010
12
101111111100
10
0111111011
8
01000010
```
#### Output #1
```
YES
NO
YES
YES
NO
```
### Note
In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true.
In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right false. The statement false or true and false is false.
Note that these examples may not be the best strategies for either Alice or Bob. | codeforces | https://codeforces.com/problemset/problem/2030/C |
2030B | B. Minimise Oneness | easy | 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
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
For each test case, output $s$ on a new line. If multiple answers exist, output any.
### Example
#### Input #1
```
3
1
2
3
```
#### Output #1
```
0
01
010
```
### Note
In the first test case, for the example output, $f(t)=1$ because there is one subsequence that contains only $\mathtt{0}$ ($\mathtt{0}$), and $g(t)=0$ because there are no subsequences that contain at least one $1$. The oneness is $|1-0|=1$. The output $\mathtt{1}$ is correct as well because its oneness is $|0-1|=1$.
For the example output of the second test case, $f(t)=1$ because there is one non-empty subsequence that contains only $\mathtt{0}$, and $g(t)=2$ because there are two non-empty subsequences that contain at least one $\mathtt{1}$ ($\mathtt{01}$ and $\mathtt{1}$). The oneness is thus $|1-2|=1$. It can be shown that $1$ is the minimum possible value of its oneness over all possible binary strings of size $2$. | codeforces | https://codeforces.com/problemset/problem/2030/B |
2030D | D. QED's Favorite Permutation | easy | 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
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
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).
### Example
#### Input #1
```
3
5 3
1 4 2 5 3
RLRLL
2
4
3
8 5
1 5 2 4 8 3 6 7
RRLLRRRL
4
3
5
3
4
6 2
1 2 3 4 5 6
RLRLRL
4
5
```
#### Output #1
```
YES
YES
NO
NO
YES
NO
NO
NO
YES
YES
```
### Note
In the first testcase, $s = \texttt{RRRLL}$ after the first query. QED may sort $p$ using the following operations:
- Initially, $p = [1,4,2,5,3]$.
- Select $i = 2$ and swap $p_2$ with $p_{3}$. Now, $p = [1,2,4,5,3]$.
- Select $i = 5$ and swap $p_5$ with $p_{4}$. Now, $p = [1,2,4,3,5]$.
- Select $i = 4$ and swap $p_4$ with $p_{3}$. Now, $p = [1,2,3,4,5]$, which is in non-decreasing order.
It can be shown that it is impossible to sort the array after all three updates of the first testcase. | codeforces | https://codeforces.com/problemset/problem/2030/D |
2030A | A. A Gift From Orangutan | easy | 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
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
For each test case, output the maximum score that you can get.
### Example
#### Input #1
```
3
1
69
3
7 6 5
5
1 1 1 2 2
```
#### Output #1
```
0
4
4
```
### Note
In the first test case, there is no other way to rearrange $a$. So, $b = [69]$ and $c = [69]$. The only possible score is $69 - 69 = 0$.
In the second test case, you can rearrange $a$ as $[7, 5, 6]$. Here, $b = [7, 5, 5]$ and $c = [7, 7, 7]$. The score in this case is $(7 - 7) + (7 - 5) + (7 - 5) = 4$. It can be shown this is the maximum possible score. | codeforces | https://codeforces.com/problemset/problem/2030/A |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.