ICPC Thailand National Competition 2024
A. Auntie's Magical Cake
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Your aunt brings you a cake consisting of $$$N$$$ pieces arranged in a straight line, labeled as cakes $$$1, 2, 3, \ldots, N$$$. Initially, each piece of cake has a deliciousness value of $$$0$$$. You can choose to eat the $$$i^{th}$$$ piece of cake, provided that neither of its adjacent pieces has been eaten and that the $$$i^{th}$$$ piece is not at the edge. Once you eat a piece of cake, it will distribute deliciousness to its adjacent pieces, keeping going until it hits the edge or its adjacent pieces have already been eaten. Once the cake distributes deliciousness to all the pieces it can, for every $$$j^{th}$$$ piece that got distributed in the previous step, its deliciousness value will increase by $$$|i-j|$$$ .

You will continue to eat cake until there are no more pieces that can be eaten under the given conditions. At that point, you can use a special ability that allows you to eat all the remaining pieces at once. Do you want to know how much deliciousness you can consume to maximize the total deliciousness of the cake?

Input

The first line contains $$$N$$$, representing the number of cake pieces. $$$(1 \leq N \leq 1,000,000)$$$

Output

Output one integer representing the maximum deliciousness.

Examples
Input
1
Output
0
Input
2
Output
0
Input
3
Output
2
Input
4
Output
4
Input
10
Output
74
Note

In the first and second examples, you can't eat any cake under the given conditions, so the answer is $$$0$$$.

In the third example, you can eat the $$$2^{nd}$$$ piece of cake, and it will distribute deliciousness to the other $$$2$$$ pieces. Then, you can eat all of the remaining cake pieces and get a total deliciousness value of $$$2$$$.

In the fourth example, you can eat the $$$3^{rd}$$$ piece of cake, which will distribute $$$1$$$ deliciousness to the $$$2^{nd}$$$ and $$$4^{th}$$$ pieces and $$$2$$$ deliciousness to the $$$1^{st}$$$ piece. After that, you can eat all the remaining pieces and achieve a total deliciousness value of $$$4$$$.

For the third and fourth examples, the cake will distribute deliciousness like the following figures.

Fig. A.1: Illustration of the cakes in the third and fourth examples.

B. Back in the Day
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Your father once shared with you a thrilling story from his days as a spy during a family reunion. He had traveled around the world gathering critical information, and one of his favorite memories was from a mission that required him to infiltrate a secret organization.

On that mission, his objective was to retrieve a report hidden in the deepest vault of a heavily secured basement. To access the vault, he had to crack a keypad, which required a passcode composed of English letters. Although he didn't know the passcode, luck was on his side. While hiding in a nearby vent, he overheard the organization's boss entering the code. He couldn't see the keypad, but he could still hear the sounds of the keys being pressed.

The keypad worked like the multi-tap system found on old phones, where pressing a key multiple times cycles through the letters associated with that key. If, for example, the passcode was $$$cat$$$, the boss would have to press the $$$2$$$ key three times for $$$c$$$, pause, press $$$2$$$ again for $$$a$$$, pause, and then press $$$7$$$ once for $$$t$$$.

Figure B.1: Examples of the keypad

Typically, pressing a key more times than the number of characters it represents will cycle back to the first character. However, the boss was extremely cautious and never pressed a key too many times. For instance, he would never press the $$$2$$$ key four times to type the letter $$$a$$$.

Each key produced a distinct pitch, and your father, with his keen ear, could identify which buttons were pressed. He jotted down the tapping sequence in his notebook but didn't record the pauses. For instance, if the passcode was $$$cat$$$, he would have written down $$$22228$$$.

Later, he realized he could simply have slipped into the vault before the door shut completely. Using his stealth skills, he managed to enter the vault, retrieve the report, and escape the building without being detected.

A few days after the mission, curiosity got the better of him. He looked back at the tapping sequence he had noted and wondered about the passcode. Now you are also intrigued; what passcode could it possibly be ?

Input

One line contains a single non-empty string consisting of the numbers from $$$2$$$ to $$$9$$$. The length of this string will not be greater than $$$200$$$ characters.

Output

Print the passcode string that corresponds to the given tapping sequence. If there are multiple such strings, print the shortest one. If there are still multiple such strings, then print the lexicographically smallest of them.

For two strings $$$a$$$ and $$$b$$$, $$$a$$$ is lexicographically smaller than $$$b$$$ if $$$a$$$ is a prefix of $$$b$$$ or in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. For example, $$$code$$$ is lexicographically smaller than $$$cow$$$.

Examples
Input
4442227222
Output
icpc
Input
22228
Output
act
Input
66666666355533
Output
noodle
Note

In the second example, both $$$aaaat$$$ and $$$cat$$$ also have a tapping sequence of $$$22228$$$, but $$$act$$$ is shorter than $$$aaaat$$$ and is lexicographically smaller than $$$cat$$$.

C. Cattering
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Throughout heaven and earth, I alone am the catter one.

— Jack probably never said this.

After returning from catching grasshoppers, Jack finds his cat, Salmon, has brought $$$N$$$ of his friends(cats), numbered $$$1$$$ to $$$N$$$, to the house. He decides to prepare food for all the cats.

Aside from the food dedicated for Salmon, there are currently $$$M$$$ types of cat food in his house ($$$M \geq N$$$). Jack is a proud "catterer", a person who specializes in catering to cats. He can estimate how much happiness each cat has eating each type of food. Formally speaking, Jack can find a matrix $$$A$$$ size $$$N \times M$$$ where $$$A_{ij}$$$ is how much happiness cat $$$i$$$ has when eating food type $$$j$$$.

The problem is, cats refuse to have the same types of food. He must prepare one type of food among $$$M$$$ types for each of $$$N$$$ cats such that there are no pairs of cats that receive the same food. He wants to $$$\textbf{maximize the minimum happiness among all cats}$$$. That is, if cat $$$i$$$ gets the food type $$$p_i$$$, he wants to $$$\textbf{maximize}\ min(A_{1p_1},A_{2p_2},\dots,A_{Np_N})$$$. Help him find the way to prepare foods to achieve his objective.

Salmon blessed you good luck for the contest.
Input

The first line contains two integers $$$N, M\ (1 \leq N \leq M \leq 1,000)$$$ — the number of cats and the number of food types, respectively.

The $$$i^{th}$$$ of the following $$$N$$$ lines contains $$$M$$$ integers $$$A_{i1},A_{i2},\dots,A_{iM}\ (1 \leq A_{i1},A_{i2},\dots,A_{iM} \leq 10^9)$$$ — the happiness cat $$$i$$$ has eating food type $$$1,2,\dots,M$$$, respectively.

Output

For the first line, output one integer — the maximum value of the minimum happiness among all cats.

For the second line, output $$$N$$$ integers $$$p_1,p_2,\dots,p_N$$$ — the food type for cat $$$1,2,\dots,N$$$, respectively.

Examples
Input
3 3
1 2 3
1 3 2
3 1 2
Output
3
3 2 1
Input
3 4
3 1 1 2
2 3 1 1
3 3 1 1
Output
2
4 2 1
Note

In the first testcase, Jack can feed food type $$$3$$$ to cat $$$1$$$, food type $$$2$$$ to cat $$$2$$$, and food type $$$1$$$ to cat $$$3$$$. They will all have happiness value $$$3$$$.

In the second testcase, the minimum value cannot be $$$3$$$ because only cat food types $$$1$$$ and $$$2$$$ can make happiness value $$$3$$$. By giving food type $$$4,\ 2,\ 1,$$$ respectively, the happiness values will be $$$2,\ 3,\ 3,$$$ respectively, which makes the least happiness $$$2$$$.

D. Disinfection Patch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

A hospital in some city has been researching technology which can completely revolutionize the current remedial methods. For this purpose, the simulation was created to evaluate the cost.

There is a disinfection patch with coordinate $$$(0,0)$$$ as the center of the patch. The $$$N$$$ drops of disinfection substances will be applied on the patch coordinate $$$(x_i,y_i)$$$. The $$$N$$$ types of bacteria have also been placed on coordinate $$$(a_i,b_i)$$$. All of the substances can disinfect any type of bacteria, but can only disinfect 1 type of bacteria per drop, and only disinfect at the point that the substances reside.

The disinfection patch can also be stretched out with the same non-negative integer $$$S$$$ both horizontally and vertically and can't be rotated or flipped. After that, the disinfection patch center can also be placed on any integer coordinates. The disinfection substances coordinate will also be changed as follows.

1. If the original coordinate for substance is $$$(p,q)$$$, then the new substance coordinate is $$$(S\cdot p,S\cdot q)$$$.

2. Assume that the center of the patch is $$$(X,Y)$$$, then the new substance coordinate is $$$(S\cdot p+X,S\cdot q+Y)$$$.

To simulate real-life situations for reducing cost, only one disinfection patch was given to you. Your task is to determine the value of $$$S,X,Y$$$ that can disinfect all of the bacteria for each simulation.

Input

The first line contains two integers $$$N$$$ $$$(1 \leq N \leq 2,000)$$$ — the number of drops of disinfection substances.

The $$$i^{th}$$$ line of the following $$$N$$$ lines contains integer $$$x_i,y_i$$$ $$$(|x_i|,|y_i| \leq 10^9)$$$ — the coordinate of the $$$i^{th}$$$ drop of disinfection substance.

The $$$i^{th}$$$ line of the following $$$N$$$ lines contains integer $$$a_i,b_i$$$ $$$(|a_i|,|b_i| \leq 10^9)$$$ — the coordinate of the $$$i^{th}$$$ type of bacteria.

Output

The first line contains an integer $$$S,X,Y$$$, the stretched out scale with value in range $$$[0,2\cdot 10^9]$$$, and the center coordinate for the disinfection patch with $$$X$$$ and $$$Y$$$ in range $$$[-4\cdot 10^{18},4\cdot 10^{18}]$$$. If there are multiple solutions, output any of them.

If there is no plausible solution, output $$$-1$$$ for the line.

Examples
Input
5
1 1
2 2
3 3
4 4
5 5
4 5
7 8
10 11
16 17
13 14
Output
3 1 2
Input
5
1 1
2 2
3 3
4 4
5 5
-1 1
-2 2
-3 3
-4 4
-5 5
Output
-1
Input
5
-1 1
0 1
1 1
-1 0
1 0
-10 -17
-7 -17
-7 -20
-13 -20
-13 -17
Output
3 -10 -20
Input
4
1 1
1 2
1 3
1 4
2 5
2 5
2 5
2 5
Output
0 2 5
Input
5
-1 1
-1 1
-1 1
-1 1
-1 4
-1 1
-1 1
-1 1
-1 4
-1 4
Output
-1
Note

In the last example, if $$$S=1,X=0,Y=0$$$, then coordinate $$$(-1,4)$$$ will have 1 drop of disinfection substances while having 2 types of bacteria.

E. Executive's Holidays
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

A company is in the process of designing a groundbreaking new game and has decided to hold several executive meetings to kick off the project.

Over the next $$$t$$$ days, the company needs to schedule $$$n$$$ executives to attend these meetings. On any given day, $$$i$$$, the company requires at least $$$a_i$$$ executives to be present.

Each executive's "satisfaction" is determined by the length of their longest continuous break from attending meetings. For instance, if an executive attends meetings on days $$$1$$$, $$$5$$$, and $$$7$$$ during a $$$7$$$-day period, their satisfaction value would be 3, representing the break they get from day $$$2$$$ to day $$$4$$$. If an executive does not attend any meetings during the entire period, their satisfaction value would be $$$t$$$.

You have been tasked by the CEO of the company to determine the maximum total satisfaction of all executives.

Input

The first line contains two integers $$$n,t$$$ $$$(1 \le n,t \le 2,000,000)$$$ denoting the number of executives in total and the number of days of meetings.

The second line contains $$$t$$$ integers $$$a_1, a_2,...,a_t$$$ $$$(0 \le a_i \le n)$$$ — the minimum number of executives required on the $$$i$$$-th day.

Output

Output the maximum sum of satisfaction of all the executives.

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

In the first example, one way to arrange the meeting is as follows:

  • Executive $$$1$$$ attends the meeting on day $$$2$$$, $$$3$$$ and $$$4$$$, having the satisfaction value of $$$3$$$.
  • Executive $$$2$$$ attends the meeting on day $$$4$$$ and $$$5$$$, having the satisfaction value of $$$3$$$.
  • Executive $$$3$$$ attends the meeting on day $$$4$$$, $$$5$$$ and $$$7$$$, having the satisfaction value of $$$3$$$.
The total satisfaction is $$$3+3+3=9$$$.

F. Fill T
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

It is the last day of your summer break, and you currently have nothing to do, so you start daydreaming about a problem you had heard once before...

Given a grid with $$$R$$$ rows and $$$C$$$ columns, your task is to cover all the cells with some $$$T$$$-shapes so that each cell is covered by exactly one $$$T$$$-shape. $$$T$$$-shapes must not exceed the boundary of the grid.

More formally, a $$$T$$$-shape in the grid is uniquely determined by six non-negative integers $$$(x,y,u,l,d,r)$$$, where $$$(x,y)$$$ represents the center point of the $$$T$$$-shape, and $$$u$$$, $$$l$$$, $$$d$$$ and $$$r$$$ represent the lengths of the arms of the $$$T$$$-shape in the upward, leftward, downward and rightward directions, respectively.

The six integers must satisfy these conditions:

  • $$$0 \le u,l,d,r$$$, $$$1 \le x \le C$$$, $$$1 \le y \le R$$$
  • $$$1 \le x-l$$$ and $$$x+r \le C$$$
  • $$$1 \le y-u$$$ and $$$y+d \le R$$$
  • Exactly one of the values of $$$u$$$, $$$l$$$, $$$d$$$ and $$$r$$$ must be zero.
Figure F.1: Examples of some $$$T$$$-shapes
Input

Each test contains multiple test cases. The first line contains a single integer $$$t(1\leq t \leq 1,000)$$$ — the number of test cases.

For each test case, there is one line that contains two integers $$$R$$$ and $$$C$$$ $$$(1 \le R,C \le 200)$$$ indicating the size of the grid.

The sum of $$$R \cdot C$$$ over all test cases does not exceed $$$100,000$$$.

Output

For each test case, on the first line, output $$$n$$$ $$$(1 \le n \le \frac{R \cdot C}{4})$$$, the number of $$$T$$$-shapes to cover the grid.

On each of the next $$$n$$$ lines, output six integers $$$x_i, y_i, u_i, l_i, d_i, r_i$$$ separated by a space indicating the $$$i$$$-th $$$T$$$-shape uniquely determined by $$$(x_i, y_i, u_i, l_i, d_i, r_i)$$$ where $$$x_i$$$ and $$$y_i$$$ $$$(1 \le x_i \le C$$$, $$$1 \le y_i \le R)$$$ represents the center point and $$$u_i$$$, $$$l_i$$$, $$$d_i$$$ and $$$r_i$$$ $$$(0 \le u_i,l_i,d_i,r_i)$$$ represent the arms of the $$$T$$$-shape as explained above. If there are multiple valid answers, you can print any of them.

If there is no valid answer, simply output $$$-1$$$ on a single line.

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

The first and the second grid in the example can be filled with $$$T$$$-shapes as follows:

G. Glory Road
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

In ancient times, three neighboring kingdoms — Antheon, Beelor, and Cicadale —were ruled by close friends.

To symbolize their friendship, they constructed three straight roads linking the kingdoms together. The first road connecting Antheon to Beelor had Statue of Unity exactly at the halfway of it. The second road connecting Beelor to Cicadale had Statue of Friendship exactly at the halfway of it. The third road connecting Antheon to Cicadale had Statue of Harmony exactly at the halfway of it.

For example, if Antheon is located at $$$(1,2)$$$, Beelor at $$$(3,6)$$$ and Cicadale at $$$(7,4)$$$, then:

  • Statue of Unity would be at $$$(\frac{1+3}{2},\frac{2+6}{2})=(2,4)$$$.
  • Statue of Friendship would be at $$$(\frac{3+7}{2},\frac{6+4}{2})=(5,5)$$$.
  • Statue of Harmony would be at $$$(\frac{1+7}{2},\frac{2+4}{2})=(4,3)$$$.
.
Figure G.1: Illustration of the location of the kingdoms and the statues corresponding to the example above

Tragically, the kingdoms were eventually wiped out by a devastating onslaught of pesticides, leading to their complete destruction. Yet, the three ancient statues somehow withstood the test of time, standing as the only surviving remnants of these lost civilizations.

Now, an archaeologist is determined to find the original locations of these lost kingdoms. He believes that the statues hold the clues to uncovering where the kingdoms once were. Your task is to help him figure out the locations of the kingdoms based on the positions of the surviving statues.

Input

The first line of the input contains two integers $$$x$$$ and $$$y$$$ $$$(|x|,|y|\le 100)$$$ — the coordinates of Statue of Unity.

The second line of the input contains two integers $$$x$$$ and $$$y$$$ $$$(|x|,|y|\le 100)$$$ — the coordinates of Statue of Friendship.

The third line of the input contains two integers $$$x$$$ and $$$y$$$ $$$(|x|,|y|\le 100)$$$ — the coordinates of Statue of Harmony.

Output

Print three lines; the first line contains the coordinates of kingdom Antheon, the second line contains the coordinates of kingdom Beelor, and the third line contains the coordinates of kingdom Cicadale.

It can be proven that the coordinates of all three kingdoms are always integers.

Examples
Input
2 4
5 5
4 3
Output
1 2
3 6
7 4
Input
-3 -4
2 5
3 -2
Output
-2 -11
-4 3
8 7

H. Heavenly Sequence
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A sequence $$$b[1],b[2],b[3],\dots,b[n]$$$ is $$$\textbf{good}$$$ if and only if, $$$n=1$$$ $$$\textbf{or}$$$, for some $$$\textbf{non-negative integers}$$$ $$$K$$$ and $$$X$$$, the sequence satisfies: $$$$$$\displaystyle K \cdot \min(b[1],b[2],\dots,b[n]) + X \cdot \max(b[1],b[2],\dots,b[n]) \geq b[n]^2 + \sum_{i=1}^{n-1} b[i] \cdot b[i+1]$$$$$$

A sequence $$$b[1],b[2],b[3],\dots,b[n]$$$ is $$$\textbf{best}$$$ if and only if $$$\textbf{every subsequence}$$$ of $$$b[1],b[2],b[3],\dots,b[n]$$$ is $$$\textbf{good}$$$.

A sequence $$$b[1],b[2],b[3],\dots,b[n]$$$ is $$$\textbf{heavenly}$$$ if and only if $$$\textbf{every permutation}$$$ of $$$b[1],b[2],b[3],\dots,b[n]$$$ is $$$\textbf{best}$$$.

Given the sequence $$$a[1],a[2],a[3],...,a[n]$$$, you have to answer $$$n-1$$$ questions. The $$$i$$$-th question is " Given integers $$$L$$$ and $$$R$$$, what is the minimum $$$\textbf{non-negative integer}$$$ $$$K$$$ that makes the sequence $$$a[1],a[2],a[3],\dots,a[i+1]$$$ $$$\textbf{heavenly}$$$, if you can choose any integer $$$X$$$ from the given range $$$L$$$ to $$$R$$$ ? "

The $$$\textbf{subsequence}$$$ of a sequence can be obtained by erasing some (possibly zero) elements from the array. (not necessary to remove consecutive elements)

Input

The first line contains integer $$$n$$$ $$$(2 \leq n \leq 100,000)$$$ — the length of the sequence

The second line contains $$$a[1], a[2], a[3], \dots, a[n]$$$ $$$(1 \leq a[i] \leq 1,000,000)$$$ — the elements of the sequence

The next $$$n-1$$$ lines contain integers $$$L$$$ and $$$R$$$ $$$(1 \leq L \leq R \leq 1,000,000)$$$ — the range of an integer $$$X$$$ that you can choose in each question

Output

Output $$$n-1$$$ lines, each line contains a single value — the $$$i$$$-th value is the smallest $$$\textbf{non-negative integer}$$$ $$$K$$$ that makes the sequence $$$a[1],a[2],a[3],\dots,a[i+1]$$$ become $$$\textbf{heavenly}$$$, if you can choose any integer $$$X$$$ from the given range $$$L$$$ to $$$R$$$

Examples
Input
4
8 4 5 6
3 4
4 4
20 20
Output
16
23
1
Input
6
12 15 8 9 20 16
9 9
10 14
6 8
40 50
67 67
Output
23
37
59
8
0
Note

For the first question of the first testcase, the sequence is $$$(8,4)$$$ and we can choose $$$X=3$$$ or $$$X=4$$$ and find the smallest non-negative integer $$$K$$$ such that $$$(8,4)$$$ is a $$$\textbf{heavenly}$$$ sequence, which is to make $$$(4)$$$, $$$(8)$$$, $$$(4,8)$$$ and $$$(8,4)$$$ become $$$\textbf{good}$$$. We can choose $$$X=4$$$ and $$$K=16$$$ to satisfy the condition and $$$K=16$$$ is the smallest value of $$$K$$$ that makes $$$(8,4)$$$ become $$$\textbf{heavenly}$$$, so the answer to this question is $$$16$$$.

For the last question of the second question, the smallest integer $$$K$$$ that makes the sequence become $$$\textbf{heavenly}$$$ is a negative integer, but we want the smallest $$$\textbf{non-negative integer}$$$ $$$K$$$, so the answer to this is $$$0$$$.

I. Ideal Permutation Pairing
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

A permutation of size $$$N$$$ is a sequence of $$$N$$$ positive integers containing the numbers $$$1$$$ to $$$N$$$ where each number appears exactly once in the sequence. For example, $$$[1,4,5,3,2]$$$ is a permutation of size $$$5$$$, while $$$[1,1,4,3,5]$$$ and $$$[1,6,2,3,4]$$$ are not.

We can compare two permutations using lexicographical order. For two permutations $$$p$$$ and $$$p$$$ of the same size $$$N$$$ where $$$p = [a_1,a_2,...,a_N]$$$ and $$$q = [b_1,b_2,...,b_N]$$$, we define $$$p$$$ is smaller than $$$q$$$ if and only if there exists an index $$$i$$$ such that $$$a_j=b_j$$$ for every $$$j \lt i$$$ and $$$a_i \lt b_i$$$. For example, $$$[1,3,4,2,5]$$$ is ordered before $$$[1,3,5,2,4]$$$.

Two permutations $$$p$$$ and $$$q$$$ of size $$$N$$$ are considered an ideal pair if, when all $$$N!$$$ permutations of size $$$N$$$ are arranged in a circle based on lexicographic order, permutation $$$p$$$ and permutation $$$q$$$ are positioned directly opposite each other in the circle.

Figure I.1: Example of the circle of permutations of size $$$3$$$.

Given a permutation $$$p$$$, your task is to identify the permutation $$$q$$$ that together forms an ideal pair.

Input

The first line contains one integer $$$N$$$ $$$(2\le N \le 1,000,000)$$$ — size of the permutation.

The second line contains $$$N$$$ distinct integers from $$$1$$$ to $$$N$$$ — the given permutation $$$p$$$.

Output

One line contains $$$N$$$ distinct integers that are the permutation that forms an ideal pair with permutation $$$p$$$.

Examples
Input
3
1 2 3
Output
2 3 1
Input
4
3 2 1 4
Output
1 3 2 4
Input
9
7 4 6 9 5 3 8 2 1
Output
2 9 6 8 5 4 7 3 1

J. Jewel Collection
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A billionaire with a vast and diverse collection of jewels, each featuring up to two distinct colors, has decided to showcase his collection in a grand exhibition. His aim is to arrange the jewels so that every color present in his collection is prominently displayed.

He wants to ensure that every color is uniquely represented by one jewel. If a jewel has the color $$$c_1$$$ and $$$c_2$$$, and is used to represent the color $$$c_1$$$, it cannot be used again to represent the color $$$c_2$$$, and vice versa.

In addition to showcasing every color, the billionaire wants to flaunt his wealth by maximizing the total price of the displayed jewels as well.

Your task is to assist him in selecting the jewels for the exhibition according to these criteria.

Input

The first line contains two integers $$$N$$$ and $$$C$$$ $$$(1 \le N,C \le 200,000)$$$ — the number of jewels, and the number of colors, respectively.

The $$$i^{th}$$$ line of the following $$$N$$$ lines describes the $$$i$$$-th jewel in the format of $$$t$$$ $$$c_1$$$ $$$c_2$$$ ... $$$c_t$$$ $$$v$$$ $$$(1\le t \le 2, 1 \le c_1 , c_2 , \dots , c_t \le C, 1 \le v \le 1,000,000)$$$ — the $$$i$$$-th jewel has $$$t$$$ colors: $$$c_1$$$, $$$c_2$$$, ... $$$c_t$$$ with the price value of $$$v$$$.

Output

The first line contains an integer $$$W$$$, the maximum total price.

The second line contains $$$C$$$ non-repeated integers, the jewel to display in the exhibition for each color from color $$$1$$$ to color $$$C$$$, respectively. If there are multiple possible rearrangements, output any of them.

If it is impossible to set up the display, output $$$-1$$$.

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

In the second example, it is impossible because there is no jewel with color $$$2$$$.

K. Kid Rally
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

The primary school main event for every year is field trips, mainly consisting of a group of energized kids, organizing a special activity for this year.

The activity is to rally to the different places on a map size $$$N\times M$$$ in order to collect the score; each position is assigned a score $$$S_{i,j}$$$ for position $$$(i,j)$$$. The position can be considered as a point in $$$XY$$$ coordinate. All of the kids will be given a map and choose a partner to form a pair. Each person in the pair will start in position $$$(1,1)$$$ and $$$(1,M)$$$ before venturing on their own.

With consideration of limited time and the risk of injury, the school imposes four special rules for the activity.

1. If the kid stays at position $$$(x,y)$$$, this kid mustn't rally to position $$$(a,b)$$$ when $$$(a \leq x)$$$.

2. The path to rally from one position to another position must be a straight line.

3. The pairs' rally paths mustn't intersect at any point of the path regardless of the time that they reach each position.

4. The kid must stay in the area of the map and can choose to finish their rally at any place on the map.

The score of an individual person is the sum of the score on the persons' rally path. The final score for this activity is calculated by the multiplication of the pairs' scores. Alice and Bob want to form a pair and have the highest score in order to boast to their friends.

Your task is to assist Alice and Bob in reporting the maximum possible score that Alice and Bob can achieve.

Input

The first line contains two integers $$$N$$$ and $$$M$$$ $$$(2 \leq N,M \leq 1,000)$$$ — the rows and columns of the map respectively.

The $$$i^{th}$$$ line of the following $$$N$$$ lines describes the $$$i$$$-th row of the map in the format of $$$S_{i,1}$$$ $$$S_{i,2}$$$ $$$S_{i,3}$$$ ... $$$S_{i,M}$$$ $$$(0\leq S_{i,j} \leq 9)$$$ — the $$$i$$$-th row of the map consists of $$$M$$$ places with score $$$S_{i,j}$$$.

Output

The first line contains an integer $$$W$$$, the maximum possible score that Alice and Bob can achieve.

Example
Input
3 5
9 1 3 2 5
0 0 9 0 0
3 1 2 6 1
Output
240
Note

Alice's rally path is $$$(1,1) \rightarrow (3,1)$$$ which earns a score of 12.

Bob's rally path is $$$(1,5) \rightarrow (2,3) \rightarrow (3,4)$$$ which earns a score of 20.

Alice can't choose the rally path $$$(1,1) \rightarrow (3,4)$$$ and Bob can't choose the rally path $$$(1,5) \rightarrow (2,3) \rightarrow (3,1)$$$ because there is an intersection between the two rally paths.

L. Lulu and Friends
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Let's put on our thinking caps! Hmm... hmmhmm, hmmhmm... ah, I got it! — Lulu

As the sun rose over Runeterra, Lulu the yordle skipped through the land, always on the lookout for magic and adventure. Today, she discovered something extraordinary — a glowing thread nestled in the roots of an ancient tree.

"This must be the Legendary String $$$T$$$!" Lulu gasped, remembering the tales of its magical properties. Holding the string, Lulu's mind flooded with memories of her adventures and the friends she'd met along the way.

But this wasn't just a simple discovery. The string held a puzzle, one tied to the names of her friends. Lulu could feel the magic in the air, urging her to solve it. For each of her friends names, she needs to solve this problem, "What's the minimum number of characters you need to delete from the legendary string $$$T$$$, so that it will contain her friend's name as a substring or determine if it's impossible?".

She turned to you, her eyes sparkling. "I need your help to figure this out! Let's unlock its secrets together!"

*A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, eter is a substring of runeterra and era is not.

Input

First line contains a legendary string $$$T$$$$$$(1 \leq |T| \leq 20) $$$, consisting of lowercase letters of the English alphabet.

The next line contains a single integer $$$Q$$$ $$$(1 \leq Q \leq 100,000)$$$, the number of her friends.

Then $$$Q$$$ lines follow, in the $$$i^{th}$$$ line $$$s_i$$$$$$(1 \leq |s_i| \leq 20)$$$, the name of the $$$i^{th}$$$ friend, is given. The names consist of lowercase letters of the English alphabet.

Output

Output $$$Q$$$ lines, the $$$i^{th}$$$ is the minimum number of characters you need to delete from the legendary string $$$T$$$ so that it will contain the $$$i^{th}$$$ friend's name as a substring or $$$-1$$$ if it's impossible.

Example
Input
leiulocuuniapnax
9
lulu
leona
lux
lilia
lucina
pix
lillia
icp
icpc
Output
4
7
9
7
9
-1
-1
8
-1
Note

In the first name, it's possible to delete the underlined characters, leiulocuuniapnax and it will contain the name as a substring in the underlined part luluuniapnax.

In the third name, it's possible to delete the underlined characters, leiulocuuniapnax and it will contain the name as a substring in the underlined part leiulux.

In the sixth and seventh names, it's impossible to delete characters from string T to make it containing pix or lillia as a substring, so the outputs are $$$-1$$$.

M. Marriage Proposals
time limit per test
7 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In the town where Jack lives, there are $$$N$$$ villages and exactly $$$N - 1$$$ bidirectional roads which connect every village; there is $$$\textbf{exactly one}$$$ shortest path for commuting between any two villages. There is also $$$\textbf{exactly one}$$$ pearl shop in each road. Each shop sells $$$\textbf{exactly one}$$$ type of pearl. The shop on the $$$i^{th}$$$ road sells pearl type $$$a_i$$$ and is limited to $$$b_i$$$ balls of pearl for each person. It is guaranteed that the shop will be able to provide any customers up to $$$b_i$$$ balls.

In this town, there are $$$M$$$ types of pearls. For each type, the price per ball will be the same in any shop.

There is a custom in this town that when a man wants to propose to a woman in a different village, he will pick one type of pearl and buy that type $$$\textbf{as many balls as possible}$$$ from shops along the shortest path from his village to her village. He will pick a type of pearl that $$$\textbf{maximizes money spent}$$$. Later, the pearls will be made into a necklace.

Jack has recently become an accountant of the bachelor club of the town. The bachelor club mainly collects money from its members to support those who are going to make proposals. When he became the accountant, he just realized that the old accountant forgot to record the expenses during a period of time. Fortunate enough, there are records of events that occurred during that period, and also the records of pearl prices. Initially, the cost per ball of pearl type $$$j$$$ is $$$c_j$$$. Then, there are $$$Q$$$ following events in total which can be classified as two types:

  • $$${Type\ 1:}$$$ A man in village $$$x$$$ proposed to a woman in village $$$y$$$.
  • $$${Type\ 2:}$$$ The cost for a single ball of pearl type $$$j$$$ was changed to $$$c^{'}_{j}$$$ dollars. Pearl type $$$j$$$ will have price $$$c^{'}_{j}$$$ until there is an event that updates the price of pearl type $$$j$$$.

Help Jack find the expense for each proposal, and Jack will reward you with his grasshoppers!

Input

The first line contains three integers $$$N$$$ and $$$M\ (2 \leq N \leq 100,000,\ 1 \leq M \leq 100,000)$$$ — the number of villages in the town, and the number of types of pearls, respectively.

Each of the following $$$N - 1$$$ lines contains four integers $$$u,\ v,\ a,$$$ and $$$b$$$ $$$(1 \leq u, v \leq N,u \neq v, 1 \leq a \leq M, 1 \leq b \leq 10,000)$$$ respectively. Those integers indicate that there is a road from village $$$u$$$ to village $$$v$$$. The pearl shop on that road sells pearls of type $$$a$$$ and is limited to $$$b$$$ balls for each person.

The sum of the purchase limitation in each shop for each type of pearl does not exceed $$$10,000$$$, i.e. $$$$$$\text{for all}\ 1 \leq j \leq M, \displaystyle\sum_{a_i=j}b_i \leq 10,000\ .$$$$$$

The next line contains $$$M$$$ integers $$$c_1,c_2,\dots,c_M(1 \leq c_1,c_2,\dots,c_M \leq 1,000) $$$ — the initial price for each type of pearl.

The next line contains one integer $$$Q$$$ $$$(1 \leq Q \leq 10^5)$$$ — the number of events.

The following $$$Q$$$ lines contain the description of the events from earliest to latest. The first integer in each line is $$$t$$$ — the type of the event.

Case $$$t=1$$$, the line contains two additional integers $$$x$$$ and $$$y$$$ $$$(1 \leq x, y \leq N, x \neq y)$$$ respectively. This means that a man in village $$$x$$$ proposed to a woman in village $$$y$$$.

Case $$$t=2$$$, the line contains two additional integers $$$j$$$ and $$$c^{'}_{j}$$$ $$$(1 \leq j \leq M, 1 \leq c^{'}_{j} \leq 1,000)$$$ respectively. This means that the price of pearl type $$$j$$$ changed to $$$c^{'}_{j}$$$ dollars per ball.

Output

For each $$$type\ 1$$$ event, output a single integer — the total expense for the proposal in dollars. The output should be one integer per line.

Example
Input
5 2
1 2 1 3
2 3 2 2
2 4 1 1
1 5 1 1
1 1
7
1 1 3
1 2 4
1 3 4
2 2 10
1 1 3
2 1 2
1 4 5
Output
3
1
2
20
10
Note
Fig. M.1: Illustration of the town in the example.
In example $$$1$$$, there are two types of pearl, let visualize it as white and black respectively. Initially, both pearls cost $$$1$$$ dollar per ball.

In the first event, the man walked from village $$$1$$$ to $$$2$$$, then from $$$2$$$ to $$$3$$$. He could buy $$$3$$$ balls of white pearl, which would cost $$$3$$$ dollars, or he could buy $$$2$$$ balls of black pearl, which would cost $$$2$$$ dollars. Hence, he bought the white pearl which would cost him more money.

In event $$$3$$$, the man could buy $$$2$$$ black pearl balls or $$$1$$$ white pearl ball. Both of them cost $$$1$$$ dollar per ball, so he chose black pearl which would cost him $$$2$$$ dollars.

In event $$$4$$$, the cost of black pearl was changed to $$$10$$$ dollars. Therefore, the man in event $$$5$$$, who travelled the same path as the man in event $$$1$$$, chose black pearls instead. That cost him $$$20$$$ dollars.

In other events, the men only walked past white pearl shop(s). That made the costs in those events equal to $$$\textbf{(sum of purchase limit in each shop)}\times\textbf{(cost of white pearl per ball at the time)}.$$$

N. [N]ew YoRHa Security
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

After the disaster hack by the machine, the new YoRHa project decides to employ a new security system. This system is among the most sophisticated ever devised, employing advanced cryptographic algorithms to ensure that vital information remains secure. At the heart of this security lies a unique hashing mechanism, where each piece of data is represented by the mathematical operation $$$x^y \pmod{p}$$$, where $$$p$$$ is a prime number. This system transforms every input into a signature, a seemingly random sequence that only the YoRHa servers can decode.

However, as the war with the machine lifeforms drags on, concerns arise among the operators. What if multiple pieces of information, when processed by this hashing algorithm, result in the same signature? This could lead to a catastrophic failure in the security system, where critical data is overwritten or lost due to collisions in the hash function. To prevent this, operator 21O is tasked with an important mission: to compute how many different inputs could possibly hash into the same signature for every possible output.

For every $$$k=1,2,3,\dots,p-1$$$, the operator must analyze how many pairs of integers $$$1 \leq a,b \leq p-1$$$ satisfies $$$a^b \equiv k \pmod{p}$$$. By determining how many different combinations result in the same hash, they can assess the risk of collisions and adjust the security protocols accordingly. This analysis becomes a race against time, as the integrity of YoRHa's communication and data storage hinges on the successful execution of this mission. The fate of the war may very well depend on the operator's ability to ensure that the security system remains unbreakable.

Input

A single number $$$p$$$ such that $$$2 \leq p \lt 100,000$$$. It's guaranteed that $$$p$$$ is a prime number.

Output

A single line of $$$p-1$$$ numbers. The $$$k^{th}$$$ number is the number of pairs $$$(x,y)$$$ such that $$$x^y \equiv k \pmod{p}$$$.

Examples
Input
3
Output
3 1 
Input
13
Output
40 4 16 8 10 4 4 10 16 8 4 20 
Input
2
Output
1
Note

Description for sample 1: There are 3 pairs that result in $$$x^y \equiv 1 \pmod{3}$$$: $$$(1,1)$$$, $$$(1,2)$$$, and $$$(2,2)$$$. There is 1 pair that results in $$$x^y \equiv 2 \pmod{3}$$$: $$$(2,1)$$$.