2024-2025 ICPC German Collegiate Programming Contest (GCPC 2024)
A. Alien Attack 2
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Aliens are visiting Earth and, as usual, they plan to abduct humans for their experiments. In the past, alien abductions have caused a lot of press coverage and wild speculation on Earth. Luckily for them, most people do not believe these stories and think that aliens are not real.

In order to keep a low profile in the future, the Galactic Committee for Person Captures (GCPC) has established rules for abductions. Besides a lot of boring paperwork, the aliens have to prepare the abduction carefully. While they can make multiple trips (in fact, alien travel is so fast in practice that this is not a limitation at all), they must be smart about it so that their secret is not revealed to humans. If aliens want to abduct a person, they are required to abduct all of their friends at the same time, so that no one notices that their friend is missing when they want to hang out. Of course, friendships on planet Earth are bidirectional, that is if Alice is a friend of Bob, then Bob is also a friend of Alice.

In preparation for the trip, the aliens have observed their targets and started taking note of all their friendships. In total, they must abduct $$$n$$$ people, including their friends. Now, they want to book a starship at their local dealership and wonder how much space they need to abduct all $$$n$$$ people. A starship's storage space is measured in terms of the number of people that can be transported simultaneously. What is the minimum storage space required to abduct all $$$n$$$ people?

Input

The input consists of:

  • One line with two integers $$$n$$$ and $$$m$$$ ($$$1\leq n\leq {2 \cdot 10^5}$$$, $$$0\leq m\leq {2 \cdot 10^5}$$$), the number of people and the total number of friendships between them.
  • $$$m$$$ lines, each with two integers $$$i$$$ and $$$j$$$ ($$$1\leq i \lt j\leq n$$$), denoting a friendship between persons $$$i$$$ and $$$j$$$.
The people are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no friendship is listed multiple times.
Output

Output the minimum storage space needed to abduct all people.

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

B. Bookshelf Bottleneck
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Brianna is a bookworm. At home, she has a big bookshelf with all her favourite books. She has a large collection ranging from detective novels and science-fiction novels to biographies.

Recently, Brianna has expanded her collection with $$$n$$$ graphic novels. However, the new books currently lie around everywhere and form huge stacks on the floor. In the meantime, one of the shelf boards has collected dust and random household utensils that do not belong there. The new books just lying around have become too much to bear, and Brianna finally decided to put them on this shelf board. To do so, she first has to make room on it.

Visualization of Sample Input 3.

Brianna wants to arrange the books in a single horizontal line without stacking multiple books on top of each other. While the shelf is wide enough to hold all books without problems, it takes time to make room on the shelf. Therefore, Brianna wants to minimize the width of the part of the shelf that she needs to clear.

Each book can be described as a cuboid with three side lengths $$$l$$$, $$$w$$$, and $$$h$$$. Since the room above the shelf board is limited by the next shelf board above it, she can only fit a book vertically if its vertical side length is at most the distance $$$H$$$ between the two shelf boards. Brianna may rotate each book in three-dimensional space as she wants. It is guaranteed that the shelf is deep enough so that the books will not fall off, no matter the orientation. However, all books must stand properly on the shelf board, meaning that every book touches the shelf board along an entire face and not just by an edge.

What is the minimum width of shelf Brianna's books need?

Input

The input consists of:

  • One line with two integers $$$n$$$ and $$$H$$$ ($$$1\leq n\leq 10^5$$$, $$$1\leq H \leq 10^9$$$), the number of books and the height of the shelf, respectively.
  • $$$n$$$ lines, each containing three integers $$$l$$$, $$$w$$$, $$$h$$$ ($$$1\leq l,w,h \leq 10^9$$$), the dimensions of the books.
Output

Output the minimum width of shelf Brianna's books need, or "impossible" if it is impossible to place the books on the shelf.

Examples
Input
1 3
10 2 5
Output
5
Input
1 3
10 4 5
Output
impossible
Input
2 10
10 2 10
2 3 4
Output
4
Input
3 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000
Output
3000000000

C. Copycat Catcher
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Your university recently established the Graduate Code Plagiarism Control (GCPC) initiative to get hold of the ever-increasing load on the graders for the computer science assignments. Currently, the graders need to check the code of assignments manually for plagiarism. The GCPC aims to simplify this part of the graders' jobs by performing the plagiarism checks automatically.

Code consists of tokens separated by spaces. Tokens are strings of alphabetical letters, numerals, and brackets. If a token consists of only a single alphabetical letter (upper or lowercase), it is a variable in the code.

The GCPC wants the plagiarism checker to compare query pieces of code to a reference code. Specifically, it should check whether each query could have been obtained by selecting a contiguous string of tokens from the reference and consistently renaming variables. Variables are consistently renamed if no two occurrences of the same variable are renamed to different variables, and if no two different variables are renamed to the same variable.

The GCPC has asked you to develop the plagiarism checker.

Input

The input consists of:

  • A description of the reference, consisting of:
    • One line containing an integer $$$n$$$ ($$$1\leq n \leq 2\,000$$$), the number of tokens in the reference.
    • One line containing $$$n$$$ tokens, each consisting only of the characters 'a'-'z', 'A'-'Z', '0'-'9', '(' and ')'.
  • An integer $$$q$$$ ($$$1 \leq q \leq 2\,000$$$), the number of queries.
  • $$$2\cdot q$$$ lines, each two lines in the same format as the reference.
It is guaranteed that each query as well as the reference consist of at most $$$2\,000$$$ characters (excluding spaces). Tokens are separated by single spaces.
Output

For each query, output "yes" if the query could have been obtained from the reference, and "no" otherwise.

Examples
Input
9
for i in range(10) do print i j end
4
3
print j i
2
do print
6
k in range(10) do print k
6
k in range(10) do print j
Output
yes
yes
yes
no
Input
5
i is i times j
7
5
i is i times j
5
a is a times b
5
j is j times c
5
a is i times j
5
j is i times j
5
0 is 0 times j
5
i is i times i
Output
yes
yes
yes
no
no
no
no
Input
5
A 1 ( ) b
4
2
b 2
2
b 1
3
1 ) (
5
a 1 ( ) F
Output
no
yes
no
yes

D. Dark Alley
time limit per test
2.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

One cold and foggy night, you walk down a shady alley. There should be a lamp every few metres but none of them seem to work, and in this night, not even the moon enlightens your path. Alone and in the dark, you wonder: "Even if there was a working lamp somewhere, how much would it lighten my way?". Now, back at home, you want to calculate this.

The alley can be modelled as a line with a length of $$$n$$$ metres. The fog has a uniform density and reduces the light of a lamp by a factor of $$$1-p$$$ every metre. The brightness at one point is the sum of the light that reaches this point from every lamp. You want to calculate this brightness at some points after placing some lamps.

Input

The input consists of:

  • One line with two integers $$$n$$$ and $$$q$$$ and one real number $$$p$$$ ($$$1\leq n, q\leq 2\cdot10^5, 0 \lt p \lt 1$$$), the length of the alley, the number of queries and the density of the fog. The density $$$p$$$ of the fog will be given with at most $$$6$$$ digits behind the decimal point.
  • $$$q$$$ lines containing one of three query types:
      [noitemsep,topsep=0pt,parsep=0pt,partopsep=0pt]
    • "+ b x" given two integers $$$b$$$ and $$$x$$$ ($$$1\leq b \leq 10^9$$$ and $$$1\leq x \leq n$$$), place a lamp with brightness $$$b$$$ at position $$$x$$$.
    • "- b x" given integers $$$b$$$ and $$$x$$$ ($$$1\leq b \leq 10^9$$$ and $$$1\leq x \leq n$$$), remove a lamp with brightness $$$b$$$ at position $$$x$$$. It is guaranteed that a lamp with that brightness was placed there earlier.
    • "? x" given one integer $$$x$$$ ($$$1\leq x \leq n$$$), calculate the brightness at position $$$x$$$.
Output

It can be shown that the brightness can be calculated as a fraction $$$\frac{P}{Q}$$$ where $$$Q$$$ is not divisible by $$$10^9+7$$$. For each query of type "?", print the brightness as $$$P\cdot Q^{-1} \bmod 10^9+7$$$ in a single line.

Examples
Input
5 6 0.25
+ 4 2
? 1
? 2
? 3
? 4
? 5
Output
3
4
3
250000004
187500003
Input
5 7 0.33
+ 9 1
? 5
+ 4 3
? 2
? 5
- 9 1
? 2
Output
312342734
470000012
341542736
760000008
Note

The brightness in the alley after placing the lamp will look like this:

3432.251.6875

E. Even Odd Game
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

As every Saturday, Eve is paying her local game club a visit. Today, Bob has brought a new game he has designed on his own. After thorough investigation, you claim that there exists a dominant strategy, so one player can always win. Bob is hesitant to accept that his game is boring to play, so he challenges you to play a round! Since you claim that there exists a dominant strategy, you may determine who begins the game.

The cards in the two sample interactions.

The rules are as follows. There are $$$n$$$ cards on the table, each containing a maths operation (+ or *) and an integer, as shown in Figure above. Eve and Bob alternate picking cards to manipulate a common number using one of the cards. For instance, if the current value of the number is $$$5$$$, and the card says $$$+3$$$, then the value gets updated to $$$8$$$. Each card can only be used once, and the game ends when all the cards have been used. The player who makes the first move wins if the final number is odd, and the other player wins if the final number is even. Given the list of cards and the starting value, help Eve win the game.

Interaction

The interactor begins by outputting the starting state of the game in the following format:

  • One line with an integer $$$n$$$ ($$$1 \le n \le 300$$$), the number of cards.
  • $$$n$$$ lines, each with a character $$$o$$$ and an integer $$$x$$$ ($$$o \in \{$$$'+'$$$, $$$'*'$$$\}$$$, $$$1 \le x \le 10^6$$$) indicating one of the cards.
  • One line with an integer $$$x$$$ ($$$1 \le x \le 10^6$$$), the starting value.
It is guaranteed that the $$$n$$$ cards in the input are distinct.

Then, your submission must output one of "me" or "you", depending on if you want to go first or you want the interactor to go first.

Then your submission and the interactor alternate outputting cards in the same format as above. No card can be played more than once. Your submission must exit when all cards have been played, regardless of which player made the last move.

Your submission will be accepted if it follows all the rules above and the final value has the parity indicated on the first line of your output.

Make sure you flush the buffer after each write.

A testing tool is provided to help you develop your solution.

Examples
Input
3
+ 7
* 4
+ 2
1


* 4

Output





me
+ 2

+ 7
Input
2
+ 8
* 5
10

+ 8

Output




you

* 5

F. Fair Fruitcake Fragmenting
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Frida's birthday is just coming up, and as her best friend, you obviously baked a cake for her. Since you know that Frida loves rotational symmetry, you thought to bake a cake that looks the same from above when rotated by $$$180^\circ$$$. Of course, you could have simply baked a boring round cake, but without a perfectly round cake tin, this sounds easier than done. Therefore, you decided to bake a cake whose shape can be described by straight line segments.

Visualization of Sample Input 2. The swirly cake looking like an S can be cut into the red and blue part with a single cut.

However, after you are done with your cake, you notice that you also want to cut the cake into two equal pieces, one for Frida and one for yourself. More precisely, you wonder if it is possible to cut the cake along an infinite line such that it splits into exactly two parts of equal weight. You can assume that the cake has uniform density and height.

Input

The input consists of:

  • One line containing an even integer $$$n$$$ ($$$4 \leq n\leq 10^5$$$), the number of points needed to describe the cake's shape.
  • $$$n$$$ lines, each containing two integers $$$x$$$, $$$y$$$ ($$$0\leq x,y \leq 10^6$$$), the $$$x$$$ and $$$y$$$ coordinates of a point on the border of the cake's shape.
The following additional guarantees are given for the shape of the cake:
  • The cake has a $$$180^\circ$$$ rotational symmetry.
  • The points are given in counterclockwise order.
  • No three consecutive points are collinear.
  • The shape is simple (no segments intersect and only consecutive segments touch at their ends).
Output

Output two different points on the desired line as $$$x_1$$$/$$$c_1$$$ $$$y_1$$$/$$$d_1$$$ $$$x_2$$$/$$$c_2$$$ $$$y_2$$$/$$$d_2$$$, where $$$|x_i|$$$, $$$|y_i|$$$, $$$|c_i|$$$ and $$$|d_i|$$$ are integers and at most $$$10^9$$$, and $$$x_i$$$/$$$c_i$$$ is the first coordinate of point $$$i$$$ and $$$y_i$$$/$$$d_i$$$ is the second ($$$1\leq i\leq2$$$). If the denominator of a fraction is $$$1$$$ you may output only the numerator. Fractions do not have to be reduced. If there is no such line, output "impossible" instead.

It can be shown that if there is a line as desired, it is possible to represent it in the given format.

Examples
Input
4
0 0
2 0
2 2
0 2
Output
1 1 1337/42 3141/1000
Input
20
7 1
8 2
8 5
7 6
4 6
4 4
3 4
3 7
6 7
7 8
2 8
1 7
1 4
2 3
5 3
5 5
6 5
6 2
3 2
2 1
Output
11 13 -2 -4
Input
10
11 5
10 2
12 6
2 2
7 3
1 1
2 4
0 0
10 4
5 3
Output
impossible

G. Geometric Gridlock
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Pentominous is a grid logic puzzle based on the twelve pentominoes. A pentomino is a polygon formed by connecting five equal-sized squares edge to edge.

The twelve pentominoes (up to mirroring and rotations) and their names.

The goal of this puzzle is to divide a grid into regions of size $$$5$$$ (that is, pentominoes), so that no two regions that share a side have the same shape. You are allowed to rotate and mirror the pentominoes, but such rotations and reflections count as the same shape. The twelve possible shapes can be seen in Figure above.

In a normal Pentominous puzzle, the player is given some pre-filled cells, for which the shape of their region is already predetermined. In this problem, you are working with a completely blank grid of dimensions $$$h\times w$$$, and your task is to create any valid arrangement of pentominoes.

Input

The input consists of:

  • One line with two integers $$$h$$$ and $$$w$$$ ($$$1 \le h,w \le 100$$$), the height and width of the grid.
Output

If there is no valid $$$h\times w$$$ Pentominous grid, output "no". Otherwise, output "yes", followed by $$$h$$$ lines of width $$$w$$$ each, a possible grid using the letters from the problem statement. If there is more than one solution, any one of them will be accepted.

Examples
Input
3 5
Output
yes
UUXUU
UXXXU
UUXUU
Input
2 10
Output
yes
LLLLNNNPPP
LIIIIINNPP
Input
99 17
Output
no
Input
6 10
Output
yes
IPPYYYYVVV
IPPXYLLLLV
IPXXXFZZLV
ITWXFFFZUU
ITWWNNFZZU
TTTWWNNNUU
Input
1 5
Output
yes
IIIII
Note

Illustration of Sample Output 1.

Illustration of Sample Output 2.

Illustration of Sample Output 4.

H. Headline Heat
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The German ICPC scene is widely considered one of the most competitive. At least, that's what we tell our students. Countless rivalries form a complex web of envy, despair, glory, and triumph woven around and manifested in the unrelenting echoes of two scoreboards – Winter Contest and GCPC. While generations of participants tend to forget the grudges of their predecessors, we coaches, acting as timeless beacons of continuity, preserve these petty conflicts between long forgotten teams. Striving for perfect balance, we express our dedication to our coaching duties in a furious outcry on social media against every unfair news article. That is, a coach gets mad if a news article is published that contains a rival university name more often than their own.

To smoothen the waves of conflict in this ocean of rage, a newly appointed authority is tasked with proofreading media coverage of GCPC and Winter Contest to prevent uneven coverage.

Input

The input consists of:

  • One line with the number of universities $$$n$$$, rivalries $$$m$$$, and articles $$$k$$$. ($$$1\leq n,m,k\leq 10^5$$$)
  • $$$n$$$ lines containing the name of a university $$$p_i$$$.
  • $$$m$$$ lines containing two integers $$$u,v$$$, meaning that universities $$$u$$$ and $$$v$$$ are rivals. ($$$1\leq u,v \leq n, u\neq v$$$)
  • $$$k$$$ lines containing a news article $$$t_i$$$.

If a university $$$u$$$ is a rival of university $$$v$$$, then $$$v$$$ is also a rival of $$$u$$$. Moreover, there are no duplicate rivalries.

Names and articles are strings of lowercase Latin letters and spaces. The first and last character of a name or article are never a space. Names can overlap and be contained in other names.

The summed length of all names and articles is at most $$$10^6$$$, i.e. $$$\sum_{i=1}^{n} |p_i| + \sum_{i=1}^{k} |t_i| \leq 10^6$$$.

Output

For each article, output "no" if it will draw the wrath of at least one coach and "yes" otherwise.

Examples
Input
3 1 4
hpi
fau
kit
1 3
kit destroys hpi at wintercontest
gcpc is great
team moshpit from hpi beats kit teams
whats the abbreviation for university of erlangen nuremberg
Output
yes
yes
no
yes
Input
6 3 5
uds
cu
tum
rwth
uni ulm
uni
4 1
2 5
1 3
last gcpc rwth had a team in top ten two places behind tum
who is team debuilding from constructor university bremen
top ten teams last year are from kit cu uds hpi tum and rwth
uni ulm cu uni ulm
sunday alright lets go
Output
no
yes
no
no
yes

I. Interference
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Physics can be so much fun! Yesterday, your teacher explained how interference works: If you have two waves, their heights add up over the whole waves' length! So if both waves have a peak, the resulting peak will be even higher. Likewise, if both waves have a wave trough below the water surface, the resulting wave has a trough that will be even further below. Technically, a wave's height is called amplitude and the distance between two wave peaks is called wavelength.

Today, your physics teacher describes the setup of an experiment she is about to perform. She will create stationary waves in a one-dimensional container of water. Due to her superior control over physical elements, all waves will have a precisely controlled amplitude and will only be created in an interval of given length. The wavelength of each wave is always $$$4$$$ and the first positive peak will always be at the first index of the interval. We only measure the wave's amplitude at integer points. For example, a wave with amplitude $$$2$$$ and length $$$9$$$ can be described as 2 0 -2 0 2 0 -2 0 2. If there is no wave at a point, the amplitude is $$$0$$$. Your task is to predict how high the resulting wave will be at given points in the container taking into account all the waves that were created up to that point.

Input

The input consists of:

  • One line with two integers $$$n$$$ and $$$w$$$ ($$$1 \le n \le 4\,000$$$, $$$1 \le w \le 10^{9}$$$), the number of lines and the width of the container.
  • $$$n$$$ lines, each containing either a wave description or a prediction task:
    • "!$$$~p~\ell~a$$$", a wave description with starting position $$$p$$$, length $$$\ell$$$ $$$(1 \le p, \ell \le w)$$$, and amplitude $$$a$$$ $$$(1 \le a \le 10^9)$$$. It is guaranteed that $$$p+\ell - 1 \le w$$$.
    • "?$$$~p$$$", a prediction task for the resulting wave at position $$$p$$$ ($$$1 \le p \le w$$$).
See Notes for a partial visualization of Sample 2.
Output

For each prediction task, output a line with a single integer, the height of the wave resulting from all former described waves at the requested position.

Examples
Input
4 10
! 2 7 1
? 9
? 7
? 6
Output
0
0
1
Input
7 10
! 2 6 1
! 3 8 2
! 5 2 3
? 6
! 5 5 4
? 8
? 9
Output
1
0
2
Input
6 12
! 1 7 1
! 7 3 2
? 6
? 7
? 8
? 10
Output
0
1
0
0
Input
6 11
! 1 6 1
? 6
! 5 7 4
? 6
! 6 3 2
? 6
Output
0
0
2
Note
Interference of three waves in Sample Input 2. The black dots represent the resulting wave's height.

J. Jigsaw Present
time limit per test
6 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Julia is preparing a present for James. She will give him some of her $$$n$$$ jigsaw puzzles, where puzzle $$$i$$$ ($$$1 \leq i \leq n$$$) consists of $$$x_i$$$ pieces and has a difficulty $$$y_i$$$ (can be negative if the puzzle is very easy).

James is already very excited and would like to know in advance what he will get. Therefore, he used some of his criminal energy to gather information about the gift. In particular, he has managed to obtain an encrypted message containing the total difficulty and total number of pieces of all the puzzles that he will receive.

Now he wonders whether it is worth spending some more time to decrypt the message. After all, it might be that this information is not enough to uniquely determine his gift. Since he was never good at these computer thingies, James asked for your assistance. Help him find out whether it is worth decrypting the message or not. If the answer is negative, you have to find two distinct gifts that result in the same encrypted message.

Input

The input consists of

  • One line with an integer $$$n$$$ ($$$2 \leq n \leq 4\,096$$$), the number of puzzles that Julia owns.
  • $$$n$$$ lines, the $$$i$$$th of which contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i \leq 4\,096$$$, $$$\left|y_i\right| \leq 4\,096$$$), the number of pieces of puzzle $$$i$$$ and the difficulty of puzzle $$$i$$$.
Output

If James can uniquely determine his gift, then print "yes". Otherwise, you should print "no" followed by two lines, where each line contains the description of a present. The description of a present should start with an integer $$$k$$$, the number of puzzles, followed by $$$k$$$ distinct integers, the indices of the puzzles.

Note that the two presents have to be distinct, meaning that there should be at least one puzzle that is contained in one present but not the other.

If there are multiple presents that result in the same encrypted message, you can print any of them.

Examples
Input
5
2 -1
3 2
3 1
1 -3
1 1
Output
no
3 2 4 5
2 1 3
Input
4
2 -1
3 2
3 1
1 -3
Output
yes
Note

In the first sample case, the first present consists of puzzles $$$2$$$, $$$4$$$, and $$$5$$$. The total number of pieces is $$$3 + 1 + 1 = 5$$$ and the total difficulty is $$$2 + (-3) + 1 = 0$$$. The second present consists of puzzles $$$1$$$ and $$$3$$$. The total number of pieces is $$$2 + 3 = 5$$$ and the total difficulty is $$$(-1) + 1 = 0$$$. Thus, if James only knows the total number of pieces and the total difficulty, he cannot recover his present. So it is not worth to decode the message.

In the second sample case, no matter what gift Julia prepares, if James knows the total number of pieces and the total difficulty, he can recover his present. So he should decode the message.

K. Kitten of Chaos
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Karen has a beautiful precious glass object on the shelf in her living room. Unfortunately, her cat Klaus does not like it when there is stuff on his favourite shelf. Everything that is not bolted or glued in place, he will gradually push over the edge while looking Karen straight in the eyes.

Now, Klaus' paw slowly executes his diabolical deed. His cute fluffy face radiates inadvertent innocence. Knowing that any intervention would only delay the inevitable, Karen wonders what will happen to the string her sister Kim wrote on the precious glass object. After all, it took Kim a whole week to gather all the bdpq letters that make up the string.

Can you describe to Karen what the string will look like from her point of view while it tumbles towards destruction?

While falling off the shelf, Karen's precious glass object is subject to the following transformations, described as seen when looking at the object from the front.

  • h: horizontal flip, e.g. bbq becomes pdd
  • v: vertical flip, e.g. bbq becomes ppd
  • r: 180-degree rotation, e.g. bbq becomes bqq

No flips along or rotations about any other axes are possible.

Input

The input consists of:

  • One line with a string $$$s$$$ consisting of the letters bdpq ($$$1 \le |s| \le 5\cdot 10^5$$$), the string printed on the glass object as seen at the start of the fall.
  • One line with a string $$$t$$$ consisting of the letters hvr ($$$1 \le |t| \le 5\cdot 10^5$$$) giving the sequence of transformations in the order that they occur during the fall.
Output

Output the string that can be seen at the moment the glass object touches the ground and just before it shatters into pieces.

Examples
Input
bbq
h
Output
pdd
Input
bbq
v
Output
ppd
Input
bbq
r
Output
bqq
Input
ppbddbq
hvrhv
Output
bqppqdd

L. Laundry
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Every Sunday is laundry day, and there is always a huge pile of clothes waiting to be washed, which is certainly going to take you forever. You are particularly annoyed by how careful you have to be when washing certain items, and how important it is that you choose an appropriate washing programme for each item.

Fortunately, your washing machine is quite old and only supports three different washing programmes: A, B, and C. You can put at most $$$k$$$ items in one load, and each load can be washed using one of the programmes.

Some items are easy to care for, and you can put them in any load you like. More delicate items must not be washed using a specific programme, but the other two are fine. Of course, the worst clothes are the ones for which only one programme is appropriate.

You have already sorted the items into seven piles by putting items together for which the same combination of programmes is fine, so you know how many items are in each pile.

What is the minimum number of loads you need to wash?

Illustration of Sample Input 2 with an optimal solution. The figure on the left shows seven piles, one for each combination. The figure on the right shows a (possible) optimal solution, where each pile is washed in one load. The numbers on the pile represent how many items of each combination are washed with this load. In particular, the leftmost pile is washed using programme A, the two piles in the middle with programme B, and the two piles on the right with programme C. Thus, we need five loads to wash all items, which is optimal since we have $$$15$$$ items in total.
Input

The input starts with a line containing one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$), the number of test cases. Then for each test case:

  • One line with an integer $$$k$$$ ($$$1\leq k\leq 10^9$$$), the number of items you can put in one load.
  • One line with seven integers $$$c_1, \ldots, c_7$$$ ($$$0 \leq c_i \leq 10^9$$$), the number of items for each combination of programmes. The integers are given in this order: A, B, C, AB, BC, AC, ABC. For example, $$$c_4$$$ must be washed using either programme A or programme B.
Output

For each test case, output the minimum number of loads that are needed to wash all clothes.

Examples
Input
4
10
15 11 9 5 2 7 1
120
0 0 0 0 0 0 0
6
5 6 8 9 1 0 0
1213
295053681 137950336 87466375 956271897 344992260 31402049 988259763
Output
6
0
6
2342454
Input
1
3
1 2 1 3 3 2 3
Output
5

M. Musical Mending
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Shortly before the concert starts, you notice that your piano is completely out of tune! Having the ability of relative pitch, you are able to discern the difference between the pitch of any piano key to the first piano key. While this does not help you find the absolute pitch, you decide to at least tune the keys relative to each other. To do this, you need to make sure that the pitch of each key is exactly one higher than the key before it and one lower than the key after it. As the concert will start shortly, you need to minimize the total tuning effort, which is the sum of the absolute changes in pitch you apply to each key. For example, the picture below illustrates a solution for Sample Input 3, resulting in a total tuning effort of 23.

Visualization of Sample Input 3.
Input

The input consists of:

  • One line with an integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$), the number of keys on the piano.
  • One line with $$$n$$$ integers $$$t_1,\dots,t_n$$$ ($$$-2 \cdot 10^5 \leq t_i \leq 2 \cdot 10^5$$$), where $$$t_i$$$ describes the difference in pitch between the $$$i$$$th key and the first key. The first integer $$$t_1$$$ is always $$$0$$$.
Output

Output the minimal total tuning effort.

Examples
Input
7
0 1 2 3 4 5 6
Output
0
Input
5
0 1 4 3 6
Output
4
Input
6
0 -2 10 6 7 -1
Output
23
Input
4
0 -4 -2 1
Output
7
Input
9
0 23452 145043 -3423 -20 9845 435 -3 4453
Output
186237