question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. There are n integers b_1, b_2, ..., b_{n} written in a row. For all i from 1 to n, values a_{i} are defined by the crows performing the following procedure: The crow sets a_{i} initially 0. The crow then adds b_{i} to a_{i}, subtracts b_{i} + 1, adds the b_{i} + 2 number, and so on until the n'th number. Thus, a_{i} = b_{i} - b_{i} + 1 + b_{i} + 2 - b_{i} + 3.... Memory gives you the values a_1, a_2, ..., a_{n}, and he now wants you to find the initial numbers b_1, b_2, ..., b_{n} written in the row? Can you do it? -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row. The next line contains n, the i'th of which is a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9) — the value of the i'th number. -----Output----- Print n integers corresponding to the sequence b_1, b_2, ..., b_{n}. It's guaranteed that the answer is unique and fits in 32-bit integer type. -----Examples----- Input 5 6 -4 8 -2 3 Output 2 4 6 1 3 Input 5 3 -2 -1 5 6 Output 1 -3 4 11 6 -----Note----- In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3. In the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good. You are given a string $s$, you have to delete minimum number of characters from this string so that it becomes good. -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of characters in $s$. The second line contains the string $s$, consisting of exactly $n$ lowercase Latin letters. -----Output----- In the first line, print one integer $k$ ($0 \le k \le n$) — the minimum number of characters you have to delete from $s$ to make it good. In the second line, print the resulting string $s$. If it is empty, you may leave the second line blank, or not print it at all. -----Examples----- Input 4 good Output 0 good Input 4 aabc Output 2 ab Input 3 aaa Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given two polynomials: * P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and * Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit <image>. Input The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). Output If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit <image>, in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. Examples Input 2 1 1 1 1 2 5 Output Infinity Input 1 0 -1 3 2 Output -Infinity Input 0 1 1 1 0 Output 0/1 Input 2 2 2 1 6 4 5 -7 Output 1/2 Input 1 1 9 0 -5 2 Output -9/5 Note Let's consider all samples: 1. <image> 2. <image> 3. <image> 4. <image> 5. <image> You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds. Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero). Calculate the minimum number of rubles you have to spend to make the fence great again! You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries. The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) — the number of boards in the fence. The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) — the length of the $i$-th board and the price for increasing it by $1$, respectively. It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$. It is guaranteed that answer to each query will not exceed $10^{18}$. -----Output----- For each query print one integer — the minimum number of rubles you have to spend to make the fence great. -----Example----- Input 3 3 2 4 2 1 3 5 3 2 3 2 10 2 6 4 1 7 3 3 2 6 1000000000 2 Output 2 9 0 -----Note----- In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$. In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$. In the third query the fence is great initially, so you don't need to spend rubles. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Collatz Conjecture states that for any natural number n, if n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. If you repeat the process continuously for n, n will eventually reach 1. For example, if n = 20, the resulting sequence will be: [20, 10, 5, 16, 8, 4, 2, 1] Write a program that will output the length of the Collatz Conjecture for any given n. In the example above, the output would be 8. For more reading see: http://en.wikipedia.org/wiki/Collatz_conjecture Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically, or two squares vertically and one square horizontally. The knights attack each other if one can reach the other in one move. ------ Input : ------ The first line contains the number of test cases T. Each of the next T lines contains two integers N and M. ------ Output : ------ Output T lines, one for each test case, each containing the required answer for the corresponding test case. ------ Constraints : ------ 1 ≤ T ≤ 10000 1 ≤ N,M ≤ 100000 ----- Sample Input 1 ------ 3 2 2 2 3 4 5 ----- Sample Output 1 ------ 12 26 312 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a positive decimal number x. Your task is to convert it to the "simple exponential notation". Let x = a·10^{b}, where 1 ≤ a < 10, then in general case the "simple exponential notation" looks like "aEb". If b equals to zero, the part "Eb" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b. -----Input----- The only line contains the positive decimal number x. The length of the line will not exceed 10^6. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other. -----Output----- Print the only line — the "simple exponential notation" of the given number x. -----Examples----- Input 16 Output 1.6E1 Input 01.23400 Output 1.234 Input .100 Output 1E-1 Input 100. Output 1E2 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 learning a lot about space exploration, a little girl named Ana wants to change the subject. Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whether it's a palindrome or not, but soon she grew tired of this problem, so she came up with a more interesting one and she needs your help to solve it: You are given an array of strings which consist of only small letters of the alphabet. Your task is to find how many palindrome pairs are there in the array. A palindrome pair is a pair of strings such that the following condition holds: at least one permutation of the concatenation of the two strings is a palindrome. In other words, if you have two strings, let's say "aab" and "abcac", and you concatenate them into "aababcac", we have to check if there exists a permutation of this new string such that it is a palindrome (in this case there exists the permutation "aabccbaa"). Two pairs are considered different if the strings are located on different indices. The pair of strings with indices (i,j) is considered the same as the pair (j,i). Input The first line contains a positive integer N (1 ≤ N ≤ 100 000), representing the length of the input array. Eacg of the next N lines contains a string (consisting of lowercase English letters from 'a' to 'z') — an element of the input array. The total number of characters in the input array will be less than 1 000 000. Output Output one number, representing how many palindrome pairs there are in the array. Examples Input 3 aa bb cd Output 1 Input 6 aab abcac dffe ed aa aade Output 6 Note The first example: 1. aa + bb → abba. The second example: 1. aab + abcac = aababcac → aabccbaa 2. aab + aa = aabaa 3. abcac + aa = abcacaa → aacbcaa 4. dffe + ed = dffeed → fdeedf 5. dffe + aade = dffeaade → adfaafde 6. ed + aade = edaade → aeddea Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7. The Little Elephant has K favorite lucky strings A_{1}, A_{2}, ..., A_{K}. He thinks that the lucky string S is good if either |S| ≥ 47 or for some j from 1 to K we have that A_{j} is a substring of S. The Little Elephant has found N lucky strings B_{1}, B_{2}, ..., B_{N} under the pillow. Now he wants to know which of them are good. Help him and find for each i from 1 to N whether the string B_{i} is good or not. Notes. Let S be some lucky string. Then |S| denotes the length of the string S; S[i] (1 ≤ i ≤ |S|) denotes the i^{th} character of S (the numeration of characters starts from 1); The string T of the length M is called a substring of S if for some k from 0 to |S| - M we have T[1] = S[k + 1], T[2] = S[k + 2], ..., T[M] = S[k + M]. ------ Input ------ The first line of the input file contains two integers K and N, the number of favorite lucky strings of the Little Elephant and the number of strings he has found under the pillow. Each of the following K lines contains one favorite lucky string. Namely, j^{th} line among these K lines contains the string A_{j}. Each of the following N lines contains one lucky string that was found under the pillow. Namely, i^{th} line among these N lines contains the string B_{i}. The input file does not contain any whitespaces. ------ Output ------ For each of the N strings that were found under the pillow print Good if it is good, and Bad otherwise. ------ Constraints ------ 1 ≤ K, N ≤ 50 For each string S in the input file we have 1 ≤ |S| ≤ 50. Each string in the input file consists only of the lucky digits 4 and 7. ----- Sample Input 1 ------ 2 4 47 744 7444 447 7774 77777777777777777777777777777777777777777777774 ----- Sample Output 1 ------ Good Good Bad Good ----- explanation 1 ------ The string S = 7444 is good since the favorite string 744 is its substring. The string S = 447 is good since the favorite string 47 is its substring. The string S = 7774 is bad since none of the favorite strings 47 and 744 is a substring of S. The string S = 77777777777777777777777777777777777777777777774 is good since its length is 47. Note, however, that S does not have favorite substrings at all. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. "Search" is an operation to obtain the desired information from a large amount of information. Familiar examples include "finding your own exam number from a large number of exam numbers" when announcing your success, or "finding Taro Aizu's phone number" from your phone book. This search operation is also widely used in the computer field. <image> There are many ways to search. Consider a search method that can be used when the data to be searched is arranged in ascending (or largest) order. Using the magnitude relationship between the value located in the center of the data string arranged in ascending order (or in descending order) and the target value, the first half is set as the search range or the second half is searched from the value located in the center. There is a way to narrow down the search range by deciding whether to make it a range. The procedure is as follows. 1. The entire column of data is the scope of the search. 2. Examine the value located in the center of the search range. 3. If the desired value matches the centered value, the search ends. 4. If it is smaller than the target value and the value located in the center, the first half is the search range, and if it is larger, the second half is the search range and returns to 2. The following is an example of the search method described above. The desired value in this example is 51. Each piece of data is numbered (index), which starts at 0. Step 1: Initially, the search range is the entire number 0-6. Step 2: Find the value in the center of the search range. However, the "centered value" is the value at the position of the number obtained by dividing (the number on the left side + the number on the right side) by 2. In other words, in this case, (0 + 6) ÷ 2 is calculated, and the value (36) at number 3 is the value located in the center. Step 3: Compare the desired value (51) with the centrally located value (36). Step 4: From the result of step 3, the target value is larger than the value located in the center, so the search range is from number 4 (next to the value located in the center) in the latter half. Use the same procedure to check the value located in the center of the search range, and if the target value is smaller than the value located in the center, the first half is the search range, and if it is larger, the second half is the search range. I will make it smaller. (Repeat of steps 2 to 4) The search ends when the target value matches the value located in the center or the search range is reached. | <image> --- | --- Create a program that takes an array of n numbers as input and outputs the number of times the target value is compared with the value located in the center. However, if the number of the value located in the center is not divisible, the value rounded down to the nearest whole number will be used as the number. It is assumed that the given data columns are sorted in ascending order. 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: n a1 a2 :: an k The number of numbers n (1 ≤ n ≤ 100) is given on the first line, and the i-th number ai (1 ≤ ai ≤ 100000, integer) is given on the following n lines. The following line is given the value k (1 ≤ k ≤ 100000) to search for. Output The number of comparisons until the search is completed for each data set is output on one line. Example Input 7 11 15 23 36 51 61 86 51 4 1 2 3 5 4 0 Output 3 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given is a string S. Each character in S is either a digit (0, ..., 9) or ?. Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0. Since the answer can be enormous, print the count modulo 10^9+7. -----Constraints----- - S is a string consisting of digits (0, ..., 9) and ?. - 1 \leq |S| \leq 10^5 -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the number of integers satisfying the condition, modulo 10^9+7. -----Sample Input----- ??2??5 -----Sample Output----- 768 For example, 482305, 002865, and 972665 satisfy the condition. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges. The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i. An edge whose removal disconnects the graph is called a bridge. Find the number of the edges that are bridges among the M edges. -----Notes----- - A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M). - Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M). - An undirected graph is said to be connected when there exists a path between every pair of vertices. -----Constraints----- - 2 \leq N \leq 50 - N-1 \leq M \leq min(N(N−1)⁄2,50) - 1 \leq a_i<b_i \leq N - The given graph does not contain self-loops and double edges. - The given graph is connected. -----Input----- Input is given from Standard Input in the following format: N M a_1 b_1 a_2 b_2 : a_M b_M -----Output----- Print the number of the edges that are bridges among the M edges. -----Sample Input----- 7 7 1 3 2 7 3 4 4 5 4 6 5 6 6 7 -----Sample Output----- 4 The figure below shows the given graph: The edges shown in red are bridges. There are four of them. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Taro went to a toy store to buy a game of life made by Aizu Hobby. Life games are played using a board with squares and roulette. As shown in the figure, the board has one start point and one goal point, which are connected by a single grid. First, the pieces are placed in the square at the starting point, and the pieces are advanced according to the number of pieces that are turned by turning the roulette wheel. Depending on the square, there are event squares where you can earn money or change the position of the pieces by stopping or passing there. The final victory or defeat is determined by the amount of money you have when the piece reaches the goal point. <image> The interesting thing about the game of life at this company is that the size of the roulette eyes, the number of squares to the goal, and the arrangement of the event squares are different for each package. They are written on the case and can be confirmed by reading it. Taro wants to choose the life game that earns the most money, and wants to buy the one with the highest expected value of money. So you decided to help Taro choose a game. Suppose a roulette wheel divides its circumference into X equal parts, each filled with the values ​​V1, V2, ..., VX. The board has squares numbered 0, 1, ..., Y, and they are connected in order. There are Z special squares called event squares in the squares, and when they reach them, they perform special actions. The number of the square of the event square is given by Ni. There are 1 to 3 types (Ei) of event masses, each of which performs the following actions: Type (Ei) | Special Behavior | Value (Ai) Range --- | --- | --- 1 | Advance by the specified value Ai | 1 ~ 10 2 | Get the amount of the specified value Ai | 1 ~ 100 3 | Pay the amount of the specified value Ai | 1 ~ 100 The first amount of money you have is 0 yen, starting from the 0th square and reaching the goal when you reach the Yth square. If you exceed the goal, it is also considered as a goal. There are no events at the start and goal, and multiple events do not overlap in one square. Ignore the events in the squares that are advanced by the event. If the amount of money you have is less than 0 yen, it will be 0 yen. For example, the expected value of money earned in a life game can be calculated as follows. <image> This example shows a life game consisting of three squares: start, event square (get 100 yen), goal, and roulette with 1 or 2. First, when you turn the roulette wheel for the first time, if you get a 1, you will reach the event square and your money will be 100 yen. On the other hand, if you get a 2, you will reach the goal and your money will remain at 0 yen. Both of these occur with a one-half chance. In addition, if you reach the event square the first time, you will turn the roulette wheel the second time, but you will reach the goal no matter what value you get, and you will have 100 yen in each case. As you can see, there are three ways to reach the goal. Focusing on the amount of money you have when you reach the goal, there is one case where the amount is 0 yen and the probability is one half, and there are two cases where the probability is 100 yen and the probability is one quarter. In this case, the expected value of the amount of money you have at the goal is the sum of (the amount of money you have x the probability) for each goal method, and the expected value of this life game is 50 yen. Create a program that inputs roulette information and board information and outputs the expected value of the amount of money you have at the goal. Input A sequence of multiple datasets is given as input. The end of the input is indicated by three zero lines. Each dataset is given in the following format: X Y Z V1 V2 ... VX N1 E1 A1 N2 E2 A2 :: NZ EZ AZ X (1 ≤ X ≤ 4), Vi (1 ≤ Vi ≤ 10), Y (1 ≤ Y ≤ 50), Ni (1 ≤ Ni ≤ Y-1), Z (0 ≤ Z ≤ Y-1), Ei (1 ≤ Ei ≤ 3), Ai (1 ≤ Ai ≤ 100) are given as integers. The number of datasets does not exceed 100. Output For each input dataset, the expected value of the final amount of money is output on one line. Please output the expected value of your money as an integer rounded down to the nearest whole number. Example Input 1 2 0 1 1 2 1 1 1 2 100 1 2 1 2 1 2 100 2 2 1 1 2 1 2 100 4 5 3 1 2 3 4 1 1 2 2 2 100 4 3 60 0 0 0 Output 0 100 0 50 20 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Let's start from some definitions. Strings A and B are called anagrams if it's possible to rearrange the letters of string A using all the original letters exactly once and achieve string B; in other words A and B are permutations of each other. For example, remote and meteor are anagrams, race and race are anagrams as well, while seat and tease aren't anagrams as tease contains an extra 'e'. String A is called a subsequence of string B if A can be obtained from B by removing some (possibly none) characters. For example, cat is a subsequence of scratch, rage is a subsequence of rage, and tame is not a subsequence of steam. String A is lexicographically smaller than string B of the same length if at the first position where A and B differ A contains a letter which is earlier in the alphabet than the corresponding letter in B. Recently, Ann received a set of strings consisting of small Latin letters a..z. 'What can I do with them?' -- she asked herself. 'What if I try to find the longest string which is a subsequence of every string from the set?'. Ann spent a lot of time trying to solve the problem... but all her attempts happened to be unsuccessful. She then decided to allow the sought string to be an anagram of some subsequence of every string from the set. This problem seemed to be easier to Ann, but she was too tired to solve it, so Ann asked for your help. So your task is, given a set of strings, to find the longest non-empty string which satisfies Ann. Moreover, if there are many such strings, choose the lexicographically smallest one. ------ Input ------ The first line of the input file contains one integer N -- the number of strings in the set (1 ≤ N ≤ 100). Each of the next N lines contains a non-empty string consisting only of small Latin letters a..z representing a string from the set. None of the strings contain more than 100 letters. ------ Output ------ Output the longest non-empty string satisfying Ann. If there are several such strings, output the lexicographically smallest one. If there are no such strings, output 'no such string' (quotes for clarity). ----- Sample Input 1 ------ 3 hope elephant path ----- Sample Output 1 ------ hp ----- explanation 1 ------ In the first test case the longest string appears to be two characters long. String 'hp' satisfies the requirements as it's an anagram of 'hp' which is a subsequence of 'hope' and an anagram of 'ph' which is a subsequence of both 'elephant' and 'path'. Note that string 'ph' also satisfies the requirements, but 'hp' is lexicographically smaller. ----- Sample Input 2 ------ 2 wall step ----- Sample Output 2 ------ no such string ----- explanation 2 ------ In this test case there is no such string. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are "a", "o", "u", "i", and "e". Other letters are consonant. In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant "n"; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words "harakiri", "yupie", "man", and "nbo" are Berlanese while the words "horse", "king", "my", and "nz" are not. Help Vitya find out if a word $s$ is Berlanese. -----Input----- The first line of the input contains the string $s$ consisting of $|s|$ ($1\leq |s|\leq 100$) lowercase Latin letters. -----Output----- Print "YES" (without quotes) if there is a vowel after every consonant except "n", otherwise print "NO". You can print each letter in any case (upper or lower). -----Examples----- Input sumimasen Output YES Input ninja Output YES Input codeforces Output NO -----Note----- In the first and second samples, a vowel goes after each consonant except "n", so the word is Berlanese. In the third sample, the consonant "c" goes after the consonant "r", and the consonant "s" stands on the end, so the word is not Berlanese. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem: Given a number m, how many polynomials P with coefficients in set {\{0,1,2,3,4,5,6,7\}} have: P(2)=m? Help Jerry Mao solve the long standing problem! Input The first line contains a single integer t (1 ≤ t ≤ 5⋅ 10^5) - number of test cases. On next line there are t numbers, m_i (1 ≤ m_i ≤ 10^{18}) - meaning that in case i you should solve for number m_i. Output For each test case i, print the answer on separate lines: number of polynomials P as described in statement such that P(2)=m_i, modulo 10^9 + 7. Example Input 2 2 4 Output 2 4 Note In first case, for m=2, polynomials that satisfy the constraint are x and 2. In second case, for m=4, polynomials that satisfy the constraint are x^2, x + 2, 2x and 4. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bob is about to take a hot bath. There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is t1, and the hot water's temperature is t2. The cold water tap can transmit any integer number of water units per second from 0 to x1, inclusive. Similarly, the hot water tap can transmit from 0 to x2 water units per second. If y1 water units per second flow through the first tap and y2 water units per second flow through the second tap, then the resulting bath water temperature will be: <image> Bob wants to open both taps so that the bath water temperature was not less than t0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible. Determine how much each tap should be opened so that Bob was pleased with the result in the end. Input You are given five integers t1, t2, x1, x2 and t0 (1 ≤ t1 ≤ t0 ≤ t2 ≤ 106, 1 ≤ x1, x2 ≤ 106). Output Print two space-separated integers y1 and y2 (0 ≤ y1 ≤ x1, 0 ≤ y2 ≤ x2). Examples Input 10 70 100 100 25 Output 99 33 Input 300 500 1000 1000 300 Output 1000 0 Input 143 456 110 117 273 Output 76 54 Note In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given four integers $a$, $b$, $x$ and $y$. Initially, $a \ge x$ and $b \ge y$. You can do the following operation no more than $n$ times: Choose either $a$ or $b$ and decrease it by one. However, as a result of this operation, value of $a$ cannot become less than $x$, and value of $b$ cannot become less than $y$. Your task is to find the minimum possible product of $a$ and $b$ ($a \cdot b$) you can achieve by applying the given operation no more than $n$ times. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains five integers $a$, $b$, $x$, $y$ and $n$ ($1 \le a, b, x, y, n \le 10^9$). Additional constraint on the input: $a \ge x$ and $b \ge y$ always holds. -----Output----- For each test case, print one integer: the minimum possible product of $a$ and $b$ ($a \cdot b$) you can achieve by applying the given operation no more than $n$ times. -----Example----- Input 7 10 10 8 5 3 12 8 8 7 2 12343 43 4543 39 123212 1000000000 1000000000 1 1 1 1000000000 1000000000 1 1 1000000000 10 11 2 1 5 10 11 9 1 10 Output 70 77 177177 999999999000000000 999999999 55 10 -----Note----- In the first test case of the example, you need to decrease $b$ three times and obtain $10 \cdot 7 = 70$. In the second test case of the example, you need to decrease $a$ one time, $b$ one time and obtain $11 \cdot 7 = 77$. In the sixth test case of the example, you need to decrease $a$ five times and obtain $5 \cdot 11 = 55$. In the seventh test case of the example, you need to decrease $b$ ten times and obtain $10 \cdot 1 = 10$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with t humps, representing them as polylines in the plane. Each polyline consists of n vertices with coordinates (x1, y1), (x2, y2), ..., (xn, yn). The first vertex has a coordinate x1 = 1, the second — x2 = 2, etc. Coordinates yi might be any, but should satisfy the following conditions: * there should be t humps precisely, i.e. such indexes j (2 ≤ j ≤ n - 1), so that yj - 1 < yj > yj + 1, * there should be precisely t - 1 such indexes j (2 ≤ j ≤ n - 1), so that yj - 1 > yj < yj + 1, * no segment of a polyline should be parallel to the Ox-axis, * all yi are integers between 1 and 4. For a series of his drawings of camels with t humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with t humps for a given number n. Input The first line contains a pair of integers n and t (3 ≤ n ≤ 20, 1 ≤ t ≤ 10). Output Output the required amount of camels with t humps. Examples Input 6 1 Output 6 Input 4 2 Output 0 Note In the first sample test sequences of y-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of yi). Read the inputs from stdin solve the problem and write the answer to stdout (do 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 grasshopper is located on the numeric axis at the point with coordinate $x_0$. Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $x$ with a distance $d$ to the left moves the grasshopper to a point with a coordinate $x - d$, while jumping to the right moves him to a point with a coordinate $x + d$. The grasshopper is very fond of positive integers, so for each integer $i$ starting with $1$ the following holds: exactly $i$ minutes after the start he makes a jump with a distance of exactly $i$. So, in the first minutes he jumps by $1$, then by $2$, and so on. The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right. For example, if after $18$ consecutive jumps he arrives at the point with a coordinate $7$, he will jump by a distance of $19$ to the right, since $7$ is an odd number, and will end up at a point $7 + 19 = 26$. Since $26$ is an even number, the next jump the grasshopper will make to the left by a distance of $20$, and it will move him to the point $26 - 20 = 6$. Find exactly which point the grasshopper will be at after exactly $n$ jumps. -----Input----- The first line of input contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Each of the following $t$ lines contains two integers $x_0$ ($-10^{14} \leq x_0 \leq 10^{14}$) and $n$ ($0 \leq n \leq 10^{14}$) — the coordinate of the grasshopper's initial position and the number of jumps. -----Output----- Print exactly $t$ lines. On the $i$-th line print one integer — the answer to the $i$-th test case — the coordinate of the point the grasshopper will be at after making $n$ jumps from the point $x_0$. -----Examples----- Input 9 0 1 0 2 10 10 10 99 177 13 10000000000 987654321 -433494437 87178291199 1 0 -1 1 Output -1 1 11 110 190 9012345679 -87611785637 1 0 -----Note----- The first two test cases in the example correspond to the first two jumps from the point $x_0 = 0$. Since $0$ is an even number, the first jump of length $1$ is made to the left, and the grasshopper ends up at the point $0 - 1 = -1$. Then, since $-1$ is an odd number, a jump of length $2$ is made to the right, bringing the grasshopper to the point with coordinate $-1 + 2 = 1$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi found an undirected connected graph with N vertices and M edges. The vertices are numbered 1 through N. The i-th edge connects vertices a_i and b_i, and has a weight of c_i. He will play Q rounds of a game using this graph. In the i-th round, two vertices S_i and T_i are specified, and he will choose a subset of the edges such that any vertex can be reached from at least one of the vertices S_i or T_i by traversing chosen edges. For each round, find the minimum possible total weight of the edges chosen by Takahashi. Constraints * 1 ≦ N ≦ 4,000 * 1 ≦ M ≦ 400,000 * 1 ≦ Q ≦ 100,000 * 1 ≦ a_i,b_i,S_i,T_i ≦ N * 1 ≦ c_i ≦ 10^{9} * a_i \neq b_i * S_i \neq T_i * The given graph is connected. Input The input is given from Standard Input in the following format: N M a_1 b_1 c_1 a_2 b_2 c_2 : a_M b_M c_M Q S_1 T_1 S_2 T_2 : S_Q T_Q Output Print Q lines. The i-th line should contain the minimum possible total weight of the edges chosen by Takahashi. Examples Input 4 3 1 2 3 2 3 4 3 4 5 2 2 3 1 4 Output 8 7 Input 4 6 1 3 5 4 1 10 2 4 6 3 2 2 3 4 5 2 1 3 1 2 3 Output 8 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it. That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right. Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix. Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists. Input The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively. Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m, - 109 ≤ xi ≤ 109) are given. Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi. Output Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value. If there are multiple valid solutions, output any of them. Examples Input 2 2 6 2 1 2 2 3 1 1 1 3 2 2 2 3 1 2 8 3 2 1 8 Output 8 2 1 8 Input 3 3 2 1 2 3 2 2 5 Output 0 0 0 0 0 5 0 0 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not. You are given an even string S consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S. It is guaranteed that such a non-empty string exists for a given input. -----Constraints----- - 2 \leq |S| \leq 200 - S is an even string consisting of lowercase English letters. - There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the length of the longest even string that can be obtained. -----Sample Input----- abaababaab -----Sample Output----- 6 - abaababaab itself is even, but we need to delete at least one character. - abaababaa is not even. - abaababa is not even. - abaabab is not even. - abaaba is even. Thus, we should print its length, 6. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alex decided to go on a touristic trip over the country. For simplicity let's assume that the country has $n$ cities and $m$ bidirectional roads connecting them. Alex lives in city $s$ and initially located in it. To compare different cities Alex assigned each city a score $w_i$ which is as high as interesting city seems to Alex. Alex believes that his trip will be interesting only if he will not use any road twice in a row. That is if Alex came to city $v$ from city $u$, he may choose as the next city in the trip any city connected with $v$ by the road, except for the city $u$. Your task is to help Alex plan his city in a way that maximizes total score over all cities he visited. Note that for each city its score is counted at most once, even if Alex been there several times during his trip. -----Input----- First line of input contains two integers $n$ and $m$, ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) which are numbers of cities and roads in the country. Second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^9$) which are scores of all cities. The following $m$ lines contain description of the roads. Each of these $m$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$) which are cities connected by this road. It is guaranteed that there is at most one direct road between any two cities, no city is connected to itself by the road and, finally, it is possible to go from any city to any other one using only roads. The last line contains single integer $s$ ($1 \le s \le n$), which is the number of the initial city. -----Output----- Output single integer which is the maximum possible sum of scores of visited cities. -----Examples----- Input 5 7 2 2 8 6 9 1 2 1 3 2 4 3 2 4 5 2 5 1 5 2 Output 27 Input 10 12 1 7 1 9 3 3 6 30 1 10 1 2 1 3 3 5 5 7 2 3 5 4 6 9 4 6 3 7 6 8 9 4 9 10 6 Output 61 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given is a string S of length N-1. Each character in S is `<` or `>`. A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1): * If S_i= `<`: a_i<a_{i+1} * If S_i= `>`: a_i>a_{i+1} Find the minimum possible sum of the elements of a good sequence of N non-negative integers. Constraints * 2 \leq N \leq 5 \times 10^5 * S is a string of length N-1 consisting of `<` and `>`. Input Input is given from Standard Input in the following format: S Output Find the minimum possible sum of the elements of a good sequence of N non-negative integers. Examples Input <>> Output 3 Input <>>><<><<<<<>>>< Output 28 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, "abcba", "a", and "abba" are palindromes, while "abab" and "xy" are not. A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, "abc", "ab", and "c" are substrings of the string "abc", while "ac" and "d" are not. Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string "aaa" is 6 because all its substrings are palindromes, and the palindromic count of the string "abc" is 3 because only its substrings of length 1 are palindromes. You are given a string s. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count. Input The first line contains an integer n (1 ≤ n ≤ 100 000) — the length of string s. The second line contains string s that consists of exactly n lowercase characters of Latin alphabet. Output Print string t, which consists of the same set of characters (and each characters appears exactly the same number of times) as string s. Moreover, t should have the maximum possible value of palindromic count among all such strings strings. If there are multiple such strings, print any of them. Examples Input 5 oolol Output ololo Input 16 gagadbcgghhchbdf Output abccbaghghghgdfd Note In the first example, string "ololo" has 9 palindromic substrings: "o", "l", "o", "l", "o", "olo", "lol", "olo", "ololo". Note, that even though some substrings coincide, they are counted as many times as they appear in the resulting string. In the second example, the palindromic count of string "abccbaghghghgdfd" is 29. Read the inputs from stdin solve the problem and write the answer to stdout (do 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've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gildong is playing a video game called Block Adventure. In Block Adventure, there are $n$ columns of blocks in a row, and the columns are numbered from $1$ to $n$. All blocks have equal heights. The height of the $i$-th column is represented as $h_i$, which is the number of blocks stacked in the $i$-th column. Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the $1$-st column. The goal of the game is to move the character to the top of the $n$-th column. The character also has a bag that can hold infinitely many blocks. When the character is on the top of the $i$-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the $i$-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the $i$-th column; if $i < n$ and $|h_i - h_{i+1}| \le k$, move the character to the top of the $i+1$-st column. $k$ is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the $i$-th column, and the value $h_i$ changes. The character initially has $m$ blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows. The first line of each test case contains three integers $n$, $m$, and $k$ ($1 \le n \le 100$, $0 \le m \le 10^6$, $0 \le k \le 10^6$) — the number of columns in the game, the number of blocks in the character's bag at the beginning, and the non-negative integer $k$ described in the statement. The second line of each test case contains $n$ integers. The $i$-th integer is $h_i$ ($0 \le h_i \le 10^6$), the initial height of the $i$-th column. -----Output----- For each test case, print "YES" if it is possible to win the game. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 5 3 0 1 4 3 5 3 1 2 1 4 7 4 10 0 10 20 10 20 2 5 5 0 11 1 9 9 99 Output YES NO YES NO YES -----Note----- In the first case, Gildong can take one block from the $1$-st column, move to the $2$-nd column, put the block on the $2$-nd column, then move to the $3$-rd column. In the second case, Gildong has to put the block in his bag on the $1$-st column to get to the $2$-nd column. But it is impossible to get to the $3$-rd column because $|h_2 - h_3| = 3 > k$ and there is no way to decrease the gap. In the fifth case, the character is already on the $n$-th column from the start so the game is won instantly. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Nezzar's favorite digit among $1,\ldots,9$ is $d$. He calls a positive integer lucky if $d$ occurs at least once in its decimal representation. Given $q$ integers $a_1,a_2,\ldots,a_q$, for each $1 \le i \le q$ Nezzar would like to know if $a_i$ can be equal to a sum of several (one or more) lucky numbers. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 9$) — the number of test cases. The first line of each test case contains two integers $q$ and $d$ ($1 \le q \le 10^4$, $1 \le d \le 9$). The second line of each test case contains $q$ integers $a_1,a_2,\ldots,a_q$ ($1 \le a_i \le 10^9$). -----Output----- For each integer in each test case, print "YES" in a single line if $a_i$ can be equal to a sum of lucky numbers. Otherwise, print "NO". You can print letters in any case (upper or lower). -----Examples----- Input 2 3 7 24 25 27 10 7 51 52 53 54 55 56 57 58 59 60 Output YES NO YES YES YES NO YES YES YES YES YES YES NO -----Note----- In the first test case, $24 = 17 + 7$, $27$ itself is a lucky number, $25$ cannot be equal to a sum of lucky numbers. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}. ------ Input ------ There are several test cases (fifteen at most), each formed as follows: The first line contains a positive integer H (H ≤ 15). The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}. The input is ended with H = 0. ------ Output ------ For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007. ----- Sample Input 1 ------ 2 1 2 3 3 3 1 5 2 6 4 7 0 ----- Sample Output 1 ------ 3 105 ----- explanation 1 ------ The second test case is constructed as follows: 3 / \ / \ 1 5 / \ / \ 2 6 4 7 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alan decided to get in shape for the summer, so he created a precise workout plan to follow. His plan is to go to a different gym every day during the next N days and lift $X[i]$ grams on day $i$. In order to improve his workout performance at the gym, he can buy exactly one pre-workout drink at the gym he is currently in and it will improve his performance by $A$ grams permanently and immediately. In different gyms these pre-workout drinks can cost different amounts $C[i]$ because of the taste and the gym's location but its permanent workout gains are the same. Before the first day of starting his workout plan, Alan knows he can lift a maximum of $K$ grams. Help Alan spend a minimum total amount of money in order to reach his workout plan. If there is no way for him to complete his workout plan successfully output $-1$. -----Input----- The first one contains two integer numbers, integers $N$ $(1 \leq N \leq 10^5)$ and $K$ $(1 \leq K \leq 10^5)$ – representing number of days in the workout plan and how many grams he can lift before starting his workout plan respectively. The second line contains $N$ integer numbers $X[i]$ $(1 \leq X[i] \leq 10^9)$ separated by a single space representing how many grams Alan wants to lift on day $i$. The third line contains one integer number $A$ $(1 \leq A \leq 10^9)$ representing permanent performance gains from a single drink. The last line contains $N$ integer numbers $C[i]$ $(1 \leq C[i] \leq 10^9)$ , representing cost of performance booster drink in the gym he visits on day $i$. -----Output----- One integer number representing minimal money spent to finish his workout plan. If he cannot finish his workout plan, output -1. -----Examples----- Input 5 10000 10000 30000 30000 40000 20000 20000 5 2 8 3 6 Output 5 Input 5 10000 10000 40000 30000 30000 20000 10000 5 2 8 3 6 Output -1 -----Note----- First example: After buying drinks on days 2 and 4 Alan can finish his workout plan. Second example: Alan cannot lift 40000 grams on day 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a following process. There is a platform with $n$ columns. $1 \times 1$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all of the $n$ columns have at least one square in them, the bottom row is being removed. You will receive $1$ point for this, and all the squares left will fall down one row. You task is to calculate the amount of points you will receive. -----Input----- The first line of input contain 2 integer numbers $n$ and $m$ ($1 \le n, m \le 1000$) — the length of the platform and the number of the squares. The next line contain $m$ integer numbers $c_1, c_2, \dots, c_m$ ($1 \le c_i \le n$) — column in which $i$-th square will appear. -----Output----- Print one integer — the amount of points you will receive. -----Example----- Input 3 9 1 1 2 2 2 3 1 2 3 Output 2 -----Note----- In the sample case the answer will be equal to $2$ because after the appearing of $6$-th square will be removed one row (counts of the squares on the platform will look like $[2~ 3~ 1]$, and after removing one row will be $[1~ 2~ 0]$). After the appearing of $9$-th square counts will be $[2~ 3~ 1]$, and after removing one row it will look like $[1~ 2~ 0]$. So the answer will be equal to $2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarpus has got n candies and m friends (n ≥ m). He wants to make a New Year present with candies to each friend. Polycarpus is planning to present all candies and he wants to do this in the fairest (that is, most equal) manner. He wants to choose such a_{i}, where a_{i} is the number of candies in the i-th friend's present, that the maximum a_{i} differs from the least a_{i} as little as possible. For example, if n is divisible by m, then he is going to present the same number of candies to all his friends, that is, the maximum a_{i} won't differ from the minimum one. -----Input----- The single line of the input contains a pair of space-separated positive integers n, m (1 ≤ n, m ≤ 100;n ≥ m) — the number of candies and the number of Polycarpus's friends. -----Output----- Print the required sequence a_1, a_2, ..., a_{m}, where a_{i} is the number of candies in the i-th friend's present. All numbers a_{i} must be positive integers, total up to n, the maximum one should differ from the minimum one by the smallest possible value. -----Examples----- Input 12 3 Output 4 4 4 Input 15 4 Output 3 4 4 4 Input 18 7 Output 2 2 2 3 3 3 3 -----Note----- Print a_{i} in any order, separate the numbers by spaces. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task Timed Reading is an educational tool used in many schools to improve and advance reading skills. A young elementary student has just finished his very first timed reading exercise. Unfortunately he's not a very good reader yet, so whenever he encountered a word longer than maxLength, he simply skipped it and read on. Help the teacher figure out how many words the boy has read by calculating the number of words in the text he has read, no longer than maxLength. Formally, a word is a substring consisting of English letters, such that characters to the left of the leftmost letter and to the right of the rightmost letter are not letters. # Example For `maxLength = 4` and `text = "The Fox asked the stork, 'How is the soup?'"`, the output should be `7` The boy has read the following words: `"The", "Fox", "the", "How", "is", "the", "soup".` # Input/Output - `[input]` integer `maxLength` A positive integer, the maximum length of the word the boy can read. Constraints: `1 ≤ maxLength ≤ 10.` - `[input]` string `text` A non-empty string of English letters and punctuation marks. - `[output]` an integer The number of words the boy has read. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit). You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it? ------ Input Format ------ The input will contain several test cases (not more than 10). \ Each testcase is a single line with a number n, it is the number written on your coin. ------ Output Format ------ For each test case output a single line, containing the maximum amount of American dollars you can make. ------ Constraints ------ $0 ≤ n ≤ 10^{9}$ ----- Sample Input 1 ------ 12 2 ----- Sample Output 1 ------ 13 2 ----- explanation 1 ------ Test case 1: You can change 12 into 6, 4 and 3, and then change these into $6+4+3 = 13$. \ Test case 2: If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1$ out of them. \ It is better just to change the $2$ coin directly into $2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers. <image> For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8 + 3 + 6 + 7 = 24, sum of its row numbers equals 9 + 5 + 3 + 2 = 19, and 24 > 19. Input The first line contains an integer n (1 ≤ n ≤ 30). Each of the following n lines contain n space-separated integers. The j-th number on the i-th line represents the number on the square that belongs to the j-th column and the i-th row on the board. All number on the board are integers from 1 to 100. Output Print the single number — the number of the winning squares. Examples Input 1 1 Output 0 Input 2 1 2 3 4 Output 2 Input 4 5 7 8 4 9 5 3 2 1 6 6 4 9 5 7 3 Output 6 Note In the first example two upper squares are winning. In the third example three left squares in the both middle rows are winning: 5 7 8 4 9 5 3 2 1 6 6 4 9 5 7 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Theory This section does not need to be read and can be skipped, but it does provide some clarity into the inspiration behind the problem. In music theory, a major scale consists of seven notes, or scale degrees, in order (with tonic listed twice for demonstrative purposes): Tonic, the base of the scale and the note the scale is named after (for example, C is the tonic note of the C major scale) Supertonic, 2 semitones (or one tone) above the tonic Mediant, 2 semitones above the supertonic and 4 above the tonic Subdominant, 1 semitone above the median and 5 above the tonic Dominant, 2 semitones above the subdominant and 7 above the tonic Submediant, 2 semitones above the dominant and 9 above the tonic Leading tone, 2 semitones above the mediant and 11 above the tonic Tonic (again!), 1 semitone above the leading tone and 12 semitones (or one octave) above the tonic An octave is an interval of 12 semitones, and the pitch class of a note consists of any note that is some integer number of octaves apart from that note. Notes in the same pitch class sound different but share similar properties. If a note is in a major scale, then any note in that note's pitch class is also in that major scale. Problem Using integers to represent notes, the major scale of an integer note will be an array (or list) of integers that follows the major scale pattern note, note + 2, note + 4, note + 5, note + 7, note + 9, note + 11. For example, the array of integers [1, 3, 5, 6, 8, 10, 12] is the major scale of 1. Secondly, the pitch class of a note will be the set of all integers some multiple of 12 above or below the note. For example, 1, 13, and 25 are all in the same pitch class. Thirdly, an integer note1 is considered to be in the key of an integer note2 if note1, or some integer in note1's pitch class, is in the major scale of note2. More mathematically, an integer note1 is in the key of an integer note2 if there exists an integer i such that note1 + i × 12 is in the major scale of note2. For example, 22 is in the key of of 1 because, even though 22 is not in 1's major scale ([1, 3, 5, 6, 8, 10, 12]), 10 is and is also a multiple of 12 away from 22. Likewise, -21 is also in the key of 1 because -21 + (2 × 12) = 3 and 3 is present in the major scale of 1. An array is in the key of an integer if all its elements are in the key of the integer. Your job is to create a function is_tune that will return whether or not an array (or list) of integers is a tune. An array will be considered a tune if there exists a single integer note all the integers in the array are in the key of. The function will accept an array of integers as its parameter and return True if the array is a tune or False otherwise. Empty and null arrays are not considered to be tunes. Additionally, the function should not change the input array. Examples ```python is_tune([1, 3, 6, 8, 10, 12]) # Returns True, all the elements are in the major scale # of 1 ([1, 3, 5, 6, 8, 10, 12]) and so are in the key of 1. is_tune([1, 3, 6, 8, 10, 12, 13, 15]) # Returns True, 14 and 15 are also in the key of 1 as # they are in the major scale of 13 which is in the pitch class of 1 (13 = 1 + 12 * 1). is_tune([1, 6, 3]) # Returns True, arrays don't need to be sorted. is_tune([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # Returns False, this array is not in the # key of any integer. is_tune([2, 4, 7, 9, 11, 13]) # Returns True, the array is in the key of 2 (the arrays # don't need to be in the key of 1, just some integer) ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have $2n$ integers $1, 2, \dots, 2n$. You have to redistribute these $2n$ elements into $n$ pairs. After that, you choose $x$ pairs and take minimum elements from them, and from the other $n - x$ pairs, you take maximum elements. Your goal is to obtain the set of numbers $\{b_1, b_2, \dots, b_n\}$ as the result of taking elements from the pairs. What is the number of different $x$-s ($0 \le x \le n$) such that it's possible to obtain the set $b$ if for each $x$ you can choose how to distribute numbers into pairs and from which $x$ pairs choose minimum elements? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_1 < b_2 < \dots < b_n \le 2n$) — the set you'd like to get. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one number — the number of different $x$-s such that it's possible to obtain the set $b$. -----Examples----- Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 -----Note----- In the first test case, $x = 1$ is the only option: you have one pair $(1, 2)$ and choose the minimum from this pair. In the second test case, there are three possible $x$-s. If $x = 1$, then you can form the following pairs: $(1, 6)$, $(2, 4)$, $(3, 5)$, $(7, 9)$, $(8, 10)$. You can take minimum from $(1, 6)$ (equal to $1$) and the maximum elements from all other pairs to get set $b$. If $x = 2$, you can form pairs $(1, 2)$, $(3, 4)$, $(5, 6)$, $(7, 9)$, $(8, 10)$ and take the minimum elements from $(1, 2)$, $(5, 6)$ and the maximum elements from the other pairs. If $x = 3$, you can form pairs $(1, 3)$, $(4, 6)$, $(5, 7)$, $(2, 9)$, $(8, 10)$ and take the minimum elements from $(1, 3)$, $(4, 6)$, $(5, 7)$. In the third test case, $x = 0$ is the only option: you can form pairs $(1, 3)$, $(2, 4)$ and take the maximum elements from both of them. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i). A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point. At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs. -----Constraints----- - All input values are integers. - 1 \leq N \leq 100 - 0 \leq a_i, b_i, c_i, d_i < 2N - a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different. - b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different. -----Input----- Input is given from Standard Input in the following format: N a_1 b_1 a_2 b_2 : a_N b_N c_1 d_1 c_2 d_2 : c_N d_N -----Output----- Print the maximum number of friendly pairs. -----Sample Input----- 3 2 0 3 1 1 3 4 2 0 4 5 5 -----Sample Output----- 2 For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N. Constraints * 1\leq N \leq 10^{16} * N is an integer. Input Input is given from Standard Input in the following format: N Output Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N. Examples Input 100 Output 18 Input 9995 Output 35 Input 3141592653589793 Output 137 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest. The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer. -----Input----- The first lines contain four integers n, m, s and t (2 ≤ n ≤ 10^5; 1 ≤ m ≤ 10^5; 1 ≤ s, t ≤ n) — the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s ≠ t). Next m lines contain the roads. Each road is given as a group of three integers a_{i}, b_{i}, l_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}; 1 ≤ l_{i} ≤ 10^6) — the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city a_{i} to city b_{i}. The cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from s to t along the roads. -----Output----- Print m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input). If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes). Otherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing. If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes). -----Examples----- Input 6 7 1 6 1 2 2 1 3 10 2 3 7 2 4 8 3 5 3 4 5 2 5 6 1 Output YES CAN 2 CAN 1 CAN 1 CAN 1 CAN 1 YES Input 3 3 1 3 1 2 10 2 3 10 1 3 100 Output YES YES CAN 81 Input 2 2 1 2 1 2 1 1 2 2 Output YES NO -----Note----- The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing. In the first sample president initially may choose one of the two following ways for a ride: 1 → 2 → 4 → 5 → 6 or 1 → 2 → 3 → 5 → 6. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 array of strings. All strings contains similar _letters_ except one. Try to find it! ```python find_uniq([ 'Aa', 'aaa', 'aaaaa', 'BbBb', 'Aaaa', 'AaAaAa', 'a' ]) # => 'BbBb' find_uniq([ 'abc', 'acb', 'bac', 'foo', 'bca', 'cab', 'cba' ]) # => 'foo' ``` Strings may contain spaces. Spaces is not significant, only non-spaces symbols matters. E.g. string that contains only spaces is like empty string. It’s guaranteed that array contains more than 3 strings. This is the second kata in series: 1. [Find the unique number](https://www.codewars.com/kata/585d7d5adb20cf33cb000235) 2. Find the unique string (this kata) 3. [Find The Unique](https://www.codewars.com/kata/5862e0db4f7ab47bed0000e5) Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n. It is known that each of the n integers 1,...,n appears at least once in this sequence. For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7. -----Notes----- - If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence. - A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not. -----Constraints----- - 1 \leq n \leq 10^5 - 1 \leq a_i \leq n - Each of the integers 1,...,n appears in the sequence. - n and a_i are integers. -----Input----- Input is given from Standard Input in the following format: n a_1 a_2 ... a_{n+1} -----Output----- Print n+1 lines. The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7. -----Sample Input----- 3 1 2 1 3 -----Sample Output----- 3 5 4 1 There are three subsequences with length 1: 1 and 2 and 3. There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3. There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3. There is one subsequence with length 4: 1,2,1,3. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A tetromino is a figure formed by joining four squares edge to edge. We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively: a60bcb8e9e8f22e3af51049eda063392.png Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession are a_I, a_O, a_T, a_J, a_L, a_S and a_Z, respectively. Snuke will join K of his tetrominos to form a rectangle that is two squares tall and 2K squares wide. Here, the following rules must be followed: * When placing each tetromino, rotation is allowed, but reflection is not. * Each square in the rectangle must be covered by exactly one tetromino. * No part of each tetromino may be outside the rectangle. Snuke wants to form as large a rectangle as possible. Find the maximum possible value of K. Constraints * 0≤a_I,a_O,a_T,a_J,a_L,a_S,a_Z≤10^9 * a_I+a_O+a_T+a_J+a_L+a_S+a_Z≥1 Input The input is given from Standard Input in the following format: a_I a_O a_T a_J a_L a_S a_Z Output Print the maximum possible value of K. If no rectangle can be formed, print `0`. Examples Input 2 1 1 0 0 0 0 Output 3 Input 0 0 10 0 0 0 0 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. YouKn0wWho has an integer sequence $a_1, a_2, \ldots a_n$. Now he will split the sequence $a$ into one or more consecutive subarrays so that each element of $a$ belongs to exactly one subarray. Let $k$ be the number of resulting subarrays, and $h_1, h_2, \ldots, h_k$ be the lengths of the longest increasing subsequences of corresponding subarrays. For example, if we split $[2, 5, 3, 1, 4, 3, 2, 2, 5, 1]$ into $[2, 5, 3, 1, 4]$, $[3, 2, 2, 5]$, $[1]$, then $h = [3, 2, 1]$. YouKn0wWho wonders if it is possible to split the sequence $a$ in such a way that the bitwise XOR of $h_1, h_2, \ldots, h_k$ is equal to $0$. You have to tell whether it is possible. The longest increasing subsequence (LIS) of a sequence $b_1, b_2, \ldots, b_m$ is the longest sequence of valid indices $i_1, i_2, \ldots, i_k$ such that $i_1 \lt i_2 \lt \ldots \lt i_k$ and $b_{i_1} \lt b_{i_2} \lt \ldots \lt b_{i_k}$. For example, the LIS of $[2, 5, 3, 3, 5]$ is $[2, 3, 5]$, which has length $3$. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, print "YES" (without quotes) if it is possible to split into subarrays in the desired way, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower). -----Examples----- Input 4 7 1 3 4 2 2 1 5 3 1 3 4 5 1 3 2 4 2 4 4 3 2 1 Output YES NO YES YES -----Note----- In the first test case, YouKn0wWho can split the sequence in the following way: $[1, 3, 4]$, $[2, 2]$, $[1, 5]$. This way, the LIS lengths are $h = [3, 1, 2]$, and the bitwise XOR of the LIS lengths is $3 \oplus 1 \oplus 2 = 0$. In the second test case, it can be shown that it is impossible to split the sequence into subarrays that will satisfy the condition. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have a large rectangular board which is divided into $n \times m$ cells (the board has $n$ rows and $m$ columns). Each cell is either white or black. You paint each white cell either red or blue. Obviously, the number of different ways to paint them is $2^w$, where $w$ is the number of white cells. After painting the white cells of the board, you want to place the maximum number of dominoes on it, according to the following rules: each domino covers two adjacent cells; each cell is covered by at most one domino; if a domino is placed horizontally (it covers two adjacent cells in one of the rows), it should cover only red cells; if a domino is placed vertically (it covers two adjacent cells in one of the columns), it should cover only blue cells. Let the value of the board be the maximum number of dominoes you can place. Calculate the sum of values of the board over all $2^w$ possible ways to paint it. Since it can be huge, print it modulo $998\,244\,353$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$; $nm \le 3 \cdot 10^5$) — the number of rows and columns, respectively. Then $n$ lines follow, each line contains a string of $m$ characters. The $j$-th character in the $i$-th string is * if the $j$-th cell in the $i$-th row is black; otherwise, that character is o. -----Output----- Print one integer — the sum of values of the board over all $2^w$ possible ways to paint it, taken modulo $998\,244\,353$. -----Examples----- Input 3 4 **oo oo*o **oo Output 144 Input 3 4 **oo oo** **oo Output 48 Input 2 2 oo o* Output 4 Input 1 4 oooo Output 9 -----Note----- None Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array $d_1, d_2, \dots, d_n$ consisting of $n$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $sum_1$, the sum of elements of the second part be $sum_2$ and the sum of elements of the third part be $sum_3$. Among all possible ways to split the array you have to choose a way such that $sum_1 = sum_3$ and $sum_1$ is maximum possible. More formally, if the first part of the array contains $a$ elements, the second part of the array contains $b$ elements and the third part contains $c$ elements, then: $$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$ $$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$ $$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$ The sum of an empty array is $0$. Your task is to find a way to split the array such that $sum_1 = sum_3$ and $sum_1$ is maximum possible. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in the array $d$. The second line of the input contains $n$ integers $d_1, d_2, \dots, d_n$ ($1 \le d_i \le 10^9$) — the elements of the array $d$. -----Output----- Print a single integer — the maximum possible value of $sum_1$, considering that the condition $sum_1 = sum_3$ must be met. Obviously, at least one valid way to split the array exists (use $a=c=0$ and $b=n$). -----Examples----- Input 5 1 3 1 1 4 Output 5 Input 5 1 3 2 1 4 Output 4 Input 3 4 1 2 Output 0 -----Note----- In the first example there is only one possible splitting which maximizes $sum_1$: $[1, 3, 1], [~], [1, 4]$. In the second example the only way to have $sum_1=4$ is: $[1, 3], [2, 1], [4]$. In the third example there is only one way to split the array: $[~], [4, 1, 2], [~]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type a_{i}. Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type. Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. -----Input----- The first line contains integer n (1 ≤ n ≤ 3·10^5) — the number of pearls in a row. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) – the type of the i-th pearl. -----Output----- On the first line print integer k — the maximal number of segments in a partition of the row. Each of the next k lines should contain two integers l_{j}, r_{j} (1 ≤ l_{j} ≤ r_{j} ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment. Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type. If there are several optimal solutions print any of them. You can print the segments in any order. If there are no correct partitions of the row print the number "-1". -----Examples----- Input 5 1 2 3 4 1 Output 1 1 5 Input 5 1 2 3 4 5 Output -1 Input 7 1 2 1 3 1 2 1 Output 2 1 3 4 7 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A $\rightarrow$ BC B $\rightarrow$ AC C $\rightarrow$ AB AAA $\rightarrow$ empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source. -----Input----- The first line contains a string S (1 ≤ |S| ≤ 10^5). The second line contains a string T (1 ≤ |T| ≤ 10^5), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 10^5). The following Q lines describe queries. The i-th of these lines contains four space separated integers a_{i}, b_{i}, c_{i}, d_{i}. These represent the i-th query: is it possible to create T[c_{i}..d_{i}] from S[a_{i}..b_{i}] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|. -----Output----- Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise. -----Example----- Input AABCCBAAB ABCB 5 1 3 1 2 2 2 2 4 7 9 1 1 3 4 2 3 4 5 1 3 Output 10011 -----Note----- In the first query we can achieve the result, for instance, by using transitions $A A B \rightarrow A A A C \rightarrow \operatorname{AAA} A B \rightarrow A B$. The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Write a function with the signature shown below: ```python def is_int_array(arr): return True ``` * returns `true / True` if every element in an array is an integer or a float with no decimals. * returns `true / True` if array is empty. * returns `false / False` for every other input. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 people are arranged in a row from left to right. You are given a string S of length N consisting of 0 and 1, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. -----Constraints----- - N is an integer satisfying 1 \leq N \leq 10^5. - K is an integer satisfying 1 \leq K \leq 10^5. - The length of the string S is N. - Each character of the string S is 0 or 1. -----Input----- Input is given from Standard Input in the following format: N K S -----Output----- Print the maximum possible number of consecutive people standing on hands after at most K directions. -----Sample Input----- 5 1 00010 -----Sample Output----- 4 We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction: - Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string. Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be? See notes for definition of a tandem repeat. -----Input----- The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters. -----Output----- Print a single number — the maximum length of the tandem repeat that could have occurred in the new string. -----Examples----- Input aaba 2 Output 6 Input aaabbbb 2 Output 6 Input abracadabra 10 Output 20 -----Note----- A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n. In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array of integers $a_1,a_2,\ldots,a_n$. Find the maximum possible value of $a_ia_ja_ka_la_t$ among all five indices $(i, j, k, l, t)$ ($i<j<k<l<t$). -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1\le t\le 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($5\le n\le 10^5$) — the size of the array. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-3\times 10^3\le a_i\le 3\times 10^3$) — given array. It's guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each test case, print one integer — the answer to the problem. -----Example----- Input 4 5 -1 -2 -3 -4 -5 6 -1 -2 -3 1 2 -1 6 -1 0 0 0 -1 -1 6 -9 -7 -5 -3 -2 1 Output -120 12 0 945 -----Note----- In the first test case, choosing $a_1,a_2,a_3,a_4,a_5$ is a best choice: $(-1)\cdot (-2) \cdot (-3)\cdot (-4)\cdot (-5)=-120$. In the second test case, choosing $a_1,a_2,a_3,a_5,a_6$ is a best choice: $(-1)\cdot (-2) \cdot (-3)\cdot 2\cdot (-1)=12$. In the third test case, choosing $a_1,a_2,a_3,a_4,a_5$ is a best choice: $(-1)\cdot 0\cdot 0\cdot 0\cdot (-1)=0$. In the fourth test case, choosing $a_1,a_2,a_3,a_4,a_6$ is a best choice: $(-9)\cdot (-7) \cdot (-5)\cdot (-3)\cdot 1=945$. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, a boy who hates any inefficiencies, pays coins so that the number of coins to be returned as change is minimized in order to do smoothly when he buys something. One day, however, he doubt if this way is really efficient. When he pays more number of coins, a clerk consumes longer time to find the total value. Maybe he should pay with least possible number of coins. Thinking for a while, he has decided to take the middle course. So he tries to minimize total number of paid coins and returned coins as change. Now he is going to buy a product of P yen having several coins. Since he is not good at calculation, please write a program that computes the minimal number of coins. You may assume following things: * There are 6 kinds of coins, 1 yen, 5 yen, 10 yen, 50 yen, 100 yen and 500 yen. * The total value of coins he has is at least P yen. * A clerk will return the change with least number of coins. Constraints * Judge data contains at most 100 data sets. * 0 ≤ Ni ≤ 1000 Input Input file contains several data sets. One data set has following format: P N1 N5 N10 N50 N100 N500 Ni is an integer and is the number of coins of i yen that he have. The end of input is denoted by a case where P = 0. You should output nothing for this data set. Output Output total number of coins that are paid and are returned. Example Input 123 3 0 2 0 1 1 999 9 9 9 9 9 9 0 0 0 0 0 0 0 Output 6 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n_1 washing machines, n_2 drying machines and n_3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine. It takes t_1 minutes to wash one piece of laundry in a washing machine, t_2 minutes to dry it in a drying machine, and t_3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have. -----Input----- The only line of the input contains seven integers: k, n_1, n_2, n_3, t_1, t_2, t_3 (1 ≤ k ≤ 10^4; 1 ≤ n_1, n_2, n_3, t_1, t_2, t_3 ≤ 1000). -----Output----- Print one integer — smallest number of minutes to do all your laundry. -----Examples----- Input 1 1 1 1 5 5 5 Output 15 Input 8 4 3 2 10 5 2 Output 32 -----Note----- In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it. In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. For positive integer x let define function F(x) = 1 * (1! + x) + 2 * (2! + x) + .. + x * (x! + x). "k!" means factorial: k! = 1 * 2 * .. * k Chef wants to calculate F(p_{1}) + F(p_{2}) + ... + F(p_{n}). As answer could be large, help him, calculate value modulo m. ------ Input ------ First line contains two integers n and m. Next line contains n space separated integers p_{i}. ------ Output ------ Output a single line containing one integer --- calculated value modulo m. ------ Constraints ------ $1 ≤ n_{} ≤ 10^{5}$ $1 ≤ p_{i} ≤ 10^{18}$ $1 ≤ m ≤ 10^{7}$ ------ Subtasks ------ $Subtask #1: 1 ≤ p_{i} ≤ 6 (10 points)$ $Subtask #2: 1 ≤ p_{i} ≤ 7 * 10^{3} (25 points)$ $Subtask #3: m_{}^{} - prime number (25 points)$ $Subtask #4: _{}^{}original constraints (40 points)$ ----- Sample Input 1 ------ 5 7 1 2 3 4 5 ----- Sample Output 1 ------ 6 ----- explanation 1 ------ F(1) = 1 * (1! + 1) = 2 F(2) = 1 * (1! + 2) + 2 * (2! + 2) = 3 + 8 = 11 F(3) = 1 * (1! + 3) + 2 * (2! + 3) + 3 * (3! + 3) = 4 + 10 + 27 = 41 F(4) = 1 * (1! + 4) + 2 * (2! + 4) + 3 * (3! + 4) + 4 * (4! + 4) = 5 + 12 + 30 + 112 = 159 F(5) = 1 * (1! + 5) + 2 * (2! + 5) + 3 * (3! + 5) + 4 * (4! + 5) + 5 * (5! + 5) = 794 F(1) + F(2) + F(3) + F(4) + F(5) = 2 + 11 + 41 + 159 + 794 = 1007 1007 modulo 7 = 6 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round. You are a new applicant for his company. Boboniu will test you with the following chess question: Consider a $n\times m$ grid (rows are numbered from $1$ to $n$, and columns are numbered from $1$ to $m$). You have a chess piece, and it stands at some cell $(S_x,S_y)$ which is not on the border (i.e. $2 \le S_x \le n-1$ and $2 \le S_y \le m-1$). From the cell $(x,y)$, you can move your chess piece to $(x,y')$ ($1\le y'\le m, y' \neq y$) or $(x',y)$ ($1\le x'\le n, x'\neq x$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column. Your goal is to visit each cell exactly once. Can you find a solution? Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point. -----Input----- The only line of the input contains four integers $n$, $m$, $S_x$ and $S_y$ ($3\le n,m\le 100$, $2 \le S_x \le n-1$, $2 \le S_y \le m-1$) — the number of rows, the number of columns, and the initial position of your chess piece, respectively. -----Output----- You should print $n\cdot m$ lines. The $i$-th line should contain two integers $x_i$ and $y_i$ ($1 \leq x_i \leq n$, $1 \leq y_i \leq m$), denoting the $i$-th cell that you visited. You should print exactly $nm$ pairs $(x_i, y_i)$, they should cover all possible pairs $(x_i, y_i)$, such that $1 \leq x_i \leq n$, $1 \leq y_i \leq m$. We can show that under these constraints there always exists a solution. If there are multiple answers, print any. -----Examples----- Input 3 3 2 2 Output 2 2 1 2 1 3 2 3 3 3 3 2 3 1 2 1 1 1 Input 3 4 2 2 Output 2 2 2 1 2 3 2 4 1 4 3 4 3 3 3 2 3 1 1 1 1 2 1 3 -----Note----- Possible routes for two examples: [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Multiplication of Big Integers Given two integers $A$ and $B$, compute the product, $A \times B$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the product in a line. Constraints * $-1 \times 10^{1000} \leq A, B \leq 10^{1000}$ Sample Input 1 5 8 Sample Output 1 40 Sample Input 2 100 25 Sample Output 2 2500 Sample Input 3 -1 0 Sample Output 3 0 Sample Input 4 12 -3 Sample Output 4 -36 Example Input 5 8 Output 40 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have managed to intercept an important message and you are trying to read it. You realise that the message has been encoded and can be decoded by switching each letter with a corresponding letter. You also notice that each letter is paired with the letter that it coincides with when the alphabet is reversed. For example: "a" is encoded with "z", "b" with "y", "c" with "x", etc You read the first sentence: ``` "r slkv mlylwb wvxlwvh gsrh nvhhztv" ``` After a few minutes you manage to decode it: ``` "i hope nobody decodes this message" ``` Create a function that will instantly decode any of these messages You can assume no punctuation or capitals, only lower case letters, but remember spaces! Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Railroad Trip There are N cities in the JOI country, numbered 1, 2, ..., and N, respectively. In addition, there are N − 1 railroads, which are numbered 1, 2, ..., and N − 1, respectively. The railroad i (1 ≤ i ≤ N − 1) connects the city i and the city i + 1 in both directions. There are two ways to get on the JOI railway, one is to use a paper ticket and the other is to use an IC card. * The fare for boarding the railway i with a paper ticket is Ai yen. * The fare for boarding the railway i with an IC card is Bi Yen. However, in order to board the railway i with an IC card, it is necessary to purchase an IC card that can be used with the railway i in advance. It costs Ci yen to purchase an IC card that can be used on the railway i. Once purchased, the IC card can be used any number of times. Since the IC card makes it easier to process the amount, the fare for boarding with an IC card is cheaper than the fare for boarding with a paper ticket. That is, for i = 1, 2, ..., N − 1, Ai> Bi holds. Since the specifications of IC cards are all different for each railroad, the IC card that can be used on railroad i cannot be used on other railroads for any i. You decide to travel all over the JOI country. We plan to start from the city P1 and visit the cities in the order of P2, P3, ..., PM. The trip consists of an M-1 day journey. On day j (1 ≤ j ≤ M − 1), travel by rail from city Pj to city Pj + 1. At this time, it may move by connecting several railroads. You may also visit the same city more than once. The railroads in JOI are so fast that you can travel from any city to any city in a day. You currently do not have an IC card for any railroad. You want to purchase some railroad IC cards in advance and minimize the cost of this trip, that is, the sum of the IC card purchase cost and the railroad fare you boarded. Task Given the number of cities in the JOI country, the itinerary of the trip, and the fare and IC card price for each railroad in the JOI country. At this time, create a program to find the minimum amount of money required for the trip. input Read the following data from standard input. * On the first line, integers N and M are written with blanks as delimiters. Each of these represents that there are N cities in the JOI country and the trip consists of an M-1 day journey. * On the second line, M integers P1, P2, ..., PM are written with a space as a delimiter. These represent the rail movement from city Pj to city Pj + 1 on day j (1 ≤ j ≤ M − 1). * On the i-th line (1 ≤ i ≤ N − 1) of the following N − 1 lines, three integers Ai, Bi, and Ci are written separated by blanks. These indicate that the fare when boarding the railway i with a paper ticket is Ai yen, the fare when boarding with an IC card is Bi yen, and the amount of the IC card that can be used on the railway i is Ci yen. output On the standard output, output an integer that represents the minimum value of the travel amount in yen on one line. Limits All input data satisfy the following conditions. * 2 ≤ N ≤ 100 000. * 2 ≤ M ≤ 100 000. * 1 ≤ Bi <Ai ≤ 100 000 (1 ≤ i ≤ N − 1). * 1 ≤ Ci ≤ 100 000 (1 ≤ i ≤ N − 1). * 1 ≤ Pj ≤ N (1 ≤ j ≤ M). * Pj ≠ Pj + 1 (1 ≤ j ≤ M − 1). Input / output example Input example 1 4 4 1 3 2 4 120 90 100 110 50 80 250 70 130 Output example 1 550 In this case, the method to minimize the cost of travel is as follows. * Purchase IC cards for Railroad 2 and Railroad 3. This costs 80 + 130 = 210 yen. * On the first day, move from city 1 to city 2 using a paper ticket, and then move from city 2 to city 3 using an IC card. This costs 120 + 50 = 170 yen. * On the second day, move from city 3 to city 2 using an IC card. This costs 50 yen. * On the third day, move from city 2 to city 3 using an IC card, and then move from city 3 to city 4 using an IC card. This costs 50 + 70 = 120 yen. When moving in this way, the total amount of money spent on the trip is 210 + 170 + 50 + 120 = 550 yen. Since this is the minimum, it outputs 550. Input example 2 8 5 7 5 3 5 4 12 5 8 16 2 1 3 1 5 17 12 17 19 7 5 12 2 19 4 1 3 Output example 2 81 The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics. Example Input 4 4 1 3 2 4 120 90 100 110 50 80 250 70 130 Output 550 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In this Kata, you will be given a string of numbers in sequence and your task will be to return the missing number. If there is no number missing or there is an error in the sequence, return `-1`. For example: ```Haskell missing("123567") = 4 missing("899091939495") = 92 missing("9899101102") = 100 missing("599600601602") = -1 -- no number missing missing("8990919395") = -1 -- error in sequence. Both 92 and 94 missing. ``` The sequence will always be in ascending order. More examples in the test cases. Good luck! Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration $\frac{a_{i}}{1000}$. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration $\frac{n}{1000}$. The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass. Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well. Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration $\frac{n}{1000}$. Assume that the friends have unlimited amount of each Coke type. -----Input----- The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 10^6) — carbon dioxide concentration the friends want and the number of Coke types. The second line contains k integers a_1, a_2, ..., a_{k} (0 ≤ a_{i} ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration. -----Output----- Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration $\frac{n}{1000}$, or -1 if it is impossible. -----Examples----- Input 400 4 100 300 450 500 Output 2 Input 50 2 100 25 Output 3 -----Note----- In the first sample case, we can achieve concentration $\frac{400}{1000}$ using one liter of Coke of types $\frac{300}{1000}$ and $\frac{500}{1000}$: $\frac{300 + 500}{1000 + 1000} = \frac{400}{1000}$. In the second case, we can achieve concentration $\frac{50}{1000}$ using two liters of $\frac{25}{1000}$ type and one liter of $\frac{100}{1000}$ type: $\frac{25 + 25 + 100}{3 \cdot 1000} = \frac{50}{1000}$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Genos needs your help. He was asked to solve the following programming problem by Saitama: The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as $\sum_{i = 1}^{|s|}|s_{i} - t_{i}|$, where s_{i} is the i-th character of s and t_{i} is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2. Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|. -----Input----- The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000). The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000). Both strings are guaranteed to consist of characters '0' and '1' only. -----Output----- Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|. -----Examples----- Input 01 00111 Output 3 Input 0011 0110 Output 2 -----Note----- For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is 1 + 0 + 1 + 1 = 3. The second sample case is described in the statement. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon. Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another. It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell. [Image] -----Input----- The only line of the input contains one integer n (0 ≤ n ≤ 10^9). -----Output----- Output one integer — the number of hexagons situated not farther than n cells away from a given cell. -----Examples----- Input 2 Output 19 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. <image> Arrange integers (0 or more and 99 or less) in a rhombus as illustrated in Fig. 1. Create a program that reads the data representing the rhombus and outputs the maximum value of the sum of the integers that pass when starting from the top and proceeding to the bottom according to the following rules. * At each step, you can proceed to the lower left diagonal or the lower right diagonal. For example, in the example of Fig. 1, as shown in Fig. 2, when 7,3,8,7,5,7,8,3,7 is selected and passed, the sum is the maximum 55 (7 + 3 + 8). + 7 + 5 + 7 + 8 + 3 + 7 = 55). Input As shown in the input example, a comma-separated sequence of integers is given to the diamond. Each line does not contain whitespace. The input example corresponds to Fig. 1. There are less than 100 rows of data. Output Outputs the maximum value of the sum of integers that pass according to the rules on one line. Example Input 7 3,8 8,1,0 2,7,4,4 4,5,2,6,5 2,7,4,4 8,1,0 3,8 7 Output 55 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 bracket sequence is a string, containing only characters "(", ")", "[" and "]". A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "([])" are correct (the resulting expressions are: "(1)+[1]", "([1+1]+1)"), and "](" and "[" are not. The empty string is a correct bracket sequence by definition. A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is the string slsl + 1... sr. The empty string is a substring of any string by definition. You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible. Input The first and the only line contains the bracket sequence as a string, consisting only of characters "(", ")", "[" and "]". It is guaranteed that the string is non-empty and its length doesn't exceed 105 characters. Output In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them. Examples Input ([]) Output 1 ([]) Input ((( Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. problem There are the following games. N characters are lined up in a vertical row. The color of these characters is red, blue, or yellow, and in the initial state, four or more characters of the same color are not lined up in a row. The player can select a character at a certain position and change it to another color. By this operation, if four or more characters of the same color are lined up in a row, those characters will disappear. When four or more characters of the same color are lined up in a row due to the disappearance of the characters, those characters also disappear, and this chain continues until there are no more places where four or more characters of the same color are lined up in a row. .. The purpose of this game is to reduce the number of characters remaining without disappearing. For example, if the color of the sixth character from the top is changed from yellow to blue in the state at the left end of the figure below, five blue characters will disappear in a row, and finally three characters will remain without disappearing. <image> Given the color sequence of N characters in the initial state, create a program that finds the minimum value M of the number of characters that remain without disappearing when the color of the character is changed in only one place. input The input consists of multiple datasets. Each dataset is given in the following format. The first line consists of only the number of characters N (1 ≤ N ≤ 10000). The following N lines contain one of 1, 2, and 3 integers, and the i + 1st line (1 ≤ i ≤ N) represents the color of the i-th character from the top in the initial state (1). Is red, 2 is blue, and 3 is yellow). When N is 0, it indicates the end of input. The number of datasets does not exceed 5. output For each dataset, output the minimum value M of the number of characters remaining without disappearing on one line. Examples Input 12 3 2 1 1 2 3 2 2 2 1 1 3 12 3 2 1 1 2 3 2 1 3 2 1 3 0 Output 3 12 Input None Output None Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are n sheets of square paper of the same size. Align the bottom of these papers horizontally and arrange them in several rows. However, adjacent rows must be arranged so that the left side is not lower than the right side. For example, n When = 5, the following 7 ways of arranging are possible. <image> We will represent these by the number of square columns in each column. For example, when n = 5, each of them will be represented. (5) (4, 1) (3, 2) (3, 1, 1) (2, 2, 1) (2, 1, 1, 1) (1, 1, 1, 1, 1) It is expressed as. When n is input, create a program that outputs all in lexicographic order. n ≤ 30. However, lexicographic order means that two arrangements (a1, a2, ..., as) are arranged (b1, For b2, ..., bt), when a1> b1 or an integer i> 1 exists and a1 = b1, ..., ai-1 = bi-1 and ai> bi holds (a1) , a2, ..., as) are arranged so that they are output before (b1, b2, ..., bt). The input data consists of one line, with n written on the first line. In the output, write one line in lexicographic order and insert a line break at the end. The output of (a1, a2, ..., as) is the integer a1, a2, ..., as. Output in this order separated by blanks. Input example 1 --- Five Output example 1 Five 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1 input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output All data sets are output in lexicographic order. Example Input 5 5 0 Output 5 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1 5 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him. Input The single line contains a number n (1 ≤ n ≤ 104) which is the number of mounds. Output Print n integers pi (1 ≤ pi ≤ n) which are the frog's route plan. * All the pi's should be mutually different. * All the |pi–pi + 1|'s should be mutually different (1 ≤ i ≤ n - 1). If there are several solutions, output any. Examples Input 2 Output 1 2 Input 3 Output 1 3 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have $a$ coins of value $n$ and $b$ coins of value $1$. You always pay in exact change, so you want to know if there exist such $x$ and $y$ that if you take $x$ ($0 \le x \le a$) coins of value $n$ and $y$ ($0 \le y \le b$) coins of value $1$, then the total value of taken coins will be $S$. You have to answer $q$ independent test cases. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of test cases. Then $q$ test cases follow. The only line of the test case contains four integers $a$, $b$, $n$ and $S$ ($1 \le a, b, n, S \le 10^9$) — the number of coins of value $n$, the number of coins of value $1$, the value $n$ and the required total value. -----Output----- For the $i$-th test case print the answer on it — YES (without quotes) if there exist such $x$ and $y$ that if you take $x$ coins of value $n$ and $y$ coins of value $1$, then the total value of taken coins will be $S$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 4 1 2 3 4 1 2 3 6 5 2 6 27 3 3 5 18 Output YES NO NO YES Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Definition An **_element is leader_** *if it is greater than The Sum all the elements to its right side*. ____ # Task **_Given_** an *array/list [] of integers* , **_Find_** *all the **_LEADERS_** in the array*. ___ # Notes * **_Array/list_** size is *at least 3* . * **_Array/list's numbers_** Will be **_mixture of positives , negatives and zeros_** * **_Repetition_** of numbers in *the array/list could occur*. * **_Returned Array/list_** *should store the leading numbers **_in the same order_** in the original array/list* . ___ # Input >> Output Examples ``` arrayLeaders ({1, 2, 3, 4, 0}) ==> return {4} ``` ## **_Explanation_**: * `4` *is greater than the sum all the elements to its right side* * **_Note_** : **_The last element_** `0` *is equal to right sum of its elements (abstract zero)*. ____ ``` arrayLeaders ({16, 17, 4, 3, 5, 2}) ==> return {17, 5, 2} ``` ## **_Explanation_**: * `17` *is greater than the sum all the elements to its right side* * `5` *is greater than the sum all the elements to its right side* * **_Note_** : **_The last element_** `2` *is greater than the sum of its right elements (abstract zero)*. ___ ``` arrayLeaders ({5, 2, -1}) ==> return {5, 2} ``` ## **_Explanation_**: * `5` *is greater than the sum all the elements to its right side* * `2` *is greater than the sum all the elements to its right side* * **_Note_** : **_The last element_** `-1` *is less than the sum of its right elements (abstract zero)*. ___ ``` arrayLeaders ({0, -1, -29, 3, 2}) ==> return {0, -1, 3, 2} ``` ## **_Explanation_**: * `0` *is greater than the sum all the elements to its right side* * `-1` *is greater than the sum all the elements to its right side* * `3` *is greater than the sum all the elements to its right side* * **_Note_** : **_The last element_** `2` *is greater than the sum of its right elements (abstract zero)*. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 given two circles $c1$ and $c2$, print 4 if they do not cross (there are 4 common tangent lines), 3 if they are circumscribed (there are 3 common tangent lines), 2 if they intersect (there are 2 common tangent lines), 1 if a circle is inscribed in another (there are 1 common tangent line), 0 if a circle includes another (there is no common tangent line). Constraints * $-1,000 \leq c1x, c1y, c2x, c2y \leq 1,000$ * $1 \leq c1r, c2r \leq 1,000$ * $c1$ and $c2$ are different Input Coordinates and radii of $c1$ and $c2$ are given in the following format. $c1x \; c1y \; c1r$ $c2x \; c2y \; c2r$ $c1x$, $c1y$ and $c1r$ represent the center coordinate and radius of the first circle. $c2x$, $c2y$ and $c2r$ represent the center coordinate and radius of the second circle. All input values are given in integers. Output Print "4", "3", "2", "1" or "0" in a line. Examples Input 1 1 1 6 2 2 Output 4 Input 1 2 1 4 2 2 Output 3 Input 1 2 1 3 2 2 Output 2 Input 0 0 1 1 0 2 Output 1 Input 0 0 1 0 0 2 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria. According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied: * All even numbers written on the document are divisible by 3 or 5. If the immigrant should be allowed entry according to the regulation, output `APPROVED`; otherwise, print `DENIED`. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq A_i \leq 1000 Input Input is given from Standard Input in the following format: N A_1 A_2 \dots A_N Output If the immigrant should be allowed entry according to the regulation, print `APPROVED`; otherwise, print `DENIED`. Examples Input 5 6 7 9 10 31 Output APPROVED Input 3 28 27 24 Output DENIED Read the inputs from stdin solve the problem and write the answer to stdout (do 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 mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states: In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the Pythagorean equation: a^2 + b^2 = c^2 where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides. [Image] Given n, your task is to count how many right-angled triangles with side-lengths a, b and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n. -----Input----- The only line contains one integer n (1 ≤ n ≤ 10^4) as we mentioned above. -----Output----- Print a single integer — the answer to the problem. -----Examples----- Input 5 Output 1 Input 74 Output 35 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi the Jumbo will practice golf. His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive). If he can achieve the objective, print OK; if he cannot, print NG. -----Constraints----- - All values in input are integers. - 1 \leq A \leq B \leq 1000 - 1 \leq K \leq 1000 -----Input----- Input is given from Standard Input in the following format: K A B -----Output----- If he can achieve the objective, print OK; if he cannot, print NG. -----Sample Input----- 7 500 600 -----Sample Output----- OK Among the multiples of 7, for example, 567 lies between 500 and 600. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n ≠ 0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string s — the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one number — the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given $n$ integers $a_1, a_2, \ldots, a_n$. Find the maximum value of $max(a_l, a_{l + 1}, \ldots, a_r) \cdot min(a_l, a_{l + 1}, \ldots, a_r)$ over all pairs $(l, r)$ of integers for which $1 \le l < r \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum possible value of the product from the statement. -----Examples----- Input 4 3 2 4 3 4 3 2 3 1 2 69 69 6 719313 273225 402638 473783 804745 323328 Output 12 6 4761 381274500335 -----Note----- Let $f(l, r) = max(a_l, a_{l + 1}, \ldots, a_r) \cdot min(a_l, a_{l + 1}, \ldots, a_r)$. In the first test case, $f(1, 2) = max(a_1, a_2) \cdot min(a_1, a_2) = max(2, 4) \cdot min(2, 4) = 4 \cdot 2 = 8$. $f(1, 3) = max(a_1, a_2, a_3) \cdot min(a_1, a_2, a_3) = max(2, 4, 3) \cdot min(2, 4, 3) = 4 \cdot 2 = 8$. $f(2, 3) = max(a_2, a_3) \cdot min(a_2, a_3) = max(4, 3) \cdot min(4, 3) = 4 \cdot 3 = 12$. So the maximum is $f(2, 3) = 12$. In the second test case, the maximum is $f(1, 2) = f(1, 3) = f(2, 3) = 6$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n. Consider that m (m ≤ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below: 1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat. After occupying all the window seats (for m > 2n) the non-window seats are occupied: 1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat. All the passengers go to a single final destination. In the final destination, the passengers get off in the given order. 1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat. [Image] The seating for n = 9 and m = 36. You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus. -----Input----- The only line contains two integers, n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 4n) — the number of pairs of rows and the number of passengers. -----Output----- Print m distinct integers from 1 to m — the order in which the passengers will get off the bus. -----Examples----- Input 2 7 Output 5 1 6 2 7 3 4 Input 9 36 Output 19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dolphin is planning to generate a small amount of a certain chemical substance C. In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b. He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy. The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock. The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan). Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C. Find the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact. -----Constraints----- - 1≦N≦40 - 1≦a_i,b_i≦10 - 1≦c_i≦100 - 1≦M_a,M_b≦10 - gcd(M_a,M_b)=1 - a_i, b_i, c_i, M_a and M_b are integers. -----Input----- The input is given from Standard Input in the following format: N M_a M_b a_1 b_1 c_1 a_2 b_2 c_2 : a_N b_N c_N -----Output----- Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead. -----Sample Input----- 3 1 1 1 2 1 2 1 2 3 3 10 -----Sample Output----- 3 The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2. In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1. The total price of these packages is 3 yen. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day Vasya was going home when he saw a box lying on the road. The box can be represented as a rectangular parallelepiped. Vasya needed no time to realize that the box is special, as all its edges are parallel to the coordinate axes, one of its vertices is at point (0, 0, 0), and the opposite one is at point (x1, y1, z1). The six faces of the box contain some numbers a1, a2, ..., a6, exactly one number right in the center of each face. <image> The numbers are located on the box like that: * number a1 is written on the face that lies on the ZOX plane; * a2 is written on the face, parallel to the plane from the previous point; * a3 is written on the face that lies on the XOY plane; * a4 is written on the face, parallel to the plane from the previous point; * a5 is written on the face that lies on the YOZ plane; * a6 is written on the face, parallel to the plane from the previous point. At the moment Vasya is looking at the box from point (x, y, z). Find the sum of numbers that Vasya sees. Note that all faces of the box are not transparent and Vasya can't see the numbers through the box. The picture contains transparent faces just to make it easier to perceive. You can consider that if Vasya is looking from point, lying on the plane of some face, than he can not see the number that is written on this face. It is enough to see the center of a face to see the corresponding number for Vasya. Also note that Vasya always reads correctly the ai numbers that he sees, independently of their rotation, angle and other factors (that is, for example, if Vasya sees some ai = 6, then he can't mistake this number for 9 and so on). Input The fist input line contains three space-separated integers x, y and z (|x|, |y|, |z| ≤ 106) — the coordinates of Vasya's position in space. The second line contains three space-separated integers x1, y1, z1 (1 ≤ x1, y1, z1 ≤ 106) — the coordinates of the box's vertex that is opposite to the vertex at point (0, 0, 0). The third line contains six space-separated integers a1, a2, ..., a6 (1 ≤ ai ≤ 106) — the numbers that are written on the box faces. It is guaranteed that point (x, y, z) is located strictly outside the box. Output Print a single integer — the sum of all numbers on the box faces that Vasya sees. Examples Input 2 2 2 1 1 1 1 2 3 4 5 6 Output 12 Input 0 0 10 3 2 3 1 2 3 4 5 6 Output 4 Note The first sample corresponds to perspective, depicted on the picture. Vasya sees numbers a2 (on the top face that is the darkest), a6 (on the right face that is the lightest) and a4 (on the left visible face). In the second sample Vasya can only see number a4. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 n markers and m marker caps. Each marker is described by two numbers: xi is the color and yi is the diameter. Correspondingly, each cap is described by two numbers: aj is the color and bj is the diameter. Cap (aj, bj) can close marker (xi, yi) only if their diameters match, that is, bj = yi. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, aj = xi. Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers. Input The first input line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of markers and the number of caps, correspondingly. Next n lines describe the markers. The i-th line contains two space-separated integers xi, yi (1 ≤ xi, yi ≤ 1000) — the i-th marker's color and diameter, correspondingly. Next m lines describe the caps. The j-th line contains two space-separated integers aj, bj (1 ≤ aj, bj ≤ 1000) — the color and diameter of the j-th cap, correspondingly. Output Print two space-separated integers u, v, where u is the number of closed markers and v is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum. Examples Input 3 4 1 2 3 4 2 4 5 4 2 4 1 1 1 2 Output 3 2 Input 2 2 1 2 2 1 3 4 5 1 Output 1 0 Note In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed — the first and the third markers. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10]. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian as well. Your friend Сhef has prepared a rectangular cake for you. Both of you want to divide the cake among yourselves. Your friend is generous enough to let you choose your share first. You have decided to take two pieces. For the first piece you make a rectangular cut (each side of this cut is parallel to the corresponding side of the cake) inside the cake. Now the cake have two pieces. You take the piece inside the rectangle cut. For the second piece, you make another rectangular cut (each side of this cut is parallel to the corresponding side of the cake) inside the cake. Now the cake again have two pieces. You take the piece inside the rectangle cut (note that this piece may not be rectangular, because of cut may cross an empty space that remains from the first piece, also it can be empty). Your friend will have the rest of the cake. Given the cuts determine the amount of cake that you will have. The amount is calculated as the sum of the areas covered by your pieces. The cake can be considered as a rectangle with the lower left corner at (0,0) and the upper right corner at (1001,1001). ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case consists of two lines. Each line contains the description of a rectangular cut by giving the information of the rectangle. A rectangle is defined by four integers (co-ordinate of the lower-left corner (x1,y1) and upper right corner (x2,y2)). ------ Output ------ For each test case, output a single line containing the amount of cake you will have. ------ Constraints ------ $1≤T≤100$ $1≤x1x2≤1000$ $1≤y1y2≤1000$ ----- Sample Input 1 ------ 2 1 1 10 10 11 11 20 20 1 1 20 20 11 11 30 30 ----- Sample Output 1 ------ 162 641 ----- explanation 1 ------ Test Case 1: The area of the first piece is 81 and the area of the second piece is 81, a total of 162. Test Case 2: The area of the first piece is 361 and the area of the second piece is 280, a total of 641. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A country called Berland consists of n cities, numbered with integer numbers from 1 to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missiles. The exact position of the Super Duper Secret Missile Silos is kept secret but Bob managed to get hold of the information. That information says that all silos are located exactly at a distance l from the capital. The capital is located in the city with number s. The documents give the formal definition: the Super Duper Secret Missile Silo is located at some place (which is either city or a point on a road) if and only if the shortest distance from this place to the capital along the roads of the country equals exactly l. Bob wants to know how many missile silos are located in Berland to sell the information then to enemy spies. Help Bob. Input The first line contains three integers n, m and s (2 ≤ n ≤ 105, <image>, 1 ≤ s ≤ n) — the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. s. Then m lines contain the descriptions of roads. Each of them is described by three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1000), where vi, ui are numbers of the cities connected by this road and wi is its length. The last input line contains integer l (0 ≤ l ≤ 109) — the distance from the capital to the missile silos. It is guaranteed that: * between any two cities no more than one road exists; * each road connects two different cities; * from each city there is at least one way to any other city by the roads. Output Print the single number — the number of Super Duper Secret Missile Silos that are located in Berland. Examples Input 4 6 1 1 2 1 1 3 3 2 3 1 2 4 1 3 4 1 1 4 2 2 Output 3 Input 5 6 3 3 1 1 3 2 1 3 4 1 3 5 1 1 2 6 4 5 8 4 Output 3 Note In the first sample the silos are located in cities 3 and 4 and on road (1, 3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3). In the second sample one missile silo is located right in the middle of the road (1, 2). Two more silos are on the road (4, 5) at a distance 3 from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given $n$ elements numbered from $1$ to $n$, the element $i$ has value $a_i$ and color $c_i$, initially, $c_i = 0$ for all $i$. The following operation can be applied: Select three elements $i$, $j$ and $k$ ($1 \leq i < j < k \leq n$), such that $c_i$, $c_j$ and $c_k$ are all equal to $0$ and $a_i = a_k$, then set $c_j = 1$. Find the maximum value of $\sum\limits_{i=1}^n{c_i}$ that can be obtained after applying the given operation any number of times. -----Input----- The first line contains an integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) — the number of elements. The second line consists of $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$), where $a_i$ is the value of the $i$-th element. -----Output----- Print a single integer in a line — the maximum value of $\sum\limits_{i=1}^n{c_i}$ that can be obtained after applying the given operation any number of times. -----Examples----- Input 7 1 2 1 2 7 4 7 Output 2 Input 13 1 2 3 2 1 3 3 4 5 5 5 4 7 Output 7 -----Note----- In the first test, it is possible to apply the following operations in order: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given n points on the plane. The polygon formed from all the n points is strictly convex, that is, the polygon is convex, and there are no three collinear points (i.e. lying in the same straight line). The points are numbered from 1 to n, in clockwise order. We define the distance between two points p_1 = (x_1, y_1) and p_2 = (x_2, y_2) as their Manhattan distance: $$$d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|.$$$ Furthermore, we define the perimeter of a polygon, as the sum of Manhattan distances between all adjacent pairs of points on it; if the points on the polygon are ordered as p_1, p_2, …, p_k (k ≥ 3), then the perimeter of the polygon is d(p_1, p_2) + d(p_2, p_3) + … + d(p_k, p_1). For some parameter k, let's consider all the polygons that can be formed from the given set of points, having any k vertices, such that the polygon is not self-intersecting. For each such polygon, let's consider its perimeter. Over all such perimeters, we define f(k) to be the maximal perimeter. Please note, when checking whether a polygon is self-intersecting, that the edges of a polygon are still drawn as straight lines. For instance, in the following pictures: <image> In the middle polygon, the order of points (p_1, p_3, p_2, p_4) is not valid, since it is a self-intersecting polygon. The right polygon (whose edges resemble the Manhattan distance) has the same order and is not self-intersecting, but we consider edges as straight lines. The correct way to draw this polygon is (p_1, p_2, p_3, p_4), which is the left polygon. Your task is to compute f(3), f(4), …, f(n). In other words, find the maximum possible perimeter for each possible number of points (i.e. 3 to n). Input The first line contains a single integer n (3 ≤ n ≤ 3⋅ 10^5) — the number of points. Each of the next n lines contains two integers x_i and y_i (-10^8 ≤ x_i, y_i ≤ 10^8) — the coordinates of point p_i. The set of points is guaranteed to be convex, all points are distinct, the points are ordered in clockwise order, and there will be no three collinear points. Output For each i (3≤ i≤ n), output f(i). Examples Input 4 2 4 4 3 3 0 1 3 Output 12 14 Input 3 0 0 0 2 2 0 Output 8 Note In the first example, for f(3), we consider four possible polygons: * (p_1, p_2, p_3), with perimeter 12. * (p_1, p_2, p_4), with perimeter 8. * (p_1, p_3, p_4), with perimeter 12. * (p_2, p_3, p_4), with perimeter 12. For f(4), there is only one option, taking all the given points. Its perimeter 14. In the second example, there is only one possible polygon. Its perimeter is 8. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have N bricks arranged in a row from left to right. The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it. Among them, you can break at most N-1 bricks of your choice. Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it. Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 200000 - 1 \leq a_i \leq N -----Input----- Input is given from Standard Input in the following format: N a_1 a_2 ... a_N -----Output----- Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable. -----Sample Input----- 3 2 1 2 -----Sample Output----- 1 If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Petya loves inequations. Help him find n positive integers a1, a2, ..., an, such that the following two conditions are satisfied: * a12 + a22 + ... + an2 ≥ x * a1 + a2 + ... + an ≤ y Input The first line contains three space-separated integers n, x and y (1 ≤ n ≤ 105, 1 ≤ x ≤ 1012, 1 ≤ y ≤ 106). Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator. Output Print n positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number "-1". If there are several solutions, print any of them. Examples Input 5 15 15 Output 4 4 1 1 2 Input 2 3 2 Output -1 Input 1 99 11 Output 11 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors. Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem? You have a perfect binary tree of $2^k - 1$ nodes — a binary tree where all vertices $i$ from $1$ to $2^{k - 1} - 1$ have exactly two children: vertices $2i$ and $2i + 1$. Vertices from $2^{k - 1}$ to $2^k - 1$ don't have any children. You want to color its vertices with the $6$ Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow). Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube. A picture of Rubik's cube and its 2D map. More formally: a white node can not be neighboring with white and yellow nodes; a yellow node can not be neighboring with white and yellow nodes; a green node can not be neighboring with green and blue nodes; a blue node can not be neighboring with green and blue nodes; a red node can not be neighboring with red and orange nodes; an orange node can not be neighboring with red and orange nodes; However, there are $n$ special nodes in the tree, colors of which are already chosen. You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color. The answer may be too large, so output the answer modulo $10^9+7$. -----Input----- The first line contains the integers $k$ ($1 \le k \le 60$) — the number of levels in the perfect binary tree you need to color. The second line contains the integer $n$ ($1 \le n \le \min(2^k - 1, 2000)$) — the number of nodes, colors of which are already chosen. The next $n$ lines contains integer $v$ ($1 \le v \le 2^k - 1$) and string $s$ — the index of the node and the color of the node ($s$ is one of the white, yellow, green, blue, red and orange). It is guaranteed that each node $v$ appears in the input at most once. -----Output----- Print one integer — the number of the different colorings modulo $10^9+7$. -----Examples----- Input 3 2 5 orange 2 white Output 1024 Input 2 2 1 white 2 white Output 0 Input 10 3 1 blue 4 red 5 orange Output 328925088 -----Note----- In the picture below, you can see one of the correct colorings of the first test example. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers). Input The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph. Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j. Output If there exists such a tree, output "YES", otherwise output "NO". Examples Input 3 0 2 7 2 0 9 7 9 0 Output YES Input 3 1 2 7 2 0 9 7 9 0 Output NO Input 3 0 2 2 7 0 9 7 9 0 Output NO Input 3 0 1 1 1 0 1 1 1 0 Output NO Input 2 0 0 0 0 Output NO Note In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7. In the second example, it is impossible because d1, 1 should be 0, but it is 1. In the third example, it is impossible because d1, 2 should equal d2, 1. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke has N dogs and M monkeys. He wants them to line up in a row. As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys. How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that). Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished. Constraints * 1 ≤ N,M ≤ 10^5 Input Input is given from Standard Input in the following format: N M Output Print the number of possible arrangements, modulo 10^9+7. Examples Input 2 2 Output 8 Input 3 2 Output 12 Input 1 8 Output 0 Input 100000 100000 Output 530123477 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Introduction There is a war and nobody knows - the alphabet war! There are two groups of hostile letters. The tension between left side letters and right side letters was too high and the war began. The letters called airstrike to help them in war - dashes and dots are spreaded everywhere on the battlefield. # Task Write a function that accepts `fight` string consists of only small letters and `*` which means a bomb drop place. Return who wins the fight after bombs are exploded. When the left side wins return `Left side wins!`, when the right side wins return `Right side wins!`, in other case return `Let's fight again!`. The left side letters and their power: ``` w - 4 p - 3 b - 2 s - 1 ``` The right side letters and their power: ``` m - 4 q - 3 d - 2 z - 1 ``` The other letters don't have power and are only victims. The `*` bombs kills the adjacent letters ( i.e. `aa*aa` => `a___a`, `**aa**` => `______` ); # Example # Alphabet war Collection Alphavet war Alphabet war - airstrike - letters massacre Alphabet wars - reinforces massacre Alphabet wars - nuclear strike Alphabet war - Wo lo loooooo priests join the war Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number. Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9). Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells: * Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1. * Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells. <image> An example of a chessboard. Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples). Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number! Input The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells. Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1. Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive. It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points. Output In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9 Examples Input 2 3 6 8 1 5 6 1 9 4 2 4 2 Output 1 Input 1 3 4 1 5 3 1 9 4 4 6 6 Output 1 Input 0 2 1 1000000000 4 1 1000000000 2 Output 2 Input 0 0 Output 0 Input 2 3 4 6 1 4 3 1 5 2 1 6 5 Output 2 Note In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell. <image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell. <image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them. <image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. In the fourth sample, we have no spells, which means that we do not need to remove anything. In the fifth example, we can remove the first vertical and third horizontal spells. <image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp, Arkady's friend, prepares to the programming competition and decides to write a contest. The contest consists of $n$ problems and lasts for $T$ minutes. Each of the problems is defined by two positive integers $a_i$ and $p_i$ — its difficulty and the score awarded by its solution. Polycarp's experience suggests that his skill level is defined with positive real value $s$, and initially $s=1.0$. To solve the $i$-th problem Polycarp needs $a_i/s$ minutes. Polycarp loves to watch series, and before solving each of the problems he will definitely watch one episode. After Polycarp watches an episode, his skill decreases by $10\%$, that is skill level $s$ decreases to $0.9s$. Each episode takes exactly $10$ minutes to watch. When Polycarp decides to solve some problem, he firstly has to watch one episode, and only then he starts solving the problem without breaks for $a_i/s$ minutes, where $s$ is his current skill level. In calculation of $a_i/s$ no rounding is performed, only division of integer value $a_i$ by real value $s$ happens. Also, Polycarp can train for some time. If he trains for $t$ minutes, he increases his skill by $C \cdot t$, where $C$ is some given positive real constant. Polycarp can train only before solving any problem (and before watching series). Duration of the training can be arbitrary real value. Polycarp is interested: what is the largest score he can get in the contest? It is allowed to solve problems in any order, while training is only allowed before solving the first problem. -----Input----- The first line contains one integer $tc$ ($1 \le tc \le 20$) — the number of test cases. Then $tc$ test cases follow. The first line of each test contains one integer $n$ ($1 \le n \le 100$) — the number of problems in the contest. The second line of the test contains two real values $C, T$ ($0 < C < 10$, $0 \le T \le 2 \cdot 10^5$), where $C$ defines the efficiency of the training and $T$ is the duration of the contest in minutes. Value $C, T$ are given exactly with three digits after the decimal point. Each of the next $n$ lines of the test contain characteristics of the corresponding problem: two integers $a_i, p_i$ ($1 \le a_i \le 10^4$, $1 \le p_i \le 10$) — the difficulty and the score of the problem. It is guaranteed that the value of $T$ is such that changing it by the $0.001$ in any direction will not change the test answer. Please note that in hacks you can only use $tc = 1$. -----Output----- Print $tc$ integers — the maximum possible score in each test case. -----Examples----- Input 2 4 1.000 31.000 12 3 20 6 30 1 5 1 3 1.000 30.000 1 10 10 10 20 8 Output 7 20 -----Note----- In the first example, Polycarp can get score of $7$ as follows: Firstly he trains for $4$ minutes, increasing $s$ to the value of $5$; Then he decides to solve $4$-th problem: he watches one episode in $10$ minutes, his skill level decreases to $s=5*0.9=4.5$ and then he solves the problem in $5/s=5/4.5$, which is roughly $1.111$ minutes; Finally, he decides to solve $2$-nd problem: he watches one episode in $10$ minutes, his skill level decreases to $s=4.5*0.9=4.05$ and then he solves the problem in $20/s=20/4.05$, which is roughly $4.938$ minutes. This way, Polycarp uses roughly $4+10+1.111+10+4.938=30.049$ minutes, to get score of $7$ points. It is not possible to achieve larger score in $31$ minutes. In the second example, Polycarp can get $20$ points as follows: Firstly he trains for $4$ minutes, increasing $s$ to the value of $5$; Then he decides to solve $1$-st problem: he watches one episode in $10$ minutes, his skill decreases to $s=5*0.9=4.5$ and then he solves problem in $1/s=1/4.5$, which is roughly $0.222$ minutes. Finally, he decides to solve $2$-nd problem: he watches one episode in $10$ minutes, his skill decreases to $s=4.5*0.9=4.05$ and then he solves the problem in $10/s=10/4.05$, which is roughly $2.469$ minutes. This way, Polycarp gets score of $20$ in $4+10+0.222+10+2.469=26.691$ minutes. It is not possible to achieve larger score in $30$ minutes. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter. Their algorithm will be tested on an array of integers, where the $i$-th integer represents the color of the $i$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive). To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $k$, and each color should belong to exactly one group. Finally, the students will replace the color of each pixel in the array with that color’s assigned group key. To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $k$ to the right. [Image] To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array. -----Input----- The first line of input contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 256$), the number of pixels in the image, and the maximum size of a group, respectively. The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($0 \leq p_i \leq 255$), where $p_i$ is the color of the $i$-th pixel. -----Output----- Print $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter. -----Examples----- Input 4 3 2 14 3 4 Output 0 12 3 3 Input 5 2 0 2 1 255 254 Output 0 1 1 254 254 -----Note----- One possible way to group colors and assign keys for the first sample: Color $2$ belongs to the group $[0,2]$, with group key $0$. Color $14$ belongs to the group $[12,14]$, with group key $12$. Colors $3$ and $4$ belong to group $[3, 5]$, with group key $3$. Other groups won't affect the result so they are not listed here. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Ultima Thule job applicants are often offered an IQ test. The test is as follows: the person gets a piece of squared paper with a 4 × 4 square painted on it. Some of the square's cells are painted black and others are painted white. Your task is to repaint at most one cell the other color so that the picture has a 2 × 2 square, completely consisting of cells of the same color. If the initial picture already has such a square, the person should just say so and the test will be completed. Your task is to write a program that determines whether it is possible to pass the test. You cannot pass the test if either repainting any cell or no action doesn't result in a 2 × 2 square, consisting of cells of the same color. -----Input----- Four lines contain four characters each: the j-th character of the i-th line equals "." if the cell in the i-th row and the j-th column of the square is painted white, and "#", if the cell is black. -----Output----- Print "YES" (without the quotes), if the test can be passed and "NO" (without the quotes) otherwise. -----Examples----- Input #### .#.. #### .... Output YES Input #### .... #### .... Output NO -----Note----- In the first test sample it is enough to repaint the first cell in the second row. After such repainting the required 2 × 2 square is on the intersection of the 1-st and 2-nd row with the 1-st and 2-nd column. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.