ICPC 2024-2025 NERC, Southern and Volga Russia Qualifier
A. Cutting into Parts
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Monocarp has a rectangular sheet of paper that he wants to divide into exactly $$$n$$$ parts.

To do this, he can make two types of cuts:

  • A horizontal cut that starts from the left side of the sheet, ends on the right side of the sheet, passing through the entire sheet parallel to the top and bottom sides of the sheet;
  • A vertical cut that starts from the top side of the sheet, ends on the bottom side of the sheet, passing through the entire sheet parallel to the left and right sides of the sheet.

Each type of cut can be made an arbitrary number of times. After each cut, none of the resulting pieces of the sheet are moved or removed; each new cut goes through the entire sheet.

Your task is to determine the minimum total number of horizontal and vertical cuts that Monocarp has to make to divide the sheet into exactly $$$n$$$ parts.

Input

The first line contains an integer $$$n$$$ ($$$2 \le n \le 1\,000\,000$$$) — the number of parts that Monocarp wants to have.

Output

Output the minimum total number of horizontal and vertical cuts that Monocarp has to make to divide the sheet into exactly $$$n$$$ parts.

Examples
Input
8
Output
4
Input
3
Output
2
Input
100
Output
18
Note

In the first example, Monocarp can, for example, make three horizontal cuts and one vertical cut. After that, the sheet will be divided into $$$8$$$ parts with a total of four cuts.

In the second example, Monocarp can, for example, make two vertical cuts. After that, the sheet will be divided into $$$3$$$ parts with two cuts.

In the third example, Monocarp has to make nine vertical cuts and nine horizontal cuts. After that, the sheet will be divided into $$$100$$$ parts with a total of $$$18$$$ cuts.

B. Two Screens
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.

In one second, you can do one of the following two actions:

  • choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen;
  • choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.

You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $$$s$$$, and the second screen displays the sequence $$$t$$$.

Input

The first line contains the string $$$s$$$, and the second line contains the string $$$t$$$ ($$$1 \le |s|, |t| \le 100$$$). Both strings consist of uppercase Latin letters.

Output

Print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence $$$s$$$, and the second screen displays the sequence $$$t$$$.

Examples
Input
GARAGE
GARAGEFORSALE
Output
14
Input
ABCDE
AABCD
Output
10
Note

In the first test case, the following sequence of actions is possible:

  • spend $$$6$$$ seconds to write the sequence GARAGE on the first screen;
  • copy the sequence from the first screen to the second screen;
  • spend $$$7$$$ seconds to complete the sequence on the second screen by writing FORSALE.

In the second test case, the following sequence of actions is possible:

  • spend $$$1$$$ second to write the sequence A on the second screen;
  • copy the sequence from the second screen to the first screen;
  • spend $$$4$$$ seconds to complete the sequence on the first screen by writing BCDE;
  • spend $$$4$$$ seconds to complete the sequence on the second screen by writing ABCD.

C. Repainting Balls
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Monocarp has balls painted in red, yellow, and green colors; exactly $$$a$$$ red balls, exactly $$$b$$$ yellow balls, and exactly $$$c$$$ green balls.

Monocarp can perform the following operation an arbitrary number of times:

  • choose any ball and repaint it in another color: a red ball into yellow or green, a yellow ball into red or green, a green ball into red or yellow.

Your task is to determine the minimum number of operations required to make the number of different colors that the balls are painted equal to two, with the number of balls of each of the two colors being the same.

Adding or removing balls is not allowed.

Input

The first line contains an integer $$$a$$$ ($$$1 \le a \le 1\,000\,000$$$) — the number of red balls.

The second line contains an integer $$$b$$$ ($$$1 \le b \le 1\,000\,000$$$) — the number of yellow balls.

The third line contains an integer $$$c$$$ ($$$1 \le c \le 1\,000\,000$$$) — the number of green balls.

Output

Output the minimum number of operations that Monocarp has to perform to make the number of different colors that the balls are painted equal to two, with the number of balls of each of the two colors being the same.

If this is impossible, output $$$-1$$$.

Examples
Input
2
3
1
Output
1
Input
2
5
4
Output
-1
Input
10
10
10
Output
10
Note

In the first example, it is necessary to repaint the only green ball to red. Then each ball will be either red or yellow, with three red balls and three yellow balls.

In the second example, it is impossible to repaint the balls in such a way that the number of balls of each of the two colors is the same, so you need to output $$$-1$$$.

D. Equal Halves
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Monocarp has an array consisting of $$$n$$$ integers, where $$$n$$$ is an even number.

Monocarp has to perform exactly one swap in his array according to the following rules: choose a position $$$x$$$ in the left half of the array ($$$1 \le x \le \frac{n}{2}$$$), choose a position $$$y$$$ in the right half of the array ($$$\frac{n}{2} + 1 \le y \le n$$$), and swap the elements at positions $$$x$$$ and $$$y$$$.

Your task is to determine whether Monocarp can perform exactly one described swap so that the sum of all elements in the left half of the array becomes equal to the sum of all elements in the right half of the array.

Input

The first line contains an even integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of elements in the array.

The next line contains a sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10\,000$$$), where $$$a_i$$$ is the $$$i$$$-th element of the array.

Output

If it is impossible to perform exactly one described swap, output "NO" (without quotes).

Otherwise, in the first line, output "YES" (without quotes). In the second line, output two integers — the position for the swap from the left half of the array and the position for the swap from the right half of the array. If there are multiple answers, print any of them.

Examples
Input
6
4 1 2 6 7 6
Output
YES
2 5
Input
4
5 4 3 5
Output
NO
Note

In the first example, Monocarp has to swap the second and fifth elements. Then the array will become $$$[4, 7, 2, 6, 1, 6]$$$. The sum of the left half will be $$$4 + 7 + 2 = 13$$$, and the sum of the right half will be $$$6 + 1 + 6 = 13$$$. Thus, the sums of the left and right halves of the array will become equal.

In the second example, it is impossible to perform a swap so that the sums of the left and right halves of the array become equal.

E. Binomial Coefficients, Kind Of
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:

    for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive)
C[n][0] = 1;
C[n][n] = 1;
for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive)
C[n][k] = C[n][k - 1] + C[n - 1][k - 1];
}

Unfortunately, he made an error, since the right formula is the following:

            C[n][k] = C[n - 1][k] + C[n - 1][k - 1]

But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $$$t$$$ various pairs $$$(n_i, k_i)$$$. Note that they should be calculated according to the first (wrong) formula.

Since values $$$C[n_i][k_i]$$$ may be too large, print them modulo $$$10^9 + 7$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of pairs. Next, $$$t$$$ pairs are written in two lines.

The second line contains $$$t$$$ integers $$$n_1, n_2, \dots, n_t$$$ ($$$2 \le n_i \le 10^5$$$).

The third line contains $$$t$$$ integers $$$k_1, k_2, \dots, k_t$$$ ($$$1 \le k_i \lt n_i$$$).

Output

Print $$$t$$$ integers $$$C[n_i][k_i]$$$ modulo $$$10^9 + 7$$$.

Examples
Input
3
2 5 5
1 2 3
Output
2
4
8
Input
4
100000 100000 100000 100000
1 33333 66666 99999
Output
2
326186014
984426998
303861760

F. New Game
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

There's a new game Monocarp wants to play. The game uses a deck of $$$n$$$ cards, where the $$$i$$$-th card has exactly one integer $$$a_i$$$ written on it.

At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.

In other words, if on the previous turn Monocarp took a card with the number $$$x$$$, then on the current turn he can take either a card with the number $$$x$$$ or a card with the number $$$x + 1$$$.

After Monocarp takes a card on the current turn, it is removed from the deck.

According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $$$k$$$.

If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.

Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

Input

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200\,000$$$) — the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.

The second line contains a sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{9}$$$), where $$$a_i$$$ is the number written on the $$$i$$$-th card.

Output

Output the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

Examples
Input
10 2
5 2 4 3 4 3 4 5 3 2
Output
6
Input
5 1
10 11 10 11 10
Output
3
Input
9 3
4 5 4 4 6 5 4 4 6
Output
9
Note

In the first example, Monocarp needs to take any of the cards with the number $$$3$$$. On the next two turns, he needs to take the two remaining cards with the number $$$3$$$. On the next three turns, he needs to take three cards with the number $$$4$$$. After that, Monocarp will not be able to take any more cards from the deck, and he will have $$$6$$$ cards.

G. Stores
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Monocarp lives in a city, which can be represented as a segment of the number line. The leftmost border of the city is at the point $$$0$$$, and the rightmost border is at the point $$$10^{9}$$$.

There are $$$n$$$ stores in the city. Each store is located at a point with integer coordinate: the $$$i$$$-th store is located at the point $$$a_i$$$. Among the $$$n$$$ stores, there is exactly one store at the point $$$0$$$ and exactly one store at the point $$$10^{9}$$$. There is at most one store at each integer coordinate.

The government has decided to build $$$m$$$ more stores in the city. It is prohibited to build a new store at a point where a store already exists, and at each integer point, at most one new store can be built. It is also prohibited to build stores to the left of point $$$0$$$ and to the right of point $$$10^{9}$$$.

Your task is to choose where to build the new $$$m$$$ stores in such a way that among all $$$(n+m-1)$$$ pairs of neighboring stores, the maximum distance between the stores in a pair is the minimum possible. In other words, you have to minimize the length of the longest segment connecting two neighboring stores.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 200\,000$$$, $$$1 \le m \le 200\,000$$$) — the number of stores already built in the city and the number of new stores to be built.

The second line contains a sequence of distinct integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{9}$$$), where $$$a_i$$$ is the coordinate of the $$$i$$$-th store.

Additional constraint on the input: there is exactly one number $$$0$$$ (the store at coordinate $$$0$$$) and exactly one number $$$10^{9}$$$ (the store at coordinate $$$10^{9}$$$) in the given sequence.

Output

Print one integer — the minimum possible maximum distance between two neighboring stores.

Examples
Input
4 1
750000000 250000000 0 1000000000
Output
250000000
Input
2 2
0 1000000000
Output
333333334
Input
3 17
500000000 0 1000000000
Output
55555556
Input
4 2000
333333333 0 777777777 1000000000
Output
499376
Note

In the first example, a new store needs to be built at point $$$500\,000\,000$$$. Then the distance between all pairs of neighboring stores will be $$$250\,000\,000$$$.

In the second example, two new stores can be built at points $$$333\,333\,333$$$ and $$$666\,666\,666$$$. Then the stores will be located at points $$$[0, 333\,333\,333, 666\,666\,666, 1\,000\,000\,000]$$$, and the maximum distance between a pair of neighboring stores will be $$$1\,000\,000\,000 - 666\,666\,666 = 333\,333\,334$$$ (between the third and fourth stores).

H. Maximum Beauty
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Let's denote the beauty of a string as follows: consider the lengths of all maximal contiguous blocks consisting of the same letters. Then, the beauty of a string is the sum of the squares of the lengths of all such blocks.

For example, let the string be "aabbbcaaadddd". In this string, the following blocks of identical letters exist: a block of two letters "a", a block of three letters "b", a block of one letter "c", a block of three letters "a", and a block of four letters "d". Thus, the beauty of this string is $$$2^2 + 3^2 + 1^2 + 3^2 + 4^2 = 4 + 9 + 1 + 9 + 16 = 39$$$.

Monocarp has a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. He will perform the following action exactly once: choose a pair of adjacent different letters in the string $$$s$$$ and swap them.

Your task is to determine the maximum beauty of the new string that Monocarp can obtain after exactly one swap of adjacent different letters in his string $$$s$$$.

Input

The first line contains an integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the length of the string $$$s$$$.

The second line contains the string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.

Additional constraint on the input:

  • there are at least two different letters in the given string.
Output

Output the maximum beauty of the new string that Monocarp can obtain after exactly one swap of adjacent different letters in his string $$$s$$$.

Examples
Input
11
aabaacaabaa
Output
21
Input
5
wwwwz
Output
11
Note

In the first example, for example, it is possible to swap the third and fourth letters. Then Monocarp will get the string "aaabacaabaa". The beauty of this string is $$$3^2 + 1^2 + 1^2 + 1^2 + 2^2 + 1^2 + 2^2 = 9 + 1 + 1 + 1 + 4 + 1 + 4 = 21$$$.

In the second example, it is possible to swap the fourth and fifth letters. Then Monocarp will get the string "wwwzw". The beauty of this string is $$$3^2 + 1^2 + 1^2 = 9 + 1 + 1 = 11$$$.

I. Attribute Checks
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.

During the game, you'll acquire $$$m$$$ attribute points that allow you to increase your attribute levels — one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.

Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?

Note that you can't change the order of records.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m \le 5000$$$; $$$m \lt n \le 2 \cdot 10^6$$$) — the number of records in the list and the total number of points you'll get during the game.

The second line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$-m \le r_i \le m$$$), where $$$r_i$$$ encodes the $$$i$$$-th record:

  • If $$$r_i = 0$$$, then the $$$i$$$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence;
  • If $$$r_i \gt 0$$$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $$$|r_i|$$$, you pass.
  • If $$$r_i \lt 0$$$, then it's a Strength check: if your Strength level is greater than or equal to $$$|r_i|$$$, you pass.
Output

What is the maximum number of checks you can pass if you can't change the order of records?

Examples
Input
10 5
0 1 0 2 0 -3 0 -4 0 -5
Output
3
Input
3 1
1 -1 0
Output
0
Input
9 3
0 0 1 0 2 -3 -2 -2 1
Output
4

J. Card Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In the most popular card game in Berland, a deck of $$$n \times m$$$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $$$1$$$ to $$$n$$$, and ranks are numbered from $$$1$$$ to $$$m$$$. There is exactly one card in the deck for each combination of suit and rank.

A card with suit $$$a$$$ and rank $$$b$$$ can beat a card with suit $$$c$$$ and rank $$$d$$$ in one of two cases:

  • $$$a = 1$$$, $$$c \ne 1$$$ (a card of suit $$$1$$$ can beat a card of any other suit);
  • $$$a = c$$$, $$$b \gt d$$$ (a card can beat any other card of the same suit but of a lower rank).

Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.

Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $$$998244353$$$.

Input

The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 500$$$).

Additional constraint on the input: $$$m$$$ is even.

Output

Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $$$998244353$$$.

Examples
Input
1 4
Output
2
Input
2 2
Output
2
Input
3 6
Output
1690
Input
5 4
Output
568
Input
500 500
Output
84693741

K. Choose Your Queries
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$, consisting of $$$n$$$ integers (numbered from $$$1$$$ to $$$n$$$). Initially, they are all zeroes.

You have to process $$$q$$$ queries. The $$$i$$$-th query consists of two different integers $$$x_i$$$ and $$$y_i$$$. During the $$$i$$$-th query, you have to choose an integer $$$p$$$ (which is either $$$x_i$$$ or $$$y_i$$$) and an integer $$$d$$$ (which is either $$$1$$$ or $$$-1$$$), and assign $$$a_p = a_p + d$$$.

After each query, every element of $$$a$$$ should be a non-negative integer.

Process all queries in such a way that the sum of all elements of $$$a$$$ after the last query is the minimum possible.

Input

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 3 \cdot 10^5$$$; $$$1 \le q \le 3 \cdot 10^5$$$) — the number of elements in $$$a$$$ and the number of queries, respectively.

Then $$$q$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$; $$$x_i \ne y_i$$$) — the description of the $$$i$$$-th query.

Output

For each query, print a line containing two characters:

  • the first character should be x if you choose $$$p=x_i$$$, or y if you choose $$$p=y_i$$$;
  • the second character should be + if you choose $$$d=1$$$, or - if you choose $$$d=-1$$$.

If there are multiple answers, print any of them.

Examples
Input
3 4
1 2
3 2
3 1
1 2
Output
y+
x+
x-
y-
Input
4 4
1 2
2 3
3 4
3 2
Output
y+
y+
x-
y-
Input
4 2
2 1
4 3
Output
y+
x+

L. Variable Damage
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Monocarp is gathering an army to fight a dragon in a videogame.

The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter — his health. Each defensive artifact also has one parameter — its durability.

Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.

The battle consists of rounds that proceed as follows:

  • first, the dragon deals damage equal to $$$\frac{1}{a + b}$$$ (a real number without rounding) to each hero, where $$$a$$$ is the number of heroes alive and $$$b$$$ is the number of active artifacts;
  • after that, all heroes with health $$$0$$$ or less die;
  • finally, some artifacts are deactivated. An artifact with durability $$$x$$$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $$$x$$$ total damage (from the start of the battle).

The battle ends when there are no heroes left alive.

Initially, the army is empty. There are $$$q$$$ queries: add a hero with health $$$x$$$ or an artifact with durability $$$y$$$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

Input

The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries.

In the $$$i$$$-th of the following $$$q$$$ lines, there are two integers $$$t_i$$$ and $$$v_i$$$ ($$$t_i \in \{1, 2\}$$$; $$$1 \le v_i \le 10^9$$$) — the type of the query and the value of the query parameter. If the type is $$$1$$$, a hero with health $$$v_i$$$ is added. If the type is $$$2$$$, an artifact with durability $$$v_i$$$ is added.

Output

Print $$$q$$$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

Examples
Input
3
2 5
1 4
1 10
Output
0
8
19
Input
10
1 9
1 6
2 4
1 8
1 3
2 10
1 3
1 6
1 10
2 6
Output
9
15
19
27
30
39
42
48
59
65
Note

Let's consider the first example.

  • An artifact with durability $$$5$$$ is added. Since there are no heroes yet, the battle ends immediately.
  • A hero with health $$$4$$$ is added. Monocarp can give him an artifact with durability $$$5$$$. First, there are rounds in which the hero takes $$$\frac{1}{1 + 1} = \frac{1}{2}$$$ damage. After $$$8$$$ such rounds, a total of $$$4$$$ damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after $$$8$$$ rounds.
  • A hero with health $$$10$$$ is added. Now let the artifact with durability $$$5$$$ be with this hero. Then, in the first $$$12$$$ rounds, the heroes will take $$$12 \cdot \frac{1}{2 + 1} = 4$$$ damage, and the first hero will die. The second hero has $$$6$$$ health left, and the artifact has $$$1$$$ durability. Now the damage is $$$\frac{1}{2}$$$, so after another $$$2$$$ rounds, the artifact will deactivate. The second hero has $$$5$$$ health left. After another $$$5$$$ rounds, the second hero will die. Therefore, the answer is $$$12 + 2 + 5 = 19$$$.