CODEFEM - Mexican Womens Competitive Programming League
A. Ladies Brunch
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Itzel is very excited because her friend group organized a "ladies brunch" at the new aesthetic restaurant in town: La Cafetería Orgánica De Especialidad: Frappés, Éclairs y Matcha (CODEFEM). She woke up at 9:00, had breakfast at 10:00, went to the gym until 12:00, got ready, and arrived on time at 15:00.

Itzel and her friends sit around a circular table in clockwise order, where Itzel occupies position $$$0$$$ and her friends occupy positions $$$1, 2, \dots, N-1$$$.

The cafe has a menu consisting of $$$M$$$ products with fixed integer prices. As the afternoon progresses, they start ordering drinks and desserts. Seeing that the waitress is overwhelmed by the high volume of orders and is having trouble keeping track of everyone's bill, Itzel decides to write a program to process the orders.

There are $$$Q$$$ operations of three types:

  • 1 PersonName ProductName: The specified person orders a product from the menu.
  • 2 L R ProductName: Everyone seated in the clockwise circular range from position $$$L$$$ to position $$$R$$$ ($$$0 \le L, R \lt N$$$) orders the specified product. If $$$L \le R$$$, this covers positions $$$L, L+1, \dots, R$$$. If $$$R \lt L$$$, this wraps around the circular table, covering positions $$$L, L+1, \dots, N-1$$$ and $$$0, 1, \dots, R$$$.
  • 3 PersonName: The waitress queries the total accumulated bill for the specified person.

Help Itzel process all operations and output the answers to all queries.

Input

The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N \le 100000$$$, $$$1 \le M \le 10000$$$), the number of people at the table and the number of menu products.

The second line contains $$$N$$$ space-separated strings: the names of the people seated in clockwise order (the first person is Itzel at position $$$0$$$).

The next $$$M$$$ lines each contain a string $$$ProductName$$$ and an integer $$$P$$$ ($$$1 \le P \le 10^9$$$), representing the product name and its price.

All person names are distinct, and all product names are distinct. Every name consists of between $$$1$$$ and $$$20$$$ characters without spaces. The total length of all strings in the input does not exceed $$$10^6$$$.

The next line contains an integer $$$Q$$$ ($$$1 \le Q \le 100000$$$), the number of operations.

The next $$$Q$$$ lines describe the operations in the format specified above.

Output

For each operation of type 3, print a single integer on a new line: the total accumulated bill of the queried person.

Example
Input
4 3
Itzel Celi Emily Sofia
MatchaLatte 85
Eclair 60
TaroFrappe 95
5
1 Itzel MatchaLatte
2 2 0 Eclair
3 Itzel
3 Celi
3 Emily
Output
145
0
60

B. Emily's Agenda
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Emily is very excited to lead a project for the first time, especially one as inspiring as CODEFEM! However, as she quickly learned: "With great power comes great responsibility... and tons of meetings!".

There are meetings scheduled with the marketing team, design team, problem setters, finance, and logistics. As a dedicated leader, Emily wants to attend as many meetings as possible. She has a list of $$$N$$$ scheduled meetings. Each meeting $$$i$$$ has a start time and an end time specified in 24-hour format (HH:MM).

The scheduling rules are as follows:

  • All meetings start during the current day (between 00:00 and 23:59).
  • Emily must attend each chosen meeting from start to finish.
  • She cannot be in two meetings at the same time.
  • A meeting may cross midnight: if its end time is less than or equal to its start time, the meeting ends on the following day.
  • If one meeting ends exactly when another starts, Emily may attend both.

Help Emily organize her agenda and find the maximum number of meetings she can attend.

Input

The first line contains an integer $$$N$$$ ($$$1 \le N \le 100000$$$), the number of scheduled meetings.

Each of the next $$$N$$$ lines contains two space-separated strings, $$$s_i$$$ and $$$e_i$$$, representing the start and end times of the $$$i$$$-th meeting in 24-hour format HH:MM ($$$00 \le HH \le 23$$$, $$$00 \le MM \le 59$$$).

If $$$e_i \le s_i$$$, the meeting crosses midnight: it starts on the current day at $$$s_i$$$ and ends on the following day at $$$e_i$$$. A meeting with $$$s_i = e_i$$$ lasts exactly $$$24$$$ hours.

It is guaranteed that no meeting lasts more than $$$24$$$ hours.

Output

Print a single integer: the maximum number of meetings Emily can attend.

Examples
Input
4
09:00 11:00
10:30 12:30
11:00 13:00
23:00 01:00
Output
3
Input
3
10:00 18:00
11:00 14:00
12:00 13:00
Output
1
Note

In the first example, the meeting 23:00–01:00 crosses midnight: it starts at 23:00 of the current day and ends at 01:00 of the following day. Emily can attend 09:00–11:00 and 11:00–13:00 back to back (the second starts exactly when the first ends), and then 23:00–01:00, for a total of $$$3$$$ meetings.

In the second example, the three meetings all overlap with each other (in particular, 11:00–14:00 and 12:00–13:00 are contained in 10:00–18:00), so Emily can attend at most one of them. The answer is $$$1$$$.

C. Gym Sis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Celi and Itzel decided to start their fitness journey. They bought matching outfits and trendy water bottles, but couldn't afford a personal trainer, so they turned to TikTok. There they found Coach Emily, whose main training philosophy is: "The gym isn't about how much weight you lift, but about the aura you radiate".

According to Coach Emily's philosophy:

  • There are $$$N$$$ different exercises, numbered from $$$1$$$ to $$$N$$$.
  • On each of the $$$Z$$$ training days, Celi and Itzel can choose to perform exactly one exercise or take a rest day.
  • If they choose to perform exercise $$$i$$$ on day $$$c$$$ of a consecutive streak of that same exercise:
    • If $$$c \le d_i$$$: they gain $$$+k_i$$$ aura points.
    • If $$$c \gt d_i$$$: they gain $$$+(k_i - x_i)$$$ aura points (the penalty $$$x_i$$$ applies for each consecutive day that exceeds the optimal limit $$$d_i$$$).
  • If they choose to rest, they gain $$$0$$$ aura points and any active streak is reset to $$$0$$$.
  • If they switch from exercise $$$i$$$ to a different exercise $$$j \ne i$$$, the streak for exercise $$$j$$$ starts at $$$1$$$.

Initially (before day 1), they have no active streak. Celi and Itzel want to know the maximum amount of aura points they can accumulate by the end of the $$$Z$$$ training days if they choose their daily activities optimally.

Input

The first line contains two integers $$$N$$$ and $$$Z$$$ ($$$1 \le N \le 1000$$$, $$$1 \le Z \le 1000$$$), the number of available exercises and the total number of training days.

The following $$$N$$$ lines describe the exercises. The $$$i$$$-th line contains three integers $$$k_i$$$, $$$d_i$$$, and $$$x_i$$$ ($$$1 \le k_i \le 10^9$$$, $$$1 \le d_i \le 100$$$, $$$0 \le x_i \le 10^9$$$), representing the base aura points per day, the optimal consecutive days threshold, and the penalty per additional consecutive day, respectively.

It is guaranteed that $$$\sum_{i=1}^N d_i \le 5000$$$.

Output

Print a single integer: the maximum amount of aura points that Celi and Itzel can accumulate over the $$$Z$$$ training days.

Example
Input
2 4
10 2 15
8 1 10
Output
38

D. Lighting a New Playground
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Your team will have an important mission after the contest: You have to set up a new playground for a couple of the beloved pets of Code Fem: two extremely cute guinea pigs. For that reason, your team needs to buy decorative lights of different colors to embellish the place. Each light is only one color.

The decorative lights are distributed all over the surface where the playground is to be built by your mates; however, you had to go to the bathroom and did not get to know the arrangement of the lights. Your peers have noticed that they could join the lights with imaginary straight lines and create polygons whose vertices are those lights themselves. After thinking a lot about the topic, they came up with a question that challenged you to solve: Regardless of the arrangement they chose, and the colors of lights they utilized, how many vertices does the largest polygon that you can guarantee have, such that all the vertices (lights) are the same color? It does not matter what color it is.

In this case, consider that the more vertices a polygon has, the greater it is, regardless of its area. Moreover, the polygon could be convex or non-convex. You can be sure there are no three collinear lights.

Input

The first line will contain a natural number $$$1 \leq T \leq 1000$$$: the number of input cases. Then, you will receive $$$T$$$ lines, each with two positive integers $$$n$$$ and $$$k$$$, where $$$n$$$ ($$$ 3 \leq n \leq 10^{15} $$$) is the number of spots where a light is to be set up, and $$$k$$$ ($$$ 1 \leq k \leq 10^{15} $$$) is the number of available colors for the lights.

Output

For each case, print on a different line the number of vertices that the greatest polygon has. If the figure is not a polygon, print 0.

Example
Input
2
10 3
7 4
Output
4
0

E. Lizard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mitzi is about to leave for CODEFEM when she notices a little lizard wandering around her patio. The problem is that her $$$k$$$ chubby cats live there too.

Mitzi's patio is an $$$n \times m$$$ grid of tiles. The lizard starts on tile $$$(s_x, s_y)$$$ and can move only to the tile directly above, below, to the left, or to the right, without leaving the patio.

Each chubby cat $$$i$$$ is sitting on tile $$$(x_i, y_i)$$$ and has a jumping range $$$f_i$$$. No matter how lazy a cat may be, if the lizard is at a Manhattan distance less than or equal to $$$f_i$$$, the cat can catch it in a single leap. Therefore, the lizard will never step on a tile within the reach of any cat.

The Manhattan distance between two tiles $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is defined as:

$$$$$$|x_1-x_2| + |y_1-y_2|$$$$$$

and represents the minimum number of moves required to travel from one tile to the other using only the four main directions.

Luckily for the lizard, there are $$$h$$$ tiles with holes in the patio where it can hide. As soon as it reaches any of them, it will be safe.

Among all holes that the lizard can reach without ever stepping on a tile within the jumping range of a cat, it will choose the one that requires the fewest moves.

If several holes can be reached using the same minimum number of moves, the lizard will choose the hole with the smallest row number. If there is still a tie, it will choose the one with the smallest column number.

Mitzi wants to know which hole the lizard will reach, so she can decide whether she can leave peacefully or needs to help it escape.

Determine the tile containing the hole the lizard will reach.

If there is no safe path to any hole, print $$$-1$$$ $$$-1$$$.

Input

The first line contains four integers $$$n$$$, $$$m$$$, $$$k$$$, and $$$h$$$ — the number of rows and columns in the patio, the number of chubby cats, and the number of tiles containing holes, respectively.

The second line contains two integers $$$s_x$$$ and $$$s_y$$$, indicating the row and column of the lizard's starting position.

Each of the next $$$k$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, and $$$f_i$$$, representing the row, column, and jumping range of the $$$i$$$-th chubby cat.

Each of the next $$$h$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$, indicating the row and column of a tile containing a hole.

The following constraints apply:

$$$1 \le n,m \le 1000$$$, $$$n \cdot m \le 5 \cdot 10^5$$$,

$$$1 \le k \le \min(2 \cdot 10^5,n\cdot m)$$$, $$$0 \le h \le \min(2 \cdot 10^5,n\cdot m)$$$,

$$$0 \le f_i \le n+m$$$, $$$1 \le s_x,x_i \le n$$$, and $$$1 \le s_y,y_i \le m$$$.

It is also guaranteed that no two chubby cats occupy the same tile, no two holes are located on the same tile, and the lizard does not start on a tile occupied by a chubby cat or on a tile containing a hole. All given positions are within the boundaries of Mitzi's patio.

Output

Print two integers $$$x$$$ and $$$y$$$, representing the row and column of the hole the lizard will reach.

If there is no path from the lizard's starting position to any hole without stepping on a tile within the jumping range of a cat, print

$$$$$$ \texttt{-1 -1}. $$$$$$

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

In the first example, the lizard can reach the hole at $$$(1,7)$$$ without stepping on any tile within the jumping range of a cat.

It can reach this hole in $$$3$$$ moves:

$$$$$$ (1,4) \rightarrow (1,5) \rightarrow (1,6) \rightarrow (1,7). $$$$$$

Although other holes may also be reachable, none of them can be reached using fewer moves. Therefore, the lizard chooses the hole at $$$(1,7)$$$.

In the second example, the tiles within the jumping range of the chubby cat at $$$(2,2)$$$ prevent the lizard from reaching the hole at $$$(1,3)$$$ without stepping on a tile within the cat's jumping range.

Therefore, there is no valid path to any hole, and the answer is -1 -1.

F. Cafe Hopping
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After CODEFEM, Ashley decides to celebrate by visiting some of the cafes located near the event.

There are $$$N$$$ cafes arranged along the same street, numbered from $$$1$$$ to $$$N$$$. At cafe $$$i$$$, Ashley can buy a drink that costs $$$a_i$$$ coins.

Ashley wants to visit a consecutive group of cafes and buy exactly one drink at each cafe she visits.

She has a budget of $$$B$$$ coins.

Find the maximum number of consecutive cafes Ashley can visit without spending more than $$$B$$$ coins.

If Ashley cannot afford a drink at any cafe, the answer is $$$0$$$.

Input

The first line contains two integers $$$N$$$ and $$$B$$$ ($$$1 \le N \le 100000$$$, $$$0 \le B \le 10^9$$$), representing the number of cafes and Ashley's budget.

The second line contains $$$N$$$ integers $$$a_1, a_2, \ldots, a_N$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the price of a drink at cafe $$$i$$$.

Output

Print a single integer: the maximum number of consecutive cafes Ashley can visit without spending more than $$$B$$$ coins.

Examples
Input
7 15
4 7 2 6 3 8 1
Output
3
Input
5 100
12 8 15 9 6
Output
5
Input
4 3
5 4 7 6
Output
0
Note

Example 1.

Ashley can visit cafes 3 through 5.

The drink prices are 2, 6, and 3. Their total cost is $$$2 + 6 + 3 = 11$$$.

It is not possible to visit 4 consecutive cafes without spending more than 15 coins.

Therefore, the maximum number of consecutive cafes Ashley can visit is 3.

Example 2.

The total cost of visiting all the cafes is $$$12 + 8 + 15 + 9 + 6 = 50$$$.

Since 50 is less than or equal to Ashley's budget of 100 coins, she can visit all 5 cafes.

Therefore, the maximum number of consecutive cafes Ashley can visit is 5.

Example 3.

Every drink costs more than Ashley's budget of 3 coins.

Therefore, Ashley cannot visit any cafe, and the answer is 0.

G. Macaron Conveyor
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After a long day at CODEFEM, the organizers prepared several trays of macarons for the participants.

There are $$$N$$$ trays arranged from left to right on a conveyor belt. The $$$i$$$-th tray initially contains $$$a_i$$$ macarons.

The conveyor belt can only move macarons to the right. In one operation, you may choose a tray $$$i$$$ ($$$1 \le i \lt N$$$) that contains at least one macaron, and move exactly one macaron from tray $$$i$$$ to tray $$$i+1$$$.

The organizers want every tray to contain exactly the same number of macarons.

Determine whether this is possible.

If it is possible, print the minimum number of operations required to make all trays contain the same number of macarons.

Otherwise, print $$$-1$$$.

Input

The first line contains an integer $$$N$$$ ($$$1 \le N \le 100$$$), representing the number of trays.

The second line contains $$$N$$$ integers $$$a_1, a_2, \ldots, a_N$$$ ($$$0 \le a_i \le 10^4$$$), where $$$a_i$$$ is the number of macarons initially placed on the $$$i$$$-th tray.

Output

Print a single integer.

If it is possible to make every tray contain the same number of macarons, print the minimum number of operations required.

Otherwise, print $$$-1$$$.

Examples
Input
3
7 3 2
Output
5
Input
4
1 5 3 3
Output
-1
Input
5
2 2 2 2 2
Output
0
Note

Example 1.

In the first example, there are 12 macarons in total, so every tray must contain 4 macarons.

The trays initially contain 7, 3, and 2 macarons. Move 3 macarons from tray 1 to tray 2, then move 2 macarons from tray 2 to tray 3.

The total number of operations is 3 + 2 = 5.

Example 2.

In the second example, there are 12 macarons in total, so every tray would need to contain 3 macarons.

However, the first tray contains only 1 macaron. Since macarons can only move to the right, it is impossible to add macarons to the first tray.

Example 3.

In the third example, all trays already contain the same number of macarons, so the answer is 0.

H. Enough Cupcakes?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After a long day of preparing everything for CODEFEM, the organizers decided that everyone deserved a cupcake.

There are $$$N$$$ tables at the event. At table $$$i$$$, there are $$$a_i$$$ people sitting.

The organizers bought $$$C$$$ cupcakes, and every person should receive exactly one cupcake.

Determine whether there are enough cupcakes for everyone.

  • If there are enough cupcakes, print YES followed by the number of cupcakes remaining.
  • Otherwise, print NO followed by the number of additional cupcakes needed.
Input

The first line contains two integers $$$N$$$ and $$$C$$$ ($$$1 \le N \le 100$$$, $$$0 \le C \le 10^9$$$), representing the number of tables and the number of available cupcakes.

The second line contains $$$N$$$ integers $$$a_1, a_2, \ldots, a_N$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the number of people sitting at the $$$i$$$-th table.

Output

If $$$C$$$ is greater than or equal to the total number of people, print YES $$$x$$$, where $$$x$$$ is the number of cupcakes remaining.

Otherwise, print NO $$$x$$$, where $$$x$$$ is the number of additional cupcakes needed.

Examples
Input
4 20
3 5 4 2
Output
YES 6
Input
3 10
4 5 3
Output
NO 2
Note

Example 1.

There are $$$3 + 5 + 4 + 2 = 14$$$ people in total.

Since there are $$$20$$$ cupcakes, $$$20 - 14 = 6$$$ cupcakes remain. Therefore, the output is YES 6.

Example 2.

There are $$$4 + 5 + 3 = 12$$$ people in total.

Since only 10 cupcakes are available, $$$12 - 10 = 2$$$ additional cupcakes are needed. Therefore, the output is NO 2.

I. Fruit Punch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Alejandra is determined to prepare the best Christmas fruit punch in history this year. For that reason, she is starting with her plan at this point of the year. To do so, she has bought many ingredients that she planned to use for experimenting and trying all possible recipes until she finds the ideal one: the holy grail of Christmas punches.

When she arrived at his family's home with all his ingredients, her sister caught her early Christmas spirit and decided that she also wanted to prepare her own punch. She asked Alejandra to sell her some of the ingredients she had bought. Alejandra agreed, on the conditions that both of them use everything they choose and that they use different ingredients, for example, if one of them uses apples in her recipe, the other cannot use them.

Furthermore, Alejandra's sister has a limited budget and can only buy $$$k$$$ ingredients (Alejandra and her sister agreed that all ingredients would have the same price). However, she is certain that this will be enough to make a memorable punch for her friends.

Alejandra is really meticulous when it comes to cooking, so she wants to take some time to evaluate the possibilities that she could prepare. To do so, she first needs to know how many recipes she will have to assess. The problem is that Alejandra is a star in the kitchen, but terrible at doing calculations, so she needs your help to determine how many recipes can be prepared without the $$$k$$$ items she sold to her sister.

Input

The first line will contain a positive integer $$$1 \leq T \leq 100$$$: the number of test cases. Each case will be composed of two natural numbers $$$n$$$ ($$$1 \leq n \leq 10^{5}$$$) and $$$k$$$ ($$$1 \leq k \leq n$$$) separated by a blank, where the former is the number of ingredients that Alejandra bought and the latter is the number of ingredients that her sister wants to purchase from her.

Output

For each case, calculate the number of recipes that Alejandra can prepare, and print it on a distinct line modulo 122333221.

Example
Input
3
10 3
65 5
54321 12
Output
120
8259888
112530695

J. 8M
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

March 8th (8M) is a very important date for any woman, and Mar knows it. Although she felt very nervous about attending a march, she was always drawn to it, and this year she finally dared to go for the first time, motivated by her group of friends.

Upon arriving, she was amazed by the huge number of women filling the streets, and then she noticed something special: some women held ribbons tied to another's wrist, and some had ribbons tied back, representing their union. If a girl $$$A$$$ tied a ribbon to a girl $$$B$$$, then following the ribbons, $$$A$$$ can reach $$$B$$$, and from $$$B$$$ she can keep going to every woman that $$$B$$$ reaches.

Mar considers a group of women to be united if, following the direction of the ribbons, every member can reach every other member.

Mar notices that, although there are many ribbons, the women of the march are not united. She wants to know the minimum number of additional ribbons that must be tied so that all the women in the march form a single united group.

Input

The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N \le 2 \cdot 10^5, 0 \le M \le 2 \cdot 10^5$$$), representing the number of women in the march and the number of initial ribbon connections, respectively. The women are numbered from $$$1$$$ to $$$N$$$.

The following $$$M$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N$$$), indicating that woman $$$u$$$ has tied a ribbon to woman $$$v$$$ (the ribbon can only be followed in that direction). There can be multiple ribbons between the same pair of women, and even a woman with a ribbon tied to herself.

Output

Print a single integer, the minimum number of additional ribbons that must be added (tied with a direction) so that all the women in the march form a single united group.

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

In the first example, assuming women $$$1, 2, 3$$$, if we add a ribbon from woman 3 to woman 1, then every woman can reach every other woman, so the three of them become united.

In the second example, every woman can already reach every other woman, so the answer is 0.

K. JajaScript
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ada and Grace have been friends for a long time, but there is one thing they have never been able to agree on: what makes a message funny.

To settle the argument once and for all, each of them created a collection of JajaScript rules. Each rule describes a pair of expressions that, when they appear in a message, contribute to its funniness score.

A rule consists of two non-empty strings $$$A$$$ and $$$B$$$, and an integer $$$P$$$, the funniness rating of the rule.

Suppose that $$$A$$$ occurs $$$x$$$ times in the message and $$$B$$$ occurs $$$y$$$ times. Then this rule contributes $$$x \cdot y \cdot P$$$ points to the owner's total score.

All occurrences are counted, including overlapping occurrences. For example, the string aa occurs three times in aaaa.

The score of Ada (respectively, Grace) is the sum of the contributions of all of her rules. Rules are considered independently: the same expression may appear in several rules, the same rule may appear more than once, and $$$A$$$ may be equal to $$$B$$$.

Given the message and both collections of rules, determine whose JajaScript score is greater.

Input

The first line contains a non-empty string $$$S$$$, the message.

The second line contains an integer $$$N$$$, the number of Ada's rules.

Each of the next $$$N$$$ lines contains two non-empty strings $$$A_i$$$, $$$B_i$$$, and an integer $$$P_i$$$, describing one of Ada's rules.

The next line contains an integer $$$M$$$, the number of Grace's rules.

Each of the next $$$M$$$ lines contains two non-empty strings $$$A_i$$$, $$$B_i$$$, and an integer $$$P_i$$$, describing one of Grace's rules.

All strings consist only of lowercase English letters ($$$a$$$–$$$z$$$).

The following constraints hold:

$$$1 \le |S| \le 10^6$$$

$$$1 \le N, M \le 2 \cdot 10^5$$$

$$$1 \le P_i \le 10$$$

The sum of the lengths of all strings $$$A_i$$$ and $$$B_i$$$ in Ada's and Grace's rules does not exceed $$$10^6$$$:

$$$\sum (|A_i| + |B_i|) \le 10^6$$$.

Output

If Ada's score is greater, print

Ada P

where $$$P$$$ is Ada's total score.

If Grace's score is greater, print

Grace P

where $$$P$$$ is Grace's total score.

If both scores are equal, print

Empate P

where $$$P$$$ is their common score.

The answer fits in a signed 64-bit integer.

Examples
Input
rrallrrll
2
rr ll 5
r all 2
2
rr ll 3
all r 4
Output
Empate 28
Input
aaaa
2
aa a 2
aaa aa 3
1
a aa 4
Output
Grace 48
Note

In the first example:

  • rr occurs $$$2$$$ times;
  • ll occurs $$$2$$$ times;
  • r occurs $$$4$$$ times;
  • all occurs $$$1$$$ time.

Therefore, Ada's score is

$$$(2\cdot2\cdot5)+(4\cdot1\cdot2)=28$$$,

while Grace's score is

$$$(2\cdot2\cdot3)+(1\cdot4\cdot4)=28$$$.

Thus, the result is Empate 28.

In the second example, overlapping occurrences are important. In aaaa:

  • a occurs $$$4$$$ times;
  • aa occurs $$$3$$$ times;
  • aaa occurs $$$2$$$ times.

Ada's score is

$$$(3\cdot4\cdot2)+(2\cdot3\cdot3)=42$$$,

while Grace's score is

$$$4\cdot3\cdot4=48$$$.

Therefore, Grace wins with a score of $$$48$$$.

L. Coding in Stitches
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Frances has two great passions: programming and cross-stitching. Naturally, she eventually found a way to combine them.

She is working on a program to improve a long embroidery design. The design is represented by a string $$$b$$$, divided into sections by spaces. Each lowercase English letter represents the color of one stitch. For example, $$$p$$$ may represent pink, $$$g$$$ green, and $$$b$$$ blue; the actual colors are not important to Frances' program.

Frances has ten favorite embroidery patterns. Each pattern is represented by a string and has a beauty value:

PatternStitch patternBeauty
Tulippgr3
Moonbyyb5
Daisywwgyw7
Catbwwbwb8
Bowprpb10
Heartprrb12
Strawberryrrggr14
Sparklepywb17
Butterflyvpggpv20
Buggpbrywvpg25

A section can be turned into one of Frances' patterns if both strings have the same length and differ in exactly one position. Frances can then replace the stitch at that position and gains the beauty value of the resulting pattern.

If a section is already exactly equal to any of Frances' favorite patterns, Frances leaves that section unchanged. It cannot be modified and gives no beauty points.

If a section can be turned into more than one pattern, Frances only considers the pattern with the highest beauty value.

Replacing stitches that are too close to each other may deform the fabric. Therefore, if Frances replaces stitches at absolute positions $$$p_1$$$, $$$p_2$$$, ..., the distance between any two selected positions must be at least $$$x$$$.

Positions are 0-indexed and refer to the complete string $$$b$$$. Spaces also occupy positions in the string, although a space can never be replaced.

Help Frances determine the maximum total beauty she can obtain.

Input

The first line contains the string $$$b$$$, representing Frances' embroidery design.

The second line contains an integer $$$x$$$, the minimum allowed distance between any two stitches that Frances replaces.

A section is any maximal contiguous sequence of lowercase English letters in $$$b$$$.

The string $$$b$$$ contains only lowercase English letters ($$$a$$$–$$$z$$$) and spaces, and contains at least one lowercase English letter.

The following constraints hold:

$$$1 \le |b| \le 10^7$$$

$$$1 \le x \le 10^7$$$

The number of sections in $$$b$$$ does not exceed $$$10^6$$$.

Output

Print one integer: the maximum total beauty Frances can obtain.

The answer fits in a signed 64-bit integer.

Examples
Input
pyrb pgx
6
Output
17
Input
pyrb pgx
5
Output
20
Input
prrb pgx
1
Output
3
Input
hello world
4
Output
0
Note

In the first example, the section pyrb can be turned into:

  • Heart (prrb), worth $$$12$$$, by changing the stitch at absolute position $$$1$$$;
  • Sparkle (pywb), worth $$$17$$$, by changing the stitch at absolute position $$$2$$$.

Frances only considers Sparkle because it has the higher beauty value.

The section pgx can be turned into Tulip (pgr), worth $$$3$$$, by changing the stitch at absolute position $$$7$$$.

Therefore, the two candidate positions are $$$2$$$ and $$$7$$$. Their distance is $$$5$$$, which is smaller than $$$x = 6$$$, so Frances can choose only one modification. The maximum total beauty is $$$17$$$.

In the second example, the same candidate modifications occur at positions $$$2$$$ and $$$7$$$. Their distance is exactly $$$5$$$, so both modifications are allowed. Frances obtains $$$17 + 3 = 20$$$.

In the third example, the section prrb is already exactly equal to Heart. Frances leaves it unchanged, so it gives no beauty points and cannot be turned into another pattern. The section pgx can be turned into Tulip, giving $$$3$$$ beauty points.

In the fourth example, neither section can be turned into any of Frances' favorite patterns by replacing exactly one stitch.