The 2024 Damascus University Collegiate Programming Contest (DCPC 2024)
A. Prefix GCD
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given an array $$$a$$$ of length $$$n$$$, perform the following operation exactly once:

— Delete an element from the array, then concatenate the resulting parts (i.e., if you remove the $$$i$$$-th element, the array becomes $$$a_1, a_2, \dots, a_{i - 1}, a_{i + 1}, \dots, a_n$$$).

Let $$$b_1, b_2, \dots, b_{n - 1}$$$ be the resulting array after performing the above operation exactly once on the array $$$a$$$.

Find the maximum possible sum of the values of GCD of each prefix in the array $$$b$$$.

More formally, find the maximum value of: $$$$$$\sum_{i = 1}^{n - 1} gcd(b_1, b_2, \dots, b_i)$$$$$$

Over all the arrays $$$b$$$ resulting from applying the mentioned operation on the array $$$a$$$ exactly once.

Note: $$$gcd(x_1, x_2, \dots, x_k)$$$ is the greatest integer that divides all the values $$$x_1, x_2, \dots, x_k$$$ evenly without a remainder.

Input

The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the size of the array.

The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

Output

Output a single integer — the answer to the problem.

Examples
Input
4
4 3 2 1
Output
7
Input
3
18 3 9
Output
27
Input
3
100 100 100
Output
200

B. Tree Tour
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given a tree consisting of $$$n$$$ nodes.

Is there a walk that visits all the nodes of the tree at least once and at most twice?

Note that the node you start the walk at is considered visited at the beginning.

A walk is a sequence of nodes $$$v_1, v_2, \dots, v_k$$$ such that for each $$$i$$$ ($$$1 \le i \lt k$$$), $$$v_i$$$ and $$$v_{i + 1}$$$ are connected by an edge.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of testcases.

The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.

Each of the following $$$n - 1$$$ lines of each testcase consists of two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) denoting an edge between nodes $$$u$$$ and $$$v$$$ in the tree.

It is guaranteed that the given edges form a tree.

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

Output

For each testcase, print "YES" (without quotes) if there exists a walk that satisfies the conditions, otherwise print "NO" (without quotes).

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

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

Explanation of the first testcase: we can start at node $$$1$$$ then go to node $$$2$$$ and stop there.

Explanation of the second testcase: a valid walk may look like this: $$$5$$$ — $$$3$$$ — $$$4$$$ — $$$3$$$ — $$$1$$$ — $$$2$$$ — $$$1$$$.

Notice that it is not necessary to start at node $$$1$$$.

Nodes $$$1$$$ and $$$3$$$ were visited twice, whereas nodes $$$2$$$, $$$4$$$, and $$$5$$$ were visited once. Thus, each node is visited at least once and at most twice and the walk is valid.

Explanation of the third testcase: it can be shown that there does not exist a walk that satisfies the conditions.

C. Powerful String
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Define the power of a string $$$t$$$ of length $$$m$$$ as follows:

$$$$$$\prod_{i = 1}^{m} i^{f_t(t_i)}$$$$$$

Where:

— $$$t_i$$$ is the character at position $$$i$$$ of the string $$$t$$$.

— $$$f_t(c)$$$ is the number of occurrences of the character $$$c$$$ in the string $$$t$$$.

For example, if $$$t = $$$ "abca", then $$$f($$$'a'$$$) = 2$$$, $$$f($$$'b'$$$) = 1$$$, and $$$f($$$'c'$$$) = 1$$$, and the power of $$$t$$$ is equal to $$$1^2 \times 2^1 \times 3^1 \times 4^2 = 96$$$.

Given a string $$$s$$$ of length $$$n$$$, and $$$q$$$ queries of the following form:

— "$$$l, r$$$": determine whether it is possible to swap two characters in $$$t[l \dots r]$$$ such that the power of $$$t[l \dots r]$$$ increases.

Input

The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.

The second line of the input contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.

The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.

Each of the following $$$q$$$ lines consists of two space-separated integers $$$l$$$ and $$$r$$$ ($$$1 \le l \lt r \le n$$$).

Output

Output $$$q$$$ lines, the $$$i$$$-th of which contains the answer of the $$$i$$$-th query as follows:

Print "YES" (without quotes) if it is possible to increase the power in the $$$i$$$-th query, and print "NO"(without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Examples
Input
5
ababa
2
1 2
1 3
Output
NO
YES
Input
2
aa
1
1 2
Output
NO

D. You Have Been Grid Squared
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

A grid of size $$$n \times n$$$ is constructed in the following way:

The first row of the grid consists of the numbers $$$1, 2, 3, \dots, n$$$ (in that order).

Then, each other cell in the grid will be filled with the square of the number in the cell above it.

Given an integer $$$n$$$, count the number of distinct integers in an $$$n \times n$$$ grid.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of testcases.

The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) – the number of rows and columns in the grid.

Output

For each testcase, output a single integer — the number of distinct integers in the grid.

Example
Input
2
2
3
Output
3
7
Note

The grid in the first testcase:

12
14

The grid in the second testcase:

123
149
11681

E. Replace with MEX
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Define $$$mex(b)$$$ as the minimum non-negative integer that doesn't appear in the array $$$b$$$. For example, $$$mex([1, 0, 2]) = 3$$$ and $$$mex([1]) = 0$$$.

Given an array $$$a$$$ of length $$$n$$$. You can do the following operation at most once:

  • Choose two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), and let $$$m = mex(a[l \dots r])$$$ (where $$$a[l \dots r]$$$ is $$$a_l, a_{l + 1}, \dots a_r$$$).
  • For each $$$i$$$ ($$$l \le i \le r$$$), assign $$$a_i$$$ = $$$m$$$.

What is the maximum value of $$$mex(a)$$$ after performing the above operation at most once.

Input

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the size of the array.

The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le n$$$).

Output

Output a single integer — the maximum possible value of $$$mex(a)$$$.

Examples
Input
4
1 2 3 0
Output
4
Input
2
2 1
Output
2

F. Queries on Distincts
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.

You have to answer $$$q$$$ queries of the following form:

— "$$$l$$$ $$$r$$$": Find the minimum size of substring $$$s[i \dots j]$$$ ($$$l \le i \le j \le r$$$) such that the number of distinct characters in $$$s[i \dots j]$$$ is equal to the number of distinct characters in $$$s[l \dots r]$$$.

Input

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string.

The second line of the input contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.

The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.

Each of the following $$$q$$$ lines of the input consists of two space-separated integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$).

Output

Output $$$q$$$ lines, the $$$i$$$-th of which contains a single integer — the answer to the $$$i$$$-th query.

Example
Input
3
aba
1
1 3
Output
2

G. Lexicographically Maximum
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given a string $$$s$$$ consisting of lowercase English letters.

You can do the following operation on $$$s$$$:

— Choose some substring $$$s[l \dots r]$$$, and let $$$x$$$ be the number of distinct characters in $$$s[l \dots r]$$$.

— Change all characters in $$$s[l \dots r]$$$ to the $$$x$$$-th character in the alphabet (e.g., the first character is 'a', the second is 'b', and so on).

You apply the above operation any number of times (possibly zero) such that $$$\textbf{no index gets changed more than once}$$$.

What is the lexicographically maximum string that could be obtained?

Input

The first and only line of the input contains a string $$$s$$$ consisting of lowercase English letters ($$$1 \le |s| \le 10^6$$$).

Output

The lexicographically maximum string that could be obtained.

Examples
Input
ab
Output
bb
Input
adbbab
Output
cccccc
Input
adbad
Output
ccccd

H. Banis Hotel
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The city of Banis has a hotel consisting of $$$n$$$ floors.

The $$$i$$$-th floor (from the bottom) can withstand a weight of $$$w_i$$$ (i.e., there should be no more than $$$w_i$$$ clients on the $$$i$$$-th floor $$$\textbf{or above}$$$, otherwise, the hotel would collapse).

The hotel manager expects $$$m$$$ clients to come to the hotel this month, and each client has a certain rank.

If the rank of client $$$i$$$ is $$$r_i$$$ ($$$1 \le r_i \le n$$$), then the manager would want them to be on the $$$r_i$$$-th floor or lower.

However, he does not know the ranks of the clients. Therefore, $$$r_i$$$ can be from $$$1$$$ to $$$n$$$ with equal probability.

If a client is placed on the $$$i$$$-th floor, they would give the hotel a rating of $$$i$$$.

As a part of the hotel's staff, you need to place the clients optimally to get the maximum possible sum of ratings (but while still satisfying the conditions mentioned above).

Let $$$X$$$ be the sum of the ratings that each client gives.

What is the expected value of $$$X$$$ if you place the $$$m$$$ clients optimally? You should print the answer modulo $$$10 ^ 9 + 7$$$.

Input

The first line of the input consists of two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2000$$$) — the number of floors and the number of clients, respectively.

The second line of the input consists of $$$n$$$ space-separated integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 2000$$$) — the $$$i$$$-th of which denotes the weight that the $$$i$$$-th floor can withstand.

It's guaranteed that $$$w_1 \ge m$$$ and $$$w_1 \ge w_2 \ge \dots \ge w_n$$$.

Output

Output one line containing a single integer — the expected value of the sum of ratings that each client would give to the hotel if they were placed optimally, taken modulo $$$10 ^ 9 + 7$$$.

It is guaranteed that the answer can always be represented as an irreducible fraction $$$\frac{a}{b}$$$, where $$$b \mod (10^9 + 7) \ne 0$$$; you have to print $$$a \cdot b^{-1} \mod (10^9 + 7)$$$.

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

I. Minimum XOR
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given an array $$$a$$$ of length $$$n$$$, and you should answer $$$q$$$ queries of the following form:

— "$$$x$$$": Find the minimum value of $$$a_i \oplus a_j$$$ ($$$1 \le i \lt j \le n$$$) such that ($$$a_i \: \: | \: \: a_j \: \: | \: \: x \: = \: x$$$).

Note that $$$\oplus$$$ and $$$|$$$ denote the bitwise $$$XOR$$$ and the bitwise $$$OR$$$ operations, respectively.

Input

The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the length of the array $$$a$$$.

The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).

The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^6$$$) — the number of queries.

Each of the following $$$q$$$ lines contains a single integer $$$x$$$ ($$$1 \le x \le 10^6$$$) — the description of the $$$i$$$-th query.

Output

Output $$$q$$$ lines, the $$$i$$$-th of which contains a single integer — the answer to the query, or $$$-1$$$ if there is no such pair that satisfies the conditions.

Example
Input
3
1 2 4
3
3
6
4
Output
3
6
-1

J. The Square Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's hard to predict the winner when legends face each other, especially in chess.

Ahmad and Ahmad are playing a series of $$$n$$$ chess matches, and one of them will win more games than the other, as there will be no ties and $$$n$$$ is odd.

Can you predict the winner?

Input

The first and only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^9 - 1$$$) — the number of matches.

It is guaranteed that $$$n$$$ is odd.

Output

Output one line containing the name of the winner.

Example
Input
1
Output
Ahmad

K. 2.. 3.. 4.. Colorful! Colorful! Colorful!
time limit per test
4.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given $$$n$$$ distinct points $$$(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)$$$.

Each point $$$i$$$ has a color $$$c_i$$$.

Find the $$$\textbf{squared}$$$ Euclidean distance between the farthest pair of points that are not from the same color.

Note: The Euclidean distance between two points $$$(a_1, b_1)$$$ and $$$(a_2, b_2)$$$ is defined as follows: $$$\sqrt{(a_1 - a_2)^2 + (b_1 - b_2)^2}$$$.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^5$$$) — the number of testcases.

The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the number of points.

Each of the next $$$n$$$ lines of each testcase consists of three space-separated integers, $$$x_i$$$, $$$y_i$$$, and $$$c_i$$$ ($$$|x_i|, |y_i| \le 10^9$$$ and $$$1 \le c_i \le n$$$).

It is guaranteed that if $$$i \ne j$$$ then $$$(x_i, y_i, c_i) \ne (x_j, y_j, c_j)$$$.

It is also guaranteed that there will be at least two different colors among the colors of the $$$n$$$ points.

It is also guaranteed that the sum of $$$n$$$ over all testcases will not exceed $$$10^6$$$.

Output

For each testcase, output a single line containing a single integer, the squared Euclidean distance between the farthest pair of points that are from different colors.

Examples
Input
2
4
0 0 1
0 1 2
1 0 2
2 2 1
3
0 0 1
1 1 2
2 2 3
Output
5
8
Input
1
2
-1 -1 1
-3 -4 2
Output
13

L. Median of the Array
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Define the median of an array $$$b_1, b_2, \dots b_k$$$ as $$$b_{\lfloor \frac{k + 1}{2} \rfloor}$$$ after sorting the array $$$b$$$.

For example, the median of the array $$$b = [5, 4, 6, 1, 1]$$$ is $$$4$$$ because after sorting the array becomes $$$b = [1, 1, 4, 5, 6]$$$ and $$$b_{\lfloor \frac{5 + 1}{2} \rfloor} = b_{\lfloor \frac{6}{2} \rfloor} = b_3 = 4$$$. In the same way, the median of the array $$$[3, 5, 2, 7]$$$ is $$$3$$$.

Given an array $$$a_1, a_2, \dots, a_n$$$, is it possible to divide it into two non-empty arrays $$$b$$$ and $$$c$$$ (not necessarily of the same length), such that each element of $$$a$$$ is either in $$$b$$$ or in $$$c$$$, and the medians of both $$$b$$$ and $$$c$$$ are equal?

Note: $$$\lfloor x \rfloor$$$ denotes the integer part of $$$x$$$ (e.g., $$$\lfloor 5.3 \rfloor = 5$$$ and $$$\lfloor 2.7 \rfloor = 2$$$).

Input

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

The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$.

The second line of each testcase consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9)$$$.

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

Output

Output $$$t$$$ lines. For each testcase, print "YES" (without quotes) if there exists two non-empty arrays that satisfy the conditions, and print "NO"(without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Example
Input
3
2
2 1
3
2 3 2
6
6 4 3 9 3 3
Output
NO
YES
YES
Note

Explanation of the first testcase:

The possible ways to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ are:

$$$b = [2]$$$, $$$c = [1]$$$

$$$b = [1]$$$, $$$c = [2]$$$

In both ways, the medians of $$$b$$$ and $$$c$$$ are not equal.

Explanation of the second testcase:

A possible way to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ is:

$$$b = [2]$$$, $$$c = [3, 2]$$$

In this case, the medians of $$$b$$$ and $$$c$$$ are equal to $$$2$$$, and remember that when calculating the median you should first sort the array. That's why, the median of $$$c$$$ is $$$2$$$.

Explanation of the third testcase:

Some possible ways to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ with equal medians are:

$$$b = [4, 9, 3, 3]$$$, $$$c = [6, 3]$$$

$$$b = [6, 4, 3, 3]$$$, $$$c = [3, 9]$$$

$$$b = [4, 3]$$$, $$$c = [6, 9, 3, 3]$$$

M. Taim and Zingers
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Kaito has $$$n$$$ Zingers, and he wants to distribute them between the Kaito kciD team members.

Kaito kciD team consists of 3 members, one of them is greedy Taim.

In order to distribute the $$$n$$$ Zingers between them, Kaito will follow the steps below in order:

— If $$$n$$$ is divisible by $$$3$$$, each of them will get $$$\frac{n}{3}$$$ Zingers.

— Otherwise, if $$$n$$$ is divisible by $$$2$$$, Taim will get $$$\frac{N}{2}$$$ Zingers, and the other two will get the rest.

— Otherwise, Taim will get all the $$$n$$$ Zingers.

Taim can steal at most $$$k$$$ Zingers before Kaito distributes them among the team.

What is the maximum number of Zingers Taim could have (stolen Zingers included)?

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases.

The first line of each testcase consists of two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 10^9$$$) — the number of Zingers Kaito will give, and the number of Zingers Taim can steal from Kaito before distributing the Zingers.

Output

Output $$$t$$$ lines. For each testcase, print a single integer — the maximum number of Zingers Taim could have.

Example
Input
3
6 1
10 1
20 2
Output
6
5
20