2024 Argentinian Programming Tournament (TAP)
A. Advanced tic-tac-toe
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Tic-tac-toe is a two-player game played on an initially empty $$$3 \times 3$$$ board, where players take turns alternately. The first player plays with the letter X, and the second with the letter O. On their turn, each player must choose an empty cell on the board and write their letter in it. The first player to form a vertical, horizontal, or diagonal line with three occurrences of their letter is the winner. If there are no empty cells left on the board where a move can be made, the result is considered a draw. The images below show some examples of possible final results in a game of tic-tac-toe.

Xavier and Olivia were fans of tic-tac-toe, but after a while, both learned to play optimally, and now the game has become boring because every time they play, the result is a draw. However, they came up with a new variant of the game, where $$$N$$$ restrictions are added before starting, which must be followed throughout the game.

Each cell on the board is assigned a number from 1 to 9, as shown in the image below, and then each restriction is defined by two integers $$$A$$$ and $$$B$$$, indicating that cell $$$B$$$ on the board cannot be used by either player if cell $$$A$$$ is empty.

123
456
789

Given $$$N$$$ restrictions, if Xavier plays first and both players play optimally, determine who will be the winner or if the result will be a draw.

Input

A line with an integer $$$N$$$ $$$(0 \leq N \leq 10^5)$$$, the number of restrictions.

Then $$$N$$$ lines, where the $$$i$$$-th of them contains two integers $$$A_i$$$ and $$$B_i$$$ $$$(1 \leq A_i, B_i \leq 9)$$$, which describe the $$$i$$$-th restriction of the game.

Output

A line with a letter indicating the result of the game:

  • "X" to indicate that Xavier wins
  • "O" to indicate that Olivia wins
  • "E" to indicate a draw (E comes from "Empate", which means draw in spanish)
Examples
Input
0
Output
E
Input
1
9 5
Output
X
Input
7
2 2
1 7
3 8
5 9
4 6
6 4
4 6
Output
O
Note

In the third example, if both players play optimally, the final result will be as shown in the third image of the statement ("O wins").

B. Period Search
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Adela and Bastian are fans of riddles, and they recently came up with a new game, but they need your help to play it.

In this game, Adela first chooses a secret word $$$s = s_1s_2\ldots s_N$$$ of length $$$N$$$, formed by $$$N$$$ lowercase letters of the English alphabet $$$s_1, s_2, \dots, s_N$$$. Bastian knows the length $$$N$$$ of the secret word, but not the word itself.

To win the game, Bastian must determine whether the word chosen by Adela is periodic or not. To do this, Bastian can choose two integers $$$L$$$ and $$$R$$$, and ask the following question: Is $$$t = s_Ls_{L+1}\ldots s_R$$$ a period of $$$s$$$? Adela will always answer each question truthfully.

Adela and Bastian consider that $$$t$$$ is a period of a word $$$s$$$ if and only if $$$s$$$ can be obtained by concatenating $$$c \geq 2$$$ copies of $$$t$$$.

A word $$$s$$$ is periodic if $$$t$$$ is a period of $$$s$$$ for some $$$t$$$. For example: ab is the only period of abab, and both a and aa are periods of aaaa, while on the other hand ab and ababa are not periodic.

To make the game fun, Adela must set a limit on the number of questions that Bastian can ask. This limit must depend on the initial information that Bastian receives: the value of $$$N$$$. In particular, she wants the limit on questions $$$K$$$ to be the minimum possible, but in such a way that Bastian can win the game if he asks the $$$K$$$ questions optimally, having absolute certainty of always being able to find the correct answer, regardless of which word of length $$$N$$$ Adela chooses.

Help them to play their game by writing a program that, given the value of $$$N$$$, finds the limit on questions $$$K$$$ that they should use. Additionally, so that Bastian does not think that Adela cheated in case he loses, the program must indicate a set of $$$K$$$ questions with which Bastian could win.

Input

A line with an integer $$$N$$$ $$$(2 \leq N \leq 10^6)$$$, the length of the word $$$s$$$ chosen by Adela.

Output

A line with the integer $$$K$$$, indicating the limit of questions they should use. Then $$$K$$$ lines indicating a set of questions with which Bastian could win. The $$$i$$$-th line describes the $$$i$$$-th question using the integers $$$L$$$ and $$$R$$$ $$$(1 \leq L \leq R \leq N)$$$, as explained above.

If there are multiple sets of optimal questions, any of them will be accepted.

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

A word of length $$$4$$$, $$$s = s_1s_2s_3s_4$$$, is periodic if and only if $$$t = s_1s_2$$$ is a period of $$$s$$$. Therefore, in the first example, one question is sufficient to determine with certainty whether $$$s$$$ is a periodic word or not.

C. Discovering Ngipto
time limit per test
0.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

A group of archaeologists recently discovered an ancient civilization in Egypt called Ngipto. The civilization lived in a desert, which is modeled as an infinite two-dimensional plane. In this desert, the Ngiptians built a pyramid with a polygonal base. Given a description of the pyramid and the position of the sun $$$(X_{sun}, Y_{sun}, Z_{sun})$$$, the archaeologists want to determine if there is any point in the desert where they can set up their camp sheltered from the sun.

More precisely, the base of the pyramid consists of a simple not necessarily convex polygon formed by $$$N$$$ points

$$$(X_1, Y_1, 0), (X_2, Y_2, 0), \ldots, (X_N, Y_N, 0)$$$

Where the first two coordinates represent Cartesian coordinates in the plane of the desert, and the third coordinate represents the height, which for the points of the base is $$$0$$$ since they are on the plane of the desert.

The pyramid's apex is located at the point $$$(X_{ape}, Y_{ape}, Z_{ape})$$$ with $$$Z_{ape} \gt 0$$$, i.e. located above the plane of the desert. The points of the pyramid are exactly those that lie on some segment with one endpoint inside or on the edge of the polygon, and the other endpoint at the apex.

You are asked to determine if there exists any point in the plane of the desert sheltered from the sun, that is, any point in the plane of the desert outside the polygon such that the segment connecting it to the sun intersects the pyramid.

Input

A line with an integer $$$N$$$ $$$(3 \leq N \leq 1000)$$$, the number of vertices in the polygonal base of the pyramid.

Then a line with three integers $$$X_{ape}, Y_{ape}, Z_{ape}$$$ $$$(-10^4 \leq X_{ape}, Y_{ape} \leq 10^4$$$, $$$1 \leq Z_{ape} \leq 10^4)$$$, the coordinates of the apex of the pyramid.

Then a line with three integers $$$X_{sun}, Y_{sun}, Z_{sun}$$$ $$$(-10^4 \leq X_{sun}, Y_{sun} \leq 10^4$$$, $$$Z_{ape} \lt Z_{sun} \leq 10^4)$$$, the coordinates of the sun.

Then $$$N$$$ lines that describe the vertices of the polygonal base of the pyramid. The $$$i$$$-th of these lines contains the integers $$$X_i, Y_i$$$ $$$(-10^4 \leq X_i, Y_i \leq 10^4)$$$. These points are given in clockwise order and form a simple polygon.

Output

A line with the letter "S" if there exists any point in the plane of the desert sheltered from the sun, or the letter "N" otherwise.

Examples
Input
4
2 2 4
2 2 6
0 0
0 4
4 4
4 0
Output
N
Input
4
2 2 4
6 6 6
0 0
0 4
4 4
4 0
Output
S
Input
4
2 2 4
2 2 6
0 0
0 4
1 1
4 0
Output
S
Note

The figure shows a top view of example 2. The light shaded areas are the points in the desert where the archaeologists can camp. The image contains some words in Spanish, sol means sun, and cúspide means apex.

D. Duo
time limit per test
0.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Ana, Beto, and Carla are playing a Tournament of Alliances with Points. Each of them has a score, and two of them will ally to form a duo. If the sum of the points of the allies exceeds the points of the third player, the duo wins. Otherwise, the other player wins alone.

Given the scores of the players, can someone win alone?

Input

A line with 3 positive integers $$$A$$$, $$$B$$$, and $$$C$$$ $$$(1 \leq A,B,C \leq 100)$$$ indicating the scores of Ana, Beto, and Carla respectively.

Output

A line with the letter "S" if someone can ensure a win alone, or the letter "N" otherwise.

Examples
Input
1 2 3
Output
S
Input
4 5 6
Output
N
Input
100 10 10
Output
S
Input
50 50 50
Output
N
Note

In the first example, the player with $$$3$$$ points can win alone, as the sum of the scores of the other two players does not exceed it.

E. Final Showdown
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Within a well-known video game, our hero faces "The Beast", the final boss of the game, and wants to know if he is capable of defeating her or not.

The hero has $$$P$$$ power points and has $$$N$$$ different weapons at his disposal. Each weapon is characterized by three integers $$$A$$$, $$$B$$$, and $$$C$$$. When attacking with a weapon, if the hero currently has $$$P$$$ power points, he will have $$$\displaystyle{\left \lfloor \frac{P-B}{A}\right \rfloor}$$$ power points after the attack, inflicting damage to The Beast such that she loses $$$C$$$ health points. After hitting The Beast, the weapons break. For this reason, each weapon can be used at most once.

The hero wins the confrontation if he manages to defeat The Beast. To achieve this, his power points must be non-negative, and The Beast's health points must be zero or negative.

What is the maximum amount of health points $$$V$$$ that The Beast can initially have for the hero to be able to defeat her?

Note: $$$\lfloor x \rfloor$$$ is the largest integer such that $$$\lfloor x \rfloor \leq x$$$. For example, $$$\lfloor 2.5 \rfloor=2, \lfloor \pi \rfloor=3, \lfloor -2.75 \rfloor=-3$$$.

Input

A line with two integers $$$N$$$ and $$$P$$$ $$$(1 \leq N \leq 200, 1 \leq P \leq 10^5)$$$, the number of weapons and the initial amount of power points of the hero.

Then $$$N$$$ lines that describe the weapons with three integers $$$A$$$, $$$B$$$, and $$$C$$$ $$$(1 \leq A, B, C \leq 10^5)$$$.

Output

A line with an integer $$$V$$$, the maximum amount of health points that The Beast can have.

Examples
Input
3 66
8 8 6
8 8 9
2 5 2
Output
11
Input
2 10
3 1 4
2 8 6
Output
10
Input
1 6
3 7 5
Output
0
Note

In the first example, if The Beast has $$$11$$$ health points, the hero can use the second weapon, reducing his power points from $$$66$$$ to $$$\lfloor \frac{66-8}{8}\rfloor=7$$$ and The Beast's health points to $$$2$$$. Then, he can use the third weapon, reducing his power points from $$$7$$$ to $$$\lfloor \frac{7-5}{2}\rfloor=1$$$ and The Beast's health points to $$$0$$$. If The Beast starts with $$$12$$$ health points or more, then it is impossible to defeat her.

In the second example, if The Beast has $$$10$$$ health points, the hero can first use the second weapon and then the first, reducing his power points to $$$0$$$ and The Beast's health points to $$$0$$$.

In the third example, the only way for the hero to win is if The Beast has $$$0$$$ health points initially.

F. Fixture
time limit per test
0.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Mati has the results of the $$$N$$$ matches he played during the last league of virtual tennis, an individual sport where there are no ties, meaning that in each match a player either wins or loses.

The league organization has a certain fascination with streaks, and the way to obtain a player's final score is somewhat peculiar. A player earns one point for each match won and loses one point for each match lost. Additionally, if after finishing a match a player has at least 3 consecutive matches won including the last match, then they receive an additional point.

Can you help Mati calculate how many points he obtained?

Input

A line with an integer $$$N$$$ $$$(1 \leq N \leq 100)$$$, the number of matches Mati played in the virtual tennis league.

Then a line with $$$N$$$ numbers $$$R_1, R_2, \dots, R_N$$$ $$$(R_i \in \{0, 1\})$$$, where $$$R_i = 0$$$ if Mati lost the $$$i$$$-th match in chronological order and $$$R_i = 1$$$ if Mati won the $$$i$$$-th match.

Output

A line with an integer indicating the points Mati obtained at the end of his matches in the league.

Examples
Input
8
1 0 1 0 1 0 1 0
Output
0
Input
5
0 0 0 0 0
Output
-5
Input
5
1 1 1 1 1
Output
8
Input
7
1 1 1 0 1 1 1
Output
7
Note

In the first example, there is no streak of 3 matches or more, and Mati wins and loses the same number of matches, obtaining $$$0$$$ points.

In the second example, Mati loses all the matches, obtaining $$$-5$$$ points.

In the third example, Mati wins all the matches, obtaining $$$8$$$ points.

In the fourth example, Mati obtains $$$7$$$ points, as he earns $$$2$$$ additional points for the consecutive matches won at the end of the third and seventh matches.

G. Garlands
time limit per test
0.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Lucía is organizing two very important competitions: the Argentinian Programming Tournament (TAP in Spanish) and the University Pasapalabra Tournament (TUP in Spanish). These tournaments are held simultaneously at multiple venues.

To decorate these venues, Lucía bought triangular paper banners with letters, like the ones shown in the following image:

Lucía realized that she can rearrange the banners to form different words. Once arranged, she plans to combine some banners to create garlands, which will be sent to the various venues. All these garlands must have exactly three banners, either forming the word "TAP" or the word "TUP". If a garland does not meet these criteria, then it cannot be sent to the venues. Below is an example of how she can do this with the previous banners:

What is the maximum number of garlands that she can send to the venues?

Input

A line with a string $$$S$$$ $$$(3 \leq |S| \leq 300)$$$, the letters of Lucía's banners.

The notation $$$|S|$$$ denotes the number of letters in $$$S$$$.

It is guaranteed that all its characters are uppercase letters of the English alphabet.

Output

A line with an integer, the maximum number of garlands that can be sent.

Examples
Input
APPUNTATTE
Output
2
Input
TULIPAN
Output
1
Input
TAPTUPTAP
Output
3
Input
TOP
Output
0
Note

The images in the statement correspond to the first example.

In the second example, she can arrange them like this: TUPNILA.

H. Electric Fence for Livestock
time limit per test
1.25 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

In a field, there are $$$N$$$ rectangular fences, with the sides of the rectangles aligned to the coordinate axes. Two distinct fences never intersect each other, not even at a point, and in particular, not even at a vertex. However, it is possible that some fences are "surrounded" or "enclosed" by other fences.

There are $$$M$$$ possible points in the field where a parachutist can land. A given parachutist always has the same probability of landing at each of the $$$M$$$ points. More specifically, two parachutists will land in this field, and the position where each lands is independent of the other. In other words, all $$$M \times M$$$ combinations of landing locations for the first and second parachutist, respectively, are equally probable. None of the $$$M$$$ locations is on an electrified fence, not even at a vertex.

The fences are made of electrified wire, which covers the entire perimeter of the rectangle, making it difficult to cross them without receiving an electric shock. Crossing a fence means passing through the perimeter of its corresponding rectangle, that is, passing through a point that belongs to one of the $$$4$$$ sides of the rectangle. To fulfill their mission, after landing, the parachutists can walk through the field until they meet at the same point in the field. Since crossing a fence is very complicated and dangerous, they will walk in such a way as to cross the minimum possible number of fences until they meet.

Your task is to calculate the expected number of fences they must cross in total to be able to meet.

Input

A line with two integers $$$N$$$ and $$$M$$$ ($$$1 \leq N,M \leq 2\cdot 10^5$$$), the number of fences and the number of landing points.

Then $$$N$$$ lines, each describing one of the $$$N$$$ fences. Each line contains $$$4$$$ integers $$$X_1$$$, $$$Y_1$$$, $$$X_2$$$, $$$Y_2$$$ ($$$0 \leq X_1 \lt X_2 \leq 10^9$$$, $$$0 \leq Y_1 \lt Y_2 \leq 10^9$$$), such that the fence has corners at $$$(X_1, Y_1)$$$, $$$(X_1, Y_2)$$$, $$$(X_2, Y_1)$$$, and $$$(X_2, Y_2)$$$.

Then $$$M$$$ lines, each describing one of the $$$M$$$ landing points. Each line contains $$$2$$$ integers $$$X,Y$$$ ($$$0 \leq X,Y \leq 10^9$$$).

Output

A single line with a single number indicating the expected number of fences that the parachutists must cross in total to be able to meet.

This answer will be accepted if it has a relative or absolute error less than or equal to $$$10^{-9}$$$.

Formally, let $$$a$$$ be your answer and $$$b$$$ be the jury's answer. Then, your answer will be accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

Examples
Input
1 2
0 0 10 30
1 1
0 31
Output
0.5000000000
Input
3 3
0 0 10 30
5 5 8 8
20 20 30 30
3 3
7 7
25 25
Output
1.3333333333
Input
1 4
10 15 100 200
1000 2000
3000 4000
5000 6000
7000 8000
Output
0.0000000000
Note

The following figure illustrates the second example:

I. Innovations in Robotics
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Franco continues to learn robotics and is very happy because he built his second robot. His original plan was for this robot to be able to play Hopscotch, but since this is very difficult, he settles for a simpler version of this game.

On the floor, there is a board drawn with $$$N \times M$$$ squares. Let $$$(i,j)$$$ be the square located in row $$$i$$$ and column $$$j$$$. Due to the heavy rain lately, some of these squares are wet.

The robot can start its journey from any point outside the board and can only move horizontally and vertically, that is, in the directions parallel to the edges of the board. The robot can walk on both dry and wet squares. However, to change its direction of movement from vertical to horizontal (or vice versa), the robot must be inside a dry square. The robot can change its direction at most once in each square, but it can pass through it as many times as it wants. It can never change its direction by $$$180^\circ$$$. The journey must end outside the board.

For the journey to be valid, the robot must have changed its direction in all the dry squares. Below is a valid journey on a $$$3\times 3$$$ board, where the squares containing a circle are wet squares and the others are dry squares:

The goal is to determine a valid journey if it exists, for which each dry square must be assigned a distinct integer between $$$1$$$ and $$$s$$$ (where $$$s$$$ is the number of dry squares), so that the numbers indicate the order of the $$$s$$$ direction changes that the robot will make on the dry squares.

Input

A line with two integers $$$N$$$ and $$$M$$$ $$$(1 \leq N, M \leq 1000)$$$, representing the number of rows and columns of the board.

Then, the $$$i$$$-th of the following $$$N$$$ lines contains $$$M$$$ characters separated by a space. Each of these is "." or "0". If the character in position $$$j$$$ is ".", then the square $$$(i,j)$$$ is dry, and if it is "0", it is wet.

It is guaranteed that there is at least one dry square.

Output

If there is no valid journey, a single line containing "*".

Otherwise, $$$N$$$ lines that describe the board with the numbers written in the dry squares.

Formally, the $$$i$$$-th of the $$$N$$$ lines must contain $$$M$$$ integers. The $$$j$$$-th of them must be "0" if the square $$$(i,j)$$$ was wet, and an integer between $$$1$$$ and $$$s$$$ if that square was dry, as described in the statement.

If there are multiple valid journeys, any of them will be accepted.

Examples
Input
3 3
. 0 0
0 . .
. 0 .
Output
1 0 0
0 5 4
2 0 3
Input
3 2
0 .
0 0
. .
Output
0 3
0 0
1 2
Input
1 3
. . .
Output
*

J. Never Add Up to X
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Máximo really enjoys collecting plushies. He has $$$N$$$ plushies, and he assigned a beauty value to each of them, which is a positive integer. He wants to arrange them all on his shelf in a row.

Since Máximo believes that the number $$$X$$$ brings bad luck, he wants to organize them on his shelf in such a way that there are no two plushies whose sum of beauties equals $$$X$$$ and that are adjacent in the row.

Can Máximo achieve his goal? If the answer is yes, you should indicate how he can do it.

Input

A line with two integers $$$N$$$ and $$$X$$$ $$$(2 \leq N \leq 3 \cdot 10^5, 1 \leq X \leq 10^9)$$$, the number of plushies Máximo has and the number he believes brings bad luck.

Then a line with $$$N$$$ integers $$$B_1, B_2, \ldots, B_N$$$ $$$(1 \leq B_i \leq X)$$$, the beauty values of each of Máximo's plushies.

Output

If Máximo cannot achieve his goal, a single line containing "*".

Otherwise, a single line containing the beauties of the $$$N$$$ plushies, in the order in which Máximo places them on his shelf.

If there are multiple solutions, any of them will be accepted.

Examples
Input
6 7
1 2 5 4 6 7
Output
1 7 5 4 2 6
Input
2 5
2 3
Output
*
Input
3 10
2 6 2
Output
2 6 2
Note

In the first example, if Máximo places the plushies in such a way that their beauties are $$$[1, 7, 5, 4, 2, 6]$$$, then the sums of adjacent plushies are $$$[8, 12, 9, 6, 8]$$$. None of these numbers is equal to $$$X=7$$$.

In the second example, Máximo cannot achieve his goal, as the only ways to arrange them would be $$$[2, 3]$$$ and $$$[3, 2]$$$, and $$$2 + 3 = 3 + 2 = 5 = X$$$.

In the third example, any arrangement of the plushies is valid.

K. Typographic Kaleidoscope
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

You have an "ASCII Art" image formed by the characters '#' and '.', such as the following:

It is known that the image is actually formed by several copies of the letters T, A, and P that we see in the image above. These letters cannot be stretched or scaled; they must have exactly the same shape as those shown. Each character '#' in the image must belong to exactly one letter, and the letters must cover all the '#', without overlapping or covering a '.'.

For example, the following image:

contains two T's and one P. For illustrative purposes, each individual letter is indicated as A, B, and C below:

Given a valid image, how many T's, how many A's, and how many P's are there?

Input

One line with two integers, $$$N$$$ and $$$M$$$ $$$(1\leq N,M\leq 1000)$$$, the number of rows and columns. The following $$$N$$$ lines make up the image, and each contains a string of $$$M$$$ characters.

The given image will always be valid, meaning it can be formed with the letters T, A, P following the rules explained in the statement.

Output

A single line with three integers indicating the quantities of T, A, and P.

If there is more than one possible triplet of values, any of them will be accepted.

Examples
Input
7 13
.............
.###.###.###.
..#..#.#.#.#.
..#..###.###.
..#..#.#.#...
..#..#.#.#...
.............
Output
1 1 1
Input
9 6
###...
.####.
.##.#.
.####.
.#####
..#.#.
....#.
....#.
....#.
Output
2 0 1
Input
11 7
.###...
..####.
..##.#.
..####.
..#####
####.#.
.#####.
.##.##.
.#####.
.##.#..
..#.#..
Output
3 1 1
Note

In the first example, the image contains exactly one T, one A, and one P.

The second example is explained in the statement.

L. Games
time limit per test
1.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Agustín and Brian play many games every weekend of the following game:

Given a list of $$$m$$$ positive integers $$$x_1, x_2, \dots, x_m$$$, each player starts with $$$0$$$ points, and alternately starting with Agustín, each player on their turn performs one of these two actions:

  1. Choose one of the elements from the list that has not been chosen previously, and add the chosen number from the list to their score.
  2. Pass the turn.

If both players pass in two consecutive turns, the game ends. Note that the fact that a player has no numbers available to choose on their turn does not necessarily imply that the game ends on that turn.

When the game ends, if one player has a higher score than the other, that player wins the game, and if both players have the same score, then the result of the game is a tie.

They are tired of always playing games of the same type, so in the last few weekends, they started to consider variants. In particular, this weekend they decided that Agustín can only choose values that are powers of 2 (i.e., values $$$x$$$ for which there exists a non-negative integer $$$k$$$ such that $$$x = 2^k$$$, such as $$$1$$$, $$$2$$$, $$$4$$$, $$$8, \dots$$$, etc.) and Brian can only choose odd values.

To avoid always playing with the same numbers, they play $$$Q$$$ games, each with a list of numbers generated from an original list of $$$N$$$ values $$$A_1, A_2, \dots A_N$$$. Each game is described with two numbers $$$L$$$ and $$$R$$$, and is played with the list of numbers that are between positions $$$L$$$ and $$$R$$$ of the original list, that is $$$A_{L}, A_{L+1}, \dots, A_{R}$$$ (including both extreme positions).

Given the $$$N$$$ values of the original list $$$A_1$$$, $$$A_2$$$, $$$\dots$$$, $$$A_N$$$ and the description of the $$$Q$$$ games, decide what the result will be for each of the $$$Q$$$ games if Agustín and Brian play optimally.

Input

A line with two values $$$N$$$ and $$$Q$$$ $$$(1 \leq N, Q \leq 2\cdot 10^5)$$$, which denote the length of the list and the number of games to be played, respectively.

A line with the $$$N$$$ values $$$A_i$$$ of the original list $$$(1 \leq A_i \leq 10^4)$$$.

Finally, there are $$$Q$$$ lines that describe the games. Each line describes a game with two values $$$L$$$ and $$$R$$$ $$$(1 \leq L \leq R \leq N)$$$ that denote that this game uses the values $$$A_{L}, A_{L+1}, \dots, A_{R}$$$ from the original list.

Output

$$$Q$$$ lines, where the $$$j$$$-th line denotes the result of the $$$j$$$-th game, which should be "A", "B" or "E" according to whether Agustín, Brian, or the game ends in a tie, respectively, when played optimally.

Example
Input
8 3
4 2 2 2 3 3 1 6
1 3
2 6
5 8
Output
A
E
B
Note

In the first game of the example, they play with the list $$$[4, 2, 2]$$$. Playing optimally, Agustín manages to get all the numbers, and therefore Agustín wins the first game.

M. Balloon Market
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

This year, Pedro was invited to participate in a programming competition that will take place in a very, very distant country. Fortunately, he has already secured his ticket, which includes $$$N$$$ layovers in different countries.

To cover the costs of the trip, he decided to take a suitcase in which he can carry a maximum of $$$K$$$ balloons that he has left over from a previous competition, in order to sell them during the layovers of the trip. Since obtaining balloons in other countries is not so easy, Pedro decided that he will not add more balloons into his suitcase at any of the layovers.

Pedro knows that in the country he will visit during the $$$i$$$-th layover, he can sell each balloon for $$$V_i$$$ pesos (Argentine currency), but there is a problem: he can only enter with up to $$$C_i$$$ balloons without having to pay taxes. For each extra balloon, he will have to pay the Carnival Products Trade Tax, which costs $$$P_i$$$ pesos per extra balloon.

Pedro wants to know what the maximum net profit he can obtain is.

Input

A line with two integers $$$N$$$ and $$$K$$$ ($$$1 \leq N \leq 2 \cdot 10^5$$$, $$$1 \leq K \leq 10^{9}$$$), representing the number of layovers in the trip and the maximum number of balloons that Pedro can carry in his suitcase.

Then a line with $$$N$$$ integers: $$$V_1, V_2, \dots, V_N$$$ ($$$0 \leq V_i \leq 10^{9}$$$), the selling price of a balloon in the country he visits during the $$$i$$$-th layover, in pesos.

Then a line with $$$N$$$ integers: $$$C_1, C_2, \dots, C_N$$$ ($$$0 \leq C_i \leq 10^{9}$$$), the number of balloons that can be brought in for free at the $$$i$$$-th country.

Finally, a line with $$$N$$$ integers: $$$P_1, P_2, \dots, P_N$$$, ($$$0 \leq P_i \leq 10^{9}$$$), the tax that must be paid for each excess balloon when entering the $$$i$$$-th country, in pesos.

Output

A line with an integer, the maximum amount of money (in pesos) that Pedro can earn from selling balloons.

Example
Input
5 6
5 7 8 8 10
2 0 3 0 2
2 2 2 1 2
Output
27
Note

In the example, Pedro could carry in his suitcase $$$6 \leq K$$$ balloons, to sell three in the first city at a price of $$$ \$5 $$$ each, one in the third city for $$$ \$8 $$$, and the remaining two in the last city for $$$ \$10 $$$.

In this way, he would obtain gross income of $$$ 3 \cdot \$5 + 1 \cdot \$8 + 2 \cdot \$10 = \$43 $$$ and would pay a total of $$$ 4 \cdot \$2 + 3 \cdot \$2 + 0 \cdot \$2 + 2 \cdot \$1 + 0 \cdot \$2 = \$16 $$$ in taxes.

Thus, he would have $$$ \$43 - \$16 = \$27 $$$ in net profit, which is the maximum possible in this example.

N. New Dimensions
time limit per test
0.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Camila sells boxes in the shape of rectangular parallelepipeds, that is, the usual shape of a box.

If the height, width, and depth of a box are $$$a$$$, $$$b$$$, and $$$c$$$ respectively, Camila sells it for a price of $$$a^2+b^2+c^2$$$ pesos. The boxes are empty and are only made up of the surface. The material she uses costs half a peso (Argentine currency) per square unit. Therefore, manufacturing a box with height, width, and depth of $$$a$$$, $$$b$$$, and $$$c$$$ costs her $$$ab+bc+ca$$$ pesos.

Camila has a list of $$$N$$$ possible values for the height, width, or depth of the boxes. She only manufactures boxes such that each of its three dimensions belongs to this list of $$$N$$$ measurements she works with. There is no problem using the same value from the list for more than one of the three dimensions of a box.

If she chooses the dimensions $$$a,b,c$$$ of a box in such a way as to maximize her net profit (the difference between the selling price and the cost), how much can she earn at most from one sale?

Input

A line with an integer $$$N$$$ $$$(1\leq N\leq 5000)$$$, the number of allowed values for the dimensions of the box.

Then a line with $$$N$$$ positive integers $$$V_i$$$ $$$(1\leq V_i\leq 10^6)$$$, the possible values for the height, width, or depth. There will be no repeated values.

Output

A single line with a single integer, the maximum amount of money that Camila can earn from selling a box.

Examples
Input
1
1000000
Output
0
Input
5
1734 69384 16 22338 320
Output
4811919424