question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya 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 a prime number p, n integers a_1, a_2, …, a_n, and an integer k. Find the number of pairs of indexes (i, j) (1 ≀ i < j ≀ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≑ k mod p. Input The first line contains integers n, p, k (2 ≀ n ≀ 3 β‹… 10^5, 2 ≀ p ≀ 10^9, 0 ≀ k ≀ p-1). p is guaranteed to be prime. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ p-1). It is guaranteed that all elements are different. Output Output a single integer β€” answer to the problem. Examples Input 3 3 0 0 1 2 Output 1 Input 6 7 2 1 2 3 4 5 6 Output 3 Note In the first example: (0+1)(0^2 + 1^2) = 1 ≑ 1 mod 3. (0+2)(0^2 + 2^2) = 8 ≑ 2 mod 3. (1+2)(1^2 + 2^2) = 15 ≑ 0 mod 3. So only 1 pair satisfies the condition. In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 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. Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend 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. [3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak) [Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive) NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms of a 2 Γ— n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side. However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type. After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa). Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells. Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her? Input The first line contains integers n, q (2 ≀ n ≀ 10^5, 1 ≀ q ≀ 10^5). The i-th of q following lines contains two integers r_i, c_i (1 ≀ r_i ≀ 2, 1 ≀ c_i ≀ n), denoting the coordinates of the cell to be flipped at the i-th moment. It is guaranteed that cells (1, 1) and (2, n) never appear in the query list. Output For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update. You can print the words in any case (either lowercase, uppercase or mixed). Example Input 5 5 2 3 1 4 2 4 2 3 1 4 Output Yes No No No Yes Note We'll crack down the example test here: * After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) β†’ (1,2) β†’ (1,3) β†’ (1,4) β†’ (1,5) β†’ (2,5). * After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3). * After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal. * After the fifth query, the column barrier has been lifted, thus she can go to the final goal again. 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_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β‰  i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option). For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't. It's guaranteed that it's always possible to shuffle an array to meet this condition. 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 array a. The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ 100). Output For each test case print the shuffled version of the array a which is good. Example Input 3 1 7 4 1 1 3 5 6 3 2 1 5 6 4 Output 7 1 5 1 3 2 4 6 1 3 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. Let's define the following recurrence: $$$a_{n+1} = a_{n} + minDigit(a_{n}) β‹… maxDigit(a_{n}).$$$ Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes. Your task is calculate a_{K} for given a_{1} and K. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. Each test case consists of a single line containing two integers a_{1} and K (1 ≀ a_{1} ≀ 10^{18}, 1 ≀ K ≀ 10^{16}) separated by a space. Output For each test case print one integer a_{K} on a separate line. Example Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 Note a_{1} = 487 a_{2} = a_{1} + minDigit(a_{1}) β‹… maxDigit(a_{1}) = 487 + min (4, 8, 7) β‹… max (4, 8, 7) = 487 + 4 β‹… 8 = 519 a_{3} = a_{2} + minDigit(a_{2}) β‹… maxDigit(a_{2}) = 519 + min (5, 1, 9) β‹… max (5, 1, 9) = 519 + 1 β‹… 9 = 528 a_{4} = a_{3} + minDigit(a_{3}) β‹… maxDigit(a_{3}) = 528 + min (5, 2, 8) β‹… max (5, 2, 8) = 528 + 2 β‹… 8 = 544 a_{5} = a_{4} + minDigit(a_{4}) β‹… maxDigit(a_{4}) = 544 + min (5, 4, 4) β‹… max (5, 4, 4) = 544 + 4 β‹… 5 = 564 a_{6} = a_{5} + minDigit(a_{5}) β‹… maxDigit(a_{5}) = 564 + min (5, 6, 4) β‹… max (5, 6, 4) = 564 + 4 β‹… 6 = 588 a_{7} = a_{6} + minDigit(a_{6}) β‹… maxDigit(a_{6}) = 588 + min (5, 8, 8) β‹… max (5, 8, 8) = 588 + 5 β‹… 8 = 628 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_1, a_2, ... , a_n consisting of integers from 0 to 9. A subarray a_l, a_{l+1}, a_{l+2}, ... , a_{r-1}, a_r is good if the sum of elements of this subarray is equal to the length of this subarray (βˆ‘_{i=l}^{r} a_i = r - l + 1). For example, if a = [1, 2, 0], then there are 3 good subarrays: a_{1 ... 1} = [1], a_{2 ... 3} = [2, 0] and a_{1 ... 3} = [1, 2, 0]. Calculate the number of good subarrays of the array a. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of the array a. The second line of each test case contains a string consisting of n decimal digits, where the i-th digit is equal to the value of a_i. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case print one integer β€” the number of good subarrays of the array a. Example Input 3 3 120 5 11011 6 600005 Output 3 6 1 Note The first test case is considered in the statement. In the second test case, there are 6 good subarrays: a_{1 ... 1}, a_{2 ... 2}, a_{1 ... 2}, a_{4 ... 4}, a_{5 ... 5} and a_{4 ... 5}. In the third test case there is only one good subarray: a_{2 ... 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. Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. <image> Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below. <image> Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^{4}). Description of the test cases follows. The first line of each test case contains two integers x and y (-10^{9} ≀ x, y ≀ 10^{9}) representing the coordinates of the target hexagon. The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 ≀ c_1, c_2, c_3, c_4, c_5, c_6 ≀ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). Output For each testcase output the smallest cost of a path from the origin to the given cell. Example Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 Note The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18. <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. Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simpler and more convenient to use. He collected the hay into cubical hay blocks of the same size. Then he stored the blocks in his barn. After a summer spent in hard toil Sam stored AΒ·BΒ·C hay blocks and stored them in a barn as a rectangular parallelepiped A layers high. Each layer had B rows and each row had C blocks. At the end of the autumn Sam came into the barn to admire one more time the hay he'd been stacking during this hard summer. Unfortunately, Sam was horrified to see that the hay blocks had been carelessly scattered around the barn. The place was a complete mess. As it turned out, thieves had sneaked into the barn. They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (A - 1) Γ— (B - 2) Γ— (C - 2) hay blocks. To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1 Γ— 1 Γ— 1 blocks and scattered them around the barn. After the theft Sam counted n hay blocks in the barn but he forgot numbers A, B ΠΈ C. Given number n, find the minimally possible and maximally possible number of stolen hay blocks. Input The only line contains integer n from the problem's statement (1 ≀ n ≀ 109). Output Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves. Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. 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 Output 28 41 Input 7 Output 47 65 Input 12 Output 48 105 Note Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32 = 2 Γ— 4 Γ— 4 hay blocks in his barn, then after the theft the barn has 4 = (2 - 1) Γ— (4 - 2) Γ— (4 - 2) hay blocks left. Thus, the thieves could have stolen 32 - 4 = 28 hay blocks. If Sam initially had a parallelepiped consisting of 45 = 5 Γ— 3 Γ— 3 hay blocks in his barn, then after the theft the barn has 4 = (5 - 1) Γ— (3 - 2) Γ— (3 - 2) hay blocks left. Thus, the thieves could have stolen 45 - 4 = 41 hay blocks. No other variants of the blocks' initial arrangement (that leave Sam with exactly 4 blocks after the theft) can permit the thieves to steal less than 28 or more than 41 blocks. 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 is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1: <image> Let's denote the node in the row i and column j by (i, j). Initially for each i the i-th row has exactly one obstacle β€” at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below: * If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently. * If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently. * Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1). Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles. 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 three integers n, u and v (2 ≀ n ≀ 100, 1 ≀ u, v ≀ 10^9) β€” the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^6) β€” where a_i represents that the obstacle in the i-th row is in node (i, a_i). It's guaranteed that the sum of n over all test cases doesn't exceed 2 β‹… 10^4. Output For each test case, output a single integer β€” the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles. It can be shown that under the constraints of the problem there is always a way to make such a trip possible. Example Input 3 2 3 4 2 2 2 3 4 3 2 2 4 3 3 2 Output 7 3 3 Note In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins. <image> In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins. <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. An array is called beautiful if all the elements in the array are equal. You can transform an array using the following steps any number of times: 1. Choose two indices i and j (1 ≀ i,j ≀ n), and an integer x (1 ≀ x ≀ a_i). Let i be the source index and j be the sink index. 2. Decrease the i-th element by x, and increase the j-th element by x. The resulting values at i-th and j-th index are a_i-x and a_j+x respectively. 3. The cost of this operation is x β‹… |j-i| . 4. Now the i-th index can no longer be the sink and the j-th index can no longer be the source. The total cost of a transformation is the sum of all the costs in step 3. For example, array [0, 2, 3, 3] can be transformed into a beautiful array [2, 2, 2, 2] with total cost 1 β‹… |1-3| + 1 β‹… |1-4| = 5. An array is called balanced, if it can be transformed into a beautiful array, and the cost of such transformation is uniquely defined. In other words, the minimum cost of transformation into a beautiful array equals the maximum cost. You are given an array a_1, a_2, …, a_n of length n, consisting of non-negative integers. Your task is to find the number of balanced arrays which are permutations of the given array. Two arrays are considered different, if elements at some position differ. Since the answer can be large, output it modulo 10^9 + 7. Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the size of the array. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Output Output a single integer β€” the number of balanced permutations modulo 10^9+7. Examples Input 3 1 2 3 Output 6 Input 4 0 4 0 4 Output 2 Input 5 0 11 12 13 14 Output 120 Note In the first example, [1, 2, 3] is a valid permutation as we can consider the index with value 3 as the source and index with value 1 as the sink. Thus, after conversion we get a beautiful array [2, 2, 2], and the total cost would be 2. We can show that this is the only transformation of this array that leads to a beautiful array. Similarly, we can check for other permutations too. In the second example, [0, 0, 4, 4] and [4, 4, 0, 0] are balanced permutations. In the third example, all permutations are balanced. 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 some country live wizards. They love to ride trolleybuses. A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the i-th trolleybus that it leaves at the moment of time ti seconds, can go at a speed of no greater than vi meters per second, and accelerate with an acceleration no greater than a meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys. Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible. You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine. Input The first input line contains three space-separated integers n, a, d (1 ≀ n ≀ 105, 1 ≀ a, d ≀ 106) β€” the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly. Next n lines contain pairs of integers ti vi (0 ≀ t1 < t2... < tn - 1 < tn ≀ 106, 1 ≀ vi ≀ 106) β€” the time when the i-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces. Output For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10 - 4. Examples Input 3 10 10000 0 10 5 11 1000 1 Output 1000.5000000000 1000.5000000000 11000.0500000000 Input 1 2 26 28 29 Output 33.0990195136 Note In the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds. 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 a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions: * After the cutting each ribbon piece should have length a, b or c. * After the cutting the number of ribbon pieces should be maximum. Help Polycarpus and find the number of ribbon pieces after the required cutting. Input The first line contains four space-separated integers n, a, b and c (1 ≀ n, a, b, c ≀ 4000) β€” the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide. Output Print a single number β€” the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists. Examples Input 5 5 3 2 Output 2 Input 7 5 5 2 Output 2 Note In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3. In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 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. Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network. But yesterday, he came to see "her" in the real world and found out "she" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names. This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method. Input The first line contains a non-empty string, that contains only lowercase English letters β€” the user name. This string contains at most 100 letters. Output If it is a female by our hero's method, print "CHAT WITH HER!" (without the quotes), otherwise, print "IGNORE HIM!" (without the quotes). Examples Input wjmzbmr Output CHAT WITH HER! Input xiaodao Output IGNORE HIM! Input sevenkplus Output CHAT WITH HER! Note For the first example. There are 6 distinct characters in "wjmzbmr". These characters are: "w", "j", "m", "z", "b", "r". So wjmzbmr is a female and you should print "CHAT WITH HER!". 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 board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles. Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer. A bad boy Vasya came up to the board and wrote number sv near each node v β€” the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board. Your task is to restore the original tree by the node colors and numbers sv. Input The first line of the input contains a single integer n (2 ≀ n ≀ 105) β€” the number of nodes in the tree. Next n lines contain pairs of space-separated integers ci, si (0 ≀ ci ≀ 1, 0 ≀ si ≀ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and si represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board. Output Print the description of n - 1 edges of the tree graph. Each description is a group of three integers vi, ui, wi (1 ≀ vi, ui ≀ n, vi β‰  ui, 0 ≀ wi ≀ 109), where vi and ui β€” are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi β‰  cui. It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces. Examples Input 3 1 3 1 2 0 5 Output 3 1 3 3 2 2 Input 6 1 0 0 3 1 8 0 2 0 3 0 0 Output 2 3 3 5 3 3 4 3 2 1 6 0 2 1 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. Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..., an of positive integers: 1. Initially, x = 1 and y = 0. If, after any step, x ≀ 0 or x > n, the program immediately terminates. 2. The program increases both x and y by a value equal to ax simultaneously. 3. The program now increases y by ax while decreasing x by ax. 4. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on. The cows are not very good at arithmetic though, and they want to see how the program works. Please help them! You are given the sequence a2, a3, ..., an. Suppose for each i (1 ≀ i ≀ n - 1) we run the program on the sequence i, a2, a3, ..., an. For each such run output the final value of y if the program terminates or -1 if it does not terminate. Input The first line contains a single integer, n (2 ≀ n ≀ 2Β·105). The next line contains n - 1 space separated integers, a2, a3, ..., an (1 ≀ ai ≀ 109). Output Output n - 1 lines. On the i-th line, print the requested value when the program is run on the sequence i, a2, a3, ...an. 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. Examples Input 4 2 4 1 Output 3 6 8 Input 3 1 2 Output -1 -1 Note In the first sample 1. For i = 1, x becomes <image> and y becomes 1 + 2 = 3. 2. For i = 2, x becomes <image> and y becomes 2 + 4 = 6. 3. For i = 3, x becomes <image> and y becomes 3 + 1 + 4 = 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. Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed. This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 ≀ a ≀ b ≀ n - k + 1, b - a β‰₯ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included). As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him. Input The first line contains two integers n and k (2 ≀ n ≀ 2Β·105, 0 < 2k ≀ n) β€” the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn β€” the absurdity of each law (1 ≀ xi ≀ 109). Output Print two integers a, b β€” the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b. Examples Input 5 2 3 6 1 1 6 Output 1 4 Input 6 2 1 1 1 1 1 1 Output 1 3 Note In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16. In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 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. Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride on some bus or trolley. It costs c1 burles; 2. A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles; 3. A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles; 4. A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets. Input The first line contains four integers c1, c2, c3, c4 (1 ≀ c1, c2, c3, c4 ≀ 1000) β€” the costs of the tickets. The second line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≀ ai ≀ 1000) β€” the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≀ bi ≀ 1000) β€” the number of times Vasya is going to use the trolley number i. Output Print a single number β€” the minimum sum of burles Vasya will have to spend on the tickets. Examples Input 1 3 7 19 2 3 2 5 4 4 4 Output 12 Input 4 3 2 1 1 3 798 1 2 3 Output 1 Input 100 100 8 100 3 5 7 94 12 100 1 47 0 42 Output 16 Note In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2Β·1) + 3 + 7 = 12 burles. In the second sample the profitable strategy is to buy one ticket of the fourth type. In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys. 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 showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost. As soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (n - 1) friends. Determine if Sereja's friends can play the game so that nobody loses. Input The first line contains integers n and s (2 ≀ n ≀ 100; 1 ≀ s ≀ 1000) β€” the number of mugs and the volume of the cup. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 10). Number ai means the volume of the i-th mug. Output In a single line, print "YES" (without the quotes) if his friends can play in the described manner, and "NO" (without the quotes) otherwise. Examples Input 3 4 1 1 1 Output YES Input 3 4 3 1 3 Output YES Input 3 4 4 4 4 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. A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one). Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero. Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it? Input The first line contains 26 integers xa, xb, ..., xz ( - 105 ≀ xi ≀ 105) β€” the value assigned to letters a, b, c, ..., z respectively. The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase lettersβ€” the string for which you need to calculate the answer. Output Print the answer to the problem. Examples Input 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 xabcab Output 2 Input 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 aaa Output 2 Note In the first sample test strings satisfying the condition above are abca and bcab. In the second sample test strings satisfying the condition above are two occurences of aa. 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 this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di β€” buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≀ n ≀ 1000, 1 ≀ s ≀ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≀ pi ≀ 105) and an integer qi (1 ≀ qi ≀ 104) β€” direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. 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. Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya. For each opponent Arya knows his schedule β€” whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents. Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 100) β€” the number of opponents and the number of days, respectively. The i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be absent on the i-th day. Output Print the only integer β€” the maximum number of consecutive days that Arya will beat all present opponents. Examples Input 2 2 10 00 Output 2 Input 4 1 0100 Output 1 Input 4 5 1101 1111 0110 1011 1111 Output 2 Note In the first and the second samples, Arya will beat all present opponents each of the d days. In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 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. PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally? Input The first input line contains two integers n and m (1 ≀ n, m ≀ 103) β€” number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line β€” words familiar to PolandBall. Then m strings follow, one per line β€” words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters. Output In a single line of print the answer β€” "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally. Examples Input 5 1 polandball is a cool character nope Output YES Input 2 2 kremowka wadowicka kremowka wiedenska Output YES Input 1 2 a a b Output NO Note In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins. 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. Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai. Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment. Help her to do so in finding the total number of such segments. Input The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≀ n ≀ 105, 1 ≀ |k| ≀ 10). Next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” affection values of chemicals. Output Output a single integer β€” the number of valid segments. Examples Input 4 2 2 2 2 2 Output 8 Input 4 -3 3 -6 -3 12 Output 3 Note Do keep in mind that k0 = 1. In the first sample, Molly can get following different affection values: * 2: segments [1, 1], [2, 2], [3, 3], [4, 4]; * 4: segments [1, 2], [2, 3], [3, 4]; * 6: segments [1, 3], [2, 4]; * 8: segments [1, 4]. Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8. In the second sample, Molly can choose segments [1, 2], [3, 3], [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. Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≀ x, y ≀ n and ax, y β‰  1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column. Help Okabe determine whether a given lab is good! Input The first line of input contains the integer n (1 ≀ n ≀ 50) β€” the size of the lab. The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≀ ai, j ≀ 105). Output Print "Yes" if the given lab is good and "No" otherwise. You can output each letter in upper or lower case. Examples Input 3 1 1 2 2 3 1 6 4 1 Output Yes Input 3 1 5 2 1 1 1 1 2 3 Output No Note In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes". In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer 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. Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle. In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n. For each cell i you are given two values: * li β€” cell containing previous element for the element in the cell i; * ri β€” cell containing next element for the element in the cell i. If cell i contains information about the element which has no previous element then li = 0. Similarly, if cell i contains information about the element which has no next element then ri = 0. <image> Three lists are shown on the picture. For example, for the picture above the values of l and r are the following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 0. Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values li, ri. Any other action, other than joining the beginning of one list to the end of another, can not be performed. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of memory cells where the doubly linked lists are located. Each of the following n lines contains two integers li, ri (0 ≀ li, ri ≀ n) β€” the cells of the previous and the next element of list for cell i. Value li = 0 if element in cell i has no previous element in its list. Value ri = 0 if element in cell i has no next element in its list. It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells. Output Print n lines, the i-th line must contain two integers li and ri β€” the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them. Example Input 7 4 7 5 0 0 0 6 1 0 2 0 4 1 0 Output 4 7 5 6 0 5 6 1 3 2 2 4 1 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. Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset. k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems. Determine if Snark and Philip can make an interesting problemset! Input The first line contains two integers n, k (1 ≀ n ≀ 105, 1 ≀ k ≀ 4) β€” the number of problems and the number of experienced teams. Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise. Output Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 5 3 1 0 1 1 1 0 1 0 0 1 0 0 1 0 0 Output NO Input 3 2 1 0 1 1 0 1 Output YES Note In the first example you can't make any interesting problemset, because the first team knows all problems. In the second example you can choose the first and the third problems. 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 a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai is written on vertex i. Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blocked subtree of vertex x as the set of vertices y such that both these conditions are met: * x is an ancestor of y (every vertex is an ancestor of itself); * d(x, y) ≀ k. You are given m queries to the tree. i-th query is represented by two numbers xi and ki, and the answer to this query is the minimum value of aj among such vertices j such that j belongs to ki-blocked subtree of xi. Write a program that would process these queries quickly! Note that the queries are given in a modified way. Input The first line contains two integers n and r (1 ≀ r ≀ n ≀ 100000) β€” the number of vertices in the tree and the index of the root, respectively. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the numbers written on the vertices. Then n - 1 lines follow, each containing two integers x and y (1 ≀ x, y ≀ n) and representing an edge between vertices x and y. It is guaranteed that these edges form a tree. Next line contains one integer m (1 ≀ m ≀ 106) β€” the number of queries to process. Then m lines follow, i-th line containing two numbers pi and qi, which can be used to restore i-th query (1 ≀ pi, qi ≀ n). i-th query can be restored as follows: Let last be the answer for previous query (or 0 if i = 1). Then xi = ((pi + last) mod n) + 1, and ki = (qi + last) mod n. Output Print m integers. i-th of them has to be equal to the answer to i-th query. Example Input 5 2 1 3 2 3 5 2 3 5 1 3 4 4 1 2 1 2 2 3 Output 2 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. You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0. It is allowed to leave a as it is. Input The first line contains integer a (1 ≀ a ≀ 1018). The second line contains integer b (1 ≀ b ≀ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists. Output Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists. The number in the output should have exactly the same length as number a. It should be a permutation of digits of a. Examples Input 123 222 Output 213 Input 3921 10000 Output 9321 Input 4940 5000 Output 4940 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. Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city vi to city ui (and from ui to vi), and it costs wi coins to use this route. Each city will be visited by "Flayer", and the cost of the concert ticket in i-th city is ai coins. You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j β‰  i). Formally, for every <image> you have to calculate <image>, where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large. Input The first line contains two integers n and m (2 ≀ n ≀ 2Β·105, 1 ≀ m ≀ 2Β·105). Then m lines follow, i-th contains three integers vi, ui and wi (1 ≀ vi, ui ≀ n, vi β‰  ui, 1 ≀ wi ≀ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input. The next line contains n integers a1, a2, ... ak (1 ≀ ai ≀ 1012) β€” price to attend the concert in i-th city. Output Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j β‰  i). Examples Input 4 2 1 2 4 2 3 7 6 20 1 25 Output 6 14 1 25 Input 3 3 1 2 1 2 3 1 1 3 1 30 10 20 Output 12 10 12 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 boys are sitting in a circle. Each of them have some apples in their hand. You find that the total number of the apples can be divided by N. So you want to divide the apples equally among all the boys. But they are so lazy that each one of them only wants to give one apple to one of the neighbors at one step. Calculate the minimal number of steps to make each boy have the same number of apples. Input The first line of input is an integer N. 2 ≀ N ≀ 10000 The second line is N integers indicates the number of apples of the ith boy. Each integer is positive and no more than 10^9. Output A single line contains the minimal number of steps to make each boy have the same number of apples. SAMPLE INPUT 4 1 3 9 7 SAMPLE OUTPUT 8 Explanation Here are the 8 steps starting from (1,3,9,7): (2,3,9,6) (3,3,9,5) (3,4,8,5) (3,5,7,5) (3,6,6,5) (3,7,5,5) (4,6,5,5) (5,5,5,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. β€œCan you answer this million dollar question ?” said the presenter to Little Achraf. β€œYes”, he responded. β€œOk … here is the question”. We define a sequence A of strings of order L as follows: A[n] = \sum\limits_{i = 0}^{L-1} A[n-L+i]; n β‰₯ L+1; where addition refers to string concatenation A[n] ∈ [a-z A-Z 0-9]; 1 ≀ n ≀ L. Now, given some constraints of the form β€œThe nth character of A[x] is equal to c”; you need to calculate the number of possible sequences (possibly none) that satisfy all the given constraints. Input Format: First line contains L and C, the order of the sequence A and the number of constraints, respectively. Each of the next C lines contain n, x, and a character c meaning β€œThe nth character of A[x] is equal to c”. Output Format: Print a single number denoting the number of possible sequences that satisfy all the given constrains modulo 10^9 + 7. Constraints: 2 ≀ L ≀ 100 0 ≀ C ≀ 10^4 1 ≀ n ≀ 10^{15} x ≀ 10^{15} character c ∈ [a-zA-Z0-9] SAMPLE INPUT 2 2 1 3 A 3 5 B SAMPLE OUTPUT 1 Explanation There is only one sequence that satisfies all the conditions and it is: A[1] = "A" A[2] = "B" A[3] = "AB" A[4] = "BAB" A[5] = "ABBAB" The first character of A[3] is A, and third character of A[5] is 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. Our monk loves food. Hence,he took up position of a manager at Sagar,a restaurant that serves people with delicious food packages. It is a very famous place and people are always queuing up to have one of those packages. Each package has a cost associated with it. The packages are kept as a pile. The job of a manager is very difficult. He needs to handle two types of queries: 1) Customer Query: When a customer demands a package, the food package on the top of the pile is given and the customer is charged according to the cost of the package. This reduces the height of the pile by 1. In case the pile is empty, the customer goes away empty-handed. 2) Chef Query: The chef prepares a food package and adds it on top of the pile. And reports the cost of the package to the Manager. Help him manage the process. Input: First line contains an integer Q, the number of queries. Q lines follow. A Type-1 ( Customer) Query, is indicated by a single integer 1 in the line. A Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) . Output: For each Type-1 Query, output the price that customer has to pay i.e. cost of the package given to the customer in a new line. If the pile is empty, print "No Food" (without the quotes). Constraints: 1 ≀ Q ≀ 10^5 1 ≀ C ≀ 10^7 SAMPLE INPUT 6 1 2 5 2 7 2 9 1 1 SAMPLE OUTPUT No Food 9 7 Explanation Initially, The pile is empty. Chef adds a package with cost=5. Chef adds a package with cost=7. Chef adds a package with cost=9. Customer takes the package on the top i.e. cost=9. Now package of cost=7 on top. Customer takes the package on the top i.e. cost=7. 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 a non-negative integer K, we define a fractal of level K as follows: * A fractal of level 0 is a grid with just one white square. * When K > 0, a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids: * The central subgrid consists of only black squares. * Each of the other eight subgrids is a fractal of level K-1. For example, a fractal of level 2 is as follows: A fractal of level 2 In a fractal of level 30, let (r, c) denote the square at the r-th row from the top and the c-th column from the left. You are given Q quadruples of integers (a_i, b_i, c_i, d_i). For each quadruple, find the distance from (a_i, b_i) to (c_i, d_i). Here the distance from (a, b) to (c, d) is the minimum integer n that satisfies the following condition: * There exists a sequence of white squares (x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions: * (x_0, y_0) = (a, b) * (x_n, y_n) = (c, d) * For every i (0 \leq i \leq n-1), (x_i, y_i) and (x_{i+1}, y_{i+1}) share a side. Constraints * 1 \leq Q \leq 10000 * 1 \leq a_i, b_i, c_i, d_i \leq 3^{30} * (a_i, b_i) \neq (c_i, d_i) * (a_i, b_i) and (c_i, d_i) are white squares. * All values in input are integers. Input Input is given from Standard Input in the following format: Q a_1 \ b_1 \ c_1 \ d_1 : a_Q \ b_Q \ c_Q \ d_Q Output Print Q lines. The i-th line should contain the distance from (a_i, b_i) to (c_i, d_i). Example Input 2 4 2 7 4 9 9 1 9 Output 5 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. Takahashi is standing on a multiplication table with infinitely many rows and columns. The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1). In one move, he can move from (i,j) to either (i+1,j) or (i,j+1). Given an integer N, find the minimum number of moves needed to reach a square that contains N. Constraints * 2 \leq N \leq 10^{12} * N is an integer. Input Input is given from Standard Input in the following format: N Output Print the minimum number of moves needed to reach a square that contains the integer N. Examples Input 10 Output 5 Input 50 Output 13 Input 10000000019 Output 10000000018 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 is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M? Constraints * All values in input are integers. * 1 \leq L, A, B < 10^{18} * 2 \leq M \leq 10^9 * All terms in the arithmetic progression are less than 10^{18}. Input Input is given from Standard Input in the following format: L A B M Output Print the remainder when the integer obtained by concatenating the terms is divided by M. Examples Input 5 3 4 10007 Output 5563 Input 4 8 1 1000000 Output 891011 Input 107 10000000000007 1000000000000007 998244353 Output 39122908 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 us consider a grid of squares with 10^9 rows and N columns. Let (i, j) be the square at the i-th column (1 \leq i \leq N) from the left and j-th row (1 \leq j \leq 10^9) from the bottom. Snuke has cut out some part of the grid so that, for each i = 1, 2, ..., N, the bottom-most h_i squares are remaining in the i-th column from the left. Now, he will paint the remaining squares in red and blue. Find the number of the ways to paint the squares so that the following condition is satisfied: * Every remaining square is painted either red or blue. * For all 1 \leq i \leq N-1 and 1 \leq j \leq min(h_i, h_{i+1})-1, there are exactly two squares painted red and two squares painted blue among the following four squares: (i, j), (i, j+1), (i+1, j) and (i+1, j+1). Since the number of ways can be extremely large, print the count modulo 10^9+7. Constraints * 1 \leq N \leq 100 * 1 \leq h_i \leq 10^9 Input Input is given from Standard Input in the following format: N h_1 h_2 ... h_N Output Print the number of the ways to paint the squares, modulo 10^9+7. Examples Input 9 2 3 5 4 1 2 4 2 1 Output 12800 Input 2 2 2 Output 6 Input 5 2 1 2 1 2 Output 256 Input 9 27 18 28 18 28 45 90 45 23 Output 844733013 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 is going on a trip in a two-dimensional plane. In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i. If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1). Note that he cannot stay at his place. Determine whether he can carry out his plan. Constraints * 1 ≀ N ≀ 10^5 * 0 ≀ x_i ≀ 10^5 * 0 ≀ y_i ≀ 10^5 * 1 ≀ t_i ≀ 10^5 * t_i < t_{i+1} (1 ≀ i ≀ N-1) * All input values are integers. Input Input is given from Standard Input in the following format: N t_1 x_1 y_1 t_2 x_2 y_2 : t_N x_N y_N Output If AtCoDeer can carry out his plan, print `Yes`; if he cannot, print `No`. Examples Input 2 3 1 2 6 1 1 Output Yes Input 1 2 100 100 Output No Input 2 5 1 1 100 1 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. You are given N items. The value of the i-th item (1 \leq i \leq N) is v_i. Your have to select at least A and at most B of these items. Under this condition, find the maximum possible arithmetic mean of the values of selected items. Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized. Constraints * 1 \leq N \leq 50 * 1 \leq A,B \leq N * 1 \leq v_i \leq 10^{15} * Each v_i is an integer. Input The input is given from Standard Input in the following format: N A B v_1 v_2 ... v_N Output Print two lines. The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}. The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized. Examples Input 5 2 2 1 2 3 4 5 Output 4.500000 1 Input 4 2 3 10 20 10 10 Output 15.000000 3 Input 5 1 5 1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996 Output 1000000000000000.000000 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. Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa. In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is `B`, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is `W`, it means that the color of the corresponding stone is white. Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place. Constraints * 1 ≦ |S| ≦ 10^5 * Each character in S is `B` or `W`. Input The input is given from Standard Input in the following format: S Output Print the minimum number of new stones that Jiro needs to place for his purpose. Examples Input BBBWW Output 1 Input WWWWWW Output 0 Input WBWBWBWBWB Output 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. Seen from above, there is a grid-like square shaped like Figure 1. The presence or absence of "walls" on each side of this grid is represented by a sequence of 0s and 1s. Create a program that stands at point A, puts your right hand on the wall, keeps walking in the direction of the arrow, and outputs the route to return to point A again. <image> --- Figure 1 --- Input The input consists of 9 lines and is given in the following format, with 1 being the presence of a wall and 0 being the absence of a wall, as shown in Figure 2 below. The first line is a character string that indicates the presence or absence of the top horizontal line wall as 0 and 1 from the left. The second line is a character string that indicates the presence or absence of the vertical line wall below it with 0 and 1 from the left. The third line is a character string that indicates the presence or absence of the wall of the second horizontal line from the top by 0 and 1 from the left. ... The 9th line is a character string representing the presence or absence of the bottom horizontal line wall with 0 and 1 from the left. <image> --- Figure 2 (Thick line shows where the wall is) (corresponding numbers) However, as shown by the thick line in Fig. 1, it is assumed that there is always a wall for one section to the right of point A. That is, the first character on the first line is always 1. Output "Advance one section to the left of the figure" is "L", "Advance one section to the right of the figure" is "R", "Advance one section to the top of the figure" is "U", "Figure" "Advance one block downward" is represented by "D", and "L", "R", "U", and "D" are output in the order of advance. Example Input 1111 00001 0110 01011 0010 01111 0010 01001 0111 Output RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL 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. Blackjack is a type of card game played in casinos, where the game is played using cards with numbers from 1 to 13. The score of each card is decided as follows. * 1 is 1 point or 11 points * From 2 to 9, the score is as written. * 10 points from 10 to 13 There are several participants in this game, including parents, each with several sets of cards. This set of cards is called a hand. The hand score is the total of the card scores. The calculation shall be performed as follows. * If the total score of the cards is greater than 21, the score of the hand is set to 0. * As the score of the card, 1 may be calculated as 1 point or 11 points, but the one with the maximum hand score shall be selected. Create a program that uses the information of the cards dealt as input and outputs the score of the hand. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: c1 c2 ... cn The integer ci (1 ≀ ci ≀ 13) written on the i-th card is given to each line, separated by blanks. The number of cards n does not exceed 100. The number of datasets does not exceed 200. Output The score of the hand is output to one line for each data set. Example Input 1 7 7 7 7 7 8 12 1 10 1 1 0 Output 11 21 0 21 12 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. Have you ever had an infinite loop when you ran a hard-working program? It would be convenient to be able to determine in advance whether a program will stop executing without having to execute it. Unfortunately, it is not possible to make such a decision for any program in the programming language you normally use. However, if you have a programming language that is much less computationally powerful, you may be able to write a program that determines if a program written in that language will stop. Consider a programming language called TinyPower. Programs in this language are line sequences. On each line of the program, write the line number at the beginning and one sentence after it. The types of sentences that can be written in this language are as follows. Sentence type | Behavior --- | --- ADD var1 var2 var3 | Assign the result of adding the value of variable var2 and the value of var3 to variable var1 ADD var1 var2 con | Assign the result of adding the value of the variable var2 and the constant con to the variable var1 SUB var1 var2 var3 | Assign the result of subtracting the value of var3 from the value of variable var2 to variable var1 SUB var1 var2 con | Substitute the result of subtracting the constant con from the value of the variable var2 into the variable var1 SET var1 var2 | Assign the value of variable var2 to variable var1 SET var1 con | Assign the constant con to the variable var1 IF var1 dest | Jump to line number dest only if the value of variable var1 is non-zero HALT | Stop the program Line numbers are positive integers, and the same line number will never appear more than once in the program. Variables are represented by a single lowercase letter, and constants and variable values ​​are integers. No variable declaration is required, the initial value of the variable is 0. Program execution starts with the first statement, and the statements are executed in the order in which they are lined up. However, as written in the table above, if the value of the variable in the IF statement is not 0, jump to the line specified by the line number written after the variable and start from the statement written in that line. Continue running. The program will stop when: * When the HALT statement is executed. * When trying to assign a negative integer or an integer greater than or equal to 16 to a variable (the value of the variable is not updated). * When trying to jump to a line number that does not appear in the program. * When you do not jump to any line from the last statement of the program. Create a program that determines if a TinyPower program, given it, will stop. Input The input is given in the following format. N stmt1 stmt2 :: stmtN The number of lines N (1 ≀ N ≀ 50) of the program is given on the first line. The following N lines are given the statement stmti of the TinyPower program. stmti is given in one of the following formats: line ADD var1 var2 var3 Or line ADD var1 var2 con Or line SUB var1 var2 var3 Or line SUB var1 var2 con Or line SET var1 var2 Or line SET var1 con Or line IF var1 dest Or line HALT line, dest (1 ≀ line, dest ≀ 1000) is the line number, varj (one lowercase letter) is the variable, and con (0 ≀ con ≀ 15) is the constant. The delimiter in stmti is one blank character. It is assumed that one or more variables always appear in the program, and only five different variable names appear. Output When the program stops, the results of the variables appearing in the program are output in the lexicographic order of the variable names, separated by line breaks, and when it does not stop, "inf" is output. The result of the variable is output by separating the variable name and the value of the variable with "=". Examples Input 6 10 SET c 1 20 SET i 5 100 ADD s s i 110 SUB i i c 120 IF i 100 200 HALT Output c=1 i=0 s=15 Input 3 10 SET c 1 120 IF c 10 20 HALT Output inf Input 3 111 SET c 1 12 SUB c c 2 777 SET a 4 Output a=0 c=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. JOI is playing with a nail in the board. As shown in the figure below, JOI stabbed nails in the shape of an equilateral triangle with N sides. A nails are lined up in the ath line (1 ≀ a ≀ N) from the top. The bth nail (1 ≀ b ≀ a) from the left is represented by (a, b). <image> Figure 1: Arrangement of nails (when N = 5) When an equilateral triangle with a nail as its apex is "each side is parallel to one of the sides of the entire equilateral triangle and has the same orientation as the entire equilateral triangle", this equilateral triangle is called a "good equilateral triangle". That is, a "good equilateral triangle" is an equilateral triangle whose vertices are three nails (a, b), (a + x, b), (a + x, b + x) (but a). , B, x satisfy 1 ≀ a <N, 1 ≀ b ≀ a, 1 ≀ x ≀ N --a)). JOI decided to use a rubber band to surround the "good equilateral triangle." <image> Figure 2: An example of how to enclose a "good equilateral triangle" with a rubber band input Read the following data from standard input. * The integers N and M are written on the first line, separated by blanks. N represents the number of nails lined up on one side of an equilateral triangle, and M represents the number of rubber bands that JOI has. * The following M line shows information on how to enclose a "good equilateral triangle" with a rubber band. The integers Ai, Bi, Xi (1 ≀ Ai <N, 1 ≀ Bi ≀ Ai, 1 ≀ Xi ≀ N --Ai) are written on the first line (1 ≀ i ≀ M), separated by blanks. .. This means that the i-th rubber band surrounds a "good equilateral triangle" with three nails (Ai, Bi), (Ai + Xi, Bi), (Ai + Xi, Bi + Xi) as vertices. Represent. output Output the number of nails surrounded by one or more rubber bands to the standard output in one line. Examples Input 5 2 2 2 1 2 1 3 Output 12 Input None Output None 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. Polygons are the most fundamental objects in geometric processing. Complex figures are often represented and handled as polygons with many short sides. If you are interested in the processing of geometric data, you'd better try some programming exercises about basic operations on polygons. Your job in this problem is to write a program that computes the area of polygons. A polygon is represented by a sequence of points that are its vertices. If the vertices p1, p2, ..., pn are given, line segments connecting pi and pi+1 (1 <= i <= n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon. You can assume that the polygon is not degenerate. Namely, the following facts can be assumed without any input data checking. * No point will occur as a vertex more than once. * Two sides can intersect only at a common endpoint (vertex). * The polygon has at least 3 vertices. Note that the polygon is not necessarily convex. In other words, an inner angle may be larger than 180 degrees. Input The input contains multiple data sets, each representing a polygon. A data set is given in the following format. n x1 y1 x2 y2 ... xn yn The first integer n is the number of vertices, such that 3 <= n <= 50. The coordinate of a vertex pi is given by (xi, yi). xi and yi are integers between 0 and 1000 inclusive. The coordinates of vertices are given in the order of clockwise visit of them. The end of input is indicated by a data set with 0 as the value of n. Output For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the polygon separated by a single space. The area should be printed with one digit to the right of the decimal point. The sequence number and the area should be printed on the same line. Since your result is checked by an automatic grading program, you should not insert any extra characters nor lines on the output. Example Input 3 1 1 3 4 6 0 7 0 0 10 10 0 20 10 30 0 40 100 40 100 0 0 Output 1 8.5 2 3800.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. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 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 be the sum of divisors of an integer N excluding the number itself. When N = S, N is called a perfect number, when N> S, N is called a defendant number, and when N <S, N is called an abundant number. Create a program that determines whether a given integer is a perfect number, a missing number, or an abundant number. Be careful not to exceed the program execution time. Input The input consists of a sequence of datasets. The number of datasets is 100 or less. Each dataset consists of one row containing only the integer N (0 <N ≀ 100000000). After the last dataset, there is a line marked 0 that marks the end of the input. Output For each dataset, print the string "` perfect number` "if the integer N is a perfect number," `deficient number`" if it is a missing number, or "` abundant number` "if it is an abundant number. .. Example Input 1 2 3 4 6 12 16 28 33550336 99999998 99999999 100000000 0 Output deficient number deficient number deficient number deficient number perfect number abundant number deficient number perfect number perfect number deficient number deficient number abundant number 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. Taro came to a square to look for treasure. There are many treasures buried in this square, but Taro has the latest machines, so he knows everything about where the treasures are buried. Since the square is very wide Taro decided to look for the treasure to decide the area, but the treasure is what treasure does not know immediately whether or not there in the area for a lot. So Taro decided to count the number of treasures in that area. Constraints > 1 ≀ n ≀ 5000 > 1 ≀ m ≀ 5 Γ— 105 > | xi |, | yi | ≀ 109 (1 ≀ i ≀ n) > | xi1 |, | yi1 |, | xi2 |, | yi2 | ≀ 109 (1 ≀ i ≀ m) > xi1 ≀ xi2, yi1 ≀ yi2 (1 ≀ i ≀ m) > * All inputs are given as integers Input > n m > x1 y1 > x2 y2 > ... > xn yn > x11 y11 x12 y12 > x21 y21 x22 y22 > ... > xm1 ym1 xm2 ym2 > * n represents the number of treasures buried in the square * m represents the number of regions to examine * The 2nd to n + 1 lines represent the coordinates where each treasure is buried. * The n + 2nd to n + m + 1 lines represent each area to be examined. * The positive direction of the x-axis represents the east and the positive direction of the y-axis represents the north. * Each region is a rectangle, xi1 and yi1 represent the coordinates of the southwestern apex of the rectangle, and xi2 and yi2 represent the coordinates of the northeastern apex of the rectangle. Output > C1 > C2 > ... > Cm > * Output the number of treasures contained in each area to each line Examples Input 3 1 1 1 2 4 5 3 0 0 5 5 Output 3 Input 4 2 -1 1 0 3 4 0 2 1 -3 1 5 1 4 0 4 0 Output 2 1 Input 2 3 0 0 0 0 -1 -1 1 1 0 0 2 2 1 1 4 4 Output 2 2 0 Input 5 5 10 5 -3 -8 2 11 6 0 -1 3 -3 1 3 13 -1 -1 9 5 -3 -8 10 11 0 0 5 5 -10 -9 15 10 Output 2 2 5 0 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. Broken crypto generator JAG (Japanese Alumni Group) is a mysterious organization composed of many programmers, and in order to enter the building where the headquarters of this organization is located, it is necessary to solve the ciphertext generated by a certain machine every time. This ciphertext consists of the symbols'+','-','[',']' and the uppercase alphabet, and is represented by <Cipher> defined by the following BNF. <Cipher> :: = <String> | <Cipher> <String> <String> :: = <Letter> |'['<Cipher>']' <Letter> :: ='+' <Letter> |'-' <Letter> | 'A' |'B' |'C' |'D' |'E' |'F' |'G' |'H' |'I' |'J' |'K' |'L' |'M '| 'N' |'O' |'P' |'Q' |'R' |'S' |'T' |'U' |'V' |'W' |'X' |'Y' |'Z ' Here, each symbol has the following meaning. * + (Character): Represents the alphabet following that character (provided that the alphabet following'Z'is'A') *-(Character): Represents the alphabet before that character (provided that the alphabet before'A'is'Z') * [(Character string)]: Represents a character string that is horizontally inverted. However, the machine that generates this ciphertext is currently out of order, and some letters of the alphabet in the ciphertext may be broken and unreadable. Unreadable characters are tentatively represented as'?'. As a result of the investigation, it was found that the method of filling the broken characters is such that the decrypted character string is the smallest in the dictionary order among the possible character strings after decoding. Your job is to decrypt this ciphertext correctly. Input The input consists of multiple datasets. Each dataset consists of one line containing a string in which some uppercase letters have been replaced with β€˜?’ In the ciphertext defined by BNF above. You can assume that the length of each string is less than $ 80 $. You can also assume that the number of'?' In each dataset is greater than or equal to $ 0 $ and less than or equal to $ 3 $. The end of the input is represented by a line containing only one character,'.'. Output For each data set, output the decrypted character string when the ciphertext is decrypted so that the decrypted character string is the smallest in the dictionary order. Sample Input A + A ++ A Z-Z--Z + -Z [ESREVER] J ---? --- J ++++++++ A +++ Z ----------- A +++ Z [[++-+-? [-++-? ++-+++ L]] [-+ ----- + -O]] ++++ --- + L .. Output for Sample Input ABC ZYXZ REVERSE JAG ICPC JAPAN Example Input A+A++A Z-Z--Z+-Z [ESREVER] J---?---J ++++++++A+++Z-----------A+++Z [[++-+--?[--++-?++-+++L]][-+-----+-O]]++++---+L . Output ABC ZYXZ REVERSE JAG ICPC JAPAN 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. B-Mansion and courier Problem Statement Taro lives alone in a mansion. Taro, who loves studying, intends to study in his study in the house today. Taro can't concentrate outside the study, so he always studies in the study. However, on this day, $ N $ of courier service to Taro arrived. $ i $ ($ 1 \ leq i \ leq N $) The arrival time of the third courier is $ a_i $. It is painful to have the delivery person wait at the front door, so Taro decided to be at the front door by the time the courier arrives. Due to the large size of the mansion, it takes $ M $ one way to move between the study and the entrance. On the other hand, Taro wants to study for as long as possible. Find the maximum amount of time Taro can study in the study from time $ 0 $ to time $ T $. Taro is in the study at time $ 0 $, and the courier does not arrive earlier than the time $ M $, and the courier does not arrive later than the time $ T $. Also, the time it takes for Taro to receive the courier can be ignored. Input Each dataset consists of two lines. The first line consists of three integers $ N, M, T $ separated by blanks. These integers satisfy $ 1 \ leq N \ leq 100 $, $ 1 \ leq M \ leq 10 {,} 000 $, $ 1 \ leq T \ leq 10 {,} 000 $. The second line consists of $ N $ integers $ a_1, a_2, \ dots, a_N $ separated by blanks. Each $ a_i $ fills $ M \ leq a_i \ leq T $ and is also $ a_i <a_ {i + 1} $ ($ 1 \ leq i <N $). Output Output an integer representing the maximum amount of time Taro can study on one line. Sample Input 1 1 1 5 3 Output for the Sample Input 1 3 Sample Input 2 2 1 10 2 7 Output for the Sample Input 2 6 Sample Input 3 2 4 10 6 8 Output for the Sample Input 3 2 Example Input 1 1 5 3 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. problem Given a sequence $ a_i $ of length $ N $. Output all integers $ K (1 \ le K \ le N) $ that satisfy the following conditions. Condition: Well sorted $ a_1, \ cdots, a_K $ matches $ a_ {N-K + 1}, \ cdots, a_N $. Example Input 8 5 2 4 9 4 9 2 5 Output 1 2 4 6 7 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. Shell Sort Shell Sort is a generalization of Insertion Sort to arrange a list of $n$ elements $A$. 1 insertionSort(A, n, g) 2 for i = g to n-1 3 v = A[i] 4 j = i - g 5 while j >= 0 && A[j] > v 6 A[j+g] = A[j] 7 j = j - g 8 cnt++ 9 A[j+g] = v 10 11 shellSort(A, n) 12 cnt = 0 13 m = ? 14 G[] = {?, ?,..., ?} 15 for i = 0 to m-1 16 insertionSort(A, n, G[i]) A function shellSort(A, n) performs a function insertionSort(A, n, g), which considers every $g$-th elements. Beginning with large values of $g$, it repeats the insertion sort with smaller $g$. Your task is to complete the above program by filling ?. Write a program which reads an integer $n$ and a sequence $A$, and prints $m$, $G_i (i = 0, 1, ..., m βˆ’ 1)$ in the pseudo code and the sequence $A$ in ascending order. The output of your program must meet the following requirements: * $1 \leq m \leq 100$ * $0 \leq G_i \leq n$ * cnt does not exceed $\lceil n^{1.5}\rceil$ Constraints * $1 \leq n \leq 1,000,000$ * $0 \leq A_i \leq 10^9$ Input In the first line, an integer $n$ is given. In the following $n$ lines, $A_i (i=0,1,...,n-1)$ are given for each line. Output In the first line, print an integer $m$. In the second line, print $m$ integers $G_i (i=0,1,...,m-1)$ separated by single space character in a line. In the third line, print cnt in a line. In the following $n$ lines, print $A_i (i=0,1,...,n-1)$ respectively. This problem has multiple solutions and the judge will be performed by a special validator. Examples Input 5 5 1 4 3 2 Output 2 4 1 3 1 2 3 4 5 Input 3 3 2 1 Output 1 1 3 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. Write a program which reads a $ n \times m$ matrix $A$ and a $m \times 1$ vector $b$, and prints their product $Ab$. A column vector with m elements is represented by the following equation. \\[ b = \left( \begin{array}{c} b_1 \\\ b_2 \\\ : \\\ b_m \\\ \end{array} \right) \\] A $n \times m$ matrix with $m$ column vectors, each of which consists of $n$ elements, is represented by the following equation. \\[ A = \left( \begin{array}{cccc} a_{11} & a_{12} & ... & a_{1m} \\\ a_{21} & a_{22} & ... & a_{2m} \\\ : & : & : & : \\\ a_{n1} & a_{n2} & ... & a_{nm} \\\ \end{array} \right) \\] $i$-th element of a $m \times 1$ column vector $b$ is represented by $b_i$ ($i = 1, 2, ..., m$), and the element in $i$-th row and $j$-th column of a matrix $A$ is represented by $a_{ij}$ ($i = 1, 2, ..., n,$ $j = 1, 2, ..., m$). The product of a $n \times m$ matrix $A$ and a $m \times 1$ column vector $b$ is a $n \times 1$ column vector $c$, and $c_i$ is obtained by the following formula: \\[ c_i = \sum_{j=1}^m a_{ij}b_j = a_{i1}b_1 + a_{i2}b_2 + ... + a_{im}b_m \\] Constraints * $1 \leq n, m \leq 100$ * $0 \leq b_i, a_{ij} \leq 1000$ Input In the first line, two integers $n$ and $m$ are given. In the following $n$ lines, $a_{ij}$ are given separated by a single space character. In the next $m$ lines, $b_i$ is given in a line. Output The output consists of $n$ lines. Print $c_i$ in a line. Example Input 3 4 1 2 0 1 0 3 0 1 4 1 1 0 1 2 3 0 Output 5 6 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. In this problem we consider a very simplified model of Barcelona city. Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0. One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A. Input The first line contains three integers a, b and c (-10^9≀ a, b, c≀ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue. The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≀ x_1, y_1, x_2, y_2≀ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2). Output Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}. Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≀ 10^{-6}. Examples Input 1 1 -3 0 3 3 0 Output 4.2426406871 Input 3 1 -9 0 3 3 -1 Output 6.1622776602 Note The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot. <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. Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic... To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most l centimeters after haircut, where l is her favorite number. Suppose, that the Alice's head is a straight line on which n hairlines grow. Let's number them from 1 to n. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length l, given that all hairlines on that segment had length strictly greater than l. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second. Alice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types: * 0 β€” Alice asks how much time the haircut would take if she would go to the hairdresser now. * 1 p d β€” p-th hairline grows by d centimeters. Note, that in the request 0 Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length. Input The first line contains three integers n, m and l (1 ≀ n, m ≀ 100 000, 1 ≀ l ≀ 10^9) β€” the number of hairlines, the number of requests and the favorite number of Alice. The second line contains n integers a_i (1 ≀ a_i ≀ 10^9) β€” the initial lengths of all hairlines of Alice. Each of the following m lines contains a request in the format described in the statement. The request description starts with an integer t_i. If t_i = 0, then you need to find the time the haircut would take. Otherwise, t_i = 1 and in this moment one hairline grows. The rest of the line than contains two more integers: p_i and d_i (1 ≀ p_i ≀ n, 1 ≀ d_i ≀ 10^9) β€” the number of the hairline and the length it grows by. Output For each query of type 0 print the time the haircut would take. Example Input 4 7 3 1 2 3 4 0 1 2 3 0 1 1 3 0 1 3 1 0 Output 1 2 2 1 Note Consider the first example: * Initially lengths of hairlines are equal to 1, 2, 3, 4 and only 4-th hairline is longer l=3, and hairdresser can cut it in 1 second. * Then Alice's second hairline grows, the lengths of hairlines are now equal to 1, 5, 3, 4 * Now haircut takes two seonds: two swings are required: for the 4-th hairline and for the 2-nd. * Then Alice's first hairline grows, the lengths of hairlines are now equal to 4, 5, 3, 4 * The haircut still takes two seconds: with one swing hairdresser can cut 4-th hairline and with one more swing cut the segment from 1-st to 2-nd hairline. * Then Alice's third hairline grows, the lengths of hairlines are now equal to 4, 5, 4, 4 * Now haircut takes only one second: with one swing it is possible to cut the segment from 1-st hairline to the 4-th. 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 call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3. You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice). For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4: * if you remove a_1, the array will look like [3, 5, 2] and it is good; * if you remove a_4, the array will look like [8, 3, 5] and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array. Input The first line of the input contains one integer n (2 ≀ n ≀ 2 β‹… 10^5) β€” the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^6) β€” elements of the array a. Output In the first line print one integer k β€” the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print k distinct integers j_1, j_2, ..., j_k in any order β€” nice indices of the array a. If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all. Examples Input 5 2 5 1 2 2 Output 3 4 1 5 Input 4 8 3 5 2 Output 2 1 4 Input 5 2 1 2 4 3 Output 0 Note In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2). In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5). In the third example you cannot make the given array good by removing exactly one element. 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 n Γ— m table, consisting of characters Β«AΒ», Β«GΒ», Β«CΒ», Β«TΒ». Let's call a table nice, if every 2 Γ— 2 square contains all four distinct characters. Your task is to find a nice table (also consisting of Β«AΒ», Β«GΒ», Β«CΒ», Β«TΒ»), that differs from the given table in the minimum number of characters. Input First line contains two positive integers n and m β€” number of rows and columns in the table you are given (2 ≀ n, m, n Γ— m ≀ 300 000). Then, n lines describing the table follow. Each line contains exactly m characters Β«AΒ», Β«GΒ», Β«CΒ», Β«TΒ». Output Output n lines, m characters each. This table must be nice and differ from the input table in the minimum number of characters. Examples Input 2 2 AG CT Output AG CT Input 3 5 AGCAG AGCAG AGCAG Output TGCAT CATGC TGCAT Note In the first sample, the table is already nice. In the second sample, you can change 9 elements to make the table nice. 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. Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task. You are given two matrices A and B of size n Γ— m, each of which consists of 0 and 1 only. You can apply the following operation to the matrix A arbitrary number of times: take any submatrix of the matrix A that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain 0, will be replaced by 1, and all corners of the submatrix that contain 1, will be replaced by 0). You have to answer whether you can obtain the matrix B from the matrix A. <image> An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow. Ramesses don't want to perform these operations by himself, so he asks you to answer this question. A submatrix of matrix M is a matrix which consist of all elements which come from one of the rows with indices x_1, x_1+1, …, x_2 of matrix M and one of the columns with indices y_1, y_1+1, …, y_2 of matrix M, where x_1, x_2, y_1, y_2 are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2), where the cell (i,j) denotes the cell on the intersection of the i-th row and the j-th column. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in matrices A and B. Each of the next n lines contain m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix A (0 ≀ A_{ij} ≀ 1). Each of the next n lines contain m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix B (0 ≀ B_{ij} ≀ 1). Output Print "Yes" (without quotes) if it is possible to transform the matrix A to the matrix B using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower). Examples Input 3 3 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 Output Yes Input 6 7 0 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 0 1 Output Yes Input 3 4 0 1 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 Output No Note The examples are explained below. <image> Example 1. <image> Example 2. <image> Example 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. The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of n students. Students will take the exam one-by-one in order from 1-th to n-th. Rules of the exam are following: * The i-th student randomly chooses a ticket. * if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. * if the student finds the ticket easy, he spends exactly t_i minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is M minutes (max t_i ≀ M), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student i, you should count the minimum possible number of students who need to fail the exam so the i-th student has enough time to pass the exam. For each student i, find the answer independently. That is, if when finding the answer for the student i_1 some student j should leave, then while finding the answer for i_2 (i_2>i_1) the student j student does not have to go home. Input The first line of the input contains two integers n and M (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ M ≀ 2 β‹… 10^7) β€” the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains n integers t_i (1 ≀ t_i ≀ 100) β€” time in minutes that i-th student spends to answer to a ticket. It's guaranteed that all values of t_i are not greater than M. Output Print n numbers: the i-th number must be equal to the minimum number of students who have to leave the exam in order to i-th student has enough time to pass the exam. Examples Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 Note The explanation for the example 1. Please note that the sum of the first five exam times does not exceed M=15 (the sum is 1+2+3+4+5=15). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are 0. In order for the 6-th student to pass the exam, it is necessary that at least 2 students must fail it before (for example, the 3-rd and 4-th, then the 6-th will finish its exam in 1+2+5+6=14 minutes, which does not exceed M). In order for the 7-th student to pass the exam, it is necessary that at least 3 students must fail it before (for example, the 2-nd, 5-th and 6-th, then the 7-th will finish its exam in 1+3+4+7=15 minutes, which does not exceed M). 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 city of Saint Petersburg, a day lasts for 2^{100} minutes. From the main station of Saint Petersburg, a train departs after 1 minute, 4 minutes, 16 minutes, and so on; in other words, the train departs at time 4^k for each integer k β‰₯ 0. Team BowWow has arrived at the station at the time s and it is trying to count how many trains have they missed; in other words, the number of trains that have departed strictly before time s. For example if s = 20, then they missed trains which have departed at 1, 4 and 16. As you are the only one who knows the time, help them! Note that the number s will be given you in a [binary representation](https://en.wikipedia.org/wiki/Binary_number#Representation) without leading zeroes. Input The first line contains a single binary number s (0 ≀ s < 2^{100}) without leading zeroes. Output Output a single number β€” the number of trains which have departed strictly before the time s. Examples Input 100000000 Output 4 Input 101 Output 2 Input 10100 Output 3 Note In the first example 100000000_2 = 256_{10}, missed trains have departed at 1, 4, 16 and 64. In the second example 101_2 = 5_{10}, trains have departed at 1 and 4. The third example is explained in the statements. 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 positive integers a_1, …, a_n, and an integer k β‰₯ 2. Count the number of pairs i, j such that 1 ≀ i < j ≀ n, and there exists an integer x such that a_i β‹… a_j = x^k. Input The first line contains two integers n and k (2 ≀ n ≀ 10^5, 2 ≀ k ≀ 100). The second line contains n integers a_1, …, a_n (1 ≀ a_i ≀ 10^5). Output Print a single integer β€” the number of suitable pairs. Example Input 6 3 1 3 9 8 24 1 Output 5 Note In the sample case, the suitable pairs are: * a_1 β‹… a_4 = 8 = 2^3; * a_1 β‹… a_6 = 1 = 1^3; * a_2 β‹… a_3 = 27 = 3^3; * a_3 β‹… a_5 = 216 = 6^3; * a_4 β‹… a_6 = 8 = 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. Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature β€” color, number, shape, and shading β€” the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. <image> Polina came up with a new game called "Hyperset". In her game, there are n cards with k features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with k = 4. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. Input The first line of each test contains two integers n and k (1 ≀ n ≀ 1500, 1 ≀ k ≀ 30) β€” number of cards and number of features. Each of the following n lines contains a card description: a string consisting of k letters "S", "E", "T". The i-th character of this string decribes the i-th feature of that card. All cards are distinct. Output Output a single integer β€” the number of ways to choose three cards that form a set. Examples Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 Note In the third example test, these two triples of cards are sets: 1. "SETT", "TEST", "EEET" 2. "TEST", "ESTE", "STES" 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 has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically, he wants to get from (0,0) to (x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its n favorite numbers: a_1, a_2, …, a_n. What is the minimum number of hops Rabbit needs to get from (0,0) to (x,0)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination. Recall that the Euclidean distance between points (x_i, y_i) and (x_j, y_j) is √{(x_i-x_j)^2+(y_i-y_j)^2}. For example, if Rabbit has favorite numbers 1 and 3 he could hop from (0,0) to (4,0) in two hops as shown below. Note that there also exists other valid ways to hop to (4,0) in 2 hops (e.g. (0,0) β†’ (2,-√{5}) β†’ (4,0)). <image> Here is a graphic for the first example. Both hops have distance 3, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number a_i and hops with distance equal to a_i in any direction he wants. The same number can be used multiple times. Input The input consists of multiple test cases. The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Next 2t lines contain test cases β€” two lines per test case. The first line of each test case contains two integers n and x (1 ≀ n ≀ 10^5, 1 ≀ x ≀ 10^9) β€” the number of favorite numbers and the distance Rabbit wants to travel, respectively. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9) β€” Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct. It is guaranteed that the sum of n over all the test cases will not exceed 10^5. Output For each test case, print a single integer β€” the minimum number of hops needed. Example Input 4 2 4 1 3 3 12 3 4 5 1 5 5 2 10 15 4 Output 2 3 1 2 Note The first test case of the sample is shown in the picture above. Rabbit can hop to (2,√{5}), then to (4,0) for a total of two hops. Each hop has a distance of 3, which is one of his favorite numbers. In the second test case of the sample, one way for Rabbit to hop 3 times is: (0,0) β†’ (4,0) β†’ (8,0) β†’ (12,0). In the third test case of the sample, Rabbit can hop from (0,0) to (5,0). In the fourth test case of the sample, Rabbit can hop: (0,0) β†’ (5,10√{2}) β†’ (10,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. The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation. Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2. Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2. You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.) Input The first line contains an integer t (1 ≀ t ≀ 10 000) denoting the number of test cases in the input. Each test case contains two lines. The first line contains one integer n (2 ≀ n ≀ 200 000): the length of a. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n-1). The total sum of n is less than 200 000. Output For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2. Each of the next k lines should contain two integers l_1 and l_2 (1 ≀ l_1, l_2 ≀ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order. Example Input 6 5 1 4 3 2 1 6 2 4 1 3 2 1 4 2 1 1 3 4 1 3 3 1 12 2 1 3 4 5 6 7 8 9 1 10 2 3 1 1 1 Output 2 1 4 4 1 1 4 2 0 0 1 2 10 0 Note In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}. In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}. In the third example, there are no possible ways. 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 models in the shop numbered from 1 to n, with sizes s_1, s_2, …, s_n. Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes). Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices i_j and i_{j+1} (note that i_j < i_{j+1}, because Orac arranged them properly), i_{j+1} is divisible by i_j and s_{i_j} < s_{i_{j+1}}. For example, for 6 models with sizes \{3, 6, 7, 7, 7, 7\}, he can buy models with indices 1, 2, and 6, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful. Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times. Input The first line contains one integer t\ (1 ≀ t≀ 100): the number of queries. Each query contains two lines. The first line contains one integer n\ (1≀ n≀ 100 000): the number of models in the shop, and the second line contains n integers s_1,...,s_n\ (1≀ s_i≀ 10^9): the sizes of models. It is guaranteed that the total sum of n is at most 100 000. Output Print t lines, the i-th of them should contain the maximum number of models that Orac can buy for the i-th query. Example Input 4 4 5 3 4 6 7 1 4 2 3 6 4 9 5 5 4 3 2 1 1 9 Output 2 3 1 1 Note In the first query, for example, Orac can buy models with indices 2 and 4, the arrangement will be beautiful because 4 is divisible by 2 and 6 is more than 3. By enumerating, we can easily find that there are no beautiful arrangements with more than two models. In the second query, Orac can buy models with indices 1, 3, and 6. By enumerating, we can easily find that there are no beautiful arrangements with more than three models. In the third query, there are no beautiful arrangements with more than one model. 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. Naman has two binary strings s and t of length n (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert s into t using the following operation as few times as possible. In one operation, he can choose any subsequence of s and rotate it clockwise once. For example, if s = 1110100, he can choose a subsequence corresponding to indices (1-based) \{2, 6, 7 \} and rotate them clockwise. The resulting string would then be s = 1010110. A string a is said to be a subsequence of string b if a can be obtained from b by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence c of size k is to perform an operation which sets c_1:=c_k, c_2:=c_1, c_3:=c_2, …, c_k:=c_{k-1} simultaneously. Determine the minimum number of operations Naman has to perform to convert s into t or say that it is impossible. Input The first line contains a single integer n (1 ≀ n ≀ 10^6) β€” the length of the strings. The second line contains the binary string s of length n. The third line contains the binary string t of length n. Output If it is impossible to convert s to t after any number of operations, print -1. Otherwise, print the minimum number of operations required. Examples Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 Note In the first test, Naman can choose the subsequence corresponding to indices \{2, 6\} and rotate it once to convert s into t. In the second test, he can rotate the subsequence corresponding to all indices 5 times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert s into t. 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 loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task. Input The single line contains four integers a1, a2, a3 and a4 (1 ≀ a1, a2, a3, a4 ≀ 106). Output On the single line print without leading zeroes the answer to the problem β€” the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes). Examples Input 2 2 1 1 Output 4774 Input 4 7 3 1 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. You are given a matrix a consisting of positive integers. It has n rows and m columns. Construct a matrix b consisting of positive integers. It should have the same size as a, and the following conditions should be met: * 1 ≀ b_{i,j} ≀ 10^6; * b_{i,j} is a multiple of a_{i,j}; * the absolute value of the difference between numbers in any adjacent pair of cells (two cells that share the same side) in b is equal to k^4 for some integer k β‰₯ 1 (k is not necessarily the same for all pairs, it is own for each pair). We can show that the answer always exists. Input The first line contains two integers n and m (2 ≀ n,m ≀ 500). Each of the following n lines contains m integers. The j-th integer in the i-th line is a_{i,j} (1 ≀ a_{i,j} ≀ 16). Output The output should contain n lines each containing m integers. The j-th integer in the i-th line should be b_{i,j}. Examples Input 2 2 1 2 2 3 Output 1 2 2 3 Input 2 3 16 16 16 16 16 16 Output 16 32 48 32 48 64 Input 2 2 3 11 12 8 Output 327 583 408 664 Note In the first example, the matrix a can be used as the matrix b, because the absolute value of the difference between numbers in any adjacent pair of cells is 1 = 1^4. In the third example: * 327 is a multiple of 3, 583 is a multiple of 11, 408 is a multiple of 12, 664 is a multiple of 8; * |408 - 327| = 3^4, |583 - 327| = 4^4, |664 - 408| = 4^4, |664 - 583| = 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. Vasya has recently bought some land and decided to surround it with a wooden fence. He went to a company called "Wooden board" that produces wooden boards for fences. Vasya read in the catalog of products that the company has at its disposal n different types of wood. The company uses the i-th type of wood to produce a board of this type that is a rectangular ai by bi block. Vasya decided to order boards in this company and build a fence from them. It turned out that the storehouse of the company is so large that Vasya can order arbitrary number of boards of every type. Note that Vasya is allowed to turn the boards as he builds the fence. However, Vasya cannot turn square boards. Vasya is required to construct a fence of length l, however, an arbitrary fence won't do. Vasya wants his fence to look beautiful. We'll say that a fence is beautiful if and only if the following two conditions are fulfilled: * there are no two successive boards of the same type * the first board of the fence has an arbitrary length, and the length of each subsequent board equals the width of the previous one In other words, the fence is considered beautiful, if the type of the i-th board in the fence is different from the i - 1-th board's type; besides, the i-th board's length is equal to the i - 1-th board's width (for all i, starting from 2). Now Vasya wonders, how many variants of arranging a fence for his land exist. Your task is to count the number of different beautiful fences of length l. Two fences will be considered the same if the corresponding sequences of fence boards types and rotations are the same, otherwise the fences are different. Since the sought number can be large enough, you need to calculate the answer modulo 1000000007 (109 + 7). Input The first line contains two integers n and l (1 ≀ n ≀ 100, 1 ≀ l ≀ 3000) β€” the number of different board types and the fence length, correspondingly. Next n lines contain descriptions of board types: the i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ 100) β€” the sizes of the board of the i-th type. All numbers on the lines are separated by spaces. Output Print a single integer β€” the sought number of variants modulo 1000000007 (109 + 7). Examples Input 2 3 1 2 2 3 Output 2 Input 1 2 2 2 Output 1 Input 6 6 2 1 3 2 2 5 3 3 5 1 2 1 Output 20 Note In the first sample there are exactly two variants of arranging a beautiful fence of length 3: * As the first fence board use the board of the first type of length 1 and width 2. As the second board use board of the second type of length 2 and width 3. * Use one board of the second type after you turn it. That makes its length equal 3, and width β€” 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. Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s. If Kirito starts duelling with the i-th (1 ≀ i ≀ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi. Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss. Input The first line contains two space-separated integers s and n (1 ≀ s ≀ 104, 1 ≀ n ≀ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≀ xi ≀ 104, 0 ≀ yi ≀ 104) β€” the i-th dragon's strength and the bonus for defeating it. Output On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't. Examples Input 2 2 1 99 100 0 Output YES Input 10 1 100 100 Output NO Note In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level. In the second sample Kirito's strength is too small to defeat the only dragon and win. 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. Valera considers a number beautiful, if it equals 2k or -2k for some integer k (k β‰₯ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands. Input The first line contains string s (1 ≀ |s| ≀ 106), that is the binary representation of number n without leading zeroes (n > 0). Output Print a single integer β€” the minimum amount of beautiful numbers that give a total of n. Examples Input 10 Output 1 Input 111 Output 2 Input 1101101 Output 4 Note In the first sample n = 2 is a beautiful number. In the second sample n = 7 and Valera can decompose it into sum 23 + ( - 20). In the third sample n = 109 can be decomposed into the sum of four summands as follows: 27 + ( - 24) + ( - 22) + 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. Do you remember how Kai constructed the word "eternity" using pieces of ice as components? Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number t. He realized that digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to use as 9 (and vice versa). Similary, 2 and 5 work the same. There is no other pair of digits with similar effect. He called this effect "Digital Mimicry". Sheldon favourite number is t. He wants to have as many instances of t as possible. How many instances he can construct using the given sequence of ice pieces. He can use any piece at most once. Input The first line contains integer t (1 ≀ t ≀ 10000). The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9. Output Print the required number of instances. Examples Input 42 23454 Output 2 Input 169 12118999 Output 1 Note This problem contains very weak pretests. 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. Β«BersoftΒ» company is working on a new version of its most popular text editor β€” Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces). Your task is to write a part of the program, responsible for Β«standardizationΒ» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≀ ri. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from the new sequence li = ri, this element should be output as li, and not as Β«li - liΒ». For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6. Input The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row. Output Output the sequence in the required format. Examples Input 1,2,3,1,1,2,6,6,2 Output 1-3,6 Input 3,2,1 Output 1-3 Input 30,20,10 Output 10,20,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. DZY loves planting, and he enjoys solving tree problems. DZY has a weighted tree (connected undirected graph without cycles) containing n nodes (they are numbered from 1 to n). He defines the function g(x, y) (1 ≀ x, y ≀ n) as the longest edge in the shortest path between nodes x and y. Specially g(z, z) = 0 for every z. For every integer sequence p1, p2, ..., pn (1 ≀ pi ≀ n), DZY defines f(p) as <image>. DZY wants to find such a sequence p that f(p) has maximum possible value. But there is one more restriction: the element j can appear in p at most xj times. Please, find the maximum possible f(p) under the described restrictions. Input The first line contains an integer n (1 ≀ n ≀ 3000). Each of the next n - 1 lines contains three integers ai, bi, ci (1 ≀ ai, bi ≀ n; 1 ≀ ci ≀ 10000), denoting an edge between ai and bi with length ci. It is guaranteed that these edges form a tree. Each of the next n lines describes an element of sequence x. The j-th line contains an integer xj (1 ≀ xj ≀ n). Output Print a single integer representing the answer. Examples Input 4 1 2 1 2 3 2 3 4 3 1 1 1 1 Output 2 Input 4 1 2 1 2 3 2 3 4 3 4 4 4 4 Output 3 Note In the first sample, one of the optimal p is [4, 3, 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. George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≀ qi). Your task is to count how many rooms has free place for both George and Alex. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of rooms. The i-th of the next n lines contains two integers pi and qi (0 ≀ pi ≀ qi ≀ 100) β€” the number of people who already live in the i-th room and the room's capacity. Output Print a single integer β€” the number of rooms where George and Alex can move in. Examples Input 3 1 1 2 2 3 3 Output 0 Input 3 1 10 0 10 10 10 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 permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not. There are many tasks on permutations. Today you are going to solve one of them. Let’s imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible. Input The first line contains an integer n (1 ≀ n ≀ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105. Output If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutations’ numbering is free. If several solutions are possible, print any one of them. If there’s no solution, print in the first line - 1. Examples Input 9 1 2 3 1 2 1 4 2 5 Output 3 3 1 2 1 2 2 2 3 2 Input 4 4 3 2 1 Output 1 1 1 1 1 Input 4 1 2 2 3 Output -1 Note In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β€” by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β€” by the first and the eigth elements. Clearly, there are other splitting variants 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. Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ— n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ— a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating β€” find Bob's first move, such that after it you can be sure that Alice cheated. Input The first line of the input contains three integers: n, k and a (1 ≀ n, k, a ≀ 2Β·105) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≀ m ≀ n) β€” the number of Bob's moves. The third line contains m distinct integers x1, x2, ..., xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. Output Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". Examples Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 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. The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: <image>. Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it? Input The only line of the input contains a single integer k (0 ≀ k ≀ 9). Output Print 2k lines consisting of 2k characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists. If there are many correct answers, print any. Examples Input 2 Output ++** +*+* ++++ +**+ Note Consider all scalar products in example: * Vectors 1 and 2: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( - 1)Β·( + 1) + ( - 1)Β·( - 1) = 0 * Vectors 1 and 3: ( + 1)Β·( + 1) + ( + 1)Β·( + 1) + ( - 1)Β·( + 1) + ( - 1)Β·( + 1) = 0 * Vectors 1 and 4: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( - 1)Β·( - 1) + ( - 1)Β·( + 1) = 0 * Vectors 2 and 3: ( + 1)Β·( + 1) + ( - 1)Β·( + 1) + ( + 1)Β·( + 1) + ( - 1)Β·( + 1) = 0 * Vectors 2 and 4: ( + 1)Β·( + 1) + ( - 1)Β·( - 1) + ( + 1)Β·( - 1) + ( - 1)Β·( + 1) = 0 * Vectors 3 and 4: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( + 1)Β·( - 1) + ( + 1)Β·( + 1) = 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. Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face. One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget β€” a rotating camera β€” come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification? But not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical. Vasya was entrusted to correct the situation β€” to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized. Help Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum. The next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to "true up". <image> The next figure shows 90 degrees clockwise turn by FPGA hardware. <image> Input The only line of the input contains one integer x ( - 1018 ≀ x ≀ 1018) β€” camera angle in degrees. Positive value denotes clockwise camera rotation, negative β€” counter-clockwise. Output Output one integer β€” the minimum required number of 90 degrees clockwise turns. Examples Input 60 Output 1 Input -60 Output 3 Note When the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from "true up" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from "true up" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise. From 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns. 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 connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads. The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another). In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city. Help the Ministry of Transport to find the minimum possible number of separate cities after the reform. Input The first line of the input contains two positive integers, n and m β€” the number of the cities and the number of roads in Berland (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 100 000). Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≀ xi, yi ≀ n, xi β‰  yi), where xi and yi are the numbers of the cities connected by the i-th road. It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads. Output Print a single integer β€” the minimum number of separated cities after the reform. Examples Input 4 3 2 1 1 3 4 3 Output 1 Input 5 5 2 1 1 3 2 3 2 5 4 3 Output 0 Input 6 5 1 2 2 3 4 5 4 6 5 6 Output 1 Note In the first sample the following road orientation is allowed: <image>, <image>, <image>. The second sample: <image>, <image>, <image>, <image>, <image>. The third sample: <image>, <image>, <image>, <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. Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all. Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 ≀ bi ≀ ai for every 1 ≀ i ≀ n. Your task is to determine the maximum possible value of mex of this array. Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1. Input The first line of the input contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of elements in the Alyona's array. The second line of the input contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print one positive integer β€” the maximum possible value of mex of the array after Alyona applies some (possibly none) operations. Examples Input 5 1 3 3 3 6 Output 5 Input 2 2 1 Output 3 Note In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5. To reach the answer to the second sample case one must not decrease any of the array 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. Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on... For example if n = 1, then his feeling is "I hate it" or if n = 2 it's "I hate that I love it", and if n = 3 it's "I hate that I love that I hate it" and so on. Please help Dr. Banner. Input The only line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of layers of love and hate. Output Print Dr.Banner's feeling in one line. Examples Input 1 Output I hate it Input 2 Output I hate that I love it Input 3 Output I hate that I love that I hate it 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 organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this size. During the registration, the organizers asked each of the n participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizes β€” this means that any of these two sizes suits him. Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size: * the size he wanted, if he specified one size; * any of the two neibouring sizes, if he specified two sizes. If it is possible, the program should find any valid distribution of the t-shirts. Input The first line of the input contains six non-negative integers β€” the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100 000. The second line contains positive integer n (1 ≀ n ≀ 100 000) β€” the number of participants. The following n lines contain the sizes specified by the participants, one line per participant. The i-th line contains information provided by the i-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring. Output If it is not possible to present a t-shirt to each participant, print Β«NOΒ» (without quotes). Otherwise, print n + 1 lines. In the first line print Β«YESΒ» (without quotes). In the following n lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input. If there are multiple solutions, print any of them. Examples Input 0 1 0 1 1 0 3 XL S,M XL,XXL Output YES XL M XXL Input 1 1 2 0 1 1 5 S M S,M XXL,XXXL XL,XXL 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. Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own. Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads. <image> There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city. However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible. Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads. Input The first line contains three integers n, k, and d (2 ≀ n ≀ 3Β·105, 1 ≀ k ≀ 3Β·105, 0 ≀ d ≀ n - 1) β€” the number of cities, the number of police stations, and the distance limitation in kilometers, respectively. The second line contains k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” each denoting the city each police station is located in. The i-th of the following n - 1 lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities directly connected by the road with index i. It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers. Output In the first line, print one integer s that denotes the maximum number of roads that can be shut down. In the second line, print s distinct integers, the indices of such roads, in any order. If there are multiple answers, print any of them. Examples Input 6 2 4 1 6 1 2 2 3 3 4 4 5 5 6 Output 1 5 Input 6 3 2 1 5 6 1 2 1 3 1 4 1 5 5 6 Output 2 4 5 Note In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers. In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line. 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 permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array. Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≀ i ≀ n) such that pi = i. Your task is to count the number of almost identity permutations for given numbers n and k. Input The first line contains two integers n and k (4 ≀ n ≀ 1000, 1 ≀ k ≀ 4). Output Print the number of almost identity permutations for given n and k. Examples Input 4 1 Output 1 Input 4 2 Output 7 Input 5 3 Output 31 Input 5 4 Output 76 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 the Tatvik Hiring Challenge, Karan's NLP professor has given him another homework. Since Manhattan Associates Hiring Challenge is round the corner and Karan is busy preparing for it, he turns to you for help. Given a string, replace all the consecutively occurring characters by a single, same character. Input: The first line contains the number of test cases T. Each test case contains the string, on a separate line. Output: Print the modified string, each on a new line. Constraints: 1 ≀ T ≀ 100 1 ≀ length(string) < 10^6 SAMPLE INPUT 1 aabbcc SAMPLE OUTPUT abc 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 young mischievous boy Harsh, got into a trouble when his mechanical workshop teacher told him to cut Iron rods. The rod cutting algorithm is as follows: Step 1. If the rod can be divided into two equal parts, cut it and choose any one of them. Step 2. Else cut the rod into two parts having non-zero integral lengths such that the difference between the lengths of the two pieces is minimized, and then choose the piece having smaller length. Step 3. Repeat the above algorithm with the currently chosen piece. If the length of the currently chosen piece becomes 1 , stop the algorithm. There can be special rods which require Step 2 in every step of its cutting. Harsh want to find out the number of such special rods. Help Harsh to find out the answer. Input: The first line of the input will contain T, the number of test-cases. Each subsequent T lines will contain an integer N, where N is the range of lengths of rods from 1 to N . Output: For each test-case print the required answer. Constraints: 1 ≀ T ≀ 1000 1 ≀ N ≀ 10^9 SAMPLE INPUT 2 3 2 SAMPLE OUTPUT 1 0 Explanation In the first case there is only 1 such special iron rod,( which is of length 3 ). A rod of length 3 cannot be divided into two equal parts, so we follow Step 2 and divide the rod into 2 and 1 length rod. ( See that the difference between the two rods obtained is minimum among all the possible choices). After that choose the rod having smaller length. Since the length of the currently rod is 1 , the algorithm stops. So we see that during the each step of cutting (although, there is only 1 step involved in cutting ) we have to do mental calculations using Step 2. There is no such rod , other than this in the range 1 and 3. So the answer is 1. In the second case there is no such rod between 1 and 2 , so the answer is 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. We have three boxes A, B, and C, each of which contains an integer. Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively. We will now do the operations below in order. Find the content of each box afterward. * Swap the contents of the boxes A and B * Swap the contents of the boxes A and C Constraints * 1 \leq X,Y,Z \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the integers contained in the boxes A, B, and C, in this order, with space in between. Examples Input 1 2 3 Output 3 1 2 Input 100 100 100 Output 100 100 100 Input 41 59 31 Output 31 41 59 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. Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east. They start simultaneously at the same point and moves as follows towards the east: * Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever. * Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever. How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact. Constraints * 1 \leq T_i \leq 100000 * 1 \leq A_i \leq 10^{10} * 1 \leq B_i \leq 10^{10} * A_1 \neq B_1 * A_2 \neq B_2 * All values in input are integers. Input Input is given from Standard Input in the following format: T_1 T_2 A_1 A_2 B_1 B_2 Output Print the number of times Takahashi and Aoki will meet each other. If they meet infinitely many times, print `infinity` instead. Examples Input 1 2 10 10 12 4 Output 1 Input 100 1 101 101 102 1 Output infinity Input 12000 15700 3390000000 3810000000 5550000000 2130000000 Output 113 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 two non-negative integers L and R. We will choose two integers i and j such that L \leq i < j \leq R. Find the minimum possible value of (i \times j) \mbox{ mod } 2019. Constraints * All values in input are integers. * 0 \leq L < R \leq 2 \times 10^9 Input Input is given from Standard Input in the following format: L R Output Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition. Examples Input 2020 2040 Output 2 Input 4 5 Output 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. You are given positive integers A and B. If A is a divisor of B, print A + B; otherwise, print B - A. Constraints * All values in input are integers. * 1 \leq A \leq B \leq 20 Input Input is given from Standard Input in the following format: A B Output If A is a divisor of B, print A + B; otherwise, print B - A. Examples Input 4 12 Output 16 Input 8 20 Output 12 Input 1 1 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. There are N people. The name of the i-th person is S_i. We would like to choose three people so that the following conditions are met: * The name of every chosen person begins with `M`, `A`, `R`, `C` or `H`. * There are no multiple people whose names begin with the same letter. How many such ways are there to choose three people, disregarding order? Note that the answer may not fit into a 32-bit integer type. Constraints * 1 \leq N \leq 10^5 * S_i consists of uppercase English letters. * 1 \leq |S_i| \leq 10 * S_i \neq S_j (i \neq j) Input Input is given from Standard Input in the following format: N S_1 : S_N Output If there are x ways to choose three people so that the given conditions are met, print x. Examples Input 5 MASHIKE RUMOI OBIRA HABORO HOROKANAI Output 2 Input 4 ZZ ZZZ Z ZZZZZZZZZZ Output 0 Input 5 CHOKUDAI RNG MAKOTO AOKI RINGO Output 7 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. Aoki is in search of Takahashi, who is missing in a one-dimentional world. Initially, the coordinate of Aoki is 0, and the coordinate of Takahashi is known to be x, but his coordinate afterwards cannot be known to Aoki. Time is divided into turns. In each turn, Aoki and Takahashi take the following actions simultaneously: * Let the current coordinate of Aoki be a, then Aoki moves to a coordinate he selects from a-1, a and a+1. * Let the current coordinate of Takahashi be b, then Takahashi moves to the coordinate b-1 with probability of p percent, and moves to the coordinate b+1 with probability of 100-p percent. When the coordinates of Aoki and Takahashi coincide, Aoki can find Takahashi. When they pass by each other, Aoki cannot find Takahashi. Aoki wants to minimize the expected number of turns taken until he finds Takahashi. Find the minimum possible expected number of turns. Constraints * 1 ≦ x ≦ 1,000,000,000 * 1 ≦ p ≦ 100 * x and p are integers. Input The input is given from Standard Input in the following format: x p Output Print the minimum possible expected number of turns. The output is considered correct if the absolute or relative error is at most 10^{-6}. Examples Input 3 100 Output 2.0000000 Input 6 40 Output 7.5000000 Input 101 80 Output 63.7500000 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. Your friend who lives in undisclosed country is involved in debt. He is borrowing 100,000-yen from a loan shark. The loan shark adds 5% interest of the debt and rounds it to the nearest 1,000 above week by week. Write a program which computes the amount of the debt in n weeks. Input An integer n (0 ≀ n ≀ 100) is given in a line. Output Print the amout of the debt in a line. Example Input 5 Output 130000 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 is the ticket release date for Aizu Entertainment's recommended idol group "Akabeko & Koboushi". There are four types of tickets: S seat 6000 yen A seat 4000 yen B seat 3000 yen C seat 2000 yen You, the sales manager, are excitedly waiting for the launch. Finally on sale. It's selling very well! Shortly after the launch, I received a table summarizing the orders up to that point. Each row of the table shows the type and number of tickets sold so far. However, the ticket types do not always appear in the order of S, A, B, C. Create a program to find the sales amount for each row in this table. input Input data is given in the following format. t1 n1 t2 n2 t3 n3 t4 n4 The input consists of 4 lines. Line i is given the integer ti (1 ≀ ti ≀ 4) for the ticket type and the integer ni (0 ≀ ni ≀ 10000) for the number of tickets. The integers 1, 2, 3, and 4 representing the ticket types represent S seats, A seats, B seats, and C seats, respectively. Numbers from 1 to 4 always appear once as values ​​for t1, t2, t3, and t4, but they are not always given in the order of 1, 2, 3, 4. output Output the sales amount for each line. Example Input 3 10 1 4 4 1 2 5 Output 30000 24000 2000 20000 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. Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≀ n ≀ 100 * -10,000 ≀ Ci ≀ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -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. Mary Thomas has a number of sheets of squared paper. Some of squares are painted either in black or some colorful color (such as red and blue) on the front side. Cutting off the unpainted part, she will have eight opened-up unit cubes. A unit cube here refers to a cube of which each face consists of one square. She is going to build those eight unit cubes with the front side exposed and then a bicube with them. A bicube is a cube of the size 2 Γ— 2 Γ— 2, consisting of eight unit cubes, that satisfies the following conditions: * faces of the unit cubes that comes to the inside of the bicube are all black; * each face of the bicube has a uniform colorful color; and * the faces of the bicube have colors all different. Your task is to write a program that reads the specification of a sheet of squared paper and decides whether a bicube can be built with the eight unit cubes resulting from it. Input The input contains the specification of a sheet. The first line contains two integers H and W, which denote the height and width of the sheet (3 ≀ H, W ≀ 50). Then H lines follow, each consisting of W characters. These lines show the squares on the front side of the sheet. A character represents the color of a grid: alphabets and digits ('A' to 'Z', 'a' to 'z', '0' to '9') for colorful squares, a hash ('#') for a black square, and a dot ('.') for an unpainted square. Each alphabet or digit denotes a unique color: squares have the same color if and only if they are represented by the same character. Each component of connected squares forms one opened-up cube. Squares are regarded as connected when they have a common edge; those just with a common vertex are not. Output Print "Yes" if a bicube can be built with the given sheet; "No" otherwise. Examples Input 3 40 .a....a....a....a....f....f....f....f... #bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.#be#. .#....#....#....#....#....#....#....#... Output Yes Input 3 40 .a....a....a....a....f....f....f....f... bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.#be#. .#....#....#....#....#....#....#....#... Output Yes Input 5 35 .a....a....a....a....f....f....f... bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#. .#..f.#....#....#....#....#....#... ..e##.............................. .b#................................ Output Yes Input 3 40 .a....a....a....a....f....f....f....f... bc#.#cd#.#de#.#eb#.#cb#.#dc#.#ed#.#eb#. .#....#....#....#....#....#....#....#... 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's solve the geometric problem Mr. A is still solving geometric problems today. It is important to be aware of floating point errors when solving geometric problems. Floating-point error is the error caused by the rounding that occurs when representing a number in binary finite decimal numbers. For example, 0.1 in decimal is an infinite decimal number of 0.00011001100110011 ... in binary, but an error occurs when rounding this to a finite number of digits. Positive integers p and q are given in decimal notation. Find the b-ary system (b is an integer greater than or equal to 2) so that the rational number p / q can be expressed as a decimal number with a finite number of digits. If there are more than one, output the smallest one. Constraints * 0 <p <q <10 ^ 9 Input Format Input is given from standard input in the following format. p q Output Format Print the answer in one line. Sample Input 1 1 2 Sample Output 1 2 1/2 is binary 0.1 Sample Input 2 21 30 Sample Output 2 Ten 21/30 is 0.7 in decimal Example Input 1 2 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. There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.