question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. A super computer has been built in the Turtle Academy of Sciences. The computer consists of n·m·k CPUs. The architecture was the paralellepiped of size n × m × k, split into 1 × 1 × 1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to n, the line number from 1 to m and the column number from 1 to k. In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (x, y, z) can send messages to CPUs (x + 1, y, z), (x, y + 1, z) and (x, y, z + 1) (of course, if they exist), there is no feedback, that is, CPUs (x + 1, y, z), (x, y + 1, z) and (x, y, z + 1) cannot send messages to CPU (x, y, z). Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (a, b, c) controls CPU (d, e, f) , if there is a chain of CPUs (x_{i}, y_{i}, z_{i}), such that (x_1 = a, y_1 = b, z_1 = c), (x_{p} = d, y_{p} = e, z_{p} = f) (here and below p is the length of the chain) and the CPU in the chain with number i (i < p) can send messages to CPU i + 1. Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (x, y, z) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (x, y, z) CPUs: (a, b, c) and (d, e, f), such that (a, b, c) controls (d, e, f) before (x, y, z) is turned off and stopped controlling it after the turning off. -----Input----- The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 100) — the dimensions of the Super Computer. Then n blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to n. Each block consists of m lines, k characters in each — the description of a layer in the format of an m × k table. Thus, the state of the CPU (x, y, z) is corresponded to the z-th character of the y-th line of the block number x. Character "1" corresponds to a working CPU and character "0" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line. -----Output----- Print a single integer — the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control. -----Examples----- Input 2 2 3 000 000 111 111 Output 2 Input 3 3 3 111 111 111 111 111 111 111 111 111 Output 19 Input 1 1 10 0101010101 Output 0 -----Note----- In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2, 1, 2) turns off, it disrupts the control by CPU (2, 1, 3) over CPU (2, 1, 1), and when CPU (2, 2, 2) is turned off, it disrupts the control over CPU (2, 2, 3) by CPU (2, 2, 1). In the second sample all processors except for the corner ones are critical. In the third sample there is not a single processor controlling another processor, so the answer is 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number — the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <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. In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 10^9. At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier. Your task is to determine the k-th identifier to be pronounced. -----Input----- The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·10^9, n·(n + 1) / 2). The second line contains the sequence id_1, id_2, ..., id_{n} (1 ≤ id_{i} ≤ 10^9) — identifiers of roborts. It is guaranteed that all identifiers are different. -----Output----- Print the k-th pronounced identifier (assume that the numeration starts from 1). -----Examples----- Input 2 2 1 2 Output 1 Input 4 5 10 4 18 3 Output 4 -----Note----- In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1. In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals 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 Given the birthdates of two people, find the date when the younger one is exactly half the age of the other. # Notes * The dates are given in the format YYYY-MM-DD and are not sorted in any particular order * Round **down** to the nearest day * Return the result as a string, like the input dates Write your solution by modifying this code: ```python def half_life(person1, person2): ``` Your solution should implemented in the function "half_life". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Check if given chord is minor or major. _____________________________________________________________ Rules: 1. Basic minor/major chord have three elements. 2. Chord is minor when interval between first and second element equals 3 and between second and third -> 4. 3. Chord is major when interval between first and second element equals 4 and between second and third -> 3. 4. In minor/major chord interval between first and third element equals... 7. _______________________________________________________________ There is a preloaded list of the 12 notes of a chromatic scale built on C. This means that there are (almost) all allowed note' s names in music. notes = ['C', ['C#', 'Db'], 'D', ['D#', 'Eb'], 'E', 'F', ['F#', 'Gb'], 'G', ['G#', 'Ab'], 'A', ['A#', 'Bb'], 'B'] Note that e. g. 'C#' - 'C' = 1, 'C' - 'C#' = 1, 'Db' - 'C' = 1 and 'B' - 'C' = 1. Input: String of notes separated by whitespace, e. g. 'A C# E' Output: String message: 'Minor', 'Major' or 'Not a chord'. Write your solution by modifying this code: ```python def minor_or_major(chord): ``` Your solution should implemented in the function "minor_or_major". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp has n dice d_1, d_2, ..., d_{n}. The i-th dice shows numbers from 1 to d_{i}. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d_1, d_2, ..., d_{n}. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible). For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A. -----Input----- The first line contains two integers n, A (1 ≤ n ≤ 2·10^5, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d_1 + d_2 + ... + d_{n}. The second line contains n integers d_1, d_2, ..., d_{n} (1 ≤ d_{i} ≤ 10^6), where d_{i} is the maximum value that the i-th dice can show. -----Output----- Print n integers b_1, b_2, ..., b_{n}, where b_{i} is the number of values for which it is guaranteed that the i-th dice couldn't show them. -----Examples----- Input 2 8 4 4 Output 3 3 Input 1 3 5 Output 4 Input 2 3 2 3 Output 0 1 -----Note----- In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3. In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5. In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 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. Ayrat has number n, represented as it's prime factorization p_{i} of size m, i.e. n = p_1·p_2·...·p_{m}. Ayrat got secret information that that the product of all divisors of n taken modulo 10^9 + 7 is the password to the secret data base. Now he wants to calculate this value. -----Input----- The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of primes in factorization of n. The second line contains m primes numbers p_{i} (2 ≤ p_{i} ≤ 200 000). -----Output----- Print one integer — the product of all divisors of n modulo 10^9 + 7. -----Examples----- Input 2 2 3 Output 36 Input 3 2 3 2 Output 1728 -----Note----- In the first sample n = 2·3 = 6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1·2·3·6 = 36. In the second sample 2·3·2 = 12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1·2·3·4·6·12 = 1728. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is. -----Input----- The first line of the input contains an integer n (1 ≤ n ≤ 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn’t exceed 100. -----Output----- For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 can’t recognize whose sentence it is. He can’t recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions. -----Examples----- Input 5 I will go to play with you lala. wow, welcome. miao.lala. miao. miao . Output Freda's OMG>.< I don't know! OMG>.< I don't know! Rainbow's OMG>.< I don't know! Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given two integers A and B. Find the largest value among A+B, A-B and A \times B. -----Constraints----- - -1000 \leq A,B \leq 1000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the largest value among A+B, A-B and A \times B. -----Sample Input----- 3 1 -----Sample Output----- 4 3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored. The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be: <image>. Your task is to find out, where each ball will be t seconds after. Input The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0. It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]). Output Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point. Examples Input 2 9 3 4 5 0 7 8 Output 68.538461538 44.538461538 Input 3 10 1 2 3 4 -5 6 7 -8 9 Output -93.666666667 -74.666666667 -15.666666667 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black. Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once. -----Input----- The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 10^5, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n. -----Output----- If it is impossible to drink n cups of tea, print "NO" (without quotes). Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black. If there are multiple answers, print any of them. -----Examples----- Input 5 1 3 2 Output GBGBG Input 7 2 2 5 Output BBGBGBB Input 4 3 4 0 Output NO Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers. For instance, if we are given an array [10, 20, 30, 40], we can permute it so that it becomes [20, 40, 10, 30]. Then on the first and the second positions the integers became larger (20>10, 40>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 2. Read the note for the first example, there is one more demonstrative test case. Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array. Output Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array. Examples Input 7 10 1 1 1 5 5 3 Output 4 Input 5 1 1 1 1 1 Output 0 Note In the first sample, one of the best permutations is [1, 5, 5, 3, 10, 1, 1]. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4. In the second sample, there is no way to increase any element with a permutation, so the answer is 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a positive integer `n`, return first n dgits of Thue-Morse sequence, as a string (see examples). Thue-Morse sequence is a binary sequence with 0 as the first element. The rest of the sequece is obtained by adding the Boolean (binary) complement of a group obtained so far. ``` For example: 0 01 0110 01101001 and so on... ``` ![alt](https://upload.wikimedia.org/wikipedia/commons/f/f1/Morse-Thue_sequence.gif) Ex.: ```python thue_morse(1); #"0" thue_morse(2); #"01" thue_morse(5); #"01101" thue_morse(10): #"0110100110" ``` - You don't need to test if n is valid - it will always be a positive integer. - `n` will be between 1 and 10000 [Thue-Morse on Wikipedia](https://en.wikipedia.org/wiki/Thue%E2%80%93Morse_sequence) [Another kata on Thue-Morse](https://www.codewars.com/kata/simple-fun-number-106-is-thue-morse) by @myjinxin2015 Write your solution by modifying this code: ```python def thue_morse(n): ``` Your solution should implemented in the function "thue_morse". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an array A of N numbers, find the number of distinct pairs (i, j) such that j ≥i and A[i] = A[j]. First line of the input contains number of test cases T. Each test case has two lines, first line is the number N, followed by a line consisting of N integers which are the elements of array A. For each test case print the number of distinct pairs. Constraints 1 ≤ T ≤ 10 1 ≤ N ≤ 10^6 -10^6 ≤ A[i] ≤ 10^6 for 0 ≤ i < N SAMPLE INPUT 2 4 1 2 3 4 3 1 2 1 SAMPLE OUTPUT 4 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. Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \leq A_i,B_i \leq N). Now, each vertex is painted black with probability 1/2 and white with probability 1/2, which is chosen independently from other vertices. Then, let S be the smallest subtree (connected subgraph) of T containing all the vertices painted black. (If no vertex is painted black, S is the empty graph.) Let the holeyness of S be the number of white vertices contained in S. Find the expected holeyness of S. Since the answer is a rational number, we ask you to print it \bmod 10^9+7, as described in Notes. -----Notes----- When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers, and x is not divisible by 10^9 + 7 (under the constraints of the problem, such representation is always possible). Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{10^9 + 7}. -----Constraints----- - 2 \leq N \leq 2 \times 10^5 - 1 \leq A_i,B_i \leq N - The given graph is a tree. -----Input----- Input is given from Standard Input in the following format: N A_1 B_1 : A_{N-1} B_{N-1} -----Output----- Print the expected holeyness of S, \bmod 10^9+7. -----Sample Input----- 3 1 2 2 3 -----Sample Output----- 125000001 If the vertices 1, 2, 3 are painted black, white, black, respectively, the holeyness of S is 1. Otherwise, the holeyness is 0, so the expected holeyness is 1/8. Since 8 \times 125000001 \equiv 1 \pmod{10^9+7}, we should print 125000001. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 is transitioning from website design to coding and wants to sharpen his skills with CodeWars. He can do ten kata in an hour, but when he makes a mistake, he must do pushups. These pushups really tire poor Alex out, so every time he does them they take twice as long. His first set of redemption pushups takes 5 minutes. Create a function, `alexMistakes`, that takes two arguments: the number of kata he needs to complete, and the time in minutes he has to complete them. Your function should return how many mistakes Alex can afford to make. Write your solution by modifying this code: ```python def alex_mistakes(number_of_katas, time_limit): ``` Your solution should implemented in the function "alex_mistakes". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum. For example, for n = 4 the sum is equal to - 1 - 2 + 3 - 4 = - 4, because 1, 2 and 4 are 2^0, 2^1 and 2^2 respectively. Calculate the answer for t values of n. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed. Each of next t lines contains a single integer n (1 ≤ n ≤ 10^9). -----Output----- Print the requested sum for each of t integers n given in the input. -----Examples----- Input 2 4 1000000000 Output -4 499999998352516354 -----Note----- The answer for the first sample is explained in the statement. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # 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 maximum difference_** *between the successive elements in its sorted form*. ___ # Notes * **_Array/list_** size is *at least 3* . * **_Array/list's numbers_** Will be **mixture of positives and negatives also zeros_** * **_Repetition_** of numbers in *the array/list could occur*. * **_The Maximum Gap_** is *computed Regardless the sign*. ___ # Input >> Output Examples ``` maxGap ({13,10,5,2,9}) ==> return (4) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `4` , *The difference between* ``` 9 - 5 = 4 ``` . ___ ``` maxGap ({-3,-27,-4,-2}) ==> return (23) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `23` , *The difference between* ` |-4- (-27) | = 23` . * **_Note_** : *Regardless the sign of negativity* . ___ ``` maxGap ({-7,-42,-809,-14,-12}) ==> return (767) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `767` , *The difference between* ` | -809- (-42) | = 767` . * **_Note_** : *Regardless the sign of negativity* . ___ ``` maxGap ({-54,37,0,64,640,0,-15}) //return (576) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `576` , *The difference between* ` | 64 - 640 | = 576` . * **_Note_** : *Regardless the sign of negativity* . ___ ___ ___ # [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 Write your solution by modifying this code: ```python def max_gap(numbers): ``` Your solution should implemented in the function "max_gap". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $2$, b = $3$ changes the value of a to $5$ (the value of b does not change). In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $n$. What is the smallest number of operations he has to perform? -----Input----- The first line contains a single integer $T$ ($1 \leq T \leq 100$) — the number of test cases. Each of the following $T$ lines describes a single test case, and contains three integers $a, b, n$ ($1 \leq a, b \leq n \leq 10^9$) — initial values of a and b, and the value one of the variables has to exceed, respectively. -----Output----- For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks. -----Example----- Input 2 1 2 3 5 4 100 Output 2 7 -----Note----- In the first case we cannot make a variable exceed $3$ in one operation. One way of achieving this in two operations is to perform "b += a" twice. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 infinite set generated as follows: $1$ is in this set. If $x$ is in this set, $x \cdot a$ and $x+b$ both are in this set. For example, when $a=3$ and $b=6$, the five smallest elements of the set are: $1$, $3$ ($1$ is in this set, so $1\cdot a=3$ is in this set), $7$ ($1$ is in this set, so $1+b=7$ is in this set), $9$ ($3$ is in this set, so $3\cdot a=9$ is in this set), $13$ ($7$ is in this set, so $7+b=13$ is in this set). Given positive integers $a$, $b$, $n$, determine if $n$ is in this set. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1\leq t\leq 10^5$) — the number of test cases. The description of the test cases follows. The only line describing each test case contains three integers $n$, $a$, $b$ ($1\leq n,a,b\leq 10^9$) separated by a single space. -----Output----- For each test case, print "Yes" if $n$ is in this set, and "No" otherwise. You can print each letter in any case. -----Examples----- Input 5 24 3 5 10 3 6 2345 1 4 19260817 394 485 19260817 233 264 Output Yes No Yes No Yes -----Note----- In the first test case, $24$ is generated as follows: $1$ is in this set, so $3$ and $6$ are in this set; $3$ is in this set, so $9$ and $8$ are in this set; $8$ is in this set, so $24$ and $13$ are in this set. Thus we can see $24$ is in this set. The five smallest elements of the set in the second test case is described in statements. We can see that $10$ isn't among 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 a sorted array $a_1, a_2, \dots, a_n$ (for each index $i > 1$ condition $a_i \ge a_{i-1}$ holds) and an integer $k$. You are asked to divide this array into $k$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $max(i)$ be equal to the maximum in the $i$-th subarray, and $min(i)$ be equal to the minimum in the $i$-th subarray. The cost of division is equal to $\sum\limits_{i=1}^{k} (max(i) - min(i))$. For example, if $a = [2, 4, 5, 5, 8, 11, 19]$ and we divide it into $3$ subarrays in the following way: $[2, 4], [5, 5], [8, 11, 19]$, then the cost of division is equal to $(4 - 2) + (5 - 5) + (19 - 8) = 13$. Calculate the minimum cost you can obtain by dividing the array $a$ into $k$ non-empty consecutive subarrays. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($ 1 \le a_i \le 10^9$, $a_i \ge a_{i-1}$). -----Output----- Print the minimum cost you can obtain by dividing the array $a$ into $k$ nonempty consecutive subarrays. -----Examples----- Input 6 3 4 8 15 16 23 42 Output 12 Input 4 4 1 3 3 7 Output 0 Input 8 1 1 1 2 3 5 8 13 21 Output 20 -----Note----- In the first test we can divide array $a$ in the following way: $[4, 8, 15, 16], [23], [42]$. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 is playing a game with her good friend, Marisa. There are n boxes arranged in a line, numbered with integers from 1 to n from left to right. Marisa will hide a doll in one of the boxes. Then Alice will have m chances to guess where the doll is. If Alice will correctly guess the number of box, where doll is now, she will win the game, otherwise, her friend will win the game. In order to win, Marisa will use some unfair tricks. After each time Alice guesses a box, she can move the doll to the neighboring box or just keep it at its place. Boxes i and i + 1 are neighboring for all 1 ≤ i ≤ n - 1. She can also use this trick once before the game starts. So, the game happens in this order: the game starts, Marisa makes the trick, Alice makes the first guess, Marisa makes the trick, Alice makes the second guess, Marisa makes the trick, …, Alice makes m-th guess, Marisa makes the trick, the game ends. Alice has come up with a sequence a_1, a_2, …, a_m. In the i-th guess, she will ask if the doll is in the box a_i. She wants to know the number of scenarios (x, y) (for all 1 ≤ x, y ≤ n), such that Marisa can win the game if she will put the doll at the x-th box at the beginning and at the end of the game, the doll will be at the y-th box. Help her and calculate this number. Input The first line contains two integers n and m, separated by space (1 ≤ n, m ≤ 10^5) — the number of boxes and the number of guesses, which Alice will make. The next line contains m integers a_1, a_2, …, a_m, separated by spaces (1 ≤ a_i ≤ n), the number a_i means the number of the box which Alice will guess in the i-th guess. Output Print the number of scenarios in a single line, or the number of pairs of boxes (x, y) (1 ≤ x, y ≤ n), such that if Marisa will put the doll into the box with number x, she can make tricks in such way, that at the end of the game the doll will be in the box with number y and she will win the game. Examples Input 3 3 2 2 2 Output 7 Input 5 2 3 1 Output 21 Note In the first example, the possible scenarios are (1, 1), (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3). Let's take (2, 2) as an example. The boxes, in which the doll will be during the game can be 2 → 3 → 3 → 3 → 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there are k such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: ai, ci, bi. Vasya wants to get from the city 1 to the city n and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes. Input The first line contains three integers n, m, k (2 ≤ n ≤ 3000, 1 ≤ m ≤ 20000, 0 ≤ k ≤ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 ≤ xi, yi ≤ n) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow k lines each containing three integers ai, bi, ci (1 ≤ ai, bi, ci ≤ n) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct. City n can be unreachable from city 1 by roads. Output If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers — any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n. Examples Input 4 4 1 1 2 2 3 3 4 1 3 1 4 3 Output 2 1 3 4 Input 3 1 0 1 2 Output -1 Input 4 4 2 1 2 2 3 3 4 1 3 1 2 3 1 3 4 Output 4 1 3 2 3 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have initially a string of N characters, denoted by A1,A2...AN. You have to print the size of the largest subsequence of string A such that all the characters in that subsequence are distinct ie. no two characters in that subsequence should be same. A subsequence of string A is a sequence that can be derived from A by deleting some elements and without changing the order of the remaining elements. -----Input----- First line contains T, number of testcases. Each testcase consists of a single string in one line. Each character of the string will be a small alphabet(ie. 'a' to 'z'). -----Output----- For each testcase, print the required answer in one line. -----Constraints----- - 1 ≤ T ≤ 10 - Subtask 1 (20 points):1 ≤ N ≤ 10 - Subtask 2 (80 points):1 ≤ N ≤ 105 -----Example----- Input: 2 abc aba Output: 3 2 -----Explanation----- For first testcase, the whole string is a subsequence which has all distinct characters. In second testcase, the we can delete last or first 'a' to get the required subsequence. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, you've to count lowercase letters in a given string and return the letter count in a hash with 'letter' as key and count as 'value'. The key must be 'symbol' instead of string in Ruby and 'char' instead of string in Crystal. Example: ```python letter_count('arithmetics') #=> {"a": 1, "c": 1, "e": 1, "h": 1, "i": 2, "m": 1, "r": 1, "s": 1, "t": 2} ``` Write your solution by modifying this code: ```python def letter_count(s): ``` Your solution should implemented in the function "letter_count". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 talked to Polycarp and asked him a question. You know that when he wants to answer "yes", he repeats Yes many times in a row. Because of the noise, you only heard part of the answer — some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se. Determine if it is true that the given string $s$ is a substring of YesYesYes... (Yes repeated many times in a row). -----Input----- The first line of input data contains the singular $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Each test case is described by a single string of Latin letters $s$ ($1 \le |s| \le 50$) — the part of Polycarp's answer that you heard, where $|s|$ — is the length of the string $s$. -----Output----- Output $t$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if the specified string $s$ is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). -----Examples----- Input 12 YES esYes codeforces es se YesY esYesYesYesYesYesYe seY Yess sY o Yes Output NO YES NO YES NO YES YES NO NO YES NO YES -----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. # Introduction There is a war and nobody knows - the alphabet war! There are two groups of hostile letters. The tension between left side letters and right side letters was too high and the war began. # Task Write a function that accepts `fight` string consists of only small letters and return who wins the fight. When the left side wins return `Left side wins!`, when the right side wins return `Right side wins!`, in other case return `Let's fight again!`. The left side letters and their power: ``` w - 4 p - 3 b - 2 s - 1 ``` The right side letters and their power: ``` m - 4 q - 3 d - 2 z - 1 ``` The other letters don't have power and are only victims. # Example # Alphabet war Collection Alphavet war Alphabet war - airstrike - letters massacre Alphabet wars - reinforces massacre Alphabet wars - nuclear strike Alphabet war - Wo lo loooooo priests join the war Write your solution by modifying this code: ```python def alphabet_war(fight): ``` Your solution should implemented in the function "alphabet_war". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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. Matsudaira is always careful about eco-friendliness in his life. Last month's water charge was 4280 yen, which exceeded the usual target of 4000 yen, so we have been trying to save water this month. How much have you saved on your water bill compared to last month? Enter this month's water usage w [m3] and create a program that outputs how much water charges you have saved compared to last month's water charges of 4280 yen. The water charge is calculated as follows. (Water charge) = (Basic charge) + (Charge based on water volume) The charge based on the amount of water is calculated according to the amount used as shown in the table below. Stage | Water volume | Price --- | --- | --- 1st stage charge | Up to 10 [m3] | Basic charge 1150 yen Second stage fee | 10 [m3] Excess up to 20 [m3] | 125 yen per [m3] Third stage charge | 20 [m3] Excess up to 30 [m3] | 140 yen per [m3] 4th stage charge | 30 [m3] excess | 160 yen per [m3] For example, if the amount of water used is 40 [m3], the basic charge is 1150 yen (1st stage) + 10 [m3] x 125 yen (2nd stage) + 10 [m3] x 140 yen (3rd stage) + 10 [ m3] x 160 yen (4th stage) = 5400 yen. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of -1. For each dataset, an integer w (0 ≤ w ≤ 100) representing the amount of water used this month is given on one line. The number of datasets does not exceed 200. Output For each input dataset, the difference from last month's water charges is output on one line. Example Input 29 40 0 -1 Output 620 -1120 3130 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Let's consider all integers in the range from $1$ to $n$ (inclusive). Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $\mathrm{gcd}(a, b)$, where $1 \leq a < b \leq n$. The greatest common divisor, $\mathrm{gcd}(a, b)$, of two positive integers $a$ and $b$ is the biggest integer that is a divisor of both $a$ and $b$. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 100$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $n$ ($2 \leq n \leq 10^6$). -----Output----- For each test case, output the maximum value of $\mathrm{gcd}(a, b)$ among all $1 \leq a < b \leq n$. -----Example----- Input 2 3 5 Output 1 2 -----Note----- In the first test case, $\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1$. In the second test case, $2$ is the maximum possible value, corresponding to $\mathrm{gcd}(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. Anshal and his friends love watching TV series together,but Anshal has a habit of giving spoilers.In order to tackle this his friends anagrammed the list of the shows they were planning to watch.Somehow Anshal got hold of this list and started guessing the shows.Determine whether Anshal made a correct guess or not. INPUT The input consists of two strings S1 and S2 (the anagrammed show name and Anshal's guess). An anagram of a string is a string obtained by permuting the letters of a string. For Example FSRDIEN and FRIENDS are anagrams of one another while KKERLOSS and SHERLOCK are not. OUTPUT You have to print "CORRECT" (without quotes) if S1 and S2 are anagrams or "WRONG"(without quotes) otherwise. CONSTRAINTS 1 ≤ len(S1) , len(S2) ≤ 10^6 The strings would consist of only letters 'A'-'Z'. SAMPLE INPUT FSRDIEN FRIENDS SAMPLE 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. Finding your seat on a plane is never fun, particularly for a long haul flight... You arrive, realise again just how little leg room you get, and sort of climb into the seat covered in a pile of your own stuff. To help confuse matters (although they claim in an effort to do the opposite) many airlines omit the letters 'I' and 'J' from their seat naming system. the naming system consists of a number (in this case between 1-60) that denotes the section of the plane where the seat is (1-20 = front, 21-40 = middle, 40+ = back). This number is followed by a letter, A-K with the exclusions mentioned above. Letters A-C denote seats on the left cluster, D-F the middle and G-K the right. Given a seat number, your task is to return the seat location in the following format: '2B' would return 'Front-Left'. If the number is over 60, or the letter is not valid, return 'No Seat!!'. Write your solution by modifying this code: ```python def plane_seat(a): ``` Your solution should implemented in the function "plane_seat". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bamboo Blossoms The bamboos live for decades, and at the end of their lives, they flower to make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in blossom in his travel to Tsukuba. He liked the flower so much that he was tempted to make a garden where the bamboos bloom annually. Dr. ACM started research of improving breed of the bamboos, and finally, he established a method to develop bamboo breeds with controlled lifetimes. With this method, he can develop bamboo breeds that flower after arbitrarily specified years. Let us call bamboos that flower k years after sowing "k-year-bamboos." k years after being sowed, k-year-bamboos make their seeds and then die, hence their next generation flowers after another k years. In this way, if he sows seeds of k-year-bamboos, he can see bamboo blossoms every k years. For example, assuming that he sows seeds of 15-year-bamboos, he can see bamboo blossoms every 15 years; 15 years, 30 years, 45 years, and so on, after sowing. Dr. ACM asked you for designing his garden. His garden is partitioned into blocks, in each of which only a single breed of bamboo can grow. Dr. ACM requested you to decide which breeds of bamboos should he sow in the blocks in order to see bamboo blossoms in at least one block for as many years as possible. You immediately suggested to sow seeds of one-year-bamboos in all blocks. Dr. ACM, however, said that it was difficult to develop a bamboo breed with short lifetime, and would like a plan using only those breeds with long lifetimes. He also said that, although he could wait for some years until he would see the first bloom, he would like to see it in every following year. Then, you suggested a plan to sow seeds of 10-year-bamboos, for example, in different blocks each year, that is, to sow in a block this year and in another block next year, and so on, for 10 years. Following this plan, he could see bamboo blossoms in one block every year except for the first 10 years. Dr. ACM objected again saying he had determined to sow in all blocks this year. After all, you made up your mind to make a sowing plan where the bamboos bloom in at least one block for as many consecutive years as possible after the first m years (including this year) under the following conditions: * the plan should use only those bamboo breeds whose lifetimes are m years or longer, and * Dr. ACM should sow the seeds in all the blocks only this year. Input The input consists of at most 50 datasets, each in the following format. m n An integer m (2 ≤ m ≤ 100) represents the lifetime (in years) of the bamboos with the shortest lifetime that Dr. ACM can use for gardening. An integer n (1 ≤ n ≤ 500,000) represents the number of blocks. The end of the input is indicated by a line containing two zeros. Output No matter how good your plan is, a "dull-year" would eventually come, in which the bamboos do not flower in any block. For each dataset, output in a line an integer meaning how many years from now the first dull-year comes after the first m years. Note that the input of m = 2 and n = 500,000 (the last dataset of the Sample Input) gives the largest answer. Sample Input 3 1 3 4 10 20 100 50 2 500000 0 0 Output for the Sample Input 4 11 47 150 7368791 Example Input 3 1 3 4 10 20 100 50 2 500000 0 0 Output 4 11 47 150 7368791 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alas, finding one's true love is not easy. Masha has been unsuccessful in that yet. Her friend Dasha told Masha about a way to determine the phone number of one's Prince Charming through arithmancy. The phone number is divined like that. First one needs to write down one's own phone numbers. For example, let's suppose that Masha's phone number is 12345. After that one should write her favorite digit from 0 to 9 under the first digit of her number. That will be the first digit of the needed number. For example, Masha's favorite digit is 9. The second digit is determined as a half sum of the second digit of Masha's number and the already written down first digit from her beloved one's number. In this case the arithmetic average equals to (2 + 9) / 2 = 5.5. Masha can round the number up or down, depending on her wishes. For example, she chooses the digit 5. Having written down the resulting digit under the second digit of her number, Masha moves to finding the third digit in the same way, i.e. finding the half sum the the third digit of her number and the second digit of the new number. The result is (5 + 3) / 2 = 4. In this case the answer is unique. Thus, every i-th digit is determined as an arithmetic average of the i-th digit of Masha's number and the i - 1-th digit of her true love's number. If needed, the digit can be rounded up or down. For example, Masha can get: 12345 95444 Unfortunately, when Masha tried dialing the number, she got disappointed: as it turned out, the number was unavailable or outside the coverage area. But Masha won't give up. Perhaps, she rounded to a wrong digit or chose the first digit badly. That's why she keeps finding more and more new numbers and calling them. Count the number of numbers Masha calls. Masha calls all the possible numbers that can be found by the described means of arithmancy, except for, perhaps, her own one. Input The first line contains nonempty sequence consisting of digits from 0 to 9 — Masha's phone number. The sequence length does not exceed 50. Output Output the single number — the number of phone numbers Masha will dial. Examples Input 12345 Output 48 Input 09 Output 15 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. I am a craftsman specialized in interior works. A customer asked me to perform wiring work on a wall whose entire rectangular surface is tightly pasted with pieces of panels. The panels are all of the same size (2 m in width, 1 m in height) and the wall is filled with an x (horizontal) by y (vertical) array of the panels. The customer asked me to stretch a wire from the left top corner of the wall to the right bottom corner whereby the wire is tied up at the crossing points with the panel boundaries (edges and vertexes) as shown in the figure. There are nine tied-up points in the illustrative figure shown below. <image> Fig: The wire is tied up at the edges and vertexes of the panels (X: 4 panels, Y: 6 panels) Write a program to provide the number of points where the wire intersects with the panel boundaries. Assume that the wire and boundary lines have no thickness. Input The input is given in the following format. x y A line of data is given that contains the integer number of panels in the horizontal direction x (1 ≤ x ≤ 1000) and those in the vertical direction y (1 ≤ y ≤ 1000). Output Output the number of points where the wire intersects with the panel boundaries. Examples Input 4 4 Output 5 Input 4 6 Output 9 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A rabbit who came to the fair found that the prize for a game at a store was a carrot cake. The rules for this game are as follows. There is a grid-like field of vertical h squares x horizontal w squares, and each block has at most one block. Each block is a color represented by one of the uppercase letters ('A'-'Z'). When n or more blocks of the same color are lined up in a straight line vertically or horizontally, those blocks disappear. Participants can select two adjacent cells next to each other and switch their states to each other. When blocks are exchanged, disappeared, or dropped and there are no blocks in the cell below the cell with the block, this block At this time, it disappears when n or more blocks of the same color are lined up again. However, the disappearance of the blocks does not occur while the falling blocks exist, and occurs at the same time when all the blocks have finished falling. If you eliminate all the blocks on the field with one operation, this game will be successful and you will get a free gift cake. Rabbit wants to get the cake with one entry fee, can not do it If you don't want to participate. From the state of the field at the beginning of the game, answer if the rabbit should participate in this game. Input The first line of input is given h, w, n separated by spaces. 2 ≤ h, w, n ≤ 30 In the following h lines, the state of the field is given in order from the top. Uppercase letters represent blocks, and'.' Represents an empty space. The state of the given field is n or more consecutive blocks of the same color in the vertical or horizontal direction. No, no blocks are in a falling state. There is one or more blocks. Output Output "YES" if the rabbit should participate in this game, otherwise output "NO" on one line. Examples Input 4 6 3 ...... ...Y.. ...Y.. RRYRYY Output YES Input 4 6 3 ...... ...Y.. ...Y.. RRYRY. Output NO Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an array, return the difference between the count of even numbers and the count of odd numbers. `0` will be considered an even number. ``` For example: solve([0,1,2,3]) = 0 because there are two even numbers and two odd numbers. Even - Odd = 2 - 2 = 0. ``` Let's now add two letters to the last example: ``` solve([0,1,2,3,'a','b']) = 0. Again, Even - Odd = 2 - 2 = 0. Ignore letters. ``` The input will be an array of lowercase letters and numbers only. In some languages (Haskell, C++, and others), input will be an array of strings: ``` solve ["0","1","2","3","a","b"] = 0 ``` Good luck! If you like this Kata, please try: [Longest vowel chain](https://www.codewars.com/kata/59c5f4e9d751df43cf000035) [Word values](https://www.codewars.com/kata/598d91785d4ce3ec4f000018) Write your solution by modifying this code: ```python def solve(a): ``` Your solution should implemented in the function "solve". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 kata is based on [Project Euler Problem 546](https://projecteuler.net/problem=546)_ # Objective Given the recursive sequence fk(n) = ∑ i = 0 n fk(floor(i / k)) where fk(0) = 1 Define a function `f` that takes arguments `k` and `n` and returns the nth term in the sequence fk ## Examples `f(2, 3)` = f2(3) = 6 `f(2, 200)` = f2(200) = 7389572 `f(7, 500)` = f7(500) = 74845 `f(1000, 0)` = f1000(0) = 1 **Note:** No error checking is needed, `k` ranges from 2 to 100 and `n` ranges between 0 and 500000 (mostly small and medium values with a few large ones) As always any feedback would be much appreciated Write your solution by modifying this code: ```python def f(k, n): ``` Your solution should implemented in the function "f". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs. -----Input----- The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 10^5). -----Output----- The output contains a single number — the maximum total gain possible. -----Examples----- Input 3 3 100 100 100 100 1 100 100 100 100 Output 800 -----Note----- Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][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. Write a simple regex to validate a username. Allowed characters are: - lowercase letters, - numbers, - underscore Length should be between 4 and 16 characters (both included). Write your solution by modifying this code: ```python def validate_usr(username): ``` Your solution should implemented in the function "validate_usr". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Oranges on Cans square1001 You put a $ N $ can of aluminum on the table. E869120 You put $ M $ of oranges on each aluminum can on the table. How many oranges are on the aluminum can? input Input is given from standard input in the following format. $ N $ $ M $ output Output the number of oranges on the aluminum can in one line. However, insert a line break at the end. Constraint * $ 1 \ leq N \ leq 9 $ * $ 1 \ leq M \ leq 9 $ * All inputs are integers. Input example 1 3 4 Output example 1 12 Input example 2 7 7 Output example 2 49 Example Input 3 4 Output 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. Goal Given a list of elements [a1, a2, ..., an], with each ai being a string, write a function **majority** that returns the value that appears the most in the list. If there's no winner, the function should return None, NULL, nil, etc, based on the programming language. Example majority(["A", "B", "A"]) returns "A" majority(["A", "B", "B", "A"]) returns None Write your solution by modifying this code: ```python def majority(arr): ``` Your solution should implemented in the function "majority". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of $n$ strings $s_1, s_2, s_3, \ldots, s_{n}$ and $m$ strings $t_1, t_2, t_3, \ldots, t_{m}$. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings $x$ and $y$ as the string that is obtained by writing down strings $x$ and $y$ one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings $s_1$ and $t_1$. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if $n = 3, m = 4, s = ${"a", "b", "c"}, $t =$ {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. [Image] You are given two sequences of strings of size $n$ and $m$ and also $q$ queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? -----Input----- The first line contains two integers $n, m$ ($1 \le n, m \le 20$). The next line contains $n$ strings $s_1, s_2, \ldots, s_{n}$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $1$ and at most $10$. The next line contains $m$ strings $t_1, t_2, \ldots, t_{m}$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $1$ and at most $10$. Among the given $n + m$ strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer $q$ ($1 \le q \le 2\,020$). In the next $q$ lines, an integer $y$ ($1 \le y \le 10^9$) is given, denoting the year we want to know the name for. -----Output----- Print $q$ lines. For each line, print the name of the year as per the rule described above. -----Example----- Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja -----Note----- The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 personal training sessions have finished in the Berland State University Olympiad Programmer Training Centre. By the results of these training sessions teams are composed for the oncoming team contest season. Each team consists of three people. All the students of the Centre possess numbers from 1 to 3n, and all the teams possess numbers from 1 to n. The splitting of students into teams is performed in the following manner: while there are people who are not part of a team, a person with the best total score is chosen among them (the captain of a new team), this person chooses for himself two teammates from those who is left according to his list of priorities. The list of every person's priorities is represented as a permutation from the rest of 3n - 1 students who attend the centre, besides himself. You are given the results of personal training sessions which are a permutation of numbers from 1 to 3n, where the i-th number is the number of student who has won the i-th place. No two students share a place. You are also given the arrangement of the already formed teams in the order in which they has been created. Your task is to determine the list of priorities for the student number k. If there are several priority lists, choose the lexicographically minimal one. Input The first line contains an integer n (1 ≤ n ≤ 105) which is the number of resulting teams. The second line contains 3n space-separated integers from 1 to 3n which are the results of personal training sessions. It is guaranteed that every student appears in the results exactly once. Then follow n lines each containing three integers from 1 to 3n — each line describes the members of a given team. The members of one team can be listed in any order, but the teams themselves are listed in the order in which they were created. It is guaranteed that the arrangement is correct, that is that every student is a member of exactly one team and those teams could really be created from the given results using the method described above. The last line contains number k (1 ≤ k ≤ 3n) which is the number of a student for who the list of priorities should be found. Output Print 3n - 1 numbers — the lexicographically smallest list of priorities for the student number k. The lexicographical comparison is performed by the standard < operator in modern programming languages. The list a is lexicographically less that the list b if exists such an i (1 ≤ i ≤ 3n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Note, that the list 1 9 10 is lexicographically less than the list 1 10 9. That is, the comparison of lists is different from the comparison of lines. Examples Input 3 5 4 1 2 6 3 7 8 9 5 6 2 9 3 4 1 7 8 4 Output 2 3 5 6 9 1 7 8 Input 3 5 4 1 2 6 3 7 8 9 5 6 2 9 3 4 1 7 8 8 Output 1 2 3 4 5 6 7 9 Input 2 4 1 3 2 5 6 4 6 5 1 2 3 4 Output 5 6 1 2 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. According to ISO 8601, the first calendar week (1) starts with the week containing the first thursday in january. Every year contains of 52 (53 for leap years) calendar weeks. **Your task is** to calculate the calendar week (1-53) from a given date. For example, the calendar week for the date `2019-01-01` (string) should be 1 (int). Good luck 👍 See also [ISO week date](https://en.wikipedia.org/wiki/ISO_week_date) and [Week Number](https://en.wikipedia.org/wiki/Week#Week_numbering) on Wikipedia for further information about calendar weeks. On [whatweekisit.org](http://whatweekisit.org/) you may click through the calender and study calendar weeks in more depth. *heads-up:* `require(xxx)` has been disabled Thanks to @ZED.CWT, @Unnamed and @proxya for their feedback. Write your solution by modifying this code: ```python def get_calendar_week(date_string): ``` Your solution should implemented in the function "get_calendar_week". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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_1, a_2, \dots, a_n$ where all $a_i$ are integers and greater than $0$. In one operation, you can choose two different indices $i$ and $j$ ($1 \le i, j \le n$). If $gcd(a_i, a_j)$ is equal to the minimum element of the whole array $a$, you can swap $a_i$ and $a_j$. $gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$. Now you'd like to make $a$ non-decreasing using the operation any number of times (possibly zero). Determine if you can do this. An array $a$ is non-decreasing if and only if $a_1 \le a_2 \le \ldots \le a_n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of array $a$. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots a_n$ ($1 \le a_i \le 10^9$) — the array itself. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case, output "YES" if it is possible to make the array $a$ non-decreasing using the described operation, or "NO" if it is impossible to do so. -----Example----- Input 4 1 8 6 4 3 6 6 2 9 4 4 5 6 7 5 7 5 2 2 4 Output YES YES YES NO -----Note----- In the first and third sample, the array is already non-decreasing. In the second sample, we can swap $a_1$ and $a_3$ first, and swap $a_1$ and $a_5$ second to make the array non-decreasing. In the forth sample, we cannot the array non-decreasing using the operation. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d_1 is the distance between the 1-st and the 2-nd station; d_2 is the distance between the 2-nd and the 3-rd station; ... d_{n} - 1 is the distance between the n - 1-th and the n-th station; d_{n} is the distance between the n-th and the 1-st station. The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t. -----Input----- The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d_1, d_2, ..., d_{n} (1 ≤ d_{i} ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces. -----Output----- Print a single number — the length of the shortest path between stations number s and t. -----Examples----- Input 4 2 3 4 9 1 3 Output 5 Input 4 5 8 2 100 4 1 Output 15 Input 3 1 1 1 3 1 Output 1 Input 3 31 41 59 1 1 Output 0 -----Note----- In the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13. In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15. In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2. In the fourth sample the numbers of stations are the same, so the shortest distance equals 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are playing a popular video game which is famous for its depthful story and interesting puzzles. In the game you were locked in a mysterious house alone and there is no way to call for help, so you have to escape on yours own. However, almost every room in the house has some kind of puzzles and you cannot move to neighboring room without solving them. One of the puzzles you encountered in the house is following. In a room, there was a device which looked just like a dice and laid on a table in the center of the room. Direction was written on the wall. It read: "This cube is a remote controller and you can manipulate a remote room, Dice Room, by it. The room has also a cubic shape whose surfaces are made up of 3x3 unit squares and some squares have a hole on them large enough for you to go though it. You can rotate this cube so that in the middle of rotation at least one edge always touch the table, that is, to 4 directions. Rotating this cube affects the remote room in the same way and positions of holes on the room change. To get through the room, you should have holes on at least one of lower three squares on the front and back side of the room." You can see current positions of holes by a monitor. Before going to Dice Room, you should rotate the cube so that you can go though the room. But you know rotating a room takes some time and you don’t have much time, so you should minimize the number of rotation. How many rotations do you need to make it possible to get though Dice Room? Input The input consists of several datasets. Each dataset contains 6 tables of 3x3 characters which describe the initial position of holes on each side. Each character is either '*' or '.'. A hole is indicated by '*'. The order which six sides appears in is: front, right, back, left, top, bottom. The order and orientation of them are described by this development view: <image> Figure 4: Dice Room <image> Figure 5: available dice rotations <image> There is a blank line after each dataset. The end of the input is indicated by a single '#'. Output Print the minimum number of rotations needed in a line for each dataset. You may assume all datasets have a solution. Examples Input ... ... .*. ... ... .*. ... ... ... ... ... .*. ... ... .*. ... ... ... ... .*. ... *.. ... ..* *.* *.* *.* *.* .*. *.* *.. .*. ..* *.* ... *.* ... .*. .*. ... .** *.. ... ... .*. .*. ... *.. ..* ... .** ... *.. ... # Output 3 1 0 Input ... ... .*. ... ... .*. ... ... ... ... ... .*. ... ... .*. ... ... ... ... .*. ... *.. ... ..* *.* *.* *.* *.* .*. *.* *.. .*. ..* *.* ... *.* ... .*. .*. ... .** *.. ... ... .*. .*. ... *.. ..* ... .** ... *.. ... Output 3 1 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. So, the Berland is at war with its eternal enemy Flatland again, and Vasya, an accountant, was assigned to fulfil his duty to the nation. Right now the situation in Berland is dismal — their both cities are surrounded! The armies of flatlanders stand on the borders of circles, the circles' centers are in the surrounded cities. At any moment all points of the flatland ring can begin to move quickly in the direction of the city — that's the strategy the flatlanders usually follow when they besiege cities. The berlanders are sure that they can repel the enemy's attack if they learn the exact time the attack starts. For that they need to construct a radar that would register any movement at the distance of at most r from it. Thus, we can install a radar at such point, that at least one point of the enemy ring will be in its detecting range (that is, at a distance of at most r). Then the radar can immediately inform about the enemy's attack. Due to the newest technologies, we can place a radar at any point without any problems. But the problem is that the berlanders have the time to make only one radar. Besides, the larger the detection radius (r) is, the more the radar costs. That's why Vasya's task (that is, your task) is to find the minimum possible detection radius for the radar. In other words, your task is to find the minimum radius r (r ≥ 0) such, that a radar with radius r can be installed at some point and it can register the start of the movements of both flatland rings from that point. In this problem you can consider the cities as material points, the attacking enemy rings - as circles with centers in the cities, the radar's detection range — as a disk (including the border) with the center at the point where the radar is placed. Input The input files consist of two lines. Each line represents the city and the flatland ring that surrounds it as three space-separated integers xi, yi, ri (|xi|, |yi| ≤ 104; 1 ≤ ri ≤ 104) — the city's coordinates and the distance from the city to the flatlanders, correspondingly. It is guaranteed that the cities are located at different points. Output Print a single real number — the minimum detection radius of the described radar. The answer is considered correct if the absolute or relative error does not exceed 10 - 6. Examples Input 0 0 1 6 0 3 Output 1.000000000000000 Input -10 10 3 10 -10 3 Output 11.142135623730951 Note The figure below shows the answer to the first sample. In this sample the best decision is to put the radar at point with coordinates (2, 0). <image> The figure below shows the answer for the second sample. In this sample the best decision is to put the radar at point with coordinates (0, 0). <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. You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$. Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds? For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 20$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 10$). The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$). -----Output----- For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise. -----Examples----- Input 5 5 4 -7 -1 5 10 1 0 3 1 10 100 4 -3 2 10 2 9 25 -171 250 174 152 242 100 -205 -258 Output YES YES NO YES YES -----Note----- In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds: $a_1 = 4 = 2 - (-2) = b_2 - b_5$; $a_2 = -7 = -9 - (-2) = b_1 - b_5$; $a_3 = -1 = 1 - 2 = b_3 - b_2$; $a_4 = 5 = 3 - (-2) = b_4 - b_5$; $a_5 = 10 = 1 - (-9) = b_3 - b_1$. In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$. In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct. When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed. As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears. For each query find the moment when the server will finish to process it or print -1 if this query will be rejected. -----Input----- The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue. Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1. -----Output----- Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected. -----Examples----- Input 5 1 2 9 4 8 10 9 15 2 19 1 Output 11 19 -1 21 22 Input 4 1 2 8 4 8 10 9 15 2 Output 10 18 27 -1 -----Note----- Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 play a strategic video game (yeah, we ran out of good problem legends). In this game you control a large army, and your goal is to conquer $n$ castles of your opponent. Let's describe the game process in detail. Initially you control an army of $k$ warriors. Your enemy controls $n$ castles; to conquer the $i$-th castle, you need at least $a_i$ warriors (you are so good at this game that you don't lose any warriors while taking over a castle, so your army stays the same after the fight). After you take control over a castle, you recruit new warriors into your army — formally, after you capture the $i$-th castle, $b_i$ warriors join your army. Furthermore, after capturing a castle (or later) you can defend it: if you leave at least one warrior in a castle, this castle is considered defended. Each castle has an importance parameter $c_i$, and your total score is the sum of importance values over all defended castles. There are two ways to defend a castle: if you are currently in the castle $i$, you may leave one warrior to defend castle $i$; there are $m$ one-way portals connecting the castles. Each portal is characterised by two numbers of castles $u$ and $v$ (for each portal holds $u > v$). A portal can be used as follows: if you are currently in the castle $u$, you may send one warrior to defend castle $v$. Obviously, when you order your warrior to defend some castle, he leaves your army. You capture the castles in fixed order: you have to capture the first one, then the second one, and so on. After you capture the castle $i$ (but only before capturing castle $i + 1$) you may recruit new warriors from castle $i$, leave a warrior to defend castle $i$, and use any number of portals leading from castle $i$ to other castles having smaller numbers. As soon as you capture the next castle, these actions for castle $i$ won't be available to you. If, during some moment in the game, you don't have enough warriors to capture the next castle, you lose. Your goal is to maximize the sum of importance values over all defended castles (note that you may hire new warriors in the last castle, defend it and use portals leading from it even after you capture it — your score will be calculated afterwards). Can you determine an optimal strategy of capturing and defending the castles? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 5000$, $0 \le m \le \min(\frac{n(n - 1)}{2}, 3 \cdot 10^5)$, $0 \le k \le 5000$) — the number of castles, the number of portals and initial size of your army, respectively. Then $n$ lines follow. The $i$-th line describes the $i$-th castle with three integers $a_i$, $b_i$ and $c_i$ ($0 \le a_i, b_i, c_i \le 5000$) — the number of warriors required to capture the $i$-th castle, the number of warriors available for hire in this castle and its importance value. Then $m$ lines follow. The $i$-th line describes the $i$-th portal with two integers $u_i$ and $v_i$ ($1 \le v_i < u_i \le n$), meaning that the portal leads from the castle $u_i$ to the castle $v_i$. There are no two same portals listed. It is guaranteed that the size of your army won't exceed $5000$ under any circumstances (i. e. $k + \sum\limits_{i = 1}^{n} b_i \le 5000$). -----Output----- If it's impossible to capture all the castles, print one integer $-1$. Otherwise, print one integer equal to the maximum sum of importance values of defended castles. -----Examples----- Input 4 3 7 7 4 17 3 0 8 11 2 0 13 3 5 3 1 2 1 4 3 Output 5 Input 4 3 7 7 4 17 3 0 8 11 2 0 13 3 5 3 1 2 1 4 1 Output 22 Input 4 3 7 7 4 17 3 0 8 11 2 0 14 3 5 3 1 2 1 4 3 Output -1 -----Note----- The best course of action in the first example is as follows: capture the first castle; hire warriors from the first castle, your army has $11$ warriors now; capture the second castle; capture the third castle; hire warriors from the third castle, your army has $13$ warriors now; capture the fourth castle; leave one warrior to protect the fourth castle, your army has $12$ warriors now. This course of action (and several other ones) gives $5$ as your total score. The best course of action in the second example is as follows: capture the first castle; hire warriors from the first castle, your army has $11$ warriors now; capture the second castle; capture the third castle; hire warriors from the third castle, your army has $13$ warriors now; capture the fourth castle; leave one warrior to protect the fourth castle, your army has $12$ warriors now; send one warrior to protect the first castle through the third portal, your army has $11$ warriors now. This course of action (and several other ones) gives $22$ as your total score. In the third example it's impossible to capture the last castle: you need $14$ warriors to do so, but you can accumulate no more than $13$ without capturing it. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i. How many kinds of items did you get? -----Constraints----- - 1 \leq N \leq 2\times 10^5 - S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive). -----Input----- Input is given from Standard Input in the following format: N S_1 : S_N -----Output----- Print the number of kinds of items you got. -----Sample Input----- 3 apple orange apple -----Sample Output----- 2 You got two kinds of items: apple and orange. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0. -----Input----- The first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 10^9, inclusive. -----Output----- Print the maximum possible even sum that can be obtained if we use some of the given integers. -----Examples----- Input 3 1 2 3 Output 6 Input 5 999999999 999999999 999999999 999999999 999999999 Output 3999999996 -----Note----- In the first sample, we can simply take all three integers for a total sum of 6. In the second sample Wet Shark should take any four out of five integers 999 999 999. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, Petya learned about a new game "Slay the Dragon". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of $n$ heroes, the strength of the $i$-th hero is equal to $a_i$. According to the rules of the game, exactly one hero should go kill the dragon, all the others will defend the castle. If the dragon's defense is equal to $x$, then you have to send a hero with a strength of at least $x$ to kill it. If the dragon's attack power is $y$, then the total strength of the heroes defending the castle should be at least $y$. The player can increase the strength of any hero by $1$ for one gold coin. This operation can be done any number of times. There are $m$ dragons in the game, the $i$-th of them has defense equal to $x_i$ and attack power equal to $y_i$. Petya was wondering what is the minimum number of coins he needs to spend to defeat the $i$-th dragon. Note that the task is solved independently for each dragon (improvements are not saved). -----Input----- The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — number of heroes. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{12}$), where $a_i$ is the strength of the $i$-th hero. The third line contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of dragons. The next $m$ lines contain two integers each, $x_i$ and $y_i$ ($1 \le x_i \le 10^{12}; 1 \le y_i \le 10^{18}$) — defense and attack power of the $i$-th dragon. -----Output----- Print $m$ lines, $i$-th of which contains a single integer — the minimum number of coins that should be spent to defeat the $i$-th dragon. -----Examples----- Input 4 3 6 2 3 5 3 12 7 9 4 14 1 10 8 7 Output 1 2 4 0 2 -----Note----- To defeat the first dragon, you can increase the strength of the third hero by $1$, then the strength of the heroes will be equal to $[3, 6, 3, 3]$. To kill the dragon, you can choose the first hero. To defeat the second dragon, you can increase the forces of the second and third heroes by $1$, then the strength of the heroes will be equal to $[3, 7, 3, 3]$. To kill the dragon, you can choose a second hero. To defeat the third dragon, you can increase the strength of all the heroes by $1$, then the strength of the heroes will be equal to $[4, 7, 3, 4]$. To kill the dragon, you can choose a fourth hero. To defeat the fourth dragon, you don't need to improve the heroes and choose a third hero to kill the dragon. To defeat the fifth dragon, you can increase the strength of the second hero by $2$, then the strength of the heroes will be equal to $[3, 8, 2, 3]$. To kill the dragon, you can choose a second hero. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 given picture with size $w \times h$. Determine if the given picture has a single "+" shape or not. A "+" shape is described below: A "+" shape has one center nonempty cell. There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction. All other cells are empty. Find out if the given picture has single "+" shape. -----Input----- The first line contains two integers $h$ and $w$ ($1 \le h$, $w \le 500$) — the height and width of the picture. The $i$-th of the next $h$ lines contains string $s_{i}$ of length $w$ consisting "." and "*" where "." denotes the empty space and "*" denotes the non-empty space. -----Output----- If the given picture satisfies all conditions, print "YES". Otherwise, print "NO". You can output each letter in any case (upper or lower). -----Examples----- Input 5 6 ...... ..*... .****. ..*... ..*... Output YES Input 3 5 ..*.. ****. .*... Output NO Input 7 7 ....... ...*... ..****. ...*... ...*... ....... .*..... Output NO Input 5 6 ..**.. ..**.. ****** ..**.. ..**.. Output NO Input 3 7 .*...*. ***.*** .*...*. Output NO Input 5 10 .......... ..*....... .*.******. ..*....... .......... Output NO -----Note----- In the first example, the given picture contains one "+". In the second example, two vertical branches are located in a different column. In the third example, there is a dot outside of the shape. In the fourth example, the width of the two vertical branches is $2$. In the fifth example, there are two shapes. In the sixth example, there is an empty space inside of the shape. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 strings A, B and C. Each of these is a string of length N consisting of lowercase English letters. Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation: * Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter. What is the minimum number of operations required to achieve the objective? Constraints * 1 \leq N \leq 100 * Each of the strings A, B and C is a string of length N. * Each character in each of the strings A, B and C is a lowercase English letter. Input Input is given from Standard Input in the following format: N A B C Output Print the minimum number of operations required. Examples Input 4 west east wait Output 3 Input 9 different different different Output 0 Input 7 zenkoku touitsu program Output 13 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a string, determine if it's a valid identifier. ## Here is the syntax for valid identifiers: * Each identifier must have at least one character. * The first character must be picked from: alpha, underscore, or dollar sign. The first character cannot be a digit. * The rest of the characters (besides the first) can be from: alpha, digit, underscore, or dollar sign. In other words, it can be any valid identifier character. ### Examples of valid identifiers: * i * wo_rd * b2h ### Examples of invalid identifiers: * 1i * wo rd * !b2h Write your solution by modifying this code: ```python def is_valid(idn): ``` Your solution should implemented in the function "is_valid". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 ```python function repeating_fractions(numerator, denominator) ``` that given two numbers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part has repeated digits, replace those digits with a single digit in parentheses. For example: ```python repeating_fractions(1,1) === '1' repeating_fractions(1,3) === '0.(3)' repeating_fractions(2,888) === '0.(0)(2)5(2)5(2)5(2)5(2)5(2)' ``` Write your solution by modifying this code: ```python def repeating_fractions(n,d): ``` Your solution should implemented in the function "repeating_fractions". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the i-th rest point the distance from the start equals xi, and its picturesqueness equals bi. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates xi. Every day the traveler wants to cover the distance l. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance l every day and visit the most picturesque places. Let's assume that if the traveler covers distance rj in a day, then he feels frustration <image>, and his total frustration over the hike is calculated as the total frustration on all days. Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used. The traveler's path must end in the farthest rest point. Input The first line of the input contains integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 105) — the number of rest points and the optimal length of one day path. Then n lines follow, each line describes one rest point as a pair of integers xi, bi (1 ≤ xi, bi ≤ 106). No two rest points have the same xi, the lines are given in the order of strictly increasing xi. Output Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to n in the order of increasing xi. The last printed number must be equal to n. Examples Input 5 9 10 10 20 10 30 1 31 5 40 10 Output 1 2 4 5 Note In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as <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. It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ — the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----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 are given an undirected graph with N vertices and 0 edges. Process Q queries of the following types. * `0 u v`: Add an edge (u, v). * `1 u v`: Print 1 if u and v are in the same connected component, 0 otherwise. Constraints * 1 \leq N \leq 200,000 * 1 \leq Q \leq 200,000 * 0 \leq u_i, v_i \lt N Input Input is given from Standard Input in the following format: N Q t_1 u_1 v_1 t_2 u_2 v_2 : t_Q u_Q v_Q 出力 For each query of the latter type, print the answer. Example Input 4 7 1 0 1 0 0 1 0 2 3 1 0 1 1 1 2 0 0 2 1 1 3 Output 0 1 0 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. Your task is to write function which takes string and list of delimiters as an input and returns list of strings/characters after splitting given string. Example: ```python multiple_split('Hi, how are you?', [' ']) => ['Hi,', 'how', 'are', 'you?'] multiple_split('1+2-3', ['+', '-']) => ['1', '2', '3'] ``` List of delimiters is optional and can be empty, so take that into account. Important note: Result cannot contain empty string. Write your solution by modifying this code: ```python def multiple_split(string, delimiters=[]): ``` Your solution should implemented in the function "multiple_split". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 3 2 R1 Output 3 1 2 4 5 6 7 8 9 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses. -----Constraints----- - All values in input are integers. - 2 \leq N \leq 10^5 - 1 \leq M \leq 10^5 - 1 \leq A_i < B_i \leq N - All pairs (A_i, B_i) are distinct. - The inconvenience is initially 0. -----Input----- Input is given from Standard Input in the following format: N M A_1 B_1 A_2 B_2 \vdots A_M B_M -----Output----- In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses. Note that the answer may not fit into a 32-bit integer type. -----Sample Input----- 4 5 1 2 3 4 1 3 2 3 1 4 -----Sample Output----- 0 0 4 5 6 For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 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. Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw n numbers a_1, a_2, …, a_n without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string s which contained that information. <image> It's easy to notice that Barbara has to place n - 1 symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in s). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression! Input The first line of the input contains a single integer n (1 ≤ n ≤ 10^5) — the amount of numbers on the paper. The second line of the input contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 9), where a_i is the i-th element of a. The third line of the input contains the string s (1 ≤ |s| ≤ 3) — symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "*". It is also guaranteed that all symbols in the string are distinct. Output Print n numbers separated by n - 1 symbols — a mathematical expression with the greatest result. If there are multiple equally valid results — output any one of them. Examples Input 3 2 2 0 +-* Output 2*2-0 Input 4 2 1 1 2 +* Output 2+1+1+2 Note The following answers also fit the first example: "2+2+0", "2+2-0", "2*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. Petya is a math teacher. n of his students has written a test consisting of m questions. For each student, it is known which questions he has answered correctly and which he has not. If the student answers the j-th question correctly, he gets p_j points (otherwise, he gets 0 points). Moreover, the points for the questions are distributed in such a way that the array p is a permutation of numbers from 1 to m. For the i-th student, Petya knows that he expects to get x_i points for the test. Petya wonders how unexpected the results could be. Petya believes that the surprise value of the results for students is equal to ∑_{i=1}^{n} |x_i - r_i|, where r_i is the number of points that the i-th student has got for the test. Your task is to help Petya find such a permutation p for which the surprise value of the results is maximum possible. If there are multiple answers, print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. The first line of each test case contains two integers n and m (1 ≤ n ≤ 10; 1 ≤ m ≤ 10^4) — the number of students and the number of questions, respectively. The second line contains n integers x_1, x_2, ..., x_n (0 ≤ x_i ≤ (m(m+1))/(2)), where x_i is the number of points that the i-th student expects to get. This is followed by n lines, the i-th line contains the string s_i (|s_i| = m; s_{i, j} ∈ \{0, 1\}), where s_{i, j} is 1 if the i-th student has answered the j-th question correctly, and 0 otherwise. The sum of m for all test cases does not exceed 10^4. Output For each test case, print m integers — a permutation p for which the surprise value of the results is maximum possible. If there are multiple answers, print any of them. Example Input 3 4 3 5 1 2 2 110 100 101 100 4 4 6 2 0 10 1001 0010 0110 0101 3 6 20 3 15 010110 000101 111111 Output 3 1 2 2 3 4 1 3 1 4 5 2 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. Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets. Mike has $n$ sweets with sizes $a_1, a_2, \ldots, a_n$. All his sweets have different sizes. That is, there is no such pair $(i, j)$ ($1 \leq i, j \leq n$) such that $i \ne j$ and $a_i = a_j$. Since Mike has taught for many years, he knows that if he gives two sweets with sizes $a_i$ and $a_j$ to one child and $a_k$ and $a_p$ to another, where $(a_i + a_j) \neq (a_k + a_p)$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset. -----Input----- The first line contains one integer $n$ ($2 \leq n \leq 1\,000$) — the number of sweets Mike has. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^5$) — the sizes of the sweets. It is guaranteed that all integers are distinct. -----Output----- Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset. -----Examples----- Input 8 1 8 3 11 4 9 2 7 Output 3 Input 7 3 1 7 11 9 2 12 Output 2 -----Note----- In the first example, Mike can give $9+2=11$ to one child, $8+3=11$ to another one, and $7+4=11$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution. In the second example, Mike can give $3+9=12$ to one child and $1+11$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 2 1 2 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. A chessboard n × m in size is given. During the zero minute we repaint all the black squares to the 0 color. During the i-th minute we repaint to the i color the initially black squares that have exactly four corner-adjacent squares painted i - 1 (all such squares are repainted simultaneously). This process continues ad infinitum. You have to figure out how many squares we repainted exactly x times. The upper left square of the board has to be assumed to be always black. Two squares are called corner-adjacent, if they have exactly one common point. Input The first line contains integers n and m (1 ≤ n, m ≤ 5000). The second line contains integer x (1 ≤ x ≤ 109). Output Print how many squares will be painted exactly x times. Examples Input 3 3 1 Output 4 Input 3 3 2 Output 1 Input 1 1 1 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. Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly. Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so on (arbitrary number of changes could be made in each number). At some point it turned out that if we sum the resulting numbers, then the number of zeroes with which the sum will end would be maximum among the possible variants of digit permutations in those numbers. Given number n, can you find the two digit permutations that have this property? Input The first line contains a positive integer n — the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes. Output Print two permutations of digits of number n, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them. Examples Input 198 Output 981 819 Input 500 Output 500 500 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower. He defines the beauty of the tower as follows: * The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored. Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors. Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K. How many such ways are there to paint the tower? Find the count modulo 998244353. Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other. Constraints * 1 ≤ N ≤ 3×10^5 * 1 ≤ A,B ≤ 3×10^5 * 0 ≤ K ≤ 18×10^{10} * All values in the input are integers. Input Input is given from Standard Input in the following format: N A B K Output Print the number of the ways to paint tiles, modulo 998244353. Examples Input 4 1 2 5 Output 40 Input 2 5 6 0 Output 1 Input 90081 33447 90629 6391049189 Output 577742975 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp likes numbers that are divisible by 3. He has a huge number $s$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $3$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $m$ such cuts, there will be $m+1$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $3$. For example, if the original number is $s=3121$, then Polycarp can cut it into three parts with two cuts: $3|1|21$. As a result, he will get two numbers that are divisible by $3$. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by $3$ that Polycarp can obtain? -----Input----- The first line of the input contains a positive integer $s$. The number of digits of the number $s$ is between $1$ and $2\cdot10^5$, inclusive. The first (leftmost) digit is not equal to 0. -----Output----- Print the maximum number of numbers divisible by $3$ that Polycarp can get by making vertical cuts in the given number $s$. -----Examples----- Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 -----Note----- In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $3$. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $33$ digits 0. Each of the $33$ digits 0 forms a number that is divisible by $3$. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $0$, $9$, $201$ and $81$ are divisible by $3$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of N integers and prints the number of prime numbers in the list. Constraints 1 ≤ N ≤ 10000 2 ≤ an element of the list ≤ 108 Input The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output Print the number of prime numbers in the given list. Examples Input 5 2 3 4 5 6 Output 3 Input 11 7 8 9 10 11 12 13 14 15 16 17 Output 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1, 3, 9]$ and $[2, 3, 1]$ are not. It follows from the definition that any sequence of length one or two is an arithmetic progression. Polycarp found some sequence of positive integers $[b_1, b_2, \dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged. Determine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible. It is possible that the resulting sequence contains element equals $0$. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 100\,000)$ — the number of elements in $b$. The second line contains a sequence $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 10^{9})$. -----Output----- If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position). -----Examples----- Input 4 24 21 14 10 Output 3 Input 2 500 500 Output 0 Input 3 14 5 1 Output -1 Input 5 1 3 6 9 12 Output 1 -----Note----- In the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression. In the second example Polycarp should not change anything, because his sequence is an arithmetic progression. In the third example it is impossible to make an arithmetic progression. In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, which is an arithmetic progression. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called "Mau-Mau". To play Mau-Mau, you need a pack of $52$ cards. Each card has a suit (Diamonds — D, Clubs — C, Spades — S, or Hearts — H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A). At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table. In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card. -----Input----- The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand. Each string is two characters long. The first character denotes the rank and belongs to the set $\{{\tt 2}, {\tt 3}, {\tt 4}, {\tt 5}, {\tt 6}, {\tt 7}, {\tt 8}, {\tt 9}, {\tt T}, {\tt J}, {\tt Q}, {\tt K}, {\tt A}\}$. The second character denotes the suit and belongs to the set $\{{\tt D}, {\tt C}, {\tt S}, {\tt H}\}$. All the cards in the input are different. -----Output----- If it is possible to play a card from your hand, print one word "YES". Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Examples----- Input AS 2H 4C TH JH AD Output YES Input 2H 3D 4C AC KD AS Output NO Input 4D AS AC AD AH 5H Output YES -----Note----- In the first example, there is an Ace of Spades (AS) on the table. You can play an Ace of Diamonds (AD) because both of them are Aces. In the second example, you cannot play any card. In the third example, you can play an Ace of Diamonds (AD) because it has the same suit as a Four of Diamonds (4D), which lies on the table. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 friend won't stop texting his girlfriend. It's all he does. All day. Seriously. The texts are so mushy too! The whole situation just makes you feel ill. Being the wonderful friend that you are, you hatch an evil plot. While he's sleeping, you take his phone and change the autocorrect options so that every time he types "you" or "u" it gets changed to "your sister." Write a function called autocorrect that takes a string and replaces all instances of "you" or "u" (not case sensitive) with "your sister" (always lower case). Return the resulting string. Here's the slightly tricky part: These are text messages, so there are different forms of "you" and "u". For the purposes of this kata, here's what you need to support: "youuuuu" with any number of u characters tacked onto the end "u" at the beginning, middle, or end of a string, but NOT part of a word "you" but NOT as part of another word like youtube or bayou Write your solution by modifying this code: ```python def autocorrect(input): ``` Your solution should implemented in the function "autocorrect". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A country has coins with denominations ```python coins_list = d1 < d2 < · · · < dn. ``` You want to make change for n cents, using the smallest number of coins. ```python # Example 1: U.S. coins d1 = 1 d2 = 5 d3 = 10 d4 = 25 ## Optimal change for 37 cents – 1 quarter, 1 dime, 2 pennies. # Example 2: Alien Planet Z coins Z_coin_a = 1 Z_coin_b = 3 Z_coin_c = 4 ## Optimal change for 6 cents - 2 Z_coin_b's ``` Write a function that will take a list of coin denominations and a desired amount and provide the least amount of coins needed. Write your solution by modifying this code: ```python def loose_change(coins_list, amount_of_change): ``` Your solution should implemented in the function "loose_change". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are two buttons, one of size A and one of size B. When you press a button of size X, you get X coins and the size of that button decreases by 1. You will press a button twice. Here, you can press the same button twice, or press both buttons once. At most how many coins can you get? -----Constraints----- - All values in input are integers. - 3 \leq A, B \leq 20 -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the maximum number of coins you can get. -----Sample Input----- 5 3 -----Sample Output----- 9 You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads. <image> Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows: let starting_time be an array of length n current_time = 0 dfs(v): current_time = current_time + 1 starting_time[v] = current_time shuffle children[v] randomly (each permutation with equal possibility) // children[v] is vector of children cities of city v for u in children[v]: dfs(u) As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)). Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help. Input The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC. Output In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i]. Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6. Examples Input 7 1 2 1 1 4 4 Output 1.0 4.0 5.0 3.5 4.5 5.0 5.0 Input 12 1 1 2 2 4 4 3 3 1 10 8 Output 1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.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. Create a function `isAlt()` that accepts a string as an argument and validates whether the vowels (a, e, i, o, u) and consonants are in alternate order. ```python is_alt("amazon") // true is_alt("apple") // false is_alt("banana") // true ``` Arguments consist of only lowercase letters. Write your solution by modifying this code: ```python def is_alt(s): ``` Your solution should implemented in the function "is_alt". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 cube which consists of n × n × n small cubes. Small cubes have marks on their surfaces. An example where n = 4 is shown in the following figure. <image> Then, as shown in the figure above (right), make a hole that penetrates horizontally or vertically from the marked surface to the opposite surface. Your job is to create a program that reads the positions marked n and counts the number of small cubes with no holes. Input The input consists of several datasets. Each dataset is given in the following format: n h c1 a1 b1 c2 a2 b2 .. .. .. ch ah bh h is an integer indicating the number of marks. The h lines that follow enter the positions of the h marks. The coordinate axes shown in the figure below will be used to specify the position of the mark. (x, y, z) = (1, 1, 1) is the lower left cube, and (x, y, z) = (n, n, n) is the upper right cube. <image> ci is a string indicating the plane marked with the i-th. ci is one of "xy", "xz", and "yz", indicating that the i-th mark is on the xy, xz, and yz planes, respectively. ai and bi indicate the coordinates on the plane indicated by ci. For the xy, xz, and yz planes, ai and bi indicate the plane coordinates (x, y), (x, z), and (y, z), respectively. For example, in the above figure, the values ​​of ci, ai, and bi of marks A, B, and C are "xy 4 4", "xz 1 2", and "yz 2 3", respectively. When both n and h are 0, it indicates the end of input. You can assume that n ≤ 500 and h ≤ 200. Output For each dataset, print the number of non-perforated cubes on one line. Example Input 4 3 xy 4 4 xz 1 2 yz 2 3 4 5 xy 1 1 xy 3 3 xz 3 3 yz 2 1 yz 3 3 0 0 Output 52 46 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Constraints * 1 ≤ |V| ≤ 1000 * 0 ≤ |E| ≤ 2000 * -10000 ≤ di ≤ 10000 * There are no parallel edges * There are no self-loops Input An edge-weighted graph G (V, E) and the source r. |V| |E| r s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 |V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph. si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge. Output If the graph contains a negative cycle (a cycle whose sum of edge costs is a negative value) which is reachable from the source r, print NEGATIVE CYCLE in a line. Otherwise, print c0 c1 : c|V|-1 The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print "INF". Examples Input 4 5 0 0 1 2 0 2 3 1 2 -5 1 3 1 2 3 2 Output 0 2 -3 -1 Input 4 6 0 0 1 2 0 2 3 1 2 -5 1 3 1 2 3 2 3 1 0 Output NEGATIVE CYCLE Input 4 5 1 0 1 2 0 2 3 1 2 -5 1 3 1 2 3 2 Output INF 0 -5 -3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Implement `String#whitespace?(str)` (Ruby), `String.prototype.whitespace(str)` (JavaScript), `String::whitespace(str)` (CoffeeScript), or `whitespace(str)` (Python), which should return `true/True` if given object consists exclusively of zero or more whitespace characters, `false/False` otherwise. Write your solution by modifying this code: ```python def whitespace(string): ``` Your solution should implemented in the function "whitespace". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Problem KND is a student programmer at the University of Aizu. His chest is known to be very sexy. <image> For simplicity, the part of the skin that can be seen from the chest is represented by the isosceles triangle ABC in the figure. However, due to the slack in the clothes, the two sides AC and BC (where these lengths are l), which have the same length, actually have an additional length x minutes. In order to increase the area of ​​the open part, let's make two new triangular ADCs and BECs by pulling the slack part. Points D and E exist outside the triangle ABC. These two new triangles are caused by slack, and the sum of the lengths of side BE and side EC and the sum of the lengths of side AD and side DC must be l + x. You determine the points D and E so that the sum M of the areas of these three triangles is maximized. As KND's neighbor, you decide to write a program to calculate the maximum area of ​​skin (M) to look out of your clothes, using a, l, x as inputs to find out how sexy his chest is. did. Constraints The input satisfies the following conditions. * All inputs are integers. * 1 ≤ a ≤ 1000 * 1 ≤ l ≤ 1000 * 1 ≤ x ≤ 1000 Input The input consists of multiple test cases. One test case is given in the following format. The end of input is indicated by EOF. a l x here, * a: Length of side AB of triangle ABC * l: Length of two sides AC and BC of triangle ABC * x: Slack on two sides AC, BC Is. Output Output the maximum area for each test case on one line. This value should not differ more than 10-5 from the value of the judge output. Example Input 2 2 1 2 3 1 3 2 3 2 3 5 Output 3.9681187851 6.7970540913 6.5668891783 13.9527248554 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi is solving quizzes. He has easily solved all but the last one. The last quiz has three choices: 1, 2, and 3. With his supernatural power, Takahashi has found out that the choices A and B are both wrong. Print the correct choice for this problem. -----Constraints----- - Each of the numbers A and B is 1, 2, or 3. - A and B are different. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the correct choice. -----Sample Input----- 3 1 -----Sample Output----- 2 When we know 3 and 1 are both wrong, the correct choice is 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array $a[0 \ldots n-1]$ of length $n$ which consists of non-negative integers. Note that array indices start from zero. An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $i$ ($0 \le i \le n - 1$) the equality $i \bmod 2 = a[i] \bmod 2$ holds, where $x \bmod 2$ is the remainder of dividing $x$ by 2. For example, the arrays [$0, 5, 2, 1$] and [$0, 17, 0, 3$] are good, and the array [$2, 4, 6, 7$] is bad, because for $i=1$, the parities of $i$ and $a[i]$ are different: $i \bmod 2 = 1 \bmod 2 = 1$, but $a[i] \bmod 2 = 4 \bmod 2 = 0$. In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent). Find the minimum number of moves in which you can make the array $a$ good, or say that this is not possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 40$) — the length of the array $a$. The next line contains $n$ integers $a_0, a_1, \ldots, a_{n-1}$ ($0 \le a_i \le 1000$) — the initial array. -----Output----- For each test case, output a single integer — the minimum number of moves to make the given array $a$ good, or -1 if this is not possible. -----Example----- Input 4 4 3 2 7 6 3 3 2 6 1 7 7 4 9 2 1 18 3 0 Output 2 1 -1 0 -----Note----- In the first test case, in the first move, you can swap the elements with indices $0$ and $1$, and in the second move, you can swap the elements with indices $2$ and $3$. In the second test case, in the first move, you need to swap the elements with indices $0$ and $1$. In the third test case, you cannot make the array good. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9. You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom. -----Constraints----- - 1≤A,B≤5 - |S|=A+B+1 - S consists of - and digits from 0 through 9. -----Input----- Input is given from Standard Input in the following format: A B S -----Output----- Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise. -----Sample Input----- 3 4 269-6650 -----Sample Output----- Yes The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. CQXYM found a rectangle A of size n × m. There are n rows and m columns of blocks. Each block of the rectangle is an obsidian block or empty. CQXYM can change an obsidian block to an empty block or an empty block to an obsidian block in one operation. A rectangle M size of a × b is called a portal if and only if it satisfies the following conditions: * a ≥ 5,b ≥ 4. * For all 1 < x < a, blocks M_{x,1} and M_{x,b} are obsidian blocks. * For all 1 < x < b, blocks M_{1,x} and M_{a,x} are obsidian blocks. * For all 1<x<a,1<y<b, block M_{x,y} is an empty block. * M_{1, 1}, M_{1, b}, M_{a, 1}, M_{a, b} can be any type. Note that the there must be a rows and b columns, not b rows and a columns. Note that corners can be any type CQXYM wants to know the minimum number of operations he needs to make at least one sub-rectangle a portal. Input The first line contains an integer t (t ≥ 1), which is the number of test cases. For each test case, the first line contains two integers n and m (5 ≤ n ≤ 400, 4 ≤ m ≤ 400). Then n lines follow, each line contains m characters 0 or 1. If the j-th character of i-th line is 0, block A_{i,j} is an empty block. Otherwise, block A_{i,j} is an obsidian block. It is guaranteed that the sum of n over all test cases does not exceed 400. It is guaranteed that the sum of m over all test cases does not exceed 400. Output Output t answers, and each answer in a line. Examples Input 1 5 4 1000 0000 0110 0000 0001 Output 12 Input 1 9 9 001010001 101110100 000010011 100000001 101010101 110001111 000001111 111100000 000110000 Output 5 Note In the first test case, the final portal is like this: 1110 1001 1001 1001 0111 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the a_{i}-th chair, and his girlfriend, sitting on the b_{i}-th chair. The chairs were numbered 1 through 2n in clockwise direction. There was exactly one person sitting on each chair. [Image] There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that: Each person had exactly one type of food, No boy had the same type of food as his girlfriend, Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive. Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions. -----Input----- The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of pairs of guests. The i-th of the next n lines contains a pair of integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ 2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair. -----Output----- If there is no solution, print -1. Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2. If there are multiple solutions, print any of them. -----Example----- Input 3 1 4 2 5 3 6 Output 1 2 2 1 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. Mash 2 arrays together so that the returning array has alternating elements of the 2 arrays . Both arrays will always be the same length. eg. [1,2,3] + ['a','b','c'] = [1, 'a', 2, 'b', 3, 'c'] Write your solution by modifying this code: ```python def array_mash(a, b): ``` Your solution should implemented in the function "array_mash". The i Read the inputs from stdin solve the problem and write the answer to stdout (do 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 wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$) — the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≤ n ≤ 1000, <image>, 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 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. Write a program which reads an directed graph $G = (V, E)$, and finds the shortest distance from vertex $1$ to each vertex (the number of edges in the shortest path). Vertices are identified by IDs $1, 2, ... n$. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ denoting the number of vertices, is given. In the next $n$ lines, adjacent lists of vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices adjacent to $u$. Output For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$ and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in order of IDs. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 1 0 2 1 3 2 4 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. A university student, Takahashi, has to take N examinations and pass all of them. Currently, his readiness for the i-th examination is A_{i}, and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i-th examination. Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness. For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions: * The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal. * For every i, B_i \leq C_i holds. If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * A_i and B_i are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_{N} B_1 B_2 ... B_{N} Output Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions. If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1. Examples Input 3 2 3 5 3 4 1 Output 3 Input 3 2 3 3 2 2 1 Output 0 Input 3 17 7 1 25 6 14 Output -1 Input 12 757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604 74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212 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. Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'! To compose a story, Stephen wrote out $n$ words consisting of the first $5$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story. Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together. For example, the story consisting of three words "bac", "aaada", "e" is interesting (the letter 'a' occurs $5$ times, all other letters occur $4$ times in total). But the story consisting of two words "aba", "abcde" is not (no such letter that it occurs more than all other letters in total). You are given a sequence of $n$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of the words in the sequence. Then $n$ lines follow, each of them contains a word — a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i. e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$; the sum of the lengths of all words over all test cases doesn't exceed $4 \cdot 10^5$. -----Output----- For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story. -----Examples----- Input 6 3 bac aaada e 3 aba abcde aba 2 baba baba 4 ab ab c bc 5 cbdca d a d e 3 b c ca Output 3 2 0 2 3 2 -----Note----- In the first test case of the example, all $3$ words can be used to make an interesting story. The interesting story is "bac aaada e". In the second test case of the example, the $1$-st and the $3$-rd words can be used to make an interesting story. The interesting story is "aba aba". Stephen can't use all three words at the same time. In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $0$. In the fourth test case of the example, Stephen can use the $3$-rd and the $4$-th words to make an interesting story. The interesting story is "c bc". Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.