2024 ICPC Gran Premio de Mexico Repechaje
A. AAEGGLNU
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Carlos has invented a new language! his new language is called aaegglnu. This new language has the weird characteristic that in each word, all letters are read at the same time! He now wants to teach aaegglnu to his students, and for this he first need to create an ordering for his language and he has decided on the following:

Lets define function $$$f$$$ which given a string $$$S$$$ returns another string $$$S'$$$ which has the same characters as $$$S$$$ but with the characters ordered. For example $$$f(\text{'language'}) = \text{'aaegglnu'}$$$. so in the ordering of aaegglnu, word $$$A$$$ goes before $$$B$$$ if lexicografically $$$f(A)$$$ goes before $$$f(B)$$$. In the case that $$$f(A)=f(B)$$$, $$$A$$$ goes before $$$B$$$ if lexicografically $$$A$$$ is smaller than $$$B$$$.

His students are however still a bit slow when looking for a word in the dictionary Carlos made, so they have asked for your help in this.

Input

In the first line $$$N$$$ ($$$1\leq N\leq 100000$$$) in the next $$$N$$$ lines there will be given $$$N$$$ words.

On the ($$$i+1$$$)-th line the word $$$a_i$$$ from the dictionary($$$1\leq |a_i|\leq 100$$$) which are made from lowercase letters from the english alphabet.

It is assured that the sum of the lengths of the $$$N$$$ words is less than $$$1000000$$$.

In the next line there will be one number $$$Q$$$ ($$$1\leq Q\leq 100000$$$), the number of queries you'll have to answer.

In the next $$$Q$$$ lines there will be one word, $$$b_i$$$ ($$$1\leq |b_i| \leq 100$$$) made of lowercase letters from the english alphabet.

It is assured that the sum of the lengths of the $$$Q$$$ words, is less than $$$1000000$$$.

Output

You'll have to print $$$Q$$$ lines, on the i-th line you'll print how many words from the dictionary are smaller or equal to word $$$b_i$$$

Example
Input
5
language
abcd
dcba
aaegglnu
b
3
bcda
c
egglnuaa
Output
3
5
1

B. Best tests
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Geose is preparing another geometry problem. To complete his task, he needs to write the model solution and the test cases. Currently, he has a convex polygon $$$P$$$ with $$$n$$$ vertices, and he wants to generate additional tests using this polygon.

To create a new test, Geose selects a sub-polygon from $$$P$$$. A sub-polygon is defined as a polygon formed by a subsequence of at least $$$3$$$ vertices from the original polygon, without changing their order. In particular, the original polygon is also considered a sub-polygon.

Second example.

For example, in the figure above, the left polygon corresponds to the second sample, and all three polygons shown are valid sub-polygons of it.

Geose believes that the larger the area of a sub-polygon, the better the quality of the test. Since he is occupied with writing the solution for the problem, he asks for your help in designing the tests. Your task is to compute the areas of all possible sub-polygons of $$$P$$$ and print them in non-increasing order.

If the number of sub-polygons exceeds $$$2\cdot10^5$$$, only print the largest $$$2\cdot10^5$$$ areas, sorted in non-increasing order.

Input

The first line contains $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$).

Each of the following lines contains the coordinates $$$x$$$ and $$$y$$$ of the vertices ($$$0 \leq x,y \leq 10^9$$$). The vertices are given in anticlockwise order. The given polygon is guaranteed to be convex.

Output

In the first line print a single integer $$$m$$$ – the number of found areas. If there are less than $$$2\cdot10^5$$$ sub-polygons, $$$m$$$ must equal the total number of sub-polygons. Otherwise, $$$m=2\cdot10^5$$$.

The second line must contain $$$m$$$ real numbers with exactly one digit after the decimal point – the found areas in non-increasing order. The areas should be printed without any precision error.

Examples
Input
4
0 0
2 0
2 2
0 2
Output
5
4.0 2.0 2.0 2.0 2.0 
Input
6
4 1
8 4
6 7
4 8
1 6
1 3
Output
42
31.0 29.0 27.5 26.5 26.5 24.5 24.5
23.0 22.5 22.0 20.5 20.0 19.0 19.0
18.5 18.0 17.5 17.5 16.0 16.0 15.0
14.5 14.0 14.0 12.0 11.5 11.0 11.0
10.5 10.5 10.5 10.0 9.0 8.5 8.5 7.5
7.0 6.5 4.5 4.5 3.5 2.0 
Note

The areas are printed in multiple lines in the second case for clarity, but you should print all of them in the second line of the output.

C. Conner Reading Session
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Conner is an avid reader. He knows there are two main reasons for reading a good book: first, to enjoy it, and second, to boast about having read it.

However, he faces a problem: every book he has takes time to read. Furthermore, his university library has adopted a policy that allows him to read any book, provided the following conditions are met:

  1. At most, he can have one book on loan; for instance, to borrow a second book, he must have returned the first one.
  2. A book can only be borrowed if it is within the librarian's working hours (from 8 am to 4 pm, for a total of 480 minutes).
  3. If a book is borrowed and not finished by 4 pm, it can be returned later, but the library closes its doors at 9 pm (300 minutes after the librarian's working hours end).

Conner wishes to respect all the rules. He knows that it takes him 3 minutes to read one page (it can be assumed that all pages take the same amount of time to read).

Given a list of books, each with its number of pages, a value indicating how enjoyable it is for Conner, and a number indicating its fame (for which Conner could boast about having read it), you must calculate two values: first, the maximum enjoyment value Conner can achieve from reading, and second, the maximum fame value he can accumulate. All readings must be complete to count and must be done in a single day. If the enjoyment is greater than the fame, then indicate to Conner that he should read for enjoyment; if the opposite is the case, indicate that he should read for fame.

Input

The first line contains a number $$$N$$$ ($$$1\leq N \leq 10^3$$$)

The second line contains $$$N$$$ integer numbers separated by space, indicating the number of pages of the books. $$$1\leq B_i \leq 10^3$$$

The third line has the $$$N$$$ numbers, indicating the pleasure $$$P$$$ $$$1\leq P_i \leq 10^3$$$ that the $$$i-th$$$ book may give

The fourth and last line has the fame of each book, so there are $$$N$$$ integers according to the fame that the book has. $$$1\leq F_i \leq 10^3$$$

Output

Print a single line with the word "EITHER" if the fame and pleasure are the same, "FAME" if Conner would rather the fame, or "PLEASURE" if he would rather pleasure.

Example
Input
4
150 6 5 50
1 2 1 1
1 1 1 1
Output
PLEASURE

D. Dance of Ferrets
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a circle drawn on the ground. The circle has $$$n$$$ equally spaced marks on its perimeter. The $$$i$$$-th mark is said to be adjacent to the $$$(i-1)$$$-th and $$$(i+1)$$$-th marks (if they exist). Marks $$$1$$$ and $$$n$$$ are adjacent as well.

$$$n$$$ ferrets numbered form $$$1$$$ to $$$n$$$ will perform a dance on this circle. Initially, the $$$i$$$-th ferret is standing on the $$$i$$$-th mark. They will dance for $$$2024!^{2024!}$$$ rounds. If a ferret is standing on mark $$$i$$$ at the beginning of a round, it will move to mark $$$p_i$$$ during that round. It is guaranteed that $$$[p_1, p_2, \dots, p_n]$$$ is a permutation.

Some pairs of ferrets are best friends and don't like to be too far from each other for a long time. For that reason, they want to know if they will be standing on adjacent marks at the beginning of any round. Write a program to answer $$$q$$$ queries and help the ferrets to know that. For each query, you are given two integers $$$a$$$ and $$$b$$$, and you must determine if ferrets $$$a$$$ and $$$b$$$ will stand on adjacent marks at the beginning of any round.

Input

The input contains multiple test cases. The first line of the input contains an integer $$$T$$$ ($$$1 \leq T \leq 10^5$$$) – the number of test cases. The description of each test case follows.

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 5 \cdot 10^5$$$ and $$$1 \leq q \leq 5 \cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \dots p_n$$$ ($$$1 \leq p_i \leq n$$$). It is guaranteed that $$$[p_1, p_2, \dots, p_n]$$$ forms a permutation.

The following $$$q$$$ lines contain two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a,b \leq n$$$ and $$$a \neq b$$$).

It is guaranteed that the sum of $$$n$$$ over all the test cases does not exceeds $$$5 \cdot 10^5$$$, and that the sum of $$$q$$$ over all the test cases does not exceeds $$$5 \cdot 10^5$$$.

Output

Print a binary string of size $$$q$$$ for each test case, where the $$$i$$$-th character is '1' if the ferrets in the $$$i$$$-th query will stand on adjacent marks at the beginning of any round, or '0' otherwise.

Examples
Input
1
5 10
2 1 4 3 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
Output
1011101111
Input
2
2 1
1 2
1 2
2 1
2 1
1 2
Output
1
1

E. Expected Closest Friend
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Jorge is a really friendly person, he is so friendly that he has $$$k$$$ friends! sadly he doesn't know where his friends live. He only knows they are all from a different city. The world of Jorge consist of $$$n$$$ cities enumerated from $$$0$$$ to $$$n-1$$$. Between them there are $$$m$$$ roads, with different lengths, that connect a pair of cities, such that at least one path between every pair of cities exist. Jorge lives in city $$$0$$$, and he wants to know the expected value of the distance to his closest friend (The distance to a friend is the length of the shortest path between city $$$0$$$ and his friend's city).

Given the description of the world, and the number of friends Jorge has, answer the value he wants. Assume all possible arrangements of his $$$k$$$ friends are equiprobable.

Input

The first line of input contains three values separated by a space, $$$N$$$ ($$$1\leq N \leq 10^5$$$), $$$M$$$ ($$$1\leq M\leq min(\frac{n*(n-1)}{2},10^6)$$$), and $$$k$$$ ($$$1\leq k \lt N$$$). Each of the next $$$M$$$ lines contains $$$3$$$ values separated by a space, $$$p_i$$$, $$$q_i$$$, and $$$k_i$$$, ($$$0\leq p_i,q_i\leq N-1, 1\leq k_i\leq 1000$$$) meaning there is a road between city $$$p_i$$$ and $$$q_i$$$ with length $$$k_i$$$.

Output

It can be proven that the answer can be represented as a rational number $$$\frac{p}{q}$$$ with coprime $$$p$$$ and $$$q$$$. You need to output $$$p \cdot q^{-1}$$$ mod $$$10^{9}+7$$$.

Example
Input
2 1 1
0 1 3
Output
3

F. Fair Toy Missing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob each bought a bag of toys at the city fair. Each toy bag is supposed to contain exactly 5 different toys, and every bag sold at the fair has the same set of toys.

When they got home and opened their bags, Alice discovered that her bag had all 5 toys, as expected. However, Bob found that his bag only contained 4 toys! He wants to return to the fair to complain, but before doing so, he needs to figure out which toy is missing from his bag.

Your task is to help Bob identify the missing toy.

Input

The first line of input contains 5 integer numbers separated by a space, the toys in Alice's bag.

The second line of input contains 4 integer numbers separated by a space, the toys in Bob's bag.

Each toy is represented by an integer value between 1 and 100, inclusive.

Output

Print a line with a single integer, the toy that is missing in Bob's bag.

Examples
Input
1 2 3 4 5
2 4 5 1
Output
3
Input
10 14 22 32 1
10 14 22 32
Output
1

G. GCDland Mystical Arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the eccentric town of GCDland, there lives a mathematician named Theo, who has a wild and paranoid conspiracy theory. Theo is convinced that in certain mystical arrays, the greatest common divisor (GCD) of every possible pair of numbers is always the same. He believes that these arrays are messages from an ancient civilization trying to communicate with us through numbers.

Theo's friends think he's gone off the deep end, but he's determined to prove them wrong and uncover the truth behind these arrays. He needs your help to validate his theory. Given an array of integers, your task is to determine whether the GCD of every pair of numbers in the array is indeed the same. If Theo's theory holds true for the given array, you should confirm it; otherwise, you should debunk it.

The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder.

Input

The first line contains an integer $$$N$$$ ($$$2 \leq N \leq 100,000$$$), the number of elements in the array.

The second line contains $$$N$$$ integers $$$A_i$$$ ($$$1 \leq A_i \leq 10^7$$$), the elements of the array.

Output

Print "YES" if the GCD of every pair of numbers in the array is the same. Otherwise, print "NO".

Examples
Input
5
10 2 4 12 15
Output
NO
Input
3
2 4 6
Output
YES
Input
4
2 4 6 8
Output
NO

Statement is not available in English language
I. Impossible Octagon Filling
time limit per test
1.8 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It is a well known fact that it's impossible to fill the plane with identical regular octagons, but the math mages didn't knew this till it was too late, they had already ordered an infinite number of them (And they only had to pay $$$-\frac{1}{12}$$$ dollars for them).

So, now, they want to try to fill the plane anyway! To do this they devised the following algorithm, they'll start by putting an octagone anywhere in the plane, they will call this octagon, octagon 0.

From octagon $$$0$$$, they choose one of its sides, and try to put another octagon next to it such that one of its faces coincide with the choosen face, this will be octagon $$$1$$$.

After that they will follow the next procedure:

- Let $$$n$$$ be the number of the last octagone that was put on the plane. From the side that coincides with the octagone $$$n-1$$$ we start trying to put octagone $$$n+1$$$ next to that side, if it is not possible, we try the next side counterclockwise, until it is possible to put a new octagone without overlapping another octagone that was put before.

It can be proven that this procedure allows you to put an infinite number of octagons in the plane.

Before starting, they want to know what is the squared distance from the center of octagone $$$k$$$ to the center of octagone $$$0$$$

Input

The first line contains an integer $$$Q$$$ ($$$1\leq Q\leq 10^6$$$), the number of queries.

Each of the next $$$Q$$$ lines, contains an integer $$$1\leq a_k \leq 10^12$$$ the number of the octagone they want to know its distance to center of octagone 0.

Output

Output $$$Q$$$ lines, where the $$$i$$$-th line contains a number indicating the distance between the center of octagone $$$a_i$$$ and the center of octagone $$$0$$$.

Example
Input
2
1
100
Output
4
200
Note

The distance from the center of the octagone to the center of any of its sides, is 1.

J. Just Deer Cookies
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The new girl in school Shikanoko is a bit weird, she has deer antlers and loves to eat deer cookies! Her friend Koshi has found out that the store next to the school has started selling packages of human and deer cookies. All packages have the same number of cookies, and both the human and deer cookies in the same order. A package can be expressed as a binary string where $$$1$$$ represents a deer cookie, and $$$0$$$ represents a human cookie, so for example $$$1101$$$ means the first and second cookie in the package will be deer cookies, and the third will be a human cookie.

Shikanoko wants Koshi to share a package with her, and she will be happy if she eats all of the deer cookies, and Koshi eats all the human cookies. She also wants, that at any moment, she has eaten at least the same number of cookies as Koshi.

Koshi, doesn't want to look violent, so she will only grab cookies from either ends of the packages. For example, Koshi won't eat or give the third cookie to Shikanoko if neither the second nor the fourth cookie has been eaten before.

Koshi knows she'll have to do this daily, so she has decided to try and pick a different order in which the cookies are eaten each day. An order is considered different than another if for at least two different cookies $$$i$$$ and $$$j$$$, the cookie $$$i$$$ is eaten before cookie $$$j$$$ in one order, and in the other cookie $$$j$$$ is eaten before cookie $$$i$$$.

Koshi will be bored the day that the order in which cookies are eaten has been repeated in another day before. Find the maximum possible day in which Koshi will be bored for the first time.

Input

In the first line a number $$$N$$$ ($$$1\leq N \leq 10000$$$) will be given which represents the number of cookies in the package.

In the second line a binary string $$$M$$$ with length $$$N$$$ will be given, the order of the cookies in the packages.

Output

Just one number, the maximum number in which Koshi will be bored for the first time. As this number can be very big, print it module $$$10^9+7$$$

Examples
Input
2
10
Output
2
Input
3
101
Output
5
Note

"Why do you eat deer cookies? because they are right there!"

K. Kitchen Closing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's a busy evening at Chef Elisa's restaurant, and the kitchen is working tirelessly to fulfill the orders of hungry clients. Elisa is known for her creativity, using a wide variety of fresh ingredients to craft her famous dishes. The kitchen is stocked with $$$N$$$ different ingredients, each available in limited quantities, and the staff has prepared a menu of $$$M$$$ signature dishes, each requiring specific ingredients to make.

A large crowd has gathered, and the kitchen is being flooded with orders, each containing requests for multiple dishes. Elisa is determined to fulfill the orders in the order they are received, but there's a catch: the kitchen only has so many ingredients, and they must be used carefully.

As the evening progresses, Elisa notices that the ingredients are running low. The kitchen staff must figure out how many consecutive orders they can fulfill, starting from the first, before they can no longer make the requested dishes due to a lack of ingredients. If the kitchen does not have enough ingredients to plate all the dishes from an order, then that order can not be fulfilled, once an order cannot be fulfilled, the kitchen will close for the night

Your task is to help Elisa and her staff determine how many orders can be processed in the order they arrive, before they run out of ingredients and are unable to fulfill the next order.

Input

The first line of input contains three integer numbers separated by a space $$$N$$$ ($$$1 \leq N \leq 100$$$), $$$M$$$ ($$$1 \leq M \leq 100$$$), and $$$O$$$ ($$$1 \leq O \leq 100$$$), representing the number of ingredients in the kitchen, the number of dishes in the menu, and the number of orders to be processed.

The second line contains $$$N$$$ integer number separated by a space, representing the quantity $$$q_i$$$ ($$$1 \leq q_i \leq 10000$$$) that the kitchen has available for the $$$i$$$-th ingredient.

$$$M$$$ descriptions of dishes follow. Each description starts with a line that contains an integer number $$$m_i$$$ ($$$1 \leq m_i \leq N$$$), representing the number of different ingredients required to serve dish $$$i$$$, followed by $$$m_i$$$ lines, each of these lines contains two integer numbers separated by a space $$$I_i$$$ ($$$1 \leq I_i \leq N$$$) and $$$q_{m_i}$$$ ($$$1 \leq q_{m_i} \leq q_{I_i}$$$), representing the $$$i-th$$$ ingredient and the quantity of the ingredient required to make that dish.

Each of the next $$$O$$$ lines describe an order, each order starts with an integer $$$o_i$$$ ($$$1 \leq o_i \leq 100$$$), representing the number of dishes in the order, followed by a list of $$$o_i$$$ integer numbers where each integer $$$d_i$$$ ($$$1 \leq d_i \leq M$$$) represents a dish in the order.

Output

Print a single integer number, the number of orders the kitchen can fulfill before closing.

Examples
Input
1 2 3
10
1
1 1
1
1 2
5 2 2 2 2 2
1 1
2 1 1
Output
1
Input
1 2 3
10
1
1 1
1
1 2
5 1 1 1 1 1
1 1
2 1 1
Output
3
Input
2 2 3
12 10
1
1 1
2
1 2
2 2
5 2 2 2 2 2
1 1
2 1 2
Output
2