2023 Abakoda Long Contest
A. ABCs of Men and Women, Part 2
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice, Bob, and Cindy are spending their summer abroad together! Not for a vacation though—they all found internship opportunities at a cluster of schools that are located conveniently close together. Thus, the three of them have decided to room together at a shared apartment for the duration of their stay.

They have been staying at this apartment for $$$n$$$ days so far. During each day, exactly one of Alice or Bob or Cindy is responsible for doing the cooking for that day. They don't particularly care about who handles cooking duty on each day, with whoever was in the mood for it usually being the one to take over. But their parents called and expressed their concern about whether the work was being split fairly.

Specifically, the parents want the following property to hold:

  • There exists a sequence of three consecutive days such that Alice cooked on one day, Bob on another day, and Cindy on the remaining day (not necessarily in that order).
If this condition does not hold, then they should assign cooking duties for the next $$$k$$$ days such that it does hold.

Find the minimum $$$k$$$ number of extra days of cooking duty needed in order for it to be possible for the parents' condition to be satisfied.

Input

The first line of input contains a single integer $$$n$$$.

The second line of input contains a string $$$s$$$ of length $$$n$$$, with each character being A or B or C. The $$$i$$$th letter of $$$s$$$ is A if Alice cooked on the $$$i$$$th day; resp. B for Bob and C for Cindy.

Output

Output a single integer, the minimum number of extra days needed until the parents' condition can be fulfilled.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline \text{The string $s$ contains only }\mathtt{A}\text{ or }\mathtt{B}\text{ or }\mathtt{C}\text{ characters.} \\ 1 \leq n \leq 2 \times 10^5 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{20} & n \leq 3 \\ \hline 2 & \mathbf{20} & n \leq 6 \\ \hline 3 & \mathbf{20} & n \leq 500 \\ \hline 4 & \mathbf{20} & \text{There is no }\mathtt{C}\text{ character in $s$.} \\ \hline 5 & \mathbf{20} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
5
BAABB
Output
2
Input
4
ACBA
Output
0
Note

In the first sample input, we can satisfy the condition after $$$k=2$$$ extra days; for example, we could have Alice cook tomorrow, and then Cindy cook the day after that.

In the second sample input, the condition already holds, so we don't need to use any extra days.

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

Here is a well-known trick question about Tagalog:

How many letters are in the word, "bunga"?

The answer is, of course, four, because in Tagalog, "ng" is considered to be a single "letter" (albeit written with two symbols)—that is, the letters are b-u-ng-a. So, for example, the word "pangngalan" has 8 letters.

Since Tagalog is so good, Alice wants to create a Tagalog 2. In Tagalog 2, she will choose two distinct consonants—let's write them as $$$xy$$$—such that this substring of two characters is counted as a single "letter" (each other character, as normal, counts as one letter). Given a string $$$s$$$, she must choose these consonants $$$x$$$ and $$$y$$$ such that the string $$$s$$$ is counted as having $$$k$$$ letters in Tagalog 2. If there are multiple possible answers, Alice wants the lexicographically smallest one (i.e. the one that would come earliest in the dictionary).

Please help Alice answer this question for all integers $$$k$$$ from $$$1$$$ to $$$|s|$$$ (the length of $$$s$$$). Good luck!

For the sake of this problem, y is always considered as a consonant.

Input

Input consists of a single line containing the string $$$s$$$.

Output

Output $$$|s|$$$ space-separated strings, the $$$k$$$th of which is the answer to the problem for that value of $$$k$$$:

  • If the task is possible, output the two lowercase consonants $$$xy$$$ that answer the question. If the task is not possible, output NO instead.
Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline \text{The string $s$ contains only lowercase letters.} \\ 1 \leq |s| \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{40} & |s| \leq 5 \\ \hline 2 & \mathbf{30} & |s| \leq 500 \\ \hline 3 & \mathbf{30} & |s| \leq 5\times 10^4 \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
bunga
Output
NO NO NO ng bc
Input
thwth
Output
NO NO th hw bc
Note

(Unimportant historical footnote): This quirk was decided on as a carryover from the fact that the velar nasal sound (represented in IPA notation as ŋ) is represented by its own standalone symbol ᜅ in the Baybayin script used in the pre-colonial Philippines, mostly in Luzon. The switch to the "ng" digraph happened during the time of Spanish colonization, when the Philippines adopted the Latin alphabet. And in fact, even then, there are records of Spanish documents representing the ŋ sound with the single symbol g̃ in their writings (think of ñ being used for the "ny-" sound) before the use of "ng" superseded it.

C. Canonizing Cannonade
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Here is a well-known joke among Jesuits:

Q: When was St. Ignatius of Loyola canonized?

A: At the Battle of Pamplona

Bob is helping choreograph a mock battle at his school, where the participants are role-playing as soldiers at the Battle of Pamplona.

A formation is an $$$r \times c$$$ grid with $$$m$$$ of its squares containing a soldier. The enemy can spend one cannonball to do the following:

  • Select a row or column, and eliminate all soldiers in the chosen row or column.
Let the resiliency of a formation be the minimum number of cannonballs needed to eliminate all its soldiers.

You are given $$$r, c, m, k$$$. Construct any formation such that its resiliency is exactly equal to $$$k$$$ (or say if the task is impossible). Also, please answer $$$T$$$ different test cases per file.

Input

The first line of input contains a single integer $$$T$$$. Then, the descriptions of each of the $$$T$$$ test cases follows.

Each test case is described by a single line containing the four space-separated integers $$$r, c, m, k$$$.

Output

For each test case, if the task is possible, output a line containing the word YES. Otherwise, output NO.

If YES, also output an $$$r \times c$$$ grid—that is, output $$$r$$$ lines, each containing a string of $$$c$$$ characters. The grid should consist only of . and # characters, representing empty space and squares with soldiers, respectively—you are reminded that exactly $$$m$$$ of these characters should be #

If there are multiple possible solutions, any will be accepted.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 0 \leq T \leq 1500 \\ 1 \leq r, c \leq 25 \\ 1 \leq m \leq rc \\ 1 \leq k \leq 10^9 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{40} & m=k \\ \hline 2 & \mathbf{30} & m=2k \\ \hline 3 & \mathbf{20} & m=3 \\ \hline 4 & \mathbf{10} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
2
7 8 11 4
9 6 2 10
Output
YES
........
...#....
#..#...#
#....#.#
...##.#.
........
...#....
NO
Note

In the first test case, you can verify that the soldiers can all be cleared with $$$4$$$ moves—select the third, fourth, and fifth rows from the top, and also select the fourth column from the left. Since you can't do it in $$$3$$$ moves, we conclude that the resiliency is $$$k=4$$$.

In the second test case, the task is impossible.

D. Dragon These Nuts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Cindy has been playing the new Pokemon game, and she's having a great time! Her team consists of five Dragon-type Pokemon (because she thinks they look awesome!), as well as her favorite Pokemon Magumaggu (the Lava Pokemon), the pre-evolution of Magcargo.

The latest entry in the Pokemon franchise contains several cooking minigames, where Cindy can prepare meals for her Pokemon to eat. In one cooking minigame, there are $$$n$$$ almonds, labeled $$$1$$$ to $$$n$$$, which her Magumaggu must roast. The $$$i$$$th almond must be roasted at least $$$a_i$$$ times. At the start of the minigame, the almonds are placed in a row in some random order. In one breath, Magumaggu can do the following:

  • Choose a subsegment of $$$k$$$ consecutive chestnuts and hit them with its fire breath; all chestnuts in this range get roasted once.
Let the power-point cost of this minigame be the minimum number of breaths needed to make each chestnut roasted its desired number of times. Help Cindy perform a worst case analysis—please provide any permutation of the $$$n$$$ almonds which maximizes the power-point cost of this minigame with those almonds.
Input

The first line of input contains a the two space-separated integers $$$n$$$ and $$$k$$$.

The second line of input contains $$$n$$$ space-separated integers $$$a_1, a_2, a_3, \dots, a_n$$$.

Output

Output a single line containing a permutation of the integers from $$$1$$$ to $$$n$$$ (separated by spaces), corresponding to a worst-case order in which the almonds should be placed (from left to right). If there are multiple possible solutions, any will be accepted.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \leq k \leq n \leq 2 \times 10^5 \\ \text{$1 \leq a_i \leq 10^9$ for each $i$.} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{40} & n \leq 5 \\ \hline 2 & \mathbf{40} & \text{$a_i \leq 2$ for each $i$} \\ \hline 3 & \mathbf{5} & \text{$a_i \leq 2023$ for each $i$} \\ \hline 4 & \mathbf{15} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
5 3
7 77 2 22 222
Output
5 4 3 2 1
Note

For the sample test case, the proposed solution would have the almonds' values in the order $$$[222, 22, 2, 77, 7]$$$.

(Translator's Note): Magumaggu is only a transliteration of the Japanese name of the pre-evolution of Magcargo.

E. Experiment! AMOGUS Edition
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This is an interactive problem.

It is the end of the year 2023. Alice still shouts AMOGUS at the top of her lungs. She believes it to be the funniest joke ever created in all of human history. She has even purchased an inflatable Amogus costume, which she will treasure for years to come. Truly, Amogus is eternal.

The Earth's greatest scientists have designed a new mod of the game, which introduces an interesting twist to the mechanics. Alice has been hired as one of their playtesters, and her job is to determine whether or not this mod is actually fun.

  • A lobby can contain an arbitrary number of players—let's call this number $$$n$$$.
  • Exactly one player is (secretly) chosen to be the imposter.
  • Another different player is (secretly) chosen to be the witness—the witness knows who the imposter is at the start of the game.
  • Alice always chooses a special role called the detective; the detective is not a player.
Up to $$$k$$$ times, the detective may call an emergency meeting, where they choose a subset of the players and interrogate them. This is the chance for the witness to tell the detective who the imposter is, but there is a twist—if the imposter is also in that same emergency meeting, then the witness is frightened into silence and cannot speak. Formally:
  • If the witness is present and the imposter is not, then the detective learns the identity of the imposter.
  • If both the witness and the imposter are present, then nothing happens.
  • In all other cases, nothing happens as well.
Alice wants to know how big of a lobby is possible (i.e. the maximum value of $$$n$$$) such that it is always possible to determine the identity of the imposter using only the $$$k$$$ meetings. She has a solution already (she has an IQ of $$$10^{100}$$$ after all) but wants you to independently solve this problem as well, to verify her approach.
Interaction

The judge first outputs a single integer $$$k$$$, the number of emergency meetings. For this problem, $$$k=18$$$ always.

Next, you should output a single integer $$$n$$$, the number of players you wish to invite to your lobby; this $$$n$$$ should be at least $$$2$$$ and at most $$$5 \times 10^4$$$. In general, the higher this $$$n$$$, the better your score will be. The players are indexed from $$$1$$$ to $$$n$$$.

The following interaction then plays out up to $$$k$$$ times:

  • The detective calls an emergency meeting; you should tell the judge whom you want to invite to this meeting.
    • First, output a single positive integer $$$t$$$, the number of players called to the meeting.
    • Then, output $$$t$$$ distinct space-separated integers (each from $$$1$$$ to $$$n$$$), the indices of the players invited to the meeting.
  • Then, the judge responds with the outcome of the meeting.
    • If the witness was invited to the meeting and the imposter was not, the judge responds with <W> REPORTS <I>, where <W> is the index of the witness, and <I> is the index of the killer. If this happens, your program was succesful, and then no further messages will be sent from the judge, so you should exit immediately.
    • In any other case, the judge responds with a line containing SILENCE... and you should proceed with the next emergency meeting.
If $$$k$$$ rounds pass with a SILENCE... verdict in all interactions, then the judge sends the message DEFEAT in its own line, and then then message <W> WAS WITNESS, <I> WAS IMPOSTER (to prove that there was a solution).

In this problem, the judge is adaptive! Basically, the judge is designed so that it is impossible for your program to "get lucky" and succeed by sheer coincidence. The judge will always trigger the "unluckiest" worst case scenario for your algorithm, and will only Accept your solution if your algorithm works 100% of the time, no matter what. You may refer to the Notes section for a bit more detail.

Scoring

If your solution is incorrect, you get $$$0$$$ points. If correct, you are awarded points based on the size of $$$n$$$ outputted in the successful attempt. $$$$$$\begin{align*}

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{35} & n \geq 18 \\ \hline 2 & \mathbf{15} & n \geq 30 \\ \hline 3 & \mathbf{10} & n \geq 80 \\ \hline 4 & \mathbf{26} & n \geq 500 \\ \hline 5 & \mathbf{6} & n \geq 700 \\ \hline 6 & \mathbf{8} & n \geq 48000 \\ \hline \end{array}\\

\end{align*}$$$$$$

Note

Here is how the adaptive judge decides on how to act. Basically, the judge does not actually select the witness and imposter in advance. The adaptive judge will try to respond with SILENCE... whenever possible, so long as it "maintains plausible deniability", i.e. so long as there still exists any selection of witness and imposter that is consistent with all its responses so far. Informally, the judge is a cheater, but will never let itself get caught (and will give you the win if you manage to checkmate it).

Here is a sample interaction:


Judge Contestant
18
4
2
1 2
SILENCE...
3
1 3 4
SILENCE...
2
2 3
SILENCE...
1
4
4 REPORTS 1
Here, the solution claims to be able to find the witness and imposter among a lobby with $$$4$$$ players. Note that once the game has been won, the rest of the rounds don't play out any more.

F. Five-Needle Telegraph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For a Media Studies presentation on the history of telecommunication, Alice is going to use her skill in electronics to re-create the five-needle telegraph design. Unlike other communication systems, like the famous Morse telegraph system that succeeded it, the five-needle telegraph had the advantage of not requiring specialist training in order for its operators to send and receive messages.

Alice wants to create a generalized version, so we will actually describe the construction of an $$$n$$$-needle telegraph. Consider a square with $$$n$$$ rows and $$$n$$$ columns which has been rotated $$$45^\circ$$$, such that it now has $$$2n-1$$$ rows—so its first row contains $$$1$$$ entry, its second row contains $$$2$$$ entries, and so on, up until its $$$n$$$th row which contains $$$n$$$ entries; then, the $$$(n+1)$$$th row contains $$$n-1$$$ entries, the $$$(n+2)$$$th row contains $$$n-2$$$ entries, and so on, up until its final row has $$$1$$$ entry again.

The $$$n$$$th row contains a needle in each entry; each other square contains a single letter in each entry. You may refer to this diagram, taken from the Wikipedia page, for an $$$n$$$-needle telegraph with $$$n=5$$$.

Credits to user Dder

Each needle can be in one of $$$3$$$ states: it can be pointed vertically (its default state), it may be rotated $$$45^\circ$$$ counterclockwise (west of north), or it may be rotated $$$45^\circ$$$ clockwise (east of north). A valid message consists of choosing exactly two needles and rotating them in opposite directions (all other needles remain in the default state). The value of this message is equal to the letter that both rotated needles are pointing at. In the above diagram, the third needle has rotated $$$45^\circ$$$ clockwise, and the fifth needle has rotated $$$45^\circ$$$ counterclockwise, and so they point at the letter G

For this problem, you will be given the $$$n(n-1)$$$ letters on Alice's design of her telegraph, as well as $$$m$$$ needle configurations that correspond to valid messages. Please decode Alice's message!

Input

Input begins with a single line containing the integers and $$$n$$$ and $$$m$$$.

Then, $$$n-1$$$ lines of input follow, containing strings with $$$1, 2, 3, \dots, n-1$$$ letters each, respectively, corresponding to the letters in the first $$$n-1$$$ rows of the telegraph.

Then, $$$n-1$$$ lines of input follow, containing strings with $$$n-1, n-2, n-3, \dots, 1$$$ letters each, respectively, corresponding to the letters in the final $$$n-1$$$ rows of the telegraph.

Note that in general, the same letter may appear in multiple places in the telegraph.

Finally, $$$m$$$ lines of input follow, each containing a string of length $$$n$$$, describing Alice's messages in order. Exactly one character will be \ to represent the needle rotated $$$45^\circ$$$ counterclockwise, and exactly one other character will be / to represent the needle rotated $$$45^\circ$$$ clockwise; all other characters in each string are | to represent needles in their default state.

Output

Output a single line containing a string of length $$$m$$$, where the $$$i$$$th character of this string corresponds to the letter pointed at by the needles in the $$$i$$$th of Alice's messages.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 2 \leq n \leq 20 \\ 1 \leq m \leq 400 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{50} & n \leq 5 \\ \hline 2 & \mathbf{30} & \text{All needle configurations point to a letter in the top half.} \\ \hline 3 & \mathbf{20} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
5 16
A
BD
EFG
HIKL
MNOP
RST
VW
Y
|\/||
||\/|
|/\||
|||\/
/\|||
/|||\
/||\|
/|||\
||/\|
||\/|
|/||\
/|||\
|||/\
||\/|
|\/||
||/|\
Output
NOIPHABAKODALONG
Note

The code ||/|\ corresponds to the needle configuration from the diagram in the problem statement.

G. Ghost Gym
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

Cindy is playing a so-called "Kaizo" ROMHack of Pokémon Crystal, meaning that fans had modified the game to massively increase the difficulty to infuriating (unfair?) levels. For some modders, this can include not just the Pokémon battles, but also the overworld puzzles.

The Ghost Gym in Ecruteak city has a puzzle which goes as follows (slightly modified for simplicity).

  • There is an $$$r \times c$$$ map that you must traverse. Your character starts at the top-left-most square, and must traverse to the bottom-right-most square.
  • In one move, your character may take a single step north, south, east, or west.
  • Each tile in the map is flagged as either SAFE or TRAP.
    • If you step on a TRAP tile, you are immediately teleported back to your starting position at the top-left corner!
    • If you exit the bounds of the grid, you are also immediately teleported back to your starting position.
    • It is guaranteed that the top-left-most and bottom-right-most squares are SAFE
  • You do not know which tiles are safe, and which ones are trapped!
Then, the challenge is simple—make it to the bottom right corner, or shout RIGGED if the task is impossible.

Of course, the "maze" in the base game is very simple, but the Kaizo modders have gone out of their way to make their maze as painful as possible. Cindy has been attempting this for hours and is starting to get tilted. She asks you if it's possible to develop a program that can solve this problem for her.

Interaction

The judge first outputs two space-separated integers $$$r$$$ and $$$c$$$, the dimensions of the grid.

Then, you can now move around the grid. You can make statements to the judge:

  • You can output one of N or S or E or W in order to move one step north, south, east, or west, respectively. The judge then responds with one of:
    • YAY if the tile you stepped on is the bottom-right corner.
      • You pass the test case!
      • The judge will send no further messages and your program should terminate.
    • SAFE if the tile you stepped on is SAFE (and not the destination); nothing more happens.
    • TRAP if the tile you stepped on is a TRAP, or if you exited the bounds of the grid; your character is immediately teleported back to the top-left corner before your next move.
  • At any point, you can output RIGGED to declare that you think it's impossible to reach the bottom-right-most corner.
    • If you are correct, then you pass this test case.
    • If you are incorrect, then you get a Wrong Answer for this test case.
    • In either case, the judge will send no further messages and your program should terminate.
If you try to take more than $$$10^5$$$ steps, the judge will output TIMES UP and then send no further messages.
Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 2 \leq r, c \leq 20 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{35} & r = c = 2 \\ \hline 2 & \mathbf{20} & r, c \leq 5 \\ \hline 3 & \mathbf{20} & \text{It is possible, and all }\mathtt{OKAY}\text{ squares lie on \\ the unique shortest path from start to finish.} \\ \hline 2 & \mathbf{25} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Note

Here is a sample interaction. We also provide the map, with safe squares colored lilac, and traps colored a deep indigo. The interaction corresponds to the following steps on the map—stepping on a trap (and being sent back to the start) is represented by the red exclamation point. We remind you that the contestant does not have access to this map—it's only provided to you here to help understand the sample.


Judge Contestant
4 6
S
SAFE
S
TRAP
S
SAFE
E
SAFE
E
SAFE
E
TRAP
S
SAFE
E
SAFE
E
SAFE
N
SAFE
E
SAFE
E
SAFE
E
SAFE
S
SAFE
S
SAFE
S
YAY

Here is another sample interaction. Here, the map is not solvable, so the contestant is correct for saying the game is RIGGED

Judge Contestant
3 3
S
TRAP
E
TRAP
RIGGED

H. Harana
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Puno ang langit ng bituin

At kay lamig pa ng hangin

Sa 'yong tingin ako'y nababaliw giliw

At sa awitin kong ito

Sana'y maibigan mo

Ibubuhos ko ang buong puso ko...

Tonight, Bob has decided that he is going to serenade Cindy with a traditional harana outside her bedroom. Let's ignore the fact that Cindy lives on the 12th floor of a condo—he'll figure out the logistics later. For now, he needs to focus on his singing!

We can massively simplify and describe each note as a single integer, and so describe a song as a sequence of integers (Chord her? I barely know her!) Bob's song consists of the $$$n$$$ notes $$$s_1, s_2, \dots, s_n$$$, and by his practice, the best he can do is sing the notes $$$b_1, b_2, \dots, b_n$$$.

The off-key-ness of his singing is calculated as the sum of the absolute difference between each of his notes and the corresponding note in the song. In symbols: $$$$$$ \text{off-key-ness} = \sum_{i=1}^n |b_i - s_i|. $$$$$$ Bob has a sneaky trick he can do. Instead of fixing his singing, he can just change the key of the backing music! Right now, it is in key $$$0$$$. If a part is to be sung in key $$$k$$$, we add $$$k$$$ to the $$$s_i$$$ of all notes in that part (this $$$k$$$ can be negative, but it must be an integer).

Also, Bob wants to do a key shift at some note $$$m$$$, meaning he will sing notes $$$1$$$ to $$$m$$$ in key $$$k_1$$$, and then sing notes $$$m+1$$$ to $$$n$$$ in key $$$k_2$$$. Note that it is allowed to have $$$k_1 = k_2$$$, if you determine that it would be optimal.

For each $$$m$$$ from $$$1$$$ to $$$n-1$$$: Determine the minimum off-key-ness of Bob's singing if the key shift happens at note $$$m$$$, and the two keys ($$$k_1$$$ and $$$k_2$$$) are chosen optimally.

Input

The first line of input contains a single integer $$$n$$$.

The second line of input contains the $$$n$$$ space-separated integers $$$s_1, s_2, \dots, s_n$$$.

The third line of input contains the $$$n$$$ space-separated integers $$$b_1, b_2, \dots, b_n$$$.

Output

Output a single line containing $$$n-1$$$ space-separated integers, the $$$m$$$th of which is the minimum off-key-ness if the key shift happens at note $$$m$$$.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 2 \leq n \leq 2 \times 10^5 \\ -10^9 \leq s_i, b_i \leq 10^9 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{50} & \text{$0 \leq s_i, b_i \lt 8$ and $n \leq 1000$} \\ \hline 2 & \mathbf{20} & 0 \leq s_i, b_i \lt 8 \\ \hline 3 & \mathbf{20} & n \leq 1000 \\ \hline 4 & \mathbf{10} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
4
7 2 2 7
5 1 0 6
Output
1 2 1
Note

For the sample input, let's consider what to do when the key change is at note $$$m=1$$$. Here is an optimal series of steps.

  • For the first part, choose $$$k_1 = -2$$$, changing the first note to $$$[5]$$$
  • For the second part, choose $$$k_2 = -1$$$, changing the second to fourth notes to $$$[1, 1, 6]$$$.
We end with notes $$$[5, 1, 1, 6]$$$, and the off-key-ness is $$$|5-5| + |1-1| + |0-1| + |6-6| = 1$$$, which we can show is minimal.

We get minimal off-key-ness answers of $$$2$$$ and $$$1$$$ for the remaining values of $$$m$$$.

I. Injurious Company
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice, Bob, and Cindy are having a blast playing the hot new game Injurious Company, an indie horror survival game where players assume the role of underpaid laborers who must persevere through ludicrously unsafe working conditions just to earn scraps for their day's wage (maybe the real horror is capitalism).

The gang are located at position $$$(x, y)$$$ (using the usual Cartesian coordinate system) and must return back to their ship, located at the origin $$$(0, 0)$$$. But they have to move strategically in order to dodge the monsters that are coming after them! The strategy guide describes a winning strategy according to $$$n$$$ pieces of information, each described by an integer $$$K_i$$$ and a direction $$$dir_i$$$ (each of which is H or V). This means that on their $$$i$$$th move they choose a direction $$$d_i$$$ and a number of steps $$$k_i$$$—then, they head $$$k_i$$$ steps in the direction of $$$d_i$$$. Their choice is subject to the following restrictions:

  • $$$1 \leq k_i \leq K_i$$$
  • If $$$dir_i$$$ is H, then $$$d_i$$$ must be E or W, for east or west, respectively.
  • If $$$dir_i$$$ is V, then $$$d_i$$$ must be N or S, for north or south, respectively.
Note that they are never allowed to skip a move or stand still. That's how the monsters get you! They also cannot use the ship to flee until all the moves have been used.

Determine if it is possible for them to return to their ship alive, and if so, provide a series of moves that they can take. Also, please answer $$$T$$$ different test cases per file.

Input

The first line of input contains a single integer $$$T$$$. Then, the descriptions of each of the $$$T$$$ test cases follows.

The first line of each test case contains the three space-separated integers $$$n$$$, $$$x$$$, and $$$y$$$.

Then, $$$n$$$ lines follow, each containing two space-separated values: the integer $$$K_i$$$, and the direction $$$dir_i$$$.

Output

For each test case, if the task is possible, output a line containing the word YES. Otherwise, output NO.

If YES, also output $$$n$$$ lines containing the moves that should be made. Each line should contain two space separated values: the number of steps $$$k_i$$$, and the direction $$$d_i$$$.

If there are multiple possible answers, any will be accepted as long as they respect the strategy guide and return the gang to the ship.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \leq T \\ 1 \leq n \\ \text{The sum of $n$ across all test cases is $\leq 2 \times 10^5$.} \\ |x|, |y| \leq 10^{15} \\ \text{$1 \leq K_i \leq 10^9$ for each $i$} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{30} & n \leq 2 \\ \hline 2 & \mathbf{20} & n \leq 3 \\ \hline 3 & \mathbf{20} & \text{$n \leq 15$ and $T \leq 50$} \\ \hline 4 & \mathbf{30} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
2
3 -3 2
1 H
3 V
4 H
2 -100 -100
3 H
3 V
Output
YES
1 E
2 S
2 E
NO
Note

These sample images correspond to the first of the test cases in the sample input.

Then, these moves bring the gang back to $$$(0, 0)$$$.

J. Jolly Divisors
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Alice noticed this trend in some contest problems where they add "flavor" to a pure mathematical statement by introducing some formal abstract arbitrary property and then binding it to an adjective that has absolutely nothing to do with that definition. These problems are usually then titled "adjective + mathematical object". For example, "beautiful numbers", "interesting sequences", and the classic "good colorings". As an aspiring problem setter, Alice wishes to partake in this tradition.

A $$$k$$$-jolly divisor of $$$n$$$ is some positive integer $$$d$$$ such that $$$n$$$ is divisible by $$$d$$$ and by $$$d^k$$$. For example, $$$4$$$ is a $$$3$$$-jolly divisor of $$$960$$$, since both $$$4$$$ and $$$4^3 = 64$$$ divide $$$960$$$. We can show that $$$4$$$ is the largest $$$3$$$-jolly divisor of $$$960$$$.

Given $$$N$$$ and $$$k$$$, find the sum of the largest $$$k$$$-jolly divisors of $$$n$$$ across all $$$n$$$ from $$$1$$$ to $$$N$$$.

Python users are advised to submit to PyPy for this problem.

Input

Input consists of a single line with the two space-separated integers $$$N$$$ and $$$k$$$.

Output

Output a line containing a single integer, the answer.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 2 \leq k \leq 10^9 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{30} & 1 \leq n \leq 10^3 \\ \hline 2 & \mathbf{30} & 1 \leq n \leq 10^5 \\ \hline 3 & \mathbf{30} & 1 \leq n \leq 10^6 \\ \hline 4 & \mathbf{10} & 1 \leq n \leq 5 \times 10^7 \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
12 2
Output
17
Input
12345678 3
Output
16854689

K. Kyuuing Theory
time limit per test
8 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Cindy is helping her dojo with the logistics of their upcoming promotion exam.

There is a queue of $$$n$$$ students, each ready to take the exam. Cindy will then hire $$$k$$$ instructors as the ones who will administer the promotion exam. The $$$i$$$th person from the front of the queue takes $$$a_i$$$ seconds to complete their test (pass or fail) under an instructor, and each instructor can only administer the test to one student at a time.

The process is as follows. The person in front of the queue looks around for any free instructors; if they see one, they go up to that instructor and start their test with them. If there are many that are free, one is chosen arbitrarily (the time spent walking to each instructor is negligible). If no instructor is free, the student at the front of the queue (and thus, everyone else behind them) waits until one is free.

Is it possible to finish all tests in at most $$$T$$$ seconds? If yes, what is the smallest $$$k$$$ (the number of instructors to hire) needed in order to achieve this goal? Also, please answer $$$t$$$ different test cases per file.

Input

The first line of each test case contains $$$t$$$, the number of test cases. The descriptions of $$$t$$$ test cases follow.

The first line of each test case contains two space-separated integers $$$n$$$ and $$$T$$$.

The second line of each test case contains the $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$, the finishing times in order of the front of the queue to the back.

Output

For each test case:

  • If the task is possible, output a line containing the word YES. Otherwise, output NO.
  • If YES, output another line containing a single positive integer, the minimum value of $$$k$$$ for which the task is possible.
Scoring

Here, $$$N$$$ denotes the sum of $$$n$$$s in a single test file.

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \le t \le 500 \\ 1 \leq n \\ 1 \leq T, a_i \leq 10^{16} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{25} & \text{$n \leq 3$, $N \leq 1500$} \\ \hline 2 & \mathbf{15} & \text{$n \leq 75$, $N \leq 1500$} \\ \hline 3 & \mathbf{20} & \text{$n \leq 500$, $N \leq 2000$} \\ \hline 4 & \mathbf{20} & \text{$n \leq 2000$, $N \leq 6000$} \\ \hline 5 & \mathbf{20} & \text{$n \leq 150000$, $N \leq 450000$} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
1
7 16
10 4 6 8 6 4 3
Output
YES
3

L. LC BB ND CNDY
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Fingers on buzzers teams, it's time for the Missing Vowels Round. In this game, we've taken famous words, phrases, or sayings, removed the vowels, and then re-spaced the consonants.

So for example, for the category "Sports for nerds," the clue might be,

CMPT TVPR GRM MNG
and the answer is,
COMPETITIVE PROGRAMMING
For the category "A poet, a peninsula, and a pronoun", the clue might be,
LC BB ND CNDY
and the answer is, obviously,
LACABA, BONDOC, AND YOU

Alice, Bob, and Cindy are training for their appearance on Only Connect (their team name is "The Github Heatmappers"), and in particular they would like to train for the Missing Vowels Round. They ask for your help in creating a program that allows them to train.

As a formalization of the rules given above, in the Missing Vowels Round, the following transformation is applied to a string:

  • First, remove all characters that aren't consonants, but preserve the order of these consonants.
    • Y is always considered a consonant for this problem
  • Capitalize all these consonants
  • You may insert spaces anywhere in the remaining letters, so long as when you're done...
    • The phrase does not begin or end in a space.
    • There are no two consecutive spaces anywhere
Let $$$\mathcal{L}(s)$$$ be the list of all possible strings that can be achieved by this process, given some starting string $$$s$$$, sorted in lexicographic order (note that the "space" character comes comes before all letters of the alphabet; see the Notes for an explanation of lexicographic ordering). Given $$$i$$$ and some string $$$s$$$, what is the $$$i$$$th element of $$$\mathcal{L}(s)$$$? Also, if $$$\mathcal{L}(s)$$$ has fewer than $$$i$$$ elements, you should say so as well.
Input

The first line of input contains the string $$$s$$$, which may contain whitespaces.

The second line of input contains a single integer $$$i$$$.

Output

If $$$\mathcal{L}(s)$$$ has fewer than $$$i$$$ elements, output the phrase "out of bounds" (without the quotes; note it is in all-lowercase), Otherwise, output the $$$i$$$th element in $$$\mathcal{L}(s)$$$.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \leq i \leq 10^{18} \\ 1 \leq |s| \leq 2 \times 10^5 \\ \text{$s$ consists of upper and lowercase letters, punctuation }\mathtt{,.!?}\text{ and spaces.} \\ \text{$s$ does not begin or end in a space, and it has no two consecutive spaces.} \\ \text{$s$ contains at least one consonant.} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{40} & |s| \leq 15 \\ \hline 2 & \mathbf{40} & i \leq 2 \times 10^5 \\ \hline 3 & \mathbf{20} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
NOI.PH
3
Output
NP H
Input
Alice, Bob, and Cindy
344
Output
LC BB ND CNDY
Input
Who Lives, Who Dies, Who Tells Your Story
1000000000000000000
Output
out of bounds
Input
!B
1
Output
B
Note

Comparison operators like < are actually defined on strings in almost all programming languages. Try it!

They are defined in a way consistent with "dictionary ordering". Suppose we have two strings $$$s$$$ and $$$t$$$ such that $$$s \neq t$$$.

  • Let $$$i$$$ be the smallest index such that $$$s_i \neq t_i$$$ (i.e. "the first place where the two strings differ")—then, $$$s \lt t$$$ if and only if $$$s_i \lt t_i$$$ (e.g. cash comes before cave because s < v).
  • If no such $$$i$$$ exists, then one is a prefix of the other word, and by convention, the prefix comes first (e.g. cat comes before caterwaul).
As for how to compare characters, their ordering is determined by getting its equivalent ASCII code and comparing the integers. For this problem, all you need to know is that the letters are in fact sorted in alphabetical order, and the space character comes before all letters.

So, for example, "LIL MAN" (without the quotes) comes before LILAC because the two strings first differ at their fourth character, and there, the space precedes A in ASCII ordering.

M1. Mondridalgo (Verification)
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The motivating story and definition of a rectilinear path (and related terms) are the same in Mondridalgo (Verification) and (Optimization). Skip to the bolded text for the first section where the two differ.

Bob created a magnificent painting this year in the Mondridalgo style (a combination of Piet Mondrian's rectangles and Felix Hidalgo's neoclassicism), which both Alice and Cindy adore. Unfortunately, when Cindy asked him if she could keep the painting, he was too lovestruck and immediately said yes... forgetting that he had already previously promised the painting to Alice! Oh no! Remember everyone—don't be like Bob.

Unable to choose between his two friends, Bob decides the best way to resolve this situation is to cut up his painting into two contiguous pieces using a pair of scissors, and then hand one piece to Alice and the other Cindy. That way, each of his friends is equally unhappy.

We formalize "cutting with scissors" by defining a rectilinear path to be a non-empty sequence of "cuts" such that:

  • Each cut is represented by a directed line segment, indicating the motion of cutting.
  • The first cut starts somewhere along the border of the painting, and moves perpendicular to the border.
  • Each next cut in the sequence begins where the previous one ended, and is oriented perpendicular to the previous cut.
  • The final cut should end anywhere along the border.
A valid rectilinear path is one that partitions the painting into exactly two regions (one to hand to Alice and one to hand to Cindy). We can show that the following conditions make a rectilinear path valid:
  • All cuts are made along the gridlines, and with the exception of the start of the first cut and the end of the last cut, the path must stay entirely within the rectangle at all times.
  • Any two non-consecutive cuts should not intersect anywhere, not even at a point.
See the Notes below for illustrations of rectilinear cuts. Each one splits the painting into two regions, colored blue and red in the diagrams. They use $$$1$$$, $$$2$$$, and $$$4$$$ cuts, respectively.
For the verification problem, each square in the grid will be indicated as "should be given to Alice" or "should be given to Cindy". Does there exist a valid rectilinear path which partitions the painting into two regions in this manner? If yes, please also determine how many cuts are used in this rectilinear path. Also, please answer $$$T$$$ different test cases per file.
Input

The first line of input contains a single integer $$$T$$$. Then, the descriptions of each of the $$$T$$$ test cases follows.

The first line of each test case contains the two space separated integers $$$r$$$ and $$$c$$$.

Then, an $$$r \times c$$$ grid follows—that is, $$$r$$$ lines follow, each containing a string of length $$$c$$$, each one A or C. The letter A means that this square should go to Alice, and the letter C means that this square should go to Cindy.

Output

For each test case:

  • If the task is possible, output a line containing the word YES. Otherwise, output NO.
  • If YES, also output a line containing the number of cuts in the partition's rectilinear path.
Scoring

Let $$$A$$$ be the sum of $$$rc$$$ across all test files.

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \leq T \leq 100 \\ 2 \leq r, c \leq 400 \\ A \le 10^6 \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{20} & \text{$r = c = 2$} \\ && \text{$T \le 16$} \\ \hline 2 & \mathbf{20} & \text{$r = 2$} \\ && \text{$c \leq 50$} \\ \hline 3 & \mathbf{20} & \text{$r \leq 3$} \\ && \text{$c \leq 50$} \\ \hline 4 & \mathbf{30} & \text{$r, c \leq 50$} \\ && \text{$A \le 20000$} \\ \hline 5 & \mathbf{10} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
5
4 6
AAAAAA
AAAAAA
AAAAAA
CCCCCC
4 6
CCCCCC
CCCCCC
AACCCC
AACCCC
4 6
CCCCCC
ACCCCC
AAACCC
AAACCC
5 5
AACCA
AAACA
ACCCA
ACCAA
AAAAA
3 3
AAA
ACA
CAA
Output
YES
1
YES
2
YES
4
YES
9
NO
Note

The first three test cases correspond to the examples illustrated in the problem statement.

M2. Mondridalgo (Optimization)
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The motivating story and definition of a rectilinear path (and related terms) are the same in Mondridalgo (Verification) and (Optimization). Skip to the bolded text for the first section where the two differ.

Bob created a magnificent painting this year in the Mondridalgo style (a combination of Piet Mondrian's rectangles and Felix Hidalgo's neoclassicism), which both Alice and Cindy adore. Unfortunately, when Cindy asked him if she could keep the painting, he was too lovestruck and immediately said yes... forgetting that he had already previously promised the painting to Alice! Oh no! Remember everyone—don't be like Bob.

Unable to choose between his two friends, Bob decides the best way to resolve this situation is to cut up his painting into two contiguous pieces using a pair of scissors, and then hand one piece to Alice and the other Cindy. That way, each of his friends is equally unhappy.

We formalize "cutting with scissors" by defining a rectilinear path to be a non-empty sequence of "cuts" such that:

  • Each cut is represented by a directed line segment, indicating the motion of cutting.
  • The first cut starts somewhere along the border of the painting, and moves perpendicular to the border.
  • Each next cut in the sequence begins where the previous one ended, and is oriented perpendicular to the previous cut.
  • The final cut should end anywhere along the border.
A valid rectilinear path is one that partitions the painting into exactly two regions (one to hand to Alice and one to hand to Cindy). We can show that the following conditions make a rectilinear path valid:
  • All cuts are made along the gridlines, and with the exception of the start of the first cut and the end of the last cut, the path must stay entirely within the rectangle at all times.
  • Any two non-consecutive cuts should not intersect anywhere, not even at a point.
See the Notes below for illustrations of rectilinear cuts. Each one splits the painting into two regions, colored blue and red in the diagrams. They use $$$1$$$, $$$2$$$, and $$$4$$$ cuts, respectively.
For the optimization problem, Alice assigned a integer "Alice adoration rating" to each square in the painting, indicating her happiness if she receives this square of the painting; Cindy similarly assigned an integer "Cindy cherish rating" to each square in the painting. These ratings can also be negative, indicating squares that they really do not like. Alice's happiness is equal to the sum of the Alice adoration ratings of all squares given to her; Cindy's happiness is equal to the Cindy cherish ratings of all squares given to her.

Our goal is to try to find a partition which is not "lop-sided"—that is, neither one of Alice nor Cindy should be significantly happier than the other in this interaction. This partition also shouldn't be too complicated.

Formally, given an integer $$$k$$$, find any partition whose valid rectilinear path uses at least $$$1$$$ and at most $$$k$$$ cuts, and which minimizes the value $$$|\text{Alice's happiness} - \text{Cindy's happiness}|$$$ among such partitions.

Input

The first line of input contains the three space-separated integers $$$r$$$ and $$$c$$$ and $$$k$$$.

Then, an $$$r \times c$$$ grid follows, describing the Alice adoration ratings—that is, $$$r$$$ lines follow, each containing $$$c$$$ integers: $$$$$$ \begin{array}{cccc} u_{1, 1} & u_{1, 2} & \dots & u_{1, c} \\ u_{2, 1} & u_{2, 2} & \dots & u_{2, c} \\ \vdots & \vdots & \ddots & \vdots \\ u_{r, 1} & u_{r, 2} & \dots & u_{r, c} \end{array} $$$$$$ where $$$u_{i, j}$$$ is the Alice adoration rating of the square in the $$$i$$$th row from the top and $$$j$$$th column from the left.

Then, another $$$r \times c$$$ grid follows, describing the Cindy cherish ratings—that is, $$$r$$$ lines follow, each containing $$$c$$$ integers: $$$$$$ \begin{array}{cccc} v_{1, 1} & v_{1, 2} & \dots & v_{1, c} \\ v_{2, 1} & v_{2, 2} & \dots & v_{2, c} \\ \vdots & \vdots & \ddots & \vdots \\ v_{r, 1} & v_{r, 2} & \dots & v_{r, c} \end{array} $$$$$$ where $$$v_{i, j}$$$ is the Cindy cherish rating of the square in the $$$i$$$th row from the top and $$$j$$$th column from the left.

Output

Output a single positive integer—the minimal value of $$$|\text{Alice's happiness} - \text{Cindy's happiness}|$$$.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 2 \leq r, c \leq 6 \\ 1 \leq k \leq 6 \\ \text{$-10^9 \leq u_{i, j}, v_{i, j} \leq 10^9$ for each $i, j$} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{20} & k=1 \\ \hline 2 & \mathbf{30} & k \leq 2 \\ \hline 3 & \mathbf{20} & r, c \leq 3 \\ \hline 4 & \mathbf{20} & r, c \leq 4 \\ \hline 5 & \mathbf{10} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
4 5 1
20 -8 11 4 0
-7 -5 12 9 -2
12 12 -9 5 -2
11 10 12 1 -2
-9 -6 -9 -2 9
15 22 -5 -6 9
-1 21 -5 10 9
-1 29 -5 10 9
Output
10
Input
4 5 2
20 -8 11 4 0
-7 -5 12 9 -2
12 12 -9 5 -2
11 10 12 1 -2
-9 -6 -9 -2 9
15 22 -5 -6 9
-1 21 -5 10 9
-1 29 -5 10 9
Output
1
Input
4 5 5
20 -8 11 4 0
-7 -5 12 9 -2
12 12 -9 5 -2
11 10 12 1 -2
-9 -6 -9 -2 9
15 22 -5 -6 9
-1 21 -5 10 9
-1 29 -5 10 9
Output
0
Input
4 5 6
20 -8 11 4 0
-7 -5 12 9 -2
12 12 -9 5 -2
11 10 12 1 -2
-9 -6 -9 -2 9
15 22 -5 -6 9
-1 21 -5 10 9
-1 29 -5 10 9
Output
0
Note

Here are the corresponding partitions for the first three sample inputs.

For example, in the first sample input, Alice's happiness would be $$$20 + (-8) + 11 + 4 + 0 + (-7) + (-5) + 12 + 9 + (-2) + 11 + 10 + 12 + 1 + (-2) = 52$$$. Meanwhile, Cindy's happiness is $$$(-1) + 29 + (-5) + 10 + 9 = 42$$$. We can show that the difference of $$$|52 - 42| = 10$$$ is minimal across all partitions that use one cut.

N. Networks Make the Net Work
time limit per test
6 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice, Bob, and Cindy are learning networking in the computer club! Each of them started with five computers; each computer has a unique name to identify it. Cables can be used to directly connect two different computers; each cable is two-way. A collection of computers and cables is called a network. Their networks are originally configured in the following shapes (for privacy reasons, we've hidden the computers' names).

Occasionally, they may need to add a repeater computer in order to extend a connection.
Formally, in order to extend the connection between computers $$$u$$$ and $$$v$$$ (where $$$u$$$ and $$$v$$$ are distinct computers already connected by a cable), one would do the following:
  • Acquire a new computer (say its name is $$$x$$$)
  • Discard the cable connecting $$$u$$$ and $$$v$$$, then add two new cables connecting $$$u$$$ and $$$x$$$, and $$$x$$$ and $$$v$$$.
Each of them then performed this "insert a repeater" connection some unknown number of times to their network.

They decide to give you a little puzzle. They'll give you the setup of a computer network. Whose is it! The answer could be even "none of them", and you also need to be able to identify if this is the case. Also, please answer $$$T$$$ different test cases per file.

Input

The first line of input contains a single integer $$$T$$$. Then, the descriptions of each of the $$$T$$$ test cases follows.

The first line of each test case contains two space-separated integers $$$n$$$ and $$$m$$$.

The second line of each test case contains $$$n$$$ space-separated strings, the names of the computers in the presented network.

Then, $$$m$$$ lines follow, each containing two space-separated strings. These strings are names of computers, and it means, "There exists a cable that connects these two computers"

Output

For each test cases, output the answer to that test case:

Consider the set of all people (among Alice and Bob and Cindy) such that it is possible for this person to have been the owner of the presented network, i.e. the set of all names that can fill in the blank in: "It is possible that the owner of this network is _____". Output a line containing their space-separated names, sorted in lexicographic (dictionary) order.

If none of them could possibly be the owner of the presented network, output PRANKED instead.

Scoring

Here, $$$N$$$ denotes the sum of $$$n$$$s in a single test file, and $$$M$$$ the sum of $$$m$$$s in a single test file.

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 0 \leq T \\ 1 \leq n \leq 2 \times 10^5 \\ N \leq 2 \times 10^5 \\ 0 \leq m \leq 2 \times 10^5 \\ M \leq 2 \times 10^5 \\ \text{Each computer's name is distinct, and consists} \\ \text{of at most $5$ upper and lowercase letters.} \\ \text{No cable connects a computer to itself.} \\ \text{At most one cable connects any pair of computers.} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{20} & \text{Someone owns each network.} \\ \hline 2 & \mathbf{20} & \text{Neither Alice nor Bob own each network.} \\ \hline 3 & \mathbf{20} & \text{Neither Alice nor Cindy own each network.} \\ \hline 4 & \mathbf{20} & \text{Neither Bob nor Cindy own each network.} \\ \hline 5 & \mathbf{20} & \text{No further constraints} \\ \hline \end{array}\\

\end{align*}$$$$$$

Example
Input
3
6 6
CompA CompB CompC CompD CompE CompF
CompA CompB
CompA CompC
CompC CompD
CompB CompD
CompB CompE
CompD CompF
7 7
CompA CompB CompC CompD CompE CompF CompG
CompA CompB
CompB CompC
CompC CompD
CompD CompE
CompE CompA
CompD CompF
CompD CompG
8 7
CompA CompB CompC CompD CompE CompF CompG CompH
CompA CompH
CompB CompH
CompB CompG
CompC CompG
CompC CompF
CompD CompF
CompD CompE
Output
Alice
PRANKED
Cindy
Note

Here are computer networks in the sample test cases: