Codeforces Round 890 (Div. 2) supported by Constructor Institute
A. Tales of a Sort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alphen has an array of positive integers $$$a$$$ of length $$$n$$$.

Alphen can perform the following operation:

  • For all $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_i$$$ with $$$\max(0, a_i - 1)$$$.

Alphen will perform the above operation until $$$a$$$ is sorted, that is $$$a$$$ satisfies $$$a_1 \leq a_2 \leq \ldots \leq a_n$$$. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.

Input

Each test contains multiple test cases. The first line of input 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$$$ ($$$2 \le n \le 50$$$) — the length of the array $$$a$$$.

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

Output

For each test case, output a single integer — the number of operations that Alphen will perform.

Example
Input
7
3
1 2 3
5
2 1 2 1 2
4
3 1 5 4
2
7 7
5
4 1 3 2 5
5
2 3 1 4 5
3
1000000000 1 2
Output
0
2
5
0
4
3
1000000000
Note

In the first test case, we have $$$a=[1,2,3]$$$. Since $$$a$$$ is already sorted, Alphen will not need to perform any operations. So, the answer is $$$0$$$.

In the second test case, we have $$$a=[2,1,2,1,2]$$$. Since $$$a$$$ is not initially sorted, Alphen will perform one operation to make $$$a=[1,0,1,0,1]$$$. After performing one operation, $$$a$$$ is still not sorted, so Alphen will perform another operation to make $$$a=[0,0,0,0,0]$$$. Since $$$a$$$ is sorted, Alphen will not perform any other operations. Since Alphen has performed two operations in total, the answer is $$$2$$$.

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

You are given an array of positive integers $$$a$$$ of length $$$n$$$.

Let's call an array of positive integers $$$b$$$ of length $$$n$$$ good if:

  1. $$$a_i \neq b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$,
  2. $$$a_1 + a_2 +\ldots + a_n = b_1 + b_2 + \ldots + b_n$$$.

Does a good array exist?

Input

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

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

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, 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 $$$10^5$$$.

Output

For each test case, output "YES" (without quotes) if there exists a good array, 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
6
3
6 1 2
2
1 1
4
3 1 2 4
1
17
5
1 2 1 1 1
3
618343152 819343431 1000000000
Output
YES
NO
YES
NO
NO
YES
Note

In the first test case, a possible good array is $$$[3, 3, 3]$$$. Some examples of not good arrays are:

  • $$$[8, 0, 1]$$$ — the array does not consist of only positive integers,
  • $$$[5, 2, 4]$$$ — the array does not have the same sum as the given array,
  • $$$[5, 2, 2]$$$ — the third element is equal to the third element of the given array.

In the second test case, $$$[1, 1]$$$ is the only array of positive integers of length $$$2$$$ that has the sum of it's elements equal to $$$2$$$. Since $$$[1, 1]$$$ is not a good array, the answer is "NO".

C. To Become Max
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of integers $$$a$$$ of length $$$n$$$.

In one operation you:

  • Choose an index $$$i$$$ such that $$$1 \le i \le n - 1$$$ and $$$a_i \le a_{i + 1}$$$.
  • Increase $$$a_i$$$ by $$$1$$$.

Find the maximum possible value of $$$\max(a_1, a_2, \ldots a_n)$$$ that you can get after performing this operation at most $$$k$$$ times.

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$$$ ($$$2 \le n \le 1000$$$, $$$1 \le k \le 10^{8}$$$) — the length of the array $$$a$$$ and the maximum number of operations that can be performed.

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^{8}$$$) — 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 a single integer — the maximum possible maximum of the array after performing at most $$$k$$$ operations.

Example
Input
6
3 4
1 3 3
5 6
1 3 4 5 1
4 13
1 1 3 179
5 3
4 3 2 2 2
5 6
6 5 4 1 5
2 17
3 5
Output
4
7
179
5
7
6
Note

In the first test case, one possible optimal sequence of operations is: $$$[\color{red}{1}, 3, 3] \rightarrow [2, \color{red}{3}, 3] \rightarrow [\color{red}{2}, 4, 3] \rightarrow [\color{red}{3}, 4, 3] \rightarrow [4, 4, 3]$$$.

In the second test case, one possible optimal sequence of operations is: $$$[1, \color{red}{3}, 4, 5, 1] \rightarrow [1, \color{red}{4}, 4, 5, 1] \rightarrow [1, 5, \color{red}{4}, 5, 1] \rightarrow [1, 5, \color{red}{5}, 5, 1] \rightarrow [1, \color{red}{5}, 6, 5, 1] \rightarrow [1, \color{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1]$$$.

D. More Wrong
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

The jury has hidden a permutation$$$^\dagger$$$ $$$p$$$ of length $$$n$$$.

In one query, you can pick two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \lt r \le n$$$) by paying $$$(r - l)^2$$$ coins. In return, you will be given the number of inversions$$$^\ddagger$$$ in the subarray $$$[p_l, p_{l + 1}, \ldots p_r]$$$.

Find the index of the maximum element in $$$p$$$ by spending at most $$$5 \cdot n^2$$$ coins.

Note: the grader is not adaptive: the permutation is fixed before any queries are made.

$$$^\dagger$$$ 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).

$$$^\ddagger$$$ The number of inversions in an array is the number of pairs of indices $$$(i,j)$$$ such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$. For example, the array $$$[10,2,6,3]$$$ contains $$$4$$$ inversions. The inversions are $$$(1,2),(1,3),(1,4)$$$, and $$$(3,4)$$$.

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 only line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$) — the length of the hidden permutation $$$p$$$.

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

Interaction

The interaction for each test case begins by reading the integer $$$n$$$.

To make a query, output "? l r" ($$$1 \le l \lt r \le n$$$) without quotes. Afterwards, you should read one single integer — the answer for your query.

If you receive the integer $$$-1$$$ instead of an answer or a valid value of $$$n$$$, it means your program has made an invalid query, has exceed the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you are ready to give the final answer, output "! i" ($$$1 \le i \le n$$$) without quotes — the index of the maximum of the hidden permutation. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.

After printing a query do not forget to output end of line and flush the output. 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.

Hacks

To hack, use the following format:

The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$) — the length of the hidden permutation $$$p$$$.

The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots, p_n$$$ ($$$1 \le p_i \le n$$$), $$$p$$$ must be a permutation.

The sum of $$$n$$$ over all test cases should not exceed $$$2000$$$.

Example
Input
2
4

1

0

2

1
Output

? 1 3

? 3 4

! 4

? 1 2

! 1
Note

In the first test, the interaction proceeds as follows:

SolutionJuryExplanation
2There are $$$2$$$ test cases.
4In the first test case, the hidden permutation is $$$[1,3,2,4]$$$, with length $$$4$$$.
? 1 3 1The solution requests the number of inversions in the subarray $$$[1,3,2]$$$ by paying $$$4$$$ coins, and the jury responds with $$$1$$$.
? 3 4 0The solution requests the number of inversions in the subarray $$$[2,4]$$$ by paying $$$1$$$ coin, and the jury responds with $$$0$$$.
! 4 The solution has somehow determined that $$$p_4 = 4$$$, and outputs it. Since the output is correct, the jury continues to the next test case.
2In the second test case, the hidden permutation is $$$[2,1]$$$, with length $$$2$$$.
? 1 2 1The solution requests the number of inversions in the subarray $$$[2,1]$$$ by paying $$$1$$$ coin, and the jury responds with $$$1$$$.
! 1 The solution has somehow determined that $$$p_1 = 2$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit.

Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.

E1. PermuTree (easy version)
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

This is the easy version of the problem. The differences between the two versions are the constraint on $$$n$$$ and the time limit. You can make hacks only if both versions of the problem are solved.

You are given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$.

For some permutation$$$^\dagger$$$ $$$a$$$ of length $$$n$$$, let $$$f(a)$$$ be the number of pairs of vertices $$$(u, v)$$$ such that $$$a_u \lt a_{\operatorname{lca}(u, v)} \lt a_v$$$. Here, $$$\operatorname{lca}(u,v)$$$ denotes the lowest common ancestor of vertices $$$u$$$ and $$$v$$$.

Find the maximum possible value of $$$f(a)$$$ over all permutations $$$a$$$ of length $$$n$$$.

$$$^\dagger$$$ 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 a single integer $$$n$$$ ($$$2 \le n \le 5000$$$).

The second line contains $$$n - 1$$$ integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1 \le p_i \lt i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$.

Output

Output the maximum value of $$$f(a)$$$.

Examples
Input
5
1 1 3 3
Output
4
Input
2
1
Output
0
Input
6
1 2 2 1 5
Output
7
Input
4
1 1 1
Output
2
Note

The tree in the first test:

One possible optimal permutation $$$a$$$ is $$$[2, 1, 4, 5, 3]$$$ with $$$4$$$ suitable pairs of vertices:

  • $$$(2, 3)$$$, since $$$\operatorname{lca}(2, 3) = 1$$$ and $$$1 \lt 2 \lt 4$$$,
  • $$$(2, 4)$$$, since $$$\operatorname{lca}(2, 4) = 1$$$ and $$$1 \lt 2 \lt 5$$$,
  • $$$(2, 5)$$$, since $$$\operatorname{lca}(2, 5) = 1$$$ and $$$1 \lt 2 \lt 3$$$,
  • $$$(5, 4)$$$, since $$$\operatorname{lca}(5, 4) = 3$$$ and $$$3 \lt 4 \lt 5$$$.

The tree in the third test:

The tree in the fourth test:

E2. PermuTree (hard version)
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The differences between the two versions are the constraint on $$$n$$$ and the time limit. You can make hacks only if both versions of the problem are solved.

You are given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$.

For some permutation$$$^\dagger$$$ $$$a$$$ of length $$$n$$$, let $$$f(a)$$$ be the number of pairs of vertices $$$(u, v)$$$ such that $$$a_u \lt a_{\operatorname{lca}(u, v)} \lt a_v$$$. Here, $$$\operatorname{lca}(u,v)$$$ denotes the lowest common ancestor of vertices $$$u$$$ and $$$v$$$.

Find the maximum possible value of $$$f(a)$$$ over all permutations $$$a$$$ of length $$$n$$$.

$$$^\dagger$$$ 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 a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$).

The second line contains $$$n - 1$$$ integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1 \le p_i \lt i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$.

Output

Output the maximum value of $$$f(a)$$$.

Examples
Input
5
1 1 3 3
Output
4
Input
2
1
Output
0
Input
6
1 2 2 1 5
Output
7
Input
4
1 1 1
Output
2
Note

The tree in the first test:

One possible optimal permutation $$$a$$$ is $$$[2, 1, 4, 5, 3]$$$ with $$$4$$$ suitable pairs of vertices:

  • $$$(2, 3)$$$, since $$$\operatorname{lca}(2, 3) = 1$$$ and $$$1 \lt 2 \lt 4$$$,
  • $$$(2, 4)$$$, since $$$\operatorname{lca}(2, 4) = 1$$$ and $$$1 \lt 2 \lt 5$$$,
  • $$$(2, 5)$$$, since $$$\operatorname{lca}(2, 5) = 1$$$ and $$$1 \lt 2 \lt 3$$$,
  • $$$(5, 4)$$$, since $$$\operatorname{lca}(5, 4) = 3$$$ and $$$3 \lt 4 \lt 5$$$.

The tree in the third test:

The tree in the fourth test: