question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Прошло много лет, и на вечеринке снова встретились n друзей. С момента последней встречи техника шагнула далеко вперёд, появились фотоаппараты с автоспуском, и теперь не требуется, чтобы один из друзей стоял с фотоаппаратом, и, тем самым, оказывался не запечатлённым на снимке. Упрощенно процесс фотографирования можно описать следующим образом. На фотографии каждый из друзей занимает прямоугольник из пикселей: в стоячем положении i-й из них занимает прямоугольник ширины w_{i} пикселей и высоты h_{i} пикселей. Но также, при фотографировании каждый человек может лечь, и тогда он будет занимать прямоугольник ширины h_{i} пикселей и высоты w_{i} пикселей. Общая фотография будет иметь размеры W × H, где W — суммарная ширина всех прямоугольников-людей, а H — максимальная из высот. Друзья хотят определить, какую минимальную площадь может иметь общая фотография. Помогите им в этом. -----Входные данные----- В первой строке следует целое число n (1 ≤ n ≤ 1000) — количество друзей. В последующих n строках следуют по два целых числа w_{i}, h_{i} (1 ≤ w_{i}, h_{i} ≤ 1000), обозначающие размеры прямоугольника, соответствующего i-му из друзей. -----Выходные данные----- Выведите единственное целое число, равное минимальной возможной площади фотографии, вмещающей всех друзей. -----Примеры----- Входные данные 3 10 1 20 2 30 3 Выходные данные 180 Входные данные 3 3 1 2 2 4 3 Выходные данные 21 Входные данные 1 5 10 Выходные данные 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 n segments on a number line, numbered from 1 to n. The i-th segments covers all integer points from l_i to r_i and has a value w_i. You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two integer points if there exists a selected segment that covers both of them. A subset is good if it's possible to reach point m starting from point 1 in arbitrary number of moves. The cost of the subset is the difference between the maximum and the minimum values of segments in it. Find the minimum cost of a good subset. In every test there exists at least one good subset. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5; 2 ≤ m ≤ 10^6) — the number of segments and the number of integer points. Each of the next n lines contains three integers l_i, r_i and w_i (1 ≤ l_i < r_i ≤ m; 1 ≤ w_i ≤ 10^6) — the description of the i-th segment. In every test there exists at least one good subset. Output Print a single integer — the minimum cost of a good subset. Examples Input 5 12 1 5 5 3 4 10 4 10 6 11 12 5 10 12 3 Output 3 Input 1 10 1 10 23 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. Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place. You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa > pb, or pa = pb and ta < tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place. Your task is to count what number of teams from the given list shared the k-th place. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces. Output In the only line print the sought number of teams that got the k-th place in the final results' table. Examples Input 7 2 4 10 4 10 4 10 3 20 2 1 2 1 1 10 Output 3 Input 5 4 3 1 3 1 5 3 3 1 3 1 Output 4 Note The final results' table for the first sample is: * 1-3 places — 4 solved problems, the penalty time equals 10 * 4 place — 3 solved problems, the penalty time equals 20 * 5-6 places — 2 solved problems, the penalty time equals 1 * 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams. The final table for the second sample is: * 1 place — 5 solved problems, the penalty time equals 3 * 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 consisting of only numbers from 0 to 9, consider the operation of creating a new string from that string according to the following rules. Read the given string one character at a time from the left end. Go, if the same number a continues r, write the number r and the number a in this order without separating them with a space. Read to the right end of the given character string, and what is on the way to the end of the last writing Even if there are times of writing, all of them are counted as one operation. For the second and subsequent operations, the same operation is performed with the character string written out by the previous operation as the given character string. For example, "122244" If the character string "" is given, the character string obtained by one operation is "113224" and "44444444444" (11) because one 1, three 2, two 4s are given in order from the left end. In the case of 4), the obtained character string is “114”. Create a program that outputs a string obtained by performing the above operation n times on a given string of 100 characters or less, where n ≤ 20. The input data consists of two lines, the first line contains the number of operations n, and the second line contains the first character string. Input example --- Five 11 Output example 13112221 input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the character string that has been operated the specified number of times is output on one line. Example Input 5 11 5 11 0 Output 13112221 13112221 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$. Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$. Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple. You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$. Note that, according to the definition, subarrays of length $1$ and $2$ are good. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of good subarrays of array $a$. -----Examples----- Input 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 Output 10 12 3 -----Note----- In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and: $d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$; $d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$; $d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$; In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$: $d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$; $d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$; $d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$; So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations. Initially, there are $n$ superheroes in avengers team having powers $a_1, a_2, \ldots, a_n$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $1$. They can do at most $m$ operations. Also, on a particular superhero at most $k$ operations can be done. Can you help the avengers team to maximize the average power of their crew? -----Input----- The first line contains three integers $n$, $k$ and $m$ ($1 \le n \le 10^{5}$, $1 \le k \le 10^{5}$, $1 \le m \le 10^{7}$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^{6}$) — the initial powers of the superheroes in the cast of avengers. -----Output----- Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$. Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$. -----Examples----- Input 2 4 6 4 7 Output 11.00000000000000000000 Input 4 2 6 1 3 2 3 Output 5.00000000000000000000 -----Note----- In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times. In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $2$ each. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gag Segtree has $ N $ of "gags", each with a value of $ V_i $. Segtree decided to publish all the gags in any order. Here, the "joy" you get when you publish the $ i $ th gag to the $ j $ th is expressed as $ V_i --j $. Find the maximum sum of the "joy" you can get. input Input is given from standard input in the following format. $ N $ $ V_1 $ $ V_2 $ $ \ ldots $ $ V_N $ output Please output the maximum value of the sum of "joy". However, the value does not always fit in a 32-bit integer. Insert a line break at the end. Constraint * $ 1 \ leq N \ leq 10 ^ 5 $ * $ 1 \ leq V_i \ leq 10 ^ 5 $ * All inputs are integers. Input example 1 1 59549 Output example 1 59548 Input example 2 Five 2 1 8 5 7 Output example 2 8 Example Input 1 59549 Output 59548 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Your task is to write a program which reads a sequence of integers and prints mode values of the sequence. The mode value is the element which occurs most frequently. Input A sequence of integers ai (1 ≤ ai ≤ 100). The number of integers is less than or equals to 100. Output Print the mode values. If there are several mode values, print them in ascending order. Example Input 5 6 3 5 8 7 5 3 9 7 3 4 Output 3 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. Write a function `consonantCount`, `consonant_count` or `ConsonantCount` that takes a string of English-language text and returns the number of consonants in the string. Consonants are all letters used to write English excluding the vowels `a, e, i, o, u`. Write your solution by modifying this code: ```python def consonant_count(s): ``` Your solution should implemented in the function "consonant_count". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there. Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell. Help Vasya! Calculate the maximum total weight of mushrooms he can collect. -----Input----- The first line contains the number n (1 ≤ n ≤ 3·10^5) — the length of the glade. The second line contains n numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^6) — the growth rate of mushrooms in the first row of the glade. The third line contains n numbers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^6) is the growth rate of mushrooms in the second row of the glade. -----Output----- Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once. -----Examples----- Input 3 1 2 3 6 5 4 Output 70 Input 3 1 1000 10000 10 100 100000 Output 543210 -----Note----- In the first test case, the optimal route is as follows: [Image] Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70. In the second test case, the optimal route is as follows: [Image] Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task An `amazon` (also known as a queen+knight compound) is an imaginary chess piece that can move like a `queen` or a `knight` (or, equivalently, like a `rook`, `bishop`, or `knight`). The diagram below shows all squares which the amazon attacks from e4 (circles represent knight-like moves while crosses correspond to queen-like moves). ![](https://codefightsuserpics.s3.amazonaws.com/tasks/amazonCheckmate/img/amazon.png?_tm=1473934566013) Recently you've come across a diagram with only three pieces left on the board: a `white amazon`, `white king` and `black king`. It's black's move. You don't have time to determine whether the game is over or not, but you'd like to figure it out in your head. Unfortunately, the diagram is smudged and you can't see the position of the `black king`, so it looks like you'll have to check them all. Given the positions of white pieces on a standard chessboard, determine the number of possible black king's positions such that: * It's a checkmate (i.e. black's king is under amazon's attack and it cannot make a valid move); * It's a check (i.e. black's king is under amazon's attack but it can reach a safe square in one move); * It's a stalemate (i.e. black's king is on a safe square but it cannot make a valid move); * Black's king is on a safe square and it can make a valid move. Note that two kings cannot be placed on two adjacent squares (including two diagonally adjacent ones). # Example For `king = "d3" and amazon = "e4"`, the output should be `[5, 21, 0, 29]`. ![](https://codefightsuserpics.s3.amazonaws.com/tasks/amazonCheckmate/img/example1.png?_tm=1473934566299) `Red crosses` correspond to the `checkmate` positions, `orange pluses` refer to `checks` and `green circles` denote `safe squares`. For `king = "a1" and amazon = "g5"`, the output should be `[0, 29, 1, 29]`. ![](https://codefightsuserpics.s3.amazonaws.com/tasks/amazonCheckmate/img/example2.png?_tm=1473934566670) `Stalemate` position is marked by a `blue square`. # Input - String `king` Position of white's king in chess notation. - String `amazon` Position of white's amazon in the same notation. Constraints: `amazon ≠ king.` # Output An array of four integers, each equal to the number of black's king positions corresponding to a specific situation. The integers should be presented in the same order as the situations were described, `i.e. 0 for checkmates, 1 for checks, etc`. Write your solution by modifying this code: ```python def amazon_check_mate(king, amazon): ``` Your solution should implemented in the function "amazon_check_mate". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section. Of course, biathlon as any sport, proved very difficult in practice. It takes much time and effort. Workouts, workouts, and workouts, — that's what awaited Valera on his way to great achievements in biathlon. As for the workouts, you all probably know that every professional biathlete should ski fast and shoot precisely at the shooting range. Only in this case you can hope to be successful, because running and shooting are the two main components of biathlon. Valera has been diligent in his ski trainings, which is why he runs really fast, however, his shooting accuracy is nothing to write home about. On a biathlon base where Valera is preparing for the competition, there is a huge rifle range with n targets. Each target have shape of a circle, and the center of each circle is located on the Ox axis. At the last training session Valera made the total of m shots. To make monitoring of his own results easier for him, one rather well-known programmer (of course it is you) was commissioned to write a program that would reveal how many and which targets Valera hit. More specifically, for each target the program must print the number of the first successful shot (in the target), or "-1" if this was not hit. The target is considered hit if the shot is inside the circle or on its boundary. Valera is counting on you and perhaps, thanks to you he will one day win international competitions. Input The first line of the input file contains the integer n (1 ≤ n ≤ 104), which is the number of targets. The next n lines contain descriptions of the targets. Each target is a circle whose center is located on the Ox axis. Each circle is given by its coordinate of the center x ( - 2·104 ≤ x ≤ 2·104) and its radius r (1 ≤ r ≤ 1000). It is guaranteed that no two targets coincide, intersect or are nested into each other, but they can touch each other. The next line contains integer m (1 ≤ m ≤ 2·105), which is the number of shots. Next m lines contain descriptions of the shots, which are points on the plane, given by their coordinates x and y ( - 2·104 ≤ x, y ≤ 2·104). All the numbers in the input are integers. Targets and shots are numbered starting from one in the order of the input. Output Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces. Examples Input 3 2 1 5 2 10 1 5 0 1 1 3 3 0 4 0 4 0 Output 2 3 3 -1 Input 3 3 2 7 1 11 2 4 2 1 6 0 6 4 11 2 Output 3 1 2 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke is going to open a contest named "AtCoder s Contest". Here, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters. Snuke has decided to abbreviate the name of the contest as "AxC". Here, x is the uppercase English letter at the beginning of s. Given the name of the contest, print the abbreviation of the name. -----Constraints----- - The length of s is between 1 and 100, inclusive. - The first character in s is an uppercase English letter. - The second and subsequent characters in s are lowercase English letters. -----Input----- The input is given from Standard Input in the following format: AtCoder s Contest -----Output----- Print the abbreviation of the name of the contest. -----Sample Input----- AtCoder Beginner Contest -----Sample Output----- ABC The contest in which you are participating now. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Simple enough this one - you will be given an array. The values in the array will either be numbers or strings, or a mix of both. You will not get an empty array, nor a sparse one. Your job is to return a single array that has first the numbers sorted in ascending order, followed by the strings sorted in alphabetic order. The values must maintain their original type. Note that numbers written as strings are strings and must be sorted with the other strings. Write your solution by modifying this code: ```python def db_sort(arr): ``` Your solution should implemented in the function "db_sort". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The new £5 notes have been recently released in the UK and they've certainly became a sensation! Even those of us who haven't been carrying any cash around for a while, having given in to the convenience of cards, suddenly like to have some of these in their purses and pockets. But how many of them could you get with what's left from your salary after paying all bills? The programme that you're about to write will count this for you! Given a salary and the array of bills, calculate your disposable income for a month and return it as a number of new £5 notes you can get with that amount. If the money you've got (or do not!) doesn't allow you to get any £5 notes return 0. £££ GOOD LUCK! £££ Write your solution by modifying this code: ```python def get_new_notes(salary,bills): ``` Your solution should implemented in the function "get_new_notes". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a string, you progressively need to concatenate the first letter from the left and the first letter to the right and "1", then the second letter from the left and the second letter to the right and "2", and so on. If the string's length is odd drop the central element. For example: ```python char_concat("abcdef") == 'af1be2cd3' char_concat("abc!def") == 'af1be2cd3' # same result ``` Write your solution by modifying this code: ```python def char_concat(word): ``` Your solution should implemented in the function "char_concat". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task Vanya gets bored one day and decides to enumerate a large pile of rocks. He first counts the rocks and finds out that he has `n` rocks in the pile, then he goes to the store to buy labels for enumeration. Each of the labels is a digit from 0 to 9 and each of the `n` rocks should be assigned a unique number from `1` to `n`. If each label costs `$1`, how much money will Vanya spend on this project? # Input/Output - `[input]` integer `n` The number of rocks in the pile. `1  ≤  n  ≤  10^9` - `[output]` an integer the cost of the enumeration. # Example For `n = 13`, the result should be `17`. ``` the numbers from 1 to n are: 1 2 3 4 5 6 7 8 9 10 11 12 13 we need 17 single digit labels: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 each label cost $1, so the output should be 17. ``` Write your solution by modifying this code: ```python def rocks(n): ``` Your solution should implemented in the function "rocks". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an expression of the form $a{+}b$, where $a$ and $b$ are integers from $0$ to $9$. You have to evaluate it and print the result. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Each test case consists of one line containing an expression of the form $a{+}b$ ($0 \le a, b \le 9$, both $a$ and $b$ are integers). The integers are not separated from the $+$ sign. -----Output----- For each test case, print one integer — the result of the expression. -----Examples----- Input 4 4+2 0+0 3+7 8+9 Output 6 0 10 17 -----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. A marine themed rival site to Codewars has started. Codwars is advertising their website all over the internet using subdomains to hide or obfuscate their domain to trick people into clicking on their site. Your task is to write a function that accepts a URL as a string and determines if it would result in an http request to codwars.com. Function should return true for all urls in the codwars.com domain. All other URLs should return false. The urls are all valid but may or may not contain http://, https:// at the beginning or subdirectories or querystrings at the end. For additional confusion, directories in can be named "codwars.com" in a url with the codewars.com domain and vise versa. Also, a querystring may contain codewars.com or codwars.com for any other domain - it should still return true or false based on the domain of the URL and not the domain in the querystring. Subdomains can also add confusion: for example `http://codwars.com.codewars.com` is a valid URL in the codewars.com domain in the same way that `http://mail.google.com` is a valid URL within google.com Urls will not necessarily have either codewars.com or codwars.com in them. The folks at Codwars aren't very good about remembering the contents of their paste buffers. All urls contain domains with a single TLD; you need not worry about domains like company.co.uk. ``` findCodwars("codwars.com"); // true findCodwars("https://subdomain.codwars.com/a/sub/directory?a=querystring"); // true findCodwars("codewars.com"); // false findCodwars("https://subdomain.codwars.codewars.com/a/sub/directory/codwars.com?a=querystring"); // false ``` Write your solution by modifying this code: ```python def find_codwars(url): ``` Your solution should implemented in the function "find_codwars". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. [Image] Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start. After n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right. Number n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a_1, a_2, ..., a_{k} such that $n = \prod_{i = 1}^{k} a_{i}$ in other words, n is multiplication of all elements of the given array. Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such that $\operatorname{gcd}(p, q) = 1$, where $gcd$ is the greatest common divisor. Since p and q can be extremely large, you only need to find the remainders of dividing each of them by 10^9 + 7. Please note that we want $gcd$ of p and q to be 1, not $gcd$ of their remainders after dividing by 10^9 + 7. -----Input----- The first line of input contains a single integer k (1 ≤ k ≤ 10^5) — the number of elements in array Barney gave you. The second line contains k integers a_1, a_2, ..., a_{k} (1 ≤ a_{i} ≤ 10^18) — the elements of the array. -----Output----- In the only line of output print a single string x / y where x is the remainder of dividing p by 10^9 + 7 and y is the remainder of dividing q by 10^9 + 7. -----Examples----- Input 1 2 Output 1/2 Input 3 1 1 1 Output 0/1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Nauuo is a girl who loves writing comments. One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes. It's known that there were $x$ persons who would upvote, $y$ persons who would downvote, and there were also another $z$ persons who would vote, but you don't know whether they would upvote or downvote. Note that each of the $x+y+z$ people would vote exactly one time. There are three different results: if there are more people upvote than downvote, the result will be "+"; if there are more people downvote than upvote, the result will be "-"; otherwise the result will be "0". Because of the $z$ unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the $z$ persons vote, that the results are different in the two situations. Tell Nauuo the result or report that the result is uncertain. -----Input----- The only line contains three integers $x$, $y$, $z$ ($0\le x,y,z\le100$), corresponding to the number of persons who would upvote, downvote or unknown. -----Output----- If there is only one possible result, print the result : "+", "-" or "0". Otherwise, print "?" to report that the result is uncertain. -----Examples----- Input 3 7 0 Output - Input 2 0 1 Output + Input 1 1 0 Output 0 Input 0 0 1 Output ? -----Note----- In the first example, Nauuo would definitely get three upvotes and seven downvotes, so the only possible result is "-". In the second example, no matter the person unknown downvotes or upvotes, Nauuo would get more upvotes than downvotes. So the only possible result is "+". In the third example, Nauuo would definitely get one upvote and one downvote, so the only possible result is "0". In the fourth example, if the only one person upvoted, the result would be "+", otherwise, the result would be "-". There are two possible results, so the result is uncertain. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 arrays, the purpose of this Kata is to check if these two arrays are the same. "The same" in this Kata means the two arrays contains arrays of 2 numbers which are same and not necessarily sorted the same way. i.e. [[2,5], [3,6]] is same as [[5,2], [3,6]] or [[6,3], [5,2]] or [[6,3], [2,5]] etc [[2,5], [3,6]] is NOT the same as [[2,3], [5,6]] Two empty arrays [] are the same [[2,5], [5,2]] is the same as [[2,5], [2,5]] but NOT the same as [[2,5]] [[2,5], [3,5], [6,2]] is the same as [[2,6], [5,3], [2,5]] or [[3,5], [6,2], [5,2]], etc An array can be empty or contain a minimun of one array of 2 integers and up to 100 array of 2 integers Note: 1. [[]] is not applicable because if the array of array are to contain anything, there have to be two numbers. 2. 100 randomly generated tests that can contains either "same" or "not same" arrays. Write your solution by modifying this code: ```python def same(arr_a, arr_b): ``` Your solution should implemented in the function "same". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 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. Your users passwords were all stole in the Yahoo! hack, and it turns out they have been lax in creating secure passwords. Create a function that checks their new password (passed as a string) to make sure it meets the following requirements: Between 8 - 20 characters Contains only the following characters: (and at least one character from each category): uppercase letters, lowercase letters, digits, and the special characters !@#$%^&*? Return "valid" if passed or else "not valid" Write your solution by modifying this code: ```python def check_password(s): ``` Your solution should implemented in the function "check_password". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ bidirectional roads. She is currently at field $1$, and will return to her home at field $n$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $k$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $1$ to field $n$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! -----Input----- The first line contains integers $n$, $m$, and $k$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le 2 \cdot 10^5$, $2 \le k \le n$) — the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — the special fields. All $a_i$ are distinct. The $i$-th of the following $m$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), representing a bidirectional road between fields $x_i$ and $y_i$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. -----Output----- Output one integer, the maximum possible length of the shortest path from field $1$ to $n$ after Farmer John installs one road optimally. -----Examples----- Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Output 3 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Output 3 -----Note----- The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $3$ and $5$, and the resulting shortest path from $1$ to $5$ is length $3$. The graph for the second example is shown below. Farmer John must add a road between fields $2$ and $4$, and the resulting shortest path from $1$ to $5$ is length $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. There is a game that uses numbers called "Fizz Buzz". In this game, multiple players count the numbers one by one, starting with 1, and each player says only one number after the previous player. At that time, you must say "Fizz" if it is divisible by 3, "Buzz" if it is divisible by 5, and "FizzBuzz" if it is divisible by both. For example, the first 16 statements are as follows: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, ・ ・ ・ Taro decided to play "Fizz Buzz" with his friends. Taro and his colleagues decided the rules as follows. "The wrong person will drop out. The next person will start with the next number after the wrong number. That is, if you say 1, 2, 3, you made a mistake with 3, so you will start with 4." I play the game according to this rule, but because I am not used to the game, I sometimes do not notice that I made a mistake, and I cannot make a fair decision. So you decided to create a program that outputs the remaining people at the end of the set number of remarks so that Taro and his friends can enjoy this game. Create a program that inputs the number of players, the number of times spoken during the game, and each statement, and outputs the number of the remaining players at the end of the input in ascending order. However, the players are numbered from 1, and the order of speaking is also from the first player, and when the speaking is completed, the first player will speak again. If the player in turn has already dropped out, the next player will speak. Also, the program must ignore subsequent statements when the player is alone. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: m n s1 s2 :: sn The first line gives the number of players m (2 ≤ m ≤ 1000) and the number of remarks n (1 ≤ n ≤ 10000). The next n lines are given the i-th statement s1. si is an integer, Fizz, Buzz, or a string (up to 8 characters) that indicates FizzBuzz. The number of datasets does not exceed 50. Output For each input dataset, the number of the player remaining when the specified number of remarks is input is output in ascending order. Example Input 5 7 1 2 Fizz 4 Buzz 6 7 3 5 1 2 3 4 5 0 0 Output 2 3 4 5 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. Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games. He came up with the following game. The player has a positive integer $n$. Initially the value of $n$ equals to $v$ and the player is able to do the following operation as many times as the player want (possibly zero): choose a positive integer $x$ that $x<n$ and $x$ is not a divisor of $n$, then subtract $x$ from $n$. The goal of the player is to minimize the value of $n$ in the end. Soon, Chouti found the game trivial. Can you also beat the game? -----Input----- The input contains only one integer in the first line: $v$ ($1 \le v \le 10^9$), the initial value of $n$. -----Output----- Output a single integer, the minimum value of $n$ the player can get. -----Examples----- Input 8 Output 1 Input 1 Output 1 -----Note----- In the first example, the player can choose $x=3$ in the first turn, then $n$ becomes $5$. He can then choose $x=4$ in the second turn to get $n=1$ as the result. There are other ways to get this minimum. However, for example, he cannot choose $x=2$ in the first turn because $2$ is a divisor of $8$. In the second example, since $n=1$ initially, the player can do nothing. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 said that a legendary treasure left by Mr. Yao is sleeping somewhere in Hachioji long ago. The treasure map, which is said to show its whereabouts, has been handed down by Yao's n descendants, divided into several pieces. Now, the descendants of Mr. Yao were trying to cooperate to obtain the treasure. However, the treasure cannot be found only by a part of the treasure map that points to the location of the treasure. Therefore, all the descendants of Mr. Yao gathered and tried to collect the map in one place. However, even if I tried to put it into practice, I couldn't get together because I couldn't meet the schedule. However, this information about the treasure is valuable information that has been secretly passed down in the clan. Considering the risk of leakage, exchanging maps using public communication means is out of the question. Therefore, I decided to collect the map for one descendant by repeating the process of meeting the descendants in person and handing over the map. There is no limit to the number of people that one person can meet in a day, but it is necessary that there is a schedule for each other. Your job is to write a program that asks for at least how many days it will take to collect a map from a list of open days on schedule for each offspring. By the way, the unity of the Yao clan is very tight. If the descendants who finally get the entire map betray the other descendants and take away the treasure, they will be sanctioned by the clan. The sanctions are so horrifying that it is virtually impossible for their descendants to actually carry away the treasure. Input The input consists of multiple datasets. Each dataset consists of multiple rows. The first line contains the integer n (1 <n <= 50), which represents the number of people with a piece of the map. The next n lines contain the schedule for each descendant. Line i represents the schedule of the i-th descendant, with some integers separated by a single character space. The first integer fi (0 <= fi <= 30) is an integer that represents the number of days that the descendant's schedule is free. The following fi integers represent dates when the schedule is free. These dates differ from each other and are all greater than or equal to 1 and less than or equal to 30. There is one line containing only 0 at the end of the input. Output Print one integer on one line for each dataset. If you can collect the map within 30 days, output the minimum number of days required to collect the map, otherwise output -1. Addendum: The above "minimum number of days required to collect maps" means the date when all maps are collected earliest starting from one day. Example Input 4 1 1 2 2 3 2 1 2 3 3 4 5 0 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 a sequence $b_1, b_2, \ldots, b_n$. Find the lexicographically minimal permutation $a_1, a_2, \ldots, a_{2n}$ such that $b_i = \min(a_{2i-1}, a_{2i})$, or determine that it is impossible. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The first line of each test case consists of one integer $n$ — the number of elements in the sequence $b$ ($1 \le n \le 100$). The second line of each test case consists of $n$ different integers $b_1, \ldots, b_n$ — elements of the sequence $b$ ($1 \le b_i \le 2n$). It is guaranteed that the sum of $n$ by all test cases doesn't exceed $100$. -----Output----- For each test case, if there is no appropriate permutation, print one number $-1$. Otherwise, print $2n$ integers $a_1, \ldots, a_{2n}$ — required lexicographically minimal permutation of numbers from $1$ to $2n$. -----Example----- Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 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. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2). Input The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100. Output Output one number — length of the longest substring that can be met in the string at least twice. Examples Input abcd Output 0 Input ababa Output 3 Input zzz Output 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Write function bmi that calculates body mass index (bmi = weight / height ^ 2). if bmi <= 18.5 return "Underweight" if bmi <= 25.0 return "Normal" if bmi <= 30.0 return "Overweight" if bmi > 30 return "Obese" Write your solution by modifying this code: ```python def bmi(weight, height): ``` Your solution should implemented in the function "bmi". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create an OR function, without use of the 'or' keyword, that takes an list of boolean values and runs OR against all of them. Assume there will be between 1 and 6 variables, and return None for an empty list. Write your solution by modifying this code: ```python def alt_or(lst): ``` Your solution should implemented in the function "alt_or". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke has four cookies with deliciousness A, B, C, and D. He will choose one or more from those cookies and eat them. Is it possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies? -----Constraints----- - All values in input are integers. - 1 \leq A,B,C,D \leq 10^{8} -----Input----- Input is given from Standard Input in the following format: A B C D -----Output----- If it is possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies, print Yes; otherwise, print No. -----Sample Input----- 1 3 2 4 -----Sample Output----- Yes - If he eats the cookies with deliciousness 1 and 4, the sum of the deliciousness of the eaten cookies will be equal to that of the remaining cookies. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()". Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ilya plays a card game by the following rules. A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded. More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards. Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards? Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has. Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly. Output Print the single number — the maximum number of points you can score in one round by the described rules. Examples Input 2 1 0 2 0 Output 2 Input 3 1 0 2 0 0 2 Output 3 Note In the first sample none of two cards brings extra moves, so you should play the one that will bring more points. In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer — the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Problem There are n vertices that are not connected to any of the vertices. An undirected side is stretched between each vertex. Find out how many sides can be stretched when the diameter is set to d. The diameter represents the largest of the shortest distances between two vertices. Here, the shortest distance is the minimum value of the number of sides required to move between vertices. Multiple edges and self-loops are not allowed. Constraints * 2 ≤ n ≤ 109 * 1 ≤ d ≤ n−1 Input n d Two integers n and d are given on one line, separated by blanks. Output Output the maximum number of sides that can be stretched on one line. Examples Input 4 3 Output 3 Input 5 1 Output 10 Input 4 2 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. The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer — the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd 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. The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit n planets. For planet i ai is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and bi is the number of citizens on the planet. The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, x presents in each. The Beaver will take to the ship exactly a1 + ... + an suitcases. As the Beaver lands on the i-th planet, he takes ai suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens — each of the bi citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel. The Beaver is going to spend exactly c days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer x so that the planned voyage will take exactly c days? Input The first input line contains space-separated integers n and c — the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly. The next n lines contain pairs of space-separated integers ai, bi (1 ≤ i ≤ n) — the number of suitcases he can bring to the i-th planet and the number of citizens of the i-th planet, correspondingly. The input limitations for getting 30 points are: * 1 ≤ n ≤ 100 * 1 ≤ ai ≤ 100 * 1 ≤ bi ≤ 100 * 1 ≤ c ≤ 100 The input limitations for getting 100 points are: * 1 ≤ n ≤ 104 * 0 ≤ ai ≤ 109 * 1 ≤ bi ≤ 109 * 1 ≤ c ≤ 109 Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations! Output Print a single number k — the number of ways to choose x so as to travel for exactly c days. If there are infinitely many possible values of x, print -1. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Examples Input 2 5 1 5 2 4 Output 1 Note In the first example there is only one suitable value x = 5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days — he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling. For x = 4 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For x = 6 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a program that outputs all leap years between the year a and year b. The leap year conditions are as follows. However, 0 <a ≤ b <3,000. If there is no leap year in the given period, output "NA". * The year is divisible by 4. * However, a year divisible by 100 is not a leap year. * However, a year divisible by 400 is a leap year. Input Given multiple datasets. The format of each dataset is as follows: a b Input ends when both a and b are 0. The number of datasets does not exceed 50. Output Print the year or NA for each dataset. Insert one blank line between the datasets. Example Input 2001 2010 2005 2005 2001 2010 0 0 Output 2004 2008 NA 2004 2008 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this! Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one. Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left. One problem with prime numbers is that there are too many of them. Let's introduce the following notation: π(n) — the number of primes no larger than n, rub(n) — the number of palindromic numbers no larger than n. Rikhail wants to prove that there are a lot more primes than palindromic ones. He asked you to solve the following problem: for a given value of the coefficient A find the maximum n, such that π(n) ≤ A·rub(n). Input The input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value of A (<image>, <image>). Output If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes). Examples Input 1 1 Output 40 Input 1 42 Output 1 Input 6 4 Output 172 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m). Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x_1, y_1), an arbitrary corner of the table (x_2, y_2) and color all cells of the table (p, q), which meet both inequations: min(x_1, x_2) ≤ p ≤ max(x_1, x_2), min(y_1, y_2) ≤ q ≤ max(y_1, y_2). Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times. -----Input----- The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50). Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers a_{i}1, a_{i}2, ..., a_{im}. If a_{ij} equals zero, then cell (i, j) isn't good. Otherwise a_{ij} equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner. -----Output----- Print a single number — the minimum number of operations Simon needs to carry out his idea. -----Examples----- Input 3 3 0 0 0 0 1 0 0 0 0 Output 4 Input 4 3 0 0 0 0 0 1 1 0 0 0 0 0 Output 2 -----Note----- In the first sample, the sequence of operations can be like this: [Image] For the first time you need to choose cell (2, 2) and corner (1, 1). For the second time you need to choose cell (2, 2) and corner (3, 3). For the third time you need to choose cell (2, 2) and corner (3, 1). For the fourth time you need to choose cell (2, 2) and corner (1, 3). In the second sample the sequence of operations can be like this: [Image] For the first time you need to choose cell (3, 1) and corner (4, 3). For the second time you need to choose cell (2, 3) and corner (1, 1). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have invented a time-machine which has taken you back to ancient Rome. Caeser is impressed with your programming skills and has appointed you to be the new information security officer. Caeser has ordered you to write a Caeser cipher to prevent Asterix and Obelix from reading his emails. A Caeser cipher shifts the letters in a message by the value dictated by the encryption key. Since Caeser's emails are very important, he wants all encryptions to have upper-case output, for example: If key = 3 "hello" -> KHOOR If key = 7 "hello" -> OLSSV Input will consist of the message to be encrypted and the encryption key. Write your solution by modifying this code: ```python def caeser(message, key): ``` Your solution should implemented in the function "caeser". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have a set of $n$ discs, the $i$-th disc has radius $i$. Initially, these discs are split among $m$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top. You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $i$ and $j$ (each containing at least one disc), take several (possibly all) top discs from the tower $i$ and put them on top of the tower $j$ in the same order, as long as the top disc of tower $j$ is bigger than each of the discs you move. You may perform this operation any number of times. For example, if you have two towers containing discs $[6, 4, 2, 1]$ and $[8, 7, 5, 3]$ (in order from bottom to top), there are only two possible operations: move disc $1$ from the first tower to the second tower, so the towers are $[6, 4, 2]$ and $[8, 7, 5, 3, 1]$; move discs $[2, 1]$ from the first tower to the second tower, so the towers are $[6, 4]$ and $[8, 7, 5, 3, 2, 1]$. Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $[[3, 1], [2]]$ is $2$: you may move the disc $1$ to the second tower, and then move both discs from the second tower to the first tower. You are given $m - 1$ queries. Each query is denoted by two numbers $a_i$ and $b_i$, and means "merge the towers $a_i$ and $b_i$" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $a_i$. For each $k \in [0, m - 1]$, calculate the difficulty of the set of towers after the first $k$ queries are performed. -----Input----- The first line of the input contains two integers $n$ and $m$ ($2 \le m \le n \le 2 \cdot 10^5$) — the number of discs and the number of towers, respectively. The second line contains $n$ integers $t_1$, $t_2$, ..., $t_n$ ($1 \le t_i \le m$), where $t_i$ is the index of the tower disc $i$ belongs to. Each value from $1$ to $m$ appears in this sequence at least once. Then $m - 1$ lines follow, denoting the queries. Each query is represented by two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le m$, $a_i \ne b_i$), meaning that, during the $i$-th query, the towers with indices $a_i$ and $b_i$ are merged ($a_i$ and $b_i$ are chosen in such a way that these towers exist before the $i$-th query). -----Output----- Print $m$ integers. The $k$-th integer ($0$-indexed) should be equal to the difficulty of the set of towers after the first $k$ queries are performed. -----Example----- Input 7 4 1 2 3 3 1 4 3 3 1 2 3 2 4 Output 5 4 2 0 -----Note----- The towers in the example are: before the queries: $[[5, 1], [2], [7, 4, 3], [6]]$; after the first query: $[[2], [7, 5, 4, 3, 1], [6]]$; after the second query: $[[7, 5, 4, 3, 2, 1], [6]]$; after the third query, there is only one tower: $[7, 6, 5, 4, 3, 2, 1]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have an N \times N square grid. We will paint each square in the grid either black or white. If we paint exactly A squares white, how many squares will be painted black? -----Constraints----- - 1 \leq N \leq 100 - 0 \leq A \leq N^2 -----Inputs----- Input is given from Standard Input in the following format: N A -----Outputs----- Print the number of squares that will be painted black. -----Sample Input----- 3 4 -----Sample Output----- 5 There are nine squares in a 3 \times 3 square grid. Four of them will be painted white, so the remaining five squares will be painted black. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers p_{i}, a_{i} and b_{i}, where p_{i} is the price of the i-th t-shirt, a_{i} is front color of the i-th t-shirt and b_{i} is back color of the i-th t-shirt. All values p_{i} are distinct, and values a_{i} and b_{i} are integers from 1 to 3. m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color c_{j}. A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served. You are to compute the prices each buyer will pay for t-shirts. -----Input----- The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts. The following line contains sequence of integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ 1 000 000 000), where p_{i} equals to the price of the i-th t-shirt. The following line contains sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 3), where a_{i} equals to the front color of the i-th t-shirt. The following line contains sequence of integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 3), where b_{i} equals to the back color of the i-th t-shirt. The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers. The following line contains sequence c_1, c_2, ..., c_{m} (1 ≤ c_{j} ≤ 3), where c_{j} equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. -----Output----- Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1. -----Examples----- Input 5 300 200 400 500 911 1 2 1 2 3 2 1 3 2 1 6 2 3 1 2 1 1 Output 200 400 300 500 911 -1 Input 2 1000000000 1 1 1 1 2 2 2 1 Output 1 1000000000 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Garden with Ponds Mr. Gardiner is a modern garden designer who is excellent at utilizing the terrain features. His design method is unique: he first decides the location of ponds and design them with the terrain features intact. According to his unique design procedure, all of his ponds are rectangular with simple aspect ratios. First, Mr. Gardiner draws a regular grid on the map of the garden site so that the land is divided into cells of unit square, and annotates every cell with its elevation. In his design method, a pond occupies a rectangular area consisting of a number of cells. Each of its outermost cells has to be higher than all of its inner cells. For instance, in the following grid map, in which numbers are elevations of cells, a pond can occupy the shaded area, where the outermost cells are shaded darker and the inner cells are shaded lighter. You can easily see that the elevations of the outermost cells are at least three and those of the inner ones are at most two. <image> A rectangular area on which a pond is built must have at least one inner cell. Therefore, both its width and depth are at least three. When you pour water at an inner cell of a pond, the water can be kept in the pond until its level reaches that of the lowest outermost cells. If you continue pouring, the water inevitably spills over. Mr. Gardiner considers the larger capacity the pond has, the better it is. Here, the capacity of a pond is the maximum amount of water it can keep. For instance, when a pond is built on the shaded area in the above map, its capacity is (3 − 1) + (3 − 0) + (3 − 2) = 6, where 3 is the lowest elevation of the outermost cells and 1, 0, 2 are the elevations of the inner cells. Your mission is to write a computer program that, given a grid map describing the elevation of each unit square cell, calculates the largest possible capacity of a pond built in the site. Note that neither of the following rectangular areas can be a pond. In the left one, the cell at the bottom right corner is not higher than the inner cell. In the right one, the central cell is as high as the outermost cells. <image> Input The input consists of at most 100 datasets, each in the following format. d w e1, 1 ... e1, w ... ed, 1 ... ed, w The first line contains d and w, representing the depth and the width, respectively, of the garden site described in the map. They are positive integers between 3 and 10, inclusive. Each of the following d lines contains w integers between 0 and 9, inclusive, separated by a space. The x-th integer in the y-th line of the d lines is the elevation of the unit square cell with coordinates (x, y). The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a single line containing the largest possible capacity of a pond that can be built in the garden site described in the dataset. If no ponds can be built, output a single line containing a zero. Sample Input 3 3 2 3 2 2 1 2 2 3 1 3 5 3 3 4 3 3 3 1 0 2 3 3 3 4 3 2 7 7 1 1 1 1 1 0 0 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0 0 0 1 0 0 1 1 1 1 1 6 6 1 1 1 1 2 2 1 0 0 2 0 2 1 0 0 2 0 2 3 3 3 9 9 9 3 0 0 9 0 9 3 3 3 9 9 9 0 0 Output for the Sample Input 0 3 1 9 Example Input 3 3 2 3 2 2 1 2 2 3 1 3 5 3 3 4 3 3 3 1 0 2 3 3 3 4 3 2 7 7 1 1 1 1 1 0 0 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0 0 0 1 0 0 1 1 1 1 1 6 6 1 1 1 1 2 2 1 0 0 2 0 2 1 0 0 2 0 2 3 3 3 9 9 9 3 0 0 9 0 9 3 3 3 9 9 9 0 0 Output 0 3 1 9 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. *Inspired by the [Fold an Array](https://www.codewars.com/kata/fold-an-array) kata. This one is sort of similar but a little different.* --- ## Task You will receive an array as parameter that contains 1 or more integers and a number `n`. Here is a little visualization of the process: * Step 1: Split the array in two: ``` [1, 2, 5, 7, 2, 3, 5, 7, 8] / \ [1, 2, 5, 7] [2, 3, 5, 7, 8] ``` * Step 2: Put the arrays on top of each other: ``` [1, 2, 5, 7] [2, 3, 5, 7, 8] ``` * Step 3: Add them together: ``` [2, 4, 7, 12, 15] ``` Repeat the above steps `n` times or until there is only one number left, and then return the array. ## Example ``` Input: arr=[4, 2, 5, 3, 2, 5, 7], n=2 Round 1 ------- step 1: [4, 2, 5] [3, 2, 5, 7] step 2: [4, 2, 5] [3, 2, 5, 7] step 3: [3, 6, 7, 12] Round 2 ------- step 1: [3, 6] [7, 12] step 2: [3, 6] [7, 12] step 3: [10, 18] Result: [10, 18] ``` Write your solution by modifying this code: ```python def split_and_add(numbers, n): ``` Your solution should implemented in the function "split_and_add". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix $a$ consisting of $n$ rows and $m$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their usual meanings — refer to the Notes section for their formal definitions. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \leq n \leq m \leq 10^6$ and $n\cdot m \leq 10^6$)  — the number of rows and columns in $a$, respectively. The following $n$ lines each contain $m$ characters, each of which is one of 0 and 1. If the $j$-th character on the $i$-th line is 1, then $a_{i,j} = 1$. Similarly, if the $j$-th character on the $i$-th line is 0, then $a_{i,j} = 0$. -----Output----- Output the minimum number of cells you need to change to make $a$ good, or output $-1$ if it's not possible at all. -----Examples----- Input 3 3 101 001 110 Output 2 Input 7 15 000100001010010 100111010110001 101101111100100 010000111111010 111010010100001 000011001111101 111111011010011 Output -1 -----Note----- In the first case, changing $a_{1,1}$ to $0$ and $a_{2,2}$ to $1$ is enough. You can verify that there is no way to make the matrix in the second case good. Some definitions — A binary matrix is one in which every element is either $1$ or $0$. A sub-matrix is described by $4$ parameters — $r_1$, $r_2$, $c_1$, and $c_2$; here, $1 \leq r_1 \leq r_2 \leq n$ and $1 \leq c_1 \leq c_2 \leq m$. This sub-matrix contains all elements $a_{i,j}$ that satisfy both $r_1 \leq i \leq r_2$ and $c_1 \leq j \leq c_2$. A sub-matrix is, further, called an even length square if $r_2-r_1 = c_2-c_1$ and $r_2-r_1+1$ is divisible by $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. We have two integers: A and B. Print the largest number among A + B, A - B, and A \times B. -----Constraints----- - All values in input are integers. - -100 \leq A,\ B \leq 100 -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the largest number among A + B, A - B, and A \times B. -----Sample Input----- -13 3 -----Sample Output----- -10 The largest number among A + B = -10, A - B = -16, and A \times B = -39 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. A tourist is visiting Byteland. The tourist knows English very well. The language of Byteland is rather different from English. To be exact it differs in following points: - Bytelandian alphabet has the same letters as English one, but possibly different in meaning. Like 'A' in Bytelandian may be 'M' in English. However this does not mean that 'M' in Bytelandian must be 'A' in English. More formally, Bytelindian alphabet is a permutation of English alphabet. It will be given to you and could be any possible permutation. Don't assume any other condition. - People of Byteland don't like to use invisible character for separating words. Hence instead of space (' ') they use underscore ('_'). Other punctuation symbols, like '?', '!' remain the same as in English. The tourist is carrying "The dummies guide to Bytelandian", for translation. The book is serving his purpose nicely. But he is addicted to sharing on BaceFook, and shares his numerous conversations in Byteland on it. The conversations are rather long, and it is quite tedious to translate for his English friends, so he asks you to help him by writing a program to do the same. -----Input----- The first line of the input contains an integer T, denoting the length of the conversation, and the string M, denoting the English translation of Bytelandian string "abcdefghijklmnopqrstuvwxyz". T and M are separated by exactly one space. Then T lines follow, each containing a Bytelandian sentence S which you should translate into English. See constraints for details. -----Output----- For each of the sentence in the input, output its English translation on a separate line. Replace each underscores ('_') with a space (' ') in the output. Each punctuation symbol (see below) should remain the same. Note that the uppercase letters in Bytelandian remain uppercase in English, and lowercase letters remain lowercase. See the example and its explanation for clarity. -----Constraints----- - 1 ≤ T ≤ 100 - M is a permutation of "abcdefghijklmnopqrstuvwxyz" - Each sentence is non-empty and contains at most 100 characters - Each sentence may contain only lowercase letters ('a'-'z'), uppercase letters ('A'-'Z'), underscores ('_') and punctuation symbols: dot ('.'), comma (','), exclamation ('!'), question-mark('?') -----Example----- Input: 5 qwertyuiopasdfghjklzxcvbnm Ph Pcssi Bpke_kdc_epclc_jcijsc_mihyo? Epcf_kdc_liswhyo_EIED_hy_Vimcvpcn_Zkdvp_siyo_viyecle. Ipp! Output: Hi Hello What are these people doing? They are solving TOTR in Codechef March long contest. Ohh! -----Explanation----- The string "qwertyuiopasdfghjklzxcvbnm" means that 'a' in Bytelandian is 'q' in English, 'b' in Bytelandian is 'w' in English, 'c' in Bytelandian is 'e' in English and so on. Thus to translate "Ph" (first sentence in example) to English: 1) We find that 'p' in Bytelandian means 'h' in English. So we replace 'P' with 'H'. 2) Then we see that 'h' in Bytelandian means 'i' in English. So we replace 'h' with 'i'. 3) Therefore, the translation is "Hi". Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Introduction A grille cipher was a technique for encrypting a plaintext by writing it onto a sheet of paper through a pierced sheet (of paper or cardboard or similar). The earliest known description is due to the polymath Girolamo Cardano in 1550. His proposal was for a rectangular stencil allowing single letters, syllables, or words to be written, then later read, through its various apertures. The written fragments of the plaintext could be further disguised by filling the gaps between the fragments with anodyne words or letters. This variant is also an example of steganography, as are many of the grille ciphers. Wikipedia Link ![Tangiers1](https://upload.wikimedia.org/wikipedia/commons/8/8a/Tangiers1.png) ![Tangiers2](https://upload.wikimedia.org/wikipedia/commons/b/b9/Tangiers2.png) # Task Write a function that accepts two inputs: `message` and `code` and returns hidden message decrypted from `message` using the `code`. The `code` is a nonnegative integer and it decrypts in binary the `message`. Write your solution by modifying this code: ```python def grille(message, code): ``` Your solution should implemented in the function "grille". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a non-empty string s=s_1s_2... s_n, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string s if there is an integer j (1 ≤ j ≤ n-2), that s_{j}s_{j+1}s_{j+2}="one" or s_{j}s_{j+1}s_{j+2}="two". For example: * Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), * Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like s="onetwone", then if Polycarp selects two indices 3 and 6, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? Input The first line of the input contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string s. Its length does not exceed 1.5⋅10^5. The string s consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed 1.5⋅10^6. Output Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain r (0 ≤ r ≤ |s|) — the required minimum number of positions to be removed, where |s| is the length of the given line. The second line of each answer should contain r different integers — the indices themselves for removal in any order. Indices are numbered from left to right from 1 to the length of the string. If r=0, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. Examples Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 Note In the first example, answers are: * "onetwone", * "testme" — Polycarp likes it, there is nothing to remove, * "oneoneone", * "twotwo". In the second example, answers are: * "onetwonetwooneooonetwooo", * "two", * "one", * "twooooo", * "ttttwo", * "ttwwoo" — Polycarp likes it, there is nothing to remove, * "ooone", * "onnne" — Polycarp likes it, there is nothing to remove, * "oneeeee", * "oneeeeeeetwooooo". Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The postal system in the area where Masa lives has changed a bit. In this area, each post office is numbered consecutively from 1 to a different number, and mail delivered to a post office is intended from that post office via several post offices. Delivered to the post office. Mail "forwarding" is only done between specific post offices. However, the transfer is bidirectional between the post offices where the transfer is performed. If there are multiple routes for forwarding mail from a post office to the destination, use the route that minimizes the distance to the destination. Therefore, the forwarding destination will be the next post office on such a shortest path. If there are multiple routes with the shortest total distance, the postal office number that is the direct transfer destination is transferred to the younger one. <image> <image> By the way, the region is suffering from a serious labor shortage, and each post office has only one transfer person to transfer between post offices. They are doing their best to go back and forth. Of course, if the forwarding agent belonging to a post office has paid the mail when the mail arrives at that post office, the forwarding from that post office will be temporarily stopped. When they are at their post office, they depart immediately if there is mail that has not been forwarded from that post office, and return immediately after delivering it to the forwarding destination. When he returns from the destination post office, he will not carry the mail that will be forwarded to his post office, even if it is at that post office (the mail will be forwarded to that mail). It is the job of the post office transferman). If multiple mail items are collected at the time of forwarding, the mail that was delivered to the post office earliest is transferred first. Here, if there are multiple mail items that arrived at the same time, the mail is forwarded from the one with the youngest post office number that is the direct forwarding destination. If there is mail that has the same forwarding destination, it will also be forwarded together. Also, if the mail that should be forwarded to the same post office arrives at the same time as the forwarding agent's departure time, it will also be forwarded. It should be noted that they all take 1 time to travel a distance of 1. You will be given information about the transfer between post offices and a list of the times when the mail arrived at each post office. At this time, simulate the movement of the mail and report the time when each mail arrived at the destination post office. It can be assumed that all the times required during the simulation of the problem are in the range 0 or more and less than 231. Input The input consists of multiple datasets, and the end of the input is represented by a single line with only 0 0. The format of each dataset is as follows. The first line of the dataset is given the integers n (2 <= n <= 32) and m (1 <= m <= 496), separated by a single character space. n represents the total number of post offices and m represents the number of direct forwarding routes between post offices. There can never be more than one direct forwarding route between two post offices. On the next m line, three integers containing information on the direct transfer route between stations are written. The first and second are the postal numbers (1 or more and n or less) at both ends of the transfer route, and the last integer represents the distance between the two stations. The distance is greater than or equal to 1 and less than 10000. The next line contains the integer l (1 <= l <= 1000), which represents the number of mail items to process, followed by the l line, which contains the details of each piece of mail. Three integers are written at the beginning of each line. These are, in order, the sender's postal office number, the destination postal office number, and the time of arrival at the sender's postal office. At the end of the line, a string representing the label of the mail is written. This label must be between 1 and 50 characters in length and consists only of letters, numbers, hyphens ('-'), and underscores ('_'). Information on each of these mail items is written in chronological order of arrival at the post office of the sender. At the same time, the order is not specified. In addition, there is no mail with the same label, mail with the same origin and destination, or mail without a delivery route. Each number or string in the input line is separated by a single character space. Output Output l lines for each dataset. Each output follows the format below. For each mail, the time when the mail arrived is output on one line. On that line, first print the label of the mail, then with a one-character space, and finally print the time when the mail arrived at the destination post office. These are output in order of arrival time. If there are multiple mails arriving at the same time, the ASCII code of the label is output in ascending order. A blank line is inserted between the outputs corresponding to each dataset. Example Input 4 5 1 2 10 1 3 15 2 3 5 2 4 3 3 4 10 2 1 4 10 LoveLetter 2 3 20 Greetings 3 3 1 2 1 2 3 1 3 1 1 3 1 2 1 BusinessMailC 2 3 1 BusinessMailB 3 1 1 BusinessMailA 0 0 Output Greetings 25 LoveLetter 33 BusinessMailA 2 BusinessMailB 2 BusinessMailC 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have been speeding on a motorway and a police car had to stop you. The policeman is a funny guy that likes to play games. Before issuing penalty charge notice he gives you a choice to change your penalty. Your penalty charge is a combination of numbers like: speed of your car, speed limit in the area, speed of the police car chasing you, the number of police cars involved, etc. So, your task is to combine the given numbers and make the penalty charge to be as small as possible. For example, if you are given numbers ```[45, 30, 50, 1]``` your best choice is ```1304550``` Examples: ```Python ['45', '30', '50', '1'] => '1304550' ['100', '10', '1'] => '100101' ['32', '3'] => '323' ``` Write your solution by modifying this code: ```python def penalty(a_list): ``` Your solution should implemented in the function "penalty". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Complete the square sum function so that it squares each number passed into it and then sums the results together. For example, for `[1, 2, 2]` it should return `9` because `1^2 + 2^2 + 2^2 = 9`. ```if:racket In Racket, use a list instead of an array, so '(1 2 3) should return 9. ``` Write your solution by modifying this code: ```python def square_sum(numbers): ``` Your solution should implemented in the function "square_sum". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. ## Enough is enough! Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like these sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order? ## Task Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3]. ~~~if:nasm ## NASM notes Write the output numbers into the `out` parameter, and return its length. The input array will contain only integers between 1 and 50 inclusive. Use it to your advantage. ~~~ ~~~if:c For C: * Assign the return array length to the pointer parameter `*szout`. * Do not mutate the input array. ~~~ ## Example ```python delete_nth ([1,1,1,1],2) # return [1,1] delete_nth ([20,37,20,21],1) # return [20,37,21] ``` Write your solution by modifying this code: ```python def delete_nth(order,max_e): ``` Your solution should implemented in the function "delete_nth". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n. Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly. Mr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (ai, bi) (1 ≤ i ≤ m) is important. He is planning to construct teleportation pipes so that for each important pair (ai, bi), it will be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively. The following m lines describe the important pairs. The i-th of them (1 ≤ i ≤ m) contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), denoting that it must be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). It is guaranteed that all pairs (ai, bi) are distinct. Output Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose. Examples Input 4 5 1 2 1 3 1 4 2 3 2 4 Output 3 Input 4 6 1 2 1 4 2 3 2 4 3 2 3 4 Output 4 Note For the first sample, one of the optimal ways to construct pipes is shown in the image below: <image> For the second sample, one of the optimal ways is shown below: <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. Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations are forbidden. It was very unfair to Petya as he adored adventure stories that featured lasers and robots. Today Petya is watching a shocking blockbuster about the Martians called "R2:D2". What can "R2:D2" possibly mean? It might be the Martian time represented in the Martian numeral system. Petya knows that time on Mars is counted just like on the Earth (that is, there are 24 hours and each hour has 60 minutes). The time is written as "a:b", where the string a stands for the number of hours (from 0 to 23 inclusive), and string b stands for the number of minutes (from 0 to 59 inclusive). The only thing Petya doesn't know is in what numeral system the Martian time is written. Your task is to print the radixes of all numeral system which can contain the time "a:b". Input The first line contains a single string as "a:b" (without the quotes). There a is a non-empty string, consisting of numbers and uppercase Latin letters. String a shows the number of hours. String b is a non-empty string that consists of numbers and uppercase Latin letters. String b shows the number of minutes. The lengths of strings a and b are from 1 to 5 characters, inclusive. Please note that strings a and b can have leading zeroes that do not influence the result in any way (for example, string "008:1" in decimal notation denotes correctly written time). We consider characters 0, 1, ..., 9 as denoting the corresponding digits of the number's representation in some numeral system, and characters A, B, ..., Z correspond to numbers 10, 11, ..., 35. Output Print the radixes of the numeral systems that can represent the time "a:b" in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "a:b", print the single integer 0. If there are infinitely many numeral systems that can represent the time "a:b", print the single integer -1. Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible. Examples Input 11:20 Output 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Input 2A:13 Output 0 Input 000B:00001 Output -1 Note Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6, represented in the ternary numeral system or as time 17:32 in hexadecimal system. Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 numeral notation. There the given string represents time 32:14 that isn't a correct time. Let's consider the third sample. String "000B:00001" can be perceived as a correct time in the infinite number of numeral systems. If you need an example, you can take any numeral system with radix no less than 12. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Pig Latin is an English language game where the goal is to hide the meaning of a word from people not aware of the rules. So, the goal of this kata is to wite a function that encodes a single word string to pig latin. The rules themselves are rather easy: 1) The word starts with a vowel(a,e,i,o,u) -> return the original string plus "way". 2) The word starts with a consonant -> move consonants from the beginning of the word to the end of the word until the first vowel, then return it plus "ay". 3) The result must be lowercase, regardless of the case of the input. If the input string has any non-alpha characters, the function must return None, null, Nothing (depending on the language). 4) The function must also handle simple random strings and not just English words. 5) The input string has no vowels -> return the original string plus "ay". For example, the word "spaghetti" becomes "aghettispay" because the first two letters ("sp") are consonants, so they are moved to the end of the string and "ay" is appended. Write your solution by modifying this code: ```python def pig_latin(s): ``` Your solution should implemented in the function "pig_latin". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. My arm is stuck and I can't pull it out. I tried to pick up something that had rolled over to the back of my desk, and I inserted my arm while illuminating it with the screen of my cell phone instead of a flashlight, but I accidentally got caught in something and couldn't pull it out. It hurts if you move it forcibly. What should I do. How can I escape? Do you call for help out loud? I'm embarrassed to dismiss it, but it seems difficult to escape on my own. No, no one should have been within reach. Hold down your impatience and look around. If you can reach that computer, you can call for help by email, but unfortunately it's too far away. After all, do we have to wait for someone to pass by by chance? No, wait, there is a machine that sends emails. It's already in your hands, rather than within reach. The screen at the tip of my deeply extended hand is blurry and hard to see, but it's a mobile phone I've been using for many years. You should be able to send emails even if you can't see the screen. Carefully press the button while drawing the screen in your head. Rely on the feel of your finger and type in a message by pressing the number keys many times to avoid making typos. It doesn't convert to kanji, and it should be transmitted even in hiragana. I'm glad I didn't switch to the mainstream smartphones for the touch panel. Take a deep breath and put your finger on the send button. After a while, my cell phone quivered lightly. It is a signal that the transmission is completed. sigh. A friend must come to help after a while. I think my friends are smart enough to interpret it properly, but the message I just sent may be different from the one I really wanted to send. The character input method of this mobile phone is the same as the widely used one, for example, use the "1" button to input the hiragana of the "A" line. "1" stands for "a", "11" stands for "i", and so on, "11111" stands for "o". Hiragana loops, that is, "111111" also becomes "a". The "ka" line uses "2", the "sa" line uses "3", ..., and the behavior when the same button is pressed in succession is the same as in the example of "1". However, this character input method has some annoying behavior. If you do not operate for a while after pressing the number button, the conversion to hiragana will be forced. In other words, if you press "1" three times in a row, it will become "U", but if you press "1" three times after a while, it will become "Ah". To give another example, when you press "111111", the character string entered depends on the interval between the presses, which can be either "a" or "ah ah ah". "111111111111" may be "yes", "yeah ah", or twelve "a". Note that even if you press a different number button, it will be converted to hiragana, so "12345" can only be the character string "Akasatana". Well, I didn't mean to press the wrong button because I typed it carefully, but I'm not confident that I could press the buttons at the right intervals. It's very possible that you've sent something different than the message you really wanted to send. Now, how many strings may have been sent? Oh, this might be a good way to kill time until a friend comes to help. They will come to help within 5 hours at the latest. I hope it can be solved by then. Input The input consists of multiple cases. Each case is given in the following format. string The end of the input is given by the line where the input consists of "#" string contains up to 100,000 numbers between 0 and 9. No more than 50 inputs have a string length greater than 10,000. The test case file size is guaranteed to be 5MB or less. Also, the number of test cases does not exceed 100. The characters that can be entered with each number key are as shown in the table below. Numbers | Enterable characters --- | --- 1 | Aiueo 2 | Kakikukeko 3 | 4 | 5 | What is it? 6 | Hahifuheho 7 | Mamimumemo 8 | Yayuyo 9 | Larry Lero 0 | Won Output Divide how to interpret the sentence by 1000000007 and output the remainder on one line. Examples Input 1 11 111111 111111111111 12345 11111111119999999999 11111111113333333333 11111111118888888888 11111111112222222222111111111 11111111110000000000444444444 11224111122411 888888888888999999999999888888888888999999999999999999 666666666666666777333333333338888888888 1111114444441111111444499999931111111222222222222888111111115555 # Output 1 2 32 1856 1 230400 230400 156480 56217600 38181120 128 26681431 61684293 40046720 Input 1 11 111111 111111111111 12345 11111111119999999999 11111111113333333333 11111111118888888888 11111111112222222222111111111 11111111110000000000444444444 11224111122411 888888888888999999999999888888888888999999999999999999 666666666666666777333333333338888888888 1111114444441111111444499999931111111222222222222888111111115555 Output 1 2 32 1856 1 230400 230400 156480 56217600 38181120 128 26681431 61684293 40046720 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 During this time of winter in Japan, hot days continue in Australia in the Southern Hemisphere. IOI, who lives in Australia, decided to plan what clothes to wear based on the weather forecast for a certain D day. The maximum temperature on day i (1 ≤ i ≤ D) is predicted to be Ti degrees. IOI has N kinds of clothes, which are numbered from 1 to N. Clothes j (1 ≤ j ≤ N) are suitable for wearing on days when the maximum temperature is above Aj and below Bj. In addition, each clothes has an integer called "flashiness", and the flashiness of clothes j is Cj. For each of the D days, IOI chooses to wear one of the clothes suitable for wearing when the maximum temperature follows the weather forecast. You may choose the same clothes as many times as you like, or you may choose clothes that are never chosen in D days. IOI, who wanted to avoid wearing similar clothes in succession, decided to maximize the total absolute value of the difference in the flashiness of the clothes worn on consecutive days. That is, assuming that the clothes xi are selected on the i-day, we want to maximize the values ​​| Cx1 --Cx2 | + | Cx2 --Cx3 | +… + | CxD-1 --CxD |. Create a program to find this maximum value. input The input consists of 1 + D + N lines. On the first line, two integers D and N (2 ≤ D ≤ 200, 1 ≤ N ≤ 200) are written with a blank as a delimiter. D is the number of days to plan clothes and N is the number of clothes types IOI has. One integer Ti (0 ≤ Ti ≤ 60) is written in the i-th line (1 ≤ i ≤ D) of the following D lines. This means that the maximum temperature on day i is forecast to be Ti degrees. In the jth line (1 ≤ j ≤ N) of the following N lines, three integers Aj, Bj, Cj (0 ≤ Aj ≤ Bj ≤ 60, 0 ≤ Cj ≤ 100) are written. These indicate that clothing j is suitable for wearing on days when the maximum temperature is above Aj and below Bj, and the flashiness is Cj. It is guaranteed that there will be at least one piece of clothing suitable for wearing when the maximum temperature follows the weather forecast for every D day. output Output the total absolute value of the difference in the flashiness of clothes worn on consecutive days, that is, the maximum value of the values ​​| Cx1 --Cx2 | + | Cx2 --Cx3 | +… + | CxD-1 --CxD | in one line. .. Input / output example Input example 1 3 4 31 27 35 20 25 30 23 29 90 21 35 60 28 33 40 Output example 1 80 In the input / output example 1, the candidates for clothes on the first day are clothes 3 and 4, the candidates for clothes on the second day are clothes 2 and clothes 3, and the candidates for clothes on the third day are only clothes 3. is there. Choose clothes 4 on the first day, clothes 2 on the second day, and clothes 3 on the third day. That is, x1 = 4, x2 = 2, x3 = 3. At this time, the absolute value of the difference in the flashiness of the clothes on the first day and the second day is | 40 --90 | = 50, and the absolute value of the difference in the flashiness of the clothes on the second and third days is | 90-60 | = 30. The total is 80, which is the maximum value. Input example 2 5 2 26 28 32 29 34 30 35 0 25 30 100 Output example 2 300 In Example 2 of input / output, clothes 2 on the first day, clothes 2 on the second day, clothes 1 on the third day, clothes 2 on the fourth day, and clothes 1 on the fifth day. You have to choose. At this time, the required value is | 100 --100 | + | 100 --0 | + | 0 --100 | + | 100 --0 | = 300. The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics. Example Input 3 4 31 27 35 20 25 30 23 29 90 21 35 60 28 33 40 Output 80 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition: The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees. Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct. Input The first line of the input contains two integers n and m (<image>) — the number of vertices and the number of edges in the graph. Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 ≤ aj ≤ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not. It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero. Output If Vladislav has made a mistake and such graph doesn't exist, print - 1. Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 ≤ uj, vj ≤ n, uj ≠ vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them. Examples Input 4 5 2 1 3 1 4 0 1 1 5 0 Output 2 4 1 4 3 4 3 1 3 2 Input 3 3 1 0 2 1 3 1 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city. Let's represent the city as an area of $n \times n$ square blocks. Yura needs to move from the block with coordinates $(s_x,s_y)$ to the block with coordinates $(f_x,f_y)$. In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are $m$ instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate $x$ or with the same coordinate $y$ as the location. Help Yura to find the smallest time needed to get home. -----Input----- The first line contains two integers $n$ and $m$ — the size of the city and the number of instant-movement locations ($1 \le n \le 10^9$, $0 \le m \le 10^5$). The next line contains four integers $s_x$ $s_y$ $f_x$ $f_y$ — the coordinates of Yura's initial position and the coordinates of his home ($ 1 \le s_x, s_y, f_x, f_y \le n$). Each of the next $m$ lines contains two integers $x_i$ $y_i$ — coordinates of the $i$-th instant-movement location ($1 \le x_i, y_i \le n$). -----Output----- In the only line print the minimum time required to get home. -----Examples----- Input 5 3 1 1 5 5 1 2 4 1 3 3 Output 5 Input 84 5 67 59 41 2 39 56 7 2 15 3 74 18 22 7 Output 42 -----Note----- In the first example Yura needs to reach $(5, 5)$ from $(1, 1)$. He can do that in $5$ minutes by first using the second instant-movement location (because its $y$ coordinate is equal to Yura's $y$ coordinate), and then walking $(4, 1) \to (4, 2) \to (4, 3) \to (5, 3) \to (5, 4) \to (5, 5)$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are n pages numbered by integers from 1 to n. Assume that somebody is on the p-th page now. The navigation will look like this: << p - k p - k + 1 ... p - 1 (p) p + 1 ... p + k - 1 p + k >> When someone clicks the button "<<" he is redirected to page 1, and when someone clicks the button ">>" he is redirected to page n. Of course if someone clicks on a number, he is redirected to the corresponding page. There are some conditions in the navigation: If page 1 is in the navigation, the button "<<" must not be printed. If page n is in the navigation, the button ">>" must not be printed. If the page number is smaller than 1 or greater than n, it must not be printed.   You can see some examples of the navigations. Make a program that prints the navigation. -----Input----- The first and the only line contains three integers n, p, k (3 ≤ n ≤ 100; 1 ≤ p ≤ n; 1 ≤ k ≤ n) -----Output----- Print the proper navigation. Follow the format of the output from the test samples. -----Examples----- Input 17 5 2 Output << 3 4 (5) 6 7 >> Input 6 5 2 Output << 3 4 (5) 6 Input 6 1 2 Output (1) 2 3 >> Input 6 2 2 Output 1 (2) 3 4 >> Input 9 6 3 Output << 3 4 5 (6) 7 8 9 Input 10 6 3 Output << 3 4 5 (6) 7 8 9 >> Input 8 5 4 Output 1 2 3 4 (5) 6 7 8 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is a_{i}. The mood can be positive, zero or negative. Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in. For example, consider the case when the mother has 5 flowers, and their moods are equal to 1, - 2, 1, 3, - 4. Suppose the mother suggested subarrays (1, - 2), (3, - 4), (1, 3), (1, - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds ( - 2)·1 = - 2, because he is in one of chosen subarrays, the third flower adds 1·2 = 2, because he is in two of chosen subarrays, the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays, the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this! Alyona can choose any number of the subarrays, even 0 or all suggested by her mother. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods — n integers a_1, a_2, ..., a_{n} ( - 100 ≤ a_{i} ≤ 100). The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) denoting the subarray a[l_{i}], a[l_{i} + 1], ..., a[r_{i}]. Each subarray can encounter more than once. -----Output----- Print single integer — the maximum possible value added to the Alyona's happiness. -----Examples----- Input 5 4 1 -2 1 3 -4 1 2 4 5 3 4 1 4 Output 7 Input 4 3 1 2 3 4 1 3 2 4 1 1 Output 16 Input 2 2 -1 -2 1 1 1 2 Output 0 -----Note----- The first example is the situation described in the statements. In the second example Alyona should choose all subarrays. The third example has answer 0 because Alyona can choose none of the subarrays. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Background My TV remote control has arrow buttons and an `OK` button. I can use these to move a "cursor" on a logical screen keyboard to type words... # Keyboard The screen "keyboard" layout looks like this #tvkb { width : 400px; border: 5px solid gray; border-collapse: collapse; } #tvkb td { color : orange; background-color : black; text-align : center; border: 3px solid gray; border-collapse: collapse; } abcde123 fghij456 klmno789 pqrst.@0 uvwxyz_/ aASP * `aA` is the SHIFT key. Pressing this key toggles alpha characters between UPPERCASE and lowercase * `SP` is the space character * The other blank keys in the bottom row have no function # Kata task How many button presses on my remote are required to type the given `words`? ## Hint This Kata is an extension of the earlier ones in this series. You should complete those first. ## Notes * The cursor always starts on the letter `a` (top left) * The alpha characters are initially lowercase (as shown above) * Remember to also press `OK` to "accept" each letter * Take the shortest route from one letter to the next * The cursor wraps, so as it moves off one edge it will reappear on the opposite edge * Although the blank keys have no function, you may navigate through them if you want to * Spaces may occur anywhere in the `words` string * Do not press the SHIFT key until you need to. For example, with the word `e.Z`, the SHIFT change happens **after** the `.` is pressed (not before) # Example words = `Code Wars` * C => `a`-`aA`-OK-`A`-`B`-`C`-OK = 6 * o => `C`-`B`-`A`-`aA`-OK-`u`-`v`-`w`-`x`-`y`-`t`-`o`-OK = 12 * d => `o`-`j`-`e`-`d`-OK = 4 * e => `d`-`e`-OK = 2 * space => `e`-`d`-`c`-`b`-`SP`-OK = 5 * W => `SP`-`aA`-OK-`SP`-`V`-`W`-OK = 6 * a => `W`-`V`-`U`-`aA`-OK-`a`-OK = 6 * r => `a`-`f`-`k`-`p`-`q`-`r`-OK = 6 * s => `r`-`s`-OK = 2 Answer = 6 + 12 + 4 + 2 + 5 + 6 + 6 + 6 + 2 = 49 *Good Luck! DM.* Series * TV Remote * TV Remote (shift and space) * TV Remote (wrap) * TV Remote (symbols) Write your solution by modifying this code: ```python def tv_remote(words): ``` Your solution should implemented in the function "tv_remote". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Mainak has an array $a_1, a_2, \ldots, a_n$ of $n$ positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers $l$ and $r$, such that $1 \le l \le r \le n$, and any positive integer $k$. Repeat this $k$ times: set $a_l=a_{l+1}, a_{l+1}=a_{l+2}, \ldots, a_{r-1}=a_r, a_r=a_l$ (all changes happen at the same time). Mainak wants to maximize the value of $(a_n - a_1)$ after exactly one such operation. Determine the maximum value of $(a_n - a_1)$ that he can obtain. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 50$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 999$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$. -----Output----- For each test case, output a single integer — the maximum value of $(a_n - a_1)$ that Mainak can obtain by doing the operation exactly once. -----Examples----- Input 5 6 1 3 9 11 5 7 1 20 3 9 99 999 4 2 1 8 1 3 2 1 5 Output 10 0 990 7 4 -----Note----- In the first test case, we can rotate the subarray from index $3$ to index $6$ by an amount of $2$ (i.e. choose $l = 3$, $r = 6$ and $k = 2$) to get the optimal array: $$[1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]$$ So the answer is $a_n - a_1 = 11 - 1 = 10$. In the second testcase, it is optimal to rotate the subarray starting and ending at index $1$ and rotating it by an amount of $2$. In the fourth testcase, it is optimal to rotate the subarray starting from index $1$ to index $4$ and rotating it by an amount of $3$. So the answer is $8 - 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. Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights w_{i} kilograms. Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions: Take the leftmost item with the left hand and spend w_{i} · l energy units (w_{i} is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Q_{l} energy units; Take the rightmost item with the right hand and spend w_{j} · r energy units (w_{j} is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Q_{r} energy units; Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items. -----Input----- The first line contains five integers n, l, r, Q_{l}, Q_{r} (1 ≤ n ≤ 10^5; 1 ≤ l, r ≤ 100; 1 ≤ Q_{l}, Q_{r} ≤ 10^4). The second line contains n integers w_1, w_2, ..., w_{n} (1 ≤ w_{i} ≤ 100). -----Output----- In the single line print a single number — the answer to the problem. -----Examples----- Input 3 4 4 19 1 42 3 99 Output 576 Input 4 7 2 3 9 1 2 3 4 Output 34 -----Note----- Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units. The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side. One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections. Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares. To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) → (0, 1) → (0, 0) cannot occur in a valid grid path. One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east. Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it. Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1 000 000) — the length of the paths. The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW". The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the second grid path. -----Output----- Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive. -----Examples----- Input 7 NNESWW SWSWSW Output YES Input 3 NN SS Output NO -----Note----- In the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW. In the second sample, no sequence of moves can get both marbles to the end. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. -----Input----- The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. -----Output----- Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. -----Examples----- Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. An ordered sequence of numbers from 1 to N is given. One number might have deleted from it, then the remaining numbers were mixed. Find the number that was deleted. Example: - The starting array sequence is `[1,2,3,4,5,6,7,8,9]` - The mixed array with one deleted number is `[3,2,4,6,7,8,1,9]` - Your function should return the int `5`. If no number was deleted from the array and no difference with it, your function should return the int `0`. Note that N may be 1 or less (in the latter case, the first array will be `[]`). Write your solution by modifying this code: ```python def find_deleted_number(arr, mixed_arr): ``` Your solution should implemented in the function "find_deleted_number". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ m) a_{i} ≠ a_{m} - i + 1. Ivan has a string s consisting of n lowercase Latin letters; n is even. He wants to form some string t that will be an antipalindromic permutation of s. Also Ivan has denoted the beauty of index i as b_{i}, and the beauty of t as the sum of b_{i} among all indices i such that s_{i} = t_{i}. Help Ivan to determine maximum possible beauty of t he can get. -----Input----- The first line contains one integer n (2 ≤ n ≤ 100, n is even) — the number of characters in s. The second line contains the string s itself. It consists of only lowercase Latin letters, and it is guaranteed that its letters can be reordered to form an antipalindromic string. The third line contains n integer numbers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 100), where b_{i} is the beauty of index i. -----Output----- Print one number — the maximum possible beauty of t. -----Examples----- Input 8 abacabac 1 1 1 1 1 1 1 1 Output 8 Input 8 abaccaba 1 2 3 4 5 6 7 8 Output 26 Input 8 abacabca 1 2 3 4 4 3 2 1 Output 17 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 will be given an array of numbers. For each number in the array you will need to create an object. The object key will be the number, as a string. The value will be the corresponding character code, as a string. Return an array of the resulting objects. All inputs will be arrays of numbers. All character codes are valid lower case letters. The input array will not be empty. Write your solution by modifying this code: ```python def num_obj(s): ``` Your solution should implemented in the function "num_obj". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}. You have a sequence of integers a_1, a_2, ..., a_{n}. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence. -----Input----- The first line contains integer n (1 ≤ n ≤ 3·10^5) — the size of the sought permutation. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Print a single number — the minimum number of moves. 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 or the %I64d specifier. -----Examples----- Input 2 3 0 Output 2 Input 3 -1 -1 2 Output 6 -----Note----- In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1). In the second sample you need 6 moves to build permutation (1, 3, 2). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A permutation is a sequence of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as p_{i}. We'll call number n the size of permutation p_1, p_2, ..., p_{n}. Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold p_{p}_{i} = i and p_{i} ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n. -----Input----- A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size. -----Output----- If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p_1, p_2, ..., p_{n} — permutation p, that is perfect. Separate printed numbers by whitespaces. -----Examples----- Input 1 Output -1 Input 2 Output 2 1 Input 4 Output 2 1 4 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. N friends of Takahashi has come to a theme park. To ride the most popular roller coaster in the park, you must be at least K centimeters tall. The i-th friend is h_i centimeters tall. How many of the Takahashi's friends can ride the roller coaster? -----Constraints----- - 1 \le N \le 10^5 - 1 \le K \le 500 - 1 \le h_i \le 500 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N K h_1 h_2 \ldots h_N -----Output----- Print the number of people among the Takahashi's friends who can ride the roller coaster. -----Sample Input----- 4 150 150 140 100 200 -----Sample Output----- 2 Two of them can ride the roller coaster: the first and fourth friends. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 programming competition site AtCode regularly holds programming contests. The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200. The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800. The contest after the ARC is called AGC, which is rated for all contestants. Takahashi's rating on AtCode is R. What is the next contest rated for him? -----Constraints----- - 0 ≤ R ≤ 4208 - R is an integer. -----Input----- Input is given from Standard Input in the following format: R -----Output----- Print the name of the next contest rated for Takahashi (ABC, ARC or AGC). -----Sample Input----- 1199 -----Sample Output----- ABC 1199 is less than 1200, so ABC will be rated. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. The order of a,b,c does matter, and some of them can be the same. Constraints * 1 \leq N,K \leq 2\times 10^5 * N and K are integers. Input Input is given from Standard Input in the following format: N K Output Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. Examples Input 3 2 Output 9 Input 5 3 Output 1 Input 31415 9265 Output 27 Input 35897 932 Output 114191 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: - Operation A: The displayed value is doubled. - Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. -----Constraints----- - 1 \leq N, K \leq 10 - All input values are integers. -----Input----- Input is given from Standard Input in the following format: N K -----Output----- Print the minimum possible value displayed in the board after N operations. -----Sample Input----- 4 3 -----Sample Output----- 10 The value will be minimized when the operations are performed in the following order: A, A, B, B. In this case, the value will change as follows: 1 → 2 → 4 → 7 → 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. DZY loves Physics, and he enjoys calculating density. Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows: <image> where v is the sum of the values of the nodes, e is the sum of the values of the edges. Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible. An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies: * <image>; * edge <image> if and only if <image>, and edge <image>; * the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node. Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected. <image> Input The first line contains two space-separated integers n (1 ≤ n ≤ 500), <image>. Integer n represents the number of nodes of the graph G, m represents the number of edges. The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n. Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges. Output Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9. Examples Input 1 0 1 Output 0.000000000000000 Input 2 1 1 2 1 2 1 Output 3.000000000000000 Input 5 6 13 56 73 98 17 1 2 56 1 3 29 1 4 42 2 3 95 2 4 88 3 4 63 Output 2.965517241379311 Note In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1. In the second sample, choosing the whole graph is optimal. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' $\rightarrow$ 'y' $\rightarrow$ 'x' $\rightarrow \ldots \rightarrow$ 'b' $\rightarrow$ 'a' $\rightarrow$ 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'. What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once? -----Input----- The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters. -----Output----- Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring. -----Examples----- Input codeforces Output bncdenqbdr Input abacaba Output aaacaba -----Note----- String s is lexicographically smaller than some other string t of the same length if there exists some 1 ≤ i ≤ |s|, such that s_1 = t_1, s_2 = t_2, ..., s_{i} - 1 = t_{i} - 1, and s_{i} < t_{i}. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Today, Chef woke up to find that he had no clean socks. Doing laundry is such a turn-off for Chef, that in such a situation, he always buys new socks instead of cleaning the old dirty ones. He arrived at the fashion store with money rupees in his pocket and started looking for socks. Everything looked good, but then Chef saw a new jacket which cost jacketCost rupees. The jacket was so nice that he could not stop himself from buying it. Interestingly, the shop only stocks one kind of socks, enabling them to take the unsual route of selling single socks, instead of the more common way of selling in pairs. Each of the socks costs sockCost rupees. Chef bought as many socks as he could with his remaining money. It's guaranteed that the shop has more socks than Chef can buy. But now, he is interested in the question: will there be a day when he will have only 1 clean sock, if he uses a pair of socks each day starting tommorow? If such an unlucky day exists, output "Unlucky Chef", otherwise output "Lucky Chef". Remember that Chef never cleans or reuses any socks used once. -----Input----- The first line of input contains three integers — jacketCost, sockCost, money — denoting the cost of a jacket, cost of a single sock, and the initial amount of money Chef has, respectively. -----Output----- In a single line, output "Unlucky Chef" if such a day exists. Otherwise, output "Lucky Chef". -----Constraints----- - 1 ≤ jacketCost ≤ money ≤ 109 - 1 ≤ sockCost ≤ 109 -----Example----- Input: 1 2 3 Output: Unlucky Chef Input: 1 2 6 Output: Lucky Chef -----Subtasks----- - Subtask 1: jacketCost, money, sockCost ≤ 103. Points - 20 - Subtask 2: Original constraints. Points - 80 -----Explanation----- Test #1: When Chef arrived at the shop, he had 3 rupees. After buying the jacket, he has 2 rupees left, enough to buy only 1 sock. Test #2: Chef had 6 rupees in the beginning. After buying the jacket, he has 5 rupees left, enough to buy a pair of socks for 4 rupees. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section. Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: <image> As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him. Input The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000. Output Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section. Examples Input 1 2 Output 1 Input 5 1 2 1 2 1 Output 3 Input 8 1 2 1 1 1 3 3 4 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. Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished. In total, Nastya dropped $n$ grains. Nastya read that each grain weighs some integer number of grams from $a - b$ to $a + b$, inclusive (numbers $a$ and $b$ are known), and the whole package of $n$ grains weighs from $c - d$ to $c + d$ grams, inclusive (numbers $c$ and $d$ are known). The weight of the package is the sum of the weights of all $n$ grains in it. Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $i$-th grain weighs some integer number $x_i$ $(a - b \leq x_i \leq a + b)$, and in total they weigh from $c - d$ to $c + d$, inclusive ($c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ $(1 \leq t \leq 1000)$  — the number of test cases. The next $t$ lines contain descriptions of the test cases, each line contains $5$ integers: $n$ $(1 \leq n \leq 1000)$  — the number of grains that Nastya counted and $a, b, c, d$ $(0 \leq b < a \leq 1000, 0 \leq d < c \leq 1000)$  — numbers that determine the possible weight of one grain of rice (from $a - b$ to $a + b$) and the possible total weight of the package (from $c - d$ to $c + d$). -----Output----- For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $n$ grains with masses from $a - b$ to $a + b$ cannot make a package with a total mass from $c - d$ to $c + d$. -----Example----- Input 5 7 20 3 101 18 11 11 10 234 2 8 9 7 250 122 19 41 21 321 10 3 10 8 6 1 Output Yes No Yes No Yes -----Note----- In the first test case of the example, we can assume that each grain weighs $17$ grams, and a pack $119$ grams, then really Nastya could collect the whole pack. In the third test case of the example, we can assume that each grain weighs $16$ grams, and a pack $128$ grams, then really Nastya could collect the whole pack. In the fifth test case of the example, we can be assumed that $3$ grains of rice weigh $2$, $2$, and $3$ grams, and a pack is $7$ grams, then really Nastya could collect the whole pack. In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Write a function that accepts a string, and returns true if it is in the form of a phone number. Assume that any integer from 0-9 in any of the spots will produce a valid phone number. Only worry about the following format: (123) 456-7890 (don't forget the space after the close parentheses) Examples: ``` validPhoneNumber("(123) 456-7890") => returns true validPhoneNumber("(1111)555 2345") => returns false validPhoneNumber("(098) 123 4567") => returns false ``` Write your solution by modifying this code: ```python def validPhoneNumber(phoneNumber): ``` Your solution should implemented in the function "validPhoneNumber". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Happy Farm 5 creators decided to invent the mechanism of cow grazing. The cows in the game are very slow and they move very slowly, it can even be considered that they stand still. However, carnivores should always be chased off them. For that a young player Vasya decided to make the shepherd run round the cows along one and the same closed path. It is very important that the cows stayed strictly inside the area limited by the path, as otherwise some cows will sooner or later be eaten. To be absolutely sure in the cows' safety, Vasya wants the path completion time to be minimum. The new game is launched for different devices, including mobile phones. That's why the developers decided to quit using the arithmetics with the floating decimal point and use only the arithmetics of integers. The cows and the shepherd in the game are represented as points on the plane with integer coordinates. The playing time is modeled by the turns. During every turn the shepherd can either stay where he stands or step in one of eight directions: horizontally, vertically, or diagonally. As the coordinates should always remain integer, then the length of a horizontal and vertical step is equal to 1, and the length of a diagonal step is equal to <image>. The cows do not move. You have to minimize the number of moves the shepherd needs to run round the whole herd. Input The first line contains an integer N which represents the number of cows in the herd (1 ≤ N ≤ 105). Each of the next N lines contains two integers Xi and Yi which represent the coordinates of one cow of (|Xi|, |Yi| ≤ 106). Several cows can stand on one point. Output Print the single number — the minimum number of moves in the sought path. Examples Input 4 1 1 5 1 5 3 1 3 Output 16 Note Picture for the example test: The coordinate grid is painted grey, the coordinates axes are painted black, the cows are painted red and the sought route is painted green. <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. AtCoder Inc. holds a contest every Saturday. There are two types of contests called ABC and ARC, and just one of them is held at a time. The company holds these two types of contests alternately: an ARC follows an ABC and vice versa. Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week. -----Constraints----- - S is ABC or ARC. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the string representing the type of the contest held this week. -----Sample Input----- ABC -----Sample Output----- ARC They held an ABC last week, so they will hold an ARC this week. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters. You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him. Constraints * 3 \leq |S| \leq 20 * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: S Output Print your answer. Examples Input takahashi Output tak Input naohiro Output nao Read the inputs from stdin solve the problem and write the answer to stdout (do 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 as input, move all of its vowels to the end of the string, in the same order as they were before. Vowels are (in this kata): `a, e, i, o, u` Note: all provided input strings are lowercase. ## Examples ```python "day" ==> "dya" "apple" ==> "pplae" ``` Write your solution by modifying this code: ```python def move_vowels(input): ``` Your solution should implemented in the function "move_vowels". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Fair Nut found a string $s$. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences $p_1, p_2, \ldots, p_k$, such that: For each $i$ ($1 \leq i \leq k$), $s_{p_i} =$ 'a'. For each $i$ ($1 \leq i < k$), there is such $j$ that $p_i < j < p_{i + 1}$ and $s_j =$ 'b'. The Nut is upset because he doesn't know how to find the number. Help him. This number should be calculated modulo $10^9 + 7$. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 10^5$) consisting of lowercase Latin letters. -----Output----- In a single line print the answer to the problem — the number of such sequences $p_1, p_2, \ldots, p_k$ modulo $10^9 + 7$. -----Examples----- Input abbaa Output 5 Input baaaa Output 4 Input agaa Output 3 -----Note----- In the first example, there are $5$ possible sequences. $[1]$, $[4]$, $[5]$, $[1, 4]$, $[1, 5]$. In the second example, there are $4$ possible sequences. $[2]$, $[3]$, $[4]$, $[5]$. In the third example, there are $3$ possible sequences. $[1]$, $[3]$, $[4]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. $N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours. Constraints * $ 1 \leq N \leq 10^5 $ * $ 1 \leq T \leq 10^5 $ * $ 0 \leq l_i < r_i \leq T $ Input The input is given in the following format. $N$ $T$ $l_1$ $r_1$ $l_2$ $r_2$ : $l_N$ $r_N$ Output Print the maximum number of persons in a line. Examples Input 6 10 0 2 1 3 2 6 3 8 4 10 5 10 Output 4 Input 2 2 0 1 1 2 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. When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to d_{i}. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most x from the root. The distance is the sum of the lengths of edges on the path between nodes. But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' — you may ask. It's just that he feels superior knowing that only he can solve this problem. Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 10^9 + 7. -----Input----- The first line contains two space-separated integers n and x (1 ≤ n ≤ 10^5, 0 ≤ x ≤ 10^9) — the number of children of each node and the distance from the root within the range of which you need to count the nodes. The next line contains n space-separated integers d_{i} (1 ≤ d_{i} ≤ 100) — the length of the edge that connects each node with its i-th child. -----Output----- Print a single number — the number of vertexes in the tree at distance from the root equal to at most x. -----Examples----- Input 3 3 1 2 3 Output 8 -----Note----- Pictures to the sample (the yellow color marks the nodes the distance to which is at most three) [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a permutation of 1,2,...,N: p_1,p_2,...,p_N. Determine if the state where p_i=i for every i can be reached by performing the following operation any number of times: - Choose three elements p_{i-1},p_{i},p_{i+1} (2\leq i\leq N-1) such that p_{i-1}>p_{i}>p_{i+1} and reverse the order of these three. -----Constraints----- - 3 \leq N \leq 3 × 10^5 - p_1,p_2,...,p_N is a permutation of 1,2,...,N. -----Input----- Input is given from Standard Input in the following format: N p_1 : p_N -----Output----- If the state where p_i=i for every i can be reached by performing the operation, print Yes; otherwise, print No. -----Sample Input----- 5 5 2 1 4 3 -----Sample Output----- Yes The state where p_i=i for every i can be reached as follows: - Reverse the order of p_1,p_2,p_3. The sequence p becomes 1,2,5,4,3. - Reverse the order of p_3,p_4,p_5. The sequence p becomes 1,2,3,4,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. Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [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. Please write a function that sums a list, but ignores any duplicate items in the list. For instance, for the list [3, 4, 3, 6] , the function should return 10. Write your solution by modifying this code: ```python def sum_no_duplicates(l): ``` Your solution should implemented in the function "sum_no_duplicates". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.