question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November l6, l974. The message consisted of l679 bits and was meant to be translated to a rectangular picture with 23 × 73 pixels. Since both 23 and 73 are prime numbers, 23 × 73 is the unique possible size of the translated rectangular picture each edge of which is longer than l pixel. Of course, there was no guarantee that the receivers would try to translate the message to a rectangular picture. Even if they would, they might put the pixels into the rectangle incorrectly. The senders of the Arecibo message were optimistic. We are planning a similar project. Your task in the project is to find the most suitable width and height of the translated rectangular picture. The term ``most suitable'' is defined as follows. An integer m greater than 4 is given. A positive fraction a/b less than or equal to 1 is also given. The area of the picture should not be greater than m. Both of the width and the height of the translated picture should be prime numbers. The ratio of the width to the height should not be less than a/b nor greater than 1. You should maximize the area of the picture under these constraints. In other words, you will receive an integer m and a fraction a/b . It holds that m > 4 and 0 < a/b ≤ 1 . You should find the pair of prime numbers p, q such that pq ≤ m and a/b ≤ p/q ≤ 1 , and furthermore, the product pq takes the maximum value among such pairs of two prime numbers. You should report p and q as the "most suitable" width and height of the translated picture. Input The input is a sequence of at most 2000 triplets of positive integers, delimited by a space character in between. Each line contains a single triplet. The sequence is followed by a triplet of zeros, 0 0 0, which indicates the end of the input and should not be treated as data to be processed. The integers of each input triplet are the integer m, the numerator a, and the denominator b described above, in this order. You may assume 4 < m < 100000 and 1 ≤ a ≤ b ≤ 1000. Output The output is a sequence of pairs of positive integers. The i-th output pair corresponds to the i-th input triplet. The integers of each output pair are the width p and the height q described above, in this order. Each output line contains a single pair. A space character is put between the integers as a delimiter. No other characters should appear in the output. Example Input 5 1 2 99999 999 999 1680 5 16 1970 1 1 2002 4 11 0 0 0 Output 2 2 313 313 23 73 43 43 37 53 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. Write a function that takes an arbitrary number of strings and interlaces them (combines them by alternating characters from each string). For example `combineStrings('abc', '123')` should return `'a1b2c3'`. If the strings are different lengths the function should interlace them until each string runs out, continuing to add characters from the remaining strings. For example `combineStrings('abcd', '123')` should return `'a1b2c3d'`. The function should take any number of arguments and combine them. For example `combineStrings('abc', '123', '£$%')` should return `'a1£b2$c3%'`. **Note: if only one argument is passed return only that string. If no arguments are passed return an empty string.** 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. Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two. 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 group of people played a game. All players had distinct scores, which are positive integers. Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i. Find the maximum possible number of players in the game. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9(1\leq i\leq N) * 0 \leq B_i \leq 10^9(1\leq i\leq N) * If i ≠ j, A_i ≠ A_j. * There exists a possible outcome of the game that are consistent with the facts. * All input values are integers. Inputs Input is given from Standard Input in the following format: N A_1 B_1 : A_N B_N Outputs Print the maximum possible number of players in the game. Examples Input 3 4 7 2 9 6 2 Output 8 Input 5 1 10 3 6 5 2 4 4 2 8 Output 7 Input 2 1 1000000000 1000000000 1 Output 1000000001 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 image with a height of H pixels and a width of W pixels. Each of the pixels is represented by either . or *. The character representing the pixel at the i-th row from the top and the j-th column from the left, is denoted by C_{i,j}. Extend this image vertically so that its height is doubled. That is, print a image with a height of 2H pixels and a width of W pixels where the pixel at the i-th row and j-th column is equal to C_{(i+1)/2,j} (the result of division is rounded down). -----Constraints----- - 1≦H, W≦100 - C_{i,j} is either . or *. -----Input----- The input is given from Standard Input in the following format: H W C_{1,1}...C_{1,W} : C_{H,1}...C_{H,W} -----Output----- Print the extended image. -----Sample Input----- 2 2 *. .* -----Sample Output----- *. *. .* .* Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Witua is a little student from the University of Lviv. He enjoys studying math. Witua knows a lot of famous mathematicians like Eratosthenes, Pythagoras, Fermat, Diophantus, Furko, Gauss and so on. However, his favorite one is Euler. The only thing Witua likes more than Euler is Euler’s totient function φ. He is exploring the nature of this function. One of the steps of his work is finding φ(i)/i for all 2≤i≤N. He doesn’t need to know every such value, but Witua wonders for what value i, is φ(i)/i the maximum he can get? Help little student to find such i that φ(i)/i is maximum among all the 2≤i≤N. -----Input----- The first line contains single integer T - the number of test cases. Each of the next T lines contains a single integer N. -----Output----- For every test case output i such that φ(i)/i is maximum among all i (2≤i≤N) in a separate line. -----Constrains----- T (1≤T≤500 ) N(2≤N≤10^18) -----Example----- Input: 3 2 3 4 Output: 2 3 3 Explanationφ(2)/2=1/2 φ(3)/3=2/3 φ(4)/4=2/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. There is a double-track line (up and down are separate lines and pass each other everywhere). There are 11 stations on this line, including the terminal station, and each station is called by the section number shown in the figure. <image> Trains depart from both terminal stations on this line at the same time and run without stopping along the way. Create a program that reads the length of each section and the speed of the two trains and outputs the number of the section where the trains pass each other in each case. However, if you pass each other just at the station, the smaller number of the section numbers on both sides will be output. In addition, the length of the train and the length of the station cannot be ignored. Input Multiple datasets are given. Each dataset is given in the following format. l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, v1, v2 li (1 ≤ li ≤ 2,000) is an integer representing the length (km) of the interval i. v1 is the speed of the train departing from the terminal station on the section 1 side (km / h), and v2 is the speed of the train departing from the terminal station on the section 10 side (km / h) (1 ≤ v1, v2) ≤ 2,000). The number of datasets does not exceed 50. Output For each data set, the number of the section where the train passes is output on one line. Example Input 1,1,1,1,1,1,1,1,1,1,40,60 1,1,1,1,1,3,3,3,3,3,50,50 10,10,10,10,10,10,10,10,10,10,50,49 Output 4 7 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. Read problems statements in Mandarin Chinese and Russian. You are given a multiset of N integers. Please find such a nonempty subset of it that the sum of the subset's elements is divisible by N. Otherwise, state that this subset doesn't exist. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test consists of a single integer N - the size of the multiset. The second line of each test contains N single space separated integers - the multiset's elements. ------ Output ------ For each test case output: -1 if the required subset doesn't exist If the required subset exists, output two lines. Output the size of the subset on the first line and output the list of indices of the multiset's element that form the required subset. Of course, any number can be taken in the subset no more than once. If there are several such subsets, you can output any. ------ Constraints ------ $1 ≤ The sum of N over all the test cases ≤ 10^{5}$ $Each element of the multiset is a positive integer, not exceeding 10^{9}.$ $1 ≤ N ≤ 15 : 37 points. $ $1 ≤ N ≤ 1000 : 24 points.$ $1 ≤ N ≤ 10^{5} : 39 points. $ ------ Example ------ Input: 1 3 4 6 10 Output: 1 2 ------ Explanation ------ We can pick {6} as the subset, then its sum is 6 and this is divisible by 3 - the size of the initial multiset. 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 coordinate line has n segments, the i-th segment starts at the position l_{i} and ends at the position r_{i}. We will denote such a segment as [l_{i}, r_{i}]. You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1. Formally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a ≤ c ≤ d ≤ b. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ 10^9) — the borders of the i-th segment. It is guaranteed that no two segments coincide. -----Output----- Print a single integer — the number of the segment that covers all other segments in the set. If there's no solution, print -1. The segments are numbered starting from 1 in the order in which they appear in the input. -----Examples----- Input 3 1 1 2 2 3 3 Output -1 Input 6 1 5 2 3 1 10 7 10 7 7 10 10 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. In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. -----Constraints----- - a, b, c, d, e and k are integers between 0 and 123 (inclusive). - a < b < c < d < e -----Input----- Input is given from Standard Input in the following format: a b c d e k -----Output----- Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair. -----Sample Input----- 1 2 4 8 9 15 -----Sample Output----- Yay! In this case, there is no pair of antennas that cannot communicate directly, because: - the distance between A and B is 2 - 1 = 1 - the distance between A and C is 4 - 1 = 3 - the distance between A and D is 8 - 1 = 7 - the distance between A and E is 9 - 1 = 8 - the distance between B and C is 4 - 2 = 2 - the distance between B and D is 8 - 2 = 6 - the distance between B and E is 9 - 2 = 7 - the distance between C and D is 8 - 4 = 4 - the distance between C and E is 9 - 4 = 5 - the distance between D and E is 9 - 8 = 1 and none of them is greater than 15. Thus, the correct output is Yay!. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma. The method takes in an array of strings and returns a single formatted string. Empty string values should be ignored. Empty arrays or null/nil values being passed into the method should result in an empty string being returned. ```Python format_words(['ninja', 'samurai', 'ronin']) # should return "ninja, samurai and ronin" format_words(['ninja', '', 'ronin']) # should return "ninja and ronin" format_words([]) # should return "" ``` ```Haskell formatWords ["ninja", "samurai", "ronin"] -- should return "ninja, samurai and ronin" formatWords ["ninja", "", "ronin"] -- should return "ninja and ronin" formatWords [] -- should return "" ``` 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 at the lesson Vitya learned a very interesting function — mex. Mex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0. Vitya quickly understood all tasks of the teacher, but can you do the same? You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps: * Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x. * Find mex of the resulting array. Note that after each query the array changes. Input First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries. Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array. Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105). Output For each query print the answer on a separate line. Examples Input 2 2 1 3 1 3 Output 1 0 Input 4 3 0 1 5 6 1 2 4 Output 2 0 0 Input 5 4 0 1 5 6 7 1 1 4 5 Output 2 2 0 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. As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest. The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.). Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.). Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum. Input The first line contains an integer n (1 ≤ n ≤ 100) — the number of the problems. The next line contains n space-separated integers ai (1 ≤ ai ≤ 720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem. Output Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy. Examples Input 3 30 330 720 Output 2 10 Note In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist. Competitions by the given rules are held annually on the site http://b23.ru/3wvc 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 unlimited number of coins with values $1, 2, \ldots, n$. You want to select some set of coins having the total value of $S$. It is allowed to have multiple coins with the same value in the set. What is the minimum number of coins required to get sum $S$? -----Input----- The only line of the input contains two integers $n$ and $S$ ($1 \le n \le 100\,000$, $1 \le S \le 10^9$) -----Output----- Print exactly one integer — the minimum number of coins required to obtain sum $S$. -----Examples----- Input 5 11 Output 3 Input 6 16 Output 3 -----Note----- In the first example, some of the possible ways to get sum $11$ with $3$ coins are: $(3, 4, 4)$ $(2, 4, 5)$ $(1, 5, 5)$ $(3, 3, 5)$ It is impossible to get sum $11$ with less than $3$ coins. In the second example, some of the possible ways to get sum $16$ with $3$ coins are: $(5, 5, 6)$ $(4, 6, 6)$ It is impossible to get sum $16$ with less than $3$ coins. 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. Ivan has n different boxes. The first of them contains some balls of n different colors. Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i. In order to do this, Ivan will make some turns. Each turn he does the following: Ivan chooses any non-empty box and takes all balls from this box; Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3. The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes. Help Ivan to determine the minimum possible penalty of the game! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of boxes and colors. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is the number of balls with color i. -----Output----- Print one number — the minimum possible penalty of the game. -----Examples----- Input 3 1 2 3 Output 6 Input 4 2 3 4 5 Output 19 -----Note----- In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6. In the second example you make two turns: Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 — to the fourth box and the rest put back into the first box. Penalty is 14; Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 — to the second box. Penalty is 5. Total penalty is 19. 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. How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"? The answer can be enormous, so print it modulo (10^9+7). -----Constraints----- - K is an integer between 1 and 10^6 (inclusive). - S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: K S -----Output----- Print the number of strings satisfying the condition, modulo (10^9+7). -----Sample Input----- 5 oof -----Sample Output----- 575111451 For example, we can obtain proofend, moonwolf, and onionpuf, while we cannot obtain oofsix, oofelevennn, voxafolt, or fooooooo. 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. Your boss decided to save money by purchasing some cut-rate optical character recognition software for scanning in the text of old novels to your database. At first it seems to capture words okay, but you quickly notice that it throws in a lot of numbers at random places in the text. For example: ```python string_clean('! !') == '! !' string_clean('123456789') == '' string_clean('This looks5 grea8t!') == 'This looks great!' ``` Your harried co-workers are looking to you for a solution to take this garbled text and remove all of the numbers. Your program will take in a string and clean out all numeric characters, and return a string with spacing and special characters `~#$%^&!@*():;"'.,?` all intact. 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. After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar. Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants. Formally, Alyona wants to find a sequence of k non-empty strings p_1, p_2, p_3, ..., p_{k} satisfying following conditions: s can be represented as concatenation a_1p_1a_2p_2... a_{k}p_{k}a_{k} + 1, where a_1, a_2, ..., a_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); t can be represented as concatenation b_1p_1b_2p_2... b_{k}p_{k}b_{k} + 1, where b_1, b_2, ..., b_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); sum of the lengths of strings in sequence is maximum possible. Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence. A substring of a string is a subsequence of consecutive characters of the string. -----Input----- In the first line of the input three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10) are given — the length of the string s, the length of the string t and Alyona's favourite number respectively. The second line of the input contains string s, consisting of lowercase English letters. The third line of the input contains string t, consisting of lowercase English letters. -----Output----- In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence. It is guaranteed, that at least one desired sequence exists. -----Examples----- Input 3 2 2 abc ab Output 2 Input 9 12 4 bbaaababb abbbabbaaaba Output 7 -----Note----- The following image describes the answer for the second sample case: [Image] 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. Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color t_{i}. For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant. There are $\frac{n \cdot(n + 1)}{2}$ non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of balls. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ n) where t_{i} is the color of the i-th ball. -----Output----- Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color. -----Examples----- Input 4 1 2 1 2 Output 7 3 0 0 Input 3 1 1 1 Output 6 0 0 -----Note----- In the first sample, color 2 is dominant in three intervals: An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4, 4] contains one ball, with color 2 again. An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of 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. You are given an array $a$ consisting of $n$ integers. Initially all elements of $a$ are either $0$ or $1$. You need to process $q$ queries of two kinds: 1 x : Assign to $a_x$ the value $1 - a_x$. 2 k : Print the $k$-th largest value of the array. As a reminder, $k$-th largest value of the array $b$ is defined as following: Sort the array in the non-increasing order, return $k$-th element from it. For example, the second largest element in array $[0, 1, 0, 1]$ is $1$, as after sorting in non-increasing order it becomes $[1, 1, 0, 0]$, and the second element in this array is equal to $1$. -----Input----- The first line contains two integers $n$ and $q$ ($1 \le n, q \le 10^5$) — the length of the given array and the number of queries. The second line contains $n$ integers $a_1, a_2, a_3, \dots, a_n$ ($0 \le a_i \le 1$) — elements of the initial array. Each of the following $q$ lines contains two integers. The first integer is $t$ ($1 \le t \le 2$) — the type of query. If $t = 1$ the second integer is $x$ ($1 \le x \le n$) — the position of the modified number. You have to assign to $a_x$ the value $1 - a_x$. If $t = 2$ the second integer is $k$ ($1 \le k \le n$) — you need to print the $k$-th largest value of the array. It's guaranteed that there will be at least one query of the second type (satisfying $t = 2$). -----Output----- For each query of the second type, print a single integer — the answer to the query. -----Examples----- Input 5 5 1 1 0 1 0 2 3 1 2 2 3 2 1 2 5 Output 1 0 1 0 -----Note----- Initially $a = [1, 1, 0, 1, 0]$. The first operation is printing the third largest value, which is $1$. The second operation is assigning $a_2$ the value $0$, $a$ becomes $[1, 0, 0, 1, 0]$. The third operation is printing the third largest value, it is $0$. The fourth operation is printing the first largest value, it is $1$. The last operation is printing the fifth largest value, it is $0$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction. Let us define a function f(r, c) as follows: - f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above) Given are integers r_1, r_2, c_1, and c_2. Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7). -----Constraints----- - 1 ≤ r_1 ≤ r_2 ≤ 10^6 - 1 ≤ c_1 ≤ c_2 ≤ 10^6 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: r_1 c_1 r_2 c_2 -----Output----- Print the sum of f(i, j) modulo (10^9+7). -----Sample Input----- 1 1 2 2 -----Sample Output----- 14 For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2. Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14. 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. Mislove had an array $a_1$, $a_2$, $\cdots$, $a_n$ of $n$ positive integers, but he has lost it. He only remembers the following facts about it: The number of different numbers in the array is not less than $l$ and is not greater than $r$; For each array's element $a_i$ either $a_i = 1$ or $a_i$ is even and there is a number $\dfrac{a_i}{2}$ in the array. For example, if $n=5$, $l=2$, $r=3$ then an array could be $[1,2,2,4,4]$ or $[1,1,1,1,2]$; but it couldn't be $[1,2,2,4,8]$ because this array contains $4$ different numbers; it couldn't be $[1,2,2,3,3]$ because $3$ is odd and isn't equal to $1$; and it couldn't be $[1,1,2,2,16]$ because there is a number $16$ in the array but there isn't a number $\frac{16}{2} = 8$. According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array. -----Input----- The only input line contains three integers $n$, $l$ and $r$ ($1 \leq n \leq 1\,000$, $1 \leq l \leq r \leq \min(n, 20)$) — an array's size, the minimal number and the maximal number of distinct elements in an array. -----Output----- Output two numbers — the minimal and the maximal possible sums of all elements in an array. -----Examples----- Input 4 2 2 Output 5 7 Input 5 1 5 Output 5 31 -----Note----- In the first example, an array could be the one of the following: $[1,1,1,2]$, $[1,1,2,2]$ or $[1,2,2,2]$. In the first case the minimal sum is reached and in the last case the maximal sum is reached. In the second example, the minimal sum is reached at the array $[1,1,1,1,1]$, and the maximal one is reached at the array $[1,2,4,8,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. Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n ⋅ k cities. The cities are numerated from 1 to n ⋅ k, the distance between the neighboring cities is exactly 1 km. Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km. Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions. Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s. Input The first line contains two integers n and k (1 ≤ n, k ≤ 100 000) — the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively. The second line contains two integers a and b (0 ≤ a, b ≤ k/2) — the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively. Output Print the two integers x and y. Examples Input 2 3 1 1 Output 1 6 Input 3 2 0 0 Output 1 3 Input 1 10 5 3 Output 5 5 Note In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6. In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3. In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Phoenix is playing with a new puzzle, which consists of $n$ identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below. A puzzle piece The goal of the puzzle is to create a square using the $n$ pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all $n$ pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it? -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains an integer $n$ ($1 \le n \le 10^9$) — the number of puzzle pieces. -----Output----- For each test case, if Phoenix can create a square with the $n$ puzzle pieces, print YES. Otherwise, print NO. -----Examples----- Input 3 2 4 6 Output YES YES NO -----Note----- For $n=2$, Phoenix can create a square like this: For $n=4$, Phoenix can create a square like this: For $n=6$, it is impossible for Phoenix to create a square. 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. Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it. Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it a_{k}) and delete it, at that all elements equal to a_{k} + 1 and a_{k} - 1 also must be deleted from the sequence. That step brings a_{k} points to the player. Alex is a perfectionist, so he decided to get as many points as possible. Help him. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) that shows how many numbers are in Alex's sequence. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). -----Output----- Print a single integer — the maximum number of points that Alex can earn. -----Examples----- Input 2 1 2 Output 2 Input 3 1 2 3 Output 4 Input 9 1 2 1 3 2 2 2 2 3 Output 10 -----Note----- Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 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. A word or a sentence in some language is called a pangram if all the characters of the alphabet of this language appear in it at least once. Pangrams are often used to demonstrate fonts in printing or test the output devices. You are given a string consisting of lowercase and uppercase Latin letters. Check whether this string is a pangram. We say that the string contains a letter of the Latin alphabet if this letter occurs in the string in uppercase or lowercase. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the number of characters in the string. The second line contains the string. The string consists only of uppercase and lowercase Latin letters. -----Output----- Output "YES", if the string is a pangram and "NO" otherwise. -----Examples----- Input 12 toosmallword Output NO Input 35 TheQuickBrownFoxJumpsOverTheLazyDog Output YES Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning. The balls are numbered from 1 to n (by the demand of the inventory commission). [Image] Figure 1. The initial position for n = 5. After receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped. [Image] Figure 2. The example of a throw. In this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2. Note that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the "ball order". The ball order is a sequence of n numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise. -----Input----- The first line contains a single number n — the number of students in the line and the number of balls. The next line contains exactly n space-separated integers. Each number corresponds to a student in the line (the i-th number corresponds to the i-th from the left student in the line) and shows the number of throws he can participate in. The input limits for scoring 30 points are (subproblem D1): 1 ≤ n ≤ 10. The input limits for scoring 70 points are (subproblems D1+D2): 1 ≤ n ≤ 500. The input limits for scoring 100 points are (subproblems D1+D2+D3): 1 ≤ n ≤ 1000000. -----Output----- The output should contain a single integer — the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (10^9 + 7). -----Examples----- Input 5 1 2 2 1 2 Output 120 Input 8 1 2 2 1 2 1 1 2 Output 16800 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. Where do odds begin, and where do they end? Where does hope emerge, and will they ever break? Given an integer sequence a_1, a_2, ..., a_{n} of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers. A subsegment is a contiguous slice of the whole sequence. For example, {3, 4, 5} and {1} are subsegments of sequence {1, 2, 3, 4, 5, 6}, while {1, 2, 4} and {7} are not. -----Input----- The first line of input contains a non-negative integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains n space-separated non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 100) — the elements of the sequence. -----Output----- Output "Yes" if it's possible to fulfill the requirements, and "No" otherwise. You can output each letter in any case (upper or lower). -----Examples----- Input 3 1 3 5 Output Yes Input 5 1 0 1 5 1 Output Yes Input 3 4 3 1 Output No Input 4 3 9 9 3 Output No -----Note----- In the first example, divide the sequence into 1 subsegment: {1, 3, 5} and the requirements will be met. In the second example, divide the sequence into 3 subsegments: {1, 0, 1}, {5}, {1}. In the third example, one of the subsegments must start with 4 which is an even number, thus the requirements cannot be met. In the fourth example, the sequence can be divided into 2 subsegments: {3, 9, 9}, {3}, but this is not a valid solution because 2 is an even 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. The campus has $m$ rooms numbered from $0$ to $m - 1$. Also the $x$-mouse lives in the campus. The $x$-mouse is not just a mouse: each second $x$-mouse moves from room $i$ to the room $i \cdot x \mod{m}$ (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the $x$-mouse is unknown. You are responsible to catch the $x$-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the $x$-mouse enters a trapped room, it immediately gets caught. And the only observation you made is $\text{GCD} (x, m) = 1$. -----Input----- The only line contains two integers $m$ and $x$ ($2 \le m \le 10^{14}$, $1 \le x < m$, $\text{GCD} (x, m) = 1$) — the number of rooms and the parameter of $x$-mouse. -----Output----- Print the only integer — minimum number of traps you need to install to catch the $x$-mouse. -----Examples----- Input 4 3 Output 3 Input 5 2 Output 2 -----Note----- In the first example you can, for example, put traps in rooms $0$, $2$, $3$. If the $x$-mouse starts in one of this rooms it will be caught immediately. If $x$-mouse starts in the $1$-st rooms then it will move to the room $3$, where it will be caught. In the second example you can put one trap in room $0$ and one trap in any other room since $x$-mouse will visit all rooms $1..m-1$ if it will start in any of these rooms. 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 decide to develop a game with your friends. The title of the game is "Battle Town". This is a game with the theme of urban warfare with tanks. As the first step in game development, we decided to develop the following prototype. In this prototype, the only tanks that appear are the player's tanks, not the enemy tanks. The player's tank makes various actions on the map according to the player's input. The specifications for tank operation are described below. The components of the map are shown in Table 1. Tanks can only move on level ground in the map. Table 1: Map Components Characters | Elements --- | --- `.` | Flat ground `*` | Brick wall `#` | Iron wall `-` | Water `^` | Tank (upward) `v` | Tank (downward) `<` | Tank (facing left) `>` | Tank (facing right) Player input is given as a string of characters. Table 2 shows the operations corresponding to each character. Table 2: Actions for player input Characters | Actions --- | --- `U` | Up: Turn the tank upwards, and if the next higher square is flat, move to that square. `D` | Down: Turn the tank downwards, and if the next lower square is flat, move to that square. `L` | Left: Turn the tank to the left, and if the square to the left is flat, move to that square. `R` | Right: Turn the tank to the right, and if the square to the right is flat, move to that square. `S` | Shoot: Fire a shell in the direction the tank is currently facing The shell hits a brick or iron wall or goes straight until it is off the map. If it hits a brick wall, the shell disappears and the brick wall turns flat. If it hits an iron wall, the shell disappears, but the iron wall does not change. When you go out of the map, the shell disappears. Your job is to create a program that outputs the final map state given the initial map and the player's input operation sequence. Input The first line of input is given the number T (0 <T ≤ 100), which represents the number of datasets. This line is followed by T datasets. The first line of each dataset is given the integers H and W, which represent the height and width of the map, separated by a single space character. These integers satisfy 2 ≤ H ≤ 20, 2 ≤ W ≤ 20. The following H line represents the initial state of the map. Each line is a string of length W. All the characters on the map are defined in Table 1 and include exactly one tank in total. The line following the map is given the length N of the input operation column (0 <N ≤ 100), and the next line is given the string of length N representing the input operation column. The input operation string is a character string consisting only of the characters defined in Table 2. Output For each dataset, output the map after all input operations in the same format as the input map. Output one blank line between the datasets. Be careful not to output extra blank lines after the last dataset. Example Input 4 4 6 *.*..* *..... ..-... ^.*#.. 10 SRSSRRUSSR 2 2 <. .. 12 DDSRRSUUSLLS 3 5 >-#** .-*#* .-**# 15 SSSDRSSSDRSSSUU 5 5 v**** ***** ***** ***** ***** 44 SSSSDDRSDRSRDSULUURSSSSRRRRDSSSSDDLSDLSDLSSD Output *....* ...... ..-... ..>#.. <. .. ^-#** .-.#* .-..# ..... .***. ..*.. ..*.. ....v Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you. Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers. Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 12$) — the number of pairs the first participant communicated to the second and vice versa. The second line contains $n$ pairs of integers, each between $1$ and $9$, — pairs of numbers communicated from first participant to the second. The third line contains $m$ pairs of integers, each between $1$ and $9$, — pairs of numbers communicated from the second participant to the first. All pairs within each set are distinct (in particular, if there is a pair $(1,2)$, there will be no pair $(2,1)$ within the same set), and no pair contains the same number twice. It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number. -----Output----- If you can deduce the shared number with certainty, print that number. If you can with certainty deduce that both participants know the shared number, but you do not know it, print $0$. Otherwise print $-1$. -----Examples----- Input 2 2 1 2 3 4 1 5 3 4 Output 1 Input 2 2 1 2 3 4 1 5 6 4 Output 0 Input 2 3 1 2 4 5 1 2 1 3 2 3 Output -1 -----Note----- In the first example the first participant communicated pairs $(1,2)$ and $(3,4)$, and the second communicated $(1,5)$, $(3,4)$. Since we know that the actual pairs they received share exactly one number, it can't be that they both have $(3,4)$. Thus, the first participant has $(1,2)$ and the second has $(1,5)$, and at this point you already know the shared number is $1$. In the second example either the first participant has $(1,2)$ and the second has $(1,5)$, or the first has $(3,4)$ and the second has $(6,4)$. In the first case both of them know the shared number is $1$, in the second case both of them know the shared number is $4$. You don't have enough information to tell $1$ and $4$ apart. In the third case if the first participant was given $(1,2)$, they don't know what the shared number is, since from their perspective the second participant might have been given either $(1,3)$, in which case the shared number is $1$, or $(2,3)$, in which case the shared number is $2$. While the second participant does know the number with certainty, neither you nor the first participant do, so the output 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. There are two rectangles. The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B. The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D. Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area. -----Constraints----- - All input values are integers. - 1≤A≤10^4 - 1≤B≤10^4 - 1≤C≤10^4 - 1≤D≤10^4 -----Input----- The input is given from Standard Input in the following format: A B C D -----Output----- Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area. -----Sample Input----- 3 5 2 7 -----Sample Output----- 15 The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14. Thus, the output should be 15, the larger area. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: - If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. - If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. -----Constraints----- - 1 \leq N \leq 500 - 3 \leq C \leq 30 - 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) - 1 \leq c_{i,j} \leq C - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} -----Output----- If the minimum possible sum of the wrongness of all the squares is x, print x. -----Sample Input----- 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 -----Sample Output----- 3 - Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1. - Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1. - Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1. In this case, the sum of the wrongness of all the squares is 3. Note that D_{i,j} \neq D_{j,i} is possible. 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 directed graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i. G does not contain directed cycles. Find the length of the longest directed path in G. Here, the length of a directed path is the number of edges in it. Constraints * All values in input are integers. * 2 \leq N \leq 10^5 * 1 \leq M \leq 10^5 * 1 \leq x_i, y_i \leq N * All pairs (x_i, y_i) are distinct. * G does not contain directed cycles. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_M y_M Output Print the length of the longest directed path in G. Examples Input 4 5 1 2 1 3 3 2 2 4 3 4 Output 3 Input 6 3 2 3 4 5 5 6 Output 2 Input 5 8 5 3 2 3 2 4 5 2 5 1 1 4 4 3 1 3 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. There is a set A = \\{ a_1, a_2, \ldots, a_N \\} consisting of N positive integers. Taro and Jiro will play the following game against each other. Initially, we have a pile consisting of K stones. The two players perform the following operation alternately, starting from Taro: * Choose an element x in A, and remove exactly x stones from the pile. A player loses when he becomes unable to play. Assuming that both players play optimally, determine the winner. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq K \leq 10^5 * 1 \leq a_1 < a_2 < \cdots < a_N \leq K Input Input is given from Standard Input in the following format: N K a_1 a_2 \ldots a_N Output If Taro will win, print `First`; if Jiro will win, print `Second`. Examples Input 2 4 2 3 Output First Input 2 5 2 3 Output Second Input 2 7 2 3 Output First Input 3 20 1 2 3 Output Second Input 3 21 1 2 3 Output First Input 1 100000 1 Output Second 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 is the easy version of the problem. The only difference is that in this version q = 1. You can make hacks only if both versions of the problem are solved. There is a process that takes place on arrays a and b of length n and length n-1 respectively. The process is an infinite sequence of operations. Each operation is as follows: * First, choose a random integer i (1 ≤ i ≤ n-1). * Then, simultaneously set a_i = min\left(a_i, \frac{a_i+a_{i+1}-b_i}{2}\right) and a_{i+1} = max\left(a_{i+1}, \frac{a_i+a_{i+1}+b_i}{2}\right) without any rounding (so values may become non-integer). See notes for an example of an operation. It can be proven that array a converges, i. e. for each i there exists a limit a_i converges to. Let function F(a, b) return the value a_1 converges to after a process on a and b. You are given array b, but not array a. However, you are given a third array c. Array a is good if it contains only integers and satisfies 0 ≤ a_i ≤ c_i for 1 ≤ i ≤ n. Your task is to count the number of good arrays a where F(a, b) ≥ x for q values of x. Since the number of arrays can be very large, print it modulo 10^9+7. Input The first line contains a single integer n (2 ≤ n ≤ 100). The second line contains n integers c_1, c_2 …, c_n (0 ≤ c_i ≤ 100). The third line contains n-1 integers b_1, b_2, …, b_{n-1} (0 ≤ b_i ≤ 100). The fourth line contains a single integer q (q=1). The fifth line contains q space separated integers x_1, x_2, …, x_q (-10^5 ≤ x_i ≤ 10^5). Output Output q integers, where the i-th integer is the answer to the i-th query, i. e. the number of good arrays a where F(a, b) ≥ x_i modulo 10^9+7. Example Input 3 2 3 4 2 1 1 -1 Output 56 Note The following explanation assumes b = [2, 1] and c=[2, 3, 4] (as in the sample). Examples of arrays a that are not good: * a = [3, 2, 3] is not good because a_1 > c_1; * a = [0, -1, 3] is not good because a_2 < 0. One possible good array a is [0, 2, 4]. We can show that no operation has any effect on this array, so F(a, b) = a_1 = 0. Another possible good array a is [0, 1, 4]. In a single operation with i = 1, we set a_1 = min((0+1-2)/(2), 0) and a_2 = max((0+1+2)/(2), 1). So, after a single operation with i = 1, a becomes equal to [-1/2, 3/2, 4]. We can show that no operation has any effect on this array, so F(a, b) = -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. Blackjack is a type of card game played in casinos, where the game is played using cards with numbers from 1 to 13. The score of each card is decided as follows. * 1 is 1 point or 11 points * From 2 to 9, the score is as written. * 10 points from 10 to 13 There are several participants in this game, including parents, each with several sets of cards. This set of cards is called a hand. The hand score is the total of the card scores. The calculation shall be performed as follows. * If the total score of the cards is greater than 21, the score of the hand is set to 0. * As the score of the card, 1 may be calculated as 1 point or 11 points, but the one with the maximum hand score shall be selected. Create a program that uses the information of the cards dealt as input and outputs the score of the hand. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: c1 c2 ... cn The integer ci (1 ≤ ci ≤ 13) written on the i-th card is given to each line, separated by blanks. The number of cards n does not exceed 100. The number of datasets does not exceed 200. Output The score of the hand is output to one line for each data set. Example Input 1 7 7 7 7 7 8 12 1 10 1 1 0 Output 11 21 0 21 12 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 new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. Input The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n. Output Output n integers. For each trip, print the sum the passenger is charged after it. Examples Input 3 10 20 30 Output 20 20 10 Input 10 13 45 46 60 103 115 126 150 256 516 Output 20 20 10 0 20 0 0 20 20 10 Note In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only. 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 Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers. Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it? Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers ai, bi is no less than 1 and no more than 109. Output Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start. Examples Input 4 1 7 2 3 8 1 1 3 Output 2 2 4 Input 8 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 Output 8 1 2 3 4 5 6 7 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. Create a function that takes a string and an integer (`n`). The function should return a string that repeats the input string `n` number of times. If anything other than a string is passed in you should return `"Not a string"` ## Example ``` "Hi", 2 --> "HiHi" 1234, 5 --> "Not a string" ``` 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 loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters' case does not matter, that is an uppercase letter is considered equivalent to the corresponding lowercase letter. Help Petya perform the comparison. Input Each of the first two lines contains a bought string. The strings' lengths range from 1 to 100 inclusive. It is guaranteed that the strings are of the same length and also consist of uppercase and lowercase Latin letters. Output If the first string is less than the second one, print "-1". If the second string is less than the first one, print "1". If the strings are equal, print "0". Note that the letters' case is not taken into consideration when the strings are compared. Examples Input aaaa aaaA Output 0 Input abs Abz Output -1 Input abcdefg AbCdEfF Output 1 Note If you want more formal information about the lexicographical order (also known as the "dictionary order" or "alphabetical order"), you can visit the following site: * http://en.wikipedia.org/wiki/Lexicographical_order Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alice and Bob play a game. They have a binary string $s$ (a string such that each character in it is either $0$ or $1$). Alice moves first, then Bob, then Alice again, and so on. During their move, the player can choose any number (not less than one) of consecutive equal characters in $s$ and delete them. For example, if the string is $10110$, there are $6$ possible moves (deleted characters are bold): $\textbf{1}0110 \to 0110$; $1\textbf{0}110 \to 1110$; $10\textbf{1}10 \to 1010$; $101\textbf{1}0 \to 1010$; $10\textbf{11}0 \to 100$; $1011\textbf{0} \to 1011$. After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: $10\textbf{11}0 \to 1\textbf{00} \to 1$. The game ends when the string becomes empty, and the score of each player is the number of $1$-characters deleted by them. Each player wants to maximize their score. Calculate the resulting score of Alice. -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) — the number of test cases. Each test case contains exactly one line containing a binary string $s$ ($1 \le |s| \le 100$). -----Output----- For each test case, print one integer — the resulting score of Alice (the number of $1$-characters deleted by her). -----Example----- Input 5 01111001 0000 111111 101010101 011011110111 Output 4 0 6 3 6 -----Note----- Questions about the optimal strategy will be ignored. 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. 'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of the i-th question is a_{i} points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price. The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again. All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve. -----Input----- The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100; m ≤ min(n, 30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the prices of the questions. The third line contains m distinct integers b_{i} (1 ≤ b_{i} ≤ n) — the numbers of auction questions. Assume that the questions are numbered from 1 to n. -----Output----- In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type. -----Examples----- Input 4 1 1 3 7 5 3 Output 18 Input 3 2 10 3 8 2 3 Output 40 Input 2 2 100 200 1 2 Output 400 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 text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters. Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be". An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c". What is the minimum length of the text after at most one abbreviation? -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 300$) — the number of words in the text. The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $10^5$. -----Output----- Print one integer — the minimum length of the text after at most one abbreviation. -----Examples----- Input 6 to be or not to be Output 12 Input 10 a ab a a b ab a a b c Output 13 Input 6 aa bb aa aa bb bb Output 11 -----Note----- In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb". 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 was gifted an array $a$ of length $n$. Polycarp considers an array beautiful if there exists a number $C$, such that each number in the array occurs either zero or $C$ times. Polycarp wants to remove some elements from the array $a$ to make it beautiful. For example, if $n=6$ and $a = [1, 3, 2, 1, 4, 2]$, then the following options are possible to make the array $a$ array beautiful: Polycarp removes elements at positions $2$ and $5$, array $a$ becomes equal to $[1, 2, 1, 2]$; Polycarp removes elements at positions $1$ and $6$, array $a$ becomes equal to $[3, 2, 1, 4]$; Polycarp removes elements at positions $1, 2$ and $6$, array $a$ becomes equal to $[2, 1, 4]$; Help Polycarp determine the minimum number of elements to remove from the array $a$ to make it beautiful. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer — the minimum number of elements that Polycarp has to remove from the array $a$ to make it beautiful. -----Examples----- Input 3 6 1 3 2 1 4 2 4 100 100 4 100 8 1 2 3 3 3 2 6 6 Output 2 1 2 -----Note----- None 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 string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string. 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. Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'. Constraints * 1 ≤ H ≤ 300 * 1 ≤ W ≤ 300 Input The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space. The input ends with two 0 (when both H and W are zero). Output For each dataset, print the rectangle made of H × W '#'. Print a blank line after each dataset. Example Input 3 4 5 6 2 2 0 0 Output #### #### #### ###### ###### ###### ###### ###### ## ## Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an undirected graph consisting of $n$ vertices and $n$ edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph. Your task is to calculate the number of simple paths of length at least $1$ in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). For example, paths $[1, 2, 3]$ and $[3, 2, 1]$ are considered the same. You have to answer $t$ independent test cases. Recall that a path in the graph is a sequence of vertices $v_1, v_2, \ldots, v_k$ such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of vertices (and the number of edges) in the graph. The next $n$ lines of the test case describe 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$), where $u_i$ and $v_i$ are vertices the $i$-th edge connects. For each pair of vertices $(u, v)$, there is at most one edge between $u$ and $v$. There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i. e. all its edges are bidirectional. The graph is connected, i. e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph. It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print one integer: the number of simple paths of length at least $1$ in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). -----Examples----- Input 3 3 1 2 2 3 1 3 4 1 2 2 3 3 4 4 2 5 1 2 2 3 1 3 2 5 4 3 Output 6 11 18 -----Note----- Consider the second test case of the example. It looks like that: There are $11$ different simple paths: $[1, 2]$; $[2, 3]$; $[3, 4]$; $[2, 4]$; $[1, 2, 4]$; $[1, 2, 3]$; $[2, 3, 4]$; $[2, 4, 3]$; $[3, 2, 4]$; $[1, 2, 3, 4]$; $[1, 2, 4, 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. Chef loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Chef has a positive integer N. He can apply any of the following operations as many times as he want in any order: - Add 1 to the number N. - Take some digit of N and replace it by any non-zero digit. - Add any non-zero leading digit to N. Find the minimum number of operations that is needed for changing N to the lucky number. -----Input----- The first line contains a single positive integer T, the number of test cases. T test cases follow. The only line of each test case contains a positive integer N without leading zeros. -----Output----- For each T test cases print one integer, the minimum number of operations that is needed for changing N to the lucky number. -----Constraints----- 1 ≤ T ≤ 10 1 ≤ N < 10100000 -----Example----- Input: 3 25 46 99 Output: 2 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. G: Tree problem Given a tree consisting of N vertices. Each vertex of the tree is numbered from 1 to N. Of the N-1 edges, the i \ (= 1, 2, ..., N-1) edge connects the vertex u_i and the vertex v_i. Write a program to find the number of K non-empty subgraph sets of this tree, each of which is concatenated and no two different subgraphs share vertices. However, the answer can be very large, so answer the remainder divided by 998244353. Note that if the set of K subgraphs is the same, the ones with different order of K subgraphs are also equated. Input format N K u_1 v_1 :: u_ {N-1} v_ {N-1} Constraint * 2 \ leq N \ leq 10 ^ {5} * 1 \ leq K \ leq min (N, 300) * 1 \ leq u_i, v_i \ leq N * u_i \ neq v_i * For i \ neq j (u_i, v_i) \ neq (u_j, v_j) * All inputs are given as integers. * The graph given is guaranteed to be a tree. Output format Print the integer that represents the answer on one line. Note that it prints too much divided by 998244353. Input example 1 3 2 1 2 13 Output example 1 Five There are five ways: * \\ {1 \\} and \\ {2 \\} * \\ {1 \\} and \\ {3 \\} * \\ {2 \\} and \\ {3 \\} * \\ {1, 2 \\} and \\ {3 \\} * \\ {1, 3 \\} and \\ {2 \\} Input example 2 4 4 1 2 13 14 Output example 2 1 There is only one way (\\ {1 \\}, \\ {2 \\}, \\ {3 \\}, \\ {4 \\}). Input example 3 7 4 1 7 twenty one 7 4 3 4 5 7 6 3 Output example 3 166 Example Input 3 2 1 2 1 3 Output 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. Polycarp has an integer $n$ that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length $l$ (in other words, $l$ leftmost digits) of $n$. So, the leftmost digit is swapped with the $l$-th digit from the left, the second digit from the left swapped with ($l-1$)-th left, etc. For example, if $n=123456789$ and $l=5$, then the new value of $n$ will be $543216789$. Note that for different operations, the values of $l$ can be different. The number $l$ can be equal to the length of the number $n$ — in this case, the whole number $n$ is reversed. Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible. Help Polycarp. Determine the minimum number of operations he needs to perform with the number $n$ to make it even or determine that this is impossible. You need to answer $t$ independent test cases. -----Input----- The first line contains the number $t$ ($1 \le t \le 10^4$) — the number of test cases. Each of the following $t$ lines contains one integer $n$ ($1 \le n < 10^9$). It is guaranteed that the given number doesn't contain the digit 0. -----Output----- Print $t$ lines. On each line print one integer — the answer to the corresponding test case. If it is impossible to make an even number, print -1. -----Examples----- Input 4 3876 387 4489 3 Output 0 2 1 -1 -----Note----- In the first test case, $n=3876$, which is already an even number. Polycarp doesn't need to do anything, so the answer is $0$. In the second test case, $n=387$. Polycarp needs to do $2$ operations: Select $l=2$ and reverse the prefix $\underline{38}7$. The number $n$ becomes $837$. This number is odd. Select $l=3$ and reverse the prefix $\underline{837}$. The number $n$ becomes $738$. This number is even. It can be shown that $2$ is the minimum possible number of operations that Polycarp needs to do with his number to make it even. In the third test case, $n=4489$. Polycarp can reverse the whole number (choose a prefix of length $l=4$). It will become $9844$ and this is an even number. In the fourth test case, $n=3$. No matter how hard Polycarp tried, he would not be able to make an even 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. $n$ boys and $m$ girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from $1$ to $n$ and all girls are numbered with integers from $1$ to $m$. For all $1 \leq i \leq n$ the minimal number of sweets, which $i$-th boy presented to some girl is equal to $b_i$ and for all $1 \leq j \leq m$ the maximal number of sweets, which $j$-th girl received from some boy is equal to $g_j$. More formally, let $a_{i,j}$ be the number of sweets which the $i$-th boy give to the $j$-th girl. Then $b_i$ is equal exactly to the minimum among values $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ and $g_j$ is equal exactly to the maximum among values $b_{1,j}, b_{2,j}, \ldots, b_{n,j}$. You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of $a_{i,j}$ for all $(i,j)$ such that $1 \leq i \leq n$ and $1 \leq j \leq m$. You are given the numbers $b_1, \ldots, b_n$ and $g_1, \ldots, g_m$, determine this number. -----Input----- The first line contains two integers $n$ and $m$, separated with space — the number of boys and girls, respectively ($2 \leq n, m \leq 100\,000$). The second line contains $n$ integers $b_1, \ldots, b_n$, separated by spaces — $b_i$ is equal to the minimal number of sweets, which $i$-th boy presented to some girl ($0 \leq b_i \leq 10^8$). The third line contains $m$ integers $g_1, \ldots, g_m$, separated by spaces — $g_j$ is equal to the maximal number of sweets, which $j$-th girl received from some boy ($0 \leq g_j \leq 10^8$). -----Output----- If the described situation is impossible, print $-1$. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied. -----Examples----- Input 3 2 1 2 1 3 4 Output 12 Input 2 2 0 1 1 0 Output -1 Input 2 3 1 0 1 1 2 Output 4 -----Note----- In the first test, the minimal total number of sweets, which boys could have presented is equal to $12$. This can be possible, for example, if the first boy presented $1$ and $4$ sweets, the second boy presented $3$ and $2$ sweets and the third boy presented $1$ and $1$ sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $12$. In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied. In the third test, the minimal total number of sweets, which boys could have presented is equal to $4$. This can be possible, for example, if the first boy presented $1$, $1$, $2$ sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $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. # Task You are given a binary string (a string consisting of only '1' and '0'). The only operation that can be performed on it is a Flip operation. It flips any binary character ( '0' to '1' and vice versa) and all characters to the `right` of it. For example, applying the Flip operation to the 4th character of string "1001010" produces the "1000101" string, since all characters from the 4th to the 7th are flipped. Your task is to find the minimum number of flips required to convert the binary string to string consisting of all '0'. # Example For `s = "0101"`, the output should be `3`. It's possible to convert the string in three steps: ``` "0101" -> "0010" ^^^ "0010" -> "0001" ^^ "0001" -> "0000" ^ ``` # Input/Output - `[input]` string `s` A binary string. - `[output]` an integer The minimum number of flips required. 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. Daniel is organizing a football tournament. He has come up with the following tournament format: In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be $\frac{x \cdot(x - 1)}{2}$ games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games. Daniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^18), the number of games that should be played. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Output----- Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1. -----Examples----- Input 3 Output 3 4 Input 25 Output 20 Input 2 Output -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. ## Euler's Method We want to calculate the shape of an unknown curve which starts at a given point with a given slope. This curve satisfies an ordinary differential equation (ODE): ```math \frac{dy}{dx} = f(x, y);\\ y(x_0) = y_0 ``` The starting point `$ A_0 (x_0, y_0) $` is known as well as the slope to the curve at `$ A_0 $` and then the tangent line at `$ A_0 $` . Take a small step along that tangent line up to a point `$ A_1 $`. Along this small step, the slope does not change too much, so `$ A_1 $` will be close to the curve. If we suppose that `$ A_1 $` is close enough to the curve, the same reasoning as for the point `$ A_1 $` above can be used for other points. After several steps, a polygonal curve `$ A_0, A_1, ..., A_n $` is computed. The error between the two curves will be small if the step is small. We define points `$ A_0, A_1, A_2, ..., A_n $` whose x-coordinates are `$ x_0, x_1, ..., x_n $` and y-coordinates are such that `$ y_{k+1} = y_k + f(x_k, y_k) \times h $` where `$ h $` is the common step. If `$ T $` is the length `$ x_n - x_0 $` we have `$ h = T/n $`. ## Task For this kata we will focus on the following differential equation: ```math \frac{dy}{dx} = 2 - e^{-4x} - 2y; \\ A_0 = (0,1) ``` with `$ x ∈ [0, 1] $`. We will then take a uniform partition of the region of `$ x $` between `$ 0 $` and `$ 1 $` and split it into `$ n $` + 1 sections. `$ n $` will be the input to the function `ex_euler(n)` and since `$ T $` is always 1, `$ h = 1/n $`. We know that an exact solution is ```math y = 1 + 0.5e^{-4x} - 0.5e^{-2x}. ``` For each `$ x_k $` we are able to calculate the `$ y_k $` as well as the values `$ z_k $` of the exact solution. Our task is, for a given number `$ n $` of steps, to return the mean (*truncated* to 6 decimal places) of the relative errors between the `$ y_k $` (our aproximation) and the `$ z_k $` (the exact solution). For that we can use: error in `$ A_k = abs(y_k - z_k) / z_k $` and then the mean is sum(errors in `$ A_k $`)/ (`$ n $` + 1) ## Examples ```python ex_euler(10) should return: 0.026314 (truncated from 0.026314433214799246) with Y = [1.0,0.9..., 0.85..., 0.83..., 0.83..., 0.85..., 0.86..., 0.88..., 0.90..., 0.91..., 0.93...] Z = [1.0, 0.9..., 0.88..., 0.87..., 0.87..., 0.88..., 0.89..., 0.90..., 0.91..., 0.93..., 0.94...] Relative errors = [0.0, 0.02..., 0.04..., 0.04..., 0.04..., 0.03..., 0.03..., 0.02..., 0.01..., 0.01..., 0.01...] ``` `ex_euler(17)` should return: `0.015193 (truncated from 0.015193336263370796)`. As expected, as `$ n $` increases, our error reduces. ### Links and graphs Wiki article ![alternative text](http://i.imgur.com/vjK7edl.png) Below comparison between approximation (red curve) and exact solution(blue curve) for n=100: ![alternative text](http://i.imgur.com/KufSkYEm.png) 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. Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions: take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color; take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color. She repeats this process until there is only one card left. What are the possible colors for the final card? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards. The next line contains a string s of length n — the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively. -----Output----- Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order. -----Examples----- Input 2 RB Output G Input 3 GRG Output BR Input 5 BBBBB Output B -----Note----- In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card. In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card. In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards. 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. > Kyoto University Programming Contest is a programming contest voluntarily held by some Kyoto University students. This contest is abbreviated as Kyoto University Programming Contest and called KUPC. > > source: Kyoto University Programming Contest Information The problem-preparing committee met to hold this year's KUPC and N problems were proposed there. The problems are numbered from 1 to N and the name of i-th problem is P_i. However, since they proposed too many problems, they decided to divide them into some sets for several contests. They decided to divide this year's KUPC into several KUPCs by dividing problems under the following conditions. * One KUPC provides K problems. * Each problem appears at most once among all the KUPCs. * All the first letters of the problem names in one KUPC must be different. You, one of the committee members, want to hold as many KUPCs as possible. Write a program to find the maximum number of KUPCs that can be held this year. Constraints * 1 \leq N \leq 10^4 * 1 \leq K \leq 26 * 1 \leq |P_i| \leq 10 * All characters in P_i are capital letters. Note that, for each i and j (1 \leq i < j \leq N), P_i \neq P_j are not necessarily satisfied. Input The input is given from Standard Input in the following format: N K P_1 : P_N Output Print the maximum number of KUPCs that can be held on one line. Examples Input 9 3 APPLE ANT ATCODER BLOCK BULL BOSS CAT DOG EGG Output 3 Input 3 2 KU KYOUDAI KYOTOUNIV 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. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are: * 232 * 110011 * 54322345 Complete the function to test if the given number (`num`) **can be rearranged** to form a numerical palindrome or not. Return a boolean (`true` if it can be rearranged to a palindrome, and `false` if it cannot). Return `"Not valid"` if the input is not an integer or is less than 0. For this kata, single digit numbers are **NOT** considered numerical palindromes. ## Examples ``` 5 => false 2121 => true 1331 => true 3357665 => true 1294 => false "109982" => "Not valid" -42 => "Not valid" ``` 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 lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (10^9 + 7). -----Input----- The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on. -----Output----- In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 1 1 Output 1 Input 4 2 1 4 Output 2 Input 11 2 4 8 Output 6720 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. After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market. This morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weight w_{i} and cost c_{i}. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than m, and total cost is maximum possible. Help Petya to determine maximum possible total cost. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 300000) — the number of Petya's souvenirs and total weight that he can carry to the market. Then n lines follow. ith line contains two integers w_{i} and c_{i} (1 ≤ w_{i} ≤ 3, 1 ≤ c_{i} ≤ 10^9) — the weight and the cost of ith souvenir. -----Output----- Print one number — maximum possible total cost of souvenirs that Petya can carry to the market. -----Examples----- Input 1 1 2 1 Output 0 Input 2 2 1 3 2 2 Output 3 Input 4 3 3 10 2 7 2 8 1 1 Output 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. Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n. Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number). The seller can obtain array b from array a if the following conditions hold: b_{i} > 0; 0 ≤ a_{i} - b_{i} ≤ k for all 1 ≤ i ≤ n. Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain). -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 3·10^5; 1 ≤ k ≤ 10^6). The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — array a. -----Output----- In the single line print a single number — the maximum possible beauty of the resulting array. -----Examples----- Input 6 1 3 6 10 12 13 16 Output 3 Input 5 3 8 21 52 15 77 Output 7 -----Note----- In the first sample we can obtain the array: 3 6 9 12 12 15 In the second sample we can obtain the next array: 7 21 49 14 77 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. Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters. Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with. Input The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character. Output If there is no way of replacing '#' characters which leads to a beautiful string print - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with. If there are several possible answers, you may output any of them. Examples Input (((#)((#) Output 1 2 Input ()((#((#(#() Output 2 2 1 Input # Output -1 Input (#) Output -1 Note |s| denotes the length of the string s. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One Martian boy called Zorg wants to present a string of beads to his friend from the Earth — Masha. He knows that Masha likes two colours: blue and red, — and right in the shop where he has come, there is a variety of adornments with beads of these two colours. All the strings of beads have a small fastener, and if one unfastens it, one might notice that all the strings of beads in the shop are of the same length. Because of the peculiarities of the Martian eyesight, if Zorg sees one blue-and-red string of beads first, and then the other with red beads instead of blue ones, and blue — instead of red, he regards these two strings of beads as identical. In other words, Zorg regards as identical not only those strings of beads that can be derived from each other by the string turnover, but as well those that can be derived from each other by a mutual replacement of colours and/or by the string turnover. It is known that all Martians are very orderly, and if a Martian sees some amount of objects, he tries to put them in good order. Zorg thinks that a red bead is smaller than a blue one. Let's put 0 for a red bead, and 1 — for a blue one. From two strings the Martian puts earlier the string with a red bead in the i-th position, providing that the second string has a blue bead in the i-th position, and the first two beads i - 1 are identical. At first Zorg unfastens all the strings of beads, and puts them into small heaps so, that in each heap strings are identical, in his opinion. Then he sorts out the heaps and chooses the minimum string in each heap, in his opinion. He gives the unnecassary strings back to the shop assistant and says he doesn't need them any more. Then Zorg sorts out the remaining strings of beads and buys the string with index k. All these manupulations will take Zorg a lot of time, that's why he asks you to help and find the string of beads for Masha. Input The input file contains two integers n and k (2 ≤ n ≤ 50;1 ≤ k ≤ 1016) —the length of a string of beads, and the index of the string, chosen by Zorg. Output Output the k-th string of beads, putting 0 for a red bead, and 1 — for a blue one. If it s impossible to find the required string, output the only number -1. Examples Input 4 4 Output 0101 Note Let's consider the example of strings of length 4 — 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110. Zorg will divide them into heaps: {0001, 0111, 1000, 1110}, {0010, 0100, 1011, 1101}, {0011, 1100}, {0101, 1010}, {0110, 1001}. Then he will choose the minimum strings of beads in each heap: 0001, 0010, 0011, 0101, 0110. The forth string — 0101. 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. Along a road running in an east-west direction, there are A shrines and B temples. The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road. Answer the following Q queries: - Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.) -----Constraints----- - 1 \leq A, B \leq 10^5 - 1 \leq Q \leq 10^5 - 1 \leq s_1 < s_2 < ... < s_A \leq 10^{10} - 1 \leq t_1 < t_2 < ... < t_B \leq 10^{10} - 1 \leq x_i \leq 10^{10} - s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B Q s_1 : s_A t_1 : t_B x_1 : x_Q -----Output----- Print Q lines. The i-th line should contain the answer to the i-th query. -----Sample Input----- 2 3 4 100 600 400 900 1000 150 2000 899 799 -----Sample Output----- 350 1400 301 399 There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road. - Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple. - Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine. - Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine. - Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple. 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 this kata we want to convert a string into an integer. The strings simply represent the numbers in words. Examples: * "one" => 1 * "twenty" => 20 * "two hundred forty-six" => 246 * "seven hundred eighty-three thousand nine hundred and nineteen" => 783919 Additional Notes: * The minimum number is "zero" (inclusively) * The maximum number, which must be supported is 1 million (inclusively) * The "and" in e.g. "one hundred and twenty-four" is optional, in some cases it's present and in others it's not * All tested numbers are valid, you don't need to validate 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. Example Input 7 >> Output 7 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once. The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches. Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. -----Input----- The first line contains two integers — n and k (1 ≤ n, k ≤ 1000). -----Output----- In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}). The numbers a_{i} and b_{i} mean, that in the i-th match the team with number a_{i} won against the team with number b_{i}. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1. -----Examples----- Input 3 1 Output 3 1 2 2 3 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. Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d_1 meter long road between his house and the first shop and a d_2 meter long road between his house and the second shop. Also, there is a road of length d_3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house. [Image] Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled. -----Input----- The first line of the input contains three integers d_1, d_2, d_3 (1 ≤ d_1, d_2, d_3 ≤ 10^8) — the lengths of the paths. d_1 is the length of the path connecting Patrick's house and the first shop; d_2 is the length of the path connecting Patrick's house and the second shop; d_3 is the length of the path connecting both shops. -----Output----- Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house. -----Examples----- Input 10 20 30 Output 60 Input 1 1 5 Output 4 -----Note----- The first sample is shown on the picture in the problem statement. One of the optimal routes is: house $\rightarrow$ first shop $\rightarrow$ second shop $\rightarrow$ house. In the second sample one of the optimal routes is: house $\rightarrow$ first shop $\rightarrow$ house $\rightarrow$ second shop $\rightarrow$ house. 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. On a random day, Neko found $n$ treasure chests and $m$ keys. The $i$-th chest has an integer $a_i$ written on it and the $j$-th key has an integer $b_j$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible. The $j$-th key can be used to unlock the $i$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $a_i + b_j \equiv 1 \pmod{2}$. One key can be used to open at most one chest, and one chest can be opened at most once. Find the maximum number of chests Neko can open. -----Input----- The first line contains integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) — the number of chests and the number of keys. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the numbers written on the treasure chests. The third line contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \leq b_i \leq 10^9$) — the numbers written on the keys. -----Output----- Print the maximum number of chests you can open. -----Examples----- Input 5 4 9 14 6 2 11 8 4 7 20 Output 3 Input 5 1 2 4 6 8 10 5 Output 1 Input 1 4 10 20 30 40 50 Output 0 -----Note----- In the first example, one possible way to unlock $3$ chests is as follows: Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice). In the third example, no key can unlock the given chest. 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. Polycarpus just has been out of luck lately! As soon as he found a job in the "Binary Cat" cafe, the club got burgled. All ice-cream was stolen. On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character "+" in his notes. Similarly, each time a visitor left the club, Polycarpus put character "-" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended. Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times. -----Input----- The only line of the input contains a sequence of characters "+" and "-", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive. -----Output----- Print the sought minimum number of people -----Examples----- Input +-+-+ Output 1 Input --- 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. Mr. Morishita was in trouble ... I had Mr. Anehara write a program, but the program crashed. Mr. Anehara wrote a program that reads the formula in Reverse Polish Notation and outputs the calculation result. According to the crash log, it seems that the cause was that it was divided by 0. This may be because Mr. Morishita entered the wrong formula, or maybe there is a bug in the program written by Mr. Anehara. I thought I'd read the program written by Mr. Anehara, but it seems that Mr. Anehara's program is written in an unfamiliar word called assembly, and just looking at it makes my head pound. So Morishita decided to ask you to write a program to check if the formula was wrong. An incorrect formula means that if you calculate according to that formula, you may end up dividing by zero. The code written by Mr. Anehara runs on a very sieve computer, and addition, subtraction, multiplication and division are performed with integers, and the result is saved as an 8-bit unsigned integer. For example, `255 + 1` will be 0 and` 3 / 2` will be 1. Oh, it's not in Anehara-san. (Reference) Pseudo code to calculate the formula expressed in Reverse Polish Notation s = empty stack n = number of elements in the expression for i in 1..n: The i-th element of the if expression is an integer: Push that integer to s The i-th element of the if expression is the variable: Push the value of that variable to s The i-th element of the if expression is the operator: Pop a value from s and let it be b Pop a value from s and make it a if operator is'+': Let r = (a + b)% 256 the if operator is'-': r = (a --b + 256)% 256 if operator is'*': r = (a * b)% 256 the if operator is'/': Let r = (a / b)% 256 Push r to s Pop the value from s and use it as the calculation result of the formula Input > m > name1 lb1 ub1 > ... > namem lbm ubm > n > e1 e2 ... en > 0 ≤ m ≤ 100 0 ≤ lbi ≤ ubi ≤ 255 Length of 1 ≤ namei (1 ≤ i ≤ m) ≤ 20 1 ≤ n ≤ 100 m is the number of variables, and namei, lbi, and ubi are the names, lower bounds, and upper bounds of the variables i, respectively. n represents the number of elements contained in the expression and ei represents the i-th element of the expression. Each variable appears at most once in the expression. Output If the expression is incorrect, output `error`, and if it is not incorrect, output` correct` on one line. Examples Input 1 a 1 10 3 10 a / Output correct Input 2 a 1 10 b 1 10 5 1 a b - / Output error Input 1 a 0 255 7 1 a 2 * 1 + / Output 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. A class of students wrote a multiple-choice test. There are $n$ students in the class. The test had $m$ questions, each of them had $5$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $i$ worth $a_i$ points. Incorrect answers are graded with zero points. The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n, m \le 1000$) — the number of students in the class and the number of questions in the test. Each of the next $n$ lines contains string $s_i$ ($|s_i| = m$), describing an answer of the $i$-th student. The $j$-th character represents the student answer (A, B, C, D or E) on the $j$-th question. The last line contains $m$ integers $a_1, a_2, \ldots, a_m$ ($1 \le a_i \le 1000$) — the number of points for the correct answer for every question. -----Output----- Print a single integer — the maximum possible total score of the class. -----Examples----- Input 2 4 ABCD ABCE 1 2 3 4 Output 16 Input 3 3 ABC BCD CDE 5 4 12 Output 21 -----Note----- In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be $16$. In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is $5 + 4 + 12 = 21$. 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. After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you. You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers. Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total. Write a code that gives out the total amount for different days(d). 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 a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there. Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who were asked to sell, say, exactly 0.273549107 beer barrels. To deal with the problem somehow, the King issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this: * If a number's integer part does not end with digit 9 and its fractional part is strictly less than 0.5, then the rounded up number coincides with the number’s integer part. * If a number's integer part does not end with digit 9 and its fractional part is not less than 0.5, the rounded up number is obtained if we add 1 to the last digit of the number’s integer part. * If the number’s integer part ends with digit 9, to round up the numbers one should go to Vasilisa the Wise. In the whole Kingdom she is the only one who can perform the tricky operation of carrying into the next position. Merchants found the algorithm very sophisticated and they asked you (the ACMers) to help them. Can you write a program that would perform the rounding according to the King’s order? Input The first line contains a single number to round up — the integer part (a non-empty set of decimal digits that do not start with 0 — with the exception of a case when the set consists of a single digit — in this case 0 can go first), then follows character «.» (a dot), and then follows the fractional part (any non-empty set of decimal digits). The number's length does not exceed 1000 characters, including the dot. There are no other characters in the input data. Output If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message "GOTO Vasilisa." (without the quotes). Examples Input 0.0 Output 0 Input 1.49 Output 1 Input 1.50 Output 2 Input 2.71828182845904523536 Output 3 Input 3.14159265358979323846 Output 3 Input 12345678901234567890.1 Output 12345678901234567890 Input 123456789123456789.999 Output GOTO Vasilisa. 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 three multisets of pairs of colored sticks: $R$ pairs of red sticks, the first pair has length $r_1$, the second pair has length $r_2$, $\dots$, the $R$-th pair has length $r_R$; $G$ pairs of green sticks, the first pair has length $g_1$, the second pair has length $g_2$, $\dots$, the $G$-th pair has length $g_G$; $B$ pairs of blue sticks, the first pair has length $b_1$, the second pair has length $b_2$, $\dots$, the $B$-th pair has length $b_B$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color. Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks. What is the maximum area you can achieve? -----Input----- The first line contains three integers $R$, $G$, $B$ ($1 \le R, G, B \le 200$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $R$ integers $r_1, r_2, \dots, r_R$ ($1 \le r_i \le 2000$) — the lengths of sticks in each pair of red sticks. The third line contains $G$ integers $g_1, g_2, \dots, g_G$ ($1 \le g_i \le 2000$) — the lengths of sticks in each pair of green sticks. The fourth line contains $B$ integers $b_1, b_2, \dots, b_B$ ($1 \le b_i \le 2000$) — the lengths of sticks in each pair of blue sticks. -----Output----- Print the maximum possible total area of the constructed rectangles. -----Examples----- Input 1 1 1 3 5 4 Output 20 Input 2 1 3 9 5 1 2 8 5 Output 99 Input 10 1 1 11 7 20 15 19 14 2 4 13 14 8 11 Output 372 -----Note----- In the first example you can construct one of these rectangles: red and green with sides $3$ and $5$, red and blue with sides $3$ and $4$ and green and blue with sides $5$ and $4$. The best area of them is $4 \times 5 = 20$. In the second example the best rectangles are: red/blue $9 \times 8$, red/blue $5 \times 5$, green/blue $2 \times 1$. So the total area is $72 + 25 + 2 = 99$. In the third example the best rectangles are: red/green $19 \times 8$ and red/blue $20 \times 11$. The total area is $152 + 220 = 372$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. 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 celebrate the opening of the Winter Computer School the organizers decided to buy in n liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly a bottles 0.5 in volume, b one-liter bottles and c of two-liter ones. The organizers have enough money to buy any amount of cola. What did cause the heated arguments was how many bottles of every kind to buy, as this question is pivotal for the distribution of cola among the participants (and organizers as well). Thus, while the organizers are having the argument, discussing different variants of buying cola, the Winter School can't start. Your task is to count the number of all the possible ways to buy exactly n liters of cola and persuade the organizers that this number is too large, and if they keep on arguing, then the Winter Computer School will have to be organized in summer. All the bottles of cola are considered indistinguishable, i.e. two variants of buying are different from each other only if they differ in the number of bottles of at least one kind. Input The first line contains four integers — n, a, b, c (1 ≤ n ≤ 10000, 0 ≤ a, b, c ≤ 5000). Output Print the unique number — the solution to the problem. If it is impossible to buy exactly n liters of cola, print 0. Examples Input 10 5 5 5 Output 9 Input 3 0 0 2 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. Converting a 24-hour time like "0830" or "2030" to a 12-hour time (like "8:30 am" or "8:30 pm") sounds easy enough, right? Well, let's see if you can do it! You will have to define a function named "to12hourtime", and you will be given a four digit time string (in "hhmm" format) as input. Your task is to return a 12-hour time string in the form of "h:mm am" or "h:mm pm". (Of course, the "h" part will be two digits if the hour is greater than 9.) If you like this kata, try converting 12-hour time to 24-hour time: https://www.codewars.com/kata/converting-12-hour-time-to-24-hour-time/train/python Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method. TV series have n seasons (numbered 1 through n), the i-th season has a_{i} episodes (numbered 1 through a_{i}). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs! -----Input----- The first line contains one integer n (1 ≤ n ≤ 2·10^5) — the number of seasons. The second line contains n integers separated by space a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — number of episodes in each season. -----Output----- Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x. -----Examples----- Input 5 1 2 3 4 5 Output 0 Input 3 8 12 7 Output 3 Input 3 3 2 1 Output 2 -----Note----- Possible pairs in the second example: x = 1, y = 2 (season 1 episode 2 [Image] season 2 episode 1); x = 2, y = 3 (season 2 episode 3 [Image] season 3 episode 2); x = 1, y = 3 (season 1 episode 3 [Image] season 3 episode 1). In the third example: x = 1, y = 2 (season 1 episode 2 [Image] season 2 episode 1); x = 1, y = 3 (season 1 episode 3 [Image] season 3 episode 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 permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 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. Hey CodeWarrior, we've got a lot to code today! I hope you know the basic string manipulation methods, because this kata will be all about them. Here we go... ## Background We've got a very long string, containing a bunch of User IDs. This string is a listing, which seperates each user ID with a comma and a whitespace ("' "). Sometimes there are more than only one whitespace. Keep this in mind! Futhermore, some user Ids are written only in lowercase, others are mixed lowercase and uppercase characters. Each user ID starts with the same 3 letter "uid", e.g. "uid345edj". But that's not all! Some stupid student edited the string and added some hashtags (#). User IDs containing hashtags are invalid, so these hashtags should be removed! ## Task 1. Remove all hashtags 2. Remove the leading "uid" from each user ID 3. Return an array of strings --> split the string 4. Each user ID should be written in only lowercase characters 5. Remove leading and trailing whitespaces --- ## Note Even if this kata can be solved by using Regex or Linq, please try to find a solution by using only C#'s string class. Some references for C#: - [Microsoft MDSN: Trim](https://msdn.microsoft.com/de-de/library/t97s7bs3%28v=vs.110%29.aspx) - [Microsoft MSDN: Split](https://msdn.microsoft.com/de-de/library/tabh47cf%28v=vs.110%29.aspx) - [Microsoft MSDN: ToLower](https://msdn.microsoft.com/en-us/library/system.string.tolower%28v=vs.110%29.aspx) - [Microsoft MSDN: Replace](https://msdn.microsoft.com/de-de/library/fk49wtc1%28v=vs.110%29.aspx) - [Microsoft MSDN: Substring](https://msdn.microsoft.com/de-de/library/aka44szs%28v=vs.110%29.aspx) 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. Consider an N \times N matrix. Let us denote by a_{i, j} the entry in the i-th row and j-th column. For a_{i, j} where i=1 or j=1 holds, its value is one of 0, 1 and 2 and given in the input. The remaining entries are defined as follows: - a_{i,j} = \mathrm{mex}(a_{i-1,j}, a_{i,j-1}) (2 \leq i, j \leq N) where \mathrm{mex}(x, y) is defined by the following table:\mathrm{mex}(x, y)y=0y=1y=2x=0121x=1200x=2100 How many entries of the matrix are 0, 1, and 2, respectively? -----Constraints----- - 1 \leq N \leq 500{,}000 - a_{i,j}'s given in input are one of 0, 1 and 2. -----Input----- Input is given from Standard Input in the following format: N a_{1, 1} a_{1, 1} ... a_{1, N} a_{2, 1} : a_{N, 1} -----Output----- Print the number of 0's, 1's, and 2's separated by whitespaces. -----Sample Input----- 4 1 2 0 2 0 0 0 -----Sample Output----- 7 4 5 The matrix is as follows: 1 2 0 2 0 1 2 0 0 2 0 1 0 1 2 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. # One is the loneliest number ## Task The range of vision of a digit is its own value. `1` can see one digit to the left and one digit to the right,` 2` can see two digits, and so on. Thus, the loneliness of a digit `N` is the sum of the digits which it can see. Given a non-negative integer, your funtion must determine if there's at least one digit `1` in this integer such that its loneliness value is minimal. ## Example ``` number = 34315 ``` digit | can see on the left | can see on the right | loneliness --- | --- | --- | --- 3 | - | 431 | 4 + 3 + 1 = 8 4 | 3 | 315 | 3 + 3 + 1 + 5 = 12 3 | 34 | 15 | 3 + 4 + 1 + 5 = 13 1 | 3 | 5 | 3 + 5 = 8 5 | 3431 | - | 3 + 4 + 3 + 1 = 11 Is there a `1` for which the loneliness is minimal? Yes. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a set S of strings consisting of `0` and `1`, and an integer K. Find the longest string that is a subsequence of K or more different strings in S. If there are multiple strings that satisfy this condition, find the lexicographically smallest such string. Here, S is given in the format below: * The data directly given to you is an integer N, and N+1 strings X_0,X_1,...,X_N. For every i (0\leq i\leq N), the length of X_i is 2^i. * For every pair of two integers (i,j) (0\leq i\leq N,0\leq j\leq 2^i-1), the j-th character of X_i is `1` if and only if the binary representation of j with i digits (possibly with leading zeros) belongs to S. Here, the first and last characters in X_i are called the 0-th and (2^i-1)-th characters, respectively. * S does not contain a string with length N+1 or more. Here, a string A is a subsequence of another string B when there exists a sequence of integers t_1 < ... < t_{|A|} such that, for every i (1\leq i\leq |A|), the i-th character of A and the t_i-th character of B is equal. Constraints * 0 \leq N \leq 20 * X_i(0\leq i\leq N) is a string of length 2^i consisting of `0` and `1`. * 1 \leq K \leq |S| * K is an integer. Input Input is given from Standard Input in the following format: N K X_0 : X_N Output Print the lexicographically smallest string among the longest strings that are subsequences of K or more different strings in S. Examples Input 3 4 1 01 1011 01001110 Output 10 Input 4 6 1 01 1011 10111010 1101110011111101 Output 100 Input 2 5 0 11 1111 Output Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 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. T is playing a game with his friend, HL. There are n piles of stones, the i-th pile initially has a_i stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends. Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game. Input The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100). Output For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) Example Input 2 1 2 2 1 1 Output T HL Note In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. 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. Ziota found a video game called "Monster Invaders". Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns. For the sake of simplicity, we only consider two different types of monsters and three different types of guns. Namely, the two types of monsters are: a normal monster with $1$ hp. a boss with $2$ hp. And the three types of guns are: Pistol, deals $1$ hp in damage to one monster, $r_1$ reloading time Laser gun, deals $1$ hp in damage to all the monsters in the current level (including the boss), $r_2$ reloading time AWP, instantly kills any monster, $r_3$ reloading time The guns are initially not loaded, and the Ziota can only reload 1 gun at a time. The levels of the game can be considered as an array $a_1, a_2, \ldots, a_n$, in which the $i$-th stage has $a_i$ normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the $a_i$ normal monsters. If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level $i$ $(1 < i < n)$ are levels $i - 1$ and $i + 1$, the only adjacent level of level $1$ is level $2$, the only adjacent level of level $n$ is level $n - 1$). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with $d$ teleportation time. In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation. Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value. -----Input----- The first line of the input contains five integers separated by single spaces: $n$ $(2 \le n \le 10^6)$ — the number of stages, $r_1, r_2, r_3$ $(1 \le r_1 \le r_2 \le r_3 \le 10^9)$ — the reload time of the three guns respectively, $d$ $(1 \le d \le 10^9)$ — the time of moving between adjacent levels. The second line of the input contains $n$ integers separated by single spaces $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6, 1 \le i \le n)$. -----Output----- Print one integer, the minimum time to finish the game. -----Examples----- Input 4 1 3 4 3 3 2 5 1 Output 34 Input 4 2 4 4 1 4 5 1 2 Output 31 -----Note----- In the first test case, the optimal strategy is: Use the pistol to kill three normal monsters and AWP to kill the boss (Total time $1\cdot3+4=7$) Move to stage two (Total time $7+3=10$) Use the pistol twice and AWP to kill the boss (Total time $10+1\cdot2+4=16$) Move to stage three (Total time $16+3=19$) Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time $19+3+3=25$) Use the pistol once, use AWP to kill the boss (Total time $25+1\cdot1+4=30$) Move back to stage three (Total time $30+3=33$) Kill the boss at stage three with the pistol (Total time $33+1=34$) Note that here, we do not finish at level $n$, but when all the bosses are killed. 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. Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices. A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle. One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one. <image> Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces. Input The input is given in the following format. N p11 p12 p13 p21 p22 p23 :: pN1 pN2 pN3 The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3). Output Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line. Examples Input 4 1 3 2 1 2 4 1 4 3 2 3 4 Output 0 Input 6 1 3 2 1 2 4 1 4 3 2 3 4 3 2 1 2 3 1 Output 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. F: Miko Mi String- story Mikko Mikkomi ~! Everyone's idol, Miko Miko Tazawa! Today ~, with Mikoto ~, practice the string algorithm, let's do it ☆ Miko's special ~~ character making ~~ The slogan "MikoMikomi" becomes "MikoMikoMi" in Roman letters! In other words, if A = “Mi” and B = “Ko”, you can write in the form of ABABA! In this way, a character string that can be decomposed into the form of ABABA by properly determining A and B is called "Mikomi character string"! Miko is a popular person for everyone to become the name of a character string! In order for everyone to use the Mikomi character string, I decided to make a program to judge whether the given character string is the Mikomi character string, but I can't write a program longer than Miko and FizzBuzz. !! So ~, for Miko ~, I want you to write a program that judges Mikomi character strings ☆ ...... You said it's cold now! ?? problem A character string S consisting of uppercase and lowercase letters is given. Here, if there are two non-empty strings A and B that can be written as S = ABABA, then S is said to be a "Mikomi string". At this time, uppercase and lowercase letters of the alphabet shall be distinguished as different characters. Create a program that determines whether a given string is a string. Input format The input consists of only one line including the character string S. It can be assumed that S satisfies the following conditions. * 1 ≤ | S | ≤ 10 ^ 6. However, | S | represents the length of the character string S. * S consists only of uppercase or lowercase alphabets. Since the input may be very large, it is recommended to use a high-speed function to receive the input. Output format If S is a character string, output "Love AB!" For A and B that satisfy S = ABABA. However, if multiple pairs of A and B satisfy the condition, output the one with the smallest | AB |. If S is not a Mikomi string, output "mitomerarenaiWA". Input example 1 NicoNicoNi Output example 1 Love Nico! Input example 2 Kashikoi Kawaii Elichika Output example 2 mitomerarenaiWA Input example 3 LiveLiveL Output example 3 Love Live! Input example 4 AizunyanPeroPero Output example 4 mitomerarenaiWA Input example 5 AAAAAAAAAAAAAA Output example 5 Love AAAAA! Example Input NicoNicoNi Output Love Nico! 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 first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase. The second line of the input is an integer between 0 and 26, inclusive. -----Output----- Output the required string. -----Examples----- Input AprilFool 14 Output AprILFooL 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. ### Introduction and Warm-up (Highly recommended) ### [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays) ___ ## Task **_Given_** an *array/list [] of integers* , **_Find the product of the k maximal_** numbers. ___ ### Notes * **_Array/list_** size is *at least 3* . * **_Array/list's numbers_** *Will be* **_mixture of positives , negatives and zeros_** * **_Repetition_** of numbers in *the array/list could occur*. ___ ### Input >> Output Examples ``` maxProduct ({4, 3, 5}, 2) ==> return (20) ``` #### _Explanation_: * **_Since_** *the size (k) equal 2* , then **_the subsequence of size 2_** *whose gives* **_product of maxima_** is `5 * 4 = 20` . ___ ``` maxProduct ({8, 10 , 9, 7}, 3) ==> return (720) ``` #### _Explanation_: * **_Since_** *the size (k) equal 3* , then **_the subsequence of size 3_** *whose gives* **_product of maxima_** is ` 8 * 9 * 10 = 720` . ___ ``` maxProduct ({10, 8, 3, 2, 1, 4, 10}, 5) ==> return (9600) ``` #### _Explanation_: * **_Since_** *the size (k) equal 5* , then **_the subsequence of size 5_** *whose gives* **_product of maxima_** is ` 10 * 10 * 8 * 4 * 3 = 9600` . ___ ``` maxProduct ({-4, -27, -15, -6, -1}, 2) ==> return (4) ``` #### _Explanation_: * **_Since_** *the size (k) equal 2* , then **_the subsequence of size 2_** *whose gives* **_product of maxima_** is ` -4 * -1 = 4` . ___ ``` maxProduct ({10, 3, -1, -27} , 3) return (-30) ``` #### _Explanation_: * **_Since_** *the size (k) equal 3* , then **_the subsequence of size 3_** *whose gives* **_product of maxima_** is ` 10 * 3 * -1 = -30 ` . ___ ___ ___ ___ #### [Playing with Numbers Series](https://www.codewars.com/collections/playing-with-numbers) #### [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays) #### [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored) ___ ##### ALL translations are welcomed ##### Enjoy Learning !! ##### Zizou 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. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$. -----Input----- The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. -----Output----- Print one integer — the number of ways to color bricks modulo $998\,244\,353$. -----Examples----- Input 3 3 0 Output 3 Input 3 2 1 Output 4 -----Note----- In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] 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. On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper. A permutation $p$ is written from left to right on the paper. First Qingshan chooses an integer index $x$ ($1\le x\le n$) and tells it to Daniel. After that, Daniel chooses another integer index $y$ ($1\le y\le n$, $y \ne x$). The game progresses turn by turn and as usual, Qingshan moves first. The rules follow: If it is Qingshan's turn, Qingshan must change $x$ to such an index $x'$ that $1\le x'\le n$, $|x'-x|=1$, $x'\ne y$, and $p_{x'}<p_x$ at the same time. If it is Daniel's turn, Daniel must change $y$ to such an index $y'$ that $1\le y'\le n$, $|y'-y|=1$, $y'\ne x$, and $p_{y'}>p_y$ at the same time. The person who can't make her or his move loses, and the other wins. You, as Qingshan's fan, are asked to calculate the number of possible $x$ to make Qingshan win in the case both players play optimally. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) — the length of the permutation. The second line contains $n$ distinct integers $p_1,p_2,\dots,p_n$ ($1\le p_i\le n$) — the permutation. -----Output----- Print the number of possible values of $x$ that Qingshan can choose to make her win. -----Examples----- Input 5 1 2 5 4 3 Output 1 Input 7 1 2 4 6 5 3 7 Output 0 -----Note----- In the first test case, Qingshan can only choose $x=3$ to win, so the answer is $1$. In the second test case, if Qingshan will choose $x=4$, Daniel can choose $y=1$. In the first turn (Qingshan's) Qingshan chooses $x'=3$ and changes $x$ to $3$. In the second turn (Daniel's) Daniel chooses $y'=2$ and changes $y$ to $2$. Qingshan can't choose $x'=2$ because $y=2$ at this time. Then Qingshan loses. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost. Input The input is given in the following format. r c a1 a2 b1 b2 Input meets the following constraints 1 ≤ r, c ≤ 1,000 0 ≤ a1, b1 <r 0 ≤ a2, b2 <c Output Output the remainder of the answer value divided by 100,000,007. Examples Input 4 4 0 0 3 3 Output 2 Input 4 4 0 0 1 1 Output 2 Input 2 3 0 0 1 2 Output 4 Input 500 500 0 0 200 200 Output 34807775 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. Adding tip to a restaurant bill in a graceful way can be tricky, thats why you need make a function for it. The function will receive the restaurant bill (always a positive number) as an argument. You need to 1) **add at least 15%** in tip, 2) round that number up to an *elegant* value and 3) return it. What is an *elegant* number? It depends on the magnitude of the number to be rounded. Numbers below 10 should simply be rounded to whole numbers. Numbers 10 and above should be rounded like this: 10 - 99.99... ---> Round to number divisible by 5 100 - 999.99... ---> Round to number divisible by 50 1000 - 9999.99... ---> Round to number divisible by 500 And so on... Good luck! ## Examples ``` 1 --> 2 7 --> 9 12 --> 15 86 --> 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. There are 3N participants in AtCoder Group Contest. The strength of the i-th participant is represented by an integer a_i. They will form N teams, each consisting of three participants. No participant may belong to multiple teams. The strength of a team is defined as the second largest strength among its members. For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3. Find the maximum possible sum of the strengths of N teams. Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ 10^{9} * a_i are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_{3N} Output Print the answer. Examples Input 2 5 2 8 5 1 5 Output 10 Input 10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 10000000000 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 chocolate bar partitioned into H horizontal rows and W vertical columns of squares. The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1. We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar. How many times do we need to cut the bar so that every block after the cuts has K or less white squares? -----Constraints----- - 1 \leq H \leq 10 - 1 \leq W \leq 1000 - 1 \leq K \leq H \times W - S_{i,j} is 0 or 1. -----Input----- Input is given from Standard Input in the following format: H W K S_{1,1}S_{1,2}...S_{1,W} : S_{H,1}S_{H,2}...S_{H,W} -----Output----- Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares. -----Sample Input----- 3 5 4 11100 10001 00111 -----Sample Output----- 2 For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works. Note that we cannot cut the bar in the ways shown in the two figures to the 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. Find places where a string P is found within a text T. Print all indices of T where P found. The indices of T start with 0. Constraints * 1 ≤ length of T ≤ 1000 * 1 ≤ length of P ≤ 1000 * The input consists of alphabetical characters and digits Input In the first line, a text T is given. In the second line, a string P is given. Examples Input aabaaa aa Output 0 3 4 Input xyzz yz Output 1 Input abc xyz Output Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel. Ari draws a regular convex polygon on the floor and numbers it's vertices 1, 2, ..., n in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2, 3, ..., n (in this particular order). And then she puts a walnut in each region inside the polygon. [Image] Ada the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner. Assuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts? -----Input----- The first and only line of the input contains a single integer n (3 ≤ n ≤ 54321) - the number of vertices of the regular polygon drawn by Ari. -----Output----- Print the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after. -----Examples----- Input 5 Output 9 Input 3 Output 1 -----Note----- One of the possible solutions for the first sample is shown on the picture above. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a string $s$ consisting of $n$ characters. Each character of $s$ is either 0 or 1. A substring of $s$ is a contiguous subsequence of its characters. You have to choose two substrings of $s$ (possibly intersecting, possibly the same, possibly non-intersecting — just any two substrings). After choosing them, you calculate the value of the chosen pair of substrings as follows: let $s_1$ be the first substring, $s_2$ be the second chosen substring, and $f(s_i)$ be the integer such that $s_i$ is its binary representation (for example, if $s_i$ is 11010, $f(s_i) = 26$); the value is the bitwise OR of $f(s_1)$ and $f(s_2)$. Calculate the maximum possible value you can get, and print it in binary representation without leading zeroes. -----Input----- The first line contains one integer $n$ — the number of characters in $s$. The second line contains $s$ itself, consisting of exactly $n$ characters 0 and/or 1. All non-example tests in this problem are generated randomly: every character of $s$ is chosen independently of other characters; for each character, the probability of it being 1 is exactly $\frac{1}{2}$. This problem has exactly $40$ tests. Tests from $1$ to $3$ are the examples; tests from $4$ to $40$ are generated randomly. In tests from $4$ to $10$, $n = 5$; in tests from $11$ to $20$, $n = 1000$; in tests from $21$ to $40$, $n = 10^6$. Hacks are forbidden in this problem. -----Output----- Print the maximum possible value you can get in binary representation without leading zeroes. -----Examples----- Input 5 11010 Output 11111 Input 7 1110010 Output 1111110 Input 4 0000 Output 0 -----Note----- In the first example, you can choose the substrings 11010 and 101. $f(s_1) = 26$, $f(s_2) = 5$, their bitwise OR is $31$, and the binary representation of $31$ is 11111. In the second example, you can choose the substrings 1110010 and 11100. 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.