There is an infinitely large pond, which we consider as a number line.
In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1.
On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
 - 1. Choose positive integers A and B. Your score is initially 0.
 - 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
 - If y = N-1, the game ends.
 - If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
 - If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
 - 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
 - If y = N-1, the game ends.
 - If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
 - If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
 - 4. Go back to step 2.
You want to end the game with as high a score as possible.
What is the score obtained by the optimal choice of A and B?

-----Constraints-----
 - 3 \leq N \leq 10^5
 - -10^9 \leq s_i \leq 10^9
 - s_0=s_{N-1}=0
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}

-----Output-----
Print the score obtained by the optimal choice of A and B.

-----Sample Input-----
5
0 2 5 1 0

-----Sample Output-----
3

If you choose A = 3 and B = 2, the game proceeds as follows:
 - Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.
 - Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.
 - Move to coordinate 1 + 3 = 4. The game ends with a score of 3.
There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.


Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
 - Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.

-----Constraints-----
 -  1 \le N \le 3 \times 10^5
 -  1 \le A_i \le N
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N

-----Output-----
Print N integers.
The t-th (1 \le t \le N) of them should be the answer for the case K=t.

-----Sample Input-----
3
2 1 2

-----Sample Output-----
3
1
0

For K = 1, we can do the operation as follows:
 - Choose the first card to eat.
 - Choose the second card to eat.
 - Choose the third card to eat.
For K = 2, we can do the operation as follows:
 - Choose the first and second cards to eat.
For K = 3, we cannot do the operation at all. Note that we cannot choose the first and third cards at the same time.


We have N logs of lengths A_1,A_2,\cdots A_N.
We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.
Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - 0 \leq K \leq 10^9
 - 1 \leq A_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 \cdots A_N

-----Output-----
Print an integer representing the answer.

-----Sample Input-----
2 3
7 9

-----Sample Output-----
4

 - First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.
 - Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.
 - Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.
In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.


You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:
 - () is a correct bracket sequence.
 - If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.
 - If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.
 - Every correct bracket sequence can be derived from the rules above.
Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

-----Constraints-----
 - The length of S is N.
 - 1 ≤ N ≤ 100
 - S consists of ( and ).

-----Input-----
Input is given from Standard Input in the following format:
N
S

-----Output-----
Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

-----Sample Input-----
3
())

-----Sample Output-----
(())


There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left.
Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it.
Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it.
Q queries are given. We ask you to process them in order.
There are two kinds of queries. Their input format and description are as follows:
 - 1 x: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.
 - 2 x: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.
How many black stones are there on the grid after processing all Q queries?

-----Constraints-----
 - 3 \leq N \leq 2\times 10^5
 - 0 \leq Q \leq \min(2N-4,2\times 10^5)
 - 2 \leq x \leq N-1
 - Queries are pairwise distinct.

-----Input-----
Input is given from Standard Input in the following format:
N Q
Query_1
\vdots
Query_Q

-----Output-----
Print how many black stones there are on the grid after processing all Q queries.

-----Sample Input-----
5 5
1 3
2 3
1 4
2 2
1 2

-----Sample Output-----
1

After each query, the grid changes in the following way:


Given are a sequence of N positive integers A_1, A_2, \ldots, A_N, and a positive integer K.
Find the number of non-empty contiguous subsequences in A such that the remainder when dividing the sum of its elements by K is equal to the number of its elements. We consider two subsequences different if they are taken from different positions, even if they are equal sequences.

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 2\times 10^5
 - 1 \leq K \leq 10^9
 - 1 \leq A_i \leq 10^9

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 \cdots A_N

-----Output-----
Print the number of subsequences that satisfy the condition.

-----Sample Input-----
5 4
1 4 2 3 5

-----Sample Output-----
4

Four sequences satisfy the condition: (1), (4,2), (1,4,2), and (5).


Joisino the magical girl has decided to turn every single digit that exists on this world into 1.
Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).
She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).
You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:
 - If A_{i,j}≠-1, the square contains a digit A_{i,j}.
 - If A_{i,j}=-1, the square does not contain a digit.
Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

-----Constraints-----
 - 1≤H,W≤200
 - 1≤c_{i,j}≤10^3 (i≠j)
 - c_{i,j}=0 (i=j)
 - -1≤A_{i,j}≤9
 - All input values are integers.
 - There is at least one digit on the wall.

-----Input-----
Input is given from Standard Input in the following format:
H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

-----Output-----
Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

-----Sample Input-----
2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

-----Sample Output-----
12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.
The wall contains two 8s, so the minimum total MP required is 6×2=12.


There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.
We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.
For each city, find the number of the cities connected to that city by both roads and railways.

-----Constraints-----
 - 2 ≦ N ≦ 2*10^5
 - 1 ≦ K, L≦ 10^5
 - 1 ≦ p_i, q_i, r_i, s_i ≦ N
 - p_i < q_i
 - r_i < s_i
 - When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)
 - When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

-----Input-----
The input is given from Standard Input in the following format:
N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

-----Output-----
Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

-----Sample Input-----
4 3 1
1 2
2 3
3 4
2 3

-----Sample Output-----
1 2 2 1

All the four cities are connected to each other by roads.
By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.


Joisino is planning to record N TV programs with recorders.
The TV can receive C channels numbered 1 through C.
The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.
Here, there will never be more than one program that are broadcast on the same channel at the same time.
When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).
Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

-----Constraints-----
 - 1≤N≤10^5
 - 1≤C≤30
 - 1≤s_i<t_i≤10^5
 - 1≤c_i≤C
 - If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.
 - All input values are integers.

-----Input-----
Input is given from Standard Input in the following format:
N C
s_1 t_1 c_1
:
s_N t_N c_N

-----Output-----
When the minimum required number of recorders is x, print the value of x.

-----Sample Input-----
3 2
1 7 2
7 8 1
8 12 1

-----Sample Output-----
2

Two recorders can record all the programs, for example, as follows:
 - With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.
 - With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.


There is an integer sequence A of length N.
Find the number of the pairs of integers l and r (1 \leq l \leq r \leq N) that satisfy the following condition:
 - A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r
Here, xor denotes the bitwise exclusive OR.
Definition of XOR
The XOR of integers c_1, c_2, ..., c_m is defined as follows:
 - Let the XOR be X. In the binary representation of X, the digit in the 2^k's place (0 \leq k; k is an integer) is 1 if there are an odd number of integers among c_1, c_2, ...c_m whose binary representation has 1 in the 2^k's place, and 0 if that number is even.
For example, let us compute the XOR of 3 and 5. The binary representation of 3 is 011, and the binary representation of 5 is 101, thus the XOR has the binary representation 110, that is, the XOR is 6.

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - 0 \leq A_i < 2^{20}
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N

-----Output-----
Print the number of the pairs of integers l and r (1 \leq l \leq r \leq N) that satisfy the condition.

-----Sample Input-----
4
2 5 4 6

-----Sample Output-----
5

(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition.
(l,r)=(1,2) also satisfies the condition, since A_1\ xor\ A_2 = A_1\ +\ A_2 = 7.
There are no other pairs that satisfy the condition, so the answer is 5.


We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).
Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.
Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

-----Constraints-----
 - All values in input are integers.
 - 1 \leq H, W \leq 3 \times 10^5
 - 1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)
 - 1 \leq h_i \leq H
 - 1 \leq w_i \leq W
 - \left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

-----Input-----
Input is given from Standard Input in the following format:
H W M
h_1 w_1
\vdots
h_M w_M

-----Output-----
Print the answer.

-----Sample Input-----
2 3 3
2 2
1 1
1 3

-----Sample Output-----
3

We can destroy all the targets by placing the bomb at \left(1, 2\right).


AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.
Then, for each card i, he judges whether it is unnecessary or not, as follows:
 - If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.
 - Otherwise, card i is NOT unnecessary.
Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

-----Constraints-----
 - All input values are integers.
 - 1≤N≤5000
 - 1≤K≤5000
 - 1≤a_i≤10^9 (1≤i≤N)

-----Partial Score-----
 - 300 points will be awarded for passing the test set satisfying N,K≤400.

-----Input-----
The input is given from Standard Input in the following format:
N K
a_1 a_2 ... a_N

-----Output-----
Print the number of the unnecessary cards.

-----Sample Input-----
3 6
1 4 3

-----Sample Output-----
1

There are two good sets: {2,3} and {1,2,3}.
Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.
For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.
Neither is card 3 for a similar reason, hence the answer is 1.


We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
 - Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - 1 \leq u_i, v_i \leq N
 - The given graph is a tree.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

-----Output-----
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

-----Sample Input-----
3
1 3
2 3

-----Sample Output-----
7

We have six possible pairs (L, R) as follows:
 - For L = 1, R = 1, S = \{1\} and we have 1 connected component.
 - For L = 1, R = 2, S = \{1, 2\} and we have 2 connected components.
 - For L = 1, R = 3, S = \{1, 2, 3\} and we have 1 connected component, since S contains both endpoints of each of the edges 1, 2.
 - For L = 2, R = 2, S = \{2\} and we have 1 connected component.
 - For L = 2, R = 3, S = \{2, 3\} and we have 1 connected component, since S contains both endpoints of Edge 2.
 - For L = 3, R = 3, S = \{3\} and we have 1 connected component.
The sum of these is 7.


Silver Fox is fighting with N monsters.
The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.
Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.
Silver Fox wins when all the monsters' healths become 0 or below.
Find the minimum number of bombs needed to win.

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - 0 \leq D \leq 10^9
 - 1 \leq A \leq 10^9
 - 0 \leq X_i \leq 10^9
 - 1 \leq H_i \leq 10^9
 - X_i are distinct.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N D A
X_1 H_1
:
X_N H_N

-----Output-----
Print the minimum number of bombs needed to win.

-----Sample Input-----
3 3 2
1 2
5 4
9 2

-----Sample Output-----
2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.
Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.
Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.


Given is a number sequence A of length N.
Find the number of integers i \left(1 \leq i \leq N\right) with the following property:
 - For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 2 \times 10^5
 - 1 \leq A_i \leq 10^6

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N

-----Output-----
Print the answer.

-----Sample Input-----
5
24 11 8 3 16

-----Sample Output-----
3

The integers with the property are 2, 3, and 4.


In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.

-----Constraints-----
 - N is an integer between 1 and 10^{1,000,000} (inclusive).

-----Input-----
Input is given from Standard Input in the following format:
N

-----Output-----
Print the minimum possible number of total banknotes used by you and the clerk.

-----Sample Input-----
36

-----Sample Output-----
8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.


There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.
Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:
 - When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.
The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.
When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

-----Constraints-----
 - 2≤N≤1000
 - 1≤M≤min(N(N-1),2000)
 - 1≤a_i,b_i≤N (1≤i≤M)
 - a_i≠b_i (1≤i≤M)
 - a_i≠a_j or b_i≠b_j (1≤i<j≤M)
 - -10^9≤c_i≤10^9 (1≤i≤M)
 - c_i is an integer.
 - In the given graph, there exists a path from vertex 1 to vertex N.

-----Input-----
Input is given from Standard Input in the following format:
N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

-----Output-----
Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

-----Sample Input-----
3 3
1 2 4
2 3 3
1 3 5

-----Sample Output-----
7

There are two ways to move the piece to vertex N=3:
 - vertex 1 → vertex 2 → vertex 3 : score 4+3=7
 - vertex 1 → vertex 3 : score 5
Thus, the maximum possible score at the end of the game is 7.


We have N integers A_1, A_2, ..., A_N.
There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 2 \times 10^5
 - 1 \leq K \leq \frac{N(N-1)}{2}
 - -10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N

-----Output-----
Print the answer.

-----Sample Input-----
4 3
3 3 -4 -2

-----Sample Output-----
-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.
Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.


You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n.
It is known that each of the n integers 1,...,n appears at least once in this sequence.
For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7.

-----Notes-----
 - If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.
 - A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

-----Constraints-----
 - 1 \leq n \leq 10^5
 - 1 \leq a_i \leq n
 - Each of the integers 1,...,n appears in the sequence.
 - n and a_i are integers.

-----Input-----
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_{n+1}

-----Output-----
Print n+1 lines.
The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7.

-----Sample Input-----
3
1 2 1 3

-----Sample Output-----
3
5
4
1

There are three subsequences with length 1: 1 and 2 and 3.
There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.
There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.
There is one subsequence with length 4: 1,2,1,3.


Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

-----Constraints-----
 - 2 \leq K \leq 10^5
 - K is an integer.

-----Input-----
Input is given from Standard Input in the following format:
K

-----Output-----
Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

-----Sample Input-----
6

-----Sample Output-----
3

12=6×2 yields the smallest sum.


You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
 - For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
 - For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

-----Constraints-----
 - 2 ≤ n ≤ 10^5
 - |a_i| ≤ 10^9
 - Each a_i is an integer.

-----Input-----
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n

-----Output-----
Print the minimum necessary count of operations.

-----Sample Input-----
4
1 -3 1 0

-----Sample Output-----
4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.


We have N integers. The i-th number is A_i.
\{A_i\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N.
\{A_i\} is said to be setwise coprime when \{A_i\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1.
Determine if \{A_i\} is pairwise coprime, setwise coprime, or neither.
Here, GCD(\ldots) denotes greatest common divisor.

-----Constraints-----
 - 2 \leq N \leq 10^6
 - 1 \leq A_i\leq 10^6

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 \ldots A_N

-----Output-----
If \{A_i\} is pairwise coprime, print pairwise coprime; if \{A_i\} is setwise coprime, print setwise coprime; if neither, print not coprime.

-----Sample Input-----
3
3 4 5

-----Sample Output-----
pairwise coprime

GCD(3,4)=GCD(3,5)=GCD(4,5)=1, so they are pairwise coprime.


There are N points in a two-dimensional plane. The initial coordinates of the i-th point are (x_i, y_i). Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x- or y- axis. You are given a character d_i that represents the specific direction in which the i-th point moves, as follows:
 - If d_i = R, the i-th point moves in the positive x direction;
 - If d_i = L, the i-th point moves in the negative x direction;
 - If d_i = U, the i-th point moves in the positive y direction;
 - If d_i = D, the i-th point moves in the negative y direction.
You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let x_{max} and x_{min} be the maximum and minimum among the x-coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y-coordinates of the N points, respectively.
Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it.

-----Constraints-----
 - 1 \leq N \leq 10^5
 - -10^8 \leq x_i,\ y_i \leq 10^8
 - x_i and y_i are integers.
 - d_i is R, L, U, or D.

-----Input-----
Input is given from Standard Input in the following format:
N
x_1 y_1 d_1
x_2 y_2 d_2
.
.
.
x_N y_N d_N

-----Output-----
Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}).
The output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-9}.

-----Sample Input-----
2
0 3 D
3 0 L

-----Sample Output-----
0

After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment.


Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.
Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):
 - There exists a non-negative integer p such that X= a_k \times (p+0.5).
Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

-----Constraints-----
 - 1 \leq N \leq 10^5
 - 1 \leq M \leq 10^9
 - 2 \leq a_i \leq 10^9
 - a_i is an even number.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N M
a_1 a_2 ... a_N

-----Output-----
Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

-----Sample Input-----
2 50
6 10

-----Sample Output-----
2

 - 15 = 6  \times 2.5
 - 15 = 10 \times 1.5
 - 45 = 6  \times 7.5
 - 45 = 10 \times 4.5
Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.


We have N non-negative integers: A_1, A_2, ..., A_N.
Consider painting at least one and at most N-1 integers among them in red, and painting the rest in blue.
Let the beauty of the painting be the \mbox{XOR} of the integers painted in red, plus the \mbox{XOR} of the integers painted in blue.
Find the maximum possible beauty of the painting.What is \mbox{XOR}?

The bitwise \mbox{XOR} x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

 -  When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.
For example, 3 \oplus 5 = 6.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 10^5
 - 0 \leq A_i < 2^{60}\ (1 \leq i \leq N)

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N

-----Output-----
Print the maximum possible beauty of the painting.

-----Sample Input-----
3
3 6 5

-----Sample Output-----
12

If we paint 3, 6, 5 in blue, red, blue, respectively, the beauty will be (6) + (3 \oplus 5) = 12.
There is no way to paint the integers resulting in greater beauty than 12, so the answer is 12.


You are given a positive integer N.
Find the number of the pairs of integers u and v (0≦u,v≦N) such that there exist two non-negative integers a and b satisfying a xor b=u and a+b=v.
Here, xor denotes the bitwise exclusive OR.
Since it can be extremely large, compute the answer modulo 10^9+7.

-----Constraints-----
 - 1≦N≦10^{18}

-----Input-----
The input is given from Standard Input in the following format:
N

-----Output-----
Print the number of the possible pairs of integers u and v, modulo 10^9+7.

-----Sample Input-----
3

-----Sample Output-----
5

The five possible pairs of u and v are:
 - u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)
 - u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.）
 - u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.）
 - u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.）
 - u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.）


You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.
There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.
According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.
 - The absolute difference of their attendee numbers is equal to the sum of their heights.
There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?
P.S.: We cannot let you know the secret.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 2 \times 10^5
 - 1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_N

-----Output-----
Print the number of pairs satisfying the condition.

-----Sample Input-----
6
2 3 3 1 3 1

-----Sample Output-----
3

 - A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.
 - A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.
 - A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.
No other pair satisfies the condition, so you should print 3.


Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.
 - There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

-----Notes-----
 - A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.
 - We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

-----Constraints-----
 - 1 \leq |s| \leq 5 \times 10^5
 - 1 \leq |t| \leq 5 \times 10^5
 - s and t consist of lowercase English letters.

-----Input-----
Input is given from Standard Input in the following format:
s
t

-----Output-----
If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

-----Sample Input-----
abcabab
ab

-----Sample Output-----
3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.
On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.
Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.


AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:
AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

-----Constraints-----
 - 1 ≤ N ≤ 10^5
 - 1 ≤ K ≤ 1000
 - 0 ≤ x_i ≤ 10^9
 - 0 ≤ y_i ≤ 10^9
 - If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).
 - c_i is B or W.
 - N, K, x_i and y_i are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

-----Output-----
Print the maximum number of desires that can be satisfied at the same time.

-----Sample Input-----
4 3
0 1 W
1 2 W
5 3 B
5 4 B

-----Sample Output-----
4

He can satisfy all his desires by painting as shown in the example above.


There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.
Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.
Process the following Q queries:
 - The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 300
 - 0 \leq M \leq \frac{N(N-1)}{2}
 - 1 \leq L \leq 10^9
 - 1 \leq A_i, B_i \leq N
 - A_i \neq B_i
 - \left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)
 - \left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)
 - 1 \leq C_i \leq 10^9
 - 1 \leq Q \leq N\left(N-1\right)
 - 1 \leq s_i, t_i \leq N
 - s_i \neq t_i
 - \left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

-----Input-----
Input is given from Standard Input in the following format:
N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

-----Output-----
Print Q lines.
The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

-----Sample Input-----
3 2 5
1 2 3
2 3 3
2
3 2
1 3

-----Sample Output-----
0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.
To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.


We have a set S of N points in a two-dimensional plane. The coordinates of the i-th point are (x_i, y_i). The N points have distinct x-coordinates and distinct y-coordinates.
For a non-empty subset T of S, let f(T) be the number of points contained in the smallest rectangle, whose sides are parallel to the coordinate axes, that contains all the points in T. More formally, we define f(T) as follows:
 - f(T) :=  (the number of integers i (1 \leq i \leq N) such that a \leq x_i \leq b and c \leq y_i \leq d, where a, b, c, and d are the minimum x-coordinate, the maximum x-coordinate, the minimum y-coordinate, and the maximum y-coordinate of the points in T)
Find the sum of f(T) over all non-empty subset T of S. Since it can be enormous, print the sum modulo 998244353.

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - -10^9 \leq x_i, y_i \leq 10^9
 - x_i \neq x_j (i \neq j)
 - y_i \neq y_j (i \neq j)
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
x_1 y_1
:
x_N y_N

-----Output-----
Print the sum of f(T) over all non-empty subset T of S, modulo 998244353.

-----Sample Input-----
3
-1 3
2 1
3 -2

-----Sample Output-----
13

Let the first, second, and third points be P_1, P_2, and P_3, respectively. S = \{P_1, P_2, P_3\} has seven non-empty subsets, and f has the following values for each of them:
 - f(\{P_1\}) = 1
 - f(\{P_2\}) = 1
 - f(\{P_3\}) = 1
 - f(\{P_1, P_2\}) = 2
 - f(\{P_2, P_3\}) = 2
 - f(\{P_3, P_1\}) = 3
 - f(\{P_1, P_2, P_3\}) = 3
The sum of these is 13.


Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.
Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:
 - Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).
 - Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.
However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:
 - Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).
What is the maximum possible happiness after M handshakes?

-----Constraints-----
 - 1 \leq N \leq 10^5
 - 1 \leq M \leq N^2
 - 1 \leq A_i \leq 10^5
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_N

-----Output-----
Print the maximum possible happiness after M handshakes.

-----Sample Input-----
5 3
10 14 19 34 33

-----Sample Output-----
202

Let us say that Takahashi performs the following handshakes:
 - In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.
 - In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.
 - In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.
Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.
We cannot achieve the happiness of 203 or greater, so the answer is 202.


Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.
In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:
 - A team should assign one member to each food, and should not assign the same member to multiple foods.
 - It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.
 - The score of a team is the longest time it takes for an individual member to finish the food.
Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.
What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 2 \times 10^5
 - 0 \leq K \leq 10^{18}
 - 1 \leq A_i \leq 10^6\ (1 \leq i \leq N)
 - 1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

-----Output-----
Print the minimum possible score of the team.

-----Sample Input-----
3 5
4 2 1
2 3 1

-----Sample Output-----
2

They can achieve the score of 2, as follows:
 - Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.
 - Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.
 - Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.
They cannot achieve a score of less than 2, so the answer is 2.


Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

-----Constraints-----
 - 1 \leq H,W,K \leq 10^6
 - H \times W \leq 10^6
 - 1 \leq x_1,x_2 \leq H
 - 1 \leq y_1,y_2 \leq W
 - x_1 \neq x_2 or y_1 \neq y_2.
 - c_{i,j} is . or @.
 - c_{x_1,y_1} = .
 - c_{x_2,y_2} = .
 - All numbers in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

-----Output-----
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

-----Sample Input-----
3 5 2
3 2 3 4
.....
.@..@
..@..

-----Sample Output-----
5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:
 - From (3, 2), go west one square to (3, 1).
 - From (3, 1), go north two squares to (1, 1).
 - From (1, 1), go east two squares to (1, 3).
 - From (1, 3), go east one square to (1, 4).
 - From (1, 4), go south two squares to (3, 4).


Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

-----Constraints-----
 - 1 \leq N \leq 10^5
 - 0 \leq K \leq N-1
 - All input values are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K

-----Output-----
Print the number of possible pairs that he may have had.

-----Sample Input-----
5 2

-----Sample Output-----
7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).


There are N persons called Person 1 through Person N.
You are given M facts that "Person A_i and Person B_i are friends." The same fact may be given multiple times.
If X and Y are friends, and Y and Z are friends, then X and Z are also friends. There is no friendship that cannot be derived from the M given facts.
Takahashi the evil wants to divide the N persons into some number of groups so that every person has no friend in his/her group.
At least how many groups does he need to make?

-----Constraints-----
 - 2 \leq N \leq 2\times 10^5
 - 0 \leq M \leq 2\times 10^5
 - 1\leq A_i,B_i\leq N
 - A_i \neq B_i

-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 B_1
\vdots
A_M B_M

-----Output-----
Print the answer.

-----Sample Input-----
5 3
1 2
3 4
5 1

-----Sample Output-----
3

Dividing them into three groups such as \{1,3\}, \{2,4\}, and \{5\} achieves the goal.


You are given a tree with N vertices and N-1 edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and b_i.
You have coloring materials of K colors.
For each vertex in the tree, you will choose one of the K colors to paint it, so that the following condition is satisfied:
 - If the distance between two different vertices x and y is less than or equal to two, x and y have different colors.
How many ways are there to paint the tree? Find the count modulo 1\ 000\ 000\ 007.
What is tree?
A tree is a kind of graph. For detail, please see: Wikipedia "Tree (graph theory)"

What is distance?
The distance between two vertices x and y is the minimum number of edges one has to traverse to get from x to y.

-----Constraints-----
 - 1 \leq N,K \leq 10^5
 - 1 \leq a_i,b_i \leq N
 - The given graph is a tree.

-----Input-----
Input is given from Standard Input in the following format:
N K
a_1 b_1
a_2 b_2
.
.
.
a_{N-1} b_{N-1}

-----Output-----
Print the number of ways to paint the tree, modulo 1\ 000\ 000\ 007.

-----Sample Input-----
4 3
1 2
2 3
3 4

-----Sample Output-----
6

There are six ways to paint the tree.


There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.
Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).
He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.
Find the maximum possible sum of the values of items he picks up.

-----Constraints-----
 - 1 \leq R, C \leq 3000
 - 1 \leq K \leq \min(2 \times 10^5, R \times C)
 - 1 \leq r_i \leq R
 - 1 \leq c_i \leq C
 - (r_i, c_i) \neq (r_j, c_j) (i \neq j)
 - 1 \leq v_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

-----Output-----
Print the maximum possible sum of the values of items Takahashi picks up.

-----Sample Input-----
2 2 3
1 1 3
2 1 4
1 2 5

-----Sample Output-----
8

He has two ways to get to the goal:
 - Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.
 - Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.
Thus, the maximum possible sum of the values of items he picks up is 8.


Snuke, who loves animals, built a zoo.
There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.
There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.
Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.
More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.
Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

-----Constraints-----
 - 3 ≤ N ≤ 10^{5}
 - s is a string of length N consisting of o and x.

-----Input-----
The input is given from Standard Input in the following format:
N
s

-----Output-----
If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.
 - t is a string of length N consisting of S and W.
 - If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

-----Sample Input-----
6
ooxoox

-----Sample Output-----
SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.
Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.


You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.
You will now successively do the following Q operations:
 - In the i-th operation, you replace every element whose value is B_{i} with C_{i}.
For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

-----Constraints-----
 - All values in input are integers.
 -  1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}
 -  B_{i} \neq C_{i}

-----Input-----
Input is given from Standard Input in the following format:
N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

-----Output-----
Print Q integers S_{i} to Standard Output in the following format:
S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

-----Sample Input-----
4
1 2 3 4
3
1 2
3 4
2 4

-----Sample Output-----
11
12
16

Initially, the sequence A is 1,2,3,4.
After each operation, it becomes the following:
 - 2, 2, 3, 4
 - 2, 2, 4, 4
 - 4, 4, 4, 4


We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.
You are given Q queries.
In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

-----Constraints-----
 - 1≤Q≤10^5
 - 1≤l_i≤r_i≤10^5
 - l_i and r_i are odd.
 - All input values are integers.

-----Input-----
Input is given from Standard Input in the following format:
Q
l_1 r_1
:
l_Q r_Q

-----Output-----
Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

-----Sample Input-----
1
3 7

-----Sample Output-----
2

 - 3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.
 - 5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.
 - 7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.
Thus, the response to the first query should be 2.


Takahashi has a string S of length N consisting of digits from 0 through 9.
He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.
Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.
Compute this count to help Takahashi.

-----Constraints-----
 - 1 \leq N \leq 2 \times 10^5
 - S consists of digits.
 - |S| = N
 - 2 \leq P \leq 10000
 - P is a prime number.

-----Input-----
Input is given from Standard Input in the following format:
N P
S

-----Output-----
Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

-----Sample Input-----
4 3
3543

-----Sample Output-----
6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:
 - 3: divisible by 3.
 - 35: not divisible by 3.
 - 354: divisible by 3.
 - 3543: divisible by 3.
 - 5: not divisible by 3.
 - 54: divisible by 3.
 - 543: divisible by 3.
 - 4: not divisible by 3.
 - 43: not divisible by 3.
 - 3: divisible by 3.
Six of these are divisible by 3, so print 6.


There are N towns in the State of Atcoder, connected by M bidirectional roads.
The i-th road connects Town A_i and B_i and has a length of C_i.
Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).
She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.
If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

-----Constraints-----
 - 2≤N≤200
 - 1≤M≤N×(N-1)/2
 - 2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)
 - r_i≠r_j (i≠j)
 - 1≤A_i,B_i≤N, A_i≠B_i
 - (A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)
 - 1≤C_i≤100000
 - Every town can be reached from every town by road.
 - All input values are integers.

-----Input-----
Input is given from Standard Input in the following format:
N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

-----Output-----
Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

-----Sample Input-----
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4

-----Sample Output-----
2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.


You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.
Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:
 - Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.
At least how many explosions do you need to cause in order to vanish all the monsters?

-----Constraints-----
 - All input values are integers.
 - 1 ≤ N ≤ 10^5
 - 1 ≤ B < A ≤ 10^9
 - 1 ≤ h_i ≤ 10^9

-----Input-----
Input is given from Standard Input in the following format:
N A B
h_1
h_2
:
h_N

-----Output-----
Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

-----Sample Input-----
4 5 3
8
7
4
2

-----Sample Output-----
2

You can vanish all the monsters in two explosion, as follows:
 - First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.
 - Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.


We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.
We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

-----Constraints-----
 - 1 ≤ K ≤ N ≤ 200000
 - 1 ≤ p_i ≤ 1000
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
p_1 ... p_N

-----Output-----
Print the maximum possible value of the expected value of the sum of the numbers shown.
Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

-----Sample Input-----
5 3
1 2 2 4 5

-----Sample Output-----
7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.


An SNS has N users - User 1, User 2, \cdots, User N.
Between these N users, there are some relationships - M friendships and K blockships.
For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.
For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.
We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:
 - a \neq b.
 - There is not a friendship between User a and User b.
 - There is not a blockship between User a and User b.
 - There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.
For each user i = 1, 2, ... N, how many friend candidates does it have?

-----Constraints-----
 - All values in input are integers.
 - 2 ≤ N ≤ 10^5
 - 0 \leq M \leq 10^5
 - 0 \leq K \leq 10^5
 - 1 \leq A_i, B_i \leq N
 - A_i \neq B_i
 - 1 \leq C_i, D_i \leq N
 - C_i \neq D_i
 - (A_i, B_i) \neq (A_j, B_j) (i \neq j)
 - (A_i, B_i) \neq (B_j, A_j)
 - (C_i, D_i) \neq (C_j, D_j) (i \neq j)
 - (C_i, D_i) \neq (D_j, C_j)
 - (A_i, B_i) \neq (C_j, D_j)
 - (A_i, B_i) \neq (D_j, C_j)

-----Input-----
Input is given from Standard Input in the following format:
N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K

-----Output-----
Print the answers in order, with space in between.

-----Sample Input-----
4 4 1
2 1
1 3
3 2
3 4
4 1

-----Sample Output-----
0 1 0 1

There is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.
However, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate.


Let N be a positive integer.
There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').
Find the maximum possible score of a'.

-----Constraints-----
 - 1 ≤ N ≤ 10^5
 - a_i is an integer.
 - 1 ≤ a_i ≤ 10^9

-----Partial Score-----
 - In the test set worth 300 points, N ≤ 1000.

-----Input-----
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{3N}

-----Output-----
Print the maximum possible score of a'.

-----Sample Input-----
2
3 1 4 1 5 9

-----Sample Output-----
1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.


Given are two sequences A and B, both of length N.
A and B are each sorted in the ascending order.
Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it.

-----Constraints-----
 - 1\leq N \leq 2 \times 10^5
 - 1\leq A_i,B_i \leq N
 - A and B are each sorted in the ascending order.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

-----Output-----
If there exist no reorderings that satisfy the condition, print No.
If there exists a reordering that satisfies the condition, print Yes on the first line.
After that, print a reordering of B on the second line, separating terms with a whitespace.
If there are multiple reorderings that satisfy the condition, you can print any of them.

-----Sample Input-----
6
1 1 1 2 2 3
1 1 1 2 2 3

-----Sample Output-----
Yes
2 2 3 1 1 1


You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.
 - Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

-----Constraints-----
 - 1\leq |S|\leq 10^5
 - S_i(1\leq i\leq N) is either 0 or 1.

-----Input-----
Input is given from Standard Input in the following format:
S

-----Output-----
Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

-----Sample Input-----
010

-----Sample Output-----
2

We can turn all the characters of S into 0 by the following operations:
 - Perform the operation on the segment S[1,3] with length 3. S is now 101.
 - Perform the operation on the segment S[1,2] with length 2. S is now 011.
 - Perform the operation on the segment S[2,3] with length 2. S is now 000.


We have N integers. The i-th integer is A_i.
Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

 - When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.
For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

-----Constraints-----
 - 2 \leq N \leq 3 \times 10^5
 - 0 \leq A_i < 2^{60}
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N

-----Output-----
Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

-----Sample Input-----
3
1 2 3

-----Sample Output-----
6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.


Takahashi has a string S consisting of lowercase English letters.
Starting with this string, he will produce a new one in the procedure given as follows.
The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:
 - If T_i = 1: reverse the string S.
 - If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.
 - If F_i = 1 : Add C_i to the beginning of the string S.
 - If F_i = 2 : Add C_i to the end of the string S.
Help Takahashi by finding the final string that results from the procedure.

-----Constraints-----
 - 1 \leq |S| \leq 10^5
 - S consists of lowercase English letters.
 - 1 \leq Q \leq 2 \times 10^5
 - T_i = 1 or 2.
 - F_i = 1 or 2, if provided.
 - C_i is a lowercase English letter, if provided.

-----Input-----
Input is given from Standard Input in the following format:
S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:
1

which means T_i = 1, and:
2 F_i C_i

which means T_i = 2.

-----Output-----
Print the resulting string.

-----Sample Input-----
a
4
2 1 p
1
2 2 c
1

-----Sample Output-----
cpa

There will be Q = 4 operations. Initially, S is a.
 - Operation 1: Add p at the beginning of S. S becomes pa.
 - Operation 2: Reverse S. S becomes ap.
 - Operation 3: Add c at the end of S. S becomes apc.
 - Operation 4: Reverse S. S becomes cpa.
Thus, the resulting string is cpa.


You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

-----Constraints-----
 - 1 \leq X \leq A \leq 10^5
 - 1 \leq Y \leq B \leq 10^5
 - 1 \leq C \leq 10^5
 - 1 \leq p_i \leq 10^9
 - 1 \leq q_i \leq 10^9
 - 1 \leq r_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

-----Output-----
Print the maximum possible sum of the deliciousness of the eaten apples.

-----Sample Input-----
1 2 2 2 1
2 4
5 1
3

-----Sample Output-----
12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:
 - Eat the 2-nd red apple.
 - Eat the 1-st green apple.
 - Paint the 1-st colorless apple green and eat it.


Dilku and Bhopu live in Artland. Dilku loves Bhopu and he writes a message : “iloveyou”
on paper and wishes to send it to Bhopu. As Dilku is busy making an artwork, he asks his friend Raj to send the message to Bhopu. However Raj has a condition that he may add/remove some  characters and jumble the letters of the message.
As Bhopu understands Dilku, she can read “iloveyou” from the message if all the characters of the string “iloveyou” are in the message received by her. Bhopu is happy if she can read “iloveyou” from the message. Otherwise, she is sad. Tell whether Bhopu is happy or sad.

-----Input-----
Input contains a string S, where S is the message received by Bhopu. String S consists of only lowercase letters.

-----Output-----
Output “happy” if Bhopu is happy and “sad” if Bhopu is sad.

-----Constraints-----
1 ≤ |S| ≤ 100
Where |S| denotes length of message string S

-----Example-----
Input 1:
iloveyou

Output 1:
happy

Input 2:
ulrvysioqjifo

Output 2:
sad

Input 3:
abcvleouioydef

Output 3:
happy


Chef loves research! Now he is looking for subarray of maximal length with non-zero product.
Chef has an array A with N elements: A1, A2, ..., AN.

Subarray Aij of array A is elements from index i to index j: Ai, Ai+1, ..., Aj.
Product of subarray Aij is product of all its elements (from ith to jth).

-----Input-----
- First line contains sinlge integer N denoting the number of elements.
- Second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.

-----Output-----
- In a single line print single integer - the maximal length of subarray with non-zero product.

-----Constraints-----
- 1 ≤ N ≤ 100000
- 0 ≤ Ai ≤ 10000

-----Example-----
Input:
6
1 0 2 3 0 4

Output:
2

Input:
1
0

Output:
0

Input:
3
1 0 1

Output:
1

-----Explanation-----
For the first sample subarray is: {2, 3}.
For the second sample there are no subbarays with non-zero product.
For the third sample subbarays is {1}, (the first element, or the third one).


Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges).  For each successful withdrawal the bank charges 0.50 $US.

Calculate Pooja's account balance after an attempted transaction.

-----Input-----
Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

-----Output-----
Output the account balance after the attempted transaction, given as a number with two digits of precision.  If there is not enough money in the account to complete the transaction, output the current bank balance.

-----Example - Successful Transaction-----
Input:
30 120.00

Output:
89.50

-----Example - Incorrect Withdrawal Amount (not multiple of 5)-----
Input:
42 120.00

Output:
120.00

-----Example - Insufficient Funds-----
Input:
300 120.00

Output:
120.00


Chefs from all over the globe gather each year for an international convention. Each chef represents some country. Please, note that more than one chef can represent a country.
Each of them presents their best dish to the audience. The audience then sends emails to a secret and secure mail server, with the subject being the name of the chef whom they wish to elect as the "Chef of the Year".
You will be given the list of the subjects of all the emails. Find the country whose chefs got the most number of votes, and also the chef who got elected as the "Chef of the Year" (the chef who got the most number of votes).
Note 1
If several countries got the maximal number of votes, consider the country with the lexicographically smaller name among them to be a winner. Similarly if several chefs got the maximal number of votes, consider the chef with the lexicographically smaller name among them to be a winner.
Note 2
The string A = a1a2...an is called lexicographically smaller then the string B = b1b2...bm in the following two cases:
- there exists index i ≤ min{n, m} such that aj = bj for 1 ≤ j < i and ai < bi;
- A is a proper prefix of B, that is, n < m and aj = bj for 1 ≤ j ≤ n.
The characters in strings are compared by their ASCII codes.
Refer to function strcmp in C or to standard comparator < for string data structure in C++ for details.

-----Input-----
The first line of the input contains two space-separated integers N and M denoting the number of chefs and the number of emails respectively. Each of the following N lines contains two space-separated strings, denoting the name of the chef and his country respectively. Each of the following M lines contains one string denoting the subject of the email.

-----Output-----
Output should consist of two lines. The first line should contain the name of the country whose chefs got the most number of votes. The second line should contain the name of the chef who is elected as the "Chef of the Year".

-----Constraints-----
- 1 ≤ N ≤ 10000 (104)
- 1 ≤ M ≤ 100000 (105)
- Each string in the input contains only letters of English alphabets (uppercase or lowercase)
- Each string in the input has length not exceeding 10
- All chef names will be distinct
- Subject of each email will coincide with the name of one of the chefs

-----Example 1-----
Input:
1 3
Leibniz Germany
Leibniz
Leibniz
Leibniz

Output:
Germany
Leibniz

-----Example 2-----
Input:
4 5
Ramanujan India
Torricelli Italy
Gauss Germany
Lagrange Italy
Ramanujan
Torricelli
Torricelli
Ramanujan
Lagrange

Output:
Italy
Ramanujan

-----Example 3-----
Input:
2 2
Newton England
Euclid Greece
Newton
Euclid

Output:
England
Euclid

-----Explanation-----
Example 1. Here we have only one chef Leibniz and he is from Germany. Clearly, all votes are for him. So Germany is the country-winner and Leibniz is the "Chef of the Year".
Example 2. Here we have chefs Torricelli and Lagrange from Italy, chef Ramanujan from India and chef Gauss from Germany. Torricelli got 2 votes, while Lagrange got one vote. Hence the Italy got 3 votes in all. Ramanujan got also 2 votes. And so India got 2 votes in all. Finally Gauss got no votes leaving Germany without votes. So the country-winner is Italy without any ties. But we have two chefs with 2 votes: Torricelli and Ramanujan. But since the string "Ramanujan" is lexicographically smaller than "Torricelli", then Ramanujan is the "Chef of the Year".
Example 3. Here we have two countries with 1 vote: England and Greece. Since the string "England" is lexicographically smaller than "Greece", then England is the country-winner. Next, we have two chefs with 1 vote: Newton and Euclid. Since the string "Euclid" is lexicographically smaller than "Newton", then Euclid is the "Chef of the Year".


You are given a sequence of integers $a_1, a_2, ..., a_N$. An element ak is said to be an average element if there are indices $i, j$ (with $i \neq  j$) such that $a_k = \frac{a_i + a_j}{2}$.
In the sequence
371022171537102217153 \quad 7 \quad 10 \quad 22 \quad 17 \quad 15
for $i=1, j=5$ and $k=3$, we get $a_k = \frac{a_i + a_j}{2}$. Thus $a_3 = 10$ is an average element in this sequence. You can check that $a_3$ is the only average element in this sequence.
Consider the sequence
371031837103183 \quad 7 \quad 10 \quad 3 \quad 18
With $i=1, j=4$ and $k=1$ we get $a_k = \frac{a_i + a_j}{2}$. Thus $a_1=3$ is an average element. We could also choose $i=1, j=4$ and $k=4$ and get $a_k = \frac{a_i + a_j}{2}$. You can check that $a_1$ and $a_4$ are the only average elements of this sequence.
On the other hand, the sequence
38111730381117303 \quad 8 \quad 11 \quad 17 \quad 30
has no average elements.
Your task is to count the number of average elements in the given sequence.

-----Input:-----
The first line contains a single integer $N$ indicating the number of elements in the sequence. This is followed by $N$ lines containing one integer each (Line $i+1$ contains $a_i$). (You may assume that $a_i + a_j$ would not exceed MAXINT for any $i$ and $j$).

-----Output:-----
The output must consist of a single line containing a single integer $k$ indicating the number of average elements in the given sequence.

-----Constraints:-----
- You may assume that $1 \leq N \leq 10000$.
- In $30 \%$ of the inputs $1 \leq N \leq 200$.
- In $60 \%$ of the inputs $1 \leq N \leq 5000$.

-----Sample Input 1:-----
6
3
7
10
17
22
15

-----Sample Output 1:-----
1

-----Sample Input 2:-----
5
3
7
10
3
18

-----Sample Output 2:-----
2

-----Sample Input 3;-----
5
3
8
11
17
30

-----Sample Output 3:-----
0


Ram has invented a magic sequence. Each element of the sequence is defined by the same recursive definition - take some linear combination of previous elements (whose coefficients are fixed) and add to them the n-th powers of some integers. Formally: Xn = Xn-1*a1 + ... + Xn-i*ai + b1*d1^n + ... + bj*dj^n, for some integer constants p,q,a1,...,ap,b1,..., bq,d1,..., dq. Of course, as the values can quickly grow, he computed them modulo a fixed value: 10^6. He wrote many consecutive values of the sequence, but then he lost most of his work. All he has now, is 10 consecutive values taken from somewhere in the sequence (he doesn't know at what n they begin), and the recursive rule. And he would like to recover the sequence, or at the very least, to be able to write the next 10 values taken from the sequence.

-----Input-----
First, two integers, 0<=p<=4, 0<=q<=4. Then come the descriptions of the coefficients, -100 <= a1,...,ap,b1,..., bq,d1,..., dq <= 100. Then, the following 10 integers are Xn,X(n+1),...,X(n+9) for some unknown n.

-----Output-----
10 integers - X(n+10),X(n+11),...,X(n+19)

-----Example-----
Input:

1 1

1

1

1

11 12 13 14 15 16 17 18 19 20

Output:

21 22 23 24 25 26 27 28 29 30

Input:

1 1

1

1

2

1 3 7 15 31 63 127 255 511 1023

Output:

2047 4095 8191 16383 32767 65535 131071 262143 524287 48575

Input:

2 0

1 1

1 1 2 3 5 8 13 21 34 55

Output:

89 144 233 377 610 987 1597 2584 4181 6765


The grand kingdom of Mancunia is famous for its tourist attractions and visitors flock to it throughout the year. King Mancunian is the benevolent ruler of the prosperous country whose main source of revenue is, of course, tourism.

The country can be represented by a network of unidirectional roads between the cities. But Mancunian, our benign monarch, has a headache. The road network of the country is not tourist-friendly and this is affecting revenue. To increase the GDP of the nation, he wants to redirect some of the roads to make the road network tourist-friendly and hence, ensure happiness and prosperity for his loyal subjects.

Now is the time for some formal definitions. :(

A road network is said to be tourist-friendly if for every city in the country, if a tourist starts his journey there, there is a path she can take to visit each and every city of the nation and traverse each road exactly once before ending up at the city where she started.

Given a description of the road network of Mancunia, can you come up with a scheme to redirect some (possibly none) of the roads to make it tourist-friendly?

-----Input-----
The first line contains two integers N and E denoting the number of cities and roads in the beautiful country of Mancunia respectively.
Each of the next E lines contains two integers a and b implying that there is a unidirectional road from city a to city b.

It is guaranteed that there aren't multiple roads in the exact same direction and that there is no road from a city to itself.

-----Output-----
If there is no solution, print "NO" (without quotes). Else, print "YES" (without quotes), followed by exactly E lines.
The ith line of output should represent the ith road in the input. It should have two integers a and b denoting that the final orientation of that road is from a to b.

-----Constraints-----

- 1 ≤ N ≤ 100000
- 1 ≤ E ≤ 200000
- 1 ≤ a, b ≤ N

Subtask 1: (20 points)
- 1 ≤ N ≤ 20
- 1 ≤ E ≤ 21

Subtask 2: (80 points)
- Same as original constraints

-----Example 1-----
Input:
3 3
1 2
2 3
3 1

Output:
YES
1 2
2 3
3 1

-----Example 2-----
Input:
3 2
1 2
2 3

Output:
NO

-----Example 3-----
Input:
5 6
1 2
2 3
3 4
2 4
2 5
1 5

Output:
YES
1 2
2 3
3 4
4 2
2 5
5 1


Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world.
So he and his wife decide to buy a gift for the kid as a token of appreciation.
Unfortunately, the Chef hasn't been doing good business lately, and is in no mood on splurging money.
On the other hand, the boy's mother wants to buy something big and expensive.
To settle the matter like reasonable parents, they play a game.

They spend the whole day thinking of various gifts and write them down in a huge matrix.
Each cell of the matrix contains the gift's cost.
Then they decide that the mother will choose a row number r while the father will choose a column number c,
the item from the corresponding cell will be gifted to the kid in a couple of days.

The boy observes all of this secretly.
He is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row,
but largest in its column.
If no such gift exists, then our little chef has no option but to keep guessing.
As the matrix is huge, he turns to you for help.

He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row,
but largest in its column.
However, since the boy is so smart, he realizes that the gift's cost is determined uniquely.
Your task is to tell him the gift's cost which is smallest in its row,
but largest in its column, or to tell him no such gift exists.

-----Input-----
First line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers - the costs of different gifts.

-----Output-----
Print a single integer - a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print "GUESS" (without quotes of course)

-----Constraints-----
1 <= R, C <= 100
All gift costs are positive and less than 100000000 (10^8)

-----Example 1-----
Input:
2 3
9 8 8
2 6 11

Output:
8

-----Example 2-----
Input:
3 3
9 8 11
2 6 34
5 9 11

Output:
GUESS

-----Example 3-----
Input:
2 2
10 10
10 10

Output:
10

-----Explanation of Sample Cases-----
Example 1: The first row contains 9, 8, 8. Observe that both 8 are the minimum. Considering the first 8, look at the corresponding column (containing 8 and 6). Here, 8 is the largest element in that column. So it will be chosen.
Example 2: There is no value in the matrix that is smallest in its row but largest in its column.
Example 3: The required gift in matrix is not determined uniquely, but the required cost is determined uniquely.


A daily train consists of N cars. Let's consider one particular car. It has 54 places numbered consecutively from 1 to 54, some of which are already booked and some are still free. The places are numbered in the following fashion:

The car is separated into 9 compartments of 6 places each, as shown in the picture. So, the 1st compartment consists of places 1, 2, 3, 4, 53 and 54, the 2nd compartment consists of places 5, 6, 7, 8, 51 and 52, and so on.

A group of X friends wants to buy tickets for free places, all of which are in one compartment (it's much funnier to travel together). You are given the information about free and booked places in each of the N cars. Find the number of ways to sell the friends exactly X tickets in one compartment (note that the order in which the tickets are sold doesn't matter).

-----Input-----
The first line of the input contains two integers X and N (1 ≤ X ≤ 6, 1 ≤ N ≤ 10) separated by a single space. Each of the following N lines contains the information about one car which is a string of length 54 consisting of '0' and '1'. The i-th character (numbered from 1) is '0' if place i in the corresponding car is free, and is '1' if place i is already booked.

-----Output-----
Output just one integer -- the requested number of ways.

-----Example-----
Input:
1 3
100101110000001011000001111110010011110010010111000101
001010000000101111100000000000000111101010101111111010
011110011110000001010100101110001011111010001001111010

Output:
85

Input:
6 3
100101110000001011000001111110010011110010010111000101
001010000000101111100000000000000111101010101111111010
011110011110000001010100101110001011111010001001111010

Output:
1

Input:
3 2
000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000

Output:
360

Explanation:
In the first test case, any of the free places can be sold. In the second test case, the only free compartment in the train is compartment 3 in the first car (places 9, 10, 11, 12, 49 and 50 are all free). In the third test case, the train is still absolutely free; as there are 20 ways to sell 3 tickets in an empty compartment, the answer is 2 * 9 * 20 = 360.


Mike is given a matrix A, N and M are numbers of rows and columns respectively. A1, 1 is the number in the top left corner. All the numbers in A are non-negative integers. He also has L pairs of integers (ik, jk). His task is to calculate Ai1, j1 + Ai2, j2 + ... + AiL, jL.

Unfortunately, Mike forgot if Ai, j was a number in the i'th row and j'th column or vice versa, if Ai, j was a number in the j'th row and i'th column.

So, Mike decided to calculate both E1 = Ai1, j1 + Ai2, j2 + ... + AiL, jL and E2 = Aj1, i1 + Aj2, i2 + ... + AjL, iL. If it is impossible to calculate E1(i.e. one of the summands doesn't exist), then let's assume, that it is equal to -1. If it is impossible to calculate E2, then let's also assume, that it is equal to -1.

Your task is to calculate max(E1, E2).

-----Input-----

The first line contains two integers N and M, denoting the number of rows and the number of columns respectively.

Each of next N lines contains M integers. The j'th integer in the (i + 1)'th line of the input denotes Ai, j.

The (N + 2)'th line contains an integer L, denoting the number of pairs of integers, that Mike has.

Each of next L lines contains a pair of integers. The (N + 2 + k)-th line in the input contains a pair (ik, jk).

-----Output-----
The first line should contain an integer, denoting max(E1, E2).

-----Examples-----
Input:
3 2
1 2
4 5
7 0
2
1 2
2 2
Output:
9

Input:
1 3
1 2 3
2
1 3
3 1
Output:
-1

Input:
1 3
1 2 3
2
1 1
3 1
Output:
4

-----Explanation-----

In the first test case N equals to 3, M equals to 2, L equals to 2. E1 = 2 + 5 = 7, E2 = 4 + 5 = 9. The answer is max(E1, E2) = max(7, 9) = 9;

In the second test case N equals to 1, M equals to 3, L equals to 2. It is impossible to calculate E1 and E2, because A3, 1 doesn't exist. So the answer is max(E1, E2) = max(-1, -1) = -1;

In the third test case N equals to 1, M equals to 3, L equals to 2. It is impossible to calculate E1, because A3, 1 doesn't exist. So E1 is equal to -1. E2 = 1 + 3 = 4. The answer is max(E1, E2) = max(-1,4) = 4.

-----Scoring-----

1 ≤ ik, jk ≤ 500 for each test case.

Subtask 1 (10 points): 1 ≤ N, M, L ≤ 5, 0 ≤ Ai, j ≤ 10;

Subtask 2 (12 points): 1 ≤ N, M, L ≤ 300, 0 ≤ Ai, j ≤ 106, all the numbers in A are equal;

Subtask 3 (20 points): 1 ≤ N, M, L ≤ 300, 0 ≤ Ai, j ≤ 109;

Subtask 4 (26 points): 1 ≤ N, M, L ≤ 500, 0 ≤ Ai, j ≤ 109;

Subtask 5 (32 points): 1 ≤ N, M ≤ 500, 1 ≤ L ≤ 250 000, 0 ≤ Ai, j ≤ 109.


You are given three strings $s$, $t$ and $p$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.

During each operation you choose any character from $p$, erase it from $p$ and insert it into string $s$ (you may insert this character anywhere you want: in the beginning of $s$, in the end or between any two consecutive characters).

For example, if $p$ is aba, and $s$ is de, then the following outcomes are possible (the character we erase from $p$ and insert into $s$ is highlighted):

  aba $\rightarrow$ ba, de $\rightarrow$ ade;  aba $\rightarrow$ ba, de $\rightarrow$ dae;  aba $\rightarrow$ ba, de $\rightarrow$ dea;  aba $\rightarrow$ aa, de $\rightarrow$ bde;  aba $\rightarrow$ aa, de $\rightarrow$ dbe;  aba $\rightarrow$ aa, de $\rightarrow$ deb;  aba $\rightarrow$ ab, de $\rightarrow$ ade;  aba $\rightarrow$ ab, de $\rightarrow$ dae;  aba $\rightarrow$ ab, de $\rightarrow$ dea;

Your goal is to perform several (maybe zero) operations so that $s$ becomes equal to $t$. Please determine whether it is possible.

Note that you have to answer $q$ independent queries.

-----Input-----

The first line contains one integer $q$ ($1 \le q \le 100$) — the number of queries. Each query is represented by three consecutive lines.

The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.

The second line of each query contains the string $t$ ($1 \le |t| \le 100$) consisting of lowercase Latin letters.

The third line of each query contains the string $p$ ($1 \le |p| \le 100$) consisting of lowercase Latin letters.

-----Output-----

For each query print YES if it is possible to make $s$ equal to $t$, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

-----Example-----
Input
4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

Output
YES
YES
NO
NO

-----Note-----

In the first test case there is the following sequence of operation:   $s = $ ab, $t = $ acxb, $p = $ cax;  $s = $ acb, $t = $ acxb, $p = $ ax;  $s = $ acxb, $t = $ acxb, $p = $ a.

In the second test case there is the following sequence of operation:   $s = $ a, $t = $ aaaa, $p = $ aaabbcc;  $s = $ aa, $t = $ aaaa, $p = $ aabbcc;  $s = $ aaa, $t = $ aaaa, $p = $ abbcc;  $s = $ aaaa, $t = $ aaaa, $p = $ bbcc.


You play your favourite game yet another time. You chose the character you didn't play before. It has $str$ points of strength and $int$ points of intelligence. Also, at start, the character has $exp$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $1$ or raise intelligence by $1$).

Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).

Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

-----Input-----

The first line contains the single integer $T$ ($1 \le T \le 100$) — the number of queries. Next $T$ lines contain descriptions of queries — one per line.

This line contains three integers $str$, $int$ and $exp$ ($1 \le str, int \le 10^8$, $0 \le exp \le 10^8$) — the initial strength and intelligence of the character and the number of free points, respectively.

-----Output-----

Print $T$ integers — one per query. For each query print the number of different character builds you can create.

-----Example-----
Input
4
5 3 4
2 1 0
3 5 5
4 10 6

Output
3
1
2
0

-----Note-----

In the first query there are only three appropriate character builds: $(str = 7, int = 5)$, $(8, 4)$ and $(9, 3)$. All other builds are either too smart or don't use all free points.

In the second query there is only one possible build: $(2, 1)$.

In the third query there are two appropriate builds: $(7, 6)$, $(8, 5)$.

In the fourth query all builds have too much brains.


Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.

You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).

-----Input-----

The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.

Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 10^6).

-----Output-----

Print t integers, where i-th integer is the answer to i-th query.

-----Examples-----
Input
3
7 22 1
7 22 2
7 22 3

Output
9
13
15

Input
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46

Output
187
87
139
128
141


You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v.

-----Input-----

The first line contains integer n (1 ≤ n ≤ 10^5) — the number of vertices in the tree.

The second line contains n integers c_{i} (1 ≤ c_{i} ≤ n), c_{i} — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers x_{j}, y_{j} (1 ≤ x_{j}, y_{j} ≤ n) — the edge of the tree. The first vertex is the root of the tree.

-----Output-----

Print n integers — the sums of dominating colours for each vertex.

-----Examples-----
Input
4
1 2 3 4
1 2
2 3
2 4

Output
10 9 3 4

Input
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13

Output
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3


You're given Q queries of the form (L, R).

For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = a^{p}.

-----Input-----

The first line contains the number of queries Q (1 ≤ Q ≤ 10^5).

The next Q lines contains two integers L, R each (1 ≤ L ≤ R ≤ 10^18).

-----Output-----

Output Q lines — the answers to the queries.

-----Example-----
Input
6
1 4
9 9
5 7
12 29
137 591
1 1000000

Output
2
1
0
3
17
1111

-----Note-----

In query one the suitable numbers are 1 and 4.


Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$.

Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$.

Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift.

You are given string $s$ which consists of digits 0–9.

What is the minimum number of characters you need to erase from $s$ to make it good?

-----Input-----

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

Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9.

It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$.

-----Output-----

For each test case, print the minimum number of characters you need to erase from $s$ to make it good.

-----Example-----
Input
3
95831
100120013
252525252525

Output
3
5
0

-----Note-----

In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good.

In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.

In the third test case, the given string $s$ is already good.


Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 ≤ j ≤ m - n. Suppose, c_{i} = a_{i} - b_{i} + j. Then f(j) = |c_1 - c_2 + c_3 - c_4... c_{n}|. More formally, $f(j) =|\sum_{i = 1}^{n}(- 1)^{i - 1} *(a_{i} - b_{i + j})|$.

Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer x_{i} to all elements in a in range [l_{i};r_{i}] i.e. they should add x_{i} to a_{l}_{i}, a_{l}_{i} + 1, ... , a_{r}_{i} and then they should calculate the minimum value of f(j) for all valid j.

Please help Mahmoud and Ehab.

-----Input-----

The first line contains three integers n, m and q (1 ≤ n ≤ m ≤ 10^5, 1 ≤ q ≤ 10^5) — number of elements in a, number of elements in b and number of queries, respectively.

The second line contains n integers a_1, a_2, ..., a_{n}. ( - 10^9 ≤ a_{i} ≤ 10^9) — elements of a.

The third line contains m integers b_1, b_2, ..., b_{m}. ( - 10^9 ≤ b_{i} ≤ 10^9) — elements of b.

Then q lines follow describing the queries. Each of them contains three integers l_{i} r_{i} x_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n,  - 10^9 ≤ x ≤ 10^9) — range to be updated and added value.

-----Output-----

The first line should contain the minimum value of the function f before any update.

Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .

-----Example-----
Input
5 6 3
1 2 3 4 5
1 2 3 4 5 6
1 1 10
1 1 -9
1 5 -1

Output
0
9
0
0

-----Note-----

For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.

After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.

After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.

After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.