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?
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:
For each test case, print an integer that is the smallest element in $$$S$$$ after $$$k$$$ days.
75 11 2 4 5 65 31 3 5 6 74 10002 3 4 59 14341 4 7 9 12 15 17 18 2010 41 3 5 7 9 11 13 15 17 1910 61 4 7 10 13 16 19 22 25 2810 1500001 3 4 5 10 11 12 13 14 15
3 9 1 12874 16 18 1499986
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$$$.
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:
Given the array $$$a$$$, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.
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$$$.
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.
51141 4 3 430 1 044 3 2 131 3 1
YES 1 NO YES -3 1 -2 YES 4 2 -1 -3 YES -1 3 -1
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.
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$$$.
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$$$.
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$$$.
24 31 2 1 37 31 2 3 1 3 2 1
2 4
In the first test case, the minimum number of boulders thrown is $$$2$$$:
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]$$$.
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.
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$$$.
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.
410 1ABBAAABBAA4 5AAAA9 17BAAABBAAB4 9ABAB
NO YES BABB YES ABABAABAB NO
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.
The pairs of different characters are shown below in red:
—————————————————
$$$\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$$$.
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:
Call an array $$$b$$$ a rival to $$$a$$$ if the following holds:
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!
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$$$.
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.
751 4 1 3 351 4 1 8 852 1 1 1 283 2 3 5 2 2 5 381 1 1 1 4 3 3 3101 9 5 9 8 1 5 8 9 1161 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7
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
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:
It can be shown there exists no rival with a greater sum than $$$2 + 4 + 2 + 3 + 3 = 14$$$.
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.
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.
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.
After each update, print the maximum number of friendships that can be formed.
5 0 6 3 4 2 -5 7 4 -6 10 5 100 10 8 7
0 3 3 3 10
5 0 6 3 4 2 -5 7 4 -6 10 5 100 11 8 7
0 3 3 3 9
7 0 8 6 2 7 -2 3 1 -6 5 3 -8 7 3 -3 8 0 -2 8 2 1
0 0 6 6 6 6 7
4 0 0 -3 0 0 2 0 0 4 0 0 -10
0 2 3 6
In the first example, the number of friendships after each update can be optimized as follows: