Codeforces Round 1067 (Div. 2)
A. Suspension
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A game is being played which initially consists of $$$n$$$ players. On fouls, the referee is allowed to award yellow and red cards. The total number of yellow cards $$$y$$$ and red cards $$$r$$$ awarded to players is known for the game.

There are $$$2$$$ ways a player can be suspended:

  • Receiving a red card.
  • Receiving $$$2$$$ yellow cards.

Once a player is suspended, they are removed from the game and can't receive any more cards. Determine the maximum number of players suspended from the game possible.

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 an integer $$$n$$$ ($$$1 \le n \le 100$$$).

The second line of each test case contains two integers $$$y$$$ and $$$r$$$ ($$$0 \le r \le n$$$, $$$0 \le y + r \le 2n$$$).

Output

For each test case, print one line containing a single integer — denoting the maximum number of players suspended from the game possible.

Example
Input
5
3
1 2
2
0 0
4
6 0
3
3 3
10
11 5
Output
2
0
3
3
10
Note

For the first test case:

  • One of the possible ways to achieve maximum: Player $$$2$$$ receives a red card, Player $$$3$$$ receives a yellow card followed by a red card.
  • This way Player $$$2$$$ and $$$3$$$ are suspended.

For the second test case:

  • No cards are awarded at all, so no one is suspended.

For the third test case:

  • One of the possible ways to achieve maximum: Player $$$1$$$ receives $$$2$$$ yellow cards, Player $$$2$$$ receives $$$2$$$ yellow cards, and Player $$$4$$$ receives $$$2$$$ yellow cards.
  • This way Player $$$1$$$, $$$2$$$, and $$$4$$$ are suspended.

B. Split
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence $$$a$$$ containing $$$2n$$$ integers. Let $$$f(b)$$$ denote the number of distinct elements with an odd number of occurrences in sequence $$$b$$$. You need to split the given array into two disjoint subsequences $$$p$$$ and $$$q$$$, each of size $$$n$$$, such that $$$f(p) + f(q)$$$ is maximized. Output the maximum value.

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) element from arbitrary positions.

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 an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).

The second line contains $$$2n$$$ integers $$$a_1,a_2,\ldots,a_{2n}$$$ ($$$1 \le a_i \le 2n$$$) — the elements of sequence $$$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, print one line.

You have to print the maximum value of $$$f(p) + f(q)$$$ that can be achieved.

Example
Input
7
2
1 2 3 4
3
5 5 5 5 5 5
4
3 3 7 6 3 7 8 7
2
2 2 2 2
6
1 2 3 4 5 4 1 4 1 5 4 6
4
1 2 1 2 1 2 1 2
5
9 9 9 7 7 7 9 7 7 7
Output
4
2
4
0
8
4
2
Note

For the first test case:

  • We can divide the array such that $$$p = [1, 3]$$$ and $$$q = [2, 4]$$$.
  • This way, $$$f(p) = 2$$$ and $$$f(q) = 2$$$ since both have two distinct elements with odd frequency.

For the second test case:

  • We can divide the array such that $$$p = [5, 5, 5]$$$ and $$$q = [5, 5, 5]$$$.
  • This way, $$$f(p) = 1$$$ and $$$f(q) = 1$$$.

For the fifth test case:

  • We can divide the array such that $$$p = [1, 2, 3, 4, 5, 6]$$$ and $$$q = [4, 1, 4, 1, 5, 4]$$$.
  • This way, $$$f(p) = 6$$$ and $$$f(q) = 2$$$.

C. Annoying Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two integer arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$, and a total number of turns $$$k$$$.

Alice and Bob play a game by taking turns modifying array $$$a$$$. Alice goes first. The game lasts for exactly $$$k$$$ turns.

On their turn, a player must choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) and perform one of the following operations:

  • Add: Increase $$$a_i$$$ by $$$b_i$$$, i.e., set $$$a_i := a_i + b_i$$$.
  • Subtract: Decrease $$$a_i$$$ by $$$b_i$$$, i.e., set $$$a_i := a_i - b_i$$$.

After the $$$k$$$-th turn is complete, the final score is calculated as the maximum non-empty subarray sum of the modified array $$$a$$$. Alice's goal is to maximize the final score, while Bob's goal is to minimize the final score.

Assuming both players play optimally to achieve their respective goals, determine the final score.

The maximum non-empty subarray sum of $$$a$$$ is defined as $$$\max_{1 \leq i \leq j \leq n} S(i, j)$$$, where $$$S(i, j) = a_i + a_{i+1} + \cdots + a_j$$$. Note that empty subarrays are not considered.

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 two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2\cdot 10^5$$$, $$$1 \le k \le 2\cdot 10^5$$$) — the length of the arrays and the total number of turns.

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_{n}$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of array $$$a$$$.

The third line contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_{n}$$$ ($$$0 \le b_i \le 10^9$$$) — the elements of array $$$b$$$.

It is guaranteed that 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 final score after $$$k$$$ turns, assuming both players play optimally.

Example
Input
5
5 200000
3 -1 9 -5 4
0 0 0 0 0
4 5
10 10 10 10
1 1 1 1
3 1
2 -7 3
1 11 3
3 2
2 -7 3
1 11 3
1 1
-3
2
Output
11
41
9
3
-1
Note

For the first test case:

  • Any move can't change the array $$$a$$$ because all elements in $$$b$$$ are zero.
  • The maximum non-empty subarray sum of this final array is $$$3 + (-1) + 9 = 11$$$.

For the second test case, one of the possible optimal sequences of moves is:

  • Alice adds $$$b_3=1$$$ to $$$a_3$$$. Array $$$a$$$ becomes $$$[10, 10, 11, 10]$$$.
  • Bob subtracts $$$b_1=1$$$ from $$$a_1$$$. Array $$$a$$$ becomes $$$[9, 10, 11, 10]$$$.
  • Alice adds $$$b_3=1$$$ to $$$a_3$$$. Array $$$a$$$ becomes $$$[9, 10, 12, 10]$$$.
  • Bob subtracts $$$b_1=1$$$ from $$$a_1$$$. Array $$$a$$$ becomes $$$[8, 10, 12, 10]$$$.
  • Alice adds $$$b_4=1$$$ to $$$a_4$$$. Array $$$a$$$ becomes $$$[8, 10, 12, 11]$$$.
  • The maximum non-empty subarray sum of this final array is $$$8+10+12+11 = 41$$$.

For the third test case, one of the possible optimal sequences of moves is:

  • Alice adds $$$b_2=11$$$ to $$$a_2$$$. Array $$$a$$$ becomes $$$[2, 4, 3]$$$.
  • The maximum non-empty subarray sum of this final array is $$$2 + 4 + 3 = 9$$$.

For the fourth test case, one of the possible optimal sequences of moves is:

  • Alice adds $$$b_2=11$$$ to $$$a_2$$$. Array $$$a$$$ becomes $$$[2, 4, 3]$$$.
  • Bob subtracts $$$b_2=11$$$ from $$$a_2$$$. Array $$$a$$$ becomes $$$[2, -7, 3]$$$.
  • The maximum non-empty subarray sum of this final array is $$$3$$$.

D. Palindrome Flipping
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two binary strings $$$s$$$ and $$$t$$$, each of the same length $$$n$$$. You are allowed to perform the following operation:

  • Pick indices $$$l$$$, $$$r$$$ ($$$1 \le \color{red}{l \lt r} \le n$$$) such that substring $$$s_{l,r}$$$ is a palindrome and flip all bits in substring $$$s_{l,r}$$$.

The goal is to finally make $$$s$$$ equal to $$$t$$$, performing any of the above operations at most $$$2n$$$ times (possibly none).

A substring $$$s_{l,r}$$$ of a string $$$s$$$ is the contiguous sequence of characters starting from index $$$l$$$ and ending at index $$$r$$$ (both inclusive), where $$$1 \leq l \lt r \leq |s|$$$. Here $$$|s|$$$ denotes the length of the string $$$s$$$.

A string is a palindrome if it reads the same forwards and backwards. For example, the strings 101 and 00 are palindromes, while 10 is not.

Flipping all bits in a substring means changing each 0 to 1 and each 1 to 0 in that substring. For example, flipping the substring 101 results in 010.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 5\cdot 10^3$$$). The description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 100$$$) — the length of the strings $$$s$$$ and $$$t$$$.

The next two lines of each test case contain the binary strings $$$s$$$ and $$$t$$$, respectively.

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

Output

For each test case, if it is impossible to achieve the goal, print $$$-1$$$.

Otherwise, the first line should contain an integer $$$k$$$ ($$$0 \leq k \leq 2n$$$) — the number of operations.

For each of the next $$$k$$$ lines, print two integers $$$l, r$$$ ($$$1 \leq l \lt r \leq n$$$) — the indices you choose in each operation. Note that $$$s_{l,r}$$$ must be a palindrome at this stage.

Example
Input
3
5
01011
10000
7
1010101
0101010
4
0010
0010
Output
2
1 3
3 5
1
1 7
0
Note

For the first test case:

  • Initially, $$$s = \mathtt{01011}$$$ and $$$t = \mathtt{10000}$$$.
  • First, we choose $$$l=1$$$ and $$$r=3$$$. This is a valid operation since $$$s_{1,3} = \mathtt{010}$$$ is a palindrome. After flipping $$$s_{1,3}$$$, $$$s = \mathtt{10111}$$$.
  • Then, we choose $$$l=3$$$ and $$$r=5$$$. This is a valid operation since $$$s_{3,5} = \mathtt{111}$$$ is a palindrome. After flipping $$$s_{3,5}$$$, $$$s = \mathtt{10000}$$$.
  • Now, $$$s$$$ is equal to $$$t$$$.

For the second test case:

  • Initially, $$$s = \mathtt{1010101}$$$ and $$$t = \mathtt{0101010}$$$.
  • First, we choose $$$l=1$$$ and $$$r=7$$$. This is a valid operation since $$$s_{1,7} = \mathtt{1010101}$$$ is a palindrome. After flipping $$$s_{1,7}$$$, $$$s = \mathtt{0101010}$$$.
  • Now, $$$s$$$ is equal to $$$t$$$.

For the third test case:

  • Initially, $$$s$$$ is equal to $$$t$$$. No operations are needed.

E. Sink
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a grid containing $$$n$$$ rows and $$$m$$$ columns. Every cell $$$(i, j)$$$, located in the $$$i$$$-th row and $$$j$$$-th column, has a positive integer value associated with it $$$a_{i, j}$$$. Two cells are adjacent if and only if they share a common side in the grid.

You are allowed to construct holes at cells of your choice. A cell $$$(x, y)$$$ is a sink if and only if it has a hole, or is adjacent to a sink $$$(i, j)$$$ with $$$a_{x, y} \ge a_{i, j}$$$.

The beauty of a grid is defined as the minimum number of holes you need to construct so that every cell becomes a sink.

You have to determine the beauty of this grid.

You are also given $$$q$$$ queries.

In a query, you are given three positive integers $$$r$$$, $$$c$$$, and $$$x$$$. The current value of cell $$$(r, c)$$$ is decreased by $$$x$$$. After each query, determine the beauty of the grid considering no holes have been constructed yet.

Note that queries are cumulative, so the effects of each query carry on to future queries.

It is guaranteed that after every query the value of each cell will remain positive.

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 two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$, $$$1 \le n \cdot m \le 2 \cdot 10^5$$$) — the number of rows and columns, respectively.

The following $$$n$$$ lines contain $$$m$$$ integers each; the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$1 \le a_{i,j} \le 10^9$$$).

The next line contains a single integer $$$q$$$ ($$$0 \le q \le 2\cdot 10^5$$$) — the number of queries.

The following $$$q$$$ lines contain $$$3$$$ integers each — $$$r, c,$$$ and $$$x$$$ ($$$1\le r \le n, 1\le c \le m, 1\le x \lt 10^9$$$).

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

It is guaranteed that after every query the value of each cell will remain positive.

Output

For each test case, print $$$q+1$$$ lines.

On the first line print the beauty of the initial grid.

Also, after each query, print the beauty of the current grid.

Example
Input
3
1 4
1 2 3 5
2
1 4 1
1 3 2
3 3
5 1 6
2 9 3
7 4 8
3
2 2 1
2 2 7
3 3 7
3 4
10 10 10 10
10 10 10 10
10 10 11 10
5
3 3 5
2 2 5
2 4 5
2 3 5
1 1 9
Output
1
1
2
4
4
1
2
1
1
2
3
1
2
Note

For the first test case:

  • Initially, we can create a hole at cell ($$$1,1$$$).
  • After the first query, we can create a hole at cell ($$$1,1$$$).
  • After the second query, we can create holes at cells ($$$1,1$$$) and ($$$1,3$$$).

For the second test case:

  • Initially, we can create holes at cells ($$$1,2$$$), ($$$2,1$$$), ($$$2,3$$$), and ($$$3,2$$$).
  • After the first query, we can create holes at cells ($$$1,2$$$), ($$$2,1$$$), ($$$2,3$$$), and ($$$3,2$$$).
  • After the second query, we can create a hole at cell ($$$2,2$$$).
  • After the third query, we can create holes at cells ($$$2,2$$$) and ($$$3,3$$$).

F1. Distinct GCDs (Easy Version)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is the easy version of the problem. The difference between the versions is that in this version, $$$n \leq 700$$$. You can hack only if you solved all versions of this problem.

Legend has it that when Gauss was a young schoolboy, his teacher tasked the class with summing the integers from $$$1$$$ to $$$100$$$, likely as a way to keep them occupied for a while. However, young Gauss quickly came up with the formula $$$\text{sum} = \frac{n(n+1)}{2}$$$ and found the answer in mere moments. Centuries later, Gauss appears before you in a nightmare with a daunting task...

You are given a positive integer $$$n$$$, find a sequence of integers $$$[a_1, a_2, \ldots, a_n]$$$ such that $$$1 \leq a_i \leq 10^{18}$$$ for all $$$1 \leq i \leq n$$$, and the GCDs of pairwise adjacent elements of $$$a$$$ are all distinct. Formally,

$$$\gcd(a_i, a_{i+1}) \neq \gcd(a_j, a_{j+1})$$$ for each $$$1 \leq i \lt j \lt n$$$

Additionally, $$$a$$$ should have the minimum possible number of distinct elements.

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 and only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 700$$$) — the size of the sequence to be found.

Output

For each test case, output $$$n$$$ space-separated positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^{18}$$$) on a new 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 always exists.

Example
Input
3
2
5
7
Output
2 2
1 4 4 6 6
4 4 6 6 9 9 4
Note

For the second test case, the GCDs of adjacent elements are $$$[\gcd(1, 4), \gcd(4, 4), \gcd(4, 6), \gcd(6, 6)] = [1, 4, 2, 6]$$$, all of which are distinct.

For the third test case, the GCDs of adjacent elements are $$$[\gcd(4, 4), \gcd(4, 6), \gcd(6, 6), \gcd(6, 9), \gcd(9, 9), \gcd(9, 4)] = [4, 2, 6, 3, 9, 1]$$$, all of which are distinct.

For each test case, it can be proven that no sequence with fewer distinct elements exists such that all adjacent GCDs are distinct.

F2. Distinct GCDs (Hard Version)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The difference between the versions is that in this version, $$$n \leq 5000$$$. You can hack only if you solved all versions of this problem.

Legend has it that when Gauss was a young schoolboy, his teacher tasked the class with summing the integers from $$$1$$$ to $$$100$$$, likely as a way to keep them occupied for a while. However, young Gauss quickly came up with the formula $$$\text{sum} = \frac{n(n+1)}{2}$$$ and found the answer in mere moments. Centuries later, Gauss appears before you in a nightmare with a daunting task...

You are given a positive integer $$$n$$$, find a sequence of integers $$$[a_1, a_2, \ldots, a_n]$$$ such that $$$1 \leq a_i \leq 10^{18}$$$ for all $$$1 \leq i \leq n$$$, and the GCDs of pairwise adjacent elements of $$$a$$$ are all distinct. Formally,

$$$\gcd(a_i, a_{i+1}) \neq \gcd(a_j, a_{j+1})$$$ for each $$$1 \leq i \lt j \lt n$$$

Additionally, $$$a$$$ should have the minimum possible number of distinct elements.

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 and only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 5000$$$) — the size of the sequence to be found.

Output

For each test case, output $$$n$$$ space-separated positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^{18}$$$) on a new 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 always exists.

Example
Input
3
2
5
7
Output
2 2
1 4 4 6 6
4 4 6 6 9 9 4
Note

For the second test case, the GCDs of adjacent elements are $$$[\gcd(1, 4), \gcd(4, 4), \gcd(4, 6), \gcd(6, 6)] = [1, 4, 2, 6]$$$, all of which are distinct.

For the third test case, the GCDs of adjacent elements are $$$[\gcd(4, 4), \gcd(4, 6), \gcd(6, 6), \gcd(6, 9), \gcd(9, 9), \gcd(9, 4)] = [4, 2, 6, 3, 9, 1]$$$, all of which are distinct.

For each test case, it can be proven that no sequence with fewer distinct elements exists such that all adjacent GCDs are distinct.