question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Overall there are m actors in Berland. Each actor has a personal identifier β€” an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors. Vasya looks at his records and wonders which movies may be his favourite, and which ones may not be. Once Vasya learns the exact cast of all movies, his favorite movies will be determined as follows: a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors. Help the boy to determine the following for each movie: * whether it surely will be his favourite movie; * whether it surely won't be his favourite movie; * can either be favourite or not. Input The first line of the input contains two integers m and k (1 ≀ m ≀ 100, 1 ≀ k ≀ m) β€” the number of actors in Berland and the number of Vasya's favourite actors. The second line contains k distinct integers ai (1 ≀ ai ≀ m) β€” the identifiers of Vasya's favourite actors. The third line contains a single integer n (1 ≀ n ≀ 100) β€” the number of movies in Vasya's list. Then follow n blocks of lines, each block contains a movie's description. The i-th movie's description contains three lines: * the first line contains string si (si consists of lowercase English letters and can have the length of from 1 to 10 characters, inclusive) β€” the movie's title, * the second line contains a non-negative integer di (1 ≀ di ≀ m) β€” the number of actors who starred in this movie, * the third line has di integers bi, j (0 ≀ bi, j ≀ m) β€” the identifiers of the actors who star in this movie. If bi, j = 0, than Vasya doesn't remember the identifier of the j-th actor. It is guaranteed that the list of actors for a movie doesn't contain the same actors. All movies have distinct names. The numbers on the lines are separated by single spaces. Output Print n lines in the output. In the i-th line print: * 0, if the i-th movie will surely be the favourite; * 1, if the i-th movie won't surely be the favourite; * 2, if the i-th movie can either be favourite, or not favourite. Examples Input 5 3 1 2 3 6 firstfilm 3 0 0 0 secondfilm 4 0 0 4 5 thirdfilm 1 2 fourthfilm 1 5 fifthfilm 1 4 sixthfilm 2 1 0 Output 2 2 1 1 1 2 Input 5 3 1 3 5 4 jumanji 3 0 0 0 theeagle 5 1 2 3 4 0 matrix 3 2 4 0 sourcecode 2 2 4 Output 2 0 1 1 Note Note to the second sample: * Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. * Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. * Movie matrix can have exactly one favourite actor. * Movie sourcecode doesn't have any favourite actors. Thus, movie theeagle will surely be favourite, movies matrix and sourcecode won't surely be favourite, and movie jumanji can be either favourite (if it has all three favourite actors), or not favourite. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gildong has an interesting machine that has an array $a$ with $n$ integers. The machine supports two kinds of operations: Increase all elements of a suffix of the array by $1$. Decrease all elements of a suffix of the array by $1$. A suffix is a subsegment (contiguous elements) of the array that contains $a_n$. In other words, for all $i$ where $a_i$ is included in the subsegment, all $a_j$'s where $i \lt j \le n$ must also be included in the subsegment. Gildong wants to make all elements of $a$ equal β€” he will always do so using the minimum number of operations necessary. To make his life even easier, before Gildong starts using the machine, you have the option of changing one of the integers in the array to any other integer. You are allowed to leave the array unchanged. You want to minimize the number of operations Gildong performs. With your help, what is the minimum number of operations Gildong will perform? Note that even if you change one of the integers in the array, you should not count that as one of the operations because Gildong did not perform it. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Each test case contains two lines. The first line of each test case consists of an integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of elements of the array $a$. The second line of each test case contains $n$ integers. The $i$-th integer is $a_i$ ($-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8$). It is guaranteed that the sum of $n$ in all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer β€” the minimum number of operations Gildong has to perform in order to make all elements of the array equal. -----Examples----- Input 7 2 1 1 3 -1 0 2 4 99 96 97 95 4 -3 -5 -2 1 6 1 4 3 2 4 1 5 5 0 0 0 5 9 -367741579 319422997 -415264583 -125558838 -300860379 420848004 294512916 -383235489 425814447 Output 0 1 3 4 6 5 2847372102 -----Note----- In the first case, all elements of the array are already equal. Therefore, we do not change any integer and Gildong will perform zero operations. In the second case, we can set $a_3$ to be $0$, so that the array becomes $[-1,0,0]$. Now Gildong can use the $2$-nd operation once on the suffix starting at $a_2$, which means $a_2$ and $a_3$ are decreased by $1$, making all elements of the array $-1$. In the third case, we can set $a_1$ to $96$, so that the array becomes $[96,96,97,95]$. Now Gildong needs to: Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[96,96,96,94]$. Use the $1$-st operation on the suffix starting at $a_4$ $2$ times, making the array $[96,96,96,96]$. In the fourth case, we can change the array into $[-3,-3,-2,1]$. Now Gildong needs to: Use the $2$-nd operation on the suffix starting at $a_4$ $3$ times, making the array $[-3,-3,-2,-2]$. Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[-3,-3,-3,-3]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town. Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar). Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. -----Input----- The first line contains two space-separated integers n, s (1 ≀ n, s ≀ 100). The i-th of the next n lines contains two integers x_{i}, y_{i} (1 ≀ x_{i} ≀ 100;Β 0 ≀ y_{i} < 100), where x_{i} represents the number of dollars and y_{i} the number of cents needed in order to buy the i-th type of sugar. -----Output----- Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar. -----Examples----- Input 5 10 3 90 12 0 9 70 5 50 7 0 Output 50 Input 5 5 10 10 20 20 30 30 40 40 50 50 Output -1 -----Note----- In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Lunar New Year is approaching, and Bob is planning to go for a famous restaurant β€” "Alice's". The restaurant "Alice's" serves $n$ kinds of food. The cost for the $i$-th kind is always $c_i$. Initially, the restaurant has enough ingredients for serving exactly $a_i$ dishes of the $i$-th kind. In the New Year's Eve, $m$ customers will visit Alice's one after another and the $j$-th customer will order $d_j$ dishes of the $t_j$-th kind of food. The $(i + 1)$-st customer will only come after the $i$-th customer is completely served. Suppose there are $r_i$ dishes of the $i$-th kind remaining (initially $r_i = a_i$). When a customer orders $1$ dish of the $i$-th kind, the following principles will be processed. If $r_i > 0$, the customer will be served exactly $1$ dish of the $i$-th kind. The cost for the dish is $c_i$. Meanwhile, $r_i$ will be reduced by $1$. Otherwise, the customer will be served $1$ dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by $1$. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is $0$. If the customer doesn't leave after the $d_j$ dishes are served, the cost for the customer will be the sum of the cost for these $d_j$ dishes. Please determine the total cost for each of the $m$ customers. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$), representing the number of different kinds of food and the number of customers, respectively. The second line contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^7$), where $a_i$ denotes the initial remain of the $i$-th kind of dishes. The third line contains $n$ positive integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^6$), where $c_i$ denotes the cost of one dish of the $i$-th kind. The following $m$ lines describe the orders of the $m$ customers respectively. The $j$-th line contains two positive integers $t_j$ and $d_j$ ($1 \leq t_j \leq n$, $1 \leq d_j \leq 10^7$), representing the kind of food and the number of dishes the $j$-th customer orders, respectively. -----Output----- Print $m$ lines. In the $j$-th line print the cost for the $j$-th customer. -----Examples----- Input 8 5 8 6 2 1 4 5 7 5 6 3 3 2 6 2 3 2 2 8 1 4 4 7 3 4 6 10 Output 22 24 14 10 39 Input 6 6 6 6 6 6 6 6 6 66 666 6666 66666 666666 1 6 2 6 3 6 4 6 5 6 6 66 Output 36 396 3996 39996 399996 0 Input 6 6 6 6 6 6 6 6 6 66 666 6666 66666 666666 1 6 2 13 3 6 4 11 5 6 6 6 Output 36 11058 99996 4333326 0 0 -----Note----- In the first sample, $5$ customers will be served as follows. Customer $1$ will be served $6$ dishes of the $2$-nd kind, $1$ dish of the $4$-th kind, and $1$ dish of the $6$-th kind. The cost is $6 \cdot 3 + 1 \cdot 2 + 1 \cdot 2 = 22$. The remain of the $8$ kinds of food will be $\{8, 0, 2, 0, 4, 4, 7, 5\}$. Customer $2$ will be served $4$ dishes of the $1$-st kind. The cost is $4 \cdot 6 = 24$. The remain will be $\{4, 0, 2, 0, 4, 4, 7, 5\}$. Customer $3$ will be served $4$ dishes of the $6$-th kind, $3$ dishes of the $8$-th kind. The cost is $4 \cdot 2 + 3 \cdot 2 = 14$. The remain will be $\{4, 0, 2, 0, 4, 0, 7, 2\}$. Customer $4$ will be served $2$ dishes of the $3$-rd kind, $2$ dishes of the $8$-th kind. The cost is $2 \cdot 3 + 2 \cdot 2 = 10$. The remain will be $\{4, 0, 0, 0, 4, 0, 7, 0\}$. Customer $5$ will be served $7$ dishes of the $7$-th kind, $3$ dishes of the $1$-st kind. The cost is $7 \cdot 3 + 3 \cdot 6 = 39$. The remain will be $\{1, 0, 0, 0, 4, 0, 0, 0\}$. In the second sample, each customer is served what they order except the last one, who leaves angrily without paying. For example, the second customer is served $6$ dishes of the second kind, so the cost is $66 \cdot 6 = 396$. In the third sample, some customers may not be served what they order. For example, the second customer is served $6$ dishes of the second kind, $6$ of the third and $1$ of the fourth, so the cost is $66 \cdot 6 + 666 \cdot 6 + 6666 \cdot 1 = 11058$. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 washing machine uses ```water``` amount of water to wash ```clothes``` amount of clothes. You are given a ```load``` amount of clothes to wash. For each single item of load above the standard amount of clothes, the washing machine will use 10% more water (multiplicative) to clean. For example, if the amount of clothes is ```10```, the amount of water it requires is ```5``` and the load is ```14```, then you need ```5 * 1.1 ^ (14 - 10)``` amount of water. Write a function ```howMuchWater``` (JS)/```how_much_water``` (Python) to work out how much water is needed if you have a ```clothes``` amount of clothes. The function will accept 3 parameters - ```howMuchWater(water, load, clothes)``` / ```how_much_water(water, load, clothes)``` My washing machine is an old model that can only handle double the amount of ```load```. If the amount of ```clothes``` is more than 2 times the standard amount of ```load```, return ```'Too much clothes'```. The washing machine also cannot handle any amount of clothes less than ```load```. If that is the case, return ```'Not enough clothes'```. The answer should be rounded to the nearest 2 decimal places. Write your solution by modifying this code: ```python def how_much_water(water, load, clothes): ``` Your solution should implemented in the function "how_much_water". 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. Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $80\%$ of applicants are girls and majority of them are going to live in the university dormitory for the next $4$ (hopefully) years. The dormitory consists of $n$ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $i$ costs $c_i$ burles. Rooms are numbered from $1$ to $n$. Mouse doesn't sit in place all the time, it constantly runs. If it is in room $i$ in second $t$ then it will run to room $a_i$ in second $t + 1$ without visiting any other rooms inbetween ($i = a_i$ means that mouse won't leave room $i$). It's second $0$ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap. That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $1$ to $n$ at second $0$. What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from? -----Input----- The first line contains as single integers $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of rooms in the dormitory. The second line contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^4$) β€” $c_i$ is the cost of setting the trap in room number $i$. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β€” $a_i$ is the room the mouse will run to the next second after being in room $i$. -----Output----- Print a single integer β€” the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from. -----Examples----- Input 5 1 2 3 2 10 1 3 4 3 3 Output 3 Input 4 1 10 2 10 2 4 2 2 Output 10 Input 7 1 1 1 1 1 1 1 2 2 2 3 6 7 6 Output 2 -----Note----- In the first example it is enough to set mouse trap in rooms $1$ and $4$. If mouse starts in room $1$ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $4$. In the second example it is enough to set mouse trap in room $2$. If mouse starts in room $2$ then it gets caught immideately. If mouse starts in any other room then it runs to room $2$ in second $1$. Here are the paths of the mouse from different starts from the third example: $1 \rightarrow 2 \rightarrow 2 \rightarrow \dots$; $2 \rightarrow 2 \rightarrow \dots$; $3 \rightarrow 2 \rightarrow 2 \rightarrow \dots$; $4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots$; $5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots$; $6 \rightarrow 7 \rightarrow 6 \rightarrow \dots$; $7 \rightarrow 6 \rightarrow 7 \rightarrow \dots$; So it's enough to set traps in rooms $2$ and $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. Count how often sign changes in array. ### result number from `0` to ... . Empty array returns `0` ### example Write your solution by modifying this code: ```python def catch_sign_change(lst): ``` Your solution should implemented in the function "catch_sign_change". 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. - Kids drink toddy. - Teens drink coke. - Young adults drink beer. - Adults drink whisky. Make a function that receive age, and return what they drink. **Rules:** - Children under 14 old. - Teens under 18 old. - Young under 21 old. - Adults have 21 or more. **Examples:** ```python people_with_age_drink(13) == "drink toddy" people_with_age_drink(17) == "drink coke" people_with_age_drink(18) == "drink beer" people_with_age_drink(20) == "drink beer" people_with_age_drink(30) == "drink whisky" ``` Write your solution by modifying this code: ```python def people_with_age_drink(age): ``` Your solution should implemented in the function "people_with_age_drink". 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. Dee is lazy but she's kind and she likes to eat out at all the nice restaurants and gastropubs in town. To make paying quick and easy she uses a simple mental algorithm she's called The Fair %20 Rule. She's gotten so good she can do this in a few seconds and it always impresses her dates but she's perplexingly still single. Like you probably. This is how she does it: - She rounds the price `P` at the tens place e.g: - 25 becomes 30 - 24 becomes 20 - 5 becomes 10 - 4 becomes 0 - She figures out the base tip `T` by dropping the singles place digit e.g: - when `P = 24` she rounds to 20 drops 0 `T = 2` - `P = 115` rounds to 120 drops 0 `T = 12` - `P = 25` rounds to 30 drops 0 `T = 3` - `P = 5` rounds to 10 drops 0 `T = 1` - `P = 4` rounds to 0 `T = 0` - She then applies a 3 point satisfaction rating `R` to `T` i.e: - When she's satisfied: `R = 1` and she'll add 1 to `T` - Unsatisfied: `R = 0` and she'll subtract 1 from `T` - Appalled: `R = -1` she'll divide `T` by 2, **rounds down** and subtracts 1 ## Your Task Implement a method `calc_tip` that takes two integer arguments for price `p` where `1 <= p <= 1000` and a rating `r` which is one of `-1, 0, 1`. The return value `T` should be a non negative integer. *Note: each step should be done in the order listed.* Dee always politely smiles and says "Thank you" on her way out. Dee is nice. Be like Dee. Write your solution by modifying this code: ```python def calc_tip(p, r): ``` Your solution should implemented in the function "calc_tip". 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. For a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ which is sorted by ascending order, eliminate all equivalent elements. Constraints * $1 \leq n \leq 100,000$ * $-1000,000,000 \leq a_i \leq 1,000,000,000$ * $a_0 \leq a_1 \leq ... \leq a_{n-1}$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ,..., \; a_{n-1}$ Output Print the sequence after eliminating equivalent elements in a line. Separate adjacency elements by a space character. Example Input 4 1 2 2 4 Output 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. The vowel substrings in the word `codewarriors` are `o,e,a,io`. The longest of these has a length of 2. Given a lowercase string that has alphabetic characters only (both vowels and consonants) and no spaces, return the length of the longest vowel substring. Vowels are any of `aeiou`. ```if:csharp Documentation: Kata.Solve Method (String) Returns the length of the greatest continuous vowel substring in a string. Syntax public static int Solve( string str Β Β ) Parameters str Type: System.String The string to be processed. Return Value Type: System.Int32 The length of the greatest continuous vowel substring in str, or 0 if str contains no vowels. Exceptions Exception Condition ArgumentNullException str is null. ``` Good luck! If you like substring Katas, please try: [Non-even substrings](https://www.codewars.com/kata/59da47fa27ee00a8b90000b4) [Vowel-consonant lexicon](https://www.codewars.com/kata/59cf8bed1a68b75ffb000026) Write your solution by modifying this code: ```python def solve(s): ``` Your solution should implemented in the function "solve". 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. Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters. Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length. -----Input----- The first line of the input contains number n (1 ≀ n ≀ 100)Β β€” the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input. -----Output----- Print a single integerΒ β€” the maximum possible total length of words in Andrew's article. -----Examples----- Input 4 abb cacc aaa bbb Output 9 Input 5 a a bcbcb cdecdecdecdecdecde aaaa Output 6 -----Note----- In the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}. In the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polygons are the most fundamental objects in geometric processing. Complex figures are often represented and handled as polygons with many short sides. If you are interested in the processing of geometric data, you'd better try some programming exercises about basic operations on polygons. Your job in this problem is to write a program that computes the area of polygons. A polygon is represented by a sequence of points that are its vertices. If the vertices p1, p2, ..., pn are given, line segments connecting pi and pi+1 (1 <= i <= n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon. You can assume that the polygon is not degenerate. Namely, the following facts can be assumed without any input data checking. * No point will occur as a vertex more than once. * Two sides can intersect only at a common endpoint (vertex). * The polygon has at least 3 vertices. Note that the polygon is not necessarily convex. In other words, an inner angle may be larger than 180 degrees. Input The input contains multiple data sets, each representing a polygon. A data set is given in the following format. n x1 y1 x2 y2 ... xn yn The first integer n is the number of vertices, such that 3 <= n <= 50. The coordinate of a vertex pi is given by (xi, yi). xi and yi are integers between 0 and 1000 inclusive. The coordinates of vertices are given in the order of clockwise visit of them. The end of input is indicated by a data set with 0 as the value of n. Output For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the polygon separated by a single space. The area should be printed with one digit to the right of the decimal point. The sequence number and the area should be printed on the same line. Since your result is checked by an automatic grading program, you should not insert any extra characters nor lines on the output. Example Input 3 1 1 3 4 6 0 7 0 0 10 10 0 20 10 30 0 40 100 40 100 0 0 Output 1 8.5 2 3800.0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Farmer Bob have a big farm, where he growths chickens, rabbits and cows. It is very difficult to count the number of animals for each type manually, so he diceded to buy a system to do it. But he bought a cheap system that can count only total number of heads, total number of legs and total number of horns of animals on the farm. Help Bob to figure out how many chickens, rabbits and cows does he have? All chickens have 2 legs, 1 head and no horns; all rabbits have 4 legs, 1 head and no horns; all cows have 4 legs, 1 head and 2 horns. Your task is to write a function ```Python get_animals_count(legs_number, heads_number, horns_number) ``` ```Csharp Dictionary get_animals_count(int legs_number, int heads_number, int horns_number) ``` , which returns a dictionary ```python {"rabbits" : rabbits_count, "chickens" : chickens_count, "cows" : cows_count} ``` ```Csharp new Dictionary(){{"rabbits", rabbits_count},{"chickens", chickens_count},{"cows", cows_count}} ``` Parameters `legs_number, heads_number, horns_number` are integer, all tests have valid input. Example: ```python get_animals_count(34, 11, 6); # Should return {"rabbits" : 3, "chickens" : 5, "cows" : 3} get_animals_count(154, 42, 10); # Should return {"rabbits" : 30, "chickens" : 7, "cows" : 5} ``` ```Csharp get_animals_count(34, 11, 6); //Should return Dictionary(){{"rabbits", 3},{"chickens", 5},{"cows", 3}} get_animals_count(154, 42, 10); //Should return Dictionary(){{"rabbits", 30},{"chickens", 7},{"cows", 5}} ``` Write your solution by modifying this code: ```python def get_animals_count(legs, heads, horns): ``` Your solution should implemented in the function "get_animals_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. So many wall designs to choose from! Even modulo 106 + 3, it's an enormous number. Given that recently Heidi acquired an unlimited supply of bricks, her choices are endless! She really needs to do something to narrow them down. Heidi is quick to come up with criteria for a useful wall: * In a useful wall, at least one segment is wider than W bricks. This should give the zombies something to hit their heads against. Or, * in a useful wall, at least one column is higher than H bricks. This provides a lookout from which zombies can be spotted at a distance. This should rule out a fair amount of possibilities, right? Help Heidi compute the number of useless walls that do not confirm to either of these criteria. In other words, a wall is useless if every segment has width at most W and height at most H. Parameter C, the total width of the wall, has the same meaning as in the easy version. However, note that the number of bricks is now unlimited. Output the number of useless walls modulo 106 + 3. Input The first and the only line of the input contains three space-separated integers C, W and H (1 ≀ C ≀ 108, 1 ≀ W, H ≀ 100). Output Output the number of different walls, modulo 106 + 3, which are useless according to Heidi's criteria. Examples Input 1 1 1 Output 2 Input 1 2 2 Output 3 Input 1 2 3 Output 4 Input 3 2 2 Output 19 Input 5 4 9 Output 40951 Input 40 37 65 Output 933869 Note If there is no brick in any of the columns, the structure is considered as a useless wall. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. -----Input----- The first line contains four integers: x_{p}, y_{p}, x_{v}, y_{v} (0 ≀ x_{p}, y_{p}, x_{v}, y_{v} ≀ 10^5) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). -----Output----- Output the name of the winner: "Polycarp" or "Vasiliy". -----Examples----- Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy -----Note----- In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi is distributing N balls to K persons. If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls? Constraints * 1 \leq K \leq N \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: N K Output Print the maximum possible difference in the number of balls received. Examples Input 3 2 Output 1 Input 3 1 Output 0 Input 8 5 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. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 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. In Dark Souls, players level up trading souls for stats. 8 stats are upgradable this way: vitality, attunement, endurance, strength, dexterity, resistance, intelligence, and faith. Each level corresponds to adding one point to a stat of the player's choice. Also, there are 10 possible classes each having their own starting level and stats: ``` Warrior (Level 4): 11, 8, 12, 13, 13, 11, 9, 9 Knight (Level 5): 14, 10, 10, 11, 11, 10, 9, 11 Wanderer (Level 3): 10, 11, 10, 10, 14, 12, 11, 8 Thief (Level 5): 9, 11, 9, 9, 15, 10, 12, 11 Bandit (Level 4): 12, 8, 14, 14, 9, 11, 8, 10 Hunter (Level 4): 11, 9, 11, 12, 14, 11, 9, 9 Sorcerer (Level 3): 8, 15, 8, 9, 11, 8, 15, 8 Pyromancer (Level 1): 10, 12, 11, 12, 9, 12, 10, 8 Cleric (Level 2): 11, 11, 9, 12, 8, 11, 8, 14 Deprived (Level 6): 11, 11, 11, 11, 11, 11, 11, 11 ``` From level 1, the necessary souls to level up each time up to 11 are `673`, `690`, `707`, `724`, `741`, `758`, `775`, `793`, `811`, and `829`. Then from 11 to 12 and onwards the amount is defined by the expression `round(pow(x, 3) * 0.02 + pow(x, 2) * 3.06 + 105.6 * x - 895)` where `x` is the number corresponding to the next level. Your function will receive a string with the character class and a list of stats. It should calculate which level is required to get the desired character build and the amount of souls needed to do so. The result should be a string in the format: `'Starting as a [CLASS], level [N] will require [M] souls.'` where `[CLASS]` is your starting class, `[N]` is the required level, and `[M]` is the amount of souls needed respectively. Write your solution by modifying this code: ```python def souls(character, build): ``` Your solution should implemented in the function "souls". 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 number m of the form 10x + y is divisible by 7 if and only if x βˆ’ 2y is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a number known to be divisible or not by 7 is obtained; you can stop when this number has *at most* 2 digits because you are supposed to know if a number of at most 2 digits is divisible by 7 or not. The original number is divisible by 7 if and only if the last number obtained using this procedure is divisible by 7. Examples: 1 - `m = 371 -> 37 βˆ’ (2Γ—1) -> 37 βˆ’ 2 = 35` ; thus, since 35 is divisible by 7, 371 is divisible by 7. The number of steps to get the result is `1`. 2 - `m = 1603 -> 160 - (2 x 3) -> 154 -> 15 - 8 = 7` and 7 is divisible by 7. 3 - `m = 372 -> 37 βˆ’ (2Γ—2) -> 37 βˆ’ 4 = 33` ; thus, since 33 is not divisible by 7, 372 is not divisible by 7. The number of steps to get the result is `1`. 4 - `m = 477557101->47755708->4775554->477547->47740->4774->469->28` and 28 is divisible by 7, so is 477557101. The number of steps is 7. # Task: Your task is to return to the function ```seven(m)``` (m integer >= 0) an array (or a pair, depending on the language) of numbers, the first being the *last* number `m` with at most 2 digits obtained by your function (this last `m` will be divisible or not by 7), the second one being the number of steps to get the result. ## Forth Note: Return on the stack `number-of-steps, last-number-m-with-at-most-2-digits ` ## Examples: ``` seven(371) should return [35, 1] seven(1603) should return [7, 2] seven(477557101) should return [28, 7] ``` Write your solution by modifying this code: ```python def seven(m): ``` Your solution should implemented in the function "seven". 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. Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get a_{i} votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate. Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe? -----Input----- The first line contains single integer n (2 ≀ n ≀ 100) - number of candidates. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 1000) - number of votes for each candidate. Limak is candidate number 1. Note that after bribing number of votes for some candidate might be zero or might be greater than 1000. -----Output----- Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate. -----Examples----- Input 5 5 1 11 2 8 Output 4 Input 4 1 8 8 8 Output 6 Input 2 7 6 Output 0 -----Note----- In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8. In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6. In the third sample Limak is a winner without bribing any citizen. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. problem JOI took six subjects: physics, chemistry, biology, earth science, history, and geography. Each test was scored on a 100-point scale. JOI chooses 3 subjects from 4 subjects of physics, chemistry, biology, and earth science, and 1 subject from 2 subjects of history and geography. When choosing a subject so that the total score of the test is the highest, find the total score of the test of the subject selected by JOI. input The input consists of 6 lines, with one integer written on each line. On the first line, JOI's physics test score A is written. On the second line, JOI's chemistry test score B is written. On the third line, JOI's biological test score C is written. On the 4th line, JOI's earth science test score D is written. On the 5th line, JOI's history test score E is written. On the 6th line, JOI's geography test score F is written. The written integers A, B, C, D, E, and F are all 0 or more and 100 or less. output JOI Output the total score of the test of the subject you chose in one line. Input / output example Input example 1 100 34 76 42 Ten 0 Output example 1 228 Input example 2 15 twenty one 15 42 15 62 Output example 2 140 In I / O Example 1, when JOI chooses four subjects: physics, biology, earth science, and history, the total score of the test is the highest. The scores for physics, biology, earth science, and history are 100, 76, 42, and 10, respectively, so the total score for the tests of the selected subject is 228. In I / O Example 2, when JOI chooses four subjects: chemistry, biology, earth science, and geography, the total score of the test is the highest. The scores for chemistry, biology, earth science, and geography are 21, 15, 42, and 62, respectively, so the total score for the tests of the selected subject is 140. In Input / Output Example 2, even if JOI chooses four subjects: physics, chemistry, earth science, and geography, the total score of the chosen test is 140. Creative Commons License Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks" Example Input 100 34 76 42 10 0 Output 228 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1, 2, ..., K and the center in the point (z, 0) a (K, z)-set. Thus, on the square were painted a (N, x)-set and a (M, y)-set. You have to find out how many parts those sets divided the square into. Input The first line contains integers N, x, M, y. (1 ≀ N, M ≀ 100000, - 100000 ≀ x, y ≀ 100000, x β‰  y). Output Print the sought number of parts. Examples Input 1 0 1 1 Output 4 Input 1 0 1 2 Output 3 Input 3 3 4 7 Output 17 Note Picture for the third sample: <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. Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ— m chessboard, a very tasty candy and two numbers a and b. Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types: move the candy from position (x, y) on the board to position (x - a, y - b); move the candy from position (x, y) on the board to position (x + a, y - b); move the candy from position (x, y) on the board to position (x - a, y + b); move the candy from position (x, y) on the board to position (x + a, y + b). Naturally, Dima doesn't allow to move the candy beyond the chessboard borders. Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task! -----Input----- The first line of the input contains six integers n, m, i, j, a, b (1 ≀ n, m ≀ 10^6;Β 1 ≀ i ≀ n;Β 1 ≀ j ≀ m;Β 1 ≀ a, b ≀ 10^6). You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1). -----Output----- In a single line print a single integer β€” the minimum number of moves needed to get the candy. If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes. -----Examples----- Input 5 7 1 3 2 2 Output 2 Input 5 5 2 3 1 1 Output Poor Inna and pony! -----Note----- Note to sample 1: Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each of them to the hero of neutral and equal to 0. As the hero completes quests, he makes actions that change the attitude of the companions, whom he took to perform this task, in positive or negative direction. Tell us what companions the hero needs to choose to make their attitude equal after completing all the quests. If this can be done in several ways, choose the one in which the value of resulting attitude is greatest possible. Input The first line contains positive integer n (1 ≀ n ≀ 25) β€” the number of important tasks. Next n lines contain the descriptions of the tasks β€” the i-th line contains three integers li, mi, wi β€” the values by which the attitude of Lynn, Meliana and Worrigan respectively will change towards the hero if the hero takes them on the i-th task. All the numbers in the input are integers and do not exceed 107 in absolute value. Output If there is no solution, print in the first line "Impossible". Otherwise, print n lines, two characters is each line β€” in the i-th line print the first letters of the companions' names that hero should take to complete the i-th task ('L' for Lynn, 'M' for Meliana, 'W' for Worrigan). Print the letters in any order, if there are multiple solutions, print any of them. Examples Input 3 1 0 0 0 1 0 0 0 1 Output LM MW MW Input 7 0 8 9 5 9 -2 6 -8 -7 9 4 5 -4 -9 9 -4 5 2 -6 8 -7 Output LM MW LM LW MW LM LW Input 2 1 0 0 1 1 0 Output Impossible Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Example Input acmicpc tsukuba Output No Read the inputs from stdin solve the problem and write the answer to stdout (do 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 non-negative number, return the next bigger polydivisible number, or an empty value like `null` or `Nothing`. A number is polydivisible if its first digit is cleanly divisible by `1`, its first two digits by `2`, its first three by `3`, and so on. There are finitely many polydivisible numbers. Write your solution by modifying this code: ```python def next_num(n): ``` Your solution should implemented in the function "next_num". 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. Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive). -----Constraints----- - 1 \leq N \leq 10^4 - 1 \leq A \leq B \leq 36 - All input values are integers. -----Input----- Input is given from Standard Input in the following format: N A B -----Output----- Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive). -----Sample Input----- 20 2 5 -----Sample Output----- 84 Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map. Formally, map is a checkered field of size 1 Γ— n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged. If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves. Help Slava to destroy all tanks using as few bombs as possible. -----Input----- The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” the size of the map. -----Output----- In the first line print m β€” the minimum number of bombs Slava needs to destroy all tanks. In the second line print m integers k_1, k_2, ..., k_{m}. The number k_{i} means that the i-th bomb should be dropped at the cell k_{i}. If there are multiple answers, you can print any of them. -----Examples----- Input 2 Output 3 2 1 2 Input 3 Output 4 2 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. We have a graph with N vertices, numbered 0 through N-1. Edges are yet to be added. We will process Q queries to add edges. In the i-th (1≦i≦Q) query, three integers A_i, B_i and C_i will be given, and we will add infinitely many edges to the graph as follows: * The two vertices numbered A_i and B_i will be connected by an edge with a weight of C_i. * The two vertices numbered B_i and A_i+1 will be connected by an edge with a weight of C_i+1. * The two vertices numbered A_i+1 and B_i+1 will be connected by an edge with a weight of C_i+2. * The two vertices numbered B_i+1 and A_i+2 will be connected by an edge with a weight of C_i+3. * The two vertices numbered A_i+2 and B_i+2 will be connected by an edge with a weight of C_i+4. * The two vertices numbered B_i+2 and A_i+3 will be connected by an edge with a weight of C_i+5. * The two vertices numbered A_i+3 and B_i+3 will be connected by an edge with a weight of C_i+6. * ... Here, consider the indices of the vertices modulo N. For example, the vertice numbered N is the one numbered 0, and the vertice numbered 2N-1 is the one numbered N-1. The figure below shows the first seven edges added when N=16, A_i=7, B_i=14, C_i=1: <image> After processing all the queries, find the total weight of the edges contained in a minimum spanning tree of the graph. Constraints * 2≦N≦200,000 * 1≦Q≦200,000 * 0≦A_i,B_i≦N-1 * 1≦C_i≦10^9 Input The input is given from Standard Input in the following format: N Q A_1 B_1 C_1 A_2 B_2 C_2 : A_Q B_Q C_Q Output Print the total weight of the edges contained in a minimum spanning tree of the graph. Examples Input 7 1 5 2 1 Output 21 Input 2 1 0 0 1000000000 Output 1000000001 Input 5 3 0 1 10 0 2 10 0 4 10 Output 42 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed. You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings. A contest has just finished. There are n teams, numbered 1 through n. The i-th team has t_{i} balloons and weight w_{i}. It's guaranteed that t_{i} doesn't exceed w_{i} so nobody floats initially. Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has. What is the best place Limak can get? -----Input----- The first line of the standard input contains one integer n (2 ≀ n ≀ 300 000)Β β€” the number of teams. The i-th of n following lines contains two integers t_{i} and w_{i} (0 ≀ t_{i} ≀ w_{i} ≀ 10^18)Β β€” respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team. -----Output----- Print one integer denoting the best place Limak can get. -----Examples----- Input 8 20 1000 32 37 40 1000 45 50 16 16 16 16 14 1000 2 1000 Output 3 Input 7 4 4 4 4 4 4 4 4 4 4 4 4 5 5 Output 2 Input 7 14000000003 1000000000000000000 81000000000 88000000000 5000000000 7000000000 15000000000 39000000000 46000000000 51000000000 0 1000000000 0 0 Output 2 -----Note----- In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified. Limak gives 1 balloon to each of two teams with 16 balloons initially. Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons. There are three other teams left and their numbers of balloons are 40, 14 and 2. Limak gets the third place because there are two teams with more balloons. In the second sample, Limak has the second place and he can't improve it. In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 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. Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest. Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory. Teams are ranked by following rules. 1. Team that solved more problems is ranked higher. 2. In case of tie (solved same number of problems), team that received less Penalty is ranked higher. Here, Penalty is calculated by these rules. 1. When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty. 2. For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added. You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given. Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems. Input Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 ≀ N ≀ 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 ≀ P[i] ≀ 10800) represents time to solve problems. Input ends with EOF. The number of datasets is less than or equal to 100. Output Output minimal possible Penalty, one line for one dataset. Example Input 3 10 20 30 7 56 26 62 43 25 80 7 Output 100 873 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: Its left half equals to its right half. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2βŒ‹, and right halfΒ β€” the suffix of the same length. ⌊|t| / 2βŒ‹ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. -----Input----- The first line contains the string s (1 ≀ |s| ≀ 5000) consisting of lowercase English letters. -----Output----- Print |s| integersΒ β€” palindromic characteristics of string s. -----Examples----- Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 -----Note----- In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads. Our child is very smart. Imagine the child want to go from area p to area q. Firstly he considers all the simple routes from p to q. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q). Finally, the child chooses one of the routes for which he writes down the value f(p, q). After the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p β‰  q)? Can you answer his question? Input The first line contains two integers n and m (2 ≀ n ≀ 105; 0 ≀ m ≀ 105). The second line contains n integers: a1, a2, ..., an (0 ≀ ai ≀ 105). Then follow m lines, each line contains two integers xi and yi (1 ≀ xi, yi ≀ n; xi β‰  yi), denoting the road between areas xi and yi. All roads are bidirectional, each pair of areas is connected by at most one road. Output Output a real number β€” the value of <image>. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. Examples Input 4 3 10 20 30 40 1 3 2 3 4 3 Output 16.666667 Input 3 3 10 20 30 1 2 2 3 3 1 Output 13.333333 Input 7 8 40 20 10 30 20 50 40 1 2 2 3 3 4 4 5 5 6 6 7 1 4 5 7 Output 18.571429 Note Consider the first sample. There are 12 possible situations: * p = 1, q = 3, f(p, q) = 10. * p = 2, q = 3, f(p, q) = 20. * p = 4, q = 3, f(p, q) = 30. * p = 1, q = 2, f(p, q) = 10. * p = 2, q = 4, f(p, q) = 20. * p = 4, q = 1, f(p, q) = 10. Another 6 cases are symmetrical to the above. The average is <image>. Consider the second sample. There are 6 possible situations: * p = 1, q = 2, f(p, q) = 10. * p = 2, q = 3, f(p, q) = 20. * p = 1, q = 3, f(p, q) = 10. Another 3 cases are symmetrical to the above. The average is <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. Monocarp plays a computer game. There are $n$ different sets of armor and $m$ different weapons in this game. If a character equips the $i$-th set of armor and wields the $j$-th weapon, their power is usually equal to $i + j$; but some combinations of armor and weapons synergize well. Formally, there is a list of $q$ ordered pairs, and if the pair $(i, j)$ belongs to this list, the power of the character equipped with the $i$-th set of armor and wielding the $j$-th weapon is not $i + j$, but $i + j + 1$. Initially, Monocarp's character has got only the $1$-st armor set and the $1$-st weapon. Monocarp can obtain a new weapon or a new set of armor in one hour. If he wants to obtain the $k$-th armor set or the $k$-th weapon, he must possess a combination of an armor set and a weapon that gets his power to $k$ or greater. Of course, after Monocarp obtains a weapon or an armor set, he can use it to obtain new armor sets or weapons, but he can go with any of the older armor sets and/or weapons as well. Monocarp wants to obtain the $n$-th armor set and the $m$-th weapon. What is the minimum number of hours he has to spend on it? -----Input----- The first line contains two integers $n$ and $m$ ($2 \le n, m \le 2 \cdot 10^5$) β€” the number of armor sets and the number of weapons, respectively. The second line contains one integer $q$ ($0 \le q \le \min(2 \cdot 10^5, nm)$) β€” the number of combinations that synergize well. Then $q$ lines follow, the $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i \le n$; $1 \le b_i \le m$) meaning that the $a_i$-th armor set synergizes well with the $b_i$-th weapon. All pairs $(a_i, b_i)$ are distinct. -----Output----- Print one integer β€” the minimum number of hours Monocarp has to spend to obtain both the $n$-th armor set and the $m$-th weapon. -----Examples----- Input 3 4 0 Output 3 Input 3 4 2 1 1 1 3 Output 2 -----Note----- In the first example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: Obtain the $2$-nd weapon using the $1$-st armor set and the $1$-st weapon; Obtain the $3$-rd armor set using the $1$-st armor set and the $2$-nd weapon; Obtain the $4$-th weapon using the $3$-rd armor set and the $2$-nd weapon. In the second example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: Obtain the $3$-rd armor set using the $1$-st armor set and the $1$-st weapon (they synergize well, so Monocarp's power is not $2$ but $3$); Obtain the $4$-th weapon using the $3$-rd armor set and the $1$-st weapon. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a function `longer` that accepts a string and sorts the words in it based on their respective lengths in an ascending order. If there are two words of the same lengths, sort them alphabetically. Look at the examples below for more details. ```python longer("Another Green World") => Green World Another longer("Darkness on the edge of Town") => of on the Town edge Darkness longer("Have you ever Seen the Rain") => the you Have Rain Seen ever ``` Assume that only only Alphabets will be entered as the input. Uppercase characters have priority over lowercase characters. That is, ```python longer("hello Hello") => Hello hello ``` Don't forget to rate this kata and leave your feedback!! Thanks Write your solution by modifying this code: ```python def longer(s): ``` Your solution should implemented in the function "longer". 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. We have an integer array with unique elements and we want to do the permutations that have an element fixed, in other words, these permutations should have a certain element at the same position than the original. These permutations will be called: **permutations with one fixed point**. Let's see an example with an array of four elements and we want the permutations that have a coincidence **only at index 0**, so these permutations are (the permutations between parenthesis): ``` arr = [1, 2, 3, 4] (1, 3, 4, 2) (1, 4, 2, 3) Two permutations matching with arr only at index 0 ``` Let's see the permutations of the same array with only one coincidence at index **1**: ``` arr = [1, 2, 3, 4] (3, 2, 4, 1) (4, 2, 1, 3) Two permutations matching with arr only at index 1 ``` Once again, let's see the permutations of the same array with only one coincidence at index **2**: ``` arr = [1, 2, 3, 4] (2, 4, 3, 1) (4, 1, 3, 2) Two permutations matching with arr only at index 2 ``` Finally, let's see the permutations of the same array with only one coincidence at index **3**: ``` arr = [1, 2, 3, 4] (2, 3, 1, 4) (3, 1, 2, 4) Two permutations matching with arr only at index 3 ``` For this array given above (arr) : - We conclude that we have 8 permutations with one fixed point (two at each index of arr). - We may do the same development for our array, `arr`, with two fixed points and we will get `6` permutations. - There are no permutations with coincidences only at three indexes. - It's good to know that the amount of permutations with no coincidences at all are `9`. See the kata Shuffle It Up!! In general: - When the amount of fixed points is equal to the array length, there is only one permutation, the original array. - When the amount of fixed points surpasses the length of the array, obvously, there are no permutations at all. Create a function that receives the length of the array and the number of fixed points and may output the total amount of permutations for these constraints. Features of the random tests: ``` length of the array = l number of fixed points = k 10 ≀ k ≀ l ≀ 9000 ``` See the example tests! Enjoy it!! Ruby versin will be released soon. #Note: This kata was published previously but in a version not well optimized. Write your solution by modifying this code: ```python def fixed_points_perms(n,k): ``` Your solution should implemented in the function "fixed_points_perms". 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 rectangular table 3 Γ— n. Each cell contains an integer. You can move from one cell to another if they share a side. Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible. Input The first line contains an integer n (1 ≀ n ≀ 105) β€” the number of columns in the table. Next three lines contain n integers each β€” the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≀ aij ≀ 109) of the table. Output Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice. Examples Input 3 1 1 1 1 -1 1 1 1 1 Output 7 Input 5 10 10 10 -1 -1 -1 10 10 10 10 -1 10 10 10 10 Output 110 Note The path for the first example: <image> The path for the second example: <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. In the snake exhibition, there are $n$ rooms (numbered $0$ to $n - 1$) arranged in a circle, with a snake in each room. The rooms are connected by $n$ conveyor belts, and the $i$-th conveyor belt connects the rooms $i$ and $(i+1) \bmod n$. In the other words, rooms $0$ and $1$, $1$ and $2$, $\ldots$, $n-2$ and $n-1$, $n-1$ and $0$ are connected with conveyor belts. The $i$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $i$ to $(i+1) \bmod n$. If it is anticlockwise, snakes can only go from room $(i+1) \bmod n$ to $i$. If it is off, snakes can travel in either direction. [Image] Above is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise. Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$): the number of test cases. The description of the test cases follows. The first line of each test case description contains a single integer $n$ ($2 \le n \le 300\,000$): the number of rooms. The next line of each test case description contains a string $s$ of length $n$, consisting of only '<', '>' and '-'. If $s_{i} = $ '>', the $i$-th conveyor belt goes clockwise. If $s_{i} = $ '<', the $i$-th conveyor belt goes anticlockwise. If $s_{i} = $ '-', the $i$-th conveyor belt is off. It is guaranteed that the sum of $n$ among all test cases does not exceed $300\,000$. -----Output----- For each test case, output the number of returnable rooms. -----Example----- Input 4 4 -><- 5 >>>>> 3 <-- 2 <> Output 3 5 3 0 -----Note----- In the first test case, all rooms are returnable except room $2$. The snake in the room $2$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 reverses the bits in an integer. For example, the number `417` is `110100001` in binary. Reversing the binary is `100001011` which is `267`. You can assume that the number is not negative. Write your solution by modifying this code: ```python def reverse_bits(n): ``` Your solution should implemented in the function "reverse_bits". 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. In this kata you need to write a function that will receive two strings (```n1``` and ```n2```), each representing an integer as a binary number. A third parameter will be provided (```o```) as a string representing one of the following operators: add, subtract, multiply. Your task is to write the calculate function so that it will perform the arithmetic and the result returned should be a string representing the binary result. Examples: ``` 1 + 1 === 10 10 + 10 === 100 ``` Negative binary numbers are usually preceded by several 1's. For this kata, negative numbers can be represented with the negative symbol at the beginning of the string. Examples of negatives: ``` 1 - 10 === -1 10 - 100 === -10 ``` Write your solution by modifying this code: ```python def calculate(n1, n2, o): ``` Your solution should implemented in the function "calculate". 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. Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $26$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $s$ appeared on the screen. When Polycarp presses a button with character $c$, one of the following events happened: if the button was working correctly, a character $c$ appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters $c$ appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $\rightarrow$ abb $\rightarrow$ abba $\rightarrow$ abbac $\rightarrow$ abbaca $\rightarrow$ abbacabb $\rightarrow$ abbacabba. You are given a string $s$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning). You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases in the input. Then the test cases follow. Each test case is represented by one line containing a string $s$ consisting of no less than $1$ and no more than $500$ lowercase Latin letters. -----Output----- For each test case, print one line containing a string $res$. The string $res$ should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, $res$ should be empty. -----Example----- Input 4 a zzaaz ccff cbddbb Output a z bc Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Arkady is playing Battleship. The rules of this game aren't really important. There is a field of $n \times n$ cells. There should be exactly one $k$-decker on the field, i.Β e. a ship that is $k$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship. Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 100$)Β β€” the size of the field and the size of the ship. The next $n$ lines contain the field. Each line contains $n$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship). -----Output----- Output two integersΒ β€” the row and the column of a cell that belongs to the maximum possible number of different locations of the ship. If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell. -----Examples----- Input 4 3 #..# #.#. .... .### Output 3 2 Input 10 4 #....##... .#...#.... ..#..#..#. ...#.#.... .#..##.#.. .....#...# ...#.##... .#...#.#.. .....#..#. ...#.#...# Output 6 1 Input 19 6 ##..............### #......#####.....## .....#########..... ....###########.... ...#############... ..###############.. .#################. .#################. .#################. .#################. #####....##....#### ####............### ####............### #####...####...#### .#####..####..##### ...###........###.. ....###########.... .........##........ #.................# Output 1 8 -----Note----- The picture below shows the three possible locations of the ship that contain the cell $(3, 2)$ in the first sample. [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. Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show". Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process. [Image] Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process! -----Input----- The first line contains a single integer n (1 ≀ n ≀ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string s_{i} is equal to "L", if the i-th domino has been pushed to the left; "R", if the i-th domino has been pushed to the right; ".", if the i-th domino has not been pushed. It is guaranteed that if s_{i} = s_{j} = "L" and i < j, then there exists such k that i < k < j and s_{k} = "R"; if s_{i} = s_{j} = "R" and i < j, then there exists such k that i < k < j and s_{k} = "L". -----Output----- Output a single integer, the number of the dominoes that remain vertical at the end of the process. -----Examples----- Input 14 .L.R...LR..L.. Output 4 Input 5 R.... Output 0 Input 1 . Output 1 -----Note----- The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange. In the second example case, all pieces fall down since the first piece topples all the other pieces. In the last example case, a single piece has not been pushed in either direction. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 always an integer in Takahashi's mind. Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1. The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat. Find the integer in Takahashi's mind after he eats all the symbols. -----Constraints----- - The length of S is 4. - Each character in S is + or -. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the integer in Takahashi's mind after he eats all the symbols. -----Sample Input----- +-++ -----Sample Output----- 2 - Initially, the integer in Takahashi's mind is 0. - The first integer for him to eat is +. After eating it, the integer in his mind becomes 1. - The second integer to eat is -. After eating it, the integer in his mind becomes 0. - The third integer to eat is +. After eating it, the integer in his mind becomes 1. - The fourth integer to eat is +. After eating it, the integer in his mind becomes 2. Thus, the integer in Takahashi's mind after he eats all the symbols is 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. # Description Given a number `n`, you should find a set of numbers for which the sum equals `n`. This set must consist exclusively of values that are a power of `2` (eg: `2^0 => 1, 2^1 => 2, 2^2 => 4, ...`). The function `powers` takes a single parameter, the number `n`, and should return an array of unique numbers. ## Criteria The function will always receive a valid input: any positive integer between `1` and the max integer value for your language (eg: for JavaScript this would be `9007199254740991` otherwise known as `Number.MAX_SAFE_INTEGER`). The function should return an array of numbers that are a **power of 2** (`2^x = y`). Each member of the returned array should be **unique**. (eg: the valid answer for `powers(2)` is `[2]`, not `[1, 1]`) Members should be sorted in **ascending order** (small -> large). (eg: the valid answer for `powers(6)` is `[2, 4]`, not `[4, 2]`) Write your solution by modifying this code: ```python def powers(n): ``` Your solution should implemented in the function "powers". 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. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows. f(0) = 1; f(1) = 2; For i > 1 : f(i) = f(i - 1) + f(i - 2) Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more). As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007). ------ Input ------ First line of the input contains an integer Q denoting number of questions Chef was asked. In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively. ------ Output ------ For each question, output a separate line containing the answer of the question. ------ ------ Constraints ----- Subtask 1 : [10 points] 1 ≀ Q ≀ 50 1 ≀ X ≀ 10^{9} 1 ≀ K ≀ 4 Subtask 2 : [20 points] 1 ≀ Q ≀ 100 1 ≀ X ≀ 10^{9} 1 ≀ K ≀ 5 Subtask 3 : [20 points] 1 ≀ Q ≀ 100 1 ≀ X ≀ 10^{2} 1 ≀ K ≀ 10 Subtask 4 : [50 points] 1 ≀ Q ≀ 100 1 ≀ X ≀ 10^{9} 1 ≀ K ≀ 10 ----- Sample Input 1 ------ 5 12 1 13 1 13 2 13 3 13 4 ----- Sample Output 1 ------ 0 1 1 2 4 ----- explanation 1 ------ Example case 1. There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number. Example case 2. There is only one way to create 13 by adding one Chefonacci number, i.e. 13. Example case 3. There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8. Example case 4. There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5. Example case 5. There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 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. Take debugging to a whole new level: Given a string, remove every *single* bug. This means you must remove all instances of the word 'bug' from within a given string, *unless* the word is plural ('bugs'). For example, given 'obugobugobuoobugsoo', you should return 'ooobuoobugsoo'. Another example: given 'obbugugo', you should return 'obugo'. Note that all characters will be lowercase. Happy squishing! Write your solution by modifying this code: ```python def debug(s): ``` Your solution should implemented in the function "debug". 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. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≀ x ≀ x_2 and y_1 ≀ y ≀ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≀ n ≀ 100 000), the number of points in Pavel's records. The second line contains 2 β‹… n integers a_1, a_2, ..., a_{2 β‹… n} (1 ≀ a_i ≀ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (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. Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score. Codeforces scores are computed as follows: If the maximum point value of a problem is x, and Kevin submitted correctly at minute m but made w wrong submissions, then his score on that problem is $\operatorname{max}(0.3 x,(1 - \frac{m}{250}) x - 50 w)$. His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack. All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer. -----Input----- The first line of the input contains five space-separated integers m_1, m_2, m_3, m_4, m_5, where m_{i} (0 ≀ m_{i} ≀ 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted. The second line contains five space-separated integers w_1, w_2, w_3, w_4, w_5, where w_{i} (0 ≀ w_{i} ≀ 10) is Kevin's number of wrong submissions on problem i. The last line contains two space-separated integers h_{s} and h_{u} (0 ≀ h_{s}, h_{u} ≀ 20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively. -----Output----- Print a single integer, the value of Kevin's final score. -----Examples----- Input 20 40 60 80 100 0 1 2 3 4 1 0 Output 4900 Input 119 119 119 119 119 0 0 0 0 0 10 0 Output 4930 -----Note----- In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets $(1 - \frac{119}{250}) = \frac{131}{250}$ of the points on each problem. So his score from solving problems is $\frac{131}{250}(500 + 1000 + 1500 + 2000 + 2500) = 3930$. Adding in 10Β·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For given integer n, count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n. Input n An integer n (1 ≀ n ≀ 1000000000). Output The number of totatives in a line. Examples Input 6 Output 2 Input 1000000 Output 400000 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Consider the following array: ``` [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789, 12345678910, 1234567891011...] ``` If we join these blocks of numbers, we come up with an infinite sequence which starts with `112123123412345123456...`. The list is infinite. You will be given an number (`n`) and your task will be to return the element at that index in the sequence, where `1 ≀ n ≀ 10^18`. Assume the indexes start with `1`, not `0`. For example: ``` solve(1) = 1, because the first character in the sequence is 1. There is no index 0. solve(2) = 1, because the second character is also 1. solve(3) = 2, because the third character is 2. ``` More examples in the test cases. Good luck! Write your solution by modifying this code: ```python def solve(n): ``` Your solution should implemented in the function "solve". 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. Your wizard cousin works at a Quidditch stadium and wants you to write a function that calculates the points for the Quidditch scoreboard! # Story Quidditch is a sport with two teams. The teams score goals by throwing the Quaffle through a hoop, each goal is worth **10 points**. The referee also deducts 30 points (**- 30 points**) from the team who are guilty of carrying out any of these fouls: Blatching, Blurting, Bumphing, Haverstacking, Quaffle-pocking, Stooging The match is concluded when the Snitch is caught, and catching the Snitch is worth **150 points**. Let's say a Quaffle goes through the hoop just seconds after the Snitch is caught, in that case the points of that goal should not end up on the scoreboard seeing as the match is already concluded. You don't need any prior knowledge of how Quidditch works in order to complete this kata, but if you want to read up on what it is, here's a link: https://en.wikipedia.org/wiki/Quidditch # Task You will be given a string with two arguments, the first argument will tell you which teams are playing and the second argument tells you what's happened in the match. Calculate the points and return a string containing the teams final scores, with the team names sorted in the same order as in the first argument. # Examples: # Given an input of: # The expected output would be: Separate the team names from their respective points with a colon and separate the two teams with a comma. Good luck! Write your solution by modifying this code: ```python def quidditch_scoreboard(teams, actions): ``` Your solution should implemented in the function "quidditch_scoreboard". 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. 3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or alive NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms of a $2 \times n$ rectangle grid. NEKO's task is to lead a Nekomimi girl from cell $(1, 1)$ to the gate at $(2, n)$ and escape the maze. The girl can only move between cells sharing a common side. However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type. After hours of streaming, NEKO finally figured out there are only $q$ such moments: the $i$-th moment toggles the state of cell $(r_i, c_i)$ (either from ground to lava or vice versa). Knowing this, NEKO wonders, after each of the $q$ moments, whether it is still possible to move from cell $(1, 1)$ to cell $(2, n)$ without going through any lava cells. Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her? -----Input----- The first line contains integers $n$, $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$). The $i$-th of $q$ following lines contains two integers $r_i$, $c_i$ ($1 \le r_i \le 2$, $1 \le c_i \le n$), denoting the coordinates of the cell to be flipped at the $i$-th moment. It is guaranteed that cells $(1, 1)$ and $(2, n)$ never appear in the query list. -----Output----- For each moment, if it is possible to travel from cell $(1, 1)$ to cell $(2, n)$, print "Yes", otherwise print "No". There should be exactly $q$ answers, one after every update. You can print the words in any case (either lowercase, uppercase or mixed). -----Example----- Input 5 5 2 3 1 4 2 4 2 3 1 4 Output Yes No No No Yes -----Note----- We'll crack down the example test here: After the first query, the girl still able to reach the goal. One of the shortest path ways should be: $(1,1) \to (1,2) \to (1,3) \to (1,4) \to (1,5) \to (2,5)$. After the second query, it's impossible to move to the goal, since the farthest cell she could reach is $(1, 3)$. After the fourth query, the $(2, 3)$ is not blocked, but now all the $4$-th column is blocked, so she still can't reach the goal. After the fifth query, the column barrier has been lifted, thus she can go to the final goal again. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor. The game is played on a square field consisting of n Γ— n cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses. The professor have chosen the field size n and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally. -----Input----- The only line of the input contains one integer n (1 ≀ n ≀ 10^18) β€” the size of the field. -----Output----- Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2. -----Examples----- Input 1 Output 1 Input 2 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. Implement a function which behaves like the uniq command in UNIX. It takes as input a sequence and returns a sequence in which all duplicate elements following each other have been reduced to one instance. Example: ``` ["a", "a", "b", "b", "c", "a", "b", "c"] => ["a", "b", "c", "a", "b", "c"] ``` Write your solution by modifying this code: ```python def uniq(seq): ``` Your solution should implemented in the function "uniq". 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. Wet Shark once had 2 sequences: {a_n}= {a_1, a_2, a_3, ... , a_(109)} {b_n} = {b_1, b_2, b_3, ... , b_(109)} However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 ≀ i ≀ 109. Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| ≀ 0.01 , Cthulhu's code checker will allow him to escape. Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 ≀ n < 109, where x = sqrt(2) and y = sqrt(3). Wet Shark is now clueless on how to compute anything, and asks you for help. Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively. Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth. -----Input----- The first line of input contains 3 space separated integers i, k, s β€” the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive). The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively. -----Output----- Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| ≀ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth. ----- Constraints ----- - SUBTASK 1: 20 POINTS - 1 ≀ i ≀ 103 - 1 ≀ k ≀ 103 - -103 ≀ s ≀ 103 - 1 ≀ a_i, b_i ≀ 103 - SUBTASK 2: 80 POINTS - 1 ≀ i ≀ 1010 - 1 ≀ k ≀ 1010 - -1010 ≀ s ≀ 1010 - 1 ≀ a_i, b_i ≀ 1010 It is guaranteed that -1010 ≀ Q ≀  1010. -----Example----- Input: 1 1 5 4 5 Output: 0.28125 -----Explanation----- Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked $n$ people about their opinions. Each person answered whether this problem is easy or hard. If at least one of these $n$ people has answered that the problem is hard, the coordinator decides to change the problem. For the given responses, check if the problem is easy enough. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) β€” the number of people who were asked to give their opinions. The second line contains $n$ integers, each integer is either $0$ or $1$. If $i$-th integer is $0$, then $i$-th person thinks that the problem is easy; if it is $1$, then $i$-th person thinks that the problem is hard. -----Output----- Print one word: "EASY" if the problem is easy according to all responses, or "HARD" if there is at least one person who thinks the problem is hard. You may print every letter in any register: "EASY", "easy", "EaSY" and "eAsY" all will be processed correctly. -----Examples----- Input 3 0 0 1 Output HARD Input 1 0 Output EASY -----Note----- In the first example the third person says it's a hard problem, so it should be replaced. In the second example the problem easy for the only person, so it doesn't have to be replaced. Read the inputs from stdin solve the problem and write the answer to stdout (do 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: Your task is to write a function which returns the sum of following series upto nth term(parameter). Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +... ## Rules: * You need to round the answer to 2 decimal places and return it as String. * If the given value is 0 then it should return 0.00 * You will only be given Natural Numbers as arguments. ## Examples: SeriesSum(1) => 1 = "1.00" SeriesSum(2) => 1 + 1/4 = "1.25" SeriesSum(5) => 1 + 1/4 + 1/7 + 1/10 + 1/13 = "1.57" **NOTE**: In PHP the function is called `series_sum()`. Write your solution by modifying this code: ```python def series_sum(n): ``` Your solution should implemented in the function "series_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. Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≀ n ≀ 105, 0 ≀ m ≀ 105, 1 ≀ k ≀ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≀ di ≀ 106, 0 ≀ fi ≀ n, 0 ≀ ti ≀ n, 1 ≀ ci ≀ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 the easy and hard versions is that tokens of type O do not appear in the input of the easy version. Errichto gave Monogon the following challenge in order to intimidate him from taking his top contributor spot on Codeforces. In a Tic-Tac-Toe grid, there are $n$ rows and $n$ columns. Each cell of the grid is either empty or contains a token. There are two types of tokens: X and O. If there exist three tokens of the same type consecutive in a row or column, it is a winning configuration. Otherwise, it is a draw configuration. The patterns in the first row are winning configurations. The patterns in the second row are draw configurations. In an operation, you can change an X to an O, or an O to an X. Let $k$ denote the total number of tokens in the grid. Your task is to make the grid a draw in at most $\lfloor \frac{k}{3}\rfloor$ (rounding down) operations. You are not required to minimize the number of operations. -----Input----- The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 300$) β€” the size of the grid. The following $n$ lines each contain a string of $n$ characters, denoting the initial grid. The character in the $i$-th row and $j$-th column is '.' if the cell is empty, or it is the type of token in the cell: 'X' or 'O'. It is guaranteed that not all cells are empty. In the easy version, the character 'O' does not appear in the input. The sum of $n$ across all test cases does not exceed $300$. -----Output----- For each test case, print the state of the grid after applying the operations. We have proof that a solution always exists. If there are multiple solutions, print any. -----Examples----- Input 3 3 .X. XXX .X. 6 XX.XXX XXXXXX XXX.XX XXXXXX XX.X.X XXXXXX 5 XXX.X .X..X XXX.X ..X.. ..X.. Output .X. XOX .X. XX.XXO XOXXOX OXX.XX XOOXXO XX.X.X OXXOXX XOX.X .X..X XXO.O ..X.. ..X.. -----Note----- In the first test case, there are initially three 'X' consecutive in the second row and the second column. By changing the middle token to 'O' we make the grid a draw, and we only changed $1\le \lfloor 5/3\rfloor$ token. In the second test case, we change only $9\le \lfloor 32/3\rfloor$ tokens, and there does not exist any three 'X' or 'O' consecutive in a row or column, so it is a draw. In the third test case, we change only $3\le \lfloor 12/3\rfloor$ tokens, and the resulting grid is a draw. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Floating-Point Numbers In this problem, we consider floating-point number formats, data representation formats to approximate real numbers on computers. Scientific notation is a method to express a number, frequently used for numbers too large or too small to be written tersely in usual decimal form. In scientific notation, all numbers are written in the form m Γ— 10e. Here, m (called significand) is a number greater than or equal to 1 and less than 10, and e (called exponent) is an integer. For example, a number 13.5 is equal to 1.35 Γ— 101, so we can express it in scientific notation with significand 1.35 and exponent 1. As binary number representation is convenient on computers, let's consider binary scientific notation with base two, instead of ten. In binary scientific notation, all numbers are written in the form m Γ— 2e. Since the base is two, m is limited to be less than 2. For example, 13.5 is equal to 1.6875 Γ— 23, so we can express it in binary scientific notation with significand 1.6875 and exponent 3. The significand 1.6875 is equal to 1 + 1/2 + 1/8 + 1/16, which is 1.10112 in binary notation. Similarly, the exponent 3 can be expressed as 112 in binary notation. A floating-point number expresses a number in binary scientific notation in finite number of bits. Although the accuracy of the significand and the range of the exponent are limited by the number of bits, we can express numbers in a wide range with reasonably high accuracy. In this problem, we consider a 64-bit floating-point number format, simplified from one actually used widely, in which only those numbers greater than or equal to 1 can be expressed. Here, the first 12 bits are used for the exponent and the remaining 52 bits for the significand. Let's denote the 64 bits of a floating-point number by b64...b1. With e an unsigned binary integer (b64...b53)2, and with m a binary fraction represented by the remaining 52 bits plus one (1.b52...b1)2, the floating-point number represents the number m Γ— 2e. We show below the bit string of the representation of 13.5 in the format described above. <image> In floating-point addition operations, the results have to be approximated by numbers representable in floating-point format. Here, we assume that the approximation is by truncation. When the sum of two floating-point numbers a and b is expressed in binary scientific notation as a + b = m Γ— 2e (1 ≀ m < 2, 0 ≀ e < 212), the result of addition operation on them will be a floating-point number with its first 12 bits representing e as an unsigned integer and the remaining 52 bits representing the first 52 bits of the binary fraction of m. A disadvantage of this approximation method is that the approximation error accumulates easily. To verify this, let's make an experiment of adding a floating-point number many times, as in the pseudocode shown below. Here, s and a are floating-point numbers, and the results of individual addition are approximated as described above. s := a for n times { s := s + a } For a given floating-point number a and a number of repetitions n, compute the bits of the floating-point number s when the above pseudocode finishes. Input The input consists of at most 1000 datasets, each in the following format. > n > b52...b1 > n is the number of repetitions. (1 ≀ n ≀ 1018) For each i, bi is either 0 or 1. As for the floating-point number a in the pseudocode, the exponent is 0 and the significand is b52...b1. The end of the input is indicated by a line containing a zero. Output For each dataset, the 64 bits of the floating-point number s after finishing the pseudocode should be output as a sequence of 64 digits, each being `0` or `1` in one line. Sample Input 1 0000000000000000000000000000000000000000000000000000 2 0000000000000000000000000000000000000000000000000000 3 0000000000000000000000000000000000000000000000000000 4 0000000000000000000000000000000000000000000000000000 7 1101000000000000000000000000000000000000000000000000 100 1100011010100001100111100101000111001001111100101011 123456789 1010101010101010101010101010101010101010101010101010 1000000000000000000 1111111111111111111111111111111111111111111111111111 0 Output for the Sample Input 0000000000010000000000000000000000000000000000000000000000000000 0000000000011000000000000000000000000000000000000000000000000000 0000000000100000000000000000000000000000000000000000000000000000 0000000000100100000000000000000000000000000000000000000000000000 0000000000111101000000000000000000000000000000000000000000000000 0000000001110110011010111011100001101110110010001001010101111111 0000000110111000100001110101011001000111100001010011110101011000 0000001101010000000000000000000000000000000000000000000000000000 Example Input 1 0000000000000000000000000000000000000000000000000000 2 0000000000000000000000000000000000000000000000000000 3 0000000000000000000000000000000000000000000000000000 4 0000000000000000000000000000000000000000000000000000 7 1101000000000000000000000000000000000000000000000000 100 1100011010100001100111100101000111001001111100101011 123456789 1010101010101010101010101010101010101010101010101010 1000000000000000000 1111111111111111111111111111111111111111111111111111 0 Output 0000000000010000000000000000000000000000000000000000000000000000 0000000000011000000000000000000000000000000000000000000000000000 0000000000100000000000000000000000000000000000000000000000000000 0000000000100100000000000000000000000000000000000000000000000000 0000000000111101000000000000000000000000000000000000000000000000 0000000001110110011010111011100001101110110010001001010101111111 0000000110111000100001110101011001000111100001010011110101011000 0000001101010000000000000000000000000000000000000000000000000000 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. **This Kata is intended as a small challenge for my students** All Star Code Challenge #16 Create a function called noRepeat() that takes a string argument and returns a single letter string of the **first** not repeated character in the entire string. ``` haskell noRepeat "aabbccdde" `shouldBe` 'e' noRepeat "wxyz" `shouldBe` 'w' noRepeat "testing" `shouldBe` 'e' ``` Note: ONLY letters from the english alphabet will be used as input There will ALWAYS be at least one non-repeating letter in the input string Write your solution by modifying this code: ```python def no_repeat(string): ``` Your solution should implemented in the function "no_repeat". 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. Given an array of integers, return the smallest common factors of all integers in the array. When i say **Smallest Common Factor** i mean the smallest number above 1 that can divide all numbers in the array without a remainder. If there are no common factors above 1, return 1 (technically 1 is always a common factor). Write your solution by modifying this code: ```python def scf(lst): ``` Your solution should implemented in the function "scf". 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. Write a program which manipulates a weighted rooted tree $T$ with the following operations: * $add(v,w)$: add $w$ to all edges from the root to node $u$ * $getSum(u)$: report the sum of weights of all edges from the root to node $u$ The given tree $T$ consists of $n$ nodes and every node has a unique ID from $0$ to $n-1$ respectively where ID of the root is $0$. Note that all weights are initialized to zero. Constraints * All the inputs are given in integers * $ 2 \leq n \leq 100000 $ * $ c_j < c_{j+1} $ $( 1 \leq j \leq k-1 )$ * $ 2 \leq q \leq 200000 $ * $ 1 \leq u,v \leq n-1 $ * $ 1 \leq w \leq 10000 $ Input The input is given in the following format. $n$ $node_0$ $node_1$ $node_2$ $:$ $node_{n-1}$ $q$ $query_1$ $query_2$ $:$ $query_{q}$ The first line of the input includes an integer $n$, the number of nodes in the tree. In the next $n$ lines,the information of node $i$ is given in the following format: ki c1 c2 ... ck $k_i$ is the number of children of node $i$, and $c_1$ $c_2$ ... $c_{k_i}$ are node IDs of 1st, ... $k$th child of node $i$. In the next line, the number of queries $q$ is given. In the next $q$ lines, $i$th query is given in the following format: 0 v w or 1 u The first integer represents the type of queries.'0' denotes $add(v, w)$ and '1' denotes $getSum(u)$. Output For each $getSum$ query, print the sum in a line. Examples Input 6 2 1 2 2 3 5 0 0 0 1 4 7 1 1 0 3 10 1 2 0 4 20 1 3 0 5 40 1 4 Output 0 0 40 150 Input 4 1 1 1 2 1 3 0 6 0 3 1000 0 2 1000 0 1 1000 1 1 1 2 1 3 Output 3000 5000 6000 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him. One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number β€” the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously). Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him. Input The first line contains the single integer n (1 ≀ n ≀ 1000) β€” the number of contests where the coder participated. The next line contains n space-separated non-negative integer numbers β€” they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000. Output Print the single number β€” the number of amazing performances the coder has had during his whole history of participating in the contests. Examples Input 5 100 50 200 150 200 Output 2 Input 10 4664 6496 5814 7010 5762 5736 6944 4850 3698 7242 Output 4 Note In the first sample the performances number 2 and 3 are amazing. In the second sample the performances number 2, 4, 9 and 10 are amazing. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2. Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r. -----Constraints----- - 1 \leq r \leq 100 - r is an integer. -----Input----- Input is given from Standard Input in the following format: r -----Output----- Print an integer representing the area of the regular dodecagon. -----Sample Input----- 4 -----Sample Output----- 48 The area of the regular dodecagon is 3 \times 4^2 = 48. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem. Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible. He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work. The anthem is the string s of no more than 10^5 small Latin letters and question marks. The most glorious victory is the string t of no more than 10^5 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string t in string s is maximal. Note that the occurrences of string t in s can overlap. Check the third example for clarification. -----Input----- The first line contains string of small Latin letters and question marks s (1 ≀ |s| ≀ 10^5). The second line contains string of small Latin letters t (1 ≀ |t| ≀ 10^5). Product of lengths of strings |s|Β·|t| won't exceed 10^7. -----Output----- Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters. -----Examples----- Input winlose???winl???w?? win Output 5 Input glo?yto?e??an? or Output 3 Input ??c????? abcab Output 2 -----Note----- In the first example the resulting string s is "winlosewinwinlwinwin" In the second example the resulting string s is "glorytoreorand". The last letter of the string can be arbitrary. In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is "abcabcab". Read the inputs from stdin solve the problem and write the answer to stdout (do 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 galactic games have begun! It's the galactic games! Beings of all worlds come together to compete in several interesting sports, like nroogring, fredling and buzzing (the beefolks love the last one). However, there's also the traditional marathon run. Unfortunately, there have been cheaters in the last years, and the committee decided to place sensors on the track. Committees being committees, they've come up with the following rule: > A sensor should be placed every 3 and 5 meters from the start, e.g. > at 3m, 5m, 6m, 9m, 10m, 12m, 15m, 18m…. Since you're responsible for the track, you need to buy those sensors. Even worse, you don't know how long the track will be! And since there might be more than a single track, and you can't be bothered to do all of this by hand, you decide to write a program instead. ## Task Return the sum of the multiples of 3 and 5 __below__ a number. Being the _galactic_ games, the tracks can get rather large, so your solution should work for _really_ large numbers (greater than 1,000,000). ### Examples ```python solution (10) # => 23 = 3 + 5 + 6 + 9 solution (20) # => 78 = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 ``` Write your solution by modifying this code: ```python def solution(number): ``` Your solution should implemented in the function "solution". 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. In olden days finding square roots seemed to be difficult but nowadays it can be easily done using in-built functions available across many languages . Assume that you happen to hear the above words and you want to give a try in finding the square root of any given integer using in-built functions. So here's your chance. ------ Input ------ The first line of the input contains an integer T, the number of test cases. T lines follow. Each line contains an integer N whose square root needs to be computed. ------ Output ------ For each line of the input, output the square root of the input integer, rounded down to the nearest integer, in a new line. ------ Constraints ------ 1≀T≀20 1≀N≀10000 ----- Sample Input 1 ------ 3 10 5 10000 ----- Sample Output 1 ------ 3 2 100 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Some time ago Mister B detected a strange signal from the space, which he started to study. After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation. Let's define the deviation of a permutation p as <image>. Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them. Let's denote id k (0 ≀ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example: * k = 0: shift p1, p2, ... pn, * k = 1: shift pn, p1, ... pn - 1, * ..., * k = n - 1: shift p2, p3, ... pn, p1. Input First line contains single integer n (2 ≀ n ≀ 106) β€” the length of the permutation. The second line contains n space-separated integers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” the elements of the permutation. It is guaranteed that all elements are distinct. Output Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them. Examples Input 3 1 2 3 Output 0 0 Input 3 2 3 1 Output 0 1 Input 3 3 2 1 Output 2 1 Note In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well. In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift. In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 distinct integers A and B. Print the integer K such that |A - K| = |B - K|. If such an integer does not exist, print IMPOSSIBLE instead. -----Constraints----- - All values in input are integers. - 0 \leq A,\ B \leq 10^9 - A and B are distinct. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the integer K satisfying the condition. If such an integer does not exist, print IMPOSSIBLE instead. -----Sample Input----- 2 16 -----Sample Output----- 9 |2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Of the real numbers, those with a circular decimal part and those with a finite number of digits can be expressed as fractions. Given a real number that can be represented by a fraction, write a program that outputs an irreducible fraction equal to that real number (a fraction that cannot be reduced any further). Input The input is given in the following format. str One line is given the string str that represents the real number you want to convert. The real value is greater than 0. The character string is a number or a character string having a length of 3 or more and 8 or less, including ".", "(", ")". "." Indicates the decimal point, "(" indicates the beginning of the number cycle, and ")" indicates the end of the number cycle. It is assumed that one or more digits are always given to both the integer part and the decimal part. However, given a recurring decimal, the string satisfies the following conditions: * The beginning and end pairs of the cycle appear only once to the right of the decimal point. * The ")" that indicates the end of the cycle appears at the end of the string. * A single or more digit is always given between the beginning and end of the cycle. Output Outputs a real number in irreducible fraction format (integer of numerator followed by integers of denominator separated by "/"). Examples Input 0.(3) Output 1/3 Input 1.0 Output 1/1 Input 5.2(143) Output 52091/9990 Input 0.0739 Output 739/10000 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on). To reverse the i-th string Vasiliy has to spent c_{i} units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order. String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B. For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically. -----Input----- The first line of the input contains a single integer n (2 ≀ n ≀ 100 000)Β β€” the number of strings. The second line contains n integers c_{i} (0 ≀ c_{i} ≀ 10^9), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string. Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000. -----Output----- If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent. -----Examples----- Input 2 1 2 ba ac Output 1 Input 3 1 3 1 aa ba ac Output 1 Input 2 5 5 bbb aaa Output -1 Input 2 3 3 aaa aa Output -1 -----Note----- In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller. In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1. In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is - 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 are given $n$ one-dimensional segments (each segment is denoted by two integers β€” its endpoints). Let's define the function $f(x)$ as the number of segments covering point $x$ (a segment covers the point $x$ if $l \le x \le r$, where $l$ is the left endpoint and $r$ is the right endpoint of the segment). An integer point $x$ is called ideal if it belongs to more segments than any other integer point, i. e. $f(y) < f(x)$ is true for any other integer point $y$. You are given an integer $k$. Your task is to determine whether it is possible to remove some (possibly zero) segments, so that the given point $k$ becomes ideal. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 50$). Then $n$ lines follow, $i$-th line of them contains two integers $l_i$ and $r_i$ ($1 \le l_i, r_i \le 50$; $l_i \le r_i$) β€” the endpoints of the $i$-th segment. -----Output----- For each test case, print YES if it is possible to remove some (possibly zero) segments, so that the given point $k$ becomes ideal, otherwise print NO. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 4 3 1 3 7 9 2 5 3 6 2 9 1 4 3 7 1 3 2 4 3 5 1 4 6 7 5 5 Output YES NO NO YES -----Note----- In the first example, the point $3$ is already ideal (it is covered by three segments), so you don't have to delete anything. In the fourth example, you can delete everything except the segment $[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. The restaurant AtCoder serves the following five dishes: - ABC Don (rice bowl): takes A minutes to serve. - ARC Curry: takes B minutes to serve. - AGC Pasta: takes C minutes to serve. - APC Ramen: takes D minutes to serve. - ATC Hanbagu (hamburger patty): takes E minutes to serve. Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered. This restaurant has the following rules on orders: - An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...). - Only one dish can be ordered at a time. - No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered. E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered. Here, he can order the dishes in any order he likes, and he can place an order already at time 0. -----Constraints----- - A, B, C, D and E are integers between 1 and 123 (inclusive). -----Input----- Input is given from Standard Input in the following format: A B C D E -----Output----- Print the earliest possible time for the last dish to be delivered, as an integer. -----Sample Input----- 29 20 7 35 120 -----Sample Output----- 215 If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows: - Order ABC Don at time 0, which will be delivered at time 29. - Order ARC Curry at time 30, which will be delivered at time 50. - Order AGC Pasta at time 50, which will be delivered at time 57. - Order ATC Hanbagu at time 60, which will be delivered at time 180. - Order APC Ramen at time 180, which will be delivered at time 215. There is no way to order the dishes in which the last dish will be delivered earlier than this. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. Recently Xenia has bought $n_r$ red gems, $n_g$ green gems and $n_b$ blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are $x$, $y$ and $z$, Xenia wants to find the minimum value of $(x-y)^2+(y-z)^2+(z-x)^2$. As her dear friend, can you help her? -----Input----- The first line contains a single integer $t$ ($1\le t \le 100$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains three integers $n_r,n_g,n_b$ ($1\le n_r,n_g,n_b\le 10^5$) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains $n_r$ integers $r_1,r_2,\ldots,r_{n_r}$ ($1\le r_i \le 10^9$) β€” $r_i$ is the weight of the $i$-th red gem. The third line of each test case contains $n_g$ integers $g_1,g_2,\ldots,g_{n_g}$ ($1\le g_i \le 10^9$) β€” $g_i$ is the weight of the $i$-th green gem. The fourth line of each test case contains $n_b$ integers $b_1,b_2,\ldots,b_{n_b}$ ($1\le b_i \le 10^9$) β€” $b_i$ is the weight of the $i$-th blue gem. It is guaranteed that $\sum n_r \le 10^5$, $\sum n_g \le 10^5$, $\sum n_b \le 10^5$ (the sum for all test cases). -----Output----- For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. -----Examples----- Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 -----Note----- In the first test case, Xenia has the following gems: If she picks the red gem with weight $7$, the green gem with weight $6$, and the blue gem with weight $4$, she will achieve the most balanced selection with $(x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job. During all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV. More formally, you are given an array s_1, s_2, ..., s_{n} of zeros and ones. Zero corresponds to an unsuccessful game, one β€” to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one. Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV. -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100). The second line contains n space-separated integer numbers s_1, s_2, ..., s_{n} (0 ≀ s_{i} ≀ 1). 0 corresponds to an unsuccessful game, 1 β€” to a successful one. -----Output----- Print one integer β€” the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one. -----Examples----- Input 4 1 1 0 1 Output 3 Input 6 0 1 0 0 1 0 Output 4 Input 1 0 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. You want to make change for $ n $ cents. Assuming that you have infinite supply of coins of 1, 5, 10 and / or 25 cents coins respectively, find the minimum number of coins you need. Constraints * $ 1 \ le n \ le 10 ^ 9 $ Input $ n $ The integer $ n $ is given in a line. output Print the minimum number of coins you need in a line. Examples Input 100 Output 4 Input 54321 Output 2175 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well). Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format: $+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help! We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks. The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$). -----Output----- After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 6 1 1 1 2 1 1 6 + 2 + 1 - 1 + 2 - 1 + 2 Output NO YES NO NO NO YES -----Note----- After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). For a positive integer $n$, we call a permutation $p$ of length $n$ good if the following condition holds for every pair $i$ and $j$ ($1 \le i \le j \le n$)Β β€” $(p_i \text{ OR } p_{i+1} \text{ OR } \ldots \text{ OR } p_{j-1} \text{ OR } p_{j}) \ge j-i+1$, where $\text{OR}$ denotes the bitwise OR operation. In other words, a permutation $p$ is good if for every subarray of $p$, the $\text{OR}$ of all elements in it is not less than the number of elements in that subarray. Given a positive integer $n$, output any good permutation of length $n$. We can show that for the given constraints such a permutation always exists. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first and only line of every test case contains a single integer $n$ ($1 \le n \le 100$). -----Output----- For every test, output any good permutation of length $n$ on a separate line. -----Example----- Input 3 1 3 7 Output 1 3 1 2 4 3 5 2 7 1 6 -----Note----- For $n = 3$, $[3,1,2]$ is a good permutation. Some of the subarrays are listed below. $3\text{ OR }1 = 3 \geq 2$ $(i = 1,j = 2)$ $3\text{ OR }1\text{ OR }2 = 3 \geq 3$ $(i = 1,j = 3)$ $1\text{ OR }2 = 3 \geq 2$ $(i = 2,j = 3)$ $1 \geq 1$ $(i = 2,j = 2)$ Similarly, you can verify that $[4,3,5,2,7,1,6]$ is also good. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. You are given two integer arrays A and B each of size N. Let us define interaction of arrays A and B to be the sum of A[i] * B[i] for each i from 1 to N. You want to maximize the value of interaction of the arrays. You are allowed to make at most K (possibly zero) operations of following kind. In a single operation, you can increase or decrease any of the elements of array A by 1. Find out the maximum value of interaction of the arrays that you can get. ------ Input ------ The first line of input contains a single integer T denoting number of test cases. For each test case: First line contains two space separated integers N, K. Second line contains N space separated integers denoting array A. Third line contains N space separated integers denoting array B. ------ Output ------ For each test case, output a single integer denoting the answer of the problem. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $0 ≀ |A[i]|, |B[i]| ≀ 10^{5}$ $0 ≀ K ≀ 10^{9}$ ------ Subtasks ------ Subtask #1 : (25 points) $1 ≀ N ≀ 10$ $0 ≀ |A[i]|, |B[i]| ≀ 10$ $0 ≀ K ≀ 10$ Subtask #2 : (35 points) $1 ≀ N ≀ 1000$ $0 ≀ |A[i]|, |B[i]| ≀ 1000$ $0 ≀ K ≀ 10^{5}$ Subtask #3 : (40 points) $No additional constraints$ ----- Sample Input 1 ------ 2 2 2 1 2 -2 3 3 5 1 2 -3 -2 3 -5 ----- Sample Output 1 ------ 10 44 ----- explanation 1 ------ In the first example, you can increase value A[2] using two two operations. Now, A would be [1, 4]. The value of interaction will be 1 * -2 + 4 * 3 = -2 + 12 = 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. Your friend is developing a computer game. He has already decided how the game world should look like β€” it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≀ n ≀ 3 β‹… 10^5, n - 1 ≀ m ≀ 3 β‹… 10^5) β€” the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≀ x, y ≀ n, x β‰  y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer β€” the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 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 town sheriff dislikes odd numbers and wants all odd numbered families out of town! In town crowds can form and individuals are often mixed with other people and families. However you can distinguish the family they belong to by the number on the shirts they wear. As the sheriff's assistant it's your job to find all the odd numbered families and remove them from the town! ~~~if-not:cpp Challenge: You are given a list of numbers. The numbers each repeat a certain number of times. Remove all numbers that repeat an odd number of times while keeping everything else the same. ~~~ ~~~if:cpp Challenge: You are given a vector of numbers. The numbers each repeat a certain number of times. Remove all numbers that repeat an odd number of times while keeping everything else the same. ~~~ ```python odd_ones_out([1, 2, 3, 1, 3, 3]) = [1, 1] ``` In the above example: - the number 1 appears twice - the number 2 appears once - the number 3 appears three times `2` and `3` both appear an odd number of times, so they are removed from the list. The final result is: `[1,1]` Here are more examples: ```python odd_ones_out([1, 1, 2, 2, 3, 3, 3]) = [1, 1, 2, 2] odd_ones_out([26, 23, 24, 17, 23, 24, 23, 26]) = [26, 24, 24, 26] odd_ones_out([1, 2, 3]) = [] odd_ones_out([1]) = [] ``` Are you up to the challenge? Write your solution by modifying this code: ```python def odd_ones_out(numbers): ``` Your solution should implemented in the function "odd_ones_out". 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. Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens. All characters in this game are lowercase English letters. There are two players: Mister B and his competitor. Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde"). The players take turns appending letters to string s. Mister B moves first. Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move. Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s. Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1. -----Input----- First and only line contains four space-separated integers: a, b, l and r (1 ≀ a, b ≀ 12, 1 ≀ l ≀ r ≀ 10^9) β€” the numbers of letters each player appends and the bounds of the segment. -----Output----- Print one integer β€” the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s. -----Examples----- Input 1 1 1 8 Output 2 Input 4 2 2 6 Output 3 Input 3 7 4 6 Output 1 -----Note----- In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2. In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3. In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 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. <image> There used to be a game called Joseph's potatoes. Let's say n people are participating. Participants form a circle facing the center and are numbered starting from 1. One hot potato is given to participant n (the large number 30 inside the figure on the left). Participants who are given the potatoes will give the potatoes to the participant on the right. The person passed the mth time is passed to the person on the right and exits the circle (the figure on the left shows the case of m = 9). Each time you hand it over, you will pass one by one, and the last remaining person will be the winner and you will receive potatoes. After n and m are decided, it would be nice to know where you can win before you actually start handing the potatoes. The figure above shows the case of playing this game with the rule that 30 participants exit every 9 people. The large numbers on the inside are the numbers assigned to the participants, and the small numbers on the outside are the numbers that are removed. According to it, it will break out of the circle in the order of 9,18,27,6,16,26, and 21 will remain at the end. That is, 21 is the winner (the smaller number is 30). Enter the number of game participants n and the interval m between the participants who break out of the circle, and create a program that outputs the winner's number. However, m, n <1000. input Given multiple datasets. Each dataset is given in the following format: n m The number of game participants n (integer) and the interval m (integer) between the participants who break out of the circle are given on one line separated by blanks. The input ends with two 0s. The number of datasets does not exceed 50. output For each dataset, output the number (integer) of the winner and the person who will receive the potatoes on one line. Example Input 41 3 30 9 0 0 Output 31 21 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. <image> At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints. Input The first line of the input contains integer n (3 ≀ n ≀ 1000). The second line contains the description of the road β€” the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. Output Print two space-separated integers β€” the values of s and t. If there are several possible solutions you can print any of them. Examples Input 9 ..RRLL... Output 3 4 Input 11 .RRRLLLLL.. Output 7 5 Note The first test sample is the one in the picture. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes. You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning. You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language. You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes. -----Input----- The first line contains two integers, n and m (1 ≀ n ≀ 3000, 1 ≀ m ≀ 3000) β€” the number of words in the professor's lecture and the number of words in each of these languages. The following m lines contain the words. The i-th line contains two strings a_{i}, b_{i} meaning that the word a_{i} belongs to the first language, the word b_{i} belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once. The next line contains n space-separated strings c_1, c_2, ..., c_{n} β€” the text of the lecture. It is guaranteed that each of the strings c_{i} belongs to the set of strings {a_1, a_2, ... a_{m}}. All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters. -----Output----- Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input. -----Examples----- Input 4 3 codeforces codesecrof contest round letter message codeforces contest letter contest Output codeforces round letter round Input 5 3 joll wuqrd euzf un hbnyiyc rsoqqveh hbnyiyc joll joll euzf joll Output hbnyiyc joll joll un joll Read the inputs from stdin solve the problem and write the answer to stdout (do 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 to calculate compound tax using the following table: For $10 and under, the tax rate should be 10%. For $20 and under, the tax rate on the first $10 is %10, and the tax on the rest is 7%. For $30 and under, the tax rate on the first $10 is still %10, the rate for the next $10 is still 7%, and everything else is 5%. Tack on an additional 3% for the portion of the total above $30. Return 0 for invalid input(anything that's not a positive real number). Examples: An input of 10, should return 1 (1 is 10% of 10) An input of 21, should return 1.75 (10% of 10 + 7% of 10 + 5% of 1) * Note that the returned value should be rounded to the nearest penny. Write your solution by modifying this code: ```python def tax_calculator(total): ``` Your solution should implemented in the function "tax_calculator". 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. In music, if you double (or halve) the pitch of any note you will get to the same note again. "Concert A" is fixed at 440 Hz, and every other note is defined based on that. 880 Hz is also an A, as is 1760 Hz, as is 220 Hz. There are 12 notes in Western music: A, A#, B, C, C#, D, D#, E, F, F#, G, G#. You are given a preloaded dictionary with these 12 notes and one of the pitches that creates that note (starting at Concert A). Now, given a pitch (in Hz), return the corresponding note. (All inputs will be valid notes). For reference, the notes dictionary looks like this: ```python notes_dictionary = { 440: "A", 466.16: "A#", 493.88: "B", 523.25: "C", 554.37: "C#", 587.33: "D", 622.25: "D#", 659.25: "E", 698.46: "F", 739.99: "F#", 783.99: "G", 830.61: "G#" } ``` Musicians: all pitches based on equal tempermanent, taken from [here](http://pages.mtu.edu/~suits/notefreqs.html). Write your solution by modifying this code: ```python def get_note(pitch): ``` Your solution should implemented in the function "get_note". 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. Moamen and Ezzat are playing a game. They create an array $a$ of $n$ non-negative integers where every element is less than $2^k$. Moamen wins if $a_1 \,\&\, a_2 \,\&\, a_3 \,\&\, \ldots \,\&\, a_n \ge a_1 \oplus a_2 \oplus a_3 \oplus \ldots \oplus a_n$. Here $\&$ denotes the bitwise AND operation , and $\oplus$ denotes the bitwise XOR operation . Please calculate the number of winning for Moamen arrays $a$. As the result may be very large, print the value modulo $1000000\,007$ ($10^9 + 7$). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 5$)β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n\le 2\cdot 10^5$, $0 \le k \le 2\cdot 10^5$). -----Output----- For each test case, print a single value β€” the number of different arrays that Moamen wins with. Print the result modulo $1000000\,007$ ($10^9 + 7$). -----Examples----- Input 3 3 1 2 1 4 0 Output 5 2 1 -----Note----- In the first example, $n = 3$, $k = 1$. As a result, all the possible arrays are $[0,0,0]$, $[0,0,1]$, $[0,1,0]$, $[1,0,0]$, $[1,1,0]$, $[0,1,1]$, $[1,0,1]$, and $[1,1,1]$. Moamen wins in only $5$ of them: $[0,0,0]$, $[1,1,0]$, $[0,1,1]$, $[1,0,1]$, and $[1,1,1]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given is a positive integer N. Consider repeatedly applying the operation below on N: - First, choose a positive integer z satisfying all of the conditions below: - z can be represented as z=p^e, where p is a prime number and e is a positive integer; - z divides N; - z is different from all integers chosen in previous operations. - Then, replace N with N/z. Find the maximum number of times the operation can be applied. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 10^{12} -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the maximum number of times the operation can be applied. -----Sample Input----- 24 -----Sample Output----- 3 We can apply the operation three times by, for example, making the following choices: - Choose z=2 (=2^1). (Now we have N=12.) - Choose z=3 (=3^1). (Now we have N=4.) - Choose z=4 (=2^2). (Now we have N=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. In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops. Polycarp made n trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes. It is known that one trip on any bus costs a burles. In case when passenger makes a transshipment the cost of trip decreases to b burles (b < a). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment. For example, if Polycarp made three consecutive trips: "BerBank" $\rightarrow$ "University", "University" $\rightarrow$ "BerMall", "University" $\rightarrow$ "BerBank", then he payed a + b + a = 2a + b burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus. Also Polycarp can buy no more than k travel cards. Each travel card costs f burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction. What is the smallest amount of money Polycarp could have spent today if he can buy no more than k travel cards? -----Input----- The first line contains five integers n, a, b, k, f (1 ≀ n ≀ 300, 1 ≀ b < a ≀ 100, 0 ≀ k ≀ 300, 1 ≀ f ≀ 1000) where: n β€” the number of Polycarp trips, a β€” the cost of a regualar single trip, b β€” the cost of a trip after a transshipment, k β€” the maximum number of travel cards Polycarp can buy, f β€” the cost of a single travel card. The following n lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space β€” the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different. -----Output----- Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than k travel cards. -----Examples----- Input 3 5 3 1 8 BerBank University University BerMall University BerBank Output 11 Input 4 2 1 300 1000 a A A aa aa AA AA a Output 5 -----Note----- In the first example Polycarp can buy travel card for the route "BerBank $\leftrightarrow$ University" and spend 8 burles. Note that his second trip "University" $\rightarrow$ "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles. In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Evlampiy has found one more cool application to process photos. However the application has certain limitations. Each photo i has a contrast v_{i}. In order for the processing to be truly of high quality, the application must receive at least k photos with contrasts which differ as little as possible. Evlampiy already knows the contrast v_{i} for each of his n photos. Now he wants to split the photos into groups, so that each group contains at least k photos. As a result, each photo must belong to exactly one group. He considers a processing time of the j-th group to be the difference between the maximum and minimum values of v_{i} in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups. Split n photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible. -----Input----- The first line contains two integers n and k (1 ≀ k ≀ n ≀ 3Β·10^5) β€” number of photos and minimum size of a group. The second line contains n integers v_1, v_2, ..., v_{n} (1 ≀ v_{i} ≀ 10^9), where v_{i} is the contrast of the i-th photo. -----Output----- Print the minimal processing time of the division into groups. -----Examples----- Input 5 2 50 110 130 40 120 Output 20 Input 4 1 2 3 4 1 Output 0 -----Note----- In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20. In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly $n$ flowers. Vladimir went to a flower shop, and he was amazed to see that there are $m$ types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the $i$-th type happiness of his wife increases by $a_i$ and after receiving each consecutive flower of this type her happiness increases by $b_i$. That is, if among the chosen flowers there are $x_i > 0$ flowers of type $i$, his wife gets $a_i + (x_i - 1) \cdot b_i$ additional happiness (and if there are no flowers of type $i$, she gets nothing for this particular type). Please help Vladimir to choose exactly $n$ flowers to maximize the total happiness of his wife. -----Input----- The first line contains the only integer $t$ ($1 \leq t \leq 10\,000$), the number of test cases. It is followed by $t$ descriptions of the test cases. Each test case description starts with two integers $n$ and $m$ ($1 \le n \le 10^9$, $1 \le m \le 100\,000$), the number of flowers Vladimir needs to choose and the number of types of available flowers. The following $m$ lines describe the types of flowers: each line contains integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$) for $i$-th available type of flowers. The test cases are separated by a blank line. It is guaranteed that the sum of values $m$ among all test cases does not exceed $100\,000$. -----Output----- For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $n$ flowers optimally. -----Example----- Input 2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1 Output 14 16 -----Note----- In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals $5 + (1 + 2 \cdot 4) = 14$. In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals $(5 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Zombies zombies everywhere!!Β  In a parallel world of zombies, there are N zombies. There are infinite number of unused cars, each of same model only differentiated by the their colors. The cars are of K colors. A zombie parent can give birth to any number of zombie-children (possibly zero), i.e. each zombie will have its parent except the head zombie which was born in the winters by combination of ice and fire. Now, zombies are having great difficulties to commute to their offices without cars, so they decided to use the cars available. Every zombie will need only one car. Head zombie called a meeting regarding this, in which he will allow each zombie to select a car for him. Out of all the cars, the head zombie chose one of cars for him. Now, he called his children to choose the cars for them. After that they called their children and so on till each of the zombie had a car. Head zombie knew that it won't be a good idea to allow children to have cars of same color as that of parent, as they might mistakenly use that. So, he enforced this rule during the selection of cars. Professor James Moriarty is a criminal mastermind and has trapped Watson again in the zombie world. Sherlock somehow manages to go there and met the head zombie. Head zombie told Sherlock that they will let Watson free if and only if Sherlock manages to tell him the maximum number of ways in which the cars can be selected by N Zombies among all possible hierarchies. A hierarchy represents parent-child relationships among the N zombies. Since the answer may be large, output the answer modulo 10^{9} + 7. Sherlock can not compute big numbers, so he confides you to solve this for him. ------ Input ------ The first line consists of a single integer T, the number of test-cases. Each test case consists of two space-separated integers N and K, denoting number of zombies and the possible number of colors of the cars respectively. ------ Output ------ For each test-case, output a single line denoting the answer of the problem. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ------ Subtasks ------ Subtask #1 : (10 points) 1 ≀ T ≀ 20 1 ≀ N, K ≀ 10 Subtask 2 : (20 points) 1 ≀ T ≀ 10 1 ≀ N, K ≀ 10000 Subtask 3 : (70 points) 1 ≀ T ≀ 100 1 ≀ N, K ≀ 10^{9} ----- Sample Input 1 ------ 2 2 2 3 3 ----- Sample Output 1 ------ 2 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. Write a program which reads an integer $S$ [second] and converts it to $h:m:s$ where $h$, $m$, $s$ denote hours, minutes (less than 60) and seconds (less than 60) respectively. Constraints * $0 \leq S \leq 86400$ Input An integer $S$ is given in a line. Output Print $h$, $m$ and $s$ separated by ':'. You do not need to put '0' for a value, which consists of a digit. Example Input 46979 Output 13:2:59 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given four integer values $a$, $b$, $c$ and $m$. Check if there exists a string that contains: $a$ letters 'A'; $b$ letters 'B'; $c$ letters 'C'; no other letters; exactly $m$ pairs of adjacent equal letters (exactly $m$ such positions $i$ that the $i$-th letter is equal to the $(i+1)$-th one). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. Each of the next $t$ lines contains the description of the testcase β€” four integers $a$, $b$, $c$ and $m$ ($1 \le a, b, c \le 10^8$; $0 \le m \le 10^8$). -----Output----- For each testcase print "YES" if there exists a string that satisfies all the requirements. Print "NO" if there are no such strings. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Examples----- Input 3 2 2 1 0 1 1 1 1 1 2 3 2 Output YES NO YES -----Note----- In the first testcase strings "ABCAB" or "BCABA" satisfy the requirements. There exist other possible strings. In the second testcase there's no way to put adjacent equal letters if there's no letter that appears at least twice. In the third testcase string "CABBCC" satisfies the requirements. There exist other possible strings. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 mission in this problem is to write a computer program that manipulates molecular for- mulae in virtual chemistry. As in real chemistry, each molecular formula represents a molecule consisting of one or more atoms. However, it may not have chemical reality. The following are the definitions of atomic symbols and molecular formulae you should consider. * An atom in a molecule is represented by an atomic symbol, which is either a single capital letter or a capital letter followed by a small letter. For instance H and He are atomic symbols. * A molecular formula is a non-empty sequence of atomic symbols. For instance, HHHeHHHe is a molecular formula, and represents a molecule consisting of four H’s and two He’s. * For convenience, a repetition of the same sub-formula <image> where n is an integer between 2 and 99 inclusive, can be abbreviated to (X)n. Parentheses can be omitted if X is an atomic symbol. For instance, HHHeHHHe is also written as H2HeH2He, (HHHe)2, (H2He)2, or even ((H)2He)2. The set of all molecular formulae can be viewed as a formal language. Summarizing the above description, the syntax of molecular formulae is defined as follows. <image> Each atom in our virtual chemistry has its own atomic weight. Given the weights of atoms, your program should calculate the weight of a molecule represented by a molecular formula. The molecular weight is defined by the sum of the weights of the constituent atoms. For instance, assuming that the atomic weights of the atoms whose symbols are H and He are 1 and 4, respectively, the total weight of a molecule represented by (H2He)2 is 12. Input The input consists of two parts. The first part, the Atomic Table, is composed of a number of lines, each line including an atomic symbol, one or more spaces, and its atomic weight which is a positive integer no more than 1000. No two lines include the same atomic symbol. The first part ends with a line containing only the string END OF FIRST PART. The second part of the input is a sequence of lines. Each line is a molecular formula, not exceeding 80 characters, and contains no spaces. A molecule contains at most 105 atoms. Some atomic symbols in a molecular formula may not appear in the Atomic Table. The sequence is followed by a line containing a single zero, indicating the end of the input. Output The output is a sequence of lines, one for each line of the second part of the input. Each line contains either an integer, the molecular weight for a given molecular formula in the correspond- ing input line if all its atomic symbols appear in the Atomic Table, or UNKNOWN otherwise. No extra characters are allowed. Example Input H 1 He 4 C 12 O 16 F 19 Ne 20 Cu 64 Cc 333 END_OF_FIRST_PART H2C (MgF)2As Cu(OH)2 H((CO)2F)99 0 Output 14 UNKNOWN 98 7426 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally. Input The first line of the input contains three integers n, m, k (1 ≀ n, m, k ≀ 1000) β€” the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals two, then on day i Valera will eat a second type dish. Output Print a single integer β€” the minimum number of times Valera will need to wash a plate/bowl. Examples Input 3 1 1 1 2 1 Output 1 Input 4 3 1 1 1 1 1 Output 1 Input 3 1 2 2 2 2 Output 0 Input 8 2 2 1 2 1 2 1 2 1 2 Output 4 Note In the first sample Valera will wash a bowl only on the third day, so the answer is one. In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once. In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.