question large_stringlengths 265 13.2k |
|---|
Solve the programming task below in a Python markdown code block.
The cat Snuke wants to play a popular Japanese game called Γ
tCoder, so Iroha has decided to teach him Japanese.
When counting pencils in Japanese, the counter word "ζ¬" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "ζ¬" in the phrase "N ζ¬" for a positive integer N not exceeding 999 is as follows:
- hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;
- pon when the digit in the one's place of N is 0, 1, 6 or 8;
- bon when the digit in the one's place of N is 3.
Given N, print the pronunciation of "ζ¬" in the phrase "N ζ¬".
-----Constraints-----
- N is a positive integer not exceeding 999.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the answer.
-----Sample Input-----
16
-----Sample Output-----
pon
The digit in the one's place of 16 is 6, so the "ζ¬" in "16 ζ¬" is pronounced pon.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
It's now the season of TAKOYAKI FESTIVAL!
This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.
As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.
There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.
-----Constraints-----
- All values in input are integers.
- 2 \leq N \leq 50
- 0 \leq d_i \leq 100
-----Input-----
Input is given from Standard Input in the following format:
N
d_1 d_2 ... d_N
-----Output-----
Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.
-----Sample Input-----
3
3 1 2
-----Sample Output-----
11
There are three possible choices:
- Eat the first and second takoyaki. You will restore 3 health points.
- Eat the second and third takoyaki. You will restore 2 health points.
- Eat the first and third takoyaki. You will restore 6 health points.
The sum of these values is 11.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are two buttons, one of size A and one of size B.
When you press a button of size X, you get X coins and the size of that button decreases by 1.
You will press a button twice. Here, you can press the same button twice, or press both buttons once.
At most how many coins can you get?
-----Constraints-----
- All values in input are integers.
- 3 \leq A, B \leq 20
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
Print the maximum number of coins you can get.
-----Sample Input-----
5 3
-----Sample Output-----
9
You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.
It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.
Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.
Assume also that the depth of the snow cover is always at least 1 meter.
-----Constraints-----
- 1 \leq a < b < 499500(=1+2+3+...+999)
- All values in input are integers.
- There is no input that contradicts the assumption.
-----Input-----
Input is given from Standard Input in the following format:
a b
-----Output-----
If the depth of the snow cover is x meters, print x as an integer.
-----Sample Input-----
8 13
-----Sample Output-----
2
The heights of the two towers are 10 meters and 15 meters, respectively.
Thus, we can see that the depth of the snow cover is 2 meters.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Serval is fighting with a monster.
The health of the monster is H.
In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.
Serval wins when the monster's health becomes 0 or below.
Find the number of attacks Serval needs to make before winning.
-----Constraints-----
- 1 \leq H \leq 10^4
- 1 \leq A \leq 10^4
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
H A
-----Output-----
Print the number of attacks Serval needs to make before winning.
-----Sample Input-----
10 4
-----Sample Output-----
3
- After one attack, the monster's health will be 6.
- After two attacks, the monster's health will be 2.
- After three attacks, the monster's health will be -2.
Thus, Serval needs to make three attacks to win.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Alice and Bob are controlling a robot. They each have one switch that controls the robot.
Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.
Bob started holding down his button C second after the start-up, and released his button D second after the start-up.
For how many seconds both Alice and Bob were holding down their buttons?
-----Constraints-----
- 0β€A<Bβ€100
- 0β€C<Dβ€100
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
A B C D
-----Output-----
Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.
-----Sample Input-----
0 75 25 100
-----Sample Output-----
50
Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.
Bob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.
Therefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?
-----Constraints-----
- N is an integer between 1 and 200 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the count.
-----Sample Input-----
105
-----Sample Output-----
1
Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.
-----Constraints-----
- 1 \leq N,K \leq 100
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N K
-----Output-----
Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.
-----Sample Input-----
7 3
-----Sample Output-----
1
When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.
What is the earliest ABC where Kurohashi can make his debut?
-----Constraints-----
- 100 \leq N \leq 999
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
-----Sample Input-----
111
-----Sample Output-----
111
The next ABC to be held is ABC 111, where Kurohashi can make his debut.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi and Aoki will have a battle using their monsters.
The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.
The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...
Here, an attack decreases the opponent's health by the value equal to the attacker's strength.
The monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.
If Takahashi will win, print Yes; if he will lose, print No.
-----Constraints-----
- 1 \leq A,B,C,D \leq 100
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
A B C D
-----Output-----
If Takahashi will win, print Yes; if he will lose, print No.
-----Sample Input-----
10 9 10 10
-----Sample Output-----
No
First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.
Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.
Takahashi's monster is the first to have 0 or less health, so Takahashi loses.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.
Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:
- Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.
It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.
-----Constraints-----
- 1 \leq H, W \leq 100
- a_{i, j} is . or #.
- There is at least one black square in the whole grid.
-----Input-----
Input is given from Standard Input in the following format:
H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}
-----Output-----
Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.
-----Sample Input-----
4 4
##.#
....
##.#
.#.#
-----Sample Output-----
###
###
.##
The second row and the third column in the original grid will be removed.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Print the circumference of a circle of radius R.
-----Constraints-----
- 1 \leq R \leq 100
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
R
-----Output-----
Print the circumference of the circle.
Your output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.
-----Sample Input-----
1
-----Sample Output-----
6.28318530717958623200
Since we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.
-----Constraints-----
- The length of S is 4.
- S consists of uppercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.
-----Sample Input-----
ASSA
-----Sample Output-----
Yes
S consists of A and S which both appear twice in S.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have two integers: A and B.
Print the largest number among A + B, A - B, and A \times B.
-----Constraints-----
- All values in input are integers.
- -100 \leq A,\ B \leq 100
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
Print the largest number among A + B, A - B, and A \times B.
-----Sample Input-----
-13 3
-----Sample Output-----
-10
The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N mountains ranging from east to west, and an ocean to the west.
At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.
The height of the i-th mountain from the west is H_i.
You can certainly see the ocean from the inn at the top of the westmost mountain.
For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.
From how many of these N inns can you see the ocean?
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 20
- 1 \leq H_i \leq 100
-----Input-----
Input is given from Standard Input in the following format:
N
H_1 H_2 ... H_N
-----Output-----
Print the number of inns from which you can see the ocean.
-----Sample Input-----
4
6 5 6 8
-----Sample Output-----
3
You can see the ocean from the first, third and fourth inns from the west.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $1$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $3$ steps, and the second contains $4$ steps, she will pronounce the numbers $1, 2, 3, 1, 2, 3, 4$.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
-----Input-----
The first line contains $n$ ($1 \le n \le 1000$) β the total number of numbers pronounced by Tanya.
The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $x$ steps, she will pronounce the numbers $1, 2, \dots, x$ in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
-----Output-----
In the first line, output $t$ β the number of stairways that Tanya climbed. In the second line, output $t$ numbers β the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
-----Examples-----
Input
7
1 2 3 1 2 3 4
Output
2
3 4
Input
4
1 1 1 1
Output
4
1 1 1 1
Input
5
1 2 3 4 5
Output
1
5
Input
5
1 2 1 2 1
Output
3
2 2 1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Recently Vova found $n$ candy wrappers. He remembers that he bought $x$ candies during the first day, $2x$ candies during the second day, $4x$ candies during the third day, $\dots$, $2^{k-1} x$ candies during the $k$-th day. But there is an issue: Vova remembers neither $x$ nor $k$ but he is sure that $x$ and $k$ are positive integers and $k > 1$.
Vova will be satisfied if you tell him any positive integer $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$. It is guaranteed that at least one solution exists. Note that $k > 1$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then $t$ test cases follow.
The only line of the test case contains one integer $n$ ($3 \le n \le 10^9$) β the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $x$ and integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$.
-----Output-----
Print one integer β any positive integer value of $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$.
-----Example-----
Input
7
3
6
7
21
28
999999999
999999984
Output
1
2
1
7
4
333333333
333333328
-----Note-----
In the first test case of the example, one of the possible answers is $x=1, k=2$. Then $1 \cdot 1 + 2 \cdot 1$ equals $n=3$.
In the second test case of the example, one of the possible answers is $x=2, k=2$. Then $1 \cdot 2 + 2 \cdot 2$ equals $n=6$.
In the third test case of the example, one of the possible answers is $x=1, k=3$. Then $1 \cdot 1 + 2 \cdot 1 + 4 \cdot 1$ equals $n=7$.
In the fourth test case of the example, one of the possible answers is $x=7, k=2$. Then $1 \cdot 7 + 2 \cdot 7$ equals $n=21$.
In the fifth test case of the example, one of the possible answers is $x=4, k=3$. Then $1 \cdot 4 + 2 \cdot 4 + 4 \cdot 4$ equals $n=28$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm: if the last digit of the number is non-zero, she decreases the number by one; if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).
You are given an integer number $n$. Tanya will subtract one from it $k$ times. Your task is to print the result after all $k$ subtractions.
It is guaranteed that the result will be positive integer number.
-----Input-----
The first line of the input contains two integer numbers $n$ and $k$ ($2 \le n \le 10^9$, $1 \le k \le 50$) β the number from which Tanya will subtract and the number of subtractions correspondingly.
-----Output-----
Print one integer number β the result of the decreasing $n$ by one $k$ times.
It is guaranteed that the result will be positive integer number.
-----Examples-----
Input
512 4
Output
50
Input
1000000000 9
Output
1
-----Note-----
The first example corresponds to the following sequence: $512 \rightarrow 511 \rightarrow 510 \rightarrow 51 \rightarrow 50$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi loves takoyaki - a ball-shaped snack.
With a takoyaki machine, he can make at most X pieces of takoyaki at a time, taking T minutes regardless of the number of pieces to make.
How long does it take to make N takoyaki?
-----Constraints-----
- 1 \leq N,X,T \leq 1000
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N X T
-----Output-----
Print an integer representing the minimum number of minutes needed to make N pieces of takoyaki.
-----Sample Input-----
20 12 6
-----Sample Output-----
12
He can make 12 pieces of takoyaki in the first 6 minutes and 8 more in the next 6 minutes, so he can make 20 in a total of 12 minutes.
Note that being able to make 12 in 6 minutes does not mean he can make 2 in 1 minute.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N students in a school.
We will divide these students into some groups, and in each group they will discuss some themes.
You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.
Divide the students so that the number of groups consisting of three or more students is maximized.
-----Constraints-----
- 1 \leq N \leq 1000
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If you can form at most x groups consisting of three or more students, print x.
-----Sample Input-----
8
-----Sample Output-----
2
For example, you can form a group of three students and another of five students.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now.
-----Constraints-----
- All values in input are integers.
- 0 \leq A, P \leq 100
-----Input-----
Input is given from Standard Input in the following format:
A P
-----Output-----
Print the maximum number of apple pies we can make with what we have.
-----Sample Input-----
1 3
-----Sample Output-----
3
We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given positive integers A and B.
If A is a divisor of B, print A + B; otherwise, print B - A.
-----Constraints-----
- All values in input are integers.
- 1 \leq A \leq B \leq 20
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
If A is a divisor of B, print A + B; otherwise, print B - A.
-----Sample Input-----
4 12
-----Sample Output-----
16
As 4 is a divisor of 12, 4 + 12 = 16 should be printed.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi wants to be a member of some web service.
He tried to register himself with the ID S, which turned out to be already used by another user.
Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.
He is now trying to register with the ID T. Determine whether this string satisfies the property above.
-----Constraints-----
- S and T are strings consisting of lowercase English letters.
- 1 \leq |S| \leq 10
- |T| = |S| + 1
-----Input-----
Input is given from Standard Input in the following format:
S
T
-----Output-----
If T satisfies the property in Problem Statement, print Yes; otherwise, print No.
-----Sample Input-----
chokudai
chokudaiz
-----Sample Output-----
Yes
chokudaiz can be obtained by appending z at the end of chokudai.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have two distinct integers A and B.
Print the integer K such that |A - K| = |B - K|.
If such an integer does not exist, print IMPOSSIBLE instead.
-----Constraints-----
- All values in input are integers.
- 0 \leq A,\ B \leq 10^9
- A and B are distinct.
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
Print the integer K satisfying the condition.
If such an integer does not exist, print IMPOSSIBLE instead.
-----Sample Input-----
2 16
-----Sample Output-----
9
|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given is a three-digit integer N. Does N contain the digit 7?
If so, print Yes; otherwise, print No.
-----Constraints-----
- 100 \leq N \leq 999
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If N contains the digit 7, print Yes; otherwise, print No.
-----Sample Input-----
117
-----Sample Output-----
Yes
117 contains 7 as its last digit.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(101) = 1 + 0 + 1 = 2.
Given an integer N, determine if S(N) divides N.
-----Constraints-----
- 1 \leq N \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If S(N) divides N, print Yes; if it does not, print No.
-----Sample Input-----
12
-----Sample Output-----
Yes
In this input, N=12.
As S(12) = 1 + 2 = 3, S(N) divides N.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)
Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).
Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.
-----Constraints-----
- |x_1|,|y_1|,|x_2|,|y_2| \leq 100
- (x_1,y_1) β (x_2,y_2)
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
x_1 y_1 x_2 y_2
-----Output-----
Print x_3,y_3,x_4 and y_4 as integers, in this order.
-----Sample Input-----
0 0 0 1
-----Sample Output-----
-1 1 -1 0
(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have A balls with the string S written on each of them and B balls with the string T written on each of them.
From these balls, Takahashi chooses one with the string U written on it and throws it away.
Find the number of balls with the string S and balls with the string T that we have now.
-----Constraints-----
- S, T, and U are strings consisting of lowercase English letters.
- The lengths of S and T are each between 1 and 10 (inclusive).
- S \not= T
- S=U or T=U.
- 1 \leq A,B \leq 10
- A and B are integers.
-----Input-----
Input is given from Standard Input in the following format:
S T
A B
U
-----Output-----
Print the answer, with space in between.
-----Sample Input-----
red blue
3 4
red
-----Sample Output-----
2 4
Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given are a positive integer N and a string S of length N consisting of lowercase English letters.
Determine whether the string is a concatenation of two copies of some string.
That is, determine whether there is a string T such that S = T + T.
-----Constraints-----
- 1 \leq N \leq 100
- S consists of lowercase English letters.
- |S| = N
-----Input-----
Input is given from Standard Input in the following format:
N
S
-----Output-----
If S is a concatenation of two copies of some string, print Yes; otherwise, print No.
-----Sample Input-----
6
abcabc
-----Sample Output-----
Yes
Let T = abc, and S = T + T.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.
-----Constraints-----
- 1 \leq W,H \leq 10^9
- 0\leq x\leq W
- 0\leq y\leq H
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
W H x y
-----Output-----
Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.
The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.
-----Sample Input-----
2 3 1 2
-----Sample Output-----
3.000000 0
The line x=1 gives the optimal cut, and no other line does.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In Japan, people make offerings called hina arare, colorful crackers, on March 3.
We have a bag that contains N hina arare. (From here, we call them arare.)
It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.
We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.
If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.
-----Constraints-----
- 1 \leq N \leq 100
- S_i is P, W, G or Y.
- There always exist i, j and k such that S_i=P, S_j=W and S_k=G.
-----Input-----
Input is given from Standard Input in the following format:
N
S_1 S_2 ... S_N
-----Output-----
If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.
-----Sample Input-----
6
G W Y P Y W
-----Sample Output-----
Four
The bag contained arare in four colors, so you should print Four.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The next lecture in a high school requires two topics to be discussed. The $i$-th topic is interesting by $a_i$ units for the teacher and by $b_i$ units for the students.
The pair of topics $i$ and $j$ ($i < j$) is called good if $a_i + a_j > b_i + b_j$ (i.e. it is more interesting for the teacher).
Your task is to find the number of good pairs of topics.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of topics.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the interestingness of the $i$-th topic for the teacher.
The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$), where $b_i$ is the interestingness of the $i$-th topic for the students.
-----Output-----
Print one integer β the number of good pairs of topic.
-----Examples-----
Input
5
4 8 2 6 2
4 5 4 1 3
Output
7
Input
4
1 3 2 4
1 3 2 4
Output
0
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Mishka got an integer array $a$ of length $n$ as a birthday present (what a surprise!).
Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: Replace each occurrence of $1$ in the array $a$ with $2$; Replace each occurrence of $2$ in the array $a$ with $1$; Replace each occurrence of $3$ in the array $a$ with $4$; Replace each occurrence of $4$ in the array $a$ with $3$; Replace each occurrence of $5$ in the array $a$ with $6$; Replace each occurrence of $6$ in the array $a$ with $5$; $\dots$ Replace each occurrence of $10^9 - 1$ in the array $a$ with $10^9$; Replace each occurrence of $10^9$ in the array $a$ with $10^9 - 1$.
Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($2i - 1, 2i$) for each $i \in\{1, 2, \ldots, 5 \cdot 10^8\}$ as described above.
For example, for the array $a = [1, 2, 4, 5, 10]$, the following sequence of arrays represents the algorithm:
$[1, 2, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $1$ with $2$) $\rightarrow$ $[2, 2, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $2$ with $1$) $\rightarrow$ $[1, 1, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $3$ with $4$) $\rightarrow$ $[1, 1, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $4$ with $3$) $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ (replace all occurrences of $5$ with $6$) $\rightarrow$ $[1, 1, 3, 6, 10]$ $\rightarrow$ (replace all occurrences of $6$ with $5$) $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ $\dots$ $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ (replace all occurrences of $10$ with $9$) $\rightarrow$ $[1, 1, 3, 5, 9]$. The later steps of the algorithm do not change the array.
Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.
-----Input-----
The first line of the input contains one integer number $n$ ($1 \le n \le 1000$) β the number of elements in Mishka's birthday present (surprisingly, an array).
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the elements of the array.
-----Output-----
Print $n$ integers β $b_1, b_2, \dots, b_n$, where $b_i$ is the final value of the $i$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $a$. Note that you cannot change the order of elements in the array.
-----Examples-----
Input
5
1 2 4 5 10
Output
1 1 3 5 9
Input
10
10000 10 50605065 1 5 89 5 999999999 60506056 1000000000
Output
9999 9 50605065 1 5 89 5 999999999 60506055 999999999
-----Note-----
The first example is described in the problem statement.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an undirected graph consisting of $n$ vertices and $m$ edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $a$ is connected with a vertex $b$, a vertex $b$ is also connected with a vertex $a$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices $u$ and $v$ belong to the same connected component if and only if there is at least one path along edges connecting $u$ and $v$.
A connected component is a cycle if and only if its vertices can be reordered in such a way that: the first vertex is connected with the second vertex by an edge, the second vertex is connected with the third vertex by an edge, ... the last vertex is connected with the first vertex by an edge, all the described edges of a cycle are distinct.
A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices. [Image] There are $6$ connected components, $2$ of them are cycles: $[7, 10, 16]$ and $[5, 11, 9, 15]$.
-----Input-----
The first line contains two integer numbers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) β number of vertices and edges.
The following $m$ lines contains edges: edge $i$ is given as a pair of vertices $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$). There is no multiple edges in the given graph, i.e. for each pair ($v_i, u_i$) there no other pairs ($v_i, u_i$) and ($u_i, v_i$) in the list of edges.
-----Output-----
Print one integer β the number of connected components which are also cycles.
-----Examples-----
Input
5 4
1 2
3 4
5 4
3 5
Output
1
Input
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
Output
2
-----Note-----
In the first example only component $[3, 4, 5]$ is also a cycle.
The illustration above corresponds to the second example.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an undirected tree of $n$ vertices.
Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.
You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.
How many nice edges are there in the given tree?
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) β the number of vertices in the tree.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2$) β the colors of the vertices. $a_i = 1$ means that vertex $i$ is colored red, $a_i = 2$ means that vertex $i$ is colored blue and $a_i = 0$ means that vertex $i$ is uncolored.
The $i$-th of the next $n - 1$ lines contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$, $v_i \ne u_i$) β the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.
-----Output-----
Print a single integer β the number of nice edges in the given tree.
-----Examples-----
Input
5
2 0 0 1 2
1 2
2 3
2 4
2 5
Output
1
Input
5
1 0 0 0 2
1 2
2 3
3 4
4 5
Output
4
Input
3
1 1 2
2 3
1 3
Output
0
-----Note-----
Here is the tree from the first example: [Image]
The only nice edge is edge $(2, 4)$. Removing it makes the tree fall apart into components $\{4\}$ and $\{1, 2, 3, 5\}$. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.
Here is the tree from the second example: [Image]
Every edge is nice in it.
Here is the tree from the third example: [Image]
Edge $(1, 3)$ splits the into components $\{1\}$ and $\{3, 2\}$, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge $(2, 3)$ splits the into components $\{1, 3\}$ and $\{2\}$, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$.
Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].
Your task is to print the median (the middle element) of this list. For the example above this will be "bc".
It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.
-----Input-----
The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β the length of strings.
The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters.
The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters.
It is guaranteed that $s$ is lexicographically less than $t$.
It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.
-----Output-----
Print one string consisting exactly of $k$ lowercase Latin letters β the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$.
-----Examples-----
Input
2
az
bf
Output
bc
Input
5
afogk
asdji
Output
alvuw
Input
6
nijfvj
tvqhwp
Output
qoztvz
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed.
Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO".
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of elements in $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.
-----Output-----
If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
-----Examples-----
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vova plans to go to the conference by train. Initially, the train is at the point $1$ and the destination point of the path is the point $L$. The speed of the train is $1$ length unit per minute (i.e. at the first minute the train is at the point $1$, at the second minute β at the point $2$ and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by $v$ (i.e. the first lantern is at the point $v$, the second is at the point $2v$ and so on).
There is also exactly one standing train which occupies all the points from $l$ to $r$ inclusive.
Vova can see the lantern at the point $p$ if $p$ is divisible by $v$ and there is no standing train at this position ($p \not\in [l; r]$). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to $t$ different conferences, so you should answer $t$ independent queries.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of queries.
Then $t$ lines follow. The $i$-th line contains four integers $L_i, v_i, l_i, r_i$ ($1 \le L, v \le 10^9$, $1 \le l \le r \le L$) β destination point of the $i$-th path, the period of the lantern appearance and the segment occupied by the standing train.
-----Output-----
Print $t$ lines. The $i$-th line should contain one integer β the answer for the $i$-th query.
-----Example-----
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
-----Note-----
For the first example query, the answer is $3$. There are lanterns at positions $2$, $4$, $6$, $8$ and $10$, but Vova didn't see the lanterns at positions $4$ and $6$ because of the standing train.
For the second example query, the answer is $0$ because the only lantern is at the point $51$ and there is also a standing train at this point.
For the third example query, the answer is $1134$ because there are $1234$ lanterns, but Vova didn't see the lanterns from the position $100$ to the position $199$ inclusive.
For the fourth example query, the answer is $0$ because the standing train covers the whole path.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a string $s$ consisting of $n$ lowercase Latin letters. Polycarp wants to remove exactly $k$ characters ($k \le n$) from the string $s$. Polycarp uses the following algorithm $k$ times:
if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; ... remove the leftmost occurrence of the letter 'z' and stop the algorithm.
This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly $k$ times, thus removing exactly $k$ characters.
Help Polycarp find the resulting string.
-----Input-----
The first line of input contains two integers $n$ and $k$ ($1 \le k \le n \le 4 \cdot 10^5$) β the length of the string and the number of letters Polycarp will remove.
The second line contains the string $s$ consisting of $n$ lowercase Latin letters.
-----Output-----
Print the string that will be obtained from $s$ after Polycarp removes exactly $k$ letters using the above algorithm $k$ times.
If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).
-----Examples-----
Input
15 3
cccaabababaccbc
Output
cccbbabaccbc
Input
15 9
cccaabababaccbc
Output
cccccc
Input
1 1
u
Output
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles.
There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$.
The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.
-----Output-----
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
-----Examples-----
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a three-digit positive integer N.
Determine whether N is a palindromic number.
Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.
-----Constraints-----
- 100β€Nβ€999
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If N is a palindromic number, print Yes; otherwise, print No.
-----Sample Input-----
575
-----Sample Output-----
Yes
N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Alice and Bob are playing One Card Poker.
One Card Poker is a two-player game using playing cards.
Each card in this game shows an integer between 1 and 13, inclusive.
The strength of a card is determined by the number written on it, as follows:
Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong
One Card Poker is played as follows:
- Each player picks one card from the deck. The chosen card becomes the player's hand.
- The players reveal their hands to each other. The player with the stronger card wins the game.
If their cards are equally strong, the game is drawn.
You are watching Alice and Bob playing the game, and can see their hands.
The number written on Alice's card is A, and the number written on Bob's card is B.
Write a program to determine the outcome of the game.
-----Constraints-----
- 1β¦Aβ¦13
- 1β¦Bβ¦13
- A and B are integers.
-----Input-----
The input is given from Standard Input in the following format:
A B
-----Output-----
Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.
-----Sample Input-----
8 6
-----Sample Output-----
Alice
8 is written on Alice's card, and 6 is written on Bob's card.
Alice has the stronger card, and thus the output should be Alice.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
As a New Year's gift, Dolphin received a string s of length 19.
The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].
Dolphin wants to convert the comma-separated string s into a space-separated string.
Write a program to perform the conversion for him.
-----Constraints-----
- The length of s is 19.
- The sixth and fourteenth characters in s are ,.
- The other characters in s are lowercase English letters.
-----Input-----
The input is given from Standard Input in the following format:
s
-----Output-----
Print the string after the conversion.
-----Sample Input-----
happy,newyear,enjoy
-----Sample Output-----
happy newyear enjoy
Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N students and M checkpoints on the xy-plane.
The coordinates of the i-th student (1 \leq i \leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \leq j \leq M) is (c_j,d_j).
When the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.
The Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.
Here, |x| denotes the absolute value of x.
If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.
Which checkpoint will each student go to?
-----Constraints-----
- 1 \leq N,M \leq 50
- -10^8 \leq a_i,b_i,c_j,d_j \leq 10^8
- All input values are integers.
-----Input-----
The input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_N b_N
c_1 d_1
:
c_M d_M
-----Output-----
Print N lines.
The i-th line (1 \leq i \leq N) should contain the index of the checkpoint for the i-th student to go.
-----Sample Input-----
2 2
2 0
0 0
-1 0
1 0
-----Sample Output-----
2
1
The Manhattan distance between the first student and each checkpoint is:
- For checkpoint 1: |2-(-1)|+|0-0|=3
- For checkpoint 2: |2-1|+|0-0|=1
The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.
The Manhattan distance between the second student and each checkpoint is:
- For checkpoint 1: |0-(-1)|+|0-0|=1
- For checkpoint 2: |0-1|+|0-0|=1
When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given an integer a as input, print the value a + a^2 + a^3.
-----Constraints-----
- 1 \leq a \leq 10
- a is an integer.
-----Input-----
Input is given from Standard Input in the following format:
a
-----Output-----
Print the value a + a^2 + a^3 as an integer.
-----Sample Input-----
2
-----Sample Output-----
14
When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.
Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.
A pixel is the smallest element of an image, and in this problem it is a square of size 1Γ1.
Also, the given images are binary images, and the color of each pixel is either white or black.
In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.
The image A is given as N strings A_1,...,A_N.
The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1β¦i,jβ¦N).
Similarly, the template image B is given as M strings B_1,...,B_M.
The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1β¦i,jβ¦M).
Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.
-----Constraints-----
- 1β¦Mβ¦Nβ¦50
- A_i is a string of length N consisting of # and ..
- B_i is a string of length M consisting of # and ..
-----Input-----
The input is given from Standard Input in the following format:
N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M
-----Output-----
Print Yes if the template image B is contained in the image A. Print No otherwise.
-----Sample Input-----
3 2
#.#
.#.
#.#
#.
.#
-----Sample Output-----
Yes
The template image B is identical to the upper-left 2 Γ 2 subimage and the lower-right 2 Γ 2 subimage of A. Thus, the output should be Yes.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In some other world, today is the day before Christmas Eve.
Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).
He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?
-----Constraints-----
- 2 \leq N \leq 10
- 100 \leq p_i \leq 10000
- p_i is an even number.
-----Input-----
Input is given from Standard Input in the following format:
N
p_1
p_2
:
p_N
-----Output-----
Print the total amount Mr. Takaha will pay.
-----Sample Input-----
3
4980
7980
6980
-----Sample Output-----
15950
The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.
Note that outputs such as 15950.0 will be judged as Wrong Answer.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The restaurant AtCoder serves the following five dishes:
- ABC Don (rice bowl): takes A minutes to serve.
- ARC Curry: takes B minutes to serve.
- AGC Pasta: takes C minutes to serve.
- APC Ramen: takes D minutes to serve.
- ATC Hanbagu (hamburger patty): takes E minutes to serve.
Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.
This restaurant has the following rules on orders:
- An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).
- Only one dish can be ordered at a time.
- No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.
E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.
Here, he can order the dishes in any order he likes, and he can place an order already at time 0.
-----Constraints-----
- A, B, C, D and E are integers between 1 and 123 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
A
B
C
D
E
-----Output-----
Print the earliest possible time for the last dish to be delivered, as an integer.
-----Sample Input-----
29
20
7
35
120
-----Sample Output-----
215
If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:
- Order ABC Don at time 0, which will be delivered at time 29.
- Order ARC Curry at time 30, which will be delivered at time 50.
- Order AGC Pasta at time 50, which will be delivered at time 57.
- Order ATC Hanbagu at time 60, which will be delivered at time 180.
- Order APC Ramen at time 180, which will be delivered at time 215.
There is no way to order the dishes in which the last dish will be delivered earlier than this.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given is a sequence of N integers A_1, \ldots, A_N.
Find the (multiplicative) inverse of the sum of the inverses of these numbers, \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.
-----Constraints-----
- 1 \leq N \leq 100
- 1 \leq A_i \leq 1000
-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
-----Output-----
Print a decimal number (or an integer) representing the value of \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.
Your output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.
-----Sample Input-----
2
10 30
-----Sample Output-----
7.5
\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5.
Printing 7.50001, 7.49999, and so on will also be accepted.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In some other world, today is Christmas Eve.
There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.
He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.
More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?
-----Constraints-----
- 2 \leq K < N \leq 10^5
- 1 \leq h_i \leq 10^9
- h_i is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N K
h_1
h_2
:
h_N
-----Output-----
Print the minimum possible value of h_{max} - h_{min}.
-----Sample Input-----
5 3
10
15
11
14
12
-----Sample Output-----
2
If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.
-----Constraints-----
- \alpha is an uppercase (A - Z) or lowercase (a - z) English letter.
-----Input-----
Input is given from Standard Input in the following format:
Ξ±
-----Output-----
If \alpha is uppercase, print A; if it is lowercase, print a.
-----Sample Input-----
B
-----Sample Output-----
A
B is uppercase, so we should print A.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You have a digit sequence S of length 4. You are wondering which of the following formats S is in:
- YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order
- MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order
If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.
-----Constraints-----
- S is a digit sequence of length 4.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.
-----Sample Input-----
1905
-----Sample Output-----
YYMM
May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.
-----Constraints-----
- 2\leq K\leq 100
- K is an integer.
-----Input-----
Input is given from Standard Input in the following format:
K
-----Output-----
Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).
-----Sample Input-----
3
-----Sample Output-----
2
Two pairs can be chosen: (2,1) and (2,3).
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.
In this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as "21 o'clock".
The current time is A o'clock, and a contest will begin in exactly B hours.
When will the contest begin? Answer in 24-hour time.
-----Constraints-----
- 0 \leq A,B \leq 23
- A and B are integers.
-----Input-----
The input is given from Standard Input in the following format:
A B
-----Output-----
Print the hour of the starting time of the contest in 24-hour time.
-----Sample Input-----
9 12
-----Sample Output-----
21
In this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi is a member of a programming competition site, ButCoder.
Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.
The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.
Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 100
- 0 \leq R \leq 4111
-----Input-----
Input is given from Standard Input in the following format:
N R
-----Output-----
Print his Inner Rating.
-----Sample Input-----
2 2919
-----Sample Output-----
3719
Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.
Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given is an integer N. Find the number of digits that N has in base K.
-----Notes-----
For information on base-K representation, see Positional notation - Wikipedia.
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 10^9
- 2 \leq K \leq 10
-----Input-----
Input is given from Standard Input in the following format:
N K
-----Output-----
Print the number of digits that N has in base K.
-----Sample Input-----
11 2
-----Sample Output-----
4
In binary, 11 is represented as 1011.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.
At least how many sheets of paper does he need?
-----Constraints-----
- N is an integer.
- 1 \leq N \leq 100
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the answer.
-----Sample Input-----
5
-----Sample Output-----
3
By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
E869120's and square1001's 16-th birthday is coming soon.
Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.
E869120 and square1001 were just about to eat A and B of those pieces, respectively,
when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".
Can both of them obey the instruction in the note and take desired numbers of pieces of cake?
-----Constraints-----
- A and B are integers between 1 and 16 (inclusive).
- A+B is at most 16.
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.
-----Sample Input-----
5 4
-----Sample Output-----
Yay!
Both of them can take desired number of pieces as follows:
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?
-----Constraints-----
- S is a string of length between 4 and 10 (inclusive).
- Each character in S is 1, 2, ..., or 9.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the minimum possible difference between X and 753.
-----Sample Input-----
1234567876
-----Sample Output-----
34
Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.
Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.
We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There were $n$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $x$ swords of each type. $y$ people have broken into the theater basement and each of them has taken exactly $z$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $x, y$ and $z$ are unknown for you.
The next morning the director of the theater discovers the loss. He counts all swords β exactly $a_i$ swords of the $i$-th type are left untouched.
The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken.
For example, if $n=3$, $a = [3, 12, 6]$ then one of the possible situations is $x=12$, $y=5$ and $z=3$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $x, y$ and $z$ beforehand but know values of $n$ and $a$.
Thus he seeks for your help. Determine the minimum number of people $y$, which could have broken into the theater basement, and the number of swords $z$ each of them has taken.
-----Input-----
The first line of the input contains one integer $n$ $(2 \le n \le 2 \cdot 10^{5})$ β the number of types of swords.
The second line of the input contains the sequence $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^{9})$, where $a_i$ equals to the number of swords of the $i$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $(j, k)$ that $a_j \neq a_k$.
-----Output-----
Print two integers $y$ and $z$ β the minimum number of people which could have broken into the basement and the number of swords each of them has taken.
-----Examples-----
Input
3
3 12 6
Output
5 3
Input
2
2 9
Output
1 7
Input
7
2 1000000000 4 6 8 4 2
Output
2999999987 2
Input
6
13 52 0 13 26 52
Output
12 13
-----Note-----
In the first example the minimum value of $y$ equals to $5$, i.e. the minimum number of people who could have broken into the basement, is $5$. Each of them has taken $3$ swords: three of them have taken $3$ swords of the first type, and two others have taken $3$ swords of the third type.
In the second example the minimum value of $y$ is $1$, i.e. the minimum number of people who could have broken into the basement, equals to $1$. He has taken $7$ swords of the first type.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a forest β an undirected graph with $n$ vertices such that each its connected component is a tree.
The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.
You task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible.
If there are multiple correct answers, print any of them.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n \le 1000$, $0 \le m \le n - 1$) β the number of vertices of the graph and the number of edges, respectively.
Each of the next $m$ lines contains two integers $v$ and $u$ ($1 \le v, u \le n$, $v \ne u$) β the descriptions of the edges.
It is guaranteed that the given graph is a forest.
-----Output-----
In the first line print the diameter of the resulting tree.
Each of the next $(n - 1) - m$ lines should contain two integers $v$ and $u$ ($1 \le v, u \le n$, $v \ne u$) β the descriptions of the added edges.
The resulting graph should be a tree and its diameter should be minimal possible.
For $m = n - 1$ no edges are added, thus the output consists of a single integer β diameter of the given tree.
If there are multiple correct answers, print any of them.
-----Examples-----
Input
4 2
1 2
2 3
Output
2
4 2
Input
2 0
Output
1
1 2
Input
3 2
1 3
2 3
Output
2
-----Note-----
In the first example adding edges (1, 4) or (3, 4) will lead to a total diameter of 3. Adding edge (2, 4), however, will make it 2.
Edge (1, 2) is the only option you have for the second example. The diameter is 1.
You can't add any edges in the third example. The diameter is already 2.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are $n$ dormitories in Berland State University, they are numbered with integers from $1$ to $n$. Each dormitory consists of rooms, there are $a_i$ rooms in $i$-th dormitory. The rooms in $i$-th dormitory are numbered from $1$ to $a_i$.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $n$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $1$ to $a_1 + a_2 + \dots + a_n$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case $n=2$, $a_1=3$ and $a_2=5$ an envelope can have any integer from $1$ to $8$ written on it. If the number $7$ is written on an envelope, it means that the letter should be delivered to the room number $4$ of the second dormitory.
For each of $m$ letters by the room number among all $n$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
-----Input-----
The first line contains two integers $n$ and $m$ $(1 \le n, m \le 2 \cdot 10^{5})$ β the number of dormitories and the number of letters.
The second line contains a sequence $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^{10})$, where $a_i$ equals to the number of rooms in the $i$-th dormitory. The third line contains a sequence $b_1, b_2, \dots, b_m$ $(1 \le b_j \le a_1 + a_2 + \dots + a_n)$, where $b_j$ equals to the room number (among all rooms of all dormitories) for the $j$-th letter. All $b_j$ are given in increasing order.
-----Output-----
Print $m$ lines. For each letter print two integers $f$ and $k$ β the dormitory number $f$ $(1 \le f \le n)$ and the room number $k$ in this dormitory $(1 \le k \le a_f)$ to deliver the letter.
-----Examples-----
Input
3 6
10 15 12
1 9 12 23 26 37
Output
1 1
1 9
2 2
2 13
3 1
3 12
Input
2 3
5 10000000000
5 6 9999999999
Output
1 5
2 1
2 9999999994
-----Note-----
In the first example letters should be delivered in the following order: the first letter in room $1$ of the first dormitory the second letter in room $9$ of the first dormitory the third letter in room $2$ of the second dormitory the fourth letter in room $13$ of the second dormitory the fifth letter in room $1$ of the third dormitory the sixth letter in room $12$ of the third dormitory
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp has guessed three positive integers $a$, $b$ and $c$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order β their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $a+b$, $a+c$, $b+c$ and $a+b+c$.
You have to guess three numbers $a$, $b$ and $c$ using given numbers. Print three guessed integers in any order.
Pay attention that some given numbers $a$, $b$ and $c$ can be equal (it is also possible that $a=b=c$).
-----Input-----
The only line of the input contains four positive integers $x_1, x_2, x_3, x_4$ ($2 \le x_i \le 10^9$) β numbers written on a board in random order. It is guaranteed that the answer exists for the given number $x_1, x_2, x_3, x_4$.
-----Output-----
Print such positive integers $a$, $b$ and $c$ that four numbers written on a board are values $a+b$, $a+c$, $b+c$ and $a+b+c$ written in some order. Print $a$, $b$ and $c$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.
-----Examples-----
Input
3 6 5 4
Output
2 1 3
Input
40 40 40 60
Output
20 20 20
Input
201 101 101 200
Output
1 100 100
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an integer array of length $n$.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $[x, x + 1, \dots, x + k - 1]$ for some value $x$ and length $k$.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $[5, 3, 1, 2, 4]$ the following arrays are subsequences: $[3]$, $[5, 3, 1, 2, 4]$, $[5, 1, 4]$, but the array $[1, 3]$ is not.
-----Input-----
The first line of the input containing integer number $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array. The second line of the input containing $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the array itself.
-----Output-----
On the first line print $k$ β the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
-----Examples-----
Input
7
3 3 4 7 5 6 8
Output
4
2 3 5 6
Input
6
1 3 5 2 4 6
Output
2
1 4
Input
4
10 9 8 7
Output
1
1
Input
9
6 7 8 3 4 5 9 10 11
Output
6
1 2 3 7 8 9
-----Note-----
All valid answers for the first example (as sequences of indices): $[1, 3, 5, 6]$ $[2, 3, 5, 6]$
All valid answers for the second example: $[1, 4]$ $[2, 5]$ $[3, 6]$
All valid answers for the third example: $[1]$ $[2]$ $[3]$ $[4]$
All valid answers for the fourth example: $[1, 2, 3, 7, 8, 9]$
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given integers A and B, each between 1 and 3 (inclusive).
Determine if there is an integer C between 1 and 3 (inclusive) such that A \times B \times C is an odd number.
-----Constraints-----
- All values in input are integers.
- 1 \leq A, B \leq 3
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.
-----Sample Input-----
3 1
-----Sample Output-----
Yes
Let C = 3. Then, A \times B \times C = 3 \times 1 \times 3 = 9, which is an odd number.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a train going from Station A to Station B that costs X yen (the currency of Japan).
Also, there is a bus going from Station B to Station C that costs Y yen.
Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.
How much does it cost to travel from Station A to Station C if she uses this ticket?
-----Constraints-----
- 1 \leq X,Y \leq 100
- Y is an even number.
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
X Y
-----Output-----
If it costs x yen to travel from Station A to Station C, print x.
-----Sample Input-----
81 58
-----Sample Output-----
110
- The train fare is 81 yen.
- The train fare is 58 β 2=29 yen with the 50% discount.
Thus, it costs 110 yen to travel from Station A to Station C.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi is going to set a 3-character password.
How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?
-----Constraints-----
- 1 \leq N \leq 9
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the number of possible passwords.
-----Sample Input-----
2
-----Sample Output-----
8
There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.
Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?
-----Constraints-----
- 1 β€ X β€ 9
- X is an integer.
-----Input-----
Input is given from Standard Input in the following format:
X
-----Output-----
If Takahashi's growth will be celebrated, print YES; if it will not, print NO.
-----Sample Input-----
5
-----Sample Output-----
YES
The growth of a five-year-old child will be celebrated.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Decades have passed since the beginning of AtCoder Beginner Contest.
The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?
In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.
You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.
-----Constraints-----
- 1 \leq N \leq 1998
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.
-----Sample Input-----
999
-----Sample Output-----
ABC
The 999-th round of AtCoder Beginner Contest is labeled as ABC999.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.
Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.
Determine if there exists a pair of antennas that cannot communicate directly.
Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.
-----Constraints-----
- a, b, c, d, e and k are integers between 0 and 123 (inclusive).
- a < b < c < d < e
-----Input-----
Input is given from Standard Input in the following format:
a
b
c
d
e
k
-----Output-----
Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.
-----Sample Input-----
1
2
4
8
9
15
-----Sample Output-----
Yay!
In this case, there is no pair of antennas that cannot communicate directly, because:
- the distance between A and B is 2 - 1 = 1
- the distance between A and C is 4 - 1 = 3
- the distance between A and D is 8 - 1 = 7
- the distance between A and E is 9 - 1 = 8
- the distance between B and C is 4 - 2 = 2
- the distance between B and D is 8 - 2 = 6
- the distance between B and E is 9 - 2 = 7
- the distance between C and D is 8 - 4 = 4
- the distance between C and E is 9 - 4 = 5
- the distance between D and E is 9 - 8 = 1
and none of them is greater than 15. Thus, the correct output is Yay!.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You will be given an integer a and a string s consisting of lowercase English letters as input.
Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.
-----Constraints-----
- 2800 \leq a < 5000
- s is a string of length between 1 and 10 (inclusive).
- Each character of s is a lowercase English letter.
-----Input-----
Input is given from Standard Input in the following format:
a
s
-----Output-----
If a is not less than 3200, print s; if a is less than 3200, print red.
-----Sample Input-----
3200
pink
-----Sample Output-----
pink
a = 3200 is not less than 3200, so we print s = pink.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A programming competition site AtCode regularly holds programming contests.
The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.
The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.
The contest after the ARC is called AGC, which is rated for all contestants.
Takahashi's rating on AtCode is R. What is the next contest rated for him?
-----Constraints-----
- 0 β€ R β€ 4208
- R is an integer.
-----Input-----
Input is given from Standard Input in the following format:
R
-----Output-----
Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).
-----Sample Input-----
1199
-----Sample Output-----
ABC
1199 is less than 1200, so ABC will be rated.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.
You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.
-----Constraints-----
- 111 \leq n \leq 999
- n is an integer consisting of digits 1 and 9.
-----Input-----
Input is given from Standard Input in the following format:
n
-----Output-----
Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.
-----Sample Input-----
119
-----Sample Output-----
991
Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string $a$ consisting of lowercase English letters. The string $a$ has a length of $2$ or more characters. Then, from string $a$ he builds a new string $b$ and offers Alice the string $b$ so that she can guess the string $a$.
Bob builds $b$ from $a$ as follows: he writes all the substrings of length $2$ of the string $a$ in the order from left to right, and then joins them in the same order into the string $b$.
For example, if Bob came up with the string $a$="abac", then all the substrings of length $2$ of the string $a$ are: "ab", "ba", "ac". Therefore, the string $b$="abbaac".
You are given the string $b$. Help Alice to guess the string $a$ that Bob came up with. It is guaranteed that $b$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
-----Input-----
The first line contains a single positive integer $t$ ($1 \le t \le 1000$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case consists of one line in which the string $b$ is written, consisting of lowercase English letters ($2 \le |b| \le 100$)Β β the string Bob came up with, where $|b|$ is the length of the string $b$. It is guaranteed that $b$ was built according to the algorithm given above.
-----Output-----
Output $t$ answers to test cases. Each answer is the secret string $a$, consisting of lowercase English letters, that Bob came up with.
-----Example-----
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
-----Note-----
The first test case is explained in the statement.
In the second test case, Bob came up with the string $a$="ac", the string $a$ has a length $2$, so the string $b$ is equal to the string $a$.
In the third test case, Bob came up with the string $a$="bcdaf", substrings of length $2$ of string $a$ are: "bc", "cd", "da", "af", so the string $b$="bccddaaf".
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.
Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:
Suppose we want to analyze the segment of $n$ consecutive days. We have measured the temperatures during these $n$ days; the temperature during $i$-th day equals $a_i$.
We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $x$ to day $y$, we calculate it as $\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $k$ consecutive days. For example, if analyzing the measures $[3, 4, 1, 2]$ and $k = 3$, we are interested in segments $[3, 4, 1]$, $[4, 1, 2]$ and $[3, 4, 1, 2]$ (we want to find the maximum value of average temperature over these segments).
You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) β the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 5000$) β the temperature measures during given $n$ days.
-----Output-----
Print one real number β the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days.
Your answer will be considered correct if the following condition holds: $|res - res_0| < 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the jury's solution.
-----Example-----
Input
4 3
3 4 1 2
Output
2.666666666666667
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string $s=s_{1}s_{2} \dots s_{m}$ ($1 \le m \le 10$), Polycarp uses the following algorithm:
he writes down $s_1$ ones, he writes down $s_2$ twice, he writes down $s_3$ three times, ... he writes down $s_m$ $m$ times.
For example, if $s$="bab" the process is: "b" $\to$ "baa" $\to$ "baabbb". So the encrypted $s$="bab" is "baabbb".
Given string $t$ β the result of encryption of some string $s$. Your task is to decrypt it, i. e. find the string $s$.
-----Input-----
The first line contains integer $n$ ($1 \le n \le 55$) β the length of the encrypted string. The second line of the input contains $t$ β the result of encryption of some string $s$. It contains only lowercase Latin letters. The length of $t$ is exactly $n$.
It is guaranteed that the answer to the test exists.
-----Output-----
Print such string $s$ that after encryption it equals $t$.
-----Examples-----
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an undirected weighted connected graph with $n$ vertices and $m$ edges without loops and multiple edges.
The $i$-th edge is $e_i = (u_i, v_i, w_i)$; the distance between vertices $u_i$ and $v_i$ along the edge $e_i$ is $w_i$ ($1 \le w_i$). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.
A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).
You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $1$. You can increase the weight of each edge multiple (possibly, zero) times.
Suppose that the initial MST cost is $k$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $k$, but MST is unique (it means that there is only one way to choose MST in the obtained graph).
Your problem is to calculate the minimum number of operations required to do it.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, n - 1 \le m \le 2 \cdot 10^5$) β the number of vertices and the number of edges in the initial graph.
The next $m$ lines contain three integers each. The $i$-th line contains the description of the $i$-th edge $e_i$. It is denoted by three integers $u_i, v_i$ and $w_i$ ($1 \le u_i, v_i \le n, u_i \ne v_i, 1 \le w \le 10^9$), where $u_i$ and $v_i$ are vertices connected by the $i$-th edge and $w_i$ is the weight of this edge.
It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $i$ from $1$ to $m$ $u_i \ne v_i$ and for each unordered pair of vertices $(u, v)$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.
-----Output-----
Print one integer β the minimum number of operations to unify MST of the initial graph without changing the cost of MST.
-----Examples-----
Input
8 10
1 2 1
2 3 2
2 4 5
1 4 2
6 3 3
6 1 3
3 5 2
3 7 1
4 8 1
6 2 4
Output
1
Input
4 3
2 1 3
4 3 4
2 4 1
Output
0
Input
3 3
1 2 1
2 3 2
1 3 3
Output
0
Input
3 3
1 2 1
2 3 3
1 3 3
Output
1
Input
1 0
Output
0
Input
5 6
1 2 2
2 3 1
4 5 3
2 4 2
1 4 2
1 5 3
Output
2
-----Note-----
The picture corresponding to the first example: [Image]
You can, for example, increase weight of the edge $(1, 6)$ or $(6, 3)$ by $1$ to unify MST.
The picture corresponding to the last example: $\$ 8$
You can, for example, increase weights of edges $(1, 5)$ and $(2, 4)$ by $1$ to unify MST.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $t$, then for each $i, j$ such that $t_i = t_j$ should be satisfied $|i-j|~ mod~ 3 = 0$. The value $|x|$ means absolute value of $x$, the operation $x~ mod~ y$ means remainder of $x$ when divided by $y$.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a nice garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
3
BRB
Output
1
GRB
Input
7
RGBGRBB
Output
3
RGBRGBR
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi received otoshidama (New Year's money gifts) from N of his relatives.
You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.
For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.
If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?
-----Constraints-----
- 2 \leq N \leq 10
- u_i = JPY or BTC.
- If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.
- If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.
-----Input-----
Input is given from Standard Input in the following format:
N
x_1 u_1
x_2 u_2
:
x_N u_N
-----Output-----
If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).
Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.
-----Sample Input-----
2
10000 JPY
0.10000000 BTC
-----Sample Output-----
48000.0
The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.
Outputs such as 48000 and 48000.1 will also be judged correct.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is an N-car train.
You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."
-----Constraints-----
- 1 \leq N \leq 100
- 1 \leq i \leq N
-----Input-----
Input is given from Standard Input in the following format:
N i
-----Output-----
Print the answer.
-----Sample Input-----
4 2
-----Sample Output-----
3
The second car from the front of a 4-car train is the third car from the back.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given are strings s and t of length N each, both consisting of lowercase English letters.
Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.
-----Constraints-----
- 1 \leq N \leq 100
- |S| = |T| = N
- S and T are strings consisting of lowercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
N
S T
-----Output-----
Print the string formed.
-----Sample Input-----
2
ip cc
-----Sample Output-----
icpc
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In AtCoder City, there are three stations numbered 1, 2, and 3.
Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.
To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.
Determine if there is a pair of stations that will be connected by a bus service.
-----Constraints-----
- Each character of S is A or B.
- |S| = 3
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.
-----Sample Input-----
ABA
-----Sample Output-----
Yes
Company A operates Station 1 and 3, while Company B operates Station 2.
There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.
Find the maximum number of consecutive rainy days in this period.
-----Constraints-----
- |S| = 3
- Each character of S is S or R.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the maximum number of consecutive rainy days in the period.
-----Sample Input-----
RRS
-----Sample Output-----
2
We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi, who is A years old, is riding a Ferris wheel.
It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)
Find the cost of the Ferris wheel for Takahashi.
-----Constraints-----
- 0 β€ A β€ 100
- 2 β€ B β€ 1000
- B is an even number.
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
Print the cost of the Ferris wheel for Takahashi.
-----Sample Input-----
30 100
-----Sample Output-----
100
Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is always an integer in Takahashi's mind.
Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.
The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.
Find the integer in Takahashi's mind after he eats all the symbols.
-----Constraints-----
- The length of S is 4.
- Each character in S is + or -.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the integer in Takahashi's mind after he eats all the symbols.
-----Sample Input-----
+-++
-----Sample Output-----
2
- Initially, the integer in Takahashi's mind is 0.
- The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.
- The second integer to eat is -. After eating it, the integer in his mind becomes 0.
- The third integer to eat is +. After eating it, the integer in his mind becomes 1.
- The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.
Thus, the integer in Takahashi's mind after he eats all the symbols is 2.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)
Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.
-----Constraints-----
- S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.
-----Sample Input-----
2019/04/30
-----Sample Output-----
Heisei
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions are constraints on $n$ and $k$.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $k$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $0$).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \le id_i \le 10^9$).
If you receive a message from $id_i$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with $id_i$ on the screen): Firstly, if the number of conversations displayed on the screen is $k$, the last conversation (which has the position $k$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen. The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 200)$ β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains $n$ integers $id_1, id_2, \dots, id_n$ ($1 \le id_i \le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message.
-----Output-----
In the first line of the output print one integer $m$ ($1 \le m \le min(n, k)$) β the number of conversations shown after receiving all $n$ messages.
In the second line print $m$ integers $ids_1, ids_2, \dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages.
-----Examples-----
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
-----Note-----
In the first example the list of conversations will change in the following way (in order from the first to last message): $[]$; $[1]$; $[2, 1]$; $[3, 2]$; $[3, 2]$; $[1, 3]$; $[1, 3]$; $[2, 1]$.
In the second example the list of conversations will change in the following way: $[]$; $[2]$; $[3, 2]$; $[3, 2]$; $[1, 3, 2]$; and then the list will not change till the end.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp has $n$ coins, the value of the $i$-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$).
Polycarp wants to know answers on $q$ queries. The $j$-th query is described as integer number $b_j$. The answer to the query is the minimum number of coins that is necessary to obtain the value $b_j$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $b_j$, the answer to the $j$-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
-----Input-----
The first line of the input contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β the number of coins and the number of queries.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ β values of coins ($1 \le a_i \le 2 \cdot 10^9$). It is guaranteed that all $a_i$ are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$).
The next $q$ lines contain one integer each. The $j$-th line contains one integer $b_j$ β the value of the $j$-th query ($1 \le b_j \le 10^9$).
-----Output-----
Print $q$ integers $ans_j$. The $j$-th integer must be equal to the answer on the $j$-th query. If Polycarp can't obtain the value $b_j$ the answer to the $j$-th query is -1.
-----Example-----
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an array $a$ consisting of $n$ integers. You can perform the following operations arbitrary number of times (possibly, zero):
Choose a pair of indices $(i, j)$ such that $|i-j|=1$ (indices $i$ and $j$ are adjacent) and set $a_i := a_i + |a_i - a_j|$; Choose a pair of indices $(i, j)$ such that $|i-j|=1$ (indices $i$ and $j$ are adjacent) and set $a_i := a_i - |a_i - a_j|$.
The value $|x|$ means the absolute value of $x$. For example, $|4| = 4$, $|-3| = 3$.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed $10^{18}$ by absolute value.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of elements in $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.
-----Output-----
In the first line print one integer $k$ β the minimum number of operations required to obtain the array of equal elements.
In the next $k$ lines print operations itself. The $p$-th operation should be printed as a triple of integers $(t_p, i_p, j_p)$, where $t_p$ is either $1$ or $2$ ($1$ means that you perform the operation of the first type, and $2$ means that you perform the operation of the second type), and $i_p$ and $j_p$ are indices of adjacent elements of the array such that $1 \le i_p, j_p \le n$, $|i_p - j_p| = 1$. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed $10^{18}$ by absolute value.
If there are many possible answers, you can print any.
-----Examples-----
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp and his friends want to visit a new restaurant. The restaurant has $n$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $1$ to $n$ in the order from left to right. The state of the restaurant is described by a string of length $n$ which contains characters "1" (the table is occupied) and "0" (the table is empty).
Restaurant rules prohibit people to sit at a distance of $k$ or less from each other. That is, if a person sits at the table number $i$, then all tables with numbers from $i-k$ to $i+k$ (except for the $i$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $k$.
For example, if $n=8$ and $k=2$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $k=2$.
In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.
You are given a binary string $s$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $s$.
Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?
For example, if $n=6$, $k=1$, $s=$Β "100010", then the answer to the problem will be $1$, since only the table at position $3$ can be occupied such that the rules are still satisfied.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing two integers $n$ and $k$ ($1 \le k \le n \le 2\cdot 10^5$)Β β the number of tables in the restaurant and the minimum allowed distance between two people.
The second line of each test case contains a binary string $s$ of length $n$ consisting of "0" and "1"Β β a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurantΒ β the difference between indices of any two "1" is more than $k$.
The sum of $n$ for all test cases in one test does not exceed $2\cdot 10^5$.
-----Output-----
For each test case output one integerΒ β the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $0$.
-----Example-----
Input
6
6 1
100010
6 2
000000
5 1
10101
3 1
001
2 2
00
1 1
0
Output
1
2
0
1
1
1
-----Note-----
The first test case is explained in the statement.
In the second test case, the answer is $2$, since you can choose the first and the sixth table.
In the third test case, you cannot take any free table without violating the rules of the restaurant.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is constraints.
You are given $n$ segments on the coordinate axis $OX$. Segments can intersect, lie inside each other and even coincide. The $i$-th segment is $[l_i; r_i]$ ($l_i \le r_i$) and it covers all integer points $j$ such that $l_i \le j \le r_i$.
The integer point is called bad if it is covered by strictly more than $k$ segments.
Your task is to remove the minimum number of segments so that there are no bad points at all.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β the number of segments and the maximum number of segments by which each integer point can be covered.
The next $n$ lines contain segments. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 2 \cdot 10^5$) β the endpoints of the $i$-th segment.
-----Output-----
In the first line print one integer $m$ ($0 \le m \le n$) β the minimum number of segments you need to remove so that there are no bad points.
In the second line print $m$ distinct integers $p_1, p_2, \dots, p_m$ ($1 \le p_i \le n$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them.
-----Examples-----
Input
7 2
11 11
9 11
7 8
8 9
7 8
9 11
7 9
Output
3
4 6 7
Input
5 1
29 30
30 30
29 29
28 30
30 30
Output
3
1 4 5
Input
6 1
2 3
3 3
2 3
2 2
2 3
2 3
Output
4
1 3 5 6
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer $q$ independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β the number of queries.
The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of problems.
The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.
-----Output-----
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
-----Example-----
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence $a$. After that sequence $a$ got shuffled. For example, some of the possible resulting sequences $a$ for an increasing sequence $[1, 3, 4]$ and a decreasing sequence $[10, 4, 2]$ are sequences $[1, 2, 3, 4, 4, 10]$ or $[4, 2, 1, 10, 4, 3]$.
This shuffled sequence $a$ is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence $a$ to increasing and decreasing sequences, print "NO".
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of elements in $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.
-----Output-----
If there is a contradiction in the input and it is impossible to split the given sequence $a$ to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print $n_i$ β the number of elements in the strictly increasing sequence. $n_i$ can be zero, in this case the increasing sequence is empty.
In the third line print $n_i$ integers $inc_1, inc_2, \dots, inc_{n_i}$ in the increasing order of its values ($inc_1 < inc_2 < \dots < inc_{n_i}$) β the strictly increasing sequence itself. You can keep this line empty if $n_i = 0$ (or just print the empty line).
In the fourth line print $n_d$ β the number of elements in the strictly decreasing sequence. $n_d$ can be zero, in this case the decreasing sequence is empty.
In the fifth line print $n_d$ integers $dec_1, dec_2, \dots, dec_{n_d}$ in the decreasing order of its values ($dec_1 > dec_2 > \dots > dec_{n_d}$) β the strictly decreasing sequence itself. You can keep this line empty if $n_d = 0$ (or just print the empty line).
$n_i + n_d$ should be equal to $n$ and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
-----Examples-----
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are $n$ products in the shop. The price of the $i$-th product is $a_i$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.
In fact, the owner of the shop can change the price of some product $i$ in such a way that the difference between the old price of this product $a_i$ and the new price $b_i$ is at most $k$. In other words, the condition $|a_i - b_i| \le k$ should be satisfied ($|x|$ is the absolute value of $x$).
He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price $b_i$ of each product $i$ should be positive (i.e. $b_i > 0$ should be satisfied for all $i$ from $1$ to $n$).
Your task is to find out the maximum possible equal price $B$ of all productts with the restriction that for all products the condiion $|a_i - B| \le k$ should be satisfied (where $a_i$ is the old price of the product and $B$ is the same new price of all products) or report that it is impossible to find such price $B$.
Note that the chosen price $B$ should be integer.
You should answer $q$ independent queries.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 100$) β the number of queries. Each query is presented by two lines.
The first line of the query contains two integers $n$ and $k$ ($1 \le n \le 100, 1 \le k \le 10^8$) β the number of products and the value $k$. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^8$), where $a_i$ is the price of the $i$-th product.
-----Output-----
Print $q$ integers, where the $i$-th integer is the answer $B$ on the $i$-th query.
If it is impossible to equalize prices of all given products with restriction that for all products the condition $|a_i - B| \le k$ should be satisfied (where $a_i$ is the old price of the product and $B$ is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.
-----Example-----
Input
4
5 1
1 1 2 3 1
4 2
6 4 8 5
2 2
1 6
3 5
5 2 5
Output
2
6
-1
7
-----Note-----
In the first example query you can choose the price $B=2$. It is easy to see that the difference between each old price and each new price $B=2$ is no more than $1$.
In the second example query you can choose the price $B=6$ and then all the differences between old and new price $B=6$ will be no more than $2$.
In the third example query you cannot choose any suitable price $B$. For any value $B$ at least one condition out of two will be violated: $|1-B| \le 2$, $|6-B| \le 2$.
In the fourth example query all values $B$ between $1$ and $7$ are valid. But the maximum is $7$, so it's the answer.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an array $a$ consisting of $n$ integers. Each $a_i$ is one of the six following numbers: $4, 8, 15, 16, 23, 42$.
Your task is to remove the minimum number of elements to make this array good.
An array of length $k$ is called good if $k$ is divisible by $6$ and it is possible to split it into $\frac{k}{6}$ subsequences $4, 8, 15, 16, 23, 42$.
Examples of good arrays: $[4, 8, 15, 16, 23, 42]$ (the whole array is a required sequence); $[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements); $[]$ (the empty array is good).
Examples of bad arrays: $[4, 8, 15, 16, 42, 23]$ (the order of elements should be exactly $4, 8, 15, 16, 23, 42$); $[4, 8, 15, 16, 23, 42, 4]$ (the length of the array is not divisible by $6$); $[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence).
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) β the number of elements in $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ (each $a_i$ is one of the following numbers: $4, 8, 15, 16, 23, 42$), where $a_i$ is the $i$-th element of $a$.
-----Output-----
Print one integer β the minimum number of elements you have to remove to obtain a good array.
-----Examples-----
Input
5
4 8 15 16 23
Output
5
Input
12
4 8 4 15 16 8 23 15 16 42 23 42
Output
0
Input
15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42
Output
3
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given two integers $a$ and $b$.
In one move, you can choose some integer $k$ from $1$ to $10$ and add it to $a$ or subtract it from $a$. In other words, you choose an integer $k \in [1; 10]$ and perform $a := a + k$ or $a := a - k$. You may use different values of $k$ in different moves.
Your task is to find the minimum number of moves required to obtain $b$ from $a$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The only line of the test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$).
-----Output-----
For each test case, print the answer: the minimum number of moves required to obtain $b$ from $a$.
-----Example-----
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
-----Note-----
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: $13 \rightarrow 23 \rightarrow 32 \rightarrow 42$ (add $10$, add $9$, add $10$).
In the third test case of the example, the following sequence of moves can be applied: $18 \rightarrow 10 \rightarrow 4$ (subtract $8$, subtract $6$).
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
International Women's Day is coming soon! Polycarp is preparing for the holiday.
There are $n$ candy boxes in the shop for sale. The $i$-th box contains $d_i$ candies.
Polycarp wants to prepare the maximum number of gifts for $k$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $k$. In other words, two boxes $i$ and $j$ ($i \ne j$) can be combined as a gift if $d_i + d_j$ is divisible by $k$.
How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes "partially" or redistribute candies between them.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5, 1 \le k \le 100$) β the number the boxes and the number the girls.
The second line of the input contains $n$ integers $d_1, d_2, \dots, d_n$ ($1 \le d_i \le 10^9$), where $d_i$ is the number of candies in the $i$-th box.
-----Output-----
Print one integer β the maximum number of the boxes Polycarp can give as gifts.
-----Examples-----
Input
7 2
1 2 2 3 2 4 10
Output
6
Input
8 2
1 2 2 3 2 4 6 10
Output
8
Input
7 3
1 2 2 3 2 4 5
Output
4
-----Note-----
In the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(2, 3)$; $(5, 6)$; $(1, 4)$.
So the answer is $6$.
In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(6, 8)$; $(2, 3)$; $(1, 4)$; $(5, 7)$.
So the answer is $8$.
In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(1, 2)$; $(6, 7)$.
So the answer is $4$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.