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:
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.
The first line contains an integer $$$n$$$ ($$$2 \le n \le 1\,000\,000$$$) — the number of parts that Monocarp wants to have.
Output the minimum total number of horizontal and vertical cuts that Monocarp has to make to divide the sheet into exactly $$$n$$$ parts.
8
4
3
2
100
18
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.
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:
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$$$.
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.
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$$$.
GARAGEGARAGEFORSALE
14
ABCDEAABCD
10
In the first test case, the following sequence of actions is possible:
In the second test case, the following sequence of actions is possible:
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:
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.
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 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$$$.
231
1
254
-1
101010
10
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$$$.
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.
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.
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.
64 1 2 6 7 6
YES 2 5
45 4 3 5
NO
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.
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$$$.
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$$$).
Print $$$t$$$ integers $$$C[n_i][k_i]$$$ modulo $$$10^9 + 7$$$.
32 5 51 2 3
2 4 8
4100000 100000 100000 1000001 33333 66666 99999
2 326186014 984426998 303861760
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.
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 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.
10 25 2 4 3 4 3 4 5 3 2
6
5 110 11 10 11 10
3
9 34 5 4 4 6 5 4 4 6
9
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.
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.
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.
Print one integer — the minimum possible maximum distance between two neighboring stores.
4 1750000000 250000000 0 1000000000
250000000
2 20 1000000000
333333334
3 17500000000 0 1000000000
55555556
4 2000333333333 0 777777777 1000000000
499376
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).
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$$$.
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:
Output the maximum beauty of the new string that Monocarp can obtain after exactly one swap of adjacent different letters in his string $$$s$$$.
11aabaacaabaa
21
5wwwwz
11
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$$$.
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.
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:
What is the maximum number of checks you can pass if you can't change the order of records?
10 50 1 0 2 0 -3 0 -4 0 -5
3
3 11 -1 0
0
9 30 0 1 0 2 -3 -2 -2 1
4
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:
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$$$.
The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 500$$$).
Additional constraint on the input: $$$m$$$ is even.
Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $$$998244353$$$.
1 4
2
2 2
2
3 6
1690
5 4
568
500 500
84693741
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.
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.
For each query, print a line containing two characters:
If there are multiple answers, print any of them.
3 41 23 23 11 2
y+ x+ x- y-
4 41 22 33 43 2
y+ y+ x- y-
4 22 14 3
y+ x+
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:
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.
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.
Print $$$q$$$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
32 51 41 10
0 8 19
101 91 62 41 81 32 101 31 61 102 6
9 15 19 27 30 39 42 48 59 65
Let's consider the first example.