question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Recently, the Fair Nut has written k strings of length n, consisting of letters "a" and "b". He calculated c β€” the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time. Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string s and not bigger than string t. He is interested: what is the maximum value of c that he could get. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains two integers n and k (1 ≀ n ≀ 5 β‹… 10^5, 1 ≀ k ≀ 10^9). The second line contains a string s (|s| = n) β€” the string consisting of letters "a" and "b. The third line contains a string t (|t| = n) β€” the string consisting of letters "a" and "b. It is guaranteed that string s is lexicographically not bigger than t. Output Print one number β€” maximal value of c. Examples Input 2 4 aa bb Output 6 Input 3 3 aba bba Output 8 Input 4 5 abbb baaa Output 8 Note In the first example, Nut could write strings "aa", "ab", "ba", "bb". These 4 strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, 6 strings. In the second example, Nut could write strings "aba", "baa", "bba". In the third example, there are only two different strings that Nut could write. If both of them are written, c=8. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Two people are playing a game with a string s, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A player not able to make a turn loses. Your task is to determine which player will win if both play optimally. Input The only line contains the string s, consisting of lowercase latin letters (1 ≀ |s| ≀ 100 000), where |s| means the length of a string s. Output If the first player wins, print "Yes". If the second player wins, print "No". Examples Input abacaba Output No Input iiq Output Yes Input abba Output No Note In the first example the first player is unable to make a turn, so he loses. In the second example first player turns the string into "q", then second player is unable to move, so he loses. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers a and b. His goal is to find a non-negative integer k such that the least common multiple of a+k and b+k is the smallest possible. If there are multiple optimal integers k, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? Input The only line contains two integers a and b (1 ≀ a, b ≀ 10^9). Output Print the smallest non-negative integer k (k β‰₯ 0) such that the lowest common multiple of a+k and b+k is the smallest possible. If there are many possible integers k giving the same value of the least common multiple, print the smallest one. Examples Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 Note In the first test, one should choose k = 2, as the least common multiple of 6 + 2 and 10 + 2 is 24, which is the smallest least common multiple possible. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements? Input The first line contains an integer n (1 ≀ n ≀ 1000), where 2n is the number of elements in the array a. The second line contains 2n space-separated integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^6) β€” the elements of the array a. Output If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order. Examples Input 3 1 2 2 1 3 1 Output 2 1 3 1 1 2 Input 1 1 1 Output -1 Note In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal. In the second example, there's no solution. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area. The strange area is enclosed by three lines, x = l, y = a and x = r, as its left side, its bottom side and its right side respectively, where l, r and a can be any real numbers satisfying that l < r. The upper side of the area is boundless, which you can regard as a line parallel to the x-axis at infinity. The following figure shows a strange rectangular area. <image> A point (x_i, y_i) is in the strange rectangular area if and only if l < x_i < r and y_i > a. For example, in the above figure, p_1 is in the area while p_2 is not. Tokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other. Input The first line contains a single integer n (1 ≀ n ≀ 2 Γ— 10^5) β€” the number of points on the plane. The i-th of the next n lines contains two integers x_i, y_i (1 ≀ x_i, y_i ≀ 10^9) β€” the coordinates of the i-th point. All points are distinct. Output Print a single integer β€” the number of different non-empty sets of points she can obtain. Examples Input 3 1 1 1 2 1 3 Output 3 Input 3 1 1 2 1 3 1 Output 6 Input 4 2 1 2 2 3 1 3 2 Output 6 Note For the first example, there is exactly one set having k points for k = 1, 2, 3, so the total number is 3. For the second example, the numbers of sets having k points for k = 1, 2, 3 are 3, 2, 1 respectively, and their sum is 6. For the third example, as the following figure shows, there are * 2 sets having one point; * 3 sets having two points; * 1 set having four points. Therefore, the number of different non-empty sets in this example is 2 + 3 + 0 + 1 = 6. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$ We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1]. Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)? Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 ≀ x ≀ n, y ∈ path(x) \} \right| β‰₯ k. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^{18}). Output Print the only integer β€” the maximum value that is contained in at least k paths. Examples Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 Note In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11). In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11). In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. <image> The dragon has a hit point of x initially. When its hit point goes to 0 or under 0, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. * Void Absorption Assume that the dragon's current hit point is h, after casting this spell its hit point will become \left⌊ h/2 \rightβŒ‹ + 10. Here \left⌊ h/2 \rightβŒ‹ denotes h divided by two, rounded down. * Lightning Strike This spell will decrease the dragon's hit point by 10. Assume that the dragon's current hit point is h, after casting this spell its hit point will be lowered to h-10. Due to some reasons Kana can only cast no more than n Void Absorptions and m Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next t lines describe test cases. For each test case the only line contains three integers x, n, m (1≀ x ≀ 10^5, 0≀ n,m≀30) β€” the dragon's intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively. Output If it is possible to defeat the dragon, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower). Example Input 7 100 3 4 189 3 4 64 2 3 63 2 3 30 27 7 10 9 1 69117 21 2 Output YES NO NO YES YES YES YES Note One possible casting sequence of the first test case is shown below: * Void Absorption \left⌊ 100/2 \rightβŒ‹ + 10=60. * Lightning Strike 60-10=50. * Void Absorption \left⌊ 50/2 \rightβŒ‹ + 10=35. * Void Absorption \left⌊ 35/2 \rightβŒ‹ + 10=27. * Lightning Strike 27-10=17. * Lightning Strike 17-10=7. * Lightning Strike 7-10=-3. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not carry in his hands objects of two different types. In other words, Polycarpus can't carry both postcards and photos simultaneously. Sometimes he goes to the closet and puts the objects there, thus leaving his hands free. Polycarpus must put all the postcards and photos to the closet. He cannot skip objects. What minimum number of times he should visit the closet if he cannot carry more than 5 items? Input The only line of the input data contains a non-empty string consisting of letters "Π‘" and "P" whose length does not exceed 100 characters. If the i-th character in the string is the letter "Π‘", that means that the i-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And if the i-th character is the letter "P", than the i-th object on the wall is a photo. Output Print the only number β€” the minimum number of times Polycarpus has to visit the closet. Examples Input CPCPCPC Output 7 Input CCCCCCPPPPPP Output 4 Input CCCCCCPPCPPPPPPPPPP Output 6 Input CCCCCCCCCC Output 2 Note In the first sample Polycarpus needs to take one item to the closet 7 times. In the second sample Polycarpus can first take 3 postcards to the closet; then 3 more. He can take the 6 photos that are left in the similar way, going to the closet twice. In the third sample Polycarpus can visit the closet twice, both times carrying 3 postcards. Then he can take there 2 photos at once, then one postcard and finally, he can carry the last 10 photos if he visits the closet twice. In the fourth sample Polycarpus can visit the closet twice and take there all 10 postcards (5 items during each go). The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18. Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1. Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10. Input Input contains one integer number A (3 ≀ A ≀ 1000). Output Output should contain required average value in format Β«X/YΒ», where X is the numerator and Y is the denominator. Examples Input 5 Output 7/3 Input 3 Output 2/1 Note In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem: Given a number m, how many polynomials P with coefficients in set {\{0,1,2,3,4,5,6,7\}} have: P(2)=m? Help Jerry Mao solve the long standing problem! Input The first line contains a single integer t (1 ≀ t ≀ 5β‹… 10^5) - number of test cases. On next line there are t numbers, m_i (1 ≀ m_i ≀ 10^{18}) - meaning that in case i you should solve for number m_i. Output For each test case i, print the answer on separate lines: number of polynomials P as described in statement such that P(2)=m_i, modulo 10^9 + 7. Example Input 2 2 4 Output 2 4 Note In first case, for m=2, polynomials that satisfy the constraint are x and 2. In second case, for m=4, polynomials that satisfy the constraint are x^2, x + 2, 2x and 4. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: * the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in a_i minutes, * Petya goes to the restaurant i on his own and picks up the dish, he will spend b_i minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order n = 4 dishes and a = [3, 7, 4, 5], and b = [2, 1, 2, 4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5 minutes, and Petya will pick up the remaining dishes in 1 + 2 = 3 minutes. Thus, in 5 minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. Input The first line contains one positive integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of dishes that Petya wants to order. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^9) β€” the time of courier delivery of the dish with the number i. The third line of each test case contains n integers b_1 … b_n (1 ≀ b_i ≀ 10^9) β€” the time during which Petya will pick up the dish with the number i. The sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case output one integer β€” the minimum time after which all dishes can be at Petya's home. Example Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Suppose you have a sequence of k integers A = [a_1, a_2, ... , a_k] where each a_i β‰₯ 2. A sequence of prime integers P = [p_1, p_2, ..., p_k] is called suitable for the sequence A if a_1 is divisible by p_1, a_2 is divisible by p_2 and so on. A sequence of prime integers P is called friendly if there are no unique integers in this sequence. A sequence A is called ideal, if each sequence P that is suitable for A is friendly as well (i. e. there is no sequence P that is suitable for A, but not friendly). For example, the sequence [2, 4, 16] is ideal, while the sequence [2, 4, 6] is not ideal (there exists a sequence P = [2, 2, 3] which is suitable for A, but not friendly). You are given n different integers x_1, x_2, ..., x_n. You have to choose exactly k of them in such a way that they form an ideal sequence, or report that it is impossible. Note that no integer can be chosen more than once. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 1000). The second line contains n pairwise distinct integers x_1, x_2, ..., x_n (2 ≀ x_i ≀ 10^{18}). Output If it is impossible to choose exactly k integers from x_1, x_2, ..., x_n in such a way that the chosen integers form an ideal sequence, print 0. Otherwise, print k pairwise distinct integers β€” the elements of the chosen ideal sequence. If there are multiple answers, print any of them. Examples Input 3 3 2 4 6 Output 0 Input 3 3 2 4 16 Output 2 4 16 Input 4 3 2 4 6 16 Output 2 4 16 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. This is an interactive problem. There exists a matrix a of size n Γ— m (n rows and m columns), you know only numbers n and m. The rows of the matrix are numbered from 1 to n from top to bottom, and columns of the matrix are numbered from 1 to m from left to right. The cell on the intersection of the x-th row and the y-th column is denoted as (x, y). You are asked to find the number of pairs (r, c) (1 ≀ r ≀ n, 1 ≀ c ≀ m, r is a divisor of n, c is a divisor of m) such that if we split the matrix into rectangles of size r Γ— c (of height r rows and of width c columns, each cell belongs to exactly one rectangle), all those rectangles are pairwise equal. You can use queries of the following type: * ? h w i_1 j_1 i_2 j_2 (1 ≀ h ≀ n, 1 ≀ w ≀ m, 1 ≀ i_1, i_2 ≀ n, 1 ≀ j_1, j_2 ≀ m) β€” to check if non-overlapping subrectangles of height h rows and of width w columns of matrix a are equal or not. The upper left corner of the first rectangle is (i_1, j_1). The upper left corner of the second rectangle is (i_2, j_2). Subrectangles overlap, if they have at least one mutual cell. If the subrectangles in your query have incorrect coordinates (for example, they go beyond the boundaries of the matrix) or overlap, your solution will be considered incorrect. You can use at most 3 β‹… \left ⌊{ log_2{(n+m)} } \right βŒ‹ queries. All elements of the matrix a are fixed before the start of your program and do not depend on your queries. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of rows and columns, respectively. Output When ready, print a line with an exclamation mark ('!') and then the answer β€” the number of suitable pairs (r, c). After that your program should terminate. Interaction To make a query, print a line with the format "? h w i_1 j_1 i_2 j_2 ", where the integers are the height and width and the coordinates of upper left corners of non-overlapping rectangles, about which you want to know if they are equal or not. After each query read a single integer t (t is 0 or 1): if the subrectangles are equal, t=1, otherwise t=0. In case your query is of incorrect format or you asked more than 3 β‹… \left ⌊{ log_2{(n+m)} } \right βŒ‹ queries, you will receive the Wrong Answer verdict. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. It is guaranteed that the matrix a is fixed and won't change during the interaction process. Hacks format For hacks use the following format. The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of rows and columns in the matrix, respectively. Each of the next n lines contains m integers β€” the elements of matrix a. All the elements of the matrix must be integers between 1 and n β‹… m, inclusive. Example Input 3 4 1 1 1 0 Output ? 1 2 1 1 1 3 ? 1 2 2 1 2 3 ? 1 2 3 1 3 3 ? 1 1 1 1 1 2 ! 2 Note In the example test the matrix a of size 3 Γ— 4 is equal to: 1 2 1 2 3 3 3 3 2 1 2 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him? A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. Input The first line contains an integer n (2 ≀ n ≀ 100) β€” the length of the array a. The second line contains n integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 2000) β€” the elements of the array a. Output The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. Examples Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 Note In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient. In the second example, the array is already good, so you don't need to remove any elements. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. This is the easy version of the problem. The only difference is that here k=2. You can make hacks only if both the versions of the problem are solved. This is an interactive problem. Every decimal number has a base k equivalent. The individual digits of a base k number are called k-its. Let's define the k-itwise XOR of two k-its a and b as (a + b)mod k. The k-itwise XOR of two base k numbers is equal to the new number formed by taking the k-itwise XOR of their corresponding k-its. The k-itwise XOR of two decimal numbers a and b is denoted by aβŠ•_{k} b and is equal to the decimal representation of the k-itwise XOR of the base k representations of a and b. All further numbers used in the statement below are in decimal unless specified. When k = 2 (it is always true in this version), the k-itwise XOR is the same as the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). You have hacked the criminal database of Rockport Police Department (RPD), also known as the Rap Sheet. But in order to access it, you require a password. You don't know it, but you are quite sure that it lies between 0 and n-1 inclusive. So, you have decided to guess it. Luckily, you can try at most n times without being blocked by the system. But the system is adaptive. Each time you make an incorrect guess, it changes the password. Specifically, if the password before the guess was x, and you guess a different number y, then the system changes the password to a number z such that xβŠ•_{k} z=y. Guess the password and break into the system. Input The first line of input contains a single integer t (1≀ t≀ 10 000) denoting the number of test cases. t test cases follow. The first line of each test case contains two integers n (1≀ n≀ 2β‹… 10^5) and k (k=2). It is guaranteed that the sum of n over all test cases does not exceed 2β‹… 10^5. Interaction For each test case, first read two integers n and k. Then you may ask up to n queries. For each query, print a single integer y (0≀ y≀ 2β‹… 10^7). Let the current password be x. After that, read an integer r. If x=y, you will read r=1 and the test case is solved. You must then continue solving the remaining test cases. Else, you will read r=0. At this moment the password is changed to a number z such that xβŠ•_{k} z=y. After printing a query, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. If you ask an invalid query or exceed n queries, you will read r=-1 and you will receive the Wrong Answer verdict. Make sure to exit immediately to avoid unexpected verdicts. Note that the interactor is adaptive. That is, the original password is not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial password such that all the answers to the queries are consistent. Hacks: To use hacks, use the following format of tests: The first line should contain a single integer t (1≀ t≀ 10 000) β€” the number of test cases. The first and only line of each test case should contain two integers n (1≀ n≀ 2β‹… 10^5) and k (k=2) denoting the number of queries and the base respectively. The optimal original password is automatically decided by the adaptive interactor. You must ensure that the sum of n over all test cases does not exceed 2β‹… 10^5. Example Input 1 5 2 0 0 1 Output 3 4 5 Note In the example test case, the hidden password is 2. The first query is 3. It is not equal to the current password. So, 0 is returned, and the password is changed to 1 since 2βŠ•_2 1=3. The second query is 4. It is not equal to the current password. So, 0 is returned, and the password is changed to 5 since 1βŠ•_2 5=4. The third query is 5. It is equal to the current password. So, 1 is returned, and the job is done. Note that this initial password is taken just for the sake of explanation. When you submit, the interactor might behave differently because it is adaptive. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost). But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths. A simple path is the path which goes through every Berland city not more than once. The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it. Note how the fools' paths are given in the input. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the number of cities. Each of the next n - 1 lines contains two space-separated integers ui, vi (1 ≀ ui, vi ≀ n, ui β‰  vi), that means that there is a road connecting cities ui and vi. The next line contains integer k (0 ≀ k ≀ 105) β€” the number of pairs of fools who visit each other. Next k lines contain two space-separated numbers. The i-th line (i > 0) contains numbers ai, bi (1 ≀ ai, bi ≀ n). That means that the fool number 2i - 1 lives in city ai and visits the fool number 2i, who lives in city bi. The given pairs describe simple paths, because between every pair of cities there is only one simple path. Output Print n - 1 integer. The integers should be separated by spaces. The i-th number should equal the number of fools who can go on the i-th road. The roads are numbered starting from one in the order, in which they occur in the input. Examples Input 5 1 2 1 3 2 4 2 5 2 1 4 3 5 Output 2 1 1 1 Input 5 3 4 4 5 1 4 2 4 3 2 3 1 3 3 5 Output 3 1 1 1 Note In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones. In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: 1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct. 2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β‰  si + 1(1 ≀ i < n). 3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest. Help him find such string or state that such string doesn't exist. String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes. Input A single line contains two positive integers n and k (1 ≀ n ≀ 106, 1 ≀ k ≀ 26) β€” the string's length and the number of distinct letters. Output In a single line print the required string. If there isn't such string, print "-1" (without the quotes). Examples Input 7 4 Output ababacd Input 4 7 Output -1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account owes the bank money. Ilya the Lion has recently had a birthday, so he got a lot of gifts. One of them (the gift of the main ZooVille bank) is the opportunity to delete the last digit or the digit before last from the state of his bank account no more than once. For example, if the state of Ilya's bank account is -123, then Ilya can delete the last digit and get his account balance equal to -12, also he can remove its digit before last and get the account balance equal to -13. Of course, Ilya is permitted not to use the opportunity to delete a digit from the balance. Ilya is not very good at math, and that's why he asks you to help him maximize his bank account. Find the maximum state of the bank account that can be obtained using the bank's gift. Input The single line contains integer n (10 ≀ |n| ≀ 109) β€” the state of Ilya's bank account. Output In a single line print an integer β€” the maximum state of the bank account that Ilya can get. Examples Input 2230 Output 2230 Input -10 Output 0 Input -100003 Output -10000 Note In the first test sample Ilya doesn't profit from using the present. In the second test sample you can delete digit 1 and get the state of the account equal to 0. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them? Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares. Help Inna maximize the total joy the hares radiate. :) Input The first line of the input contains integer n (1 ≀ n ≀ 3000) β€” the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≀ ai, bi, ci ≀ 105. Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full. Output In a single line, print the maximum possible total joy of the hares Inna can get by feeding them. Examples Input 4 1 2 3 4 4 3 2 1 0 1 1 0 Output 13 Input 7 8 5 7 6 1 8 9 2 7 9 5 4 3 1 2 3 3 4 1 1 3 Output 44 Input 3 1 1 1 1 2 1 1 1 1 Output 4 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current sequence and adds them c times to the end. More formally, if we represent the current sequence as a1, a2, ..., an, then after we apply the described operation, the sequence transforms into a1, a2, ..., an[, a1, a2, ..., al] (the block in the square brackets must be repeated c times). A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja. Input The first line contains integer m (1 ≀ m ≀ 105) β€” the number of stages to build a sequence. Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer xi (1 ≀ xi ≀ 105) β€” the number to add. Type 2 means copying a prefix of length li to the end ci times, in this case the line further contains two integers li, ci (1 ≀ li ≀ 105, 1 ≀ ci ≀ 104), li is the length of the prefix, ci is the number of copyings. It is guaranteed that the length of prefix li is never larger than the current length of the sequence. The next line contains integer n (1 ≀ n ≀ 105) β€” the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the elements that Sereja is interested in, in the order in which their numbers occur in the input. Examples Input 6 1 1 1 2 2 2 1 1 3 2 5 2 1 4 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output 1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≀ i ≀ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≀ i < n), ai + 1 - ai = k, where k is the number the Queen chose. Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes? Input The first line contains two space-separated integers: n, k (1 ≀ n, k ≀ 1000). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the heights of the trees in the row. Output In the first line print a single integer p β€” the minimum number of minutes the gardener needs. In the next p lines print the description of his actions. If the gardener needs to increase the height of the j-th (1 ≀ j ≀ n) tree from the left by x (x β‰₯ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≀ j ≀ n) tree from the left by x (x β‰₯ 1) meters, print on the corresponding line "- j x". If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them. Examples Input 4 1 1 2 1 5 Output 2 + 3 2 - 4 1 Input 4 1 1 2 3 4 Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Right now you are to solve a very, very simple problem β€” to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both by one; you may choose two adjacent even numbers and divide both by two. Nothing else. Crack the safe! Input The single line of the input contains four space-separated integer positive numbers not greater than 109 each β€” four numbers on the circle in consecutive order. Output The output should contain "-1" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification). If there are several solutions, output any of them. Examples Input 1 1 1 1 Output Input 1 2 4 2 Output /2 /3 Input 3 3 1 1 Output +1 /1 /1 Input 2 1 2 4 Output /3 /4 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 organizing a cycling race on the streets of the city. The city contains n junctions, some pairs of them are connected by roads; on each road you can move in any direction. No two roads connect the same pair of intersections, and no road connects the intersection with itself. You want the race to be open to both professional athletes and beginner cyclists, and that's why you will organize the race in three nominations: easy, moderate and difficult; each participant will choose the more suitable nomination. For each nomination you must choose the route β€” the chain of junctions, consecutively connected by roads. Routes must meet the following conditions: * all three routes should start at the same intersection, and finish at the same intersection (place of start and finish can't be the same); * to avoid collisions, no two routes can have common junctions (except for the common start and finish), and can not go along the same road (irrespective of the driving direction on the road for those two routes); * no route must pass twice through the same intersection or visit the same road twice (irrespective of the driving direction on the road for the first and second time of visit). Preparing for the competition is about to begin, and you need to determine the routes of the race as quickly as possible. The length of the routes is not important, it is only important that all the given requirements were met. Input The first line contains two integers n and m (1 ≀ n, m ≀ 2Β·105) β€” the number of intersections and roads, respectively. The following m lines contain two integers β€” the numbers of the intersections connected by a road (the intersections are numbered starting with 1). It is guaranteed that each pair of intersections is connected by no more than one road, and no road connects the intersection to itself. Please note that it is not guaranteed that you can get from any junction to any other one by using the roads. Output If it is possible to create the routes, in the first line print "YES". In the next three lines print the descriptions of each of the three routes in the format "l p1 ... pl", where l is the number of intersections in the route, and p1, ..., pl are their numbers in the order they follow. The routes must meet all the requirements specified in the statement. If it is impossible to make the routes in accordance with the requirements, print NO. Examples Input 4 4 1 2 2 3 3 4 4 1 Output NO Input 5 6 1 2 1 3 1 4 2 5 3 5 4 5 Output YES 3 5 4 1 3 5 3 1 3 5 2 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β€” the start time li and the finish time ri (li ≀ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≀ n ≀ 5Β·105) β€” number of orders. The following n lines contain integer values li and ri each (1 ≀ li ≀ ri ≀ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are <image> roads in total. It takes exactly y seconds to traverse any single road. A spanning tree is a set of roads containing exactly n - 1 roads such that it's possible to travel between any two cities using only these roads. Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from y to x seconds. Note that it's not guaranteed that x is smaller than y. You would like to travel through all the cities using the shortest path possible. Given n, x, y and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once. Input The first line of the input contains three integers n, x and y (2 ≀ n ≀ 200 000, 1 ≀ x, y ≀ 109). Each of the next n - 1 lines contains a description of a road in the spanning tree. The i-th of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n) β€” indices of the cities connected by the i-th road. It is guaranteed that these roads form a spanning tree. Output Print a single integer β€” the minimum number of seconds one needs to spend in order to visit all the cities exactly once. Examples Input 5 2 3 1 2 1 3 3 4 5 3 Output 9 Input 5 3 2 1 2 1 3 3 4 5 3 Output 8 Note In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is <image>. In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is <image>. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n. Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree β€” he can tell you only three values n, d and h: * The tree had exactly n vertices. * The tree had diameter d. In other words, d was the biggest distance between two vertices. * Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex. The distance between two vertices of the tree is the number of edges on the simple path between them. Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1". Input The first line contains three integers n, d and h (2 ≀ n ≀ 100 000, 1 ≀ h ≀ d ≀ n - 1) β€” the number of vertices, diameter, and height after rooting in vertex 1, respectively. Output If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes). Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order. Examples Input 5 3 2 Output 1 2 1 3 3 4 3 5 Input 8 5 2 Output -1 Input 8 4 2 Output 4 8 5 7 2 3 8 1 2 1 5 6 1 5 Note Below you can see trees printed to the output in the first sample and the third sample. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs. The stairs consists of n steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number n. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction "up" changes to "down", the direction "down" β€” to the direction "up". Olga always moves to the next step in the direction which is shown on the pointer above the step. If Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the n-th) step. In one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second. For each step find the duration of the dream if Olga was at this step at the beginning of the dream. Olga's fall also takes one second, so if she was on the first step and went down, she would wake up in the next second. Input The first line contains single integer n (1 ≀ n ≀ 106) β€” the number of steps on the stairs. The second line contains a string s with the length n β€” it denotes the initial direction of pointers on the stairs. The i-th character of string s denotes the direction of the pointer above i-th step, and is either 'U' (it means that this pointer is directed up), or 'D' (it means this pointed is directed down). The pointers are given in order from bottom to top. Output Print n numbers, the i-th of which is equal either to the duration of Olga's dream or to - 1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the i-th step. Examples Input 3 UUD Output 5 6 3 Input 10 UUDUDUUDDU Output 5 12 23 34 36 27 18 11 6 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi. Not all of sellers are honest, so now some products could be more expensive than after a week of discounts. Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items. Input In the first line there are two positive integer numbers n and k (1 ≀ n ≀ 2Β·105, 0 ≀ k ≀ n) β€” total number of items to buy and minimal number of items Igor wants to by right now. The second line contains sequence of integers a1, a2, ..., an (1 ≀ ai ≀ 104) β€” prices of items during discounts (i.e. right now). The third line contains sequence of integers b1, b2, ..., bn (1 ≀ bi ≀ 104) β€” prices of items after discounts (i.e. after a week). Output Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now. Examples Input 3 1 5 4 6 3 1 5 Output 10 Input 5 3 3 4 7 10 3 4 5 5 12 5 Output 25 Note In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10. In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2n flowers, numbered from 1 to 2n clockwise, each of which has a colour among all n possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals n. Additionally, if flowers u and v are of the same colour, then flowers opposite to u and opposite to v should be of the same colour as well β€” symmetry is beautiful! Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with n = 6 that cover all possibilities. <image> The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1 Γ— 3 Γ— 1 Γ— 3 = 9 β€” the segments are {2}, {4, 5, 6}, {8} and {10, 11, 12}. While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998 244 353. Two arrangements are considered different, if a pair (u, v) (1 ≀ u, v ≀ 2n) exists such that flowers u and v are of the same colour in one of them, but not in the other. Input The first and only line of input contains a lonely positive integer n (3 ≀ n ≀ 50 000) β€” the number of colours present on the Floral Clock. Output Output one integer β€” the sum of beauty over all possible arrangements of flowers, modulo 998 244 353. Examples Input 3 Output 24 Input 4 Output 4 Input 7 Output 1316 Input 15 Output 3436404 Note With n = 3, the following six arrangements each have a beauty of 2 Γ— 2 = 4. <image> While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself. Input The first line contains single integer q (1 ≀ q ≀ 105) β€” the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≀ ni ≀ 109) β€” the i-th query. Output For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings. Examples Input 1 12 Output 3 Input 2 6 8 Output 1 2 Input 3 1 2 3 Output -1 -1 -1 Note 12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. β€” Thanks a lot for today. β€” I experienced so many great things. β€” You gave me memories like dreams... But I have to leave now... β€” One last request, can you... β€” Help me solve a Codeforces problem? β€” ...... β€” What? Chtholly has been thinking about a problem for days: If a number is palindrome and length of its decimal representation without leading zeros is even, we call it a zcy number. A number is palindrome means when written in decimal representation, it contains no leading zeros and reads the same forwards and backwards. For example 12321 and 1221 are palindromes and 123 and 12451 are not. Moreover, 1221 is zcy number and 12321 is not. Given integers k and p, calculate the sum of the k smallest zcy numbers and output this sum modulo p. Unfortunately, Willem isn't good at solving this kind of problems, so he asks you for help! Input The first line contains two integers k and p (1 ≀ k ≀ 105, 1 ≀ p ≀ 109). Output Output single integer β€” answer to the problem. Examples Input 2 100 Output 33 Input 5 30 Output 15 Note In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22. In the second example, <image>. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. And where the are the phone numbers? You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t. It's guaranteed that the answer exists. Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}. String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a. Input The first line of input contains two space separated integers n and k (1 ≀ n, k ≀ 100 000) β€” the length of s and the required length of t. The second line of input contains the string s consisting of n lowercase English letters. Output Output the string t conforming to the requirements above. It's guaranteed that the answer exists. Examples Input 3 3 abc Output aca Input 3 2 abc Output ac Input 3 3 ayy Output yaa Input 2 3 ba Output baa Note In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Rachel, being an awesome android programmer, just finished an App that will let us draw a triangle by selecting three points on the touch-plane.Now She asks her friend Bruce to draw a Right-Angled Triangle (we'll call it RAT) by selecting 3 integral points on the plane. A RAT is a triangle with Non-Zero area and a right-angle. But Bruce, being in a hurry as always, makes a mistake on drawing the points. Now Rachel's app can fix Bruce's mistake if only one coordinate of any one point can be adjusted within a distance of 1 units. i.e., if the drawn triangle can be made a RAT by adjusting any one coordinate within 1 unit of distance. Your task is simple, given 6 coordinates (each Integer pair representing a point), you have to tell can Rachel's app correct Bruce's mistake automatically? Input: First line containing T, the number of test cases. Then T lines follow with 6 space separated coordinates. Output: print "YES" in a newline if it's possible. otherwise print "NO" in a newline. Constraints: 1 ≀ T ≀ 100 -1000 ≀ xi,yi ≀ 1000 SAMPLE INPUT 3 -1 0 2 0 0 1 2 3 4 5 6 6 0 0 2 0 10 10 SAMPLE OUTPUT YES NO NO Explanation Case 1: For (-1, 0), (2, 0) and (0, 1), shifting (-1, 0) to (0, 0) makes it a RAT. Case 2 and Case 3: No any possible shift results into a RAT. Note that any point can be moved either in x- or in y- direction at a time by 1 unit. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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, you have been given the task of handling the entire Taxi Network of Berland City. Berland city has a huge number of taxi travellers, and you need to help them in transportation in the most efficient manner. To be precise, this city consists of N users who want to travel via a Taxi today. You have a total of M taxis and need to cater to the users using these taxis. Each user has two parameters associated with them, S_{i} and J_{i}, denoting the time at which a user requests a taxi and the travel time required to reach the destination of this particular user. Each taxi can be used by a maximum of 1 user at each point in time. If, at any point in time a user requests a taxi and all M taxis are busy, then this user's request is rejected. If multiple taxis are available at the time of the request of a user, the taxi with the lowest index that is available is alloted to them. Now, you need to find for each user, the index of the taxi alloted to them. If a particular user's request is rejected, print "-1" (without quotes) for them. Note: For the purpose of the problem, we consider a user gets their taxi immediately if their request is accepted. The taxi's are enumerated from 1 to M. A taxi is considered to be free as soon as the previous user's journey ends. It is guaranteed that the request time of each user is unique. Input Format: The first line contains two integers N and M denoting the number of users and the number of taxis respectively. Each of the next N lines contains 2 space separated integers S_{i} and J_{i} denoting the request and journey time of the i^{th} user. Output Format: For each user from 1 to N, print the index number of the taxi alloted to them. If a user's request is rejected , print "-1"(without quotes) for them. Constraints: 1 ≀ N ≀ 10^5 1 ≀ M ≀ 100 1 ≀ S_{i} ,J_{i} ≀ 10^9 SAMPLE INPUT 5 5 1 100 2 100 3 100 4 100 5 100 SAMPLE OUTPUT 1 2 3 4 5 Explanation Here, user 1 is given taxi 1, user 2 is given taxi 2, user 3 is given taxi 3 and so on for user 4and 5. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Max feels lonely after shifting to a new locality, as he does not have any friend there. So his parents bought him a new number from the Integers SuperMarket! Every child in the locality has bought a number from the same market. He takes the number to go play with other children in the locality. But to his surprise, there is a rule in the locality, two people A and B can only be considered friends if numbers with A and B are not Coprime, i.e they have a common factor other than 1. You are given the number that Max bought and the numbers of other children in the locality. Can you find how many friends he can have? Input: First line contains an integer A, the number that Max has. Second line contains N, the count of children in the locality. Third line contains N space-separated integers, where Xi is the integer with the i^th child. Output: Output the maximum number of friends that Max can make. Constraints: 1 ≀ A ≀ 10^3 1 ≀ N ≀ 10^3 1 ≀ Xi ≀ 10^3 SAMPLE INPUT 6 3 4 7 12 SAMPLE OUTPUT 2 Explanation Puchi can become friends with First and Third child. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"? The answer can be enormous, so print it modulo (10^9+7). Constraints * K is an integer between 1 and 10^6 (inclusive). * S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters. Input Input is given from Standard Input in the following format: K S Output Print the number of strings satisfying the condition, modulo (10^9+7). Examples Input 5 oof Output 575111451 Input 37564 whydidyoudesertme Output 318008117 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. N friends of Takahashi has come to a theme park. To ride the most popular roller coaster in the park, you must be at least K centimeters tall. The i-th friend is h_i centimeters tall. How many of the Takahashi's friends can ride the roller coaster? Constraints * 1 \le N \le 10^5 * 1 \le K \le 500 * 1 \le h_i \le 500 * All values in input are integers. Input Input is given from Standard Input in the following format: N K h_1 h_2 \ldots h_N Output Print the number of people among the Takahashi's friends who can ride the roller coaster. Examples Input 4 150 150 140 100 200 Output 2 Input 1 500 499 Output 0 Input 5 1 100 200 300 400 500 Output 5 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Takahashi's mind, there is always an integer sequence of length 2 \times 10^9 + 1: A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9}) and an integer P. Initially, all the elements in the sequence A in Takahashi's mind are 0, and the value of the integer P is 0. When Takahashi eats symbols `+`, `-`, `>` and `<`, the sequence A and the integer P will change as follows: * When he eats `+`, the value of A_P increases by 1; * When he eats `-`, the value of A_P decreases by 1; * When he eats `>`, the value of P increases by 1; * When he eats `<`, the value of P decreases by 1. Takahashi has a string S of length N. Each character in S is one of the symbols `+`, `-`, `>` and `<`. He chose a pair of integers (i, j) such that 1 \leq i \leq j \leq N and ate the symbols that are the i-th, (i+1)-th, ..., j-th characters in S, in this order. We heard that, after he finished eating, the sequence A became the same as if he had eaten all the symbols in S from first to last. How many such possible pairs (i, j) are there? Constraints * 1 \leq N \leq 250000 * |S| = N * Each character in S is `+`, `-`, `>` or `<`. Input Input is given from Standard Input in the following format: N S Output Print the answer. Examples Input 5 +>+<- Output 3 Input 5 +>+-< Output 5 Input 48 -+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><< Output 475 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime. You are given Q queries. In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≀ x ≀ r_i. Constraints * 1≀Q≀10^5 * 1≀l_i≀r_i≀10^5 * l_i and r_i are odd. * All input values are integers. Input Input is given from Standard Input in the following format: Q l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line (1≀i≀Q) should contain the response to the i-th query. Examples Input 1 3 7 Output 2 Input 4 13 13 7 11 7 11 2017 2017 Output 1 0 0 1 Input 6 1 53 13 91 37 55 19 51 73 91 13 49 Output 4 4 1 1 1 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke, who loves animals, built a zoo. There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle. The animal numbered i (2≀i≀N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1. There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies. Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is `o`, the animal said that the two neighboring animals are of the same species, and if s_i is `x`, the animal said that the two neighboring animals are of different species. More formally, a sheep answered `o` if the two neighboring animals are both sheep or both wolves, and answered `x` otherwise. Similarly, a wolf answered `x` if the two neighboring animals are both sheep or both wolves, and answered `o` otherwise. Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print `-1`. Constraints * 3 ≀ N ≀ 10^{5} * s is a string of length N consisting of `o` and `x`. Input The input is given from Standard Input in the following format: N s Output If there does not exist an valid assignment that is consistent with s, print `-1`. Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s. * t is a string of length N consisting of `S` and `W`. * If t_i is `S`, it indicates that the animal numbered i is a sheep. If t_i is `W`, it indicates that the animal numbered i is a wolf. Examples Input 6 ooxoox Output SSSWWS Input 3 oox Output -1 Input 10 oxooxoxoox Output SSWWSSSWWS The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of N turns. In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition: (β€») After each turn, (the number of times the player has played Paper)≦(the number of times the player has played Rock). Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors. (For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.) With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts. Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score. The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is `g`, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in `p`, TopCoDeer will play Paper in the i-th turn. Constraints * 1≦N≦10^5 * N=|s| * Each character in s is `g` or `p`. * The gestures represented by s satisfy the condition (β€»). Input The input is given from Standard Input in the following format: s Output Print the AtCoDeer's maximum possible score. Examples Input gpg Output 0 Input ggppgggpgg Output 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. With the motto "Creating your own path," a shrine created a fortune-telling fortune with its own hands. Ask the person who draws the lottery to throw six stones first, then the line segment connecting the first and second of the thrown stones, the line segment connecting the third and fourth, the fifth and six The fortune is determined from the area of ​​the triangle whose apex is the intersection of the three line segments connecting the second line segment. The relationship between each fortune and the area of ​​the triangle is as follows. Area of ​​a triangle whose apex is the intersection of line segments | Fortune --- | --- Over 1,900,000 | Daikichi 1,000,000 or more and less than 1,900,000 | Nakayoshi (chu-kichi) 100,000 or more and less than 1,000,000 | Kichi Greater than 0 and less than 100,000 | Kokichi (syo-kichi) No triangle | Kyo However, the size of the area of ​​the triangle is determined by the priest by hand, so it cannot be said to be accurate and it takes time. So, as an excellent programmer living in the neighborhood, you decided to write a program as soon as possible to help the priest. Create a program that takes the information of three line segments as input and outputs the fortune from the area of ​​the triangle whose vertices are the intersections of the line segments. The line segment information is given the coordinates of the start point (x1, y1) and the coordinates of the end point (x2, y2), and the coordinates of the start point and the end point must be different. Also, if two or more line segments are on the same straight line, if there are two line segments that do not have intersections, or if three line segments intersect at one point, it is "no triangle". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by four zero lines. Each dataset is given in the following format: line1 line2 line3 The i-th line is given the information of the i-th line segment. Information for each line is given in the following format. x1 y1 x2 y2 Integers (x1, y1), (x2, y2) (-1000 ≀ x1, y1, x2, y2 ≀ 1000) representing the coordinates of the endpoints of the line segment are given, separated by blanks. Output The result of the fortune telling is output to one line for each data set. Example Input -3 -2 9 6 3 -2 7 6 -1 0 5 0 2 2 -1 -1 0 1 2 1 -3 -1 3 1 0 0 0 0 Output syo-kichi kyo The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. N different natural numbers are given. If you select four different ones and set them as $ A $, $ B $, $ C $, $ D $, the following formula $ \ Frac {A + B} {C --D} $ I want to find the maximum value of. Given N different natural numbers, choose 4 different from them and create a program to find the maximum value of the above formula. Input The input is given in the following format. N a1 a2 ... aN The number N (4 ≀ N ≀ 1000) of natural numbers is given in the first line. The value ai (1 ≀ ai ≀ 108) of each natural number is given in the second line. However, the same natural number does not appear more than once (ai β‰  aj for i β‰  j). Output Outputs the maximum value of the above formula as a real number for a given N natural numbers. However, the error must not exceed plus or minus 10-5. Examples Input 10 1 2 3 4 5 6 7 8 9 10 Output 19.00000 Input 5 22 100 42 3 86 Output 9.78947 Input 6 15 21 36 10 34 5 Output 18.00000 Input 4 100000 99999 8 1 Output 28571.285714 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Carving the cake 2 (Cake 2) JOI-kun and IOI-chan are twin brothers and sisters. JOI has been enthusiastic about making sweets lately, and JOI tried to bake a cake and eat it today, but when it was baked, IOI who smelled it came, so we decided to divide the cake. became. The cake is round. We made radial cuts from a point, cut the cake into N pieces, and numbered the pieces counterclockwise from 1 to N. That is, for 1 ≀ i ≀ N, the i-th piece is adjacent to the i βˆ’ 1st and i + 1st pieces (though the 0th is considered to be the Nth and the N + 1st is considered to be the 1st). The size of the i-th piece was Ai, but I was so bad at cutting that all Ai had different values. <image> Figure 1: Cake example (N = 5, A1 = 2, A2 = 8, A3 = 1, A4 = 10, A5 = 9) I decided to divide these N pieces by JOI-kun and IOI-chan. I decided to divide it as follows: 1. First, JOI chooses and takes one of N. 2. After that, starting with IOI-chan, IOI-chan and JOI-kun alternately take the remaining pieces one by one. However, if you can only take a piece that has already been taken at least one of the pieces on both sides, and there are multiple pieces that can be taken, IOI will choose the largest one and JOI will take it. You can choose what you like. JOI wants to maximize the total size of the pieces he will finally take. Task Given the number N of cake pieces and the size information of N pieces, create a program to find the maximum value of the total size of pieces that JOI can take. input Read the following input from standard input. * The integer N is written on the first line, which means that the cake is cut into N pieces. * The integer Ai is written on the i-th line (1 ≀ i ≀ N) of the following N lines, which indicates that the size of the i-th piece is Ai. output Output an integer representing the maximum value of the total size of pieces that JOI can take to the standard output on one line. Limits All input data satisfy the following conditions. * 1 ≀ N ≀ 20000. * 1 ≀ Ai ≀ 1 000 000 000. * Ai are all different. Input / output example Input example 1 Five 2 8 1 Ten 9 Output example 1 18 JOI is best to take the pieces as follows. 1. JOI takes the second piece. The size of this piece is 8. 2. IOI takes the first piece. The size of this piece is 2. 3. JOI takes the 5th piece. The size of this piece is 9. 4. IOI takes the 4th piece. The size of this piece is 10. 5. JOI takes the third piece. The size of this piece is 1. Finally, the total size of the pieces taken by JOI is 8 + 9 + 1 = 18. Input example 2 8 1 Ten Four Five 6 2 9 3 Output example 2 26 Input example 3 15 182243672 10074562 977552215 122668426 685444213 3784162 463324752 560071245 134465220 21447865 654556327 183481051 20041805 405079805 564327789 Output example 3 3600242976 The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics. Example Input 5 2 8 1 10 9 Output 18 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 many caves deep in mountains found in the countryside. In legend, each cave has a treasure hidden within the farthest room from the cave's entrance. The Shogun has ordered his Samurais to explore these caves with Karakuri dolls (robots) and to find all treasures. These robots move in the caves and log relative distances and directions between rooms. Each room in a cave is mapped to a point on an integer grid (x, y >= 0). For each cave, the robot always starts from its entrance, runs along the grid and returns to the entrance (which also serves as the exit). The treasure in each cave is hidden in the farthest room from the entrance, using Euclid distance for measurement, i.e. the distance of the room at point (x, y) from the entrance (0, 0) is defined as the square root of (x*x+y*y). If more than one room has the same farthest distance from the entrance, the treasure is hidden in the room having the greatest value of x. While the robot explores a cave, it records how it has moved. Each time it takes a new direction at a room, it notes the difference (dx, dy) from the last time it changed its direction. For example, suppose the robot is currently in the room at point (2, 4). If it moves to room (6, 4), the robot records (4, 0), i.e. dx=6-2 and dy=4-4. The first data is defined as the difference from the entrance. The following figure shows rooms in the first cave of the Sample Input. In the figure, the farthest room is the square root of 61 distant from the entrance. <image> Based on the records that the robots bring back, your job is to determine the rooms where treasures are hidden. Input In the first line of the input, an integer N showing the number of caves in the input is given. Integers dxi and dyi are i-th data showing the differences between rooms. Note that since the robot moves along the grid, either dxi or dyi is zero. An integer pair dxi = dyi = 0 signals the end of one cave's data which means the robot finished the exploration for the cave and returned back to the entrance. The coordinates are limited to (0,0)-(1000,1000). Output Print the position (x, y) of the treasure room on a line for each cave. Example Input 3 1 0 0 1 -1 0 1 0 0 5 -1 0 0 -1 5 0 0 1 0 -1 1 0 0 -4 -3 0 0 3 2 0 0 -2 -1 0 0 1 0 -1 1 0 0 2 -2 0 0 -3 3 0 0 4 -4 0 0 -5 -2 0 0 0 1 0 -1 0 0 0 2 0 0 1 -1 0 0 1 -1 0 0 -2 0 0 Output 6 5 1 0 2 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In the year 2020, a race of atomically energized cars will be held. Unlike today’s car races, fueling is not a concern of racing teams. Cars can run throughout the course without any refueling. Instead, the critical factor is tire (tyre). Teams should carefully plan where to change tires of their cars. The race is a road race having n checkpoints in the course. Their distances from the start are a1, a2, ... , and an (in kilometers). The n-th checkpoint is the goal. At the i-th checkpoint (i < n), tires of a car can be changed. Of course, a team can choose whether to change or not to change tires at each checkpoint. It takes b seconds to change tires (including overhead for braking and accelerating). There is no time loss at a checkpoint if a team chooses not to change tires. A car cannot run fast for a while after a tire change, because the temperature of tires is lower than the designed optimum. After running long without any tire changes, on the other hand, a car cannot run fast because worn tires cannot grip the road surface well. The time to run an interval of one kilometer from x to x + 1 is given by the following expression (in seconds). Here x is a nonnegative integer denoting the distance (in kilometers) from the latest checkpoint where tires are changed (or the start). r, v, e and f are given constants. 1/(v - e Γ— (x - r)) (if x β‰₯ r) 1/(v - f Γ— (r - x)) (if x < r) Your mission is to write a program to determine the best strategy of tire changes which minimizes the total time to the goal. Input The input consists of multiple datasets each corresponding to a race situation. The format of a dataset is as follows. n a1 a2 . . . an b r v e f The meaning of each of the input items is given in the problem statement. If an input line contains two or more input items, they are separated by a space. n is a positive integer not exceeding 100. Each of a1, a2, ... , and an is a positive integer satisfying 0 < a1 < a2 < . . . < an ≀ 10000. b is a positive decimal fraction not exceeding 100.0. r is a nonnegative integer satisfying 0 ≀ r ≀ an - 1. Each of v, e and f is a positive decimal fraction. You can assume that v - e Γ— (an - 1 - r) β‰₯ 0.01 and v - f Γ— r β‰₯ 0.01. The end of the input is indicated by a line with a single zero. Output For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the elapsed time at the goal (in seconds) when the best strategy is taken. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Example Input 2 2 3 1.0 1 1.0 0.1 0.3 5 5 10 15 20 25 0.15 1 1.0 0.04 0.5 10 1783 3640 3991 4623 5465 5481 6369 6533 6865 8425 4.172 72 59.4705 0.0052834 0.0611224 0 Output 3.5397 31.9249 168.6682 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Fair Chocolate-Cutting You are given a flat piece of chocolate of convex polygon shape. You are to cut it into two pieces of precisely the same amount with a straight knife. Write a program that computes, for a given convex polygon, the maximum and minimum lengths of the line segments that divide the polygon into two equal areas. The figures below correspond to first two sample inputs. Two dashed lines in each of them correspond to the equal-area cuts of minimum and maximum lengths. <image> Figure F.1. Sample Chocolate Pieces and Cut Lines Input The input consists of a single test case of the following format. $n$ $x_1$ $y_1$ ... $x_n$ $y_n$ The first line has an integer $n$, which is the number of vertices of the given polygon. Here, $n$ is between 3 and 5000, inclusive. Each of the following $n$ lines has two integers $x_i$ and $y_i$, which give the coordinates ($x_i, y_i$) of the $i$-th vertex of the polygon, in counterclockwise order. Both $x_i$ and $y_i$ are between 0 and 100 000, inclusive. The polygon is guaranteed to be simple and convex. In other words, no two edges of the polygon intersect each other and interior angles at all of its vertices are less than $180^\circ$. Output Two lines should be output. The first line should have the minimum length of a straight line segment that partitions the polygon into two parts of the equal area. The second line should have the maximum length of such a line segment. The answer will be considered as correct if the values output have an absolute or relative error less than $10^{-6}$. Sample Input 1 4 0 0 10 0 10 10 0 10 Sample Output 1 10 14.142135623730950488 Sample Input 2 3 0 0 6 0 3 10 Sample Output 2 4.2426406871192851464 10.0 Sample Input 3 5 0 0 99999 20000 100000 70000 33344 63344 1 50000 Sample Output 3 54475.580091580027976 120182.57592539864775 Sample Input 4 6 100 350 101 349 6400 3440 6400 3441 1200 7250 1199 7249 Sample Output 4 4559.2050019027964982 6216.7174287968524227 Example Input 4 0 0 10 0 10 10 0 10 Output 10 14.142135623730950488 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the Boolean expressions has terminals `0` `1` `a` `b` `c` `d` `-` `^` `*` `(` `)`, start symbol <E> and the following production rule: > <E> ::= `0` | `1` | `a` | `b` | `c` | `d` | `-`<E> | `(`<E>`^`<E>`)` | `(`<E>`*`<E>`)` Letters `a`, `b`, `c` and `d` represent Boolean variables that have values of either `0` or `1`. Operators are evaluated as shown in the Table below. In other words, `-` means negation (NOT), `^` means exclusive disjunction (XOR), and `*` means logical conjunction (AND). Table: Evaluations of operators <image> Write a program that calculates the length of the shortest expression that evaluates equal to the given expression with whatever values of the four variables. For example, `0`, that is the first expression in the sample input, cannot be shortened further. Therefore the shortest length for this expression is 1. For another example, `(a*(1*b))`, the second in the sample input, always evaluates equal to `(a*b)` and `(b*a)`, which are the shortest. The output for this expression, thus, should be `5`. Input The input consists of multiple datasets. A dataset consists of one line, containing an expression conforming to the grammar described above. The length of the expression is less than or equal to 16 characters. The end of the input is indicated by a line containing one Β‘`.`Β’ (period). The number of datasets in the input is at most 200. Output For each dataset, output a single line containing an integer which is the length of the shortest expression that has the same value as the given expression for all combinations of values in the variables. Sample Input 0 (a*(1*b)) (1^a) (-(-a*-b)*a) (a^(b^(c^d))) . Output for the Sample Input 1 5 2 1 13 Example Input 0 (a*(1*b)) (1^a) (-(-a*-b)*a) (a^(b^(c^d))) . Output 1 5 2 1 13 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ikta has an extraordinary feeling for undirected graphs. Ikta likes the undirected graph G and its two-point s, t pair (G, s, t) that has the greatest "beauty". The "beauty" of a pair (G, s, t) is the edge e = \\ {u, v \\} (u and v are two different points of G), and the shortest path from s to t in G. Is the number of things whose length is greater than the length of the shortest path from s to t in the undirected graph with g plus e. Your job is to write a program that seeks its "beauty" given a pair (G, s, t). Input The input is given in the following format. > N M s t > x1 y1 > ... > xi yi > ... > xM yM > First, the number of vertices, the number of edges, and the integers N, M, s, t representing the two vertices of the undirected graph are input. From the 2nd line to the M + 1th line, two vertices connected by edges are input. (However, let the set of G vertices be \\ {1, ..., N \\}.) Constraints Each variable being input satisfies the following constraints. * 2 ≀ N ≀ 100,000 * 1 ≀ M ≀ 300,000 * 1 ≀ s, t, xi, yi ≀ N * Different from s and t * Guaranteed to reach t from s Output When the given graph is G, output the "beauty" of the set (G, s, t) in one line. Examples Input 3 2 1 3 1 2 2 3 Output 1 Input 9 8 7 8 2 6 4 9 8 6 9 2 3 8 1 8 8 5 7 9 Output 7 Input 4 3 1 4 1 2 3 4 4 1 Output 0 Input 9 7 8 9 9 6 6 5 3 6 3 7 2 5 8 5 1 4 Output 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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: Isono, let's do that! --Sendame - story Nakajima "Isono ~, let's do that!" Isono "What is that, Nakajima" Nakajima "Look, that's that. It's hard to explain because I have to express it in letters for some reason." Isono "No, it seems that you can put in figures and photos, right?" <image> Nakajima "It's true!" Isono "So what are you going to do?" Nakajima "Look, the guy who rhythmically clapping his hands twice and then poses for defense, accumulation, and attack." Isono "Hmm, I don't know ..." Nakajima "After clapping hands twice, for example, if it was defense" <image> Nakajima "And if it was a reservoir" <image> Nakajima "If it was an attack" <image> Nakajima "Do you know who you are?" Isono "Oh! It's dramatically easier to understand when a photo is included!" Nakajima "This is the progress of civilization!" (It's been a long time since then) Hanazawa "Iso's" "I'm" "I'm" Two people "(cracking ... crackling ... crackling ...)" Hanazawa "You guys are doing that while sleeping ...!?" Hanazawa: "You've won the game right now ... Isono-kun, have you won now?" Isono "... Mr. Hanazawa was here ... Nakajima I'll do it again ... zzz" Nakajima "(Kokuri)" Two people "(cracking ... crackling ...)" Hanazawa "Already ... I'll leave that winning / losing judgment robot here ..." Then Mr. Hanazawa left. Please write that winning / losing judgment program. problem "That" is a game played by two people. According to the rhythm, the two people repeat the pose of defense, accumulation, or attack at the same time. Here, the timing at which two people pose is referred to as "times". In this game, when there is a victory or defeat in a certain time and there is no victory or defeat in the previous times, the victory or defeat is the victory or defeat of the game. Each of the two has a parameter called "attack power", and the attack power is 0 at the start of the game. When the attack pose is taken, it becomes as follows according to the attack power at that time. * If you pose for an attack when the attack power is 0, you will lose the foul. However, if the opponent also poses for an attack with an attack power of 0, he cannot win or lose at that time. * If you pose for an attack when your attack power is 1 or more, you will attack your opponent. Also, if the opponent also poses for an attack at that time, the player with the higher attack power wins. However, if both players pose for an attack with the same attack power, they cannot win or lose at that time. Also, at the end of the attack pose, your attack power becomes 0. Taking a puddle pose increases the player's attack power by 1. However, if the attack power is 5, the attack power remains at 5 even if the player poses in the pool. If the opponent attacks in the time when you take the pose of the pool, the opponent wins. In addition, if the opponent takes a pose other than the attack in the time when the pose of the pool is taken, the victory or defeat cannot be determined. If the opponent makes an attack with an attack power of 5 each time he takes a defensive pose, the opponent wins. On the other hand, if the opponent makes an attack with an attack power of 4 or less in the defense pose, or if the opponent poses for accumulation or defense, the victory or defeat cannot be achieved at that time. Even if you take a defensive pose, the attack power of that player does not change. Since the poses of both players are given in order, output the victory or defeat. Both players may continue to pose after the victory or defeat is decided, but the pose after the victory or defeat is decided is ignored. Input format The input is given in the following format. K I_1_1 ... I_K N_1_1 ... N_K The first line of input is given an integer K (1 ≀ K ≀ 100). The pose I_i (1 ≀ i ≀ K) taken by Isono is given to the K lines from the second line in order. Immediately after that, the pose N_i (1 ≀ i ≀ K) taken by Nakajima is given to the K line in order. I_i and N_i are one of β€œmamoru”, β€œtameru”, and β€œkougekida”. These strings, in order, represent defense, pool, and attack poses. Output format Output β€œIsono-kun” if Isono wins, β€œNakajima-kun” if Nakajima wins, and β€œHikiwake-kun” if you cannot win or lose in K times. Input example 1 3 tameru tameru tameru tameru kougekida tameru Output example 1 Nakajima-kun In the second time, Isono is in the pose of the pool, while Nakajima is attacking with an attack power of 1, so Nakajima wins. Input example 2 3 mamoru mamoru mamoru tameru tameru tameru Output example 2 Hikiwake-kun Neither attacked, so I couldn't win or lose. Input example 3 Five tameru tameru mamoru mamoru kougekida tameru tameru kougekida tameru kougekida Output example 3 Isono-kun There is no victory or defeat from the 1st to the 4th. In the 5th time, both players are posing for attack, but Isono's attack power is 2, while Nakajima's attack power is 1, so Isono wins. Input example 4 3 kougekida kougekida tameru kougekida mamoru kougekida Output example 4 Nakajima-kun In the first time, both players are posing for attack with 0 attack power, so there is no victory or defeat. In the second time, only Isono poses for an attack with an attack power of 0, so Nakajima wins. Input example 5 8 tameru mamoru tameru tameru tameru tameru tameru kougekida tameru kougekida mamoru mamoru mamoru mamoru mamoru mamoru Output example 5 Isono-kun In the second time, Nakajima poses for attack with an attack power of 1, but Isono poses for defense, so there is no victory or defeat. In the 7th time, Isono poses with an attack power of 5, so Isono's attack power remains at 5. In the 8th time, Isono poses for attack with an attack power of 5, and Nakajima poses for defense, so Isono wins. Example Input 3 tameru tameru tameru tameru kougekida tameru Output Nakajima-kun The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Print all subsets of a set $S$, which contains $0, 1, ... n-1$ as elements. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements. Constraints * $1 \leq n \leq 18$ Input The input is given in the following format. $n$ Output Print subsets ordered by their decimal integers. Print a subset in a line in the following format. $d$: $e_0$ $e_1$ ... Print ':' after the integer value $d$, then print elements $e_i$ in the subset in ascending order. Seprate two adjacency elements by a space character. Example Input 4 Output 0: 1: 0 2: 1 3: 0 1 4: 2 5: 0 2 6: 1 2 7: 0 1 2 8: 3 9: 0 3 10: 1 3 11: 0 1 3 12: 2 3 13: 0 2 3 14: 1 2 3 15: 0 1 2 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Asmany strings are strings of '0's and '1's that have as many 00 as 11. A string such as 00110001 consists of 3 "00" and 1 "11". Of course this is not an Asmany string. 0011, 1100, 000111000111 are Asmany strings. An L'th Asmany number is the number of Asmany strings of length L for all positive integers L. For esoteric purposes Chef had an oracle (a device) that was capable of answering whether a number that he entered was an Asmany number. The problem is that his oracle takes too long for large numbers. Him being Chef, he wants to ask the oracle very large numbers! You tell him that you can give him a better oracle (a program) that will tell him what he wants to know in the blink of an eye. Input The first Line contains a single number T, the number of test cases. Each test case contains 1 positive integer N, with not more than 1000 digits. Output Print YES if N is an Asmany number, NO otherwise. Constraints 1 ≀ T ≀ 100 1 ≀ Number of digits in N ≀ 1000 Sample Input 2 3 4 Sample Output NO YES Explanation 4 is an Asmany number. To be precise, it is the 4th Asmany number: There are 4 Asmany strings of length 4. 0011, 1100, 0101, 1010. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 start from some definitions. Strings A and B are called anagrams if it's possible to rearrange the letters of string A using all the original letters exactly once and achieve string B; in other words A and B are permutations of each other. For example, remote and meteor are anagrams, race and race are anagrams as well, while seat and tease aren't anagrams as tease contains an extra 'e'. String A is called a subsequence of string B if A can be obtained from B by removing some (possibly none) characters. For example, cat is a subsequence of scratch, rage is a subsequence of rage, and tame is not a subsequence of steam. String A is lexicographically smaller than string B of the same length if at the first position where A and B differ A contains a letter which is earlier in the alphabet than the corresponding letter in B. Recently, Ann received a set of strings consisting of small Latin letters a..z. 'What can I do with them?' -- she asked herself. 'What if I try to find the longest string which is a subsequence of every string from the set?'. Ann spent a lot of time trying to solve the problem... but all her attempts happened to be unsuccessful. She then decided to allow the sought string to be an anagram of some subsequence of every string from the set. This problem seemed to be easier to Ann, but she was too tired to solve it, so Ann asked for your help. So your task is, given a set of strings, to find the longest non-empty string which satisfies Ann. Moreover, if there are many such strings, choose the lexicographically smallest one. Input The first line of the input file contains one integer N -- the number of strings in the set (1 ≀ N ≀ 100). Each of the next N lines contains a non-empty string consisting only of small Latin letters a..z representing a string from the set. None of the strings contain more than 100 letters. Output Output the longest non-empty string satisfying Ann. If there are several such strings, output the lexicographically smallest one. If there are no such strings, output 'no such string' (quotes for clarity). Example Input: 3 hope elephant path Output: hp Input: 2 wall step Output: no such string Explanation: In the first test case the longest string appears to be two characters long. String 'hp' satisfies the requirements as it's an anagram of 'hp' which is a subsequence of 'hope' and an anagram of 'ph' which is a subsequence of both 'elephant' and 'path'. Note that string 'ph' also satisfies the requirements, but 'hp' is lexicographically smaller. In the second test case there is no such string. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≀ l_a ≀ r_a ≀ t_a - 1, 2 ≀ t_a ≀ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≀ l_b ≀ r_b ≀ t_b - 1, 2 ≀ t_b ≀ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day. In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j. Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them. Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?" Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task! Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≀ j ≀ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive. Input The first line contains an integer n (1 ≀ n ≀ 100 000) β€” the number of strings. The second line contains n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ 10^{18}) β€” the tuning of the ukulele. The third line contains an integer q (1 ≀ q ≀ 100 000) β€” the number of questions. The k-th among the following q lines contains two integers l_k,r_k (0 ≀ l_k ≀ r_k ≀ 10^{18}) β€” a question from the fleas. Output Output one number for each question, separated by spaces β€” the number of different pitches. Examples Input 6 3 1 4 1 5 9 3 7 7 0 2 8 17 Output 5 10 18 Input 2 1 500000000000000000 2 1000000000000000000 1000000000000000000 0 1000000000000000000 Output 2 1500000000000000000 Note For the first example, the pitches on the 6 strings are as follows. $$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$ There are 5 different pitches on fret 7 β€” 8, 10, 11, 12, 16. There are 10 different pitches on frets 0, 1, 2 β€” 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 has a string s. She really likes the letter "a". She calls a string good if strictly more than half of the characters in that string are "a"s. For example "aaabb", "axaa" are good strings, and "baca", "awwwa", "" (empty string) are not. Alice can erase some characters from her string s. She would like to know what is the longest string remaining after erasing some characters (possibly zero) to get a good string. It is guaranteed that the string has at least one "a" in it, so the answer always exists. Input The first line contains a string s (1 ≀ |s| ≀ 50) consisting of lowercase English letters. It is guaranteed that there is at least one "a" in s. Output Print a single integer, the length of the longest good string that Alice can get after erasing some characters from s. Examples Input xaxxxxa Output 3 Input aaabaa Output 6 Note In the first example, it's enough to erase any four of the "x"s. The answer is 3 since that is the maximum number of characters that can remain. In the second example, we don't need to erase any characters. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not. You are given a string s of length n, consisting of digits. In one operation you can delete any character from string s. For example, it is possible to obtain strings 112, 111 or 121 from string 1121. You need to determine whether there is such a sequence of operations (possibly empty), after which the string s becomes a telephone number. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The first line of each test case contains one integer n (1 ≀ n ≀ 100) β€” the length of string s. The second line of each test case contains the string s (|s| = n) consisting of digits. Output For each test print one line. If there is a sequence of operations, after which s becomes a telephone number, print YES. Otherwise, print NO. Example Input 2 13 7818005553535 11 31415926535 Output YES NO Note In the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Mislove had an array a_1, a_2, β‹…β‹…β‹…, a_n of n positive integers, but he has lost it. He only remembers the following facts about it: * The number of different numbers in the array is not less than l and is not greater than r; * For each array's element a_i either a_i = 1 or a_i is even and there is a number (a_i)/(2) in the array. For example, if n=5, l=2, r=3 then an array could be [1,2,2,4,4] or [1,1,1,1,2]; but it couldn't be [1,2,2,4,8] because this array contains 4 different numbers; it couldn't be [1,2,2,3,3] because 3 is odd and isn't equal to 1; and it couldn't be [1,1,2,2,16] because there is a number 16 in the array but there isn't a number 16/2 = 8. According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array. Input The only input line contains three integers n, l and r (1 ≀ n ≀ 1 000, 1 ≀ l ≀ r ≀ min(n, 20)) β€” an array's size, the minimal number and the maximal number of distinct elements in an array. Output Output two numbers β€” the minimal and the maximal possible sums of all elements in an array. Examples Input 4 2 2 Output 5 7 Input 5 1 5 Output 5 31 Note In the first example, an array could be the one of the following: [1,1,1,2], [1,1,2,2] or [1,2,2,2]. In the first case the minimal sum is reached and in the last case the maximal sum is reached. In the second example, the minimal sum is reached at the array [1,1,1,1,1], and the maximal one is reached at the array [1,2,4,8,16]. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 working for the Gryzzl company, headquartered in Pawnee, Indiana. The new national park has been opened near Pawnee recently and you are to implement a geolocation system, so people won't get lost. The concept you developed is innovative and minimalistic. There will be n antennas located somewhere in the park. When someone would like to know their current location, their Gryzzl hologram phone will communicate with antennas and obtain distances from a user's current location to all antennas. Knowing those distances and antennas locations it should be easy to recover a user's location... Right? Well, almost. The only issue is that there is no way to distinguish antennas, so you don't know, which distance corresponds to each antenna. Your task is to find a user's location given as little as all antennas location and an unordered multiset of distances. Input The first line of input contains a single integer n (2 ≀ n ≀ 10^5) which is the number of antennas. The following n lines contain coordinates of antennas, i-th line contain two integers x_i and y_i (0 ≀ x_i,y_i ≀ 10^8). It is guaranteed that no two antennas coincide. The next line of input contains integer m (1 ≀ n β‹… m ≀ 10^5), which is the number of queries to determine the location of the user. Following m lines contain n integers 0 ≀ d_1 ≀ d_2 ≀ ... ≀ d_n ≀ 2 β‹… 10^{16} each. These integers form a multiset of squared distances from unknown user's location (x;y) to antennas. For all test cases except the examples it is guaranteed that all user's locations (x;y) were chosen uniformly at random, independently from each other among all possible integer locations having 0 ≀ x, y ≀ 10^8. Output For each query output k, the number of possible a user's locations matching the given input and then output the list of these locations in lexicographic order. It is guaranteed that the sum of all k over all points does not exceed 10^6. Examples Input 3 0 0 0 1 1 0 1 1 1 2 Output 1 1 1 Input 4 0 0 0 1 1 0 1 1 2 0 1 1 2 2 5 5 8 Output 4 0 0 0 1 1 0 1 1 4 -1 -1 -1 2 2 -1 2 2 Note As you see in the second example, although initially a user's location is picked to have non-negative coordinates, you have to output all possible integer locations. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked n of them how much effort they needed to reach red. "Oh, I just spent x_i hours solving problems", said the i-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes (60 β‹… x_i), thanked the grandmasters and went home. Bob could write numbers with leading zeroes β€” for example, if some grandmaster answered that he had spent 2 hours, Bob could write 000120 instead of 120. Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: * rearranged its digits, or * wrote a random number. This way, Alice generated n numbers, denoted y_1, ..., y_n. For each of the numbers, help Bob determine whether y_i can be a permutation of a number divisible by 60 (possibly with leading zeroes). Input The first line contains a single integer n (1 ≀ n ≀ 418) β€” the number of grandmasters Bob asked. Then n lines follow, the i-th of which contains a single integer y_i β€” the number that Alice wrote down. Each of these numbers has between 2 and 100 digits '0' through '9'. They can contain leading zeroes. Output Output n lines. For each i, output the following. If it is possible to rearrange the digits of y_i such that the resulting number is divisible by 60, output "red" (quotes for clarity). Otherwise, output "cyan". Example Input 6 603 006 205 228 1053 0000000000000000000000000000000000000000000000 Output red red cyan cyan cyan red Note In the first example, there is one rearrangement that yields a number divisible by 60, and that is 360. In the second example, there are two solutions. One is 060 and the second is 600. In the third example, there are 6 possible rearrangments: 025, 052, 205, 250, 502, 520. None of these numbers is divisible by 60. In the fourth example, there are 3 rearrangements: 228, 282, 822. In the fifth example, none of the 24 rearrangements result in a number divisible by 60. In the sixth example, note that 000...0 is a valid solution. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside. The text is a string s of lowercase Latin letters. She considers a string t as hidden in string s if t exists as a subsequence of s whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices 1, 3, and 5, which form an arithmetic progression with a common difference of 2. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of S are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message! For example, in the string aaabb, a is hidden 3 times, b is hidden 2 times, ab is hidden 6 times, aa is hidden 3 times, bb is hidden 1 time, aab is hidden 2 times, aaa is hidden 1 time, abb is hidden 1 time, aaab is hidden 1 time, aabb is hidden 1 time, and aaabb is hidden 1 time. The number of occurrences of the secret message is 6. Input The first line contains a string s of lowercase Latin letters (1 ≀ |s| ≀ 10^5) β€” the text that Bessie intercepted. Output Output a single integer β€” the number of occurrences of the secret message. Examples Input aaabb Output 6 Input usaco Output 1 Input lol Output 2 Note In the first example, these are all the hidden strings and their indice sets: * a occurs at (1), (2), (3) * b occurs at (4), (5) * ab occurs at (1,4), (1,5), (2,4), (2,5), (3,4), (3,5) * aa occurs at (1,2), (1,3), (2,3) * bb occurs at (4,5) * aab occurs at (1,3,5), (2,3,4) * aaa occurs at (1,2,3) * abb occurs at (3,4,5) * aaab occurs at (1,2,3,4) * aabb occurs at (2,3,4,5) * aaabb occurs at (1,2,3,4,5) Note that all the sets of indices are arithmetic progressions. In the second example, no hidden string occurs more than once. In the third example, the hidden string is the letter l. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow: * \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x. * lcm(s) is the minimum positive integer x, that divisible on all integers from s. For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x. Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence. Input The first line contains one integer n\ (2≀ n≀ 100 000). The second line contains n integers, a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000). Output Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}). Examples Input 2 1 1 Output 1 Input 4 10 24 40 80 Output 40 Input 10 540 648 810 648 720 540 594 864 972 648 Output 54 Note For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1. For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well). Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has n planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format: * + x: the storehouse received a plank with length x * - x: one plank with length x was removed from the storehouse (it is guaranteed that the storehouse had some planks with length x). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help! We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides. Input The first line contains a single integer n (1 ≀ n ≀ 10^5): the initial amount of planks at the company's storehouse, the second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^5): the lengths of the planks. The third line contains a single integer q (1 ≀ q ≀ 10^5): the number of events in the company. Each of the next q lines contains a description of the events in a given format: the type of the event (a symbol + or -) is given first, then goes the integer x (1 ≀ x ≀ 10^5). Output After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower). Example Input 6 1 1 1 2 1 1 6 + 2 + 1 - 1 + 2 - 1 + 2 Output NO YES NO NO NO YES Note After the second event Applejack can build a rectangular storage using planks with lengths 1, 2, 1, 2 and a square storage using planks with lengths 1, 1, 1, 1. After the sixth event Applejack can build a rectangular storage using planks with lengths 2, 2, 2, 2 and a square storage using planks with lengths 1, 1, 1, 1. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 very popular game called Cubecraft. Initially, you have one stick and want to craft k torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: * exchange 1 stick for x sticks (you lose 1 stick and gain x sticks). * exchange y sticks for 1 coal (you lose y sticks and gain 1 coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least k torches. The answer always exists under the given constraints. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 2 β‹… 10^4) β€” the number of test cases. Then t test cases follow. The only line of the test case contains three integers x, y and k (2 ≀ x ≀ 10^9; 1 ≀ y, k ≀ 10^9) β€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. Output For each test case, print the answer: the minimum number of trades you need to craft at least k torches. The answer always exists under the given constraints. Example Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has sequence a consisting of n integers. The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements. Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence). A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times). Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7). Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a. Output On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7). Examples Input 3 2 10 10 10 Output 3 Input 4 2 4 4 7 7 Output 4 Note In the first sample all 3 subsequences of the needed length are considered lucky. In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 - 1 integers a_2, ..., a_n and a tree with n vertices rooted at vertex 1. The leaves are all at the same distance d from the root. Recall that a tree is a connected undirected graph without cycles. The distance between two vertices is the number of edges on the simple path between them. All non-root vertices with degree 1 are leaves. If vertices s and f are connected by an edge and the distance of f from the root is greater than the distance of s from the root, then f is called a child of s. Initially, there are a red coin and a blue coin on the vertex 1. Let r be the vertex where the red coin is and let b be the vertex where the blue coin is. You should make d moves. A move consists of three steps: * Move the red coin to any child of r. * Move the blue coin to any vertex b' such that dist(1, b') = dist(1, b) + 1. Here dist(x, y) indicates the length of the simple path between x and y. Note that b and b' are not necessarily connected by an edge. * You can optionally swap the two coins (or skip this step). Note that r and b can be equal at any time, and there is no number written on the root. After each move, you gain |a_r - a_b| points. What's the maximum number of points you can gain after d moves? 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 a single integer n (2 ≀ n ≀ 2 β‹… 10^5) β€” the number of vertices in the tree. The second line of each test case contains n-1 integers v_2, v_3, ..., v_n (1 ≀ v_i ≀ n, v_i β‰  i) β€” the i-th of them indicates that there is an edge between vertices i and v_i. It is guaranteed, that these edges form a tree. The third line of each test case contains n-1 integers a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” the numbers written on the vertices. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer: the maximum number of points you can gain after d moves. Example Input 4 14 1 1 1 2 3 4 4 5 5 6 7 8 8 2 3 7 7 6 9 5 9 7 3 6 6 5 6 1 2 2 3 4 32 78 69 5 41 15 1 15 1 10 4 9 11 2 4 1 8 6 10 11 62 13 12 43 39 65 42 86 25 38 19 19 43 62 15 11 2 7 6 9 8 10 1 1 1 5 3 15 2 50 19 30 35 9 45 13 24 8 44 16 26 10 40 Output 14 45 163 123 Note In the first test case, an optimal solution is to: * move 1: r = 4, b = 2; no swap; * move 2: r = 7, b = 6; swap (after it r = 6, b = 7); * move 3: r = 11, b = 9; no swap. The total number of points is |7 - 2| + |6 - 9| + |3 - 9| = 14. <image> In the second test case, an optimal solution is to: * move 1: r = 2, b = 2; no swap; * move 2: r = 3, b = 4; no swap; * move 3: r = 5, b = 6; no swap. The total number of points is |32 - 32| + |78 - 69| + |5 - 41| = 45. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Omkar has received a message from Anton saying "Your story for problem A is confusing. Just make a formal statement." Because of this, Omkar gives you an array a = [a_1, a_2, …, a_n] of n distinct integers. An array b = [b_1, b_2, …, b_k] is called nice if for any two distinct elements b_i, b_j of b, |b_i-b_j| appears in b at least once. In addition, all elements in b must be distinct. Can you add several (maybe, 0) integers to a to create a nice array b of size at most 300? If a is already nice, you don't have to add any elements. For example, array [3, 6, 9] is nice, as |6-3|=|9-6| = 3, which appears in the array, and |9-3| = 6, which appears in the array, while array [4, 2, 0, 6, 9] is not nice, as |9-4| = 5 is not present in the array. For integers x and y, |x-y| = x-y if x > y and |x-y| = y-x otherwise. Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 50), the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer n (2 ≀ n ≀ 100) β€” the length of the array a. The second line of each test case contains n distinct integers a_1, a_2, β‹…β‹…β‹…, a_n (-100 ≀ a_i ≀ 100) β€” the elements of the array a. Output For each test case, output one line containing YES if Omkar can create a nice array b by adding elements to a and NO otherwise. The case of each letter does not matter, so yEs and nO will also be accepted. If the first line is YES, output a second line containing a single integer k (n ≀ k ≀ 300). Then output one line containing k distinct integers b_1, b_2, β‹…β‹…β‹…, b_k (-10^9 ≀ b_i ≀ 10^9), the elements of the nice array b. b_1, b_2, β‹…β‹…β‹…, b_k can be in any order. For each a_i in a, a_i must appear at least once in b. It can be proved that if Omkar can create such an array b, then he can also do so in a way that satisfies the above constraints. If multiple solutions exist, you can print any. Example Input 4 3 3 0 9 2 3 4 5 -7 3 13 -2 8 4 4 8 12 6 Output yes 4 6 0 3 9 yEs 5 5 3 1 2 4 NO Yes 6 8 12 6 2 4 10 Note For the first case, you can add integers to a to receive the array b = [6, 0, 3, 9]. Note that |6-3| = |9-6| = |3-0| = 3 and 3 is in b, |6-0| = |9-3| = 6 and 6 is in b, and |9-0| = 9 is in b, so b is nice. For the second case, you can add integers to a to receive the array b = [5, 3, 1, 2, 4]. We have that |2-1| = |3-2| = |4-3| = |5-4| = 1 is in b, |3-1| = |4-2| = |5-3| = 2 is in b, |4-1| = |5-2| = 3 is in b, and |5-1| = 4 is in b, so b is nice. For the fourth case, you can add integers to a to receive the array b = [8, 12, 6, 2, 4, 10]. We have that |4-2| = |6-4| = |8-6| = |10-8| = |12-10| = 2 is in b, |6-2| = |8-4| = |10-6| = |12-8| = 4 is in b, |8-2| = |10-4| = |12-6| = 6 is in b, |10-2| = |12-4| = 8 is in b, and |12-2| = 10 is in b, so b is nice. It can be proven that for all other test cases it is impossible to create a nice array b. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom. The outlaw's car was equipped with a GPS transmitter. The transmitter showed that the car made exactly n movements on its way from the headquarters to the stall. A movement can move the car from point (x, y) to one of these four points: to point (x - 1, y) which we will mark by letter "L", to point (x + 1, y) β€” "R", to point (x, y - 1) β€” "D", to point (x, y + 1) β€” "U". The GPS transmitter is very inaccurate and it doesn't preserve the exact sequence of the car's movements. Instead, it keeps records of the car's possible movements. Each record is a string of one of these types: "UL", "UR", "DL", "DR" or "ULDR". Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string "UL" means that the car moved either "U", or "L". You've received the journal with the outlaw's possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point (0, 0), your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car's origin). Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of the car's movements from the headquarters to the stall. Each of the following n lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULDR". All movements are given in chronological order. Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier. Output Print a single integer β€” the number of different possible locations of the gang's headquarters. Examples Input 3 UR UL ULDR Output 9 Input 2 DR DL Output 4 Note The figure below shows the nine possible positions of the gang headquarters from the first sample: <image> For example, the following movements can get the car from point (1, 0) to point (0, 0): <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Π’-prime, if t has exactly three distinct positive divisors. You are given an array of n positive integers. For each of them determine whether it is Π’-prime or not. Input The first line contains a single positive integer, n (1 ≀ n ≀ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≀ xi ≀ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. Output Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Π’-prime, and "NO" (without the quotes), if it isn't. Examples Input 3 4 5 6 Output YES NO NO Note The given test has three numbers. The first number 4 has exactly three divisors β€” 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Β«PolygonΒ» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test. You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests. Input The first line contains one integer n (1 ≀ n ≀ 3000) β€” the amount of previously added tests. The second line contains n distinct integers a1, a2, ..., an (1 ≀ ai ≀ 3000) β€” indexes of these tests. Output Output the required default value for the next test index. Examples Input 3 1 7 2 Output 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n Γ— n grid. The rows are numbered 1 through n from top to bottom, and the columns are numbered 1 through n from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The cleaning of all evil will awaken the door! Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells. The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile β€” then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once. You would like to purify all n Γ— n cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell. Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way. Input The first line will contain a single integer n (1 ≀ n ≀ 100). Then, n lines follows, each contains n characters. The j-th character in the i-th row represents the cell located at row i and column j. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise. Output If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts x "Purification" spells (where x is the minimum possible number of spells), output x lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell. Examples Input 3 .E. E.E .E. Output 1 1 2 2 3 3 Input 3 EEE E.. E.E Output -1 Input 5 EE.EE E.EE. E...E .EE.E EE.EE Output 3 3 1 3 2 2 4 4 5 3 Note The first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which "Purification" is cast. Yellow tiles are the tiles being purified as a result of the current "Purification" spell. Green tiles are tiles that have been purified previously. <image> In the second example, it is impossible to purify the cell located at row 1 and column 1. For the third example: <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 cities in Berland. Each city has its index β€” an integer number from 1 to n. The capital has index r1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept in the following way: for each city i, different from the capital, there is kept number pi β€” index of the last city on the way from the capital to i. Once the king of Berland Berl XXXIV decided to move the capital from city r1 to city r2. Naturally, after this the old representation of the road map in Berland's chronicles became incorrect. Please, help the king find out a new representation of the road map in the way described above. Input The first line contains three space-separated integers n, r1, r2 (2 ≀ n ≀ 5Β·104, 1 ≀ r1 β‰  r2 ≀ n) β€” amount of cities in Berland, index of the old capital and index of the new one, correspondingly. The following line contains n - 1 space-separated integers β€” the old representation of the road map. For each city, apart from r1, there is given integer pi β€” index of the last city on the way from the capital to city i. All the cities are described in order of increasing indexes. Output Output n - 1 numbers β€” new representation of the road map in the same format. Examples Input 3 2 3 2 2 Output 2 3 Input 6 2 4 6 1 2 4 2 Output 6 4 1 4 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 assume that * v(n) is the largest prime number, that does not exceed n; * u(n) is the smallest prime number strictly greater than n. Find <image>. Input The first line contains integer t (1 ≀ t ≀ 500) β€” the number of testscases. Each of the following t lines of the input contains integer n (2 ≀ n ≀ 109). Output Print t lines: the i-th of them must contain the answer to the i-th test as an irreducible fraction "p/q", where p, q are integers, q > 0. Examples Input 2 2 3 Output 1/6 7/30 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. While resting on the ship after the "Russian Code Cup" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n Γ— m, consisting of positive integers such that the sum of the squares of numbers for each row and each column was also a square. Since checking the correctness of the table manually is difficult, Misha asks you to make each number in the table to not exceed 108. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the size of the table. Output Print the table that meets the condition: n lines containing m integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer. Examples Input 1 1 Output 1 Input 1 2 Output 3 4 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge. You task is to find any suitable placement of chessmen on the given chessboard. Input The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 100). Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad. Output Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell. If multiple answers exist, print any of them. It is guaranteed that at least one answer exists. Examples Input 1 1 . Output B Input 2 2 .. .. Output BW WB Input 3 3 .-. --- --. Output B-B --- --B Note In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK. In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output. In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game Β«Call of Soldiers 3Β». The game has (m + 1) players and n types of soldiers in total. Players Β«Call of Soldiers 3Β» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type. Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends. Input The first line contains three integers n, m, k (1 ≀ k ≀ n ≀ 20; 1 ≀ m ≀ 1000). The i-th of the next (m + 1) lines contains a single integer xi (1 ≀ xi ≀ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player. Output Print a single integer β€” the number of Fedor's potential friends. Examples Input 7 3 1 8 5 111 17 Output 0 Input 3 3 3 1 2 3 4 Output 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s β‰  t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest. The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer. Input The first lines contain four integers n, m, s and t (2 ≀ n ≀ 105; 1 ≀ m ≀ 105; 1 ≀ s, t ≀ n) β€” the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s β‰  t). Next m lines contain the roads. Each road is given as a group of three integers ai, bi, li (1 ≀ ai, bi ≀ n; ai β‰  bi; 1 ≀ li ≀ 106) β€” the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city ai to city bi. The cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from s to t along the roads. Output Print m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input). If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes). Otherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing. If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes). Examples Input 6 7 1 6 1 2 2 1 3 10 2 3 7 2 4 8 3 5 3 4 5 2 5 6 1 Output YES CAN 2 CAN 1 CAN 1 CAN 1 CAN 1 YES Input 3 3 1 3 1 2 10 2 3 10 1 3 100 Output YES YES CAN 81 Input 2 2 1 2 1 2 1 1 2 2 Output YES NO Note The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing. In the first sample president initially may choose one of the two following ways for a ride: 1 β†’ 2 β†’ 4 β†’ 5 β†’ 6 or 1 β†’ 2 β†’ 3 β†’ 5 β†’ 6. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Department of economic development of IT City created a model of city development till year 2100. To prepare report about growth perspectives it is required to get growth estimates from the model. To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots. The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one. Input The only line of the input contains three integers a, b, c ( - 1000 ≀ a, b, c ≀ 1000) β€” the coefficients of ax2 + bx + c = 0 equation. Output In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6. Examples Input 1 30 200 Output -10.000000000000000 -20.000000000000000 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on. The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u. Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex β€” root. Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove? Input In the first line of the input integer n (1 ≀ n ≀ 105) is given β€” the number of vertices in the tree. In the second line the sequence of n integers a1, a2, ..., an (1 ≀ ai ≀ 109) is given, where ai is the number written on vertex i. The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci (1 ≀ pi ≀ n, - 109 ≀ ci ≀ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it. Output Print the only integer β€” the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree. Example Input 9 88 22 83 14 95 91 98 53 11 3 24 7 -8 1 67 1 64 9 65 5 12 6 -80 3 8 Output 5 Note The following image represents possible process of removing leaves from the tree: <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Captain Bill the Hummingbird and his crew recieved an interesting challenge offer. Some stranger gave them a map, potion of teleportation and said that only this potion might help them to reach the treasure. Bottle with potion has two values x and y written on it. These values define four moves which can be performed using the potion: * <image> * <image> * <image> * <image> Map shows that the position of Captain Bill the Hummingbird is (x1, y1) and the position of the treasure is (x2, y2). You task is to tell Captain Bill the Hummingbird whether he should accept this challenge or decline. If it is possible for Captain to reach the treasure using the potion then output "YES", otherwise "NO" (without quotes). The potion can be used infinite amount of times. Input The first line contains four integer numbers x1, y1, x2, y2 ( - 105 ≀ x1, y1, x2, y2 ≀ 105) β€” positions of Captain Bill the Hummingbird and treasure respectively. The second line contains two integer numbers x, y (1 ≀ x, y ≀ 105) β€” values on the potion bottle. Output Print "YES" if it is possible for Captain to reach the treasure using the potion, otherwise print "NO" (without quotes). Examples Input 0 0 0 6 2 3 Output YES Input 1 1 3 6 1 5 Output NO Note In the first example there exists such sequence of moves: 1. <image> β€” the first type of move 2. <image> β€” the third type of move The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characterized by two integers β€” amount of experience and cost. The efficiency of a potion is the ratio of the amount of experience to the cost. Efficiency may be a non-integer number. For each two integer numbers a and b such that l ≀ a ≀ r and x ≀ b ≀ y there is a potion with experience a and cost b in the store (that is, there are (r - l + 1)Β·(y - x + 1) potions). Kirill wants to buy a potion which has efficiency k. Will he be able to do this? Input First string contains five integer numbers l, r, x, y, k (1 ≀ l ≀ r ≀ 107, 1 ≀ x ≀ y ≀ 107, 1 ≀ k ≀ 107). Output Print "YES" without quotes if a potion with efficiency exactly k can be bought in the store and "NO" without quotes otherwise. You can output each of the letters in any register. Examples Input 1 10 1 10 1 Output YES Input 1 5 6 10 1 Output NO The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 quasi-palindromic number be such number that adding some leading zeros (possible none) to it produces a palindromic string. String t is called a palindrome, if it reads the same from left to right and from right to left. For example, numbers 131 and 2010200 are quasi-palindromic, they can be transformed to strings "131" and "002010200", respectively, which are palindromes. You are given some integer number x. Check if it's a quasi-palindromic number. Input The first line contains one integer number x (1 ≀ x ≀ 109). This number is given without any leading zeroes. Output Print "YES" if number x is quasi-palindromic. Otherwise, print "NO" (without quotes). Examples Input 131 Output YES Input 320 Output NO Input 2010200 Output YES The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, ..., bk (1 ≀ b1 < b2 < ... < bk ≀ n) in such a way that the value of <image> is maximized. Chosen sequence can be empty. Print the maximum possible value of <image>. Input The first line contains two integers n and m (1 ≀ n ≀ 35, 1 ≀ m ≀ 109). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print the maximum possible value of <image>. Examples Input 4 4 5 2 4 1 Output 3 Input 3 20 199 41 299 Output 19 Note In the first example you can choose a sequence b = {1, 2}, so the sum <image> is equal to 7 (and that's 3 after taking it modulo 4). In the second example you can choose a sequence b = {3}. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on. When a garland is switched on, it periodically changes its state β€” sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on. Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one β€” during x2-th second, and the third one β€” during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit. Help Mishka by telling him if it is possible to do this! Input The first line contains three integers k1, k2 and k3 (1 ≀ ki ≀ 1500) β€” time intervals of the garlands. Output If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES. Otherwise, print NO. Examples Input 2 2 3 Output YES Input 4 2 3 Output NO Note In the first example Mishka can choose x1 = 1, x2 = 2, x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second β€” 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though. In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon. A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an. Little Tommy is among them. He would like to choose an interval [l, r] (1 ≀ l ≀ r ≀ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum. A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≀ ap2 ≀ ... ≀ apk. The length of the subsequence is k. Input The first line contains an integer n (1 ≀ n ≀ 2000), denoting the length of the original sequence. The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≀ ai ≀ 2, i = 1, 2, ..., n). Output Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence. Examples Input 4 1 2 1 2 Output 4 Input 10 1 1 2 2 2 1 1 2 2 1 Output 9 Note In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4. In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: * the Power Gem of purple color, * the Time Gem of green color, * the Space Gem of blue color, * the Soul Gem of orange color, * the Reality Gem of red color, * the Mind Gem of yellow color. Using colors of Gems you saw in the Gauntlet determine the names of absent Gems. Input In the first line of input there is one integer n (0 ≀ n ≀ 6) β€” the number of Gems in Infinity Gauntlet. In next n lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowercase English letters. Output In the first line output one integer m (0 ≀ m ≀ 6) β€” the number of absent Gems. Then in m lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase. Examples Input 4 red purple yellow orange Output 2 Space Time Input 0 Output 6 Time Mind Soul Power Reality Space Note In the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space. In the second sample Thanos doesn't have any Gems, so he needs all six. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Lily Aldrin, as the slap bet commissioner, needs to resolve issues regarding slap bets due with Barney and Marshall. She has mandated that there be a fixed period of time in which the slaps may be used. So, she draws two lines representing the given two time slots, each of Barney and Marshall; you need to help her write a program that: Outputs "Nothing" if given lines are indistinguishable. Outputs "Line" if there exists a timeline(more than one day) in which none of them have opportunity to slap. Outputs "Point" if the intersection of lines is just in one point. Input: The first line of input file contains a number T indicating number of test cases. The following T lines, each contain 4 space seperated integers bt1, bt2, mt1, and mt2. Output: The output file should contain answer to each query in a new line. Constraints 1 ≀ T ≀ 100 -1000 ≀ bt1, bt2, mt1, mt2 ≀ 1000 Note: 1. It is NOT necessary that bt1 ≀ bt2. 2. It is NOT necessary that mt1 ≀ mt2. SAMPLE INPUT 3 0 1 4 5 1 2 -1 5 1 2 2 3 SAMPLE OUTPUT Line Nothing Point The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. DG (Departmental Gathering) is coming…. As one of the most famous girls in the college everybody is smitten with Harshada and wants to give her roses. But Harshada is only into coders , therefore she sends out word that she would accept roses from the nth guy only if he brings her x roses such that β€˜x’ is the nth term of the sequence given below. One of your friends is madly in love with Harshada but he is not so much of a coder. Help him show his coder metal to the girl of his dreams. 1, 2, 2, 4, 8,12……….. Constraints: 1 ≀ T ≀ 100 1 ≀ n ≀ 20 Input First line of input contains a variable T, number of test cases. Each test case contains a single integer input n. Output For each test case output a single line that has an integer value x which in the nth term of the given series. SAMPLE INPUT 3 1 5 10 SAMPLE OUTPUT 1 8 10476 Explanation There are 3 test case Test case 1: 1st guy has to give 1 rose. Test case 2: 5th guy has to give 8 roses. Test case 3: 10th guy has to give 10476 roses. You don’t want to be the 10th guy, do you? :p The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 : The Starks(S),Lannisters(L) and the Baratheons(B) are fighting for the Iron Throne. These three houses are fighting from ages to capture the throne. The N th battle goes on for N days before a winner emerges. Interestingly,the battles follow a pattern. If the battle number N is divisible by 3 it is fought between S and L, if the remainder of division with 3 is 1 it is fought between L and B and if the remainder of divison with 3 is 2 it is fought between B and S. Given a day number D, you will have to say which two houses are fighting on the D th day. Input : First line will contain an integer T (number of testcases). The next T lines will contain an integer D. Output : You will have to report which of the two houses are fighting on the D th day (i.e you will have to print one of the three battles ( SL, LB, BS ) ). Constraints : 1 ≀ T ≀ 1000 1 ≀ D ≀ 10^9 Problem Setter : Sayan Problem Tester : Sandeep (By IIT Kgp HackerEarth Programming Club) SAMPLE INPUT 3 1 6 12 SAMPLE OUTPUT LB SL BS Explanation In test case 3:Β­ The battle 1 goes on for 1 day, 2 for 2 days and so on.The series is 1 2 2 3 3 3 4 4 4 4 5 5. On the 12th day battle number 5 is going on. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Karan has decided to give stickers to all the attendees of the last day of Google Week. The stickers will be given such that the last person to enter the hall gets a single sticker. The second last person entering the hall gets two stickers and so on. Given the strength of the audience, can you tell him the total number of stickers he will be needing? Input The first line of the input contains T, the number of test cases. T lines follow. Each test case contains a single integer N denoting the strength of the audience. Output For each test case, print the total number of stickers he will be needing in a new line. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 10^8 SAMPLE INPUT 1 2 SAMPLE OUTPUT 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have N strings of lowercase English letters: S_1, S_2, \cdots, S_N. Takahashi wants to make a string that is a palindrome by choosing one or more of these strings - the same string can be chosen more than once - and concatenating them in some order of his choice. The cost of using the string S_i once is C_i, and the cost of using it multiple times is C_i multiplied by that number of times. Find the minimum total cost needed to choose strings so that Takahashi can make a palindrome. If there is no choice of strings in which he can make a palindrome, print -1. Constraints * 1 \leq N \leq 50 * 1 \leq |S_i| \leq 20 * S_i consists of lowercase English letters. * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: N S_1 C_1 S_2 C_2 : S_N C_N Output Print the minimum total cost needed to choose strings so that Takahashi can make a palindrome, or -1 if there is no such choice. Examples Input 3 ba 3 abc 4 cbaa 5 Output 7 Input 2 abcab 5 cba 3 Output 11 Input 4 ab 5 cba 3 a 12 ab 10 Output 8 Input 2 abc 1 ab 2 Output -1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have held a popularity poll for N items on sale. Item i received A_i votes. From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes. If M popular items can be selected, print `Yes`; otherwise, print `No`. Constraints * 1 \leq M \leq N \leq 100 * 1 \leq A_i \leq 1000 * A_i are distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 ... A_N Output If M popular items can be selected, print `Yes`; otherwise, print `No`. Examples Input 4 1 5 4 2 1 Output Yes Input 3 2 380 19 1 Output No Input 12 3 4 56 78 901 2 345 67 890 123 45 6 789 Output Yes The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number. Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1). When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N). One day, each of the mountains received a non-negative even number of liters of rain. As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water. Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem. Constraints * All values in input are integers. * 3 \leq N \leq 10^5-1 * N is an odd number. * 0 \leq A_i \leq 10^9 * The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order. Examples Input 3 2 2 4 Output 4 0 4 Input 5 3 8 7 5 5 Output 2 4 12 2 8 Input 3 1000000000 1000000000 0 Output 0 2000000000 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. The order of a,b,c does matter, and some of them can be the same. Constraints * 1 \leq N,K \leq 2\times 10^5 * N and K are integers. Input Input is given from Standard Input in the following format: N K Output Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. Examples Input 3 2 Output 9 Input 5 3 Output 1 Input 31415 9265 Output 27 Input 35897 932 Output 114191 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.