Codeforces Round 813 (Div. 2)
A. Wonderful Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
God's Blessing on This PermutationForces!
A Random Pebble

You are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$.

In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i \lt j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.

Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.

A permutation 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 100$$$). Description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).

The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.

Output

For each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.

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

In the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.

In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.

B. Woeful Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
I wonder, does the falling rain
Forever yearn for it's disdain?
Effluvium of the Mind

You are given a positive integer $$$n$$$.

Find any permutation $$$p$$$ of length $$$n$$$ such that the sum $$$\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)$$$ is as large as possible.

Here $$$\operatorname{lcm}(x, y)$$$ denotes the least common multiple (LCM) of integers $$$x$$$ and $$$y$$$.

A permutation 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 1\,000$$$). Description of the test cases follows.

The only line for each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).

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

Output

For each test case print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_n$$$ — the permutation with the maximum possible value of $$$\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)$$$.

If there are multiple answers, print any of them.

Example
Input
2
1
2
Output
1 
2 1 
Note

For $$$n = 1$$$, there is only one permutation, so the answer is $$$[1]$$$.

For $$$n = 2$$$, there are two permutations:

  • $$$[1, 2]$$$ — the sum is $$$\operatorname{lcm}(1,1) + \operatorname{lcm}(2, 2) = 1 + 2 = 3$$$.
  • $$$[2, 1]$$$ — the sum is $$$\operatorname{lcm}(1,2) + \operatorname{lcm}(2, 1) = 2 + 2 = 4$$$.

C. Sort Zero
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
An array is sorted if it has no inversions
A Young Boy

You are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$.

In one operation you do the following:

  1. Choose any integer $$$x$$$.
  2. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$).

Find the minimum number of operations required to sort the array in non-decreasing order.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). 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 second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).

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

Output

For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.

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

In the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.

In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.

D. Empty Graph
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
 — Do you have a wish?
 — I want people to stop gifting each other arrays.
O_o and Another Young Boy

An array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.

You can apply the following operation at most $$$k$$$ times:

  • Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$).

Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l \lt r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.

You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.

The diameter of a graph is equal to $$$\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).

The second line of each test case contains $$$n$$$ positive 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 $$$10^5$$$.

Output

For each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.

Example
Input
6
3 1
2 4 1
3 2
1 9 84
3 1
10 2 6
3 2
179 17 1000000000
2 1
5 9
2 2
4 2
Output
4
168
10
1000000000
9
1000000000
Note

In the first test case, one of the optimal arrays is $$$[2,4,5]$$$.

The graph built on this array:

$$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.

E1. LCM Sum (easy version)
time limit per test
3.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
We are sum for we are many
Some Number

This version of the problem differs from the next one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved.

You are given two positive integers $$$l$$$ and $$$r$$$.

Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l \le i \lt j \lt k \le r$$$ and $$$\operatorname{lcm}(i,j,k) \ge i + j + k$$$.

Here $$$\operatorname{lcm}(i, j, k)$$$ denotes the least common multiple (LCM) of integers $$$i$$$, $$$j$$$, and $$$k$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$\bf{1 \le t \le 5}$$$). Description of the test cases follows.

The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$, $$$l + 2 \le r$$$).

Output

For each test case print one integer — the number of suitable triplets.

Example
Input
5
1 4
3 5
8 86
68 86
6 86868
Output
3
1
78975
969
109229059713337
Note

In the first test case, there are $$$3$$$ suitable triplets:

  • $$$(1,2,3)$$$,
  • $$$(1,3,4)$$$,
  • $$$(2,3,4)$$$.

In the second test case, there is $$$1$$$ suitable triplet:

  • $$$(3,4,5)$$$.

E2. LCM Sum (hard version)
time limit per test
3.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
We are sum for we are many
Some Number

This version of the problem differs from the previous one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved.

You are given two positive integers $$$l$$$ and $$$r$$$.

Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l \le i \lt j \lt k \le r$$$ and $$$\operatorname{lcm}(i,j,k) \ge i + j + k$$$.

Here $$$\operatorname{lcm}(i, j, k)$$$ denotes the least common multiple (LCM) of integers $$$i$$$, $$$j$$$, and $$$k$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$\bf{1 \le t \le 10^5}$$$). Description of the test cases follows.

The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$, $$$l + 2 \le r$$$).

Output

For each test case print one integer — the number of suitable triplets.

Example
Input
5
1 4
3 5
8 86
68 86
6 86868
Output
3
1
78975
969
109229059713337
Note

In the first test case, there are $$$3$$$ suitable triplets:

  • $$$(1,2,3)$$$,
  • $$$(1,3,4)$$$,
  • $$$(2,3,4)$$$.

In the second test case, there is $$$1$$$ suitable triplet:

  • $$$(3,4,5)$$$.

F. Triameter
time limit per test
4.5 seconds
memory limit per test
768 megabytes
input
standard input
output
standard output
 — What is my mission?
 — To count graph diameters.
You and Your Submission

A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.

You are given a weighted tree with $$$n$$$ vertices, each edge has a weight of $$$1$$$. Let $$$L$$$ be the set of vertices with degree equal to $$$1$$$.

You have to answer $$$q$$$ independent queries. In the $$$i$$$-th query:

  1. You are given a positive integer $$$x_i$$$.
  2. For all $$$u,v \in L$$$ such that $$$u \lt v$$$, add edge $$$(u, v)$$$ with weight $$$x_i$$$ to the graph (initially the given tree).
  3. Find the diameter of the resulting graph.

The diameter of a graph is equal to $$$\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.

Input

The first line contains a single integer $$$n$$$ ($$$3 \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$$$. It is guaranteed that the given edges form a tree.

The third line contains a single integer $$$q$$$ ($$$1 \le q \le 10$$$).

The fourth line contains $$$q$$$ integers $$$x_1,x_2,\ldots,x_q$$$ ($$$1 \le x_i \le n$$$). All $$$x_i$$$ are distinct.

Output

Print $$$q$$$ integers in a single line — the answers to the queries.

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

The graph in the first test after adding the edges: