UTPC Spring 2021 Open Contest
A. Alaska Don't Release Them
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Cheyenne is playing a fish-themed variant of the poker game Texas Hold 'Em called Alaska Don't Release Them. Just like in a round of Texas Hold 'Em, each player in Alaska Don't Release Them gets dealt a private two-card hand at the beginning of the round that they will ultimately combine with five community cards that get flipped over one at a time. Once all five community cards are revealed, each player that has not yet folded (and thus remains in the betting by the end of the round) looks at their two private cards and the five community cards and then plays the highest-ranking five-card hand that they can make from amongst those seven cards.

In Alaska Don't Release Them, there are twelve possible ranked hands, listed below in order from highest to lowest value.

  1. Rockfish Flush – an Ace, 2, 3, 4, and 5, all of the same suit
  2. Fisherman's Five – five cards of the same rank
  3. Harpoon Flush – five cards of the same suit, in consecutive sequence, and not including an Ace card
  4. Four of a Species – four cards of the same rank
  5. Full Net – three cards of the same rank, along with a pair of cards of another rank
  6. Riverbed – any five cards of the same suit
  7. Harpoon – five cards in consequence sequence, not all of the same suit, and not including an Ace card
  8. Three of a Species – three cards of the same rank
  9. Double Pacific – two unique pairs of cards of the same rank, where the two pairs differ in their ranks
  10. Pacific Duo – two cards of the same rank
  11. Fish Bait – any hand containing an Ace card
  12. Garbage – a hand not qualifying as any of the above-ranked hands

Additionally, an important rule in this variant of poker is that Queen cards are considered wild and can be used to represent any card in the deck as part of forming a ranked hand. And whoever can create the highest ranked hand wins the pot!

Cheyenne wants to get better at Alaskan poker by quickly determining the ranked value of her poker hand. Help Cheyenne achieve her goals by computing the highest-ranking hand that she could possibly produce out of the five community cards and the two cards in her private hand. Given $$$N$$$ sets of seven known cards, output the highest-ranking hand possible for each one.

Input

The first line of input contains an integer $$$N$$$ ($$$1 \leq N \leq 10^6$$$), the number of card sets to follow. Then $$$N$$$ lines follow, each beginning with an integer $$$i$$$ ($$$1 \leq i \leq N$$$) denoting the index of that card set. The rest of the $$$i^{th}$$$ line contains seven card specifier strings, each formatted with the card's rank followed by its suit. A card rank can be any of A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, where A, J, Q, K represent Ace, Jack, Queen, and King respectively. Then the card's suit can be any of H, D, S, C, where these represent each of the four suits—Hearts, Diamonds, Spades, and Clubs, respectively.

Output

For each set of cards, output the index of the set, followed by the name of the highest-ranking hand that Cheyenne could possibly produce using its cards.

Example
Input
12
1 QS QH 5C QD JH 4D 4C
2 AH 9H QS QC 9S JS 9D
3 6D QC KC 6S 4S 5S QD
4 AH QD 5S 2S AC QC 3H
5 KS 2S AC 3D 3S 3C 2D
6 2C 8D QC QH AD 5D 10C
7 6H 8H QC KH 10C 9S JS
8 10H 7H 5D QH AH 3S 10C
9 5C KH 7D JH 3H 3C JC
10 2H 7C 6C 8C 10C 5S 8D
11 2C 8D 10S 3H 5S AH 6C
12 10S 3D 8D JD 2S 9S 6C
Output
1 Rockfish Flush
2 Fisherman's Five
3 Harpoon Flush
4 Four of a Species
5 Full Net
6 Riverbed
7 Harpoon
8 Three of a Species
9 Double Pacific
10 Pacific Duo
11 Fish Bait
12 Garbage

B. Betting Confusion
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Priya has placed a series of convoluted bets on the results of the upcoming horse races. Unfortunately, she's placed so many bets that she's now unable to calculate how much money she expects to gain or lose from her bets and is worried that she may be losing too much.

She's figured out for each pair of horses how much she stands to gain or lose if either horse beats the other, but doesn't know how much she can expect to earn overall, where her total reward is the sum of each pairwise reward earned given the ordering of the horses defined by the horses' placement after the race is finished.

Given that each possible ordering of horses in the race is equally probable (it is impossible for any two horses to tie), what is the expected value of Priya's winnings or losses?

Input

The input begins with a single integer $$$n$$$ $$$(1 \leq n \leq 300)$$$ giving the number of horses in the race. The next $$$n$$$ lines each consist of $$$n$$$ integers where the $$$j$$$th integer on the $$$i$$$th line, $$$a_{ij}$$$ $$$(-10^5 \leq a_{ij} \leq 10^5)$$$, gives the reward that Priya earns if horse $$$i$$$ places higher than horse $$$j$$$ in the race. Note that $$$a_{ii} = 0$$$ for all $$$1 \leq i \leq n$$$.

Output

Output a single real number giving the expected value of Priya's winnings given that each ordering of horses is equally likely. Your answer will be judged as correct if it has an absolute or relative error less than $$$10^{-6}$$$.

Examples
Input
3
0 1 1
1 0 1
1 1 0
Output
3.0000000000
Input
5
0 -1 -3 4 2
1 0 3 -4 -5
10 -20 0 5 0
8 7 -12 0 -13
1 1 -3 2 0
Output
-8.5000000000
Note

For the first sample, Priya will collect a total reward of $$$3$$$ for any of the $$$6$$$ possible orderings of horses.

C. Chess Tournament
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vivek is attending a chess tournament for the first time in many years! His hope are high, but he is also very realistic about his chances to win. He wants to determine the probability that he wins the tournament. Winning the tournament means that he has more points than any other individual player.

In this amateur USCF chess tournament, every single person has a rating ranging from $$$1000$$$ to $$$2000$$$ inclusive, so each match is by no means entirely one-sided. If two people $$$A$$$ and $$$B$$$ with ratings $$$r_A$$$ and $$$r_B$$$ respectively play each other, for the purposes of Vivek's estimations, the probability that $$$A$$$ wins is $$$\dfrac{r_A}{r_A + r_B}$$$, while the probability that $$$B$$$ wins is $$$\dfrac{r_B}{r_A + r_B}$$$, assuming that draws are not in the picture.

The tournament organizers decided that since this was a relatively small chess tournament with only $$$n$$$ participants one week before the tournament started, the format of the tournament will be round robin. Since the tournament directors do not like draws, in a slightly different twist, each pair of players instead of playing each other once, play each other twice. Player $$$A$$$ gets a point if they can defeat player $$$B$$$ in the first match they play, and player $$$B$$$ gets a point if they can defeat player $$$A$$$ in the second match they play. In this slightly different format, Vivek assumes that probability that player $$$A$$$ wins the first match is still $$$\dfrac{r_A}{r_A + r_B}$$$, andthe probability that $$$B$$$ wins the second match is $$$\dfrac{r_B}{r_A + r_B}$$$.

Now, given this tournament style, help Vivek determine the probability that he can be expected to win the tournament! Since the answer will be of the form $$$\dfrac{n}{d}$$$, where $$$n$$$ and $$$d$$$ can be very large, output instead $$$nd^{-1} \mod 10^9 + 7$$$, where $$$d^{-1}$$$ is the modular inverse of $$$d$$$, or the number such that $$$d \cdot d^{-1} \equiv 1 \pmod{10^9 + 7}$$$.

Input

The first line of the input will contain $$$n$$$ ($$$1 \le n \le 250$$$), the number of participants in the chess tournament.

The next line will contain $$$e_v$$$, Vivek's ELO.

The remaining $$$n - 1$$$ lines will each contain an ELO of a participant in the tournament. It is given that for all participants, we have that the ELO $$$e \in [1000, 2000]$$$.

Output

Output $$$nd^{-1} \mod 10^9 + 7$$$, where $$$\dfrac nd$$$ is the probability that Vivek wins the chess tournament, given his assumptions.

Example
Input
2
1500
1000
Output
520000004
Note

In the sample, Vivek has an ELO of 1500, while his one opponent has an ELO of 1000. He has to win both the first game to get his point, and not lose the second game to ensure that his opponent does not tie him. The probability that he wins one match is $$$\dfrac{1500}{1500 + 1000} = \dfrac 35$$$, so the probability of winning both matches is $$$\left( \dfrac 35 \right)^2 = \dfrac{9}{25}$$$. Here, we have $$$25^{-1} \mod 10^9 + 7 = 280000002$$$, and so $$$nd^{-1} \mod 10^9 + 7 = 9 \cdot 280000002\mod 10^9 + 7 = 520000004$$$.

D. Doggis
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Doggis is a new kind of dating app... that matches people with lovable pets from the local shelter! After a user creates a profile on the app, the backend generates a list of pets that are compatible with the user. Although the app creators are allowing people to sign up, development of the matching algorithm that pairs users with their new best friends has not yet begun! Internal conflicts ranging from the optimal brand of coffee to buy for the kitchen to whether or not labradoodles are the best dog breed have forced Doggis to hire an independent contractor to write the algorithm for them (while the main team resolves the myriad of existing disputes).

Doggis insists that their matching algorithm maps at most one pet to a single user, and that a user is only matched to a pet on their generated compatibility list. You, the independent contractor, have a heart of gold and want to maximize the number of pets assigned to people for adoption; however, you strongly believe that the constraint that matching at most one pet to a single user is a poor one that works against your ideals. To make your case, you decide to find the number of generated matches from the compatibility lists that will never occur when maximizing the number of pets assigned to people under current constraints. You hope that presenting your findings to the CEO of Doggis, Sehr Pöderman, will convince the company to rethink the desired design of their matching algorithm.

Input

The first line of input contains two integers $$$N$$$ and $$$M$$$ ($$$1 \leq N \leq M \leq 10^5$$$), representing the number of app users and the number of pets, respectively. Note that pets are represented by unique integers from $$$1$$$ to $$$M$$$, inclusive.

Each of the next $$$N$$$ lines detail a list of pets compatible with a particular user. The first integer on each line, $$$p_i$$$, represents the number of pets compatible with a user. Then $$$p_i$$$ distinct integers between $$$1$$$ and $$$M$$$, inclusive, follow, representing the unique pets compatible with the user. The sum of all $$$p_i$$$ will be at most $$$2 \cdot 10^5$$$.

Output

Output a single integer representing the number of generated user/pet matches that will never occur when maximizing the number of pets assigned to people under current constraints.

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

E. Even Subarrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Steven developed an obsession with all things even. Ever since that day, Steven has used level bars on all his tables and desks, refused to play two player games where either player has a winning strategy, and spent several hours thinking about functions like $$$x^2$$$ and $$$\cos(x)$$$.

However, Steven wants to cure himself of his even obsession and realizes the only way he can do so is to solve a competitive programming question related to even numbers. The question Steven wishes to solve is the following: given an array $$$a$$$, count the number of even subarrays of $$$a$$$. A subarray is even if each element inside the subarray appears exactly twice within the subarray.

Can you help Steven solve this problem?

Input

The first line of the input consists of a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5)$$$. The next line consists of $$$n$$$ space separated integers $$$a_i$$$ ($$$1 \leq a_i \leq n$$$), giving the elements of the array $$$a$$$.

Output

Output a single integer giving the number of even subarrays of the array $$$a$$$.

Example
Input
6
1 1 3 3 2 2
Output
6
Note

In this example, any subarray that starts on an even index and ends on an odd index is an even subarray, and there are $$$6$$$ such arrays.

F. Famished Flesheating Frogs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob have been abducted by an alien race of flesheating frogs! The frogs often get bored and like to play with their food. Their favorite method is a game called "1-D Leapfrog".

1-D Leapfrog is a game performed on an one dimensional board of $$$N$$$ boxes, indexed by consecutive integers from $$$0$$$ to $$$N-1$$$, inclusive. Each box contains at most 1 frog.

Each frog on the board can "leap" from one box to another. In one leap, a frog in box $$$i$$$ can leap to any box $$$0 \leq j \lt i$$$, such that all the intermediate boxes $$$k$$$ ($$$i \gt k \gt j$$$) have frogs in them, and $$$j$$$ currently is empty. It is possible that there are no intermediate boxes that are jumped over in a single leap.

The board is initialized with some number of frogs. Alice and Bob take turns commanding frogs to leap, with Alice going first. When a player doesn't give a command, cannot give a command, or gives an invalid command, the frogs get very irritated and consume the offending player, bones and all.

Bob realizes he is at a disadvantage with this game, and proposes a new variant, called 2-D Leapfrog. 2-D Leapfrog consists of playing $$$M$$$ games of 1-D Leapfrog simultaneously. Play proceeds as follows:

  • Alice chooses one of the games of 1-D Leapfrog
  • Alice then makes a valid leap in the selected game
  • If Alice cannot make a valid leap, then Alice is eaten alive
  • Bob chooses one of the games of 1-D Leapfrog
  • Bob then makes a valid leap in the selected game
  • If Bob cannot make a valid leap, then Bob is eaten alive
  • Repeat

Alice and the frogs are fascinated with this new game, and want to play immediately! They have gathered $$$M$$$ 1-D Leapfrog boards. However, Bob has gotten suspicious that Alice has rigged the game against him, and decides to randomize the direction of the 1-D boards before play begins. That is, before the game starts, for each board independently with probability $$$\frac{1}{2}$$$, he reverses the board.

Assuming that Alice and Bob both play optimally on the potentially modified set of boards, what is the probability that Bob survives?

Input

The first line of input contains a single integer $$$M$$$ ($$$1 \leq M \leq 5 \cdot 10^6$$$) representing the number of 1-D boards, followed by $$$M$$$ lines of the following form:

A single positive integer $$$N$$$, the length of the board, followed by a string $$$s$$$ consisting of $$$N$$$ characters that are either 0 or 1, where the $$$i$$$-th position is 1 if a frog is at that position and $$$0$$$ otherwise.

The sum of the length of the boards is at most $$$5 \cdot 10^6$$$.

Output

If $$$P$$$ is the probability Bob survives, output a single integer $$$(P \cdot 2^M) \mod (10^9 + 7)$$$.

Examples
Input
1
3 010
Output
0
Input
2
2 01
2 10
Output
2

G. Four Horsemen
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Magicians J. Daniel Atlas, Merritt McKinney, Henley Reeves, and Jack Wilder each receive a tarot card leading them to a New York City apartment. A year after their initial meeting, they begin to perform as 'The Four Horsemen' in Las Vegas. Atlas and his comrades mesmerize audiences with magic that drains the bank accounts of the corrupt and funnels the money to audience members.

For the group's next trick, they wish to teleport the contents of a vault from its physical location in Paris to the MGM Grand. The Horsemen are big on audience participation and plan to ask a randomly chosen member to draw a rectangle on stage to serve as a teleportation endpoint. Atlas has a low opinion of the mathematical abilities of audience members (given that they cannot figure out how his tricks work), so he decides to aid the process by marking $$$N$$$ points on the stage that can be chosen as corners of the rectangle. Although McKinney could hypnotize the chosen audience member to pick a specific rectangle, he finds it more enjoyable to watch Reeves and Wilder wrangle the logistics of multiple possible rectangles. The latter two wish to know the number of non-zero-area rectangles an audience member can choose before handling other aspects of the show. Can you help Henley and Jack?

Input

The first line of input contains a single integer $$$N$$$ ($$$1 \leq N \leq 1000$$$), representing the number of points drawn on stage. The next $$$N$$$ lines of input each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 \leq x_i, y_i \leq 10^9$$$), representing the location $$$(x_i, y_i)$$$ of the $$$i$$$-th point when considering the stage as a Cartesian plane.

Output

Output a single integer representing the number of possible rectangles an audience member can choose.

Example
Input
9
0 0
1 0
0 1
-1 0
0 -1
1 1
1 -1
-1 1
-1 -1
Output
10

H. Pancake Scheduling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Don's new breakfast chain, Donny's, is a huge success! Today, he has received orders for $$$N$$$ pancakes ($$$1 \leq N \leq 10^{12}$$$). Don has two frying pans with which to cook the pancakes: the first pan can cook one pancake in $$$A$$$ minutes, while the second pan can cook one pancake in $$$B$$$ minutes ($$$1 \leq A, B \leq 10^6$$$). As a master chef, Don can use both pans simultaneously, and it takes no time to move pancakes in and out of the frying pans. However, once a pancake starts cooking in a pan, it must stay in that pan until the pancake is finished cooking. What is the minimum amount of time, in minutes, that Don needs to cook all $$$N$$$ pancakes?

Input

There is one line of input with three integers, separated by spaces.

The first integer is N, the number of pancakes Don needs to cook ($$$1 \leq N \leq 10^{12}$$$). The second integer is $$$A$$$, the number of minutes it takes Don's first pan to cook one pancake ($$$1 \leq A \leq 10^6$$$). The third integer is $$$B$$$, the number of minutes it takes Don's second pan to cook one pancake ($$$1 \leq B \leq 10^6$$$).

Output

Output a single integer, the minimum number of minutes for Don to cook all $$$N$$$ pancakes.

Examples
Input
5 2 3
Output
6
Input
6 2 3
Output
8
Input
3 3 9
Output
9
Note

In the first sample test case, we have a pan that cooks a pancake every 2 minutes, and a pan that cooks a pancake every three minutes. To cook 5 pancakes, we can let the first pan cook 3 pancakes in 6 minutes, and the second pan can cook 2 pancakes in 6 minutes. Since Don can use both pans at the same time, this takes 6 minutes total.

In the second sample test case, Don needs at least 8 minutes. To cook 6 pancakes, we can let the first pan cook 4 pancakes in 8 minutes, and the second pan can cook 2 pancakes in 6 minutes.

In the third sample test case, Don needs at least 9 minutes. One possible way to cook 3 pancakes is to let the first pan cook 2 pancakes in 6 minutes, and the second pan can cook 1 pancake in 9 minutes, for a total of 3 pancakes in 9 minutes.

Be sure to use 64-bit integers!

I. Prefix Prizes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mateo, the owner of Austin's most popular video game store, was in a generous mood one day. He decides to give a gift to each of the $$$n$$$ people currently in his store and he has $$$n$$$ distinct prizes labeled $$$0, 1, \dots, n - 1$$$ to give out. Since Mateo never does anything the simple way, he comes up with the following scheme to give the prizes out.

He gives each of the $$$n$$$ people in the store unique numbers from $$$1$$$ to $$$n$$$ (inclusive). He then asks everyone to form a line and so the resulting ordering of the people can be written as the sequence $$$[x_1, x_2, \dots, x_n]$$$ (where each $$$x_i$$$ is unique and $$$1 \leq x_i \leq n$$$). The prize which Mateo gives person $$$x_j$$$ is equal to $$$x_1 \cdot x_2 \cdot \ldots \cdot x_{j-1} \cdot x_{j} \pmod{n}$$$. Unfortunately, Mateo only has one of each prize so he asks you to come up with an ordering of the $$$n$$$ people so that each person will get a unique prize number.

Input

The first and only line of input will contain a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$), representing the number of people in the store.

Output

A space-separated permutation of the numbers from $$$1$$$ to $$$n$$$ (inclusive), representing the ordering of people such that each person can get a distinct prize. Output -1 if there is no such permutation.

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

In the first example:

  • The product after the first person is $$$1$$$ so the first person gets prize $$$1$$$.
  • The product after the second person is $$$1 \cdot 4 = 4 \equiv 4 \pmod{7}$$$ so the second person gets prize $$$4$$$
  • The product after the third person is $$$4 \cdot 3 = 12 \equiv 5 \pmod{7}$$$ so the third person gets prize $$$5$$$.
  • The product after the fourth person is $$$5 \cdot 6 = 30 \equiv 2 \pmod{7}$$$ so the fourth person gets prize $$$2$$$.
  • The product after the fifth person is $$$5 \cdot 2 = 10 \equiv 3 \pmod{7}$$$ so the fifth person gets prize $$$3$$$.
  • The product after the sixth person is $$$3 \cdot 2 = 6 \equiv 6 \pmod{7}$$$ so the sixth person gets prize $$$6$$$.
  • The product after the seventh person is $$$6 \cdot 7 = 42 \equiv 0 \pmod{7}$$$ so the seventh person gets prize $$$0$$$.
Thus, with this ordering of people, each person will get a unique one of the $$$7$$$ prizes.

J. Rolling Encryption
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Leroy has been given a sequence of $$$n$$$ integers $$$X$$$ between $$$0$$$ and $$$k - 1$$$ (inclusive) for some positive integer $$$k$$$ which is vital to national security. He doesn't want to write down the numbers themselves in case they fall into the wrong hands so he decides to use a new technique: the 'rolling encryption' to obfuscate the true numbers until he gets safely to his destination.

The rolling encryption technique takes the $$$n$$$ integers and picks a sequence of $$$m$$$ additional integers $$$Y$$$ known as the encryption key (where $$$m \leq n$$$ and each of the $$$m$$$ integers are also between $$$0$$$ and $$$k - 1$$$ inclusive). It takes $$$n - m + 1$$$ steps to encrypt all the numbers and on each step $$$i$$$ (for $$$1 \leq i \leq n - m + 1$$$), the algorithm takes the numbers $$$x_{i}, x_{i + 1}, \dots, x_{i + m - 1}$$$ and adds $$$y_1, y_2, \dots y_m$$$ respectively to each number (so $$$x_{i} = x_{i} + y_{1}$$$, $$$x_{i + 1} = x_{i + 1} + y_{2}$$$, and so on and so forth). Leroy realizes that this technique can cause the numbers to be very big so he decides to do all of the operations modulo $$$k$$$.

Given $$$X$$$ (the sequence of $$$n$$$ integers to encrypt) and $$$Y$$$ (the sequence of $$$m$$$ integers which is the encryption key), Leroy wants your help to compute the new encrypted sequence.

Input

The first line of input will give you three space-separated $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq m \leq n \leq 10^{3}$$$ and $$$1 \leq k \leq 10^{3}$$$) where $$$n$$$ is the length of the original sequence, $$$m$$$ is the length of the encryption key, and $$$k$$$ is the range of the integers.

The next line of input will have $$$n$$$ space-separated integers representing the numbers in the original sequence $$$X$$$ ($$$0 \leq X_{i} \leq k - 1$$$ for all $$$1 \leq i \leq n$$$).

The final line of input will have $$$m$$$ space-separated integers representing the numbers in the encryption key sequence $$$Y$$$ ($$$0 \leq Y_{i} \leq k - 1$$$ for all $$$1 \leq i \leq m$$$).

Output

Output $$$n$$$ space-separated integers, the result of doing the rolling encryption on the original $$$X$$$ sequence.

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

In the first sample, after the first step $$$X = [0, 0, 1, 2]$$$, after the second step $$$X = [0, 1, 1, 2]$$$, after the third step $$$X = [0, 1, 2, 2]$$$ and there are no more steps $$$(n - m + 1) = (4 - 2 + 1)$$$ and so the final answer is $$$[0, 1, 2, 2]$$$.

K. Cereal Serial Number
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Salem loves to eat cereal and actually considers themself a bit of an enthusiast for their favorite cereal brand, Mucky Farms. Recently, Mucky Farms put out a promotional challenge to their customers, encouraging them to analyze the long, $$$N$$$-digit serial number stamped on every box of Mucky Farms cereal. If a customer can use scissors to cut up the large serial number into a series of chunks that denote strictly increasing numbers, then that person can submit the cut-up serial number to Mucky Farms Inc. who will ship them a free box of cereal, but only if the customer has chopped up the serial number into the maximum possible number of substrings that denote increasing numbers.

Salem wants nothing more than to win a free box of their favorite cereal. Help them achieve their dreams! Compute the maximum number of partitions that the serial number can be divided into such that, when interpreted as decimal integers, each substring appears in increasing order from left to right in the serial number?

Input

The first and only line of input contains the $$$N$$$-digit ($$$1 \leq N \leq 2000$$$) serial number stamped on Salem's box of Mucky Farms. Note that only digits 1 through 9 appear in the serial number, in honor of the founder of Mucky Farms who holds a deep-seated oudenophobia or fear of the number zero.

Output

Print out the maximum number of partitions that the serial number can be divided into such that each partition denotes a strictly increasing integer value.

Example
Input
54281326333
Output
5
Note

The sample case can be maximally partitioned into the following five, strictly-increasing integers: 5|42|81|326|333

L. Tennis Cup
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Back before the pandemic, Lang was an avid tennis player. In fact, he was so naturally talented at the game that when he exerts $$$100\%$$$ of his capabilities, he is guaranteed that he will win any tennis match he played.

In fact, this was demonstrated in a tennis cup that he attended back a couple of years back. In this single-elimination tournament, Lang was able to win the tournament without so much as dropping a single set. When setting up a tournament, the tournament organizers had two goals. The first of which was to minimize the total number of rounds. Specifically, a round in a single-elimination bracket is where everyone that does not have a bye plays an opponent. The tournament organizers want to set it up so that once a participant plays a match, they cannot have any more byes later on in the tournament. Note however, that a participant can have multiple byes before they play a match.

Now, the tournament organizers know that Lang will inevitably win the tournament in such a convincing fashion that they actually want to limit the number of matches that Lang has to play. In fact, they want to maximize the number of byes that Lang will have. Given that they still want to limit the tournament to as few total rounds as possible, determine the minimum matches that the tournament organizers could have made Lang play and have everyone else eliminated with $$$n$$$ participants.

Here is an example with 12 participants, where Lang is able to play only two matches and win, while the total number rounds is limited to 4 and everyone else is eliminated.

Input

The first line of the input contains $$$n$$$ ($$$1 \le n \le 10^8$$$), the number of participants, including Lang.

Output

Output a single integer, the number of matches that Lang needs to play to be crowned victor, given that the number of rounds in the single elimination tournament are limited to the minimum number.

Examples
Input
9
Output
1
Input
16
Output
4
Input
11
Output
2
Input
12
Output
2

M. Triforce of Wisdom
time limit per test
10 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A small kingdom in the land of Hyrule is engulfed by chaos when an army led by Ganon, the Prince of Darkness, invaded and stole the Triforce of Power, one part of a magical artifact that alone bestows great strength. In an attempt to prevent him from acquiring the Triforce of Wisdom, Princess Zelda splits it into eight fragments and hides them in secret underground dungeons. Before eventually being kidnapped by Ganon, she commands her nursemaid Impa to find someone courageous enough to save the kingdom. While wandering the land, the old woman is surrounded by Ganon's henchmen, when a young boy named Link appears and rescues her. Upon hearing Impa's plea, he resolves to save Zelda and sets out to reassemble the scattered fragments of the Triforce of Wisdom, with which Ganon can then be defeated.

Three pieces of the Triforce of Wisdom have been scattered in a network of $$$N$$$ dungeons conveniently numbered $$$1\dots N$$$, which are connected by a set of corridors. Each corridor connects two distinct dungeons, and they are arranged in a way such that there is a unique path between each pair of dungeons. Link knows that each dungeon holds at most one piece of the magical item, but he is not sure about which dungeons he should search. Corridors between dungeons are highly unstable and risk collapsing at any time, and exploration could go awry when traveling between dungeons. However, Link is able to buy 'corridor stabilizer' items from shopkeepers; each stabilizer can prevent the collapse of exactly one corridor while Link is still exploring.

A friendly cartographer has helped our hero come up with $$$G$$$ guesses for where the fragments could be in the dungeon network, with the $$$i$$$-th guess expressed as a triple of integers $$$(a_i, b_i, c_i)$$$. If Link can enter into any dungeon in the network to begin his quest, but will only leave the maze once he has collected all three fragments, what is the minimum number of corridor stabilizers Link will need to purchase in order to travel in corridors between dungeons safely? Help the Hero of Hyrule find the optimal solution for each of the $$$G$$$ guesses!

Input

The first line of input contains two integers $$$N$$$ and $$$G$$$ ($$$1 \leq N, G \leq 10^5$$$), representing the number of dungeons in the network and the number of guesses for fragment locations, respectively. The next $$$N-1$$$ lines of input contain two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq N$$$), which designate endpoint dungeons for a bidirectional corridor. The next $$$G$$$ lines of input contain three distinct integers $$$a_i, b_i, c_i$$$ ($$$1 \leq a_i, b_i, c_i \leq N$$$) that represent dungeons from the $$$i$$$-th guess.

Output

Output $$$G$$$ lines each with a single integer representing the minimum number of corridor stabilizers Link needs to purchase, assuming information from the $$$i$$$-th guess is accurate.

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