question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Create a function that takes a number as an argument and returns a grade based on that number. Score | Grade -----------------------------------------|----- Anything greater than 1 or less than 0.6 | "F" 0.9 or greater | "A" 0.8 or greater | "B" 0.7 or greater | "C" 0.6 or greater | "D" Examples: ``` grader(0) should be "F" grader(1.1) should be "F" grader(0.9) should be "A" grader(0.8) should be "B" grader(0.7) should be "C" grader(0.6) should be "D" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference. -----Input----- The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. -----Output----- In a single line print a single real number — the required difference with exactly three digits after the decimal point. -----Examples----- Input 3 0.000 0.500 0.750 1.000 2.000 3.000 Output 0.250 Input 3 4469.000 6526.000 4864.000 9356.383 7490.000 995.896 Output 0.279 -----Note----- In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. Read the inputs from stdin solve the problem and write the answer to stdout (do 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've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round. The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2. Diameter of multiset consisting of one point is 0. You are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d? -----Input----- The first line contains two integers n and d (1 ≤ n ≤ 100, 0 ≤ d ≤ 100) — the amount of points and the maximum allowed diameter respectively. The second line contains n space separated integers (1 ≤ x_{i} ≤ 100) — the coordinates of the points. -----Output----- Output a single integer — the minimum number of points you have to remove. -----Examples----- Input 3 1 2 1 4 Output 1 Input 3 0 7 7 7 Output 0 Input 6 3 1 3 4 6 9 10 Output 3 -----Note----- In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1. In the second test case the diameter is equal to 0, so its is unnecessary to remove any points. In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 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. "The zombies are lurking outside. Waiting. Moaning. And when they come..." "When they come?" "I hope the Wall is high enough." Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are. The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide. -----Input----- The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows: each of the R lines contains a string of length C, the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise. The input will contain at least one character B and it will be valid. -----Output----- The number of wall segments in the input configuration. -----Examples----- Input 3 7 ....... ....... .BB.B.. Output 2 Input 4 5 ..B.. ..B.. B.B.B BBB.B Output 2 Input 4 6 ..B... B.B.BB BBB.BB BBBBBB Output 1 Input 1 1 B Output 1 Input 10 7 ....... ....... ....... ....... ....... ....... ....... ....... ...B... B.BB.B. Output 3 Input 8 8 ........ ........ ........ ........ .B...... .B.....B .B.....B .BB...BB Output 2 -----Note----- In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are n piles of cows, with the i-th pile containing a_{i} cows. During each player's turn, that player calls upon the power of Sunlight, and uses it to either: Remove a single cow from a chosen non-empty pile. Choose a pile of cows with even size 2·x (x > 0), and replace it with k piles of x cows each. The player who removes the last cow wins. Given n, k, and a sequence a_1, a_2, ..., a_{n}, help Kevin and Nicky find the winner, given that both sides play in optimal way. -----Input----- The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 10^9). The second line contains n integers, a_1, a_2, ... a_{n} (1 ≤ a_{i} ≤ 10^9) describing the initial state of the game. -----Output----- Output the name of the winning player, either "Kevin" or "Nicky" (without quotes). -----Examples----- Input 2 1 3 4 Output Kevin Input 1 2 3 Output Nicky -----Note----- In the second sample, Nicky can win in the following way: Kevin moves first and is forced to remove a cow, so the pile contains two cows after his move. Next, Nicky replaces this pile of size 2 with two piles of size 1. So the game state is now two piles of size 1. Kevin then removes one of the remaining cows and Nicky wins by removing the other. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Complete the function that returns an array of length `n`, starting with the given number `x` and the squares of the previous number. If `n` is negative or zero, return an empty array/list. ## Examples ``` 2, 5 --> [2, 4, 16, 256, 65536] 3, 3 --> [3, 9, 81] ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out from the rectangular piece of the multicolored fabric. The piece of the multicolored fabric consists of $n \times m$ separate square scraps. Since Rarity likes dresses in style, a dress pattern must only include scraps sharing the same color. A dress pattern must be the square, and since Rarity is fond of rhombuses, the sides of a pattern must form a $45^{\circ}$ angle with sides of a piece of fabric (that way it will be resembling the traditional picture of a rhombus). Examples of proper dress patterns: [Image] Examples of improper dress patterns: [Image] The first one consists of multi-colored scraps, the second one goes beyond the bounds of the piece of fabric, the third one is not a square with sides forming a $45^{\circ}$ angle with sides of the piece of fabric. Rarity wonders how many ways to cut out a dress pattern that satisfies all the conditions that do exist. Please help her and satisfy her curiosity so she can continue working on her new masterpiece! -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2000$). Each of the next $n$ lines contains $m$ characters: lowercase English letters, the $j$-th of which corresponds to scrap in the current line and in the $j$-th column. Scraps having the same letter share the same color, scraps having different letters have different colors. -----Output----- Print a single integer: the number of ways to cut out a dress pattern to satisfy all of Rarity's conditions. -----Examples----- Input 3 3 aaa aaa aaa Output 10 Input 3 4 abab baba abab Output 12 Input 5 5 zbacg baaac aaaaa eaaad weadd Output 31 -----Note----- In the first example, all the dress patterns of size $1$ and one of size $2$ are satisfactory. In the second example, only the dress patterns of size $1$ are satisfactory. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n steps. On the i-th step Greg removes vertex number x_{i} from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex. Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex x_{i}, then Greg wants to know the value of the following sum: $\sum_{v, u, v \neq u} d(i, v, u)$. Help Greg, print the value of the required sum before each step. -----Input----- The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph. Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line a_{ij} (1 ≤ a_{ij} ≤ 10^5, a_{ii} = 0) represents the weight of the edge that goes from vertex i to vertex j. The next line contains n distinct integers: x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ n) — the vertices that Greg deletes. -----Output----- Print n integers — the i-th number equals the required sum before the i-th step. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. -----Examples----- Input 1 0 1 Output 0 Input 2 0 5 4 0 1 2 Output 9 0 Input 4 0 3 1 1 6 0 400 1 2 4 0 1 1 1 1 0 4 1 2 3 Output 17 23 404 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. Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied. The classroom contains $n$ rows of seats and there are $m$ seats in each row. Then the classroom can be represented as an $n \times m$ matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find $k$ consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs. -----Input----- The first line contains three positive integers $n,m,k$ ($1 \leq n, m, k \leq 2\,000$), where $n,m$ represent the sizes of the classroom and $k$ is the number of consecutive seats you need to find. Each of the next $n$ lines contains $m$ characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat. -----Output----- A single number, denoting the number of ways to find $k$ empty seats in the same row or column. -----Examples----- Input 2 3 2 **. ... Output 3 Input 1 2 2 .. Output 1 Input 3 3 4 .*. *.* .*. Output 0 -----Note----- In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement. $(1,3)$, $(2,3)$ $(2,2)$, $(2,3)$ $(2,1)$, $(2,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. The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Books are indexed. Write a program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers. You can assume that a word consists of at most 30 characters, and the page number is less than or equal to 1000. The number of pairs of a word and a page number is less than or equal to 100. A word never appear in a page more than once. The words should be printed in alphabetical order and the page numbers should be printed in ascending order. Input word page_number :: :: Output word a_list_of_the_page_number word a_list_of_the_Page_number :: :: Example Input style 12 even 25 introduction 3 easy 9 style 7 document 13 style 21 even 18 Output document 13 easy 9 even 18 25 introduction 3 style 7 12 21 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A country has a budget of more than 81 trillion yen. We want to process such data, but conventional integer type which uses signed 32 bit can represent up to 2,147,483,647. Your task is to write a program which reads two integers (more than or equal to zero), and prints a sum of these integers. If given integers or the sum have more than 80 digits, print "overflow". Input Input consists of several datasets. In the first line, the number of datasets N (1 ≤ N ≤ 50) is given. Each dataset consists of 2 lines: The first integer The second integer The integer has at most 100 digits. Output For each dataset, print the sum of given integers in a line. Example Input 6 1000 800 9999999999999999999999999999999999999999 1 99999999999999999999999999999999999999999999999999999999999999999999999999999999 1 99999999999999999999999999999999999999999999999999999999999999999999999999999999 0 100000000000000000000000000000000000000000000000000000000000000000000000000000000 1 100000000000000000000000000000000000000000000000000000000000000000000000000000000 100000000000000000000000000000000000000000000000000000000000000000000000000000000 Output 1800 10000000000000000000000000000000000000000 overflow 99999999999999999999999999999999999999999999999999999999999999999999999999999999 overflow overflow Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate. [Image] Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x = A × 10^{B} is true. In our case A is between 0 and 9 and B is non-negative. Barney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding. -----Input----- The first and only line of input contains a single string of form a.deb where a, d and b are integers and e is usual character 'e' (0 ≤ a ≤ 9, 0 ≤ d < 10^100, 0 ≤ b ≤ 100) — the scientific notation of the desired distance value. a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero. -----Output----- Print the only real number x (the desired distance value) in the only line in its decimal notation. Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes. Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero). -----Examples----- Input 8.549e2 Output 854.9 Input 8.549e3 Output 8549 Input 0.33e0 Output 0.33 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a square of size $10^6 \times 10^6$ on the coordinate plane with four points $(0, 0)$, $(0, 10^6)$, $(10^6, 0)$, and $(10^6, 10^6)$ as its vertices. You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square. Now you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square. -----Input----- The first line contains two integers $n$ and $m$ ($0 \le n, m \le 10^5$) — the number of horizontal segments and the number of vertical segments. The next $n$ lines contain descriptions of the horizontal segments. The $i$-th line contains three integers $y_i$, $lx_i$ and $rx_i$ ($0 < y_i < 10^6$; $0 \le lx_i < rx_i \le 10^6$), which means the segment connects $(lx_i, y_i)$ and $(rx_i, y_i)$. The next $m$ lines contain descriptions of the vertical segments. The $i$-th line contains three integers $x_i$, $ly_i$ and $ry_i$ ($0 < x_i < 10^6$; $0 \le ly_i < ry_i \le 10^6$), which means the segment connects $(x_i, ly_i)$ and $(x_i, ry_i)$. It's guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square's sides. -----Output----- Print the number of pieces the square is divided into after drawing all the segments. -----Example----- Input 3 3 2 3 1000000 4 0 4 3 0 1000000 4 0 1 2 0 5 3 1 1000000 Output 7 -----Note----- The sample is like this: [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. Kurohashi has never participated in AtCoder Beginner Contest (ABC). The next ABC to be held is ABC N (the N-th ABC ever held). Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same. What is the earliest ABC where Kurohashi can make his debut? -----Constraints----- - 100 \leq N \leq 999 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If the earliest ABC where Kurohashi can make his debut is ABC n, print n. -----Sample Input----- 111 -----Sample Output----- 111 The next ABC to be held is ABC 111, where Kurohashi can make his debut. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array. A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous. -----Input----- The first line contains single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print the maximum length of an increasing subarray of the given array. -----Examples----- Input 5 1 7 2 11 15 Output 3 Input 6 100 100 100 100 100 100 Output 1 Input 3 1 2 3 Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array $a$ of length $2^n$. You should process $q$ queries on it. Each query has one of the following $4$ types: $Replace(x, k)$ — change $a_x$ to $k$; $Reverse(k)$ — reverse each subarray $[(i-1) \cdot 2^k+1, i \cdot 2^k]$ for all $i$ ($i \ge 1$); $Swap(k)$ — swap subarrays $[(2i-2) \cdot 2^k+1, (2i-1) \cdot 2^k]$ and $[(2i-1) \cdot 2^k+1, 2i \cdot 2^k]$ for all $i$ ($i \ge 1$); $Sum(l, r)$ — print the sum of the elements of subarray $[l, r]$. Write a program that can quickly process given queries. -----Input----- The first line contains two integers $n$, $q$ ($0 \le n \le 18$; $1 \le q \le 10^5$) — the length of array $a$ and the number of queries. The second line contains $2^n$ integers $a_1, a_2, \ldots, a_{2^n}$ ($0 \le a_i \le 10^9$). Next $q$ lines contains queries — one per line. Each query has one of $4$ types: "$1$ $x$ $k$" ($1 \le x \le 2^n$; $0 \le k \le 10^9$) — $Replace(x, k)$; "$2$ $k$" ($0 \le k \le n$) — $Reverse(k)$; "$3$ $k$" ($0 \le k < n$) — $Swap(k)$; "$4$ $l$ $r$" ($1 \le l \le r \le 2^n$) — $Sum(l, r)$. It is guaranteed that there is at least one $Sum$ query. -----Output----- Print the answer for each $Sum$ query. -----Examples----- Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 -----Note----- In the first sample, initially, the array $a$ is equal to $\{7,4,9,9\}$. After processing the first query. the array $a$ becomes $\{7,8,9,9\}$. After processing the second query, the array $a_i$ becomes $\{9,9,7,8\}$ Therefore, the answer to the third query is $9+7+8=24$. In the second sample, initially, the array $a$ is equal to $\{7,0,8,8,7,1,5,2\}$. What happens next is: $Sum(3, 7)$ $\to$ $8 + 8 + 7 + 1 + 5 = 29$; $Reverse(1)$ $\to$ $\{0,7,8,8,1,7,2,5\}$; $Swap(2)$ $\to$ $\{1,7,2,5,0,7,8,8\}$; $Sum(1, 6)$ $\to$ $1 + 7 + 2 + 5 + 0 + 7 = 22$; $Reverse(3)$ $\to$ $\{8,8,7,0,5,2,7,1\}$; $Replace(5, 16)$ $\to$ $\{8,8,7,0,16,2,7,1\}$; $Sum(8, 8)$ $\to$ $1$; $Swap(0)$ $\to$ $\{8,8,0,7,2,16,1,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. Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed. This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included). As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him. -----Input----- The first line contains two integers n and k (2 ≤ n ≤ 2·10^5, 0 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x_1, x_2, ..., x_{n} — the absurdity of each law (1 ≤ x_{i} ≤ 10^9). -----Output----- Print two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b. -----Examples----- Input 5 2 3 6 1 1 6 Output 1 4 Input 6 2 1 1 1 1 1 1 Output 1 3 -----Note----- In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16. In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [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. The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single `100`, `50` or `25` dollar bill. An "Avengers" ticket costs `25 dollars`. Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line. Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue? Return `YES`, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return `NO`. ### Examples: ```csharp Line.Tickets(new int[] {25, 25, 50}) // => YES Line.Tickets(new int[] {25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars Line.Tickets(new int[] {25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50) ``` ```python tickets([25, 25, 50]) # => YES tickets([25, 100]) # => NO. Vasya will not have enough money to give change to 100 dollars tickets([25, 25, 50, 50, 100]) # => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50) ``` ```cpp tickets({25, 25, 50}) // => YES tickets({25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars tickets({25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50) ``` Read the inputs from stdin solve the problem and write the answer to stdout (do 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 non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string. A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s_1s_2... s_{|}s| (where |s| is the length of string s) is string s_{l}s_{l} + 1... s_{r}. String x = x_1x_2... x_{p} is lexicographically smaller than string y = y_1y_2... y_{q}, if either p < q and x_1 = y_1, x_2 = y_2, ... , x_{p} = y_{p}, or there exists such number r (r < p, r < q), that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 < y_{r} + 1. The string characters are compared by their ASCII codes. -----Input----- The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive. -----Output----- Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title. -----Examples----- Input 5 threehorses goodsubstrings secret primematrix beautifulyear Output j Input 4 aa bdefghijklmn opqrstuvwxyz c Output ab -----Note----- In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j. In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Flash has invited his nemesis The Turtle (He actually was a real villain! ) to play his favourite card game, SNAP. In this game a 52 card deck is dealt out so both Flash and the Turtle get 26 random cards. Each players cards will be represented by an array like below Flash’s pile: ```[ 'A', '5', 'Q', 'Q', '6', '2', 'A', '9', '10', '6', '4', '3', '10', '9', '3', '8', 'K', 'J', 'J', 'K', '7', '9', '5', 'J', '7', '2' ]``` Turtle’s pile: ```[ '8', 'A', '2', 'Q', 'K', '8', 'J', '6', '4', '8', '7', 'A', '5', 'K', '3', 'Q', '6', '9', '4', '3', '4', '10', '2', '7', '10', '5' ]``` The players take it in turn to take the top card from their deck (the first element in their array) and place it in a face up pile in the middle. Flash goes first. When a card is placed on the face up pile that matches the card it is placed on top of the first player to shout ‘SNAP!’ wins that round. Due to Flash's speed he wins every round. Face up pile in the middle: ```[ 'A', '8', '5', 'A', 'Q', '2', 'Q', 'Q',``` => SNAP! The face up pile of cards in the middle are added to the bottom of Flash's pile. Flash’s pile after one round: ```['6', '2', 'A', '9', '10', '6', '4', '3', '10', '9', '3', '8', 'K', 'J', 'J', 'K', '7', '9', '5', 'J', '7', '2', 'A', '8', '5', 'A', 'Q', '2', 'Q', 'Q' ]``` Flash then starts the next round by putting down the next card. When Turtle runs out of cards the game is over. How many times does Flash get to call Snap before Turtle runs out of cards? If both the player put down all their cards into the middle without any matches then the game ends a draw and Flash calls SNAP 0 times. Read the inputs from stdin solve the problem and write the answer to stdout (do 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$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 loves working out. He is now exercising N times. Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i. Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7. -----Constraints----- - 1 ≤ N ≤ 10^{5} -----Input----- The input is given from Standard Input in the following format: N -----Output----- Print the answer modulo 10^{9}+7. -----Sample Input----- 3 -----Sample Output----- 6 - After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1. - After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2. - After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 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. Masha lives in a multi-storey building, where floors are numbered with positive integers. Two floors are called adjacent if their numbers differ by one. Masha decided to visit Egor. Masha lives on the floor $x$, Egor on the floor $y$ (not on the same floor with Masha). The house has a staircase and an elevator. If Masha uses the stairs, it takes $t_1$ seconds for her to walk between adjacent floors (in each direction). The elevator passes between adjacent floors (in each way) in $t_2$ seconds. The elevator moves with doors closed. The elevator spends $t_3$ seconds to open or close the doors. We can assume that time is not spent on any action except moving between adjacent floors and waiting for the doors to open or close. If Masha uses the elevator, it immediately goes directly to the desired floor. Coming out of the apartment on her floor, Masha noticed that the elevator is now on the floor $z$ and has closed doors. Now she has to choose whether to use the stairs or use the elevator. If the time that Masha needs to get to the Egor's floor by the stairs is strictly less than the time it will take her using the elevator, then she will use the stairs, otherwise she will choose the elevator. Help Mary to understand whether to use the elevator or the stairs. -----Input----- The only line contains six integers $x$, $y$, $z$, $t_1$, $t_2$, $t_3$ ($1 \leq x, y, z, t_1, t_2, t_3 \leq 1000$) — the floor Masha is at, the floor Masha wants to get to, the floor the elevator is located on, the time it takes Masha to pass between two floors by stairs, the time it takes the elevator to pass between two floors and the time it takes for the elevator to close or open the doors. It is guaranteed that $x \ne y$. -----Output----- If the time it will take to use the elevator is not greater than the time it will take to use the stairs, print «YES» (without quotes), otherwise print «NO> (without quotes). You can print each letter in any case (upper or lower). -----Examples----- Input 5 1 4 4 2 1 Output YES Input 1 6 6 2 1 1 Output NO Input 4 1 7 4 1 2 Output YES -----Note----- In the first example: If Masha goes by the stairs, the time she spends is $4 \cdot 4 = 16$, because she has to go $4$ times between adjacent floors and each time she spends $4$ seconds. If she chooses the elevator, she will have to wait $2$ seconds while the elevator leaves the $4$-th floor and goes to the $5$-th. After that the doors will be opening for another $1$ second. Then Masha will enter the elevator, and she will have to wait for $1$ second for the doors closing. Next, the elevator will spend $4 \cdot 2 = 8$ seconds going from the $5$-th floor to the $1$-st, because the elevator has to pass $4$ times between adjacent floors and spends $2$ seconds each time. And finally, it will take another $1$ second before the doors are open and Masha can come out. Thus, all the way by elevator will take $2 + 1 + 1 + 8 + 1 = 13$ seconds, which is less than $16$ seconds, so Masha has to choose the elevator. In the second example, it is more profitable for Masha to use the stairs, because it will take $13$ seconds to use the elevator, that is more than the $10$ seconds it will takes to go by foot. In the third example, the time it takes to use the elevator is equal to the time it takes to walk up by the stairs, and is equal to $12$ seconds. That means Masha will take the elevator. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem. There are $n$ positive integers $a_1, a_2, \ldots, a_n$ on Bob's homework paper, where $n$ is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least $2$ numbers. Suppose the numbers are divided into $m$ groups, and the sum of the numbers in the $j$-th group is $s_j$. Bob's aim is to minimize the sum of the square of $s_j$, that is $$\sum_{j = 1}^{m} s_j^2.$$ Bob is puzzled by this hard problem. Could you please help him solve it? -----Input----- The first line contains an even integer $n$ ($2 \leq n \leq 3 \cdot 10^5$), denoting that there are $n$ integers on Bob's homework paper. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^4$), describing the numbers you need to deal with. -----Output----- A single line containing one integer, denoting the minimum of the sum of the square of $s_j$, which is $$\sum_{i = j}^{m} s_j^2,$$ where $m$ is the number of groups. -----Examples----- Input 4 8 5 2 3 Output 164 Input 6 1 1 1 2 2 2 Output 27 -----Note----- In the first sample, one of the optimal solutions is to divide those $4$ numbers into $2$ groups $\{2, 8\}, \{5, 3\}$. Thus the answer is $(2 + 8)^2 + (5 + 3)^2 = 164$. In the second sample, one of the optimal solutions is to divide those $6$ numbers into $3$ groups $\{1, 2\}, \{1, 2\}, \{1, 2\}$. Thus the answer is $(1 + 2)^2 + (1 + 2)^2 + (1 + 2)^2 = 27$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points A and B on the plane are 4-connected if and only if: the Euclidean distance between A and B is one unit and neither A nor B is blocked; or there is some integral point C, such that A is 4-connected with C, and C is 4-connected with B. Let's assume that the plane doesn't contain blocked points. Consider all the integral points of the plane whose Euclidean distance from the origin is no more than n, we'll name these points special. Chubby Yang wants to get the following property: no special point is 4-connected to some non-special point. To get the property she can pick some integral points of the plane and make them blocked. What is the minimum number of points she needs to pick? -----Input----- The first line contains an integer n (0 ≤ n ≤ 4·10^7). -----Output----- Print a single integer — the minimum number of points that should be blocked. -----Examples----- Input 1 Output 4 Input 2 Output 8 Input 3 Output 16 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$). Note that after this operation string $s$ doesn't change. Calculate the minimum number of such operations to turn string $z$ into string $t$. -----Input----- The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases. The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters. The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$. -----Output----- For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$. -----Example----- Input 3 aabce ace abacaba aax ty yyt Output 1 -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. Two bears are playing tic-tac-toe via mail. It's boring for them to play usual tic-tac-toe game, so they are a playing modified version of this game. Here are its rules. The game is played on the following field. [Image] Players are making moves by turns. At first move a player can put his chip in any cell of any small field. For following moves, there are some restrictions: if during last move the opposite player put his chip to cell with coordinates (x_{l}, y_{l}) in some small field, the next move should be done in one of the cells of the small field with coordinates (x_{l}, y_{l}). For example, if in the first move a player puts his chip to lower left cell of central field, then the second player on his next move should put his chip into some cell of lower left field (pay attention to the first test case). If there are no free cells in the required field, the player can put his chip to any empty cell on any field. You are given current state of the game and coordinates of cell in which the last move was done. You should find all cells in which the current player can put his chip. A hare works as a postman in the forest, he likes to foul bears. Sometimes he changes the game field a bit, so the current state of the game could be unreachable. However, after his changes the cell where the last move was done is not empty. You don't need to find if the state is unreachable or not, just output possible next moves according to the rules. -----Input----- First 11 lines contains descriptions of table with 9 rows and 9 columns which are divided into 9 small fields by spaces and empty lines. Each small field is described by 9 characters without spaces and empty lines. character "x" (ASCII-code 120) means that the cell is occupied with chip of the first player, character "o" (ASCII-code 111) denotes a field occupied with chip of the second player, character "." (ASCII-code 46) describes empty cell. The line after the table contains two integers x and y (1 ≤ x, y ≤ 9). They describe coordinates of the cell in table where the last move was done. Rows in the table are numbered from up to down and columns are numbered from left to right. It's guaranteed that cell where the last move was done is filled with "x" or "o". Also, it's guaranteed that there is at least one empty cell. It's not guaranteed that current state of game is reachable. -----Output----- Output the field in same format with characters "!" (ASCII-code 33) on positions where the current player can put his chip. All other cells should not be modified. -----Examples----- Input ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... x.. ... ... ... ... ... ... ... ... ... ... 6 4 Output ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... x.. ... !!! ... ... !!! ... ... !!! ... ... Input xoo x.. x.. ooo ... ... ooo ... ... x.. x.. x.. ... ... ... ... ... ... x.. x.. x.. ... ... ... ... ... ... 7 4 Output xoo x!! x!! ooo !!! !!! ooo !!! !!! x!! x!! x!! !!! !!! !!! !!! !!! !!! x!! x!! x!! !!! !!! !!! !!! !!! !!! Input o.. ... ... ... ... ... ... ... ... ... xxx ... ... xox ... ... ooo ... ... ... ... ... ... ... ... ... ... 5 5 Output o!! !!! !!! !!! !!! !!! !!! !!! !!! !!! xxx !!! !!! xox !!! !!! ooo !!! !!! !!! !!! !!! !!! !!! !!! !!! !!! -----Note----- In the first test case the first player made a move to lower left cell of central field, so the second player can put a chip only to cells of lower left field. In the second test case the last move was done to upper left cell of lower central field, however all cells in upper left field are occupied, so the second player can put his chip to any empty cell. In the third test case the last move was done to central cell of central field, so current player can put his chip to any cell of central field, which is already occupied, so he can move anywhere. Pay attention that this state of the game is unreachable. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a string `S` and a character `C`, return an array of integers representing the shortest distance from the current character in `S` to `C`. ### Notes * All letters will be lowercase. * If the string is empty, return an empty array. * If the character is not present, return an empty array. ## Examples ```python shortest_to_char("lovecodewars", "e") == [3, 2, 1, 0, 1, 2, 1, 0, 1, 2, 3, 4] shortest_to_char("aaaabbbb", "b") == [4, 3, 2, 1, 0, 0, 0, 0] shortest_to_char("", "b") == [] shortest_to_char("abcde", "") == [] ``` ___ If you liked it, please rate :D Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}. The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet. Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal. -----Constraints----- - All values in input are integers. - 1 \leq A_{i, j} \leq 100 - A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2)) - 1 \leq N \leq 10 - 1 \leq b_i \leq 100 - b_i \neq b_j (i \neq j) -----Input----- Input is given from Standard Input in the following format: A_{1, 1} A_{1, 2} A_{1, 3} A_{2, 1} A_{2, 2} A_{2, 3} A_{3, 1} A_{3, 2} A_{3, 3} N b_1 \vdots b_N -----Output----- If we will have a bingo, print Yes; otherwise, print No. -----Sample Input----- 84 97 66 79 89 11 61 59 7 7 89 7 87 79 24 84 30 -----Sample Output----- Yes We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K. She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier. Constraints * 1 ≦ N < 10000 * 1 ≦ K < 10 * 0 ≦ D_1 < D_2 < … < D_K≦9 * \\{D_1,D_2,...,D_K\\} ≠ \\{1,2,3,4,5,6,7,8,9\\} Input The input is given from Standard Input in the following format: N K D_1 D_2 … D_K Output Print the amount of money that Iroha will hand to the cashier. Examples Input 1000 8 1 3 4 5 6 7 8 9 Output 2000 Input 9999 1 0 Output 9999 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night. In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the two hills there was no hill higher than any of the two. As for any two hills there are two different circle arcs connecting them, the signal was seen if the above mentioned condition was satisfied on at least one of the arcs. For example, for any two neighbouring watchmen it is true that the signal of one will be seen by the other. An important characteristics of this watch system was the amount of pairs of watchmen able to see each other's signals. You are to find this amount by the given heights of the hills. Input The first line of the input data contains an integer number n (3 ≤ n ≤ 106), n — the amount of hills around the capital. The second line contains n numbers — heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109. Output Print the required amount of pairs. Examples Input 5 1 2 4 5 3 Output 7 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n. Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold. For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey: * c = [1, 2, 4]; * c = [3, 5, 6, 8]; * c = [7] (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? Input The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland. The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city. Output Print one integer — the maximum beauty of a journey Tanya can choose. Examples Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 Note The optimal journey plan in the first example is c = [2, 4, 5]. The optimal journey plan in the second example is c = [1]. The optimal journey plan in the third example is c = [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. Problem N idols, numbered from 1 to n in order, are lined up in a row. Idle i can transmit information to idle i-1 and idle i + 1 in a unit time. However, idol 1 can transmit information only to idol 2, and idol n can transmit information only to idol n-1. At time 0, m idols with numbers a1, a2, ..., am have secret information. Find the minimum amount of time all idols can get confidential information. Constraints * 2 ≤ n ≤ 105 * 1 ≤ m ≤ n * 1 ≤ ai ≤ n * All ai values ​​are different * ai are given in ascending order Input The input is given in the following format. n m a1 a2 ... am Two integers n and m are given on the first line, separated by blanks. On the second line, m integers a1, a2, ..., am are given, separated by blanks. Output Outputs the minimum time that information is transmitted to all idles on one line. Examples Input 3 2 1 3 Output 1 Input 10 3 2 5 7 Output 3 Input 10 5 2 5 6 8 10 Output 1 Input 100000 1 1 Output 99999 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 JOI Pizza sells pizza home delivery along the d-meter-long ring road that runs through the city center. JOI Pizza has n stores S1, ..., Sn on the loop line. The head office is S1. The distance from S1 to Si when moving the loop line clockwise is set to di meters. D2 , ..., dn is an integer greater than or equal to 1 and less than or equal to d -1. D2, ..., dn are all different. Bake and deliver pizza at the shortest store. The location of the delivery destination is represented by an integer k that is greater than or equal to 0 and less than or equal to d -1. This means that the distance from the head office S1 to the delivery destination in the clockwise direction is k meters. Pizza delivery is done along the loop line and no other road is allowed. However, the loop line may move clockwise or counterclockwise. For example, if the location of the store and the location of the delivery destination are as shown in the figure below (this example corresponds to Example 1 of "I / O example"). <image> The store closest to the delivery destination 1 is S2, so the delivery is from store S2. At this time, the distance traveled from the store is 1. Also, the store closest to delivery destination 2 is S1 (main store), so store S1 (main store). ) To deliver to home. At this time, the distance traveled from the store is 2. Total length of the loop line d, Number of JOI pizza stores n, Number of orders m, N --1 integer representing a location other than the main store d2, ..., dn, Integer k1, .. representing the location of the delivery destination Given ., km, create a program to find the sum of all orders for each order by the distance traveled during delivery (ie, the distance from the nearest store to the delivery destination). input The input consists of multiple datasets. Each dataset is given in the following format. The first line is a positive integer d (2 ≤ d ≤ 1000000000 = 109) that represents the total length of the loop line, the second line is a positive integer n (2 ≤ n ≤ 100000) that represents the number of stores, and the third line is A positive integer m (1 ≤ m ≤ 10000) is written to represent the number of orders. The n --1 lines after the 4th line are integers d2, d3, ..., dn that represent the location of stores other than the main store. (1 ≤ di ≤ d -1) is written in this order, and the integers k1, k2, ..., km (0 ≤ ki ≤ d) representing the delivery destination location are in the m lines after the n + 3rd line. --1) are written in this order. Of the scoring data, for 40% of the points, n ≤ 10000 is satisfied. For 40% of the points, the total distance traveled and the value of d are both 1000000 or less. In the scoring data, the total distance traveled is 1000000000 = 109 or less. When d is 0, it indicates the end of input. The number of data sets does not exceed 10. output For each data set, one integer representing the total distance traveled during delivery is output on one line. Examples Input 8 3 2 3 1 4 6 20 4 4 12 8 16 7 7 11 8 0 Output 3 3 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. This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2. Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly $\frac{n}{k}$ times consecutively. In other words, array a is k-periodic, if it has period of length k. For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic. For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0. -----Input----- The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2), a_{i} is the i-th element of the array. -----Output----- Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0. -----Examples----- Input 6 2 2 1 2 2 2 1 Output 1 Input 8 4 1 1 2 1 1 1 2 1 Output 0 Input 9 3 2 1 1 1 2 1 1 1 2 Output 3 -----Note----- In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1]. In the second sample, the given array already is 4-periodic. In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: - He never travels a distance of more than L in a single day. - He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. -----Constraints----- - 2 \leq N \leq 10^5 - 1 \leq L \leq 10^9 - 1 \leq Q \leq 10^5 - 1 \leq x_i < x_2 < ... < x_N \leq 10^9 - x_{i+1} - x_i \leq L - 1 \leq a_j,b_j \leq N - a_j \neq b_j - N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. -----Partial Score----- - 200 points will be awarded for passing the test set satisfying N \leq 10^3 and Q \leq 10^3. -----Input----- The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q -----Output----- Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. -----Sample Input----- 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 -----Sample Output----- 4 2 1 2 For the 1-st query, he can travel from the 1-st hotel to the 8-th hotel in 4 days, as follows: - Day 1: Travel from the 1-st hotel to the 2-nd hotel. The distance traveled is 2. - Day 2: Travel from the 2-nd hotel to the 4-th hotel. The distance traveled is 10. - Day 3: Travel from the 4-th hotel to the 7-th hotel. The distance traveled is 6. - Day 4: Travel from the 7-th hotel to the 8-th hotel. The distance traveled is 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. PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of $6$ slices, medium ones consist of $8$ slices, and large pizzas consist of $10$ slices each. Baking them takes $15$, $20$ and $25$ minutes, respectively. Petya's birthday is today, and $n$ of his friends will come, so he decided to make an order from his favorite pizzeria. Petya wants to order so much pizza that each of his friends gets at least one slice of pizza. The cooking time of the order is the total baking time of all the pizzas in the order. Your task is to determine the minimum number of minutes that is needed to make pizzas containing at least $n$ slices in total. For example: if $12$ friends come to Petya's birthday, he has to order pizzas containing at least $12$ slices in total. He can order two small pizzas, containing exactly $12$ slices, and the time to bake them is $30$ minutes; if $15$ friends come to Petya's birthday, he has to order pizzas containing at least $15$ slices in total. He can order a small pizza and a large pizza, containing $16$ slices, and the time to bake them is $40$ minutes; if $300$ friends come to Petya's birthday, he has to order pizzas containing at least $300$ slices in total. He can order $15$ small pizzas, $10$ medium pizzas and $13$ large pizzas, in total they contain $15 \cdot 6 + 10 \cdot 8 + 13 \cdot 10 = 300$ slices, and the total time to bake them is $15 \cdot 15 + 10 \cdot 20 + 13 \cdot 25 = 750$ minutes; if only one friend comes to Petya's birthday, he can order a small pizza, and the time to bake it is $15$ minutes. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. Each testcase consists of a single line that contains a single integer $n$ ($1 \le n \le 10^{16}$) — the number of Petya's friends. -----Output----- For each testcase, print one integer — the minimum number of minutes that is needed to bake pizzas containing at least $n$ slices in total. -----Examples----- Input 6 12 15 300 1 9999999999999999 3 Output 30 40 750 15 25000000000000000 15 -----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. Chef and his girlfriend are going to have a promenade. They are walking along the straight road which consists of segments placed one by one. Before walking Chef and his girlfriend stay at the beginning of the first segment, they want to achieve the end of the last segment. There are few problems: - At the beginning Chef should choose constant integer - the velocity of mooving. It can't be changed inside one segment. - The velocity should be decreased by at least 1 after achieving the end of some segment. - There is exactly one shop on each segment. Each shop has an attractiveness. If it's attractiveness is W and Chef and his girlfriend move with velocity V then if V < W girlfriend will run away into the shop and the promenade will become ruined. Chef doesn't want to lose her girl in such a way, but he is an old one, so you should find the minimal possible velocity at the first segment to satisfy all conditions. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N denoting the number of segments. The second line contains N space-separated integers W1, W2, ..., WN denoting the attractiveness of shops. -----Output----- - For each test case, output a single line containing the minimal possible velocity at the beginning. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 10^5 - 1 ≤ Wi ≤ 10^6 -----Example----- Input: 2 5 6 5 4 3 2 5 3 4 3 1 1 Output: 6 5 -----Explanation----- Example case 1. If we choose velocity 6, on the first step we have 6 >= 6 everything is OK, then we should decrease the velocity to 5 and on the 2nd segment we'll receive 5 >= 5, again OK, and so on. Example case 2. If we choose velocity 4, the promanade will be ruined on the 2nd step (we sould decrease our velocity, so the maximal possible will be 3 which is less than 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. Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? Input The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn't contain any leading zeroes. Output Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. Examples Input 0 Output 0 Input 10 Output 1 Input 991 Output 3 Note In the first sample the number already is one-digit — Herald can't cast a spell. The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once. The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team. The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team. -----Input----- The first line contains single integer n (2 ≤ n ≤ 2·10^5) — the number of groups. The second line contains a sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2), where a_{i} is the number of people in group i. -----Output----- Print the maximum number of teams of three people the coach can form. -----Examples----- Input 4 1 1 2 1 Output 1 Input 2 2 2 Output 0 Input 7 2 2 2 1 1 1 1 Output 3 Input 3 1 1 1 Output 1 -----Note----- In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups. In the second example he can't make a single team. In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person). Read the inputs from stdin solve the problem and write the answer to stdout (do 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 list/array which contains only integers (positive and negative). Your job is to sum only the numbers that are the same and consecutive. The result should be one list. Extra credit if you solve it in one line. You can assume there is never an empty list/array and there will always be an integer. Same meaning: 1 == 1 1 != -1 #Examples: ``` [1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1] """So as you can see sum of consecutives 1 is 1 sum of 3 consecutives 4 is 12 sum of 0... and sum of 2 consecutives 3 is 6 ...""" [1,1,7,7,3] # should return [2,14,3] [-5,-5,7,7,12,0] # should return [-10,14,12,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. -----Input----- The input contains a single integer a (10 ≤ a ≤ 999). -----Output----- Output 0 or 1. -----Examples----- Input 13 Output 1 Input 927 Output 1 Input 48 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. In the board game Talisman, when two players enter combat the outcome is decided by a combat score, equal to the players power plus any modifiers plus the roll of a standard 1-6 dice. The player with the highest combat score wins and the opposing player loses a life. In the case of a tie combat ends with neither player losing a life. For example: ``` Player 1: 5 Power, 0 Modifier Player 2: 3 Power, 2 Modifier Player 1 rolls a 4, Player 2 rolls a 2. (5 + 0 + 4) -> (3 + 2 + 2) Player 1 wins (9 > 7) ``` Your task is to write a method that calculates the required roll for the player to win. The player and enemy stats are given as an array in the format: ```python [power, modifier] ``` For example for the examples used above the stats would be given as: ```python get_required([5, 0], [3, 2]) # returns 'Random' ``` If the player has at least 6 more power (including modifiers) than the enemy they automatically wins the fight, as the enemy's combat score couldn't possibly exceed the player's. In this instance the method should return "Auto-win". For example: ```python get_required([9, 0], [2, 1]) # returns 'Auto-win' as the enemy can't possibly win ``` If the enemy has at least 6 more power (including modifiers) than the player they automatically wins the fight, as the player's combat score couldn't possibly exceed the enemy's. In this instance the method should return "Auto-lose". For example: ```python get_required([2, 1], [9, 0]) # returns 'Auto-lose' as the player can't possibly win ``` If the player and enemy have the same power (including modifiers) the outcome is purely down to the dice roll, and hence would be considered completely random. In this instance the method should return "Random". For example (as above): ```python get_required([5, 0], [3, 2]) # returns 'Random' as it is purely down to the dice roll ``` If the player has greater power than the enemy (including modifiers) the player could guarantee a win by rolling a high enough number on the dice. In this instance the method should return a range equal to the numbers which would guarantee victory for the player. ```python get_required([6, 0], [2, 2]) # returns '(5..6)' as rolling a 5 or 6 would mean the enemy could not win get_required([7, 1], [2, 2]) # returns '(3..6)' as rolling anything 3 through 6 would mean the enemy could not win ``` If the player has less power than the enemy (including modifiers) the player can only win if the enemy rolls a low enough number, providing they then roll a high enough number. In this instance the method should return a range equal to the numbers which would allow the player a chance to win. ```python get_required([4, 0], [6, 0]) # returns '(1..3)' as this would be the only outcome for which the player could still win get_required([1, 1], [6, 0]) # returns '(1..1)' as this would be the only outcome for which the player could still win ``` If the better case scenario for the player is to hope for a tie, then return `"Pray for a tie!"`. ```python get_required([7, 2], [6, 8]) # returns "Pray for a tie!" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ignoring the air resistance, velocity of a freely falling object $v$ after $t$ seconds and its drop $y$ in $t$ seconds are represented by the following formulas: $ v = 9.8 t $ $ y = 4.9 t^2 $ A person is trying to drop down a glass ball and check whether it will crack. Your task is to write a program to help this experiment. You are given the minimum velocity to crack the ball. Your program should print the lowest possible floor of a building to crack the ball. The height of the $N$ floor of the building is defined by $5 \times N - 5$. Input The input consists of multiple datasets. Each dataset, a line, consists of the minimum velocity v (0 < v < 200) to crack the ball. The value is given by a decimal fraction, with at most 4 digits after the decimal point. The input ends with EOF. The number of datasets is less than or equal to 50. Output For each dataset, print the lowest possible floor where the ball cracks. Example Input 25.4 25.4 Output 8 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. No Story No Description Only by Thinking and Testing Look at the results of the testcases, and guess the code! --- ## Series: 01. [A and B?](http://www.codewars.com/kata/56d904db9963e9cf5000037d) 02. [Incomplete string](http://www.codewars.com/kata/56d9292cc11bcc3629000533) 03. [True or False](http://www.codewars.com/kata/56d931ecc443d475d5000003) 04. [Something capitalized](http://www.codewars.com/kata/56d93f249c844788bc000002) 05. [Uniq or not Uniq](http://www.codewars.com/kata/56d949281b5fdc7666000004) 06. [Spatiotemporal index](http://www.codewars.com/kata/56d98b555492513acf00077d) 07. [Math of Primary School](http://www.codewars.com/kata/56d9b46113f38864b8000c5a) 08. [Math of Middle school](http://www.codewars.com/kata/56d9c274c550b4a5c2000d92) 09. [From nothingness To nothingness](http://www.codewars.com/kata/56d9cfd3f3928b4edd000021) 10. [Not perfect? Throw away!](http://www.codewars.com/kata/56dae2913cb6f5d428000f77) 11. [Welcome to take the bus](http://www.codewars.com/kata/56db19703cb6f5ec3e001393) 12. [A happy day will come](http://www.codewars.com/kata/56dc41173e5dd65179001167) 13. [Sum of 15(Hetu Luosliu)](http://www.codewars.com/kata/56dc5a773e5dd6dcf7001356) 14. [Nebula or Vortex](http://www.codewars.com/kata/56dd3dd94c9055a413000b22) 15. [Sport Star](http://www.codewars.com/kata/56dd927e4c9055f8470013a5) 16. [Falsetto Rap Concert](http://www.codewars.com/kata/56de38c1c54a9248dd0006e4) 17. [Wind whispers](http://www.codewars.com/kata/56de4d58301c1156170008ff) 18. [Mobile phone simulator](http://www.codewars.com/kata/56de82fb9905a1c3e6000b52) 19. [Join but not join](http://www.codewars.com/kata/56dfce76b832927775000027) 20. [I hate big and small](http://www.codewars.com/kata/56dfd5dfd28ffd52c6000bb7) 21. [I want to become diabetic ;-)](http://www.codewars.com/kata/56e0e065ef93568edb000731) 22. [How many blocks?](http://www.codewars.com/kata/56e0f1dc09eb083b07000028) 23. [Operator hidden in a string](http://www.codewars.com/kata/56e1161fef93568228000aad) 24. [Substring Magic](http://www.codewars.com/kata/56e127d4ef93568228000be2) 25. [Report about something](http://www.codewars.com/kata/56eccc08b9d9274c300019b9) 26. [Retention and discard I](http://www.codewars.com/kata/56ee0448588cbb60740013b9) 27. [Retention and discard II](http://www.codewars.com/kata/56eee006ff32e1b5b0000c32) 28. [How many "word"?](http://www.codewars.com/kata/56eff1e64794404a720002d2) 29. [Hail and Waterfall](http://www.codewars.com/kata/56f167455b913928a8000c49) 30. [Love Forever](http://www.codewars.com/kata/56f214580cd8bc66a5001a0f) 31. [Digital swimming pool](http://www.codewars.com/kata/56f25b17e40b7014170002bd) 32. [Archery contest](http://www.codewars.com/kata/56f4202199b3861b880013e0) 33. [The repair of parchment](http://www.codewars.com/kata/56f606236b88de2103000267) 34. [Who are you?](http://www.codewars.com/kata/56f6b4369400f51c8e000d64) 35. [Safe position](http://www.codewars.com/kata/56f7eb14f749ba513b0009c3) --- ## Special recommendation Another series, innovative and interesting, medium difficulty. People who like challenges, can try these kata: * [Play Tetris : Shape anastomosis](http://www.codewars.com/kata/56c85eebfd8fc02551000281) * [Play FlappyBird : Advance Bravely](http://www.codewars.com/kata/56cd5d09aa4ac772e3000323) Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely — the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $1$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output. There are four types of logical elements: AND ($2$ inputs), OR ($2$ inputs), XOR ($2$ inputs), NOT ($1$ input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input. For each input, determine what the output will be if Natasha changes this input. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 10^6$) — the number of vertices in the graph (both inputs and elements). The $i$-th of the next $n$ lines contains a description of $i$-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vertex is "IN", then the value of this input follows ($0$ or $1$), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have $2$ inputs, whereas "NOT" has $1$ input. The vertices are numbered from one. It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex $1$. -----Output----- Print a string of characters '0' and '1' (without quotes) — answers to the problem for each input in the ascending order of their vertex indices. -----Example----- Input 10 AND 9 4 IN 1 IN 1 XOR 6 5 AND 3 7 IN 0 NOT 10 IN 1 IN 1 AND 2 8 Output 10110 -----Note----- The original scheme from the example (before the input is changed): [Image] Green indicates bits '1', yellow indicates bits '0'. If Natasha changes the input bit $2$ to $0$, then the output will be $1$. If Natasha changes the input bit $3$ to $0$, then the output will be $0$. If Natasha changes the input bit $6$ to $1$, then the output will be $1$. If Natasha changes the input bit $8$ to $0$, then the output will be $1$. If Natasha changes the input bit $9$ to $0$, then the output will be $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. It is known that passages in Singer house are complex and intertwined. Let's define a Singer k-house as a graph built by the following process: take complete binary tree of height k and add edges from each vertex to all its successors, if they are not yet present. <image> Singer 4-house Count the number of non-empty paths in Singer k-house which do not pass the same vertex twice. Two paths are distinct if the sets or the orders of visited vertices are different. Since the answer can be large, output it modulo 109 + 7. Input The only line contains single integer k (1 ≤ k ≤ 400). Output Print single integer — the answer for the task modulo 109 + 7. Examples Input 2 Output 9 Input 3 Output 245 Input 20 Output 550384565 Note There are 9 paths in the first example (the vertices are numbered on the picture below): 1, 2, 3, 1-2, 2-1, 1-3, 3-1, 2-1-3, 3-1-2. <image> Singer 2-house Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. This morning, Roman woke up and opened the browser with $n$ opened tabs numbered from $1$ to $n$. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them. He decided to accomplish this by closing every $k$-th ($2 \leq k \leq n - 1$) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be $b$) and then close all tabs with numbers $c = b + i \cdot k$ that satisfy the following condition: $1 \leq c \leq n$ and $i$ is an integer (it may be positive, negative or zero). For example, if $k = 3$, $n = 14$ and Roman chooses $b = 8$, then he will close tabs with numbers $2$, $5$, $8$, $11$ and $14$. After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it $e$) and the amount of remaining social network tabs ($s$). Help Roman to calculate the maximal absolute value of the difference of those values $|e - s|$ so that it would be easy to decide what to do next. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k < n \leq 100$) — the amount of tabs opened currently and the distance between the tabs closed. The second line consists of $n$ integers, each of them equal either to $1$ or to $-1$. The $i$-th integer denotes the type of the $i$-th tab: if it is equal to $1$, this tab contains information for the test, and if it is equal to $-1$, it's a social network tab. -----Output----- Output a single integer — the maximum absolute difference between the amounts of remaining tabs of different types $|e - s|$. -----Examples----- Input 4 2 1 1 -1 1 Output 2 Input 14 3 -1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1 Output 9 -----Note----- In the first example we can choose $b = 1$ or $b = 3$. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, $e = 2$ and $s = 0$ and $|e - s| = 2$. In the second example, on the contrary, we can leave opened only tabs that have social networks opened in 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. One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of n brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly: It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains). There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false. If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid. -----Input----- The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n, a ≠ b). -----Output----- The output consists of one line, containing either yes or no depending on whether the nervous system is valid. -----Examples----- Input 4 4 1 2 2 3 3 1 4 1 Output no Input 6 5 1 2 2 3 3 4 4 5 3 6 Output yes Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are standing near a very strange machine. If you put C cents in the machine, the remaining money in your purse will transform in an unusual way. If you have A dollars and B cents remaining in your purse after depositing the C cents, then after the transformation you will have B dollars and A cents. You can repeat this procedure as many times as you want unless you don't have enough money for the machine. If at any point C > B and A > 0, then the machine will allow you to break one of the A dollars into 100 cents so you can place C cents in the machine. The machine will not allow you to exchange a dollar for 100 cents if B >= C. Of course, you want to do this to maximize your profit. For example if C=69 and you have 9 dollars and 77 cents then after you put 69 cents in the machine you will have 8 dollars and 9 cents (9.77 --> 9.08 --> 8.09). But I should warn you that you can't cheat. If you try to throw away 9 cents before the transformation (in order to obtain 99 dollars and 8 cents after), the machine will sense you are cheating and take away all of your money. You need to know how many times you should do this transformation in order to make a maximum profit. Since you are very busy man, you want to obtain the maximum possible profit in the minimum amount of time. -----Input----- The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains three nonnegative integers A, B and C where A, B, C < 100. It means that you have A dollars and B cents in your purse and you need to put C cents in the machine to make the transformation. -----Output----- For each test case, output a single line containing the minimal number of times you should do this transformation in order to make a maximal profit. It is guaranteed that the answer is less than 10000. -----Example----- Input: 2 9 77 69 98 99 69 Output: 4 0 -----Explanation----- In the first test we have the following sequence: 9.77, 8.09, 40.07, 38.39, 70.37, 68.69, 0.68. After last step we have not enough money for further transformations. The maximal profit will be after 4 transformations. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event. As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows: * There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n. * The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament. * After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament. * The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament. You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a. Write the code that calculates for each knight, the name of the knight that beat him. Input The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ n; li ≤ xi ≤ ri) — the description of the i-th fight. It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle. Output Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0. Examples Input 4 3 1 2 1 1 3 3 1 4 4 Output 3 1 4 0 Input 8 4 3 5 4 3 7 6 2 8 8 1 8 1 Output 0 8 4 6 4 8 6 1 Note Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, 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. New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells. So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a_1, a_2, ..., a_{n} - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ a_{i} ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + a_{i}), and one can travel from cell i to cell (i + a_{i}) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + a_{i}) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ a_{i} ≤ n - i one can't leave the Line World using portals. Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system. -----Input----- The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 10^4) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to. The second line contains n - 1 space-separated integers a_1, a_2, ..., a_{n} - 1 (1 ≤ a_{i} ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World. -----Output----- If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO". -----Examples----- Input 8 4 1 2 1 2 1 2 1 Output YES Input 8 5 1 2 1 2 1 1 1 Output NO -----Note----- In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4. In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Americans are odd people: in their buildings, the first floor is actually the ground floor and there is no 13th floor (due to superstition). Write a function that given a floor in the american system returns the floor in the european system. With the 1st floor being replaced by the ground floor and the 13th floor being removed, the numbers move down to take their place. In case of above 13, they move down by two because there are two omitted numbers below them. Basements (negatives) stay the same as the universal level. [More information here](https://en.wikipedia.org/wiki/Storey#European_scheme) ## Examples ``` 1 => 0 0 => 0 5 => 4 15 => 13 -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. Example Input 5 Output 5 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string s of length n comes out from Tavas' mouth instead. [Image] Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on s. Malekas has a favorite string p. He determined all positions x_1 < x_2 < ... < x_{k} where p matches s. More formally, for each x_{i} (1 ≤ i ≤ k) he condition s_{x}_{i}s_{x}_{i} + 1... s_{x}_{i} + |p| - 1 = p is fullfilled. Then Malekas wrote down one of subsequences of x_1, x_2, ... x_{k} (possibly, he didn't write anything) on a piece of paper. Here a sequence b is a subsequence of sequence a if and only if we can turn a into b by removing some of its elements (maybe no one of them or all). After Tavas woke up, Malekas told him everything. He couldn't remember string s, but he knew that both p and s only contains lowercase English letters and also he had the subsequence he had written on that piece of paper. Tavas wonders, what is the number of possible values of s? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him. Answer can be very large, so Tavas wants you to print the answer modulo 10^9 + 7. -----Input----- The first line contains two integers n and m, the length of s and the length of the subsequence Malekas wrote down (1 ≤ n ≤ 10^6 and 0 ≤ m ≤ n - |p| + 1). The second line contains string p (1 ≤ |p| ≤ n). The next line contains m space separated integers y_1, y_2, ..., y_{m}, Malekas' subsequence (1 ≤ y_1 < y_2 < ... < y_{m} ≤ n - |p| + 1). -----Output----- In a single line print the answer modulo 1000 000 007. -----Examples----- Input 6 2 ioi 1 3 Output 26 Input 5 2 ioi 1 2 Output 0 -----Note----- In the first sample test all strings of form "ioioi?" where the question mark replaces arbitrary English letter satisfy. Here |x| denotes the length of string x. Please note that it's possible that there is no such string (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. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp loves ciphers. He has invented his own cipher called Right-Left. Right-Left cipher is used for strings. To encrypt the string $s=s_{1}s_{2} \dots s_{n}$ Polycarp uses the following algorithm: he writes down $s_1$, he appends the current word with $s_2$ (i.e. writes down $s_2$ to the right of the current result), he prepends the current word with $s_3$ (i.e. writes down $s_3$ to the left of the current result), he appends the current word with $s_4$ (i.e. writes down $s_4$ to the right of the current result), he prepends the current word with $s_5$ (i.e. writes down $s_5$ to the left of the current result), and so on for each position until the end of $s$. For example, if $s$="techno" the process is: "t" $\to$ "te" $\to$ "cte" $\to$ "cteh" $\to$ "ncteh" $\to$ "ncteho". So the encrypted $s$="techno" is "ncteho". Given string $t$ — the result of encryption of some string $s$. Your task is to decrypt it, i.e. find the string $s$. -----Input----- The only line of the input contains $t$ — the result of encryption of some string $s$. It contains only lowercase Latin letters. The length of $t$ is between $1$ and $50$, inclusive. -----Output----- Print such string $s$ that after encryption it equals $t$. -----Examples----- Input ncteho Output techno Input erfdcoeocs Output codeforces Input z Output z Read the inputs from stdin solve the problem and write the answer to stdout (do 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 exist two zeroes: +0 (or just 0) and -0. Write a function that returns `true` if the input number is -0 and `false` otherwise (`True` and `False` for Python). In JavaScript / TypeScript / Coffeescript the input will be a number. In Python / Java / C / NASM / Haskell / the input will be a float. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3. Let x_{i,j} defined as follows: * x_{1,j} := a_j \quad (1 \leq j \leq N) * x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i) Find x_{N,1}. Constraints * 2 \leq N \leq 10^6 * a_i = 1,2,3 (1 \leq i \leq N) Input Input is given from Standard Input in the following format: N a_1a_2\ldotsa_N Output Print x_{N,1}. Examples Input 4 1231 Output 1 Input 10 2311312312 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. In this Kata you need to write the method SharedBits that returns true if 2 integers share at least two '1' bits. For simplicity assume that all numbers are positive For example int seven = 7; //0111 int ten = 10; //1010 int fifteen = 15; //1111 SharedBits(seven, ten); //false SharedBits(seven, fifteen); //true SharedBits(ten, fifteen); //true - seven and ten share only a single '1' (at index 3) - seven and fifteen share 3 bits (at indexes 1, 2, and 3) - ten and fifteen share 2 bits (at indexes 0 and 2) Hint: you can do this with just string manipulation, but binary operators will make your life much easier. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 an integer N. Snuke will choose integers s_1, s_2, n_1, n_2, u_1, u_2, k_1, k_2, e_1, and e_2 so that all of the following six conditions will be satisfied: * 0 \leq s_1 < s_2 * 0 \leq n_1 < n_2 * 0 \leq u_1 < u_2 * 0 \leq k_1 < k_2 * 0 \leq e_1 < e_2 * s_1 + s_2 + n_1 + n_2 + u_1 + u_2 + k_1 + k_2 + e_1 + e_2 \leq N For every possible choice (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2), compute (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1), and find the sum of all computed values, modulo (10^{9} +7). Solve this problem for each of the T test cases given. Constraints * All values in input are integers. * 1 \leq T \leq 100 * 1 \leq N \leq 10^{9} Input Input is given from Standard Input in the following format: T \mathrm{case}_1 \vdots \mathrm{case}_T Each case is given in the following format: N Output Print T lines. The i-th line should contain the answer to the i-th test case. Example Input 4 4 6 10 1000000000 Output 0 11 4598 257255556 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems. Polycarp decided to store the hash of the password, generated by the following algorithm: take the password $p$, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain $p'$ ($p'$ can still be equal to $p$); generate two random strings, consisting of lowercase Latin letters, $s_1$ and $s_2$ (any of these strings can be empty); the resulting hash $h = s_1 + p' + s_2$, where addition is string concatenation. For example, let the password $p =$ "abacaba". Then $p'$ can be equal to "aabcaab". Random strings $s1 =$ "zyx" and $s2 =$ "kjh". Then $h =$ "zyxaabcaabkjh". Note that no letters could be deleted or added to $p$ to obtain $p'$, only the order could be changed. Now Polycarp asks you to help him to implement the password check module. Given the password $p$ and the hash $h$, check that $h$ can be the hash for the password $p$. Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a non-empty string $p$, consisting of lowercase Latin letters. The length of $p$ does not exceed $100$. The second line of each test case contains a non-empty string $h$, consisting of lowercase Latin letters. The length of $h$ does not exceed $100$. -----Output----- For each test case print the answer to it — "YES" if the given hash $h$ could be obtained from the given password $p$ or "NO" otherwise. -----Example----- Input 5 abacaba zyxaabcaabkjh onetwothree threetwoone one zzonneyy one none twenty ten Output YES YES NO YES NO -----Note----- The first test case is explained in the statement. In the second test case both $s_1$ and $s_2$ are empty and $p'=$ "threetwoone" is $p$ shuffled. In the third test case the hash could not be obtained from the password. In the fourth test case $s_1=$ "n", $s_2$ is empty and $p'=$ "one" is $p$ shuffled (even thought it stayed the same). In the fifth test case the hash could not be obtained from the password. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind. Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins. After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one. Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins. Help Roma find the maximum profit of the grind. Input The first line contains three integers n, m, and p (1 ≤ n, m, p ≤ 2 ⋅ 10^5) — the number of available weapons, armor sets and monsters respectively. The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 ≤ a_i ≤ 10^6, 1 ≤ ca_i ≤ 10^9) — the attack modifier and the cost of the weapon i. The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 ≤ b_j ≤ 10^6, 1 ≤ cb_j ≤ 10^9) — the defense modifier and the cost of the armor set j. The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 ≤ x_k, y_k ≤ 10^6, 1 ≤ z_k ≤ 10^3) — defense, attack and the number of coins of the monster k. Output Print a single integer — the maximum profit of the grind. Example Input 2 3 3 2 3 4 7 2 4 3 2 5 11 1 2 4 2 1 6 3 4 6 Output 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Your task is to perform a simple table calculation. Write a program which reads the number of rows r, columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column. Constraints * 1 ≤ r, c ≤ 100 * 0 ≤ an element of the table ≤ 100 Input In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters. Output Print the new table of (r+1) × (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table. Example Input 4 5 1 1 3 4 5 2 2 2 4 5 3 3 0 1 1 2 3 4 4 6 Output 1 1 3 4 5 14 2 2 2 4 5 15 3 3 0 1 1 8 2 3 4 4 6 19 8 9 9 13 17 56 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [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. The faculty of application management and consulting services (FAMCS) of the Berland State University (BSU) has always been popular among Berland's enrollees. This year, N students attended the entrance exams, but no more than K will enter the university. In order to decide who are these students, there are series of entrance exams. All the students with score strictly greater than at least (N-K) students' total score gets enrolled. In total there are E entrance exams, in each of them one can score between 0 and M points, inclusively. The first E-1 exams had already been conducted, and now it's time for the last tribulation. Sergey is the student who wants very hard to enter the university, so he had collected the information about the first E-1 from all N-1 enrollees (i.e., everyone except him). Of course, he knows his own scores as well. In order to estimate his chances to enter the University after the last exam, Sergey went to a fortune teller. From the visit, he learnt about scores that everyone except him will get at the last exam. Now he wants to calculate the minimum score he needs to score in order to enter to the university. But now he's still very busy with minimizing the amount of change he gets in the shops, so he asks you to help him. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains four space separated integers N, K, E, M denoting the number of students, the maximal number of students who'll get enrolled, the total number of entrance exams and maximal number of points for a single exam, respectively. The following N-1 lines will contain E integers each, where the first E-1 integers correspond to the scores of the exams conducted. The last integer corresponds to the score at the last exam, that was predicted by the fortune-teller. The last line contains E-1 integers denoting Sergey's score for the first E-1 exams. -----Output----- For each test case, output a single line containing the minimum score Sergey should get in the last exam in order to be enrolled. If Sergey doesn't have a chance to be enrolled, output "Impossible" (without quotes). -----Constraints----- - 1 ≤ T ≤ 5 - 1 ≤ K < N ≤ 104 - 1 ≤ M ≤ 109 - 1 ≤ E ≤ 4 -----Example----- Input:1 4 2 3 10 7 7 7 4 6 10 7 10 9 9 9 Output:4 -----Explanation----- Example case 1. If Sergey gets 4 points at the last exam, his score will be equal to 9+9+4=22. This will be the second score among all the enrollees - the first one will get 21, the second one will get 20 and the third will have the total of 26. Thus, Sergey will enter the university. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure: * Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j). * First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored. * Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell. <image> Now she is wondering how to determine the crop plants in some certain cells. Input In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4·104, 1 ≤ m ≤ 4·104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j). Output For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes. Examples Input 4 5 5 6 4 3 1 3 3 3 2 5 3 2 1 3 1 4 2 3 2 4 1 1 1 1 Output Waste Grapes Carrots Kiwis Carrots Carrots Note The sample corresponds to the figure 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. There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $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. Tic-tac-toe is a game in which you win when you put ○ and × alternately in the 3 × 3 squares and line up ○ or × in one of the vertical, horizontal, and diagonal lines (Fig.). 1 to Fig. 3) <image> | <image> | <image> --- | --- | --- Figure 1: ○ wins | Figure 2: × wins | Figure 3: Draw In tic-tac-toe, ○ and × alternately fill the squares, and the game ends when either one is lined up. Therefore, as shown in Fig. 4, it is impossible for both ○ and × to be aligned. No improbable aspect is entered. <image> --- Figure 4: Impossible phase Please create a program that reads the board of tic-tac-toe and outputs the result of victory or defeat. Input The input consists of multiple datasets. For each data set, one character string representing the board is given on one line. The character strings on the board are represented by o, x, and s in half-width lowercase letters for ○, ×, and blanks, respectively, and are arranged in the order of the squares in the figure below. <image> The number of datasets does not exceed 50. Output For each data set, output o in half-width lowercase letters if ○ wins, x in lowercase half-width letters if × wins, and d in lowercase half-width letters if it is a draw. Example Input ooosxssxs xoosxsosx ooxxxooxo Output o x d Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise. g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon. Note that the polygon is not necessarily convex. Constraints * 3 ≤ n ≤ 100 * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * No point of the polygon will occur more than once. * Two sides of the polygon can intersect only at a common endpoint. Input The entire input looks like: g (the sequence of the points of the polygon) q (the number of queris = the number of target points) 1st query 2nd query : qth query g is given by coordinates of the points p1,..., pn in the following format: n x1 y1 x2 y2 : xn yn The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them. Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y. Output For each query, print "2", "1" or "0". Example Input 4 0 0 3 1 2 3 0 3 3 2 1 0 2 3 2 Output 2 1 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large). Killjoy's account is already infected and has a rating equal to $x$. Its rating is constant. There are $n$ accounts except hers, numbered from $1$ to $n$. The $i$-th account's initial rating is $a_i$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed. Contests are regularly held on Codeforces. In each contest, any of these $n$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer. Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change. It can be proven that all accounts can be infected in some finite number of contests. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 100)$ — the number of test cases. The next $2t$ lines contain the descriptions of all test cases. The first line of each test case contains two integers $n$ and $x$ ($2 \le n \le 10^3$, $-4000 \le x \le 4000$) — the number of accounts on Codeforces and the rating of Killjoy's account. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ $(-4000 \le a_i \le 4000)$ — the ratings of other accounts. -----Output----- For each test case output the minimal number of contests needed to infect all accounts. -----Example----- Input 3 2 69 68 70 6 4 4 4 4 4 4 4 9 38 -21 83 50 -59 -77 15 -71 -78 20 Output 1 0 2 -----Note----- In the first test case it's possible to make all ratings equal to $69$. First account's rating will increase by $1$, and second account's rating will decrease by $1$, so the sum of all changes will be equal to zero. In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $4$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success? Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number. -----Input----- The single line contains integers n, m and k (1 ≤ n, m ≤ 5·10^5; 1 ≤ k ≤ n·m). -----Output----- Print the k-th largest number in a n × m multiplication table. -----Examples----- Input 2 2 2 Output 2 Input 2 3 4 Output 3 Input 1 10 5 Output 5 -----Note----- A 2 × 3 multiplication table looks like this: 1 2 3 2 4 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 a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation: you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$. For example, if $s=$"codeforces", then you can apply the following sequence of operations: $i=6 \Rightarrow s=$"codefrces"; $i=1 \Rightarrow s=$"odefrces"; $i=7 \Rightarrow s=$"odefrcs"; Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique. A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if: there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$; or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$. For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus". -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow. Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$). It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique. -----Examples----- Input 6 codeforces aezakmi abacaba convexhull swflldjgpaxs myneeocktxpqjpz Output odfrces ezakmi cba convexhul wfldjgpaxs myneocktxqjpz -----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. As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that <image> for some function <image>. (This equation should hold for any integer x in the range 0 to p - 1, inclusive.) It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 109 + 7. Input The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number. Output Print a single integer, the number of distinct functions f modulo 109 + 7. Examples Input 3 2 Output 3 Input 5 4 Output 25 Note In the first sample, p = 3 and k = 2. The following functions work: 1. f(0) = 0, f(1) = 1, f(2) = 2. 2. f(0) = 0, f(1) = 2, f(2) = 1. 3. f(0) = f(1) = f(2) = 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. From tomorrow, the long-awaited summer vacation will begin. So I decided to invite my friends to go out to the sea. However, many of my friends are shy. They would hate it if they knew that too many people would come with them. Besides, many of my friends want to stand out. They will probably hate it if they know that not many people come with them. Also, some of my friends are always shy, though they always want to stand out. They will surely hate if too many or too few people come with them. This kind of thing should be more fun to do in large numbers. That's why I want to invite as many friends as possible. However, it is not good to force a friend you dislike. How many friends can I invite up to? I'm not very good at this kind of problem that seems to use my head. So I have a request for you. If you don't mind, could you solve this problem for me? No, I'm not overdoing it. But if I'm slacking off, I'm very happy. Input N a1 b1 a2 b2 .. .. .. aN bN The integer N (1 ≤ N ≤ 100,000) is written on the first line of the input. This represents the number of friends. On the following N lines, the integer ai and the integer bi (2 ≤ ai ≤ bi ≤ 100,001) are written separated by blanks. The integers ai and bi written on the 1 + i line indicate that the i-th friend does not want to go to the sea unless the number of people going to the sea is ai or more and bi or less. Note that the number of people going to the sea includes "I". Output Output the maximum number of friends you can invite to the sea so that no friends dislike it. Examples Input 4 2 5 4 7 2 4 3 6 Output 3 Input 5 8 100001 7 100001 12 100001 8 100001 3 100001 Output 0 Input 6 2 9 4 8 6 7 6 6 5 7 2 100001 Output 5 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given two matrices A and B. Both have N rows and M columns. In the matrix A, numbers from 1 to MN have been written in row major order. Row major order numbers cells from left to right, and top to bottom. That is, 1 2 3 ... M A = M+1 M+2 M+3 ... 2M 2M+1 2M+2 2M+3 ... 3M . . . ... . . . . ... . (N-1)M+1 (N-1)M+2 (N-1)M+3 ... NM Similarly, in the matrix B, numbers from 1 to MN have been written in column major order. Column major order numbers cells from top to bottom and left to right. You are to count number of pairs (i,j) such that A_{i,j}=B_{i,j}. ------ Input ------ The input consists of multiple test cases. The first line of input contains a single integer T, the number of test cases. T test cases follow. Each test case is described by one line containing two space separated integers, N and M ------ Output ------ Output T lines, i^{th} line containing answer of the i^{th} test case. ------ Constraints ------ 1 ≤ T ≤ 10^{5} 1 ≤ N, M ≤ 10^{9} ----- Sample Input 1 ------ 1 4 5 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ For the first case two matrices look as follows: A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20B= 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 A1,1=B1,1A4,5=B4,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. Bob is a duck. He wants to get to Alice's nest, so that those two can duck! [Image] Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of $n$ segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. Each segment has a length in meters, and also terrain type: grass, water or lava. Bob has three movement types: swimming, walking and flying. He can switch between them or change his direction at any point in time (even when he is located at a non-integer coordinate), and doing so doesn't require any extra time. Bob can swim only on the water, walk only on the grass and fly over any terrain. Flying one meter takes $1$ second, swimming one meter takes $3$ seconds, and finally walking one meter takes $5$ seconds. Bob has a finite amount of energy, called stamina. Swimming and walking is relaxing for him, so he gains $1$ stamina for every meter he walks or swims. On the other hand, flying is quite tiring, and he spends $1$ stamina for every meter flown. Staying in place does not influence his stamina at all. Of course, his stamina can never become negative. Initially, his stamina is zero. What is the shortest possible time in which he can reach Alice's nest? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of segments of terrain. The second line contains $n$ integers $l_1, l_2, \dots, l_n$ ($1 \leq l_i \leq 10^{12}$). The $l_i$ represents the length of the $i$-th terrain segment in meters. The third line contains a string $s$ consisting of $n$ characters "G", "W", "L", representing Grass, Water and Lava, respectively. It is guaranteed that the first segment is not Lava. -----Output----- Output a single integer $t$ — the minimum time Bob needs to reach Alice. -----Examples----- Input 1 10 G Output 30 Input 2 10 10 WL Output 40 Input 2 1 2 WL Output 8 Input 3 10 10 10 GLW Output 80 -----Note----- In the first sample, Bob first walks $5$ meters in $25$ seconds. Then he flies the remaining $5$ meters in $5$ seconds. In the second sample, Bob first swims $10$ meters in $30$ seconds. Then he flies over the patch of lava for $10$ seconds. In the third sample, the water pond is much smaller. Bob first swims over the water pond, taking him $3$ seconds. However, he cannot fly over the lava just yet, as he only has one stamina while he needs two. So he swims back for half a meter, and then half a meter forward, taking him $3$ seconds in total. Now he has $2$ stamina, so he can spend $2$ seconds flying over the lava. In the fourth sample, he walks for $50$ seconds, flies for $10$ seconds, swims for $15$ seconds, and finally flies for $5$ seconds. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))": 1st bracket is paired with 8th, 2d bracket is paired with 3d, 3d bracket is paired with 2d, 4th bracket is paired with 7th, 5th bracket is paired with 6th, 6th bracket is paired with 5th, 7th bracket is paired with 4th, 8th bracket is paired with 1st. Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported: «L» — move the cursor one position to the left, «R» — move the cursor one position to the right, «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to). After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted). There are pictures illustrated several usages of operation "D" below. [Image] All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor. Polycarp is very proud of his development, can you implement the functionality of his editor? -----Input----- The first line contains three positive integers n, m and p (2 ≤ n ≤ 500 000, 1 ≤ m ≤ 500 000, 1 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even. It is followed by the string of n characters "(" and ")" forming the correct bracket sequence. Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty. -----Output----- Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence. -----Examples----- Input 8 4 5 (())()() RDLD Output () Input 12 5 3 ((()())(())) RRDLD Output (()(())) Input 8 8 8 (())()() LLLLLLDD Output ()() -----Note----- In the first sample the cursor is initially at position 5. Consider actions of the editor: command "R" — the cursor moves to the position 6 on the right; command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5; command "L" — the cursor moves to the position 4 on the left; command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1. Thus, the answer is equal to (). Read the inputs from stdin solve the problem and write the answer to stdout (do 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 system of pipes. It consists of two rows, each row consists of $n$ pipes. The top left pipe has the coordinates $(1, 1)$ and the bottom right — $(2, n)$. There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types: [Image] Types of pipes You can turn each of the given pipes $90$ degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types $1$ and $2$ can become each other and types $3, 4, 5, 6$ can become each other). You want to turn some pipes in a way that the water flow can start at $(1, 0)$ (to the left of the top left pipe), move to the pipe at $(1, 1)$, flow somehow by connected pipes to the pipe at $(2, n)$ and flow right to $(2, n + 1)$. Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes: [Image] Examples of connected pipes Let's describe the problem using some example: [Image] The first example input And its solution is below: [Image] The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at $(1, 2)$ $90$ degrees clockwise, the pipe at $(2, 3)$ $90$ degrees, the pipe at $(1, 6)$ $90$ degrees, the pipe at $(1, 7)$ $180$ degrees and the pipe at $(2, 7)$ $180$ degrees. Then the flow of water can reach $(2, n + 1)$ from $(1, 0)$. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ queries follow. Each query consists of exactly three lines. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of $n$ digits from $1$ to $6$ without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For the $i$-th query print the answer for it — "YES" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach $(2, n + 1)$ from $(1, 0)$, and "NO" otherwise. -----Example----- Input 6 7 2323216 1615124 1 3 4 2 13 24 2 12 34 3 536 345 2 46 54 Output YES YES YES NO YES NO -----Note----- The first query from the example is described in the problem 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. Takahashi is at an all-you-can-eat restaurant. The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i. The restaurant has the following rules: - You can only order one dish at a time. The dish ordered will be immediately served and ready to eat. - You cannot order the same kind of dish more than once. - Until you finish eating the dish already served, you cannot order a new dish. - After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served. Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant. What is the maximum possible happiness achieved by making optimal choices? -----Constraints----- - 2 \leq N \leq 3000 - 1 \leq T \leq 3000 - 1 \leq A_i \leq 3000 - 1 \leq B_i \leq 3000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N T A_1 B_1 : A_N B_N -----Output----- Print the maximum possible happiness Takahashi can achieve. -----Sample Input----- 2 60 10 10 100 100 -----Sample Output----- 110 By ordering the first and second dishes in this order, Takahashi's happiness will be 110. Note that, if we manage to order a dish in time, we can spend any amount of time to eat it. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward"). You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list? Input The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F". The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list. Output Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list. Examples Input FT 1 Output 2 Input FFFTFFF 2 Output 6 Note In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units. In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 A of length N. Find the maximum absolute difference of two elements (with different indices) in A. -----Constraints----- - 2 \leq N \leq 100 - 1 \leq A_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N -----Output----- Print the maximum absolute difference of two elements (with different indices) in A. -----Sample Input----- 4 1 4 6 3 -----Sample Output----- 5 The maximum absolute difference of two elements is A_3-A_1=6-1=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. # MOD 256 without the MOD operator The MOD-operator % (aka mod/modulus/remainder): ``` Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand. (Different behavior in Python!) ``` The short unbelievable mad story for this kata: I wrote a program and needed the remainder of the division by 256. And then it happened: The "5"/"%"-Key did not react. It must be broken! So I needed a way to: ``` Calculate the remainder of the division by 256 without the %-operator. ``` Also here some examples: ``` Input 254 -> Result 254 Input 256 -> Result 0 Input 258 -> Result 2 Input -258 -> Result -2 (in Python: Result: 254!) ``` It is always expected the behavior of the MOD-Operator of the language! The input number will always between -10000 and 10000. For some languages the %-operator will be blocked. If it is not blocked and you know how to block it, tell me and I will include it. For all, who say, this would be a duplicate: No, this is no duplicate! There are two katas, in that you have to write a general method for MOD without %. But this kata is only for MOD 256. And so you can create also other specialized solutions. ;-) Of course you can use the digit "5" in your solution. :-) I'm very curious for your solutions and the way you solve it. I found several interesting "funny" ways. Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata! Read the inputs from stdin solve the problem and write the answer to stdout (do 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 the Internet, data is divided into packets, and each packet is transferred to a destination via a relay device called a router. Each router determines the next router to forward from the destination described in the packet. In addition, a value called TTL (Time To Live) is added to the packet to prevent it from being forwarded between routers indefinitely. The router subtracts 1 from the TTL of the received packet, discards the packet if the result is 0, and forwards it to the next router otherwise. So I decided to create a program to help design the network. Create a program that takes the network connection information and the outbound packet information as input and displays the minimum number of routers that each packet goes through before arriving at the destination router. The network consists of multiple routers and cables connecting them as shown in the figure. However, keep in mind that each connection (cable) is unidirectional. An array of router numbers to which each router is directly connected is given as network connection information. If the number of routers is n, then each router is identified by an integer from 1 to n. If there are multiple routes from the source to the destination router, output the value with the smaller number of routers. If the packet does not reach the destination, output NA. For example, consider a network like the one shown below with 6 source routers and 5 destination routers. The shortest route is 6 → 1 → 5, and there are 3 routers to go through. In this case, the TTL is subtracted by routers 6 and 1, respectively, so the packet can be reached if the TTL at the time of transmission is 3 or more. The destination router does not need to subtract the TTL. Also, it is assumed that there is no packet whose source and destination are the same router. <image> Input The input is given in the following format: n r1 k1 t11 t12 ... t1 k1 r2 k2 t21 t22 ... t2 k2 :: rn kn tn1 tn2 ... tnkn p s1 d1 v1 s2 d2 v2 :: sp dp vp The first line gives the total number of routers n (n ≤ 100), and the following n lines give the connection information for the i-th router. The connection information is given the i-th router number ri, the number of routers directly connected to the i-th router ki, and the router numbers ti1, ti2, ... tiki that can be sent from the i-th router. The following line gives the number of packets p (p ≤ 1000), and the following p line gives the information of the i-th packet. The information in the packet is given the source router number si, the destination router number di, and the TTL value vi (0 ≤ vi ≤ 10000). Output For each packet, output the number of routers or NA to pass through on one line. Example Input 7 1 4 2 5 4 3 2 1 5 3 1 6 4 1 7 5 2 7 6 6 1 1 7 0 6 1 2 2 1 5 3 1 2 1 5 1 3 6 3 3 1 7 4 Output 2 2 NA 3 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. When registering in a social network, users are allowed to create their own convenient login to make it easier to share contacts, print it on business cards, etc. Login is an arbitrary sequence of lower and uppercase latin letters, digits and underline symbols («_»). However, in order to decrease the number of frauds and user-inattention related issues, it is prohibited to register a login if it is similar with an already existing login. More precisely, two logins s and t are considered similar if we can transform s to t via a sequence of operations of the following types: transform lowercase letters to uppercase and vice versa; change letter «O» (uppercase latin letter) to digit «0» and vice versa; change digit «1» (one) to any letter among «l» (lowercase latin «L»), «I» (uppercase latin «i») and vice versa, or change one of these letters to other. For example, logins «Codeforces» and «codef0rces» as well as «OO0OOO00O0OOO0O00OOO0OO_lol» and «OO0OOO0O00OOO0O00OO0OOO_1oI» are considered similar whereas «Codeforces» and «Code_forces» are not. You're given a list of existing logins with no two similar amonst and a newly created user login. Check whether this new login is similar with any of the existing ones. -----Input----- The first line contains a non-empty string s consisting of lower and uppercase latin letters, digits and underline symbols («_») with length not exceeding 50  — the login itself. The second line contains a single integer n (1 ≤ n ≤ 1 000) — the number of existing logins. The next n lines describe the existing logins, following the same constraints as the user login (refer to the first line of the input). It's guaranteed that no two existing logins are similar. -----Output----- Print «Yes» (without quotes), if user can register via this login, i.e. none of the existing logins is similar with it. Otherwise print «No» (without quotes). -----Examples----- Input 1_wat 2 2_wat wat_1 Output Yes Input 000 3 00 ooA oOo Output No Input _i_ 3 __i_ _1_ I Output No Input La0 3 2a0 La1 1a0 Output No Input abc 1 aBc Output No Input 0Lil 2 LIL0 0Ril Output Yes -----Note----- In the second sample case the user wants to create a login consisting of three zeros. It's impossible due to collision with the third among the existing. In the third sample case the new login is similar with the second one. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a function that will return ```true``` if the input is in the following date time format ```01-09-2016 01:20``` and ```false``` if it is not. This Kata has been inspired by the Regular Expressions chapter from the book Eloquent JavaScript. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Statement ------ Past In the year of 2048, the Virtual Reality Massively Multiplayer Online Role-Playing Game (VRMMORPG), Code Art Online (CAO), is released. With the Chef Gear, a virtual reality helmet that stimulates the user's five senses via their brain, players can experience and control their in-game characters with their minds. On August the 2nd, 2048, all the players log in for the first time, and subsequently discover that they are unable to log out. They are then informed by Code Master, the creator of CAO, that if they wish to be free, they must reach the second stage of the game. Kirito is a known star player of CAO. You have to help him log out. Present Stage 1 A map is described by a 2D grid of cells. Each cell is either labelled as a # or a ^. # denotes a wall. A monster exists in a cell if the cell is not a wall and the cell is a centre of Prime-Cross (CPC). Let L be the number of contiguous ^ to the left of X, in the same row as X. R be the number of contiguous ^ to the right of X, in the same row as X. T be the number of contiguous ^ above X, in the same column as X. B be the number of contiguous ^ below X, in the same column as X. A cell X is said to be a CPC if there exists a prime number P such that P ≤ minimum of [L, R, T, B]. Note: While computing L, R, T, B for a cell X, you should not count the ^ of the cell X. Given a map, you have to tell Kirito the number of cells where monsters exist. Future If you are done with this task, go help Kirito with Stage 2 :-) ------ 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 case starts with a line containing two space separated integers R, C denoting the number of rows and columns in the map respectively. The next R lines contain C characters each, describing the map. ------ Output ------ For each test case, output a single line containing the number of cells where monsters exist. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ R ≤ 50$ $1 ≤ C ≤ 50$ ----- Sample Input 1 ------ 2 5 5 ^^^^^ ^^^^^ ^^^^# ^^^^^ ^^^^^ 5 7 ^^#^^^^ ^^#^#^# #^^^^^^ ^^#^^#^ ^^^^^^^ ----- Sample Output 1 ------ 0 1 ----- explanation 1 ------ Example case 1. There is no cell for which minimum of L, R, T, B is greater than some prime P. Example case 2. The cell at [3, 4], (1-based indexing) is the only CPC. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a line with 1000 cells numbered from 1 to 1000 from left to right and N coins placed on it. Coin i is placed at cell X_{i}, and no two coins are placed at the same cell. Bob would like to move the coins to the N leftmost cells of the line. To do this, he is allowed to take a coin from any cell T and move it to cell T-j, where j is an integer between 1 and K, inclusive. This action is possible only if: cell T-j actually exists and doesn't contain a coin; each of the cells T-j+1, ..., T-1 contains a coin. One coin movement takes exactly one second. Find the smallest time in which Bob can achieve his goal. ------ Input ------ The first line of the input file contains one integer T -- the number of test cases (no more than 10). Then T test cases follow, and every test case is described by two lines: the first of them contains two integers N and K (1 ≤ N, K ≤ 1000), the second of them contains N integers X_{1}, ..., X_{N} in strictly increasing order (1 ≤ X_{i} ≤ 1000). ------ Output ------ For each test case output one line containing the requested minimal time for Bob to put all the coins to the left side of the line. ----- Sample Input 1 ------ 2 3 2 2 4 7 5 3 1 2 3 4 5 ----- Sample Output 1 ------ 5 0 ----- explanation 1 ------ In the first example Bob can move the coin from cell 7 consequently to cells 6, 5, 3 and 1, then move the coin from cell 4 to cell 3. In the second example there is nothing to move. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work. Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well *Cinquebonacci* would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on. Well, guess what? You have to build a Xbonacci function that takes a **signature** of X elements *- and remember each next element is the sum of the last X elements -* and returns the first **n** elements of the so seeded sequence. ``` xbonacci {1,1,1,1} 10 = {1,1,1,1,4,7,13,25,49,94} xbonacci {0,0,0,0,1} 10 = {0,0,0,0,1,1,2,4,8,16} xbonacci {1,0,0,0,0,0,1} 10 = {1,0,0,0,0,0,1,2,3,6} xbonacci {1,1} produces the Fibonacci sequence ``` Read the inputs from stdin solve the problem and write the answer to stdout (do 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 $n$. Pair the integers $1$ to $2n$ (i.e. each integer should be in exactly one pair) so that each sum of matched pairs is consecutive and distinct. Formally, let $(a_i, b_i)$ be the pairs that you matched. $\{a_1, b_1, a_2, b_2, \ldots, a_n, b_n\}$ should be a permutation of $\{1, 2, \ldots, 2n\}$. Let the sorted list of $\{a_1+b_1, a_2+b_2, \ldots, a_n+b_n\}$ be $s_1 < s_2 < \ldots < s_n$. We must have $s_{i+1}-s_i = 1$ for $1 \le i \le n - 1$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. For each test case, a single integer $n$ ($1 \leq n \leq 10^5$) is given. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case, if it is impossible to make such a pairing, print "No". Otherwise, print "Yes" followed by $n$ lines. At each line, print two integers that are paired. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. If there are multiple solutions, print any. -----Examples----- Input 4 1 2 3 4 Output Yes 1 2 No Yes 1 6 3 5 4 2 No -----Note----- For the third test case, each integer from $1$ to $6$ appears once. The sums of matched pairs are $4+2=6$, $1+6=7$, and $3+5=8$, which are consecutive and distinct. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none — in writing a composition. As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one? Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a_{i} characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line). Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key: "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1; "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1; "Right": if the cursor can move to the right in this line (c < a_{r} + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key; "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key. You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r_1, c_1) to position (r_2, c_2). -----Input----- The first line of the input contains an integer n (1 ≤ n ≤ 100) — the number of lines in the file. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5), separated by single spaces. The third line contains four integers r_1, c_1, r_2, c_2 (1 ≤ r_1, r_2 ≤ n, 1 ≤ c_1 ≤ a_{r}_1 + 1, 1 ≤ c_2 ≤ a_{r}_2 + 1). -----Output----- Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (r_1, c_1) to position (r_2, c_2). -----Examples----- Input 4 2 1 6 4 3 4 4 2 Output 3 Input 4 10 5 6 4 1 11 4 2 Output 6 Input 3 10 1 10 1 10 1 1 Output 3 -----Note----- In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table. 123 12 123s567 1t345 One of the possible answers in the given sample is: "Left", "Down", "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. A number `n` is called `prime happy` if there is at least one prime less than `n` and the `sum of all primes less than n` is evenly divisible by `n`. Write `isPrimeHappy(n)` which returns `true` if `n` is `prime happy` else `false`. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. I have n tickets for a train with a rabbit. Each ticket is numbered from 0 to n − 1, and you can use the k ticket to go to p⋅ak + q⋅bk station. Rabbit wants to go to the all-you-can-eat carrot shop at the station m station ahead of the current station, but wants to walk as short as possible. The stations are lined up at regular intervals. When using, how many stations can a rabbit walk to reach the store? Input 1 ≤ n, m, a, b, p, q ≤ 1 000 000 000 000 (integer) Output Output the number of rabbits that can reach the store on foot at the minimum number of stations in one line. Examples Input 6 200 2 3 4 5 Output 1 Input 6 1 2 3 4 5 Output 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ k) denoting marks received by Noora before Leha's hack. -----Output----- Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. -----Examples----- Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 -----Note----- Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to $\frac{8 + 9 + 10 + 10 + 10 + 10}{6} = \frac{57}{6} = 9.5$. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.) The state of each piece is represented by a string S of length N. If S_i=`B`, the i-th piece from the left is showing black; If S_i=`W`, the i-th piece from the left is showing white. Consider performing the following operation: * Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black. Find the maximum possible number of times this operation can be performed. Constraints * 1 \leq |S| \leq 2\times 10^5 * S_i=`B` or `W` Input Input is given from Standard Input in the following format: S Output Print the maximum possible number of times the operation can be performed. Examples Input BBW Output 2 Input BWBWBW Output 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 a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. Also, each of these vertices and edges has a specified weight. Vertex i has a weight of X_i; Edge i has a weight of Y_i and connects Vertex A_i and B_i. We would like to remove zero or more edges so that the following condition is satisfied: * For each edge that is not removed, the sum of the weights of the vertices in the connected component containing that edge, is greater than or equal to the weight of that edge. Find the minimum number of edges that need to be removed. Constraints * 1 \leq N \leq 10^5 * N-1 \leq M \leq 10^5 * 1 \leq X_i \leq 10^9 * 1 \leq A_i < B_i \leq N * 1 \leq Y_i \leq 10^9 * (A_i,B_i) \neq (A_j,B_j) (i \neq j) * The given graph is connected. * All values in input are integers. Input Input is given from Standard Input in the following format: N M X_1 X_2 ... X_N A_1 B_1 Y_1 A_2 B_2 Y_2 : A_M B_M Y_M Output Find the minimum number of edges that need to be removed. Examples Input 4 4 2 3 5 7 1 2 7 1 3 9 2 3 12 3 4 18 Output 2 Input 6 10 4 4 1 1 1 7 3 5 19 2 5 20 4 5 8 1 6 16 2 3 9 3 6 16 3 4 1 2 6 20 2 4 19 1 2 9 Output 4 Input 10 9 81 16 73 7 2 61 86 38 90 28 6 8 725 3 10 12 1 4 558 4 9 615 5 6 942 8 9 918 2 7 720 4 7 292 7 10 414 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.