question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----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 elements in the array and the number of equal numbers required. 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 $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 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. You are given the array $a$ consisting of $n$ elements and the integer $k \le n$. You want to obtain at least $k$ equal elements in the array $a$. In one move, you can make one of the following two operations: Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $a$ is $mn$ then you choose such index $i$ that $a_i = mn$ and set $a_i := a_i + 1$); take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $a$ is $mx$ then you choose such index $i$ that $a_i = mx$ and set $a_i := a_i - 1$). Your task is to calculate the minimum number of moves required to obtain at least $k$ equal elements in the array. -----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 elements in $a$ and the required number of equal elements. 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 $i$-th element of $a$. -----Output----- Print one integer β€” the minimum number of moves required to obtain at least $k$ equal elements in the array. -----Examples----- Input 6 5 1 2 2 4 2 3 Output 3 Input 7 5 3 3 2 1 1 1 3 Output 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.
Solve the programming task below in a Python markdown code block. Takahashi is practicing shiritori alone again today. Shiritori is a game as follows: - In the first turn, a player announces any one word. - In the subsequent turns, a player announces a word that satisfies the following conditions: - That word is not announced before. - The first character of that word is the same as the last character of the last word announced. In this game, he is practicing to announce as many words as possible in ten seconds. You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions. -----Constraints----- - N is an integer satisfying 2 \leq N \leq 100. - W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: N W_1 W_2 : W_N -----Output----- If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No. -----Sample Input----- 4 hoge english hoge enigma -----Sample Output----- No As hoge is announced multiple times, the rules of shiritori was not observed. 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 organizing a party. At the party, each guest will receive one or more snack pieces. Takahashi predicts that the number of guests at this party will be A or B. Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted. We assume that a piece cannot be divided and distributed to multiple guests. -----Constraints----- - 1 \leq A, B \leq 10^5 - A \neq B - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests. -----Sample Input----- 2 3 -----Sample Output----- 6 When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests. 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 string S. Replace every character in S with x and print the result. -----Constraints----- - S is a string consisting of lowercase English letters. - The length of S is between 1 and 100 (inclusive). -----Input----- Input is given from Standard Input in the following format: S -----Output----- Replace every character in S with x and print the result. -----Sample Input----- sardine -----Sample Output----- xxxxxxx Replacing every character in S with x results in xxxxxxx. 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 will buy a product for N yen (the currency of Japan) at a shop. If we use only 1000-yen bills to pay the price, how much change will we receive? Assume we use the minimum number of bills required. -----Constraints----- - 1 \leq N \leq 10000 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the amount of change as an integer. -----Sample Input----- 1900 -----Sample Output----- 100 We will use two 1000-yen bills to pay the price and receive 100 yen in change. 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. Our world is one-dimensional, and ruled by two empires called Empire A and Empire B. The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y. One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control. If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out. - X < Z \leq Y - x_1, x_2, ..., x_N < Z - y_1, y_2, ..., y_M \geq Z Determine if war will break out. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 100 - -100 \leq X < Y \leq 100 - -100 \leq x_i, y_i \leq 100 - x_1, x_2, ..., x_N \neq X - x_i are all different. - y_1, y_2, ..., y_M \neq Y - y_i are all different. -----Input----- Input is given from Standard Input in the following format: N M X Y x_1 x_2 ... x_N y_1 y_2 ... y_M -----Output----- If war will break out, print War; otherwise, print No War. -----Sample Input----- 3 2 10 20 8 15 13 16 22 -----Sample Output----- No War The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement. - X = 10 < 16 \leq 20 = Y - 8, 15, 13 < 16 - 16, 22 \geq 16 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. Each day in Berland consists of $n$ hours. Polycarp likes time management. That's why he has a fixed schedule for each day β€” it is a sequence $a_1, a_2, \dots, a_n$ (each $a_i$ is either $0$ or $1$), where $a_i=0$ if Polycarp works during the $i$-th hour of the day and $a_i=1$ if Polycarp rests during the $i$-th hour of the day. Days go one after another endlessly and Polycarp uses the same schedule for each day. What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day. -----Input----- The first line contains $n$ ($1 \le n \le 2\cdot10^5$) β€” number of hours per day. The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i=0$ if the $i$-th hour in a day is working and $a_i=1$ if the $i$-th hour is resting. It is guaranteed that $a_i=0$ for at least one $i$. -----Output----- Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day. -----Examples----- Input 5 1 0 1 0 1 Output 2 Input 6 0 1 0 1 1 0 Output 2 Input 7 1 0 1 1 1 0 1 Output 3 Input 3 0 0 0 Output 0 -----Note----- In the first example, the maximal rest starts in last hour and goes to the first hour of the next day. In the second example, Polycarp has maximal rest from the $4$-th to the $5$-th hour. In the third example, Polycarp has maximal rest from the $3$-rd to the $5$-th hour. In the fourth example, Polycarp has no rest at all. 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 Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (Div.2). The scientists rejoiced at the gift and decided to put some important facts to this array. Here are the first few of the facts: The highest mountain above sea level in the world is Mount Everest. Its peak rises to 8848 m. The largest board game tournament consisted of 958 participants playing chapaev. The largest online maths competition consisted of 12766 participants. The Nile is credited as the longest river in the world. From its farthest stream in Burundi, it extends 6695 km in length. While not in flood, the main stretches of the Amazon river in South America can reach widths of up to 1100 km at its widest points. Angel Falls is the highest waterfall. Its greatest single drop measures 807 m. The Hotel Everest View above Namche, Nepal β€” the village closest to Everest base camp – is at a record height of 31962 m Uranium is the heaviest of all the naturally occurring elements. Its most common isotope has a nucleus containing 146 neutrons. The coldest permanently inhabited place is the Siberian village of Oymyakon, where the temperature of -68Β°C was registered in the twentieth century. The longest snake held in captivity is over 25 feet long. Its name is Medusa. Colonel Meow holds the world record for longest fur on a cat β€” almost 134 centimeters. Sea otters can have up to 10000 hairs per square inch. This is the most dense fur in the animal kingdom. The largest state of USA is Alaska; its area is 663268 square miles Alaska has a longer coastline than all of the other 49 U.S. States put together: it is 154103 miles long. Lake Baikal is the largest freshwater lake in the world. It reaches 1642Β meters in depth and contains around one-fifth of the world’s unfrozen fresh water. The most colorful national flag is the one of Turkmenistan, with 106 colors. -----Input----- The input will contain a single integer between 1 and 16. -----Output----- Output a single integer. -----Examples----- Input 1 Output 1 Input 7 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. Not to be confused with chessboard. [Image] -----Input----- The first line of input contains a single integer N (1 ≀ N ≀ 100) β€” the number of cheeses you have. The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct. -----Output----- Output a single number. -----Examples----- Input 9 brie soft camembert soft feta soft goat soft muenster soft asiago hard cheddar hard gouda hard swiss hard Output 3 Input 6 parmesan hard emmental hard edam hard colby hard gruyere hard asiago hard Output 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.
Solve the programming task below in a Python markdown code block. [Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 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. Takahashi is meeting up with Aoki. They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now. Takahashi will leave his house now and go straight to the place at a speed of S meters per minute. Will he arrive in time? -----Constraints----- - 1 \leq D \leq 10000 - 1 \leq T \leq 10000 - 1 \leq S \leq 10000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: D T S -----Output----- If Takahashi will reach the place in time, print Yes; otherwise, print No. -----Sample Input----- 1000 15 80 -----Sample Output----- Yes It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in 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. We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}. The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet. Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal. -----Constraints----- - All values in input are integers. - 1 \leq A_{i, j} \leq 100 - A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2)) - 1 \leq N \leq 10 - 1 \leq b_i \leq 100 - b_i \neq b_j (i \neq j) -----Input----- Input is given from Standard Input in the following format: A_{1, 1} A_{1, 2} A_{1, 3} A_{2, 1} A_{2, 2} A_{2, 3} A_{3, 1} A_{3, 2} A_{3, 3} N b_1 \vdots b_N -----Output----- If we will have a bingo, print Yes; otherwise, print No. -----Sample Input----- 84 97 66 79 89 11 61 59 7 7 89 7 87 79 24 84 30 -----Sample Output----- Yes We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right. 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 N days of summer vacation. His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment. He cannot do multiple assignments on the same day, or hang out on a day he does an assignment. What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation? If Takahashi cannot finish all the assignments during the vacation, print -1 instead. -----Constraints----- - 1 \leq N \leq 10^6 - 1 \leq M \leq 10^4 - 1 \leq A_i \leq 10^4 -----Input----- Input is given from Standard Input in the following format: N M A_1 ... A_M -----Output----- Print the maximum number of days Takahashi can hang out during the vacation, or -1. -----Sample Input----- 41 2 5 6 -----Sample Output----- 30 For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out. 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. Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo. As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times. Find the N-th smallest integer that would make Ringo happy. -----Constraints----- - D is 0, 1 or 2. - N is an integer between 1 and 100 (inclusive). -----Input----- Input is given from Standard Input in the following format: D N -----Output----- Print the N-th smallest integer that can be divided by 100 exactly D times. -----Sample Input----- 0 5 -----Sample Output----- 5 The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ... Thus, the 5-th smallest integer that would make Ringo happy is 5. 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 teacher responsible for a class of N students. The students are given distinct student numbers from 1 to N. Today, all the students entered the classroom at different times. According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i). From these records, reconstruct the order in which the students entered the classroom. -----Constraints----- - 1 \le N \le 10^5 - 1 \le A_i \le N - A_i \neq A_j (i \neq j) - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 \ldots A_N -----Output----- Print the student numbers of the students in the order the students entered the classroom. -----Sample Input----- 3 2 3 1 -----Sample Output----- 3 1 2 First, student number 3 entered the classroom. Then, student number 1 entered the classroom. Finally, student number 2 entered the classroom. 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. N friends of Takahashi has come to a theme park. To ride the most popular roller coaster in the park, you must be at least K centimeters tall. The i-th friend is h_i centimeters tall. How many of the Takahashi's friends can ride the roller coaster? -----Constraints----- - 1 \le N \le 10^5 - 1 \le K \le 500 - 1 \le h_i \le 500 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N K h_1 h_2 \ldots h_N -----Output----- Print the number of people among the Takahashi's friends who can ride the roller coaster. -----Sample Input----- 4 150 150 140 100 200 -----Sample Output----- 2 Two of them can ride the roller coaster: the first and fourth friends. 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 held a popularity poll for N items on sale. Item i received A_i votes. From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes. If M popular items can be selected, print Yes; otherwise, print No. -----Constraints----- - 1 \leq M \leq N \leq 100 - 1 \leq A_i \leq 1000 - A_i are distinct. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N M A_1 ... A_N -----Output----- If M popular items can be selected, print Yes; otherwise, print No. -----Sample Input----- 4 1 5 4 2 1 -----Sample Output----- Yes There were 12 votes in total. The most popular item received 5 votes, and we can select it. 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 grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #. Consider doing the following operation: - Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns. You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices. -----Constraints----- - 1 \leq H, W \leq 6 - 1 \leq K \leq HW - c_{i,j} is . or #. -----Input----- Input is given from Standard Input in the following format: H W K c_{1,1}c_{1,2}...c_{1,W} c_{2,1}c_{2,2}...c_{2,W} : c_{H,1}c_{H,2}...c_{H,W} -----Output----- Print an integer representing the number of choices of rows and columns satisfying the condition. -----Sample Input----- 2 3 2 ..# ### -----Sample Output----- 5 Five choices below satisfy the condition. - The 1-st row and 1-st column - The 1-st row and 2-nd column - The 1-st row and 3-rd column - The 1-st and 2-nd column - The 3-rd column 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 non-negative integers L and R. We will choose two integers i and j such that L \leq i < j \leq R. Find the minimum possible value of (i \times j) \mbox{ mod } 2019. -----Constraints----- - All values in input are integers. - 0 \leq L < R \leq 2 \times 10^9 -----Input----- Input is given from Standard Input in the following format: L R -----Output----- Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition. -----Sample Input----- 2020 2040 -----Sample Output----- 2 When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 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. Polycarp likes numbers that are divisible by 3. He has a huge number $s$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $3$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $m$ such cuts, there will be $m+1$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $3$. For example, if the original number is $s=3121$, then Polycarp can cut it into three parts with two cuts: $3|1|21$. As a result, he will get two numbers that are divisible by $3$. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by $3$ that Polycarp can obtain? -----Input----- The first line of the input contains a positive integer $s$. The number of digits of the number $s$ is between $1$ and $2\cdot10^5$, inclusive. The first (leftmost) digit is not equal to 0. -----Output----- Print the maximum number of numbers divisible by $3$ that Polycarp can get by making vertical cuts in the given number $s$. -----Examples----- Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 -----Note----- In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $3$. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $33$ digits 0. Each of the $33$ digits 0 forms a number that is divisible by $3$. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $0$, $9$, $201$ and $81$ are divisible by $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 arrays $a$ and $b$, each contains $n$ integers. You want to create a new array $c$ as follows: choose some real (i.e. not necessarily integer) number $d$, and then for every $i \in [1, n]$ let $c_i := d \cdot a_i + b_i$. Your goal is to maximize the number of zeroes in array $c$. What is the largest possible answer, if you choose $d$ optimally? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in both arrays. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($-10^9 \le a_i \le 10^9$). The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($-10^9 \le b_i \le 10^9$). -----Output----- Print one integer β€” the maximum number of zeroes in array $c$, if you choose $d$ optimally. -----Examples----- Input 5 1 2 3 4 5 2 4 7 11 3 Output 2 Input 3 13 37 39 1 2 3 Output 2 Input 4 0 0 0 0 1 2 3 4 Output 0 Input 3 1 2 -1 -6 -12 6 Output 3 -----Note----- In the first example, we may choose $d = -2$. In the second example, we may choose $d = -\frac{1}{13}$. In the third example, we cannot obtain any zero in array $c$, no matter which $d$ we choose. In the fourth example, we may choose $d = 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. There are $n$ left boots and $n$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $l$ and $r$, both of length $n$. The character $l_i$ stands for the color of the $i$-th left boot and the character $r_i$ stands for the color of the $i$-th right boot. A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color. For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z'). Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible. Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. -----Input----- The first line contains $n$ ($1 \le n \le 150000$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $l$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th left boot. The third line contains the string $r$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th right boot. -----Output----- Print $k$ β€” the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $k$ lines should contain pairs $a_j, b_j$ ($1 \le a_j, b_j \le n$). The $j$-th of these lines should contain the index $a_j$ of the left boot in the $j$-th pair and index $b_j$ of the right boot in the $j$-th pair. All the numbers $a_j$ should be distinct (unique), all the numbers $b_j$ should be distinct (unique). If there are many optimal answers, print any of them. -----Examples----- Input 10 codeforces dodivthree Output 5 7 8 4 9 2 2 9 10 3 1 Input 7 abaca?b zabbbcc Output 5 6 5 2 3 4 6 7 4 1 2 Input 9 bambarbia hellocode Output 0 Input 10 code?????? ??????test Output 10 6 2 1 6 7 3 3 5 4 8 9 7 5 1 2 4 10 9 8 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. This problem is given in two editions, which differ exclusively in the constraints on the number $n$. You are given an array of integers $a[1], a[2], \dots, a[n].$ A block is a sequence of contiguous (consecutive) elements $a[l], a[l+1], \dots, a[r]$ ($1 \le l \le r \le n$). Thus, a block is defined by a pair of indices $(l, r)$. Find a set of blocks $(l_1, r_1), (l_2, r_2), \dots, (l_k, r_k)$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $(l_i, r_i)$ and $(l_j, r_j$) where $i \neq j$ either $r_i < l_j$ or $r_j < l_i$. For each block the sum of its elements is the same. Formally, $$a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]=$$ $$\dots =$$ $$a[l_k]+a[l_k+1]+\dots+a[r_k].$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $(l_1', r_1'), (l_2', r_2'), \dots, (l_{k'}', r_{k'}')$ satisfying the above two requirements with $k' > k$. $\left. \begin{array}{|l|l|l|l|l|l|} \hline 4 & {1} & {2} & {2} & {1} & {5} & {3} \\ \hline \end{array} \right.$ The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. -----Input----- The first line contains integer $n$ ($1 \le n \le 1500$) β€” the length of the given array. The second line contains the sequence of elements $a[1], a[2], \dots, a[n]$ ($-10^5 \le a_i \le 10^5$). -----Output----- In the first line print the integer $k$ ($1 \le k \le n$). The following $k$ lines should contain blocks, one per line. In each line print a pair of indices $l_i, r_i$ ($1 \le l_i \le r_i \le n$) β€” the bounds of the $i$-th block. You can print blocks in any order. If there are multiple answers, print any of them. -----Examples----- Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 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 an array $a$, consisting of $n$ positive integers. Let's call a concatenation of numbers $x$ and $y$ the number that is obtained by writing down numbers $x$ and $y$ one right after another without changing the order. For example, a concatenation of numbers $12$ and $3456$ is a number $123456$. Count the number of ordered pairs of positions $(i, j)$ ($i \neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $2 \le k \le 10^9$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer β€” the number of ordered pairs of positions $(i, j)$ ($i \neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$. -----Examples----- Input 6 11 45 1 10 12 11 7 Output 7 Input 4 2 2 78 4 10 Output 12 Input 5 2 3 7 19 3 3 Output 0 -----Note----- In the first example pairs $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 1)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ suffice. They produce numbers $451$, $4510$, $110$, $1045$, $1012$, $121$, $1210$, respectively, each of them is divisible by $11$. In the second example all $n(n - 1)$ pairs suffice. In the third example no pair is sufficient. 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 integer sequence A of length N whose values are unknown. Given is an integer sequence B of length N-1 which is known to satisfy the following: B_i \geq \max(A_i, A_{i+1}) Find the maximum possible sum of the elements of A. -----Constraints----- - All values in input are integers. - 2 \leq N \leq 100 - 0 \leq B_i \leq 10^5 -----Input----- Input is given from Standard Input in the following format: N B_1 B_2 ... B_{N-1} -----Output----- Print the maximum possible sum of the elements of A. -----Sample Input----- 3 2 5 -----Sample Output----- 9 A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum. 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 sequence A of length N. Find the maximum absolute difference of two elements (with different indices) in A. -----Constraints----- - 2 \leq N \leq 100 - 1 \leq A_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N -----Output----- Print the maximum absolute difference of two elements (with different indices) in A. -----Sample Input----- 4 1 4 6 3 -----Sample Output----- 5 The maximum absolute difference of two elements is A_3-A_1=6-1=5. 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 towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \sqrt{\left(x_i-x_j\right)^2+\left(y_i-y_j\right)^2}. There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths. -----Constraints----- - 2 \leq N \leq 8 - -1000 \leq x_i \leq 1000 - -1000 \leq y_i \leq 1000 - \left(x_i, y_i\right) \neq \left(x_j, y_j\right) (if i \neq j) - (Added 21:12 JST) All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N -----Output----- Print the average length of the paths. Your output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}. -----Sample Input----- 3 0 0 1 0 0 1 -----Sample Output----- 2.2761423749 There are six paths to visit the towns: 1 β†’ 2 β†’ 3, 1 β†’ 3 β†’ 2, 2 β†’ 1 β†’ 3, 2 β†’ 3 β†’ 1, 3 β†’ 1 β†’ 2, and 3 β†’ 2 β†’ 1. The length of the path 1 β†’ 2 β†’ 3 is \sqrt{\left(0-1\right)^2+\left(0-0\right)^2} + \sqrt{\left(1-0\right)^2+\left(0-1\right)^2} = 1+\sqrt{2}. By calculating the lengths of the other paths in this way, we see that the average length of all routes is: \frac{\left(1+\sqrt{2}\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)}{6} = 2.276142... 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 window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.) We will close the window so as to minimize the total horizontal length of the uncovered part of the window. Find the total horizontal length of the uncovered parts of the window then. -----Constraints----- - 1 \leq A \leq 100 - 1 \leq B \leq 100 - A and B are integers. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the total horizontal length of the uncovered parts of the window. -----Sample Input----- 12 4 -----Sample Output----- 4 We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 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.
Solve the programming task below in a Python markdown code block. You are given an integer N. For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B. For example, F(3,11) = 2 since 3 has one digit and 11 has two digits. Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B. -----Constraints----- - 1 \leq N \leq 10^{10} - N is an integer. -----Input----- The input is given from Standard Input in the following format: N -----Output----- Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B. -----Sample Input----- 10000 -----Sample Output----- 3 F(A,B) has a minimum value of 3 at (A,B)=(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. Katsusando loves omelette rice. Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone. To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not. The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food. Find the number of the foods liked by all the N people. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 30 - 1 \leq K_i \leq M - 1 \leq A_{ij} \leq M - For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct. -----Constraints----- Input is given from Standard Input in the following format: N M K_1 A_{11} A_{12} ... A_{1K_1} K_2 A_{21} A_{22} ... A_{2K_2} : K_N A_{N1} A_{N2} ... A_{NK_N} -----Output----- Print the number of the foods liked by all the N people. -----Sample Input----- 3 4 2 1 3 3 1 2 3 2 3 2 -----Sample Output----- 1 As only the third food is liked by all the three people, 1 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. Given is an integer N. Takahashi chooses an integer a from the positive integers not greater than N with equal probability. Find the probability that a is odd. -----Constraints----- - 1 \leq N \leq 100 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the probability that a is odd. Your output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}. -----Sample Input----- 4 -----Sample Output----- 0.5000000000 There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \frac{2}{4} = 0.5. 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 numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? -----Constraints----- - All values in input are integers. - 1 \leq N \leq 15 - 0 \leq A_i \leq N - 1 - 1 \leq x_{ij} \leq N - x_{ij} \neq i - x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) - y_{ij} = 0, 1 -----Input----- Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} -----Output----- Print the maximum possible number of honest persons among the N people. -----Sample Input----- 3 1 2 1 1 1 1 1 2 0 -----Sample Output----- 2 If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons. 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 string S consisting of lowercase English letters. If the length of S is at most K, print S without change. If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result. -----Constraints----- - K is an integer between 1 and 100 (inclusive). - S is a string consisting of lowercase English letters. - The length of S is between 1 and 100 (inclusive). -----Input----- Input is given from Standard Input in the following format: K S -----Output----- Print a string as stated in Problem Statement. -----Sample Input----- 7 nikoandsolstice -----Sample Output----- nikoand... nikoandsolstice has a length of 15, which exceeds K=7. We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand.... 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 the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters. In Taknese, the plural form of a noun is spelled based on the following rules: - If a noun's singular form does not end with s, append s to the end of the singular form. - If a noun's singular form ends with s, append es to the end of the singular form. You are given the singular form S of a Taknese noun. Output its plural form. -----Constraints----- - S is a string of length 1 between 1000, inclusive. - S contains only lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the plural form of the given Taknese word. -----Sample Input----- apple -----Sample Output----- apples apple ends with e, so its plural form is apples. 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. N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town. There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}. Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief? -----Constraints----- - All values in input are integers. - 1 \leq N \leq 100 - 1 \leq K \leq 100 - 1 \leq d_i \leq N - 1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N -----Input----- Input is given from Standard Input in the following format: N K d_1 A_{1, 1} \cdots A_{1, d_1} \vdots d_K A_{K, 1} \cdots A_{K, d_K} -----Output----- Print the answer. -----Sample Input----- 3 2 2 1 3 1 3 -----Sample Output----- 1 - Snuke 1 has Snack 1. - Snuke 2 has no snacks. - Snuke 3 has Snack 1 and 2. Thus, there will be one victim: Snuke 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 slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S. Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime. Ultimately, how many slimes will be there? -----Constraints----- - 1 \leq N \leq 10^5 - |S| = N - S consists of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the final number of slimes. -----Sample Input----- 10 aabbbbaaca -----Sample Output----- 5 Ultimately, these slimes will fuse into abaca. 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 AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}. Snuke, an employee, would like to play with this sequence. Specifically, he would like to repeat the following operation as many times as possible: For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3". Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer. At most how many operations can be performed? -----Constraints----- - N is an integer between 1 and 10 \ 000 (inclusive). - a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive). -----Input----- Input is given from Standard Input in the following format: N a_1 a_2 a_3 ... a_N -----Output----- Print the maximum number of operations that Snuke can perform. -----Sample Input----- 3 5 2 4 -----Sample Output----- 3 The sequence is initially {5, 2, 4}. Three operations can be performed as follows: - First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}. - Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}. - Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 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. We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s. We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen? -----Constraints----- - All values in input are integers. - 0 \leq A, B, C - 1 \leq K \leq A + B + C \leq 2 \times 10^9 -----Input----- Input is given from Standard Input in the following format: A B C K -----Output----- Print the maximum possible sum of the numbers written on the cards chosen. -----Sample Input----- 2 1 1 3 -----Sample Output----- 2 Consider picking up two cards with 1s and one card with a 0. In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value. 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 some animals in a garden. Each of them is a crane with two legs or a turtle with four legs. Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct. -----Constraints----- - 1 \leq X \leq 100 - 1 \leq Y \leq 100 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: X Y -----Output----- If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No. -----Sample Input----- 3 8 -----Sample Output----- Yes The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is 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 an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges. Here, a self-loop is an edge where a_i = b_i (1≀i≀M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≀i<j≀M). How many different paths start from vertex 1 and visit all the vertices exactly once? Here, the endpoints of a path are considered visited. For example, let us assume that the following undirected graph shown in Figure 1 is given. Figure 1: an example of an undirected graph The following path shown in Figure 2 satisfies the condition. Figure 2: an example of a path that satisfies the condition However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices. Figure 3: an example of a path that does not satisfy the condition Neither the following path shown in Figure 4, because it does not start from vertex 1. Figure 4: another example of a path that does not satisfy the condition -----Constraints----- - 2≦N≦8 - 0≦M≦N(N-1)/2 - 1≦a_i<b_i≦N - The given graph contains neither self-loops nor double edges. -----Input----- The input is given from Standard Input in the following format: N M a_1 b_1 a_2 b_2 : a_M b_M -----Output----- Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once. -----Sample Input----- 3 3 1 2 1 3 2 3 -----Sample Output----- 2 The given graph is shown in the following figure: The following two paths satisfy 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. Let us define the FizzBuzz sequence a_1,a_2,... as follows: - If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}. - If the above does not hold but 3 divides i, a_i=\mbox{Fizz}. - If none of the above holds but 5 divides i, a_i=\mbox{Buzz}. - If none of the above holds, a_i=i. Find the sum of all numbers among the first N terms of the FizzBuzz sequence. -----Constraints----- - 1 \leq N \leq 10^6 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the sum of all numbers among the first N terms of the FizzBuzz sequence. -----Sample Input----- 15 -----Sample Output----- 60 The first 15 terms of the FizzBuzz sequence are: 1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz} Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60. 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 an integer X and an integer sequence of length N: p_1, \ldots, p_N. Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer. -----Constraints----- - 1 \leq X \leq 100 - 0 \leq N \leq 100 - 1 \leq p_i \leq 100 - p_1, \ldots, p_N are all distinct. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: X N p_1 ... p_N -----Output----- Print the answer. -----Sample Input----- 6 5 4 7 10 6 5 -----Sample Output----- 8 Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8. 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 H rows and W columns of white square cells. You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns. How many white cells will remain? It can be proved that this count does not depend on what rows and columns are chosen. -----Constraints----- - All values in input are integers. - 1 \leq H, W \leq 20 - 1 \leq h \leq H - 1 \leq w \leq W -----Input----- Input is given from Standard Input in the following format: H W h w -----Output----- Print the number of white cells that will remain. -----Sample Input----- 3 2 2 1 -----Sample Output----- 1 There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains. 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 sequence of integers of length $n$ and integer number $k$. You should print any integer number $x$ in the range of $[1; 10^9]$ (i.e. $1 \le x \le 10^9$) such that exactly $k$ elements of given sequence are less than or equal to $x$. Note that the sequence can contain equal elements. If there is no such $x$, print "-1" (without quotes). -----Input----- The first line of the input contains integer numbers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $0 \le k \le n$). The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β€” the sequence itself. -----Output----- Print any integer number $x$ from range $[1; 10^9]$ such that exactly $k$ elements of given sequence is less or equal to $x$. If there is no such $x$, print "-1" (without quotes). -----Examples----- Input 7 4 3 7 5 1 10 3 20 Output 6 Input 7 2 3 7 5 1 10 3 20 Output -1 -----Note----- In the first example $5$ is also a valid answer because the elements with indices $[1, 3, 4, 6]$ is less than or equal to $5$ and obviously less than or equal to $6$. In the second example you cannot choose any number that only $2$ elements of the given sequence will be less than or equal to this number because $3$ elements of the given sequence will be also less than or equal to this 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. 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 100$) β€” 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. Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good. You are given a string $s$, you have to delete minimum number of characters from this string so that it becomes good. -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of characters in $s$. The second line contains the string $s$, consisting of exactly $n$ lowercase Latin letters. -----Output----- In the first line, print one integer $k$ ($0 \le k \le n$) β€” the minimum number of characters you have to delete from $s$ to make it good. In the second line, print the resulting string $s$. If it is empty, you may leave the second line blank, or not print it at all. -----Examples----- Input 4 good Output 0 good Input 4 aabc Output 2 ab Input 3 aaa 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 a connected undirected graph consisting of $n$ vertices and $m$ edges. There are no self-loops or multiple edges in the given graph. You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges). -----Input----- The first line contains two integer numbers $n$ and $m$ ($2 \le n \le 2 \cdot 10^5$, $n - 1 \le m \le 2 \cdot 10^5$) β€” the number of vertices and edges, respectively. The following $m$ lines contain edges: edge $i$ is given as a pair of vertices $u_i$, $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). There are no multiple edges in the given graph, i. e. for each pair ($u_i, v_i$) there are no other pairs ($u_i, v_i$) and ($v_i, u_i$) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph). -----Output----- If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line. Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length $m$. The $i$-th element of this string should be '0' if the $i$-th edge of the graph should be directed from $u_i$ to $v_i$, and '1' otherwise. Edges are numbered in the order they are given in the input. -----Example----- Input 6 5 1 5 2 1 1 4 3 1 6 1 Output YES 10100 -----Note----- The picture corresponding to the first example: [Image] And one of possible answers: $\text{of}$ 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 four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D. -----Constraints----- - 1\leq A\leq B\leq 10^{18} - 1\leq C,D\leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B C D -----Output----- Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D. -----Sample Input----- 4 9 2 3 -----Sample Output----- 2 5 and 7 satisfy 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. An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9. Determine whether N is a multiple of 9. -----Constraints----- - 0 \leq N < 10^{200000} - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If N is a multiple of 9, print Yes; otherwise, print No. -----Sample Input----- 123456789 -----Sample Output----- Yes The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 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. To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation: - 1 yen (the currency of Japan) - 6 yen, 6^2(=36) yen, 6^3(=216) yen, ... - 9 yen, 9^2(=81) yen, 9^3(=729) yen, ... At least how many operations are required to withdraw exactly N yen in total? It is not allowed to re-deposit the money you withdrew. -----Constraints----- - 1 \leq N \leq 100000 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If at least x operations are required to withdraw exactly N yen in total, print x. -----Sample Input----- 127 -----Sample Output----- 4 By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations. 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 string S and T consisting of lowercase English letters. Determine if S equals T after rotation. That is, determine if S equals T after the following operation is performed some number of times: Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}. Here, |X| denotes the length of the string X. -----Constraints----- - 2 \leq |S| \leq 100 - |S| = |T| - S and T consist of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S T -----Output----- If S equals T after rotation, print Yes; if it does not, print No. -----Sample Input----- kyoto tokyo -----Sample Output----- Yes - In the first operation, kyoto becomes okyot. - In the second operation, okyot becomes tokyo. 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 two strings S and T. Let us change some of the characters in S so that T will be a substring of S. At least how many characters do we need to change? Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx. -----Constraints----- - The lengths of S and T are each at least 1 and at most 1000. - The length of T is at most that of S. - S and T consist of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S T -----Output----- Print the minimum number of characters in S that need to be changed. -----Sample Input----- cabacc abc -----Sample Output----- 1 For example, changing the fourth character a in S to c will match the second through fourth characters in S to T. Since S itself does not have T as its substring, this number of changes - one - is the minimum needed. 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. Find the K-th largest positive integer that divides both A and B. The input guarantees that there exists such a number. -----Constraints----- - All values in input are integers. - 1 \leq A, B \leq 100 - The K-th largest positive integer that divides both A and B exists. - K \geq 1 -----Input----- Input is given from Standard Input in the following format: A B K -----Output----- Print the K-th largest positive integer that divides both A and B. -----Sample Input----- 8 12 2 -----Sample Output----- 2 Three positive integers divides both 8 and 12: 1, 2 and 4. Among them, the second largest 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. Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.) Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn? (We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.) -----Constraints----- - 0 \leq X \leq 10^9 - X is an integer. -----Input----- Input is given from Standard Input in the following format: X -----Output----- Print the maximum number of happiness points that can be earned. -----Sample Input----- 1024 -----Sample Output----- 2020 By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned. 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 living on a number line. The i-th person lives at coordinate X_i. You are going to hold a meeting that all N people have to attend. The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting. Find the minimum total points of stamina the N people have to spend. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 100 - 1 \leq X_i \leq 100 -----Input----- Input is given from Standard Input in the following format: N X_1 X_2 ... X_N -----Output----- Print the minimum total stamina the N people have to spend. -----Sample Input----- 2 1 4 -----Sample Output----- 5 Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend. Note that you can hold the meeting only at an integer coordinate. 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's house has only one socket. Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets. One power strip with A sockets can extend one empty socket into A empty sockets. Find the minimum number of power strips required. -----Constraints----- - All values in input are integers. - 2 \leq A \leq 20 - 1 \leq B \leq 20 -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the minimum number of power strips required. -----Sample Input----- 4 10 -----Sample Output----- 3 3 power strips, each with 4 sockets, extend the socket into 10 empty sockets. 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. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of points on a plane. Each of the next n lines contains two real coordinates x_{i} and y_{i} of the $i^{\text{th}}$ point, specified with exactly 2 fractional digits. All coordinates are between - 1000 and 1000, inclusive. -----Output----- Output a single real number ΞΈ β€” the answer to the problem statement. The absolute or relative error of your answer should be at most 10^{ - 2}. -----Examples----- Input 8 -2.14 2.06 -1.14 2.04 -2.16 1.46 -2.14 0.70 -1.42 0.40 -0.94 -0.48 -1.42 -1.28 -2.16 -1.62 Output 5.410 Input 5 2.26 1.44 2.28 0.64 2.30 -0.30 1.58 0.66 3.24 0.66 Output 5.620 Input 8 6.98 2.06 6.40 1.12 5.98 0.24 5.54 -0.60 7.16 0.30 7.82 1.24 8.34 0.24 8.74 -0.76 Output 5.480 Input 5 10.44 2.06 10.90 0.80 11.48 -0.48 12.06 0.76 12.54 2.06 Output 6.040 Input 8 16.94 2.42 15.72 2.38 14.82 1.58 14.88 0.50 15.76 -0.16 16.86 -0.20 17.00 0.88 16.40 0.92 Output 6.040 Input 7 20.62 3.00 21.06 2.28 21.56 1.36 21.66 0.56 21.64 -0.52 22.14 2.32 22.62 3.04 Output 6.720 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 the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of cups of coffee and the number of pages in the coursework. 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 caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer 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. You are given an array $s$ consisting of $n$ integers. You have to find any array $t$ of length $k$ such that you can cut out maximum number of copies of array $t$ from array $s$. Cutting out the copy of $t$ means that for each element $t_i$ of array $t$ you have to find $t_i$ in $s$ and remove it from $s$. If for some $t_i$ you cannot find such element in $s$, then you cannot cut out one more copy of $t$. The both arrays can contain duplicate elements. For example, if $s = [1, 2, 3, 2, 4, 3, 1]$ and $k = 3$ then one of the possible answers is $t = [1, 2, 3]$. This array $t$ can be cut out $2$ times. To cut out the first copy of $t$ you can use the elements $[1, \underline{\textbf{2}}, 3, 2, 4, \underline{\textbf{3}}, \underline{\textbf{1}}]$ (use the highlighted elements). After cutting out the first copy of $t$ the array $s$ can look like $[1, 3, 2, 4]$. To cut out the second copy of $t$ you can use the elements $[\underline{\textbf{1}}, \underline{\textbf{3}}, \underline{\textbf{2}}, 4]$. After cutting out the second copy of $t$ the array $s$ will be $[4]$. Your task is to find such array $t$ that you can cut out the copy of $t$ from $s$ maximum number of times. If there are multiple answers, you may choose any of them. -----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 elements in $s$ and the desired number of elements in $t$, respectively. The second line of the input contains exactly $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2 \cdot 10^5$). -----Output----- Print $k$ integers β€” the elements of array $t$ such that you can cut out maximum possible number of copies of this array from $s$. If there are multiple answers, print any of them. The required array $t$ can contain duplicate elements. All the elements of $t$ ($t_1, t_2, \dots, t_k$) should satisfy the following condition: $1 \le t_i \le 2 \cdot 10^5$. -----Examples----- Input 7 3 1 2 3 2 4 3 1 Output 1 2 3 Input 10 4 1 3 1 3 10 3 7 7 12 3 Output 7 3 1 3 Input 15 2 1 2 1 1 1 2 1 1 2 1 2 1 1 1 1 Output 1 1 -----Note----- The first example is described in the problem statement. In the second example the only answer is $[7, 3, 1, 3]$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $2$. In the third example the array $t$ can be cut out $5$ times. 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 matrix $a$, consisting of $n$ rows and $m$ columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be $s_1, s_2, \dots, s_{nm}$. The traversal is $k$-acceptable if for all $i$ ($1 \le i \le nm - 1$) $|s_i - s_{i + 1}| \ge k$. Find the maximum integer $k$ such that there exists some order of rows of matrix $a$ that it produces a $k$-acceptable traversal. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 16$, $1 \le m \le 10^4$, $2 \le nm$) β€” the number of rows and the number of columns, respectively. Each of the next $n$ lines contains $m$ integers ($1 \le a_{i, j} \le 10^9$) β€” the description of the matrix. -----Output----- Print a single integer $k$ β€” the maximum number such that there exists some order of rows of matrix $a$ that it produces an $k$-acceptable traversal. -----Examples----- Input 4 2 9 9 10 8 5 3 4 3 Output 5 Input 2 4 1 2 3 4 10 3 7 3 Output 0 Input 6 1 3 6 2 5 1 4 Output 3 -----Note----- In the first example you can rearrange rows as following to get the $5$-acceptable traversal: 5 3 10 8 4 3 9 9 Then the sequence $s$ will be $[5, 10, 4, 9, 3, 8, 3, 9]$. Each pair of neighbouring elements have at least $k = 5$ difference between them. In the second example the maximum $k = 0$, any order is $0$-acceptable. In the third example the given order is already $3$-acceptable, you can leave it as it is. 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 can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed. Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx". You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by $1$. For example, if you delete the character in the position $2$ from the string "exxxii", then the resulting string is "exxii". -----Input----- The first line contains integer $n$ $(3 \le n \le 100)$ β€” the length of the file name. The second line contains a string of length $n$ consisting of lowercase Latin letters only β€” the file name. -----Output----- Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0. -----Examples----- Input 6 xxxiii Output 1 Input 5 xxoxx Output 0 Input 10 xxxxxxxxxx Output 8 -----Note----- In the first example Polycarp tried to send a file with name contains number $33$, written in Roman numerals. But he can not just send the file, because it name contains three letters "x" in a row. To send the file he needs to remove any one of this letters. 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 is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2. Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r. -----Constraints----- - 1 \leq r \leq 100 - r is an integer. -----Input----- Input is given from Standard Input in the following format: r -----Output----- Print an integer representing the area of the regular dodecagon. -----Sample Input----- 4 -----Sample Output----- 48 The area of the regular dodecagon is 3 \times 4^2 = 48. 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 S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print unsafe; otherwise, print safe. -----Constraints----- - 1 \leq S \leq 100 - 1 \leq W \leq 100 -----Input----- Input is given from Standard Input in the following format: S W -----Output----- If the wolves will attack the sheep, print unsafe; otherwise, print safe. -----Sample Input----- 4 5 -----Sample Output----- unsafe There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them. 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 right triangle ABC with ∠ABC=90°. Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC. It is guaranteed that the area of the triangle ABC is an integer. -----Constraints----- - 1 \leq |AB|,|BC|,|CA| \leq 100 - All values in input are integers. - The area of the triangle ABC is an integer. -----Input----- Input is given from Standard Input in the following format: |AB| |BC| |CA| -----Output----- Print the area of the triangle ABC. -----Sample Input----- 3 4 5 -----Sample Output----- 6 This triangle has an area of 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. Takahashi likes the sound when he buys a drink from a vending machine. That sound can be heard by spending A yen (the currency of Japan) each time. Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time. How many times will he hear the sound? -----Constraints----- - All values in input are integers. - 1 \leq A, B, C \leq 100 -----Input----- Input is given from Standard Input in the following format: A B C -----Output----- Print the number of times Takahashi will hear his favorite sound. -----Sample Input----- 2 11 4 -----Sample Output----- 4 Since he has not less than 8 yen, he will hear the sound four times and be satisfied. 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. Compute A \times B. -----Constraints----- - 1 \leq A \leq 100 - 1 \leq B \leq 100 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the value A \times B as an integer. -----Sample Input----- 2 5 -----Sample Output----- 10 We have 2 \times 5 = 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. A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation. Find the total number of biscuits produced within T + 0.5 seconds after activation. -----Constraints----- - All values in input are integers. - 1 \leq A, B, T \leq 20 -----Input----- Input is given from Standard Input in the following format: A B T -----Output----- Print the total number of biscuits produced within T + 0.5 seconds after activation. -----Sample Input----- 3 5 7 -----Sample Output----- 10 - Five biscuits will be produced three seconds after activation. - Another five biscuits will be produced six seconds after activation. - Thus, a total of ten biscuits will be produced within 7.5 seconds after activation. 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 the Jumbo will practice golf. His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive). If he can achieve the objective, print OK; if he cannot, print NG. -----Constraints----- - All values in input are integers. - 1 \leq A \leq B \leq 1000 - 1 \leq K \leq 1000 -----Input----- Input is given from Standard Input in the following format: K A B -----Output----- If he can achieve the objective, print OK; if he cannot, print NG. -----Sample Input----- 7 500 600 -----Sample Output----- OK Among the multiples of 7, for example, 567 lies between 500 and 600. 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 order to pass the entrance examination tomorrow, Taro has to study for T more hours. Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A). While (X \times t) hours pass in World B, t hours pass in World A. How many hours will pass in World A while Taro studies for T hours in World B? -----Constraints----- - All values in input are integers. - 1 \leq T \leq 100 - 1 \leq X \leq 100 -----Input----- Input is given from Standard Input in the following format: T X -----Output----- Print the number of hours that will pass in World A. The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}. -----Sample Input----- 8 3 -----Sample Output----- 2.6666666667 While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A. Note that an absolute or relative error of at most 10^{-3} is 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. We have two bottles for holding water. Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water. Bottle 2 contains C milliliters of water. We will transfer water from Bottle 2 to Bottle 1 as much as possible. How much amount of water will remain in Bottle 2? -----Constraints----- - All values in input are integers. - 1 \leq B \leq A \leq 20 - 1 \leq C \leq 20 -----Input----- Input is given from Standard Input in the following format: A B C -----Output----- Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2. -----Sample Input----- 6 4 3 -----Sample Output----- 1 We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 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. We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)). There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|. -----Notes----- For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k. -----Constraints----- - 2 \leq N \leq 8 - P and Q are permutations of size N. -----Input----- Input is given from Standard Input in the following format: N P_1 P_2 ... P_N Q_1 Q_2 ... Q_N -----Output----- Print |a - b|. -----Sample Input----- 3 1 3 2 3 1 2 -----Sample Output----- 3 There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 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 a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S. Here, a ACGT string is a string that contains no characters other than A, C, G and T. -----Notes----- A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T. For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC. -----Constraints----- - S is a string of length between 1 and 10 (inclusive). - Each character in S is an uppercase English letter. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the length of the longest ACGT string that is a substring of S. -----Sample Input----- ATCODER -----Sample Output----- 3 Among the ACGT strings that are substrings of ATCODER, the longest one is ATC. 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 positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros). -----Constraints----- - 1 \leq N \leq 10^5 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the number of positive integers less than or equal to N that have an odd number of digits. -----Sample Input----- 11 -----Sample Output----- 9 Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 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. Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so. Operation: Choose one character of S and replace it with a different character. -----Constraints----- - S and T have lengths between 1 and 2\times 10^5 (inclusive). - S and T consists of lowercase English letters. - S and T have equal lengths. -----Input----- Input is given from Standard Input in the following format: S T -----Output----- Print the answer. -----Sample Input----- cupofcoffee cupofhottea -----Sample Output----- 4 We can achieve the objective in four operations, such as the following: - First, replace the sixth character c with h. - Second, replace the eighth character f with t. - Third, replace the ninth character f with t. - Fourth, replace the eleventh character e with 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 will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above. The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner? -----Constraints----- - -40 \leq X \leq 40 - X is an integer. -----Input----- Input is given from Standard Input in the following format: X -----Output----- Print Yes if you will turn on the air conditioner; print No otherwise. -----Sample Input----- 25 -----Sample 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. There are N points in a D-dimensional space. The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}). The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}. How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer? -----Constraints----- - All values in input are integers. - 2 \leq N \leq 10 - 1 \leq D \leq 10 - -20 \leq X_{ij} \leq 20 - No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}. -----Input----- Input is given from Standard Input in the following format: N D X_{11} X_{12} ... X_{1D} X_{21} X_{22} ... X_{2D} \vdots X_{N1} X_{N2} ... X_{ND} -----Output----- Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer. -----Sample Input----- 3 2 1 2 5 5 -2 8 -----Sample Output----- 1 The number of pairs with an integer distance is one, as follows: - The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer. - The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer. - The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer. 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 door of Snuke's laboratory is locked with a security code. The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same. You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good. -----Constraints----- - S is a 4-character string consisting of digits. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S is hard to enter, print Bad; otherwise, print Good. -----Sample Input----- 3776 -----Sample Output----- Bad The second and third digits are the same, so 3776 is hard to enter. 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 pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i. When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot. After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient. -----Constraints----- - 2 \leq N \leq 50 - 1 \leq v_i \leq 1000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N v_1 v_2 \ldots v_N -----Output----- Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining. 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 3 4 -----Sample Output----- 3.5 If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5. Printing 3.50001, 3.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. Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once. The i-th dish (1 \leq i \leq N) he ate was Dish A_i. When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points. Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points. Find the sum of the satisfaction points he gained. -----Constraints----- - All values in input are integers. - 2 \leq N \leq 20 - 1 \leq A_i \leq N - A_1, A_2, ..., A_N are all different. - 1 \leq B_i \leq 50 - 1 \leq C_i \leq 50 -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N B_1 B_2 ... B_N C_1 C_2 ... C_{N-1} -----Output----- Print the sum of the satisfaction points Takahashi gained, as an integer. -----Sample Input----- 3 3 1 2 2 5 4 3 6 -----Sample Output----- 14 Takahashi gained 14 satisfaction points in total, as follows: - First, he ate Dish 3 and gained 4 satisfaction points. - Next, he ate Dish 1 and gained 2 satisfaction points. - Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points. 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 string S of length N consisting of uppercase English letters. How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)? -----Constraints----- - 3 \leq N \leq 50 - S consists of uppercase English letters. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print number of occurrences of ABC in S as contiguous subsequences. -----Sample Input----- 10 ZABCDBABCQ -----Sample Output----- 2 Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters. 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 participating in a programming contest, AXC001. He has just submitted his code to Problem A. The problem has N test cases, all of which must be passed to get an AC verdict. Takahashi's submission has passed M cases out of the N test cases. Determine whether Takahashi's submission gets an AC. -----Constraints----- - 1 \leq N \leq 100 - 0 \leq M \leq N - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N M -----Output----- If Takahashi's submission gets an AC, print Yes; otherwise, print No. -----Sample Input----- 3 3 -----Sample Output----- Yes All three test cases have been passed, so his submission gets an AC. 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 string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal. Given a string S, determine whether it is coffee-like. -----Constraints----- - S is a string of length 6 consisting of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S is coffee-like, print Yes; otherwise, print No. -----Sample Input----- sippuu -----Sample Output----- Yes In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal. 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. When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC. You, the smartwatch, has found N routes to his home. If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i. Find the smallest cost of a route that takes not longer than time T. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 100 - 1 \leq T \leq 1000 - 1 \leq c_i \leq 1000 - 1 \leq t_i \leq 1000 - The pairs (c_i, t_i) are distinct. -----Input----- Input is given from Standard Input in the following format: N T c_1 t_1 c_2 t_2 : c_N t_N -----Output----- Print the smallest cost of a route that takes not longer than time T. If there is no route that takes not longer than time T, print TLE instead. -----Sample Input----- 3 70 7 60 1 80 4 50 -----Sample Output----- 4 - The first route gets him home at cost 7. - The second route takes longer than time T = 70. - The third route gets him home at cost 4. Thus, the cost 4 of the third route is the minimum. 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. N of us are going on a trip, by train or taxi. The train will cost each of us A yen (the currency of Japan). The taxi will cost us a total of B yen. How much is our minimum total travel expense? -----Constraints----- - All values in input are integers. - 1 \leq N \leq 20 - 1 \leq A \leq 50 - 1 \leq B \leq 50 -----Input----- Input is given from Standard Input in the following format: N A B -----Output----- Print an integer representing the minimum total travel expense. -----Sample Input----- 4 2 9 -----Sample Output----- 8 The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 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. The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are 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. Treeland consists of $n$ cities and $n-1$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right β€” the country's topology is an undirected tree. There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads. The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $k$ and the number of companies taking part in the privatization is minimal. Choose the number of companies $r$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $k$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $r$ that there is such assignment to companies from $1$ to $r$ that the number of cities which are not good doesn't exceed $k$. [Image] The picture illustrates the first example ($n=6, k=2$). The answer contains $r=2$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $3$) is not good. The number of such vertices (just one) doesn't exceed $k=2$. It is impossible to have at most $k=2$ not good cities in case of one company. -----Input----- The first line contains two integers $n$ and $k$ ($2 \le n \le 200000, 0 \le k \le n - 1$) β€” the number of cities and the maximal number of cities which can have two or more roads belonging to one company. The following $n-1$ lines contain roads, one road per line. Each line contains a pair of integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$, $y_i$ are cities connected with the $i$-th road. -----Output----- In the first line print the required $r$ ($1 \le r \le n - 1$). In the second line print $n-1$ numbers $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le r$), where $c_i$ is the company to own the $i$-th road. If there are multiple answers, print any of them. -----Examples----- Input 6 2 1 4 4 3 3 5 3 6 5 2 Output 2 1 2 1 1 2 Input 4 2 3 1 1 4 1 2 Output 1 1 1 1 Input 10 2 10 3 1 2 1 3 1 4 2 5 2 6 2 7 3 8 3 9 Output 3 1 1 2 3 2 3 1 3 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. Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year... $n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once. For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively). For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones. So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of friends. The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β€” the coordinates of the houses of the friends. -----Output----- Print two integers β€” the minimum and the maximum possible number of occupied houses after all moves are performed. -----Examples----- Input 4 1 2 4 4 Output 2 4 Input 9 1 1 8 8 8 4 4 4 4 Output 3 8 Input 7 4 3 7 1 4 3 3 Output 3 6 -----Note----- In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses. For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for 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. There are $n$ kids, numbered from $1$ to $n$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $p_1$, $p_2$, ..., $p_n$ (all these numbers are from $1$ to $n$ and are distinct, so $p$ is a permutation). Let the next kid for a kid $p_i$ be kid $p_{i + 1}$ if $i < n$ and $p_1$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $x$) and the next kid for $x$. Each kid told you which kids he/she remembered: the kid $i$ remembered kids $a_{i, 1}$ and $a_{i, 2}$. However, the order of $a_{i, 1}$ and $a_{i, 2}$ can differ from their order in the circle. [Image] Example: 5 kids in a circle, $p=[3, 2, 4, 1, 5]$ (or any cyclic shift). The information kids remembered is: $a_{1,1}=3$, $a_{1,2}=5$; $a_{2,1}=1$, $a_{2,2}=4$; $a_{3,1}=2$, $a_{3,2}=4$; $a_{4,1}=1$, $a_{4,2}=5$; $a_{5,1}=2$, $a_{5,2}=3$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists. 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 $n$ ($3 \le n \le 2 \cdot 10^5$) β€” the number of the kids. The next $n$ lines contain $2$ integers each. The $i$-th line contains two integers $a_{i, 1}$ and $a_{i, 2}$ ($1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}$) β€” the kids the $i$-th kid remembered, given in arbitrary order. -----Output----- Print $n$ integers $p_1$, $p_2$, ..., $p_n$ β€” permutation of integers from $1$ to $n$, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists. -----Examples----- Input 5 3 5 1 4 2 4 1 5 2 3 Output 3 2 4 1 5 Input 3 2 3 3 1 1 2 Output 3 1 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 a coach at your local university. There are $n$ students under your supervision, the programming skill of the $i$-th student is $a_i$. You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $5$. Your task is to report the maximum possible number of students in a balanced team. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of students. 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 a programming skill of the $i$-th student. -----Output----- Print one integer β€” the maximum possible number of students in a balanced team. -----Examples----- Input 6 1 10 17 12 15 2 Output 3 Input 10 1337 1337 1337 1337 1337 1337 1337 1337 1337 1337 Output 10 Input 6 1 1000 10000 10 100 1000000000 Output 1 -----Note----- In the first example you can create a team with skills $[12, 17, 15]$. In the second example you can take all students in a team because their programming skills are equal. In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two 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. Vova is playing a computer game. There are in total $n$ turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is $k$. During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than $a$, Vova can just play, and then the charge of his laptop battery will decrease by $a$; if the current charge of his laptop battery is strictly greater than $b$ ($b<a$), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by $b$; if the current charge of his laptop battery is less than or equal to $a$ and $b$ at the same time then Vova cannot do anything and loses the game. Regardless of Vova's turns the charge of the laptop battery is always decreases. Vova wants to complete the game (Vova can complete the game if after each of $n$ turns the charge of the laptop battery is strictly greater than $0$). Vova has to play exactly $n$ turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all. Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^5$) β€” the number of queries. Each query is presented by a single line. The only line of the query contains four integers $k, n, a$ and $b$ ($1 \le k, n \le 10^9, 1 \le b < a \le 10^9$) β€” the initial charge of Vova's laptop battery, the number of turns in the game and values $a$ and $b$, correspondingly. -----Output----- For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise. -----Example----- Input 6 15 5 3 2 15 5 4 3 15 5 2 1 15 5 5 1 16 7 5 2 20 5 7 3 Output 4 -1 5 2 0 1 -----Note----- In the first example query Vova can just play $4$ turns and spend $12$ units of charge and then one turn play and charge and spend $2$ more units. So the remaining charge of the battery will be $1$. In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be $0$ after the last turn. 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 lives on a coordinate line at the point $x = 0$. He goes to his friend that lives at the point $x = a$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $n$ non-intersecting segments, the $i$-th segment which is in the rain is represented as $[l_i, r_i]$ ($0 \le l_i < r_i \le a$). There are $m$ umbrellas lying on the line, the $i$-th umbrella is located at point $x_i$ ($0 \le x_i \le a$) and has weight $p_i$. When Polycarp begins his journey, he doesn't have any umbrellas. During his journey from $x = 0$ to $x = a$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $x$ to $x + 1$ if a segment $[x, x + 1]$ is in the rain (i.e. if there exists some $i$ such that $l_i \le x$ and $x + 1 \le r_i$). The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain. Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving. Can Polycarp make his way from point $x = 0$ to point $x = a$? If yes, find the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. -----Input----- The first line contains three integers $a$, $n$ and $m$ ($1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$) β€” the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \le l_i < r_i \le a$) β€” the borders of the $i$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $i$ and $j$ either $r_i < l_j$ or $r_j < l_i$. Each of the next $m$ lines contains two integers $x_i$ and $p_i$ ($0 \le x_i \le a$, $1 \le p_i \le 10^5$) β€” the location and the weight of the $i$-th umbrella. -----Output----- Print "-1" (without quotes) if Polycarp can't make his way from point $x = 0$ to point $x = a$. Otherwise print one integer β€” the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. -----Examples----- Input 10 2 4 3 7 8 10 0 10 3 4 8 1 1 2 Output 14 Input 10 1 1 0 9 0 5 Output 45 Input 10 1 1 0 9 1 5 Output -1 -----Note----- In the first example the only possible strategy is to take the fourth umbrella at the point $x = 1$, keep it till the point $x = 7$ (the total fatigue at $x = 7$ will be equal to $12$), throw it away, move on from $x = 7$ to $x = 8$ without an umbrella, take the third umbrella at $x = 8$ and keep it till the end (the total fatigue at $x = 10$ will be equal to $14$). In the second example the only possible strategy is to take the first umbrella, move with it till the point $x = 9$, throw it away and proceed without an umbrella 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. A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers. You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No. -----Constraints----- - A, B, and C are all integers between 1 and 9 (inclusive). -----Input----- Input is given from Standard Input in the following format: A B C -----Output----- If the given triple is poor, print Yes; otherwise, print No. -----Sample Input----- 5 7 5 -----Sample Output----- Yes A and C are equal, but B is different from those two numbers, so this triple is poor. 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 country decides to build a palace. In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius. There are N places proposed for the place. The elevation of Place i is H_i meters. Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there. Print the index of the place where the palace should be built. It is guaranteed that the solution is unique. -----Constraints----- - 1 \leq N \leq 1000 - 0 \leq T \leq 50 - -60 \leq A \leq T - 0 \leq H_i \leq 10^5 - All values in input are integers. - The solution is unique. -----Input----- Input is given from Standard Input in the following format: N T A H_1 H_2 ... H_N -----Output----- Print the index of the place where the palace should be built. -----Sample Input----- 2 12 5 1000 2000 -----Sample Output----- 1 - The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius. - The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius. Thus, the palace should be built at Place 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. You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries: - Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring? -----Notes----- A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T. For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC. -----Constraints----- - 2 \leq N \leq 10^5 - 1 \leq Q \leq 10^5 - S is a string of length N. - Each character in S is A, C, G or T. - 1 \leq l_i < r_i \leq N -----Input----- Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q -----Output----- Print Q lines. The i-th line should contain the answer to the i-th query. -----Sample Input----- 8 3 ACACTACG 3 7 2 3 1 8 -----Sample Output----- 2 0 3 - Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring. - Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring. - Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring. 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 shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.) Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits. -----Constraints----- - 1 \leq K \leq N \leq 1000 - 1 \leq p_i \leq 1000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N K p_1 p_2 \ldots p_N -----Output----- Print an integer representing the minimum possible total price of fruits. -----Sample Input----- 5 3 50 100 80 120 80 -----Sample Output----- 210 This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively. The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5. 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. X and A are integers between 0 and 9 (inclusive). If X is less than A, print 0; if X is not less than A, print 10. -----Constraints----- - 0 \leq X, A \leq 9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: X A -----Output----- If X is less than A, print 0; if X is not less than A, print 10. -----Sample Input----- 3 5 -----Sample Output----- 0 3 is less than 5, so we should print 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. Given any integer x, Aoki can do the operation below. Operation: Replace x with the absolute difference of x and K. You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times. -----Constraints----- - 0 ≀ N ≀ 10^{18} - 1 ≀ K ≀ 10^{18} - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N K -----Output----- Print the minimum possible value taken by N after Aoki does the operation zero or more times. -----Sample Input----- 7 4 -----Sample Output----- 1 Initially, N=7. After one operation, N becomes |7-4| = 3. After two operations, N becomes |3-4| = 1, which is the minimum value taken by 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. Given are three integers A_1, A_2, and A_3. If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win. -----Constraints----- - 1 \leq A_i \leq 13 \ \ (i=1,2,3) - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A_1 A_2 A_3 -----Output----- If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win. -----Sample Input----- 5 7 9 -----Sample Output----- win 5+7+9=21, so print 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. You are given a positive integer N. Find the minimum positive integer divisible by both 2 and N. -----Constraints----- - 1 \leq N \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the minimum positive integer divisible by both 2 and N. -----Sample Input----- 3 -----Sample Output----- 6 6 is divisible by both 2 and 3. Also, there is no positive integer less than 6 that is divisible by both 2 and 3. Thus, the answer is 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.