Codeforces Round 887 (Div. 1)
A. Ntarsis' Set
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has been given a set $$$S$$$, initially containing integers $$$1, 2, 3, \ldots, 10^{1000}$$$ in sorted order. Every day, he will remove the $$$a_1$$$-th, $$$a_2$$$-th, $$$\ldots$$$, $$$a_n$$$-th smallest numbers in $$$S$$$ simultaneously.

What is the smallest element in $$$S$$$ after $$$k$$$ days?

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 first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n,k \leq 2 \cdot 10^5$$$) — the length of $$$a$$$ and the number of days.

The following line of each test case consists of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of array $$$a$$$.

It is guaranteed that:

  • The sum of $$$n$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • The sum of $$$k$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • $$$a_1 \lt a_2 \lt \cdots \lt a_n$$$ for all test cases.
Output

For each test case, print an integer that is the smallest element in $$$S$$$ after $$$k$$$ days.

Example
Input
7
5 1
1 2 4 5 6
5 3
1 3 5 6 7
4 1000
2 3 4 5
9 1434
1 4 7 9 12 15 17 18 20
10 4
1 3 5 7 9 11 13 15 17 19
10 6
1 4 7 10 13 16 19 22 25 28
10 150000
1 3 4 5 10 11 12 13 14 15
Output
3
9
1
12874
16
18
1499986
Note

For the first test case, each day the $$$1$$$-st, $$$2$$$-nd, $$$4$$$-th, $$$5$$$-th, and $$$6$$$-th smallest elements need to be removed from $$$S$$$. So after the first day, $$$S$$$ will become $$$\require{cancel}$$$ $$$\{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}$$$. The smallest element is $$$3$$$.

For the second case, each day the $$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, $$$6$$$-th and $$$7$$$-th smallest elements need to be removed from $$$S$$$. $$$S$$$ will be changed as follows:

Day$$$S$$$ before$$$S$$$ after
1$$$\{\cancel 1, 2, \cancel 3, 4, \cancel 5, \cancel 6, \cancel 7, 8, 9, 10, \ldots \}$$$$$$\to$$$$$$\{2, 4, 8, 9, 10, \ldots\}$$$
2$$$\{\cancel 2, 4, \cancel 8, 9, \cancel{10}, \cancel{11}, \cancel{12}, 13, 14, 15, \ldots\}$$$$$$\to$$$$$$\{4, 9, 13, 14, 15, \ldots\}$$$
3$$$\{\cancel 4, 9, \cancel{13}, 14, \cancel{15}, \cancel{16}, \cancel{17}, 18, 19, 20, \ldots\}$$$$$$\to$$$$$$\{9, 14, 18, 19, 20, \ldots\}$$$

The smallest element left after $$$k = 3$$$ days is $$$9$$$.

B. Imbalanced Arrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has come up with an array $$$a$$$ of $$$n$$$ non-negative integers.

Call an array $$$b$$$ of $$$n$$$ integers imbalanced if it satisfies the following:

  • $$$-n\le b_i\le n$$$, $$$b_i \ne 0$$$,
  • there are no two indices $$$(i, j)$$$ ($$$1 \le i, j \le n$$$) such that $$$b_i + b_j = 0$$$,
  • for each $$$1 \leq i \leq n$$$, there are exactly $$$a_i$$$ indices $$$j$$$ ($$$1 \le j \le n$$$) such that $$$b_i+b_j \gt 0$$$, where $$$i$$$ and $$$j$$$ are not necessarily distinct.

Given the array $$$a$$$, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.

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 first line of each test case has a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$).

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

Output

For each test case, output "NO" if there exists no imbalanced array.

Otherwise, output "YES". Then, on the next line, output $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ where $$$b_i \neq 0$$$ for all $$$1 \leq i \leq n$$$ — an imbalanced array.

Example
Input
5
1
1
4
1 4 3 4
3
0 1 0
4
4 3 2 1
3
1 3 1
Output
YES
1 
NO
YES
-3 1 -2 
YES
4 2 -1 -3 
YES
-1 3 -1
Note

For the first test case, $$$b = [1]$$$ is an imbalanced array. This is because for $$$i = 1$$$, there is exactly one $$$j$$$ ($$$j = 1$$$) where $$$b_1 + b_j \gt 0$$$.

For the second test case, it can be shown that there exists no imbalanced array.

For the third test case, $$$a = [0, 1, 0]$$$. The array $$$b = [-3, 1, -2]$$$ is an imbalanced array.

  • For $$$i = 1$$$ and $$$i = 3$$$, there exists no index $$$j$$$ such that $$$b_i + b_j \gt 0$$$.
  • For $$$i = 2$$$, there is only one index $$$j = 2$$$ such that $$$b_i + b_j \gt 0$$$ ($$$b_2 + b_2 = 1 + 1 = 2$$$).
Another possible output for the third test case could be $$$b = [-2, 1, -3]$$$.

C. Ina of the Mountain
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To prepare her "Takodachi" dumbo octopuses for world domination, Ninomae Ina'nis, a.k.a. Ina of the Mountain, orders Hoshimachi Suisei to throw boulders at them. Ina asks you, Kiryu Coco, to help choose where the boulders are thrown.

There are $$$n$$$ octopuses on a single-file trail on Ina's mountain, numbered $$$1, 2, \ldots, n$$$. The $$$i$$$-th octopus has a certain initial health value $$$a_i$$$, where $$$1 \leq a_i \leq k$$$.

Each boulder crushes consecutive octopuses with indexes $$$l, l+1, \ldots, r$$$, where $$$1 \leq l \leq r \leq n$$$. You can choose the numbers $$$l$$$ and $$$r$$$ arbitrarily for each boulder.

For each boulder, the health value of each octopus the boulder crushes is reduced by $$$1$$$. However, as octopuses are immortal, once they reach a health value of $$$0$$$, they will immediately regenerate to a health value of $$$k$$$.

Given the octopuses' initial health values, find the minimum number of boulders that need to be thrown to make the health of all octopuses equal to $$$k$$$.

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 first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) – the number of octopuses, and the upper bound of a octopus's health value.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le k$$$) – the initial health values of the octopuses.

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 boulders that need to be thrown to make the health values of all octopuses equal to $$$k$$$.

Example
Input
2
4 3
1 2 1 3
7 3
1 2 3 1 3 2 1
Output
2
4
Note

In the first test case, the minimum number of boulders thrown is $$$2$$$:

  • Throw the first boulder between $$$[l,r] = [1,3]$$$. Then, the octopuses' health values become $$$[3, 1, 3, 3]$$$.
  • Throw the second boulder between $$$[l,r] = [2,2]$$$. Then, the octopuses' health values become $$$[3, 3, 3, 3]$$$.

In the second test case, the minimum number of boulders thrown is $$$4$$$. The $$$[l,r]$$$ ranges are $$$[1,7], [2, 6], [3, 5], [4, 4]$$$.

D. Miriany and Matchstick
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Miriany's matchstick is a $$$2 \times n$$$ grid that needs to be filled with characters A or B.

He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters$$$^\dagger$$$ is equal to $$$k$$$. If it is impossible, report so.

$$$^\dagger$$$ An adjacent pair of cells with different characters is a pair of cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ ($$$1 \le r_1, r_2 \le 2$$$, $$$1 \le c_1, c_2 \le n$$$) such that $$$|r_1 - r_2| + |c_1 - c_2| = 1$$$ and the characters in $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are different.

Input

The first line consists of an integer $$$t$$$, the number of test cases ($$$1 \leq t \leq 1000$$$). The description of the test cases follows.

The first line of each test case has two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 3 \cdot n$$$) – the number of columns of the matchstick, and the number of adjacent pairs of cells with different characters required.

The following line contains string $$$s$$$ of $$$n$$$ characters ($$$s_i$$$ is either A or B) – Miriany's top row of the matchstick.

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 way to fill the second row with the number of adjacent pairs of cells with different characters equals $$$k$$$, output "NO".

Otherwise, output "YES". Then, print $$$n$$$ characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any of them.

Example
Input
4
10 1
ABBAAABBAA
4 5
AAAA
9 17
BAAABBAAB
4 9
ABAB
Output
NO
YES
BABB
YES
ABABAABAB
NO
Note

In the first test case, it can be proved that there exists no possible way to fill in row $$$2$$$ of the grid such that $$$k = 1$$$.

For the second test case, BABB is one possible answer.

The grid below is the result of filling in BABB as the second row.

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & A & B & B \cr \hline \end{array}$$$

The pairs of different characters are shown below in red:

$$$\begin{array}{|c|c|} \hline \color{red}{A} & A & A & A \cr \hline \color{red}{B} & A & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & \color{red}{A} & A \cr \hline B & A & \color{red}{B} & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & \color{red}{A} \cr \hline B & A & B & \color{red}{B} \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline \color{red}{B} & \color{red}{A} & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & \color{red}{A} & \color{red}{B} & B \cr \hline \end{array}$$$

There are a total of $$$5$$$ pairs, which satisfies $$$k$$$.

E. Rivalries
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Ntarsis has an array $$$a$$$ of length $$$n$$$.

The power of a subarray $$$a_l \dots a_r$$$ ($$$1 \leq l \leq r \leq n$$$) is defined as:

  • The largest value $$$x$$$ such that $$$a_l \dots a_r$$$ contains $$$x$$$ and neither $$$a_1 \dots a_{l-1}$$$ nor $$$a_{r+1} \dots a_n$$$ contains $$$x$$$.
  • If no such $$$x$$$ exists, the power is $$$0$$$.

Call an array $$$b$$$ a rival to $$$a$$$ if the following holds:

  • The length of both $$$a$$$ and $$$b$$$ are equal to some $$$n$$$.
  • Over all $$$l, r$$$ where $$$1 \leq l \leq r \leq n$$$, the power of $$$a_l \dots a_r$$$ equals the power of $$$b_l \dots b_r$$$.
  • The elements of $$$b$$$ are positive.

Ntarsis wants you to find a rival $$$b$$$ to $$$a$$$ such that the sum of $$$b_i$$$ over $$$1 \leq i \leq n$$$ is maximized. Help him with this task!

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 first line of each test case has a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$).

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 $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ — a valid rival to $$$a$$$ such that $$$b_1 + b_2 + \cdots + b_n$$$ is maximal.

If there exist multiple rivals with the maximum sum, output any of them.

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

For the first test case, one rival with the maximal sum is $$$[2, 4, 2, 3, 3]$$$.

$$$[2, 4, 2, 3, 3]$$$ can be shown to be a rival to $$$[1, 4, 1, 3, 3]$$$.

All possible subarrays of $$$a$$$ and $$$b$$$ and their corresponding powers are listed below:

  • The power of $$$a[1:1] = [1] = 0$$$, the power of $$$b[1:1] = [2] = 0$$$.
  • The power of $$$a[1:2] = [1, 4] = 4$$$, the power of $$$b[1:2] = [2, 4] = 4$$$.
  • The power of $$$a[1:3] = [1, 4, 1] = 4$$$, the power of $$$b[1:3] = [2, 4, 2] = 4$$$.
  • The power of $$$a[1:4] = [1, 4, 1, 3] = 4$$$, the power of $$$b[1:4] = [2, 4, 2, 3] = 4$$$.
  • The power of $$$a[1:5] = [1, 4, 1, 3, 3] = 4$$$, the power of $$$b[1:5] = [2, 4, 2, 3, 3] = 4$$$.
  • The power of $$$a[2:2] = [4] = 4$$$, the power of $$$b[2:2] = [4] = 4$$$.
  • The power of $$$a[2:3] = [4, 1] = 4$$$, the power of $$$b[2:3] = [4, 2] = 4$$$.
  • The power of $$$a[2:4] = [4, 1, 3] = 4$$$, the power of $$$b[2:4] = [4, 2, 3] = 4$$$.
  • The power of $$$a[2:5] = [4, 1, 3, 3] = 4$$$, the power of $$$b[2:5] = [4, 2, 3, 3] = 4$$$.
  • The power of $$$a[3:3] = [1] = 0$$$, the power of $$$b[3:3] = [2] = 0$$$.
  • The power of $$$a[3:4] = [1, 3] = 0$$$, the power of $$$b[3:4] = [2, 3] = 0$$$.
  • The power of $$$a[3:5] = [1, 3, 3] = 3$$$, the power of $$$b[3:5] = [2, 3, 3] = 3$$$.
  • The power of $$$a[4:4] = [3] = 0$$$, the power of $$$b[4:4] = [3] = 0$$$.
  • The power of $$$a[4:5] = [3, 3] = 3$$$, the power of $$$b[4:5] = [3, 3] = 3$$$.
  • The power of $$$a[5:5] = [3] = 0$$$, the power of $$$b[5:5] = [3] = 0$$$.

It can be shown there exists no rival with a greater sum than $$$2 + 4 + 2 + 3 + 3 = 14$$$.

F. Panda Meetups
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The red pandas are in town to meet their relatives, the blue pandas! The town is modeled by a number line.

The pandas have already planned their meetup, but the schedule keeps changing. You are given $$$q$$$ updates of the form x t c.

  • If $$$c \lt 0$$$, it means $$$|c|$$$ more red pandas enter the number line at position $$$x$$$ and time $$$t$$$. Then, each unit of time, they can each independently move one unit in either direction across the number line, or not move at all.
  • If $$$c \gt 0$$$, it means that $$$c$$$ more blue pandas check position $$$x$$$ for red pandas at time $$$t$$$. If a blue panda does not meet a red panda at that specific location and time, they dejectedly leave the number line right away. If there is a red panda at a position at the same time a blue panda checks it, they form a friendship and leave the number line. Each red panda can form a friendship with at most one blue panda and vice versa.

The updates will be given in order of non-decreasing $$$x$$$ values. After each update, please print the maximum number of friendships if the red pandas move in an optimal order based on all the updates given in the input above (and including) this update.

The order in which a red panda moves can change between updates.

Input

The first line contains $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$)  – the number of updates.

The $$$i$$$-th line of the next $$$q$$$ lines consists of $$$3$$$ integers $$$x_i$$$, $$$t_i$$$ and $$$c_i$$$ ($$$0 \leq x_i \leq 10^9$$$, $$$0 \leq t_i \leq 10^9$$$, $$$0 \lt |c_i| \leq 1000$$$) – the description of the $$$i$$$-th update.

It is guaranteed that the $$$x_i$$$ will be given in non-decreasing order.

Output

After each update, print the maximum number of friendships that can be formed.

Examples
Input
5
0 6 3
4 2 -5
7 4 -6
10 5 100
10 8 7
Output
0
3
3
3
10
Input
5
0 6 3
4 2 -5
7 4 -6
10 5 100
11 8 7
Output
0
3
3
3
9
Input
7
0 8 6
2 7 -2
3 1 -6
5 3 -8
7 3 -3
8 0 -2
8 2 1
Output
0
0
6
6
6
6
7
Input
4
0 0 -3
0 0 2
0 0 4
0 0 -10
Output
0
2
3
6
Note

In the first example, the number of friendships after each update can be optimized as follows:

  1. $$$3$$$ blue pandas now check for red pandas at position $$$0$$$ at time $$$6$$$. There are no red pandas anywhere, so there are no friendships.
  2. $$$5$$$ red pandas now appear at position $$$4$$$ and time $$$2$$$. $$$4$$$ of the red pandas can travel to position $$$0$$$ at time $$$6$$$, where $$$3$$$ of them can make friendships with the $$$3$$$ existing blue pandas.
  3. $$$6$$$ red pandas now appear at position $$$7$$$ and time $$$4$$$. No new blue pandas are added, so the maximum number of friendships is still $$$3$$$.
  4. $$$100$$$ blue pandas now appear at position $$$10$$$ and time $$$5$$$. No red pandas can reach them at a time of $$$5$$$, so no new friendships are created.
  5. $$$7$$$ blue pandas now appear at position $$$10$$$ and time $$$8$$$. $$$6$$$ of the red pandas at position $$$7$$$ and time $$$4$$$, along with $$$1$$$ red panda at position $$$4$$$ and time $$$2$$$, can reach $$$7$$$ of the blue pandas at position $$$10$$$ at time $$$8$$$, adding $$$7$$$ new friendships. This brings the total to $$$10$$$ friendships.