question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the maximum value of $n$. You are given a positive integer number $n$. You really love good numbers so you want to find the smallest good number greater than or equal to $n$. The positive integer is called good if it can be represented as a sum of distinct powers of $3$ (i.e. no duplicates of powers of $3$ are allowed). For example: $30$ is a good number: $30 = 3^3 + 3^1$, $1$ is a good number: $1 = 3^0$, $12$ is a good number: $12 = 3^2 + 3^1$, but $2$ is not a good number: you can't represent it as a sum of distinct powers of $3$ ($2 = 3^0 + 3^0$), $19$ is not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representations $19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$ are invalid), $20$ is also not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representation $20 = 3^2 + 3^2 + 3^0 + 3^0$ is invalid). Note, that there exist other representations of $19$ and $20$ as sums of powers of $3$ but none of them consists of distinct powers of $3$. For the given positive integer $n$ find such smallest $m$ ($n \le m$) that $m$ is a good number. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. Then $q$ queries follow. The only line of the query contains one integer $n$ ($1 \le n \le 10^4$). -----Output----- For each query, print such smallest integer $m$ (where $n \le m$) that $m$ is a good number. -----Example----- Input 7 1 2 6 13 14 3620 10000 Output 1 3 9 13 27 6561 19683 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 matrix (H Γ— W) which contains only 1 and 0, find the area of the largest rectangle which only contains 0s. Constraints * 1 ≀ H, W ≀ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H Γ— W matrix, are given. Output Print the area (the number of 0s) of the largest rectangle. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 6 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Elephant from the Zoo of Lviv likes cards. He has N cards, each of which has one of 1000 colors. The colors are numbered from 1 to 1000. Little Elephant and Big Hippo are playing the following game. At first Little Elephant takes some subset of cards, and Big Hippo takes the rest of them. Here, Little Elephant can choose to take all of the cards, or none of the cards. Then they play 1000 rounds: in round k (k = 1, 2, ..., 1000), they count the number of cards each has of the color k. Let Little Elephant has a cards of the color k, and Big Hippo has b cards of the color k. Then if a > b Little Elephant scores |a-b| points, otherwise Big Hippo scores |a-b| points in this round, where |x| denotes the absolute value of x. You are given the number of cards N and the array C - list of colors of all N cards. Find the number of subsets (among all 2^{N} subsets) for which Little Elephant wins the game: that is, he gains more points than Big Hippo in total, if Little Elephant takes the subset at first. Since the answer can be large, print it modulo 1000000007 (10^{9}+7). ------ Input ------ First line of the input contains single integer T - the number of test cases. Then T test cases follow. First line of each test case contains single integer N. Second line contains N integers separated by space - the array C. ------ Output ------ For each test case, print the answer in one line. ------ Constraints ------ 1 ≀ T ≀ 100 1 ≀ N ≀ 1000 1 ≀ C_{i} ≀ 1000, where C_{i} denotes the i-th element of the array C ----- Sample Input 1 ------ 2 3 1 2 3 4 1 1 3 2 ----- Sample Output 1 ------ 4 5 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!). The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β€” to the 1-st and the 3-rd ones, the 3-rd one β€” only to the 2-nd one. The transitions are possible only between the adjacent sections. The spacecraft team consists of n aliens. Each of them is given a rank β€” an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship. Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B. At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task. Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m. Input The first line contains two space-separated integers: n and m (1 ≀ n, m ≀ 109) β€” the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly. Output Print a single number β€” the answer to the problem modulo m. Examples Input 1 10 Output 2 Input 3 8 Output 2 Note In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes. To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test 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. A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not. You are given a string $s$ of length $n$, consisting of digits. In one operation you can delete any character from string $s$. For example, it is possible to obtain strings 112, 111 or 121 from string 1121. You need to determine whether there is such a sequence of operations (possibly empty), after which the string $s$ becomes a telephone number. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 100$) β€” the length of string $s$. The second line of each test case contains the string $s$ ($|s| = n$) consisting of digits. -----Output----- For each test print one line. If there is a sequence of operations, after which $s$ becomes a telephone number, print YES. Otherwise, print NO. -----Example----- Input 2 13 7818005553535 11 31415926535 Output YES NO -----Note----- In the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press β€” upvote and downvote. However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes. $n$ reviewers enter the site one by one. Each reviewer is one of the following types: type $1$: a reviewer has watched the movie, and they like it β€” they press the upvote button; type $2$: a reviewer has watched the movie, and they dislike it β€” they press the downvote button; type $3$: a reviewer hasn't watched the movie β€” they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once. Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one. What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. Then the descriptions of $t$ testcases follow. The first line of each testcase contains a single integer $n$ ($1 \le n \le 50$) β€” the number of reviewers. The second line of each testcase contains $n$ integers $r_1, r_2, \dots, r_n$ ($1 \le r_i \le 3$) β€” the types of the reviewers in the same order they enter the site. -----Output----- For each testcase print a single integer β€” the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to. -----Examples----- Input 4 1 2 3 1 2 3 5 1 1 1 1 1 3 3 3 2 Output 0 2 5 2 -----Note----- In the first testcase of the example you can send the only reviewer to either of the servers β€” they'll downvote anyway. The movie won't receive any upvotes. In the second testcase of the example you can send all reviewers to the first server: the first reviewer upvotes; the second reviewer downvotes; the last reviewer sees that the number of downvotes is not greater than the number of upvotes β€” upvote themselves. There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer β€” to the second server: the first reviewer upvotes on the first server; the second reviewer downvotes on the first server; the last reviewer sees no upvotes or downvotes on the second server β€” upvote themselves. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 plays the Need For Brake. He plays because he was presented with a new computer wheel for birthday! Now he is sure that he will win the first place in the championship in his favourite racing computer game! n racers take part in the championship, which consists of a number of races. After each race racers are arranged from place first to n-th (no two racers share the same place) and first m places are awarded. Racer gains bi points for i-th awarded place, which are added to total points, obtained by him for previous races. It is known that current summary score of racer i is ai points. In the final standings of championship all the racers will be sorted in descending order of points. Racers with an equal amount of points are sorted by increasing of the name in lexicographical order. Unfortunately, the championship has come to an end, and there is only one race left. Vasya decided to find out what the highest and lowest place he can take up as a result of the championship. Input The first line contains number n (1 ≀ n ≀ 105) β€” number of racers. Each of the next n lines contains si and ai β€” nick of the racer (nonempty string, which consist of no more than 20 lowercase Latin letters) and the racer's points (0 ≀ ai ≀ 106). Racers are given in the arbitrary order. The next line contains the number m (0 ≀ m ≀ n). Then m nonnegative integer numbers bi follow. i-th number is equal to amount of points for the i-th awarded place (0 ≀ bi ≀ 106). The last line contains Vasya's racer nick. Output Output two numbers β€” the highest and the lowest place Vasya can take up as a result of the championship. Examples Input 3 teama 10 teamb 20 teamc 40 2 10 20 teama Output 2 3 Input 2 teama 10 teamb 10 2 10 10 teamb Output 2 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A positive integer is called a 2-3-integer, if it is equal to 2^{x}Β·3^{y} for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β€” are 2-3 integers, while 5, 10, 21 and 120 are not. Print the number of 2-3-integers on the given segment [l, r], i.Β e. the number of sich 2-3-integers t that l ≀ t ≀ r. -----Input----- The only line contains two integers l and r (1 ≀ l ≀ r ≀ 2Β·10^9). -----Output----- Print a single integer the number of 2-3-integers on the segment [l, r]. -----Examples----- Input 1 10 Output 7 Input 100 200 Output 5 Input 1 2000000000 Output 326 -----Note----- In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9. In the second example the 2-3-integers are 108, 128, 144, 162 and 192. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a permutation $p$ of $n$ integers $1$, $2$, ..., $n$ (a permutation is an array where each element from $1$ to $n$ occurs exactly once). Let's call some subsegment $p[l, r]$ of this permutation special if $p_l + p_r = \max \limits_{i = l}^{r} p_i$. Please calculate the number of special subsegments. -----Input----- The first line contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$). The second line contains $n$ integers $p_1$, $p_2$, ..., $p_n$ ($1 \le p_i \le n$). All these integers are pairwise distinct. -----Output----- Print the number of special subsegments of the given permutation. -----Examples----- Input 5 3 4 1 5 2 Output 2 Input 3 1 3 2 Output 1 -----Note----- Special subsegments in the first example are $[1, 5]$ and $[1, 3]$. The only special subsegment in the second example is $[1, 3]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Fourier transformations are hard. Fouriest transformations are harder. This Kata is based on the SMBC Comic on fourier transformations. A fourier transformation on a number is one that converts the number to a base in which it has more `4`s ( `10` in base `6` is `14`, which has `1` four as opposed to none, hence, fourier in base `6` ). A number's fouriest transformation converts it to the base in which it has the most `4`s. For example: `35353` is the fouriest in base `6`: `431401`. This kata requires you to create a method `fouriest` that takes a number and makes it the fouriest, telling us in which base this happened, as follows: ```python fouriest(number) -> "{number} is the fouriest ({fouriest_representation}) in base {base}" ``` ## Important notes * For this kata we don't care about digits greater than `9` ( only `0` to `9` ), so we will represent all digits greater than `9` as `'x'`: `10` in base `11` is `'x'`, `119` in base `20` is `'5x'`, `118` in base `20` is also `'5x'` * When a number has several fouriest representations, we want the one with the LOWEST base ```if:haskell,javascript * Numbers below `9` will not be tested ``` ```if:javascript * A `BigNumber` library has been provided; documentation is [here](https://mikemcl.github.io/bignumber.js/) ``` ## Examples ```python "30 is the fouriest (42) in base 7" "15 is the fouriest (14) in base 11" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 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. Taro came to a square to look for treasure. There are many treasures buried in this square, but Taro has the latest machines, so he knows everything about where the treasures are buried. Since the square is very wide Taro decided to look for the treasure to decide the area, but the treasure is what treasure does not know immediately whether or not there in the area for a lot. So Taro decided to count the number of treasures in that area. Constraints > 1 ≀ n ≀ 5000 > 1 ≀ m ≀ 5 Γ— 105 > | xi |, | yi | ≀ 109 (1 ≀ i ≀ n) > | xi1 |, | yi1 |, | xi2 |, | yi2 | ≀ 109 (1 ≀ i ≀ m) > xi1 ≀ xi2, yi1 ≀ yi2 (1 ≀ i ≀ m) > * All inputs are given as integers Input > n m > x1 y1 > x2 y2 > ... > xn yn > x11 y11 x12 y12 > x21 y21 x22 y22 > ... > xm1 ym1 xm2 ym2 > * n represents the number of treasures buried in the square * m represents the number of regions to examine * The 2nd to n + 1 lines represent the coordinates where each treasure is buried. * The n + 2nd to n + m + 1 lines represent each area to be examined. * The positive direction of the x-axis represents the east and the positive direction of the y-axis represents the north. * Each region is a rectangle, xi1 and yi1 represent the coordinates of the southwestern apex of the rectangle, and xi2 and yi2 represent the coordinates of the northeastern apex of the rectangle. Output > C1 > C2 > ... > Cm > * Output the number of treasures contained in each area to each line Examples Input 3 1 1 1 2 4 5 3 0 0 5 5 Output 3 Input 4 2 -1 1 0 3 4 0 2 1 -3 1 5 1 4 0 4 0 Output 2 1 Input 2 3 0 0 0 0 -1 -1 1 1 0 0 2 2 1 1 4 4 Output 2 2 0 Input 5 5 10 5 -3 -8 2 11 6 0 -1 3 -3 1 3 13 -1 -1 9 5 -3 -8 10 11 0 0 5 5 -10 -9 15 10 Output 2 2 5 0 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Passer ratings are the generally accepted standard for evaluating NFL quarterbacks. I knew a rating of 100 is pretty good, but never knew what makes up the rating. So out of curiosity I took a look at the wikipedia page and had an idea or my first kata: https://en.wikipedia.org/wiki/Passer_rating ## Formula There are four parts to the NFL formula: ```python A = ((Completions / Attempts) - .3) * 5 B = ((Yards / Attempts) - 3) * .25 C = (Touchdowns / Attempt) * 20 D = 2.375 - ((Interceptions / Attempts) * 25) ``` However, if the result of any calculation is greater than `2.375`, it is set to `2.375`. If the result is a negative number, it is set to zero. Finally the passer rating is: `((A + B + C + D) / 6) * 100` Return the rating rounded to the nearest tenth. ## Example Last year Tom Brady had 432 attempts, 3554 yards, 291 completions, 28 touchdowns, and 2 interceptions. His passer rating was 112.2 Happy coding! Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Berland recently a new collection of toys went on sale. This collection consists of 10^9 types of toys, numbered with integers from 1 to 10^9. A toy from the new collection of the i-th type costs i bourles. Tania has managed to collect n different types of toys a_1, a_2, ..., a_{n} from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has. Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this. -----Input----- The first line contains two integers n (1 ≀ n ≀ 100 000) and m (1 ≀ m ≀ 10^9)Β β€” the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys. The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9)Β β€” the types of toys that Tanya already has. -----Output----- In the first line print a single integer kΒ β€” the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m. In the second line print k distinct space-separated integers t_1, t_2, ..., t_{k} (1 ≀ t_{i} ≀ 10^9)Β β€” the types of toys that Tanya should choose. If there are multiple answers, you may print any of them. Values of t_{i} can be printed in any order. -----Examples----- Input 3 7 1 3 4 Output 2 2 5 Input 4 14 4 6 12 8 Output 4 7 2 3 1 -----Note----- In the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Decimal numbers are a common notation system currently in use and use ten symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to represent all numbers. Binary numbers are a popular notation in the computer world and use two symbols, 0 and 1, to represent all numbers. Only the four numbers 0, 1, 2, and 3 are used in quaternary numbers. In quaternary numbers, when the number is incremented from 0, it will be carried to the next digit when it reaches 4. Therefore, the decimal number 4 is carried to the expression "10". Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |- --- Binary | 0 | 1 | 10 | 11 | 100 | 101 | 110 | 111 | 1000 | 101 | 1010 | ... Quadrant | 0 | 1 | 2 | 3 | 10 | 11 | 12 | 13 | 20 | 21 | 22 | ... In Hawaii, fish and taro were counted between fingers in the old days, so it seems that they used quaternary numbers instead of decimal numbers. Create a program that converts the integer n input in decimal to decimal and outputs it. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of -1. One integer n (0 ≀ n ≀ 1000000) is given on one row for each dataset. The number of datasets does not exceed 2000. Output The result of conversion to quaternary number for each input data set is output on one line. Example Input 7 4 0 12 10 10000 -1 Output 13 10 0 30 22 2130100 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is an infinitely long street that runs west to east, which we consider as a number line. There are N roadworks scheduled on this street. The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5. Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point. Find the distance each of the Q people will walk. -----Constraints----- - All values in input are integers. - 1 \leq N, Q \leq 2 \times 10^5 - 0 \leq S_i < T_i \leq 10^9 - 1 \leq X_i \leq 10^9 - 0 \leq D_1 < D_2 < ... < D_Q \leq 10^9 - If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap. -----Input----- Input is given from Standard Input in the following format: N Q S_1 T_1 X_1 : S_N T_N X_N D_1 : D_Q -----Output----- Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever. -----Sample Input----- 4 6 1 3 2 7 13 10 18 20 13 3 4 2 0 1 2 3 5 8 -----Sample Output----- 2 2 10 -1 13 -1 The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2. The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2. The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases 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. Find places where a string P is found within a text T. Print all indices of T where P found. The indices of T start with 0. Constraints * 1 ≀ length of T ≀ 1000000 * 1 ≀ length of P ≀ 10000 * The input consists of alphabetical characters and digits Input In the first line, a text T is given. In the second line, a string P is given. Output Print an index of T where P found in a line. Print the indices in ascending order. Examples Input aabaaa aa Output 0 3 4 Input xyzz yz Output 1 Input abc xyz Output Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≀ i ≀ n) such that ai > 0 and an integer t (t β‰₯ 0) such that i + 2t ≀ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≀ k < n). Input The first input line contains a single integer n. The second line contains n integers ai (0 ≀ ai ≀ 104), separated by single spaces. The input limitations for getting 20 points are: * 1 ≀ n ≀ 300 The input limitations for getting 50 points are: * 1 ≀ n ≀ 2000 The input limitations for getting 100 points are: * 1 ≀ n ≀ 105 Output Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier. Examples Input 4 1 0 1 2 Output 1 1 3 Input 8 1 2 3 4 5 6 7 8 Output 1 3 6 10 16 24 40 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sakuzyo - Imprinting A.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them. It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal. ConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there. CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant. Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way! -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases in the test. Then the descriptions of $t$ test cases follow. The first line of a test case contains three integers $n$, $s$ and $k$ ($2 \le n \le 10^9$, $1 \le s \le n$, $1 \le k \le \min(n-1, 1000)$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $k$ distinct integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $k$ over all test cases does not exceed $1000$. -----Output----- For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant. -----Example----- Input 5 5 2 3 1 2 3 4 3 3 4 1 2 10 2 6 1 2 3 4 5 7 2 1 1 2 100 76 8 76 75 36 67 41 74 10 77 Output 2 0 4 0 2 -----Note----- In the first example test case, the nearest floor with an open restaurant would be the floor $4$. In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere. In the third example test case, the closest open restaurant is on the $6$-th floor. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Yes, your eyes are no problem, this is toLoverCase (), not toLowerCase (), we want to make the world full of love. ### What do we need to do? You need to add a prototype function to the String, the name is toLoverCase. Function can convert the letters in the string, converted to "L", "O", "V", "E", if not the letter, don't change it. ### How to convert? : ``` "love!".toLoverCase()="EVOL!" ``` more example see the testcases. ### Series: - [Bug in Apple](http://www.codewars.com/kata/56fe97b3cc08ca00e4000dc9) - [Father and Son](http://www.codewars.com/kata/56fe9a0c11086cd842000008) - [Jumping Dutch act](http://www.codewars.com/kata/570bcd9715944a2c8e000009) - [Planting Trees](http://www.codewars.com/kata/5710443187a36a9cee0005a1) - [Give me the equation](http://www.codewars.com/kata/56fe9b65cc08cafbc5000de3) - [Find the murderer](http://www.codewars.com/kata/570f3fc5b29c702c5500043e) - [Reading a Book](http://www.codewars.com/kata/570ca6a520c69f39dd0016d4) - [Eat watermelon](http://www.codewars.com/kata/570df12ce6e9282a7d000947) - [Special factor](http://www.codewars.com/kata/570e5d0b93214b1a950015b1) - [Guess the Hat](http://www.codewars.com/kata/570ef7a834e61306da00035b) - [Symmetric Sort](http://www.codewars.com/kata/5705aeb041e5befba20010ba) - [Are they symmetrical?](http://www.codewars.com/kata/5705cc3161944b10fd0004ba) - [Max Value](http://www.codewars.com/kata/570771871df89cf59b000742) - [Trypophobia](http://www.codewars.com/kata/56fe9ffbc25bf33fff000f7c) - [Virus in Apple](http://www.codewars.com/kata/5700af83d1acef83fd000048) - [Balance Attraction](http://www.codewars.com/kata/57033601e55d30d3e0000633) - [Remove screws I](http://www.codewars.com/kata/5710a50d336aed828100055a) - [Remove screws II](http://www.codewars.com/kata/5710a8fd336aed00d9000594) - [Regular expression compression](http://www.codewars.com/kata/570bae4b0237999e940016e9) - [Collatz Array(Split or merge)](http://www.codewars.com/kata/56fe9d579b7bb6b027000001) - [Tidy up the room](http://www.codewars.com/kata/5703ace6e55d30d3e0001029) - [Waiting for a Bus](http://www.codewars.com/kata/57070eff924f343280000015) Read the inputs from stdin solve the problem and write the answer to stdout (do 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 tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: - 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex. - 2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex. - 3. Go back to step 1. Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible. Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy. It can be proved that the game is bound to end. -----Constraints----- - 2 \leq N \leq 10^5 - 1 \leq u,v \leq N - u \neq v - 1 \leq A_i,B_i \leq N - The given graph is a tree. -----Input----- Input is given from Standard Input in the following format: N u v A_1 B_1 : A_{N-1} B_{N-1} -----Output----- Print the number of moves Aoki will perform before the end of the game. -----Sample Input----- 5 4 1 1 2 2 3 3 4 3 5 -----Sample Output----- 2 If both players play optimally, the game will progress as follows: - Takahashi moves to Vertex 3. - Aoki moves to Vertex 2. - Takahashi moves to Vertex 5. - Aoki moves to Vertex 3. - Takahashi moves to Vertex 3. Here, Aoki performs two moves. Note that, in each move, it is prohibited to stay at the current vertex. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where Ξ΅ < 10^{ - 7}. To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help. -----Input----- First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer p_{i} (1 ≀ p_{i} ≀ 1000) β€” i-th query. -----Output----- Output q lines. On i-th of them output single integer β€” answer for i-th query. -----Examples----- Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have to create a method "compoundArray" which should take as input two int arrays of different length and return one int array with numbers of both arrays shuffled one by one. ```Example: Input - {1,2,3,4,5,6} and {9,8,7,6} Output - {1,9,2,8,3,7,4,6,5,6} ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are n types of monsters, each with an ID between 1 and n. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monsters may be able to split in multiple ways. At the begining Piegirl has exactly one monster. She begins by feeding the monster a pie. She continues feeding pies to monsters until no more monsters are left. Then she collects all the diamonds that were created. You will be given a list of split rules describing the way in which the various monsters can split. Every monster can split in at least one way, and if a monster can split in multiple ways then each time when it splits Piegirl can choose the way it splits. For each monster, determine the smallest and the largest number of diamonds Piegirl can possibly collect, if initially she has a single instance of that monster. Piegirl has an unlimited supply of pies. Input The first line contains two integers: m and n (1 ≀ m, n ≀ 105), the number of possible splits and the number of different monster types. Each of the following m lines contains a split rule. Each split rule starts with an integer (a monster ID) mi (1 ≀ mi ≀ n), and a positive integer li indicating the number of monsters and diamonds the current monster can split into. This is followed by li integers, with positive integers representing a monster ID and -1 representing a diamond. Each monster will have at least one split rule. Each split rule will have at least one diamond. The sum of li across all split rules will be at most 105. Output For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an arbitrarily large number of diamonds, print -2 as the largest number of diamonds. If any number in output exceeds 314000000 (but is finite), print 314000000 instead of that number. Examples Input 6 4 1 3 -1 1 -1 1 2 -1 -1 2 3 -1 3 -1 2 3 -1 -1 -1 3 2 -1 -1 4 2 4 -1 Output 2 -2 3 4 2 2 -1 -1 Input 3 2 1 2 1 -1 2 2 -1 -1 2 3 2 1 -1 Output -1 -1 2 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi has an empty string S and a variable x whose initial value is 0. Also, we have a string T consisting of `0` and `1`. Now, Takahashi will do the operation with the following two steps |T| times. * Insert a `0` or a `1` at any position of S of his choice. * Then, increment x by the sum of the digits in the odd positions (first, third, fifth, ...) of S. For example, if S is `01101`, the digits in the odd positions are `0`, `1`, `1` from left to right, so x is incremented by 2. Print the maximum possible final value of x in a sequence of operations such that S equals T in the end. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `0` and `1`. Input Input is given from Standard Input in the following format: T Output Print the maximum possible final value of x in a sequence of operations such that S equals T in the end. Examples Input 1101 Output 5 Input 0111101101 Output 26 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 2 elevators (named "left" and "right") in a building with 3 floors (numbered `0` to `2`), write a function `elevator` accepting 3 arguments (in order): - `left` - The current floor of the left elevator - `right` - The current floor of the right elevator - `call` - The floor that called an elevator It should return the name of the elevator closest to the called floor (`"left"`/`"right"`). In the case where both elevators are equally distant from the called floor, choose the elevator to the right. You can assume that the inputs will always be valid integers between 0-2. Examples: ```python elevator(0, 1, 0) # => "left" elevator(0, 1, 1) # => "right" elevator(0, 1, 2) # => "right" elevator(0, 0, 0) # => "right" elevator(0, 2, 1) # => "right" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Problem Given the integer n, output the smallest m such that nCm (the number of combinations that choose m out of n different ones) is even. Constraints The input satisfies the following conditions. * 1 ≀ n ≀ 1018 Input The input is given in the following format. n Output Output the minimum m such that nCm is an even number on one line. Examples Input 2 Output 1 Input 111 Output 16 Input 3 Output 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. Write a function that accepts two parameters (a and b) and says whether a is smaller than, bigger than, or equal to b. Here is an example code: var noIfsNoButs = function (a,b) { if(a > b) return a + " is greater than " + b else if(a < b) return a + " is smaller than " + b else if(a == b) return a + " is equal to " + b } There's only one problem... You can't use if statements, and you can't use shorthands like (a < b)?true:false; in fact the word "if" and the character "?" are not allowed in the code. Inputs are guarenteed to be numbers You are always welcome to check out some of my other katas: Very Easy (Kyu 8) Add Numbers Easy (Kyu 7-6) Convert Color image to greyscale Array Transformations Basic Compression Find Primes in Range No Ifs No Buts Medium (Kyu 5-4) Identify Frames In An Image Photoshop Like - Magic Wand Scientific Notation Vending Machine - FSA Find Matching Parenthesis Hard (Kyu 3-2) Ascii Art Generator Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i). Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N. In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B. Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i). Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N. When activated, each type of robot will operate as follows. - When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything. - When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything. Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots. -----Constraints----- - 1 \leq N \leq 100 - 1 \leq K \leq 100 - 0 < x_i < K - All input values are integers. -----Inputs----- Input is given from Standard Input in the following format: N K x_1 x_2 ... x_N -----Outputs----- Print the minimum possible total distance covered by robots. -----Sample Input----- 1 10 2 -----Sample Output----- 4 There are just one ball, one type-A robot and one type-B robot. If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4. Similarly, if the type-B robot is used, the total distance covered will be 16. Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 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. There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive (1 Based indexing). You are guaranteed that after excluding the part of the array remaining array is non empty. ------ Input ------ First line of input contains an integer T denoting number of test cases. For each test case, first line will contain two space separated integers N, Q. Next line contains N space separated integers denoting array A. For next Q lines, each line will contain a query denoted by two space separated integers L, R. ------ Output ------ For each query, print a single integer representing the answer of that query. ------ Constraints ------ Subtask #1: 40 points $2 ≀ T, N ≀ 100, 1 ≀ Q ≀ N, 1 ≀ A[i] ≀ 10^{5}$ $1 ≀ L, R ≀ N and L ≀ R$ Subtask #2: 60 points $2 ≀ T, N ≀ 10^{5}, 1 ≀ Q ≀ N, 1 ≀ A[i] ≀ 10^{5}$ $1 ≀ L, R ≀ N and L ≀ R$ $ Sum of N over all the test cases will be less than or equal to 10^{6}.Warning : Large IO(input output), please use faster method for IO. ----- Sample Input 1 ------ 1 3 3 2 6 9 1 1 2 2 2 3 ----- Sample Output 1 ------ 3 1 2 ----- explanation 1 ------ Test case $1$: The given array is $[2, 6, 9]$. - Query $1$: On excluding the range $[1,1]$, the remaining elements are $[6, 9]$. The *gcd* of $6$ and $9$ is $3$. - Query $2$: On excluding the range $[2,2]$, the remaining elements are $[2, 9]$. The *gcd* of $2$ and $9$ is $1$. - Query $3$: On excluding the range $[2,3]$, the remaining elements are $[2]$. The *gcd* of $2$ 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. As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively. Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques. Let's define a divisibility graph for a set of positive integers A = {a_1, a_2, ..., a_{n}} as follows. The vertices of the given graph are numbers from set A, and two numbers a_{i} and a_{j} (i β‰  j) are connected by an edge if and only if either a_{i} is divisible by a_{j}, or a_{j} is divisible by a_{i}. You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^6), that sets the size of set A. The second line contains n distinct positive integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^6) β€” elements of subset A. The numbers in the line follow in the ascending order. -----Output----- Print a single number β€” the maximum size of a clique in a divisibility graph for set A. -----Examples----- Input 8 3 4 6 8 10 18 21 24 Output 3 -----Note----- In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. Read the inputs from stdin solve the problem and write the answer to stdout (do 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're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Multiple polygonal lines are given on the xy-plane. Given a list of polygonal lines and a template, you must find out polygonal lines which have the same shape as the template. A polygonal line consists of several line segments parallel to x-axis or y-axis. It is defined by a list of xy-coordinates of vertices from the start-point to the end-point in order, and always turns 90 degrees at each vertex. A single polygonal line does not pass the same point twice. Two polygonal lines have the same shape when they fully overlap each other only with rotation and translation within xy-plane (i.e. without magnification or a flip). The vertices given in reverse order from the start-point to the end-point is the same as that given in order. Figure 1 shows examples of polygonal lines. In this figure, polygonal lines A and B have the same shape. Write a program that answers polygonal lines which have the same shape as the template. <image> --- Figure 1: Polygonal lines Input The input consists of multiple datasets. The end of the input is indicated by a line which contains a zero. A dataset is given as follows. > n > Polygonal line0 > Polygonal line1 > Polygonal line2 > ... > Polygonal linen n is the number of polygonal lines for the object of search on xy-plane. n is an integer, and 1 <= n <= 50. Polygonal line0 indicates the template. A polygonal line is given as follows. > m > x1 y1 > x2 y2 > ... > xm ym > m is the number of the vertices of a polygonal line (3 <= m <= 10). xi and yi, separated by a space, are the x- and y-coordinates of a vertex, respectively (-10000 < xi < 10000, -10000 <yi < 10000). Output For each dataset in the input, your program should report numbers assigned to the polygonal lines that have the same shape as the template, in ascending order. Each number must be written in a separate line without any other characters such as leading or trailing spaces. Five continuous "+"s must be placed in a line at the end of each dataset. Example Input 5 5 0 0 2 0 2 1 4 1 4 0 5 0 0 0 2 -1 2 -1 4 0 4 5 0 0 0 1 -2 1 -2 2 0 2 5 0 0 0 -1 2 -1 2 0 4 0 5 0 0 2 0 2 -1 4 -1 4 0 5 0 0 2 0 2 1 4 1 4 0 4 4 -60 -75 -60 -78 -42 -78 -42 -6 4 10 3 10 7 -4 7 -4 40 4 -74 66 -74 63 -92 63 -92 135 4 -12 22 -12 25 -30 25 -30 -47 4 12 -22 12 -25 30 -25 30 47 3 5 -8 5 -8 2 0 2 0 4 8 4 5 -3 -1 0 -1 0 7 -2 7 -2 16 5 -1 6 -1 3 7 3 7 5 16 5 5 0 1 0 -2 8 -2 8 0 17 0 0 Output 1 3 5 +++++ 3 4 +++++ +++++ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given $n$ numbers $a_1, a_2, \dots, a_n$. With a cost of one coin you can perform the following operation: Choose one of these numbers and add or subtract $1$ from it. In particular, we can apply this operation to the same number several times. We want to make the product of all these numbers equal to $1$, in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$. For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$. And $1\cdot (-1) \cdot (-1) = 1$. What is the minimum cost we will have to pay to do that? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” the numbers. -----Output----- Output a single numberΒ β€” the minimal number of coins you need to pay to make the product equal to $1$. -----Examples----- Input 2 -1 1 Output 2 Input 4 0 0 0 0 Output 4 Input 5 -5 -3 5 3 0 Output 13 -----Note----- In the first example, you can change $1$ to $-1$ or $-1$ to $1$ in $2$ coins. In the second example, you have to apply at least $4$ operations for the product not to be $0$. In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are times you recall a good old friend and everything you've come through together. Luckily there are social networksΒ β€” they store all your message history making it easy to know what you argued over 10 years ago. More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat. Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown. Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner. Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one. -----Input----- The first line contains two integers n and k (1 ≀ n ≀ 10^5, 0 ≀ k ≀ n) β€” the total amount of messages and the number of previous and next messages visible. The second line features a sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} < i), where a_{i} denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x. -----Output----- Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible. -----Examples----- Input 6 0 0 1 1 2 3 2 Output 1 2 2 3 3 3 Input 10 1 0 1 0 3 4 5 2 3 7 0 Output 2 3 3 4 5 6 6 6 8 2 Input 2 2 0 1 Output 2 2 -----Note----- Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go. In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 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. Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result. -----Input----- The first line contains integer n (1 ≀ n ≀ 100) β€” the number of rectangles. Each of the following n lines contains four integers x_1, y_1, x_2, y_2 (1 ≀ x_1 ≀ x_2 ≀ 100, 1 ≀ y_1 ≀ y_2 ≀ 100), where x_1 and y_1 are the number of the column and row of the lower left cell and x_2 and y_2 are the number of the column and row of the upper right cell of a rectangle. -----Output----- In a single line print the sum of all values in the cells of the table. -----Examples----- Input 2 1 1 2 3 2 2 3 3 Output 10 Input 2 1 1 3 3 1 1 3 3 Output 18 -----Note----- Note to the first sample test: Values of the table in the first three rows and columns will be as follows: 121 121 110 So, the sum of values will be equal to 10. Note to the second sample test: Values of the table in the first three rows and columns will be as follows: 222 222 222 So, the sum of values will be equal to 18. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For a given polygon g, print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≀ i ≀ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon. Constraints * 3 ≀ n ≀ 100 * -10000 ≀ xi, yi ≀ 10000 * No point of the polygon will occur more than once. * Two sides of the polygon can intersect only at a common endpoint. Input g is given by coordinates of the points p1,..., pn in the following format: n x1 y1 x2 y2 : xn yn The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them. Output Print "1" or "0" in a line. Examples Input 4 0 0 3 1 2 3 0 3 Output 1 Input 5 0 0 2 0 1 1 2 2 0 2 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them. The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct. Andryusha wants to use as little different colors as possible. Help him to choose the colors! -----Input----- The first line contains single integer n (3 ≀ n ≀ 2Β·10^5)Β β€” the number of squares in the park. Each of the next (n - 1) lines contains two integers x and y (1 ≀ x, y ≀ n)Β β€” the indices of two squares directly connected by a path. It is guaranteed that any square is reachable from any other using the paths. -----Output----- In the first line print single integer kΒ β€” the minimum number of colors Andryusha has to use. In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k. -----Examples----- Input 3 2 3 1 3 Output 3 1 3 2 Input 5 2 3 5 3 4 3 1 3 Output 5 1 3 2 5 4 Input 5 2 1 3 2 4 3 5 4 Output 3 1 2 3 1 2 -----Note----- In the first sample the park consists of three squares: 1 β†’ 3 β†’ 2. Thus, the balloon colors have to be distinct. [Image] Illustration for the first sample. In the second example there are following triples of consequently connected squares: 1 β†’ 3 β†’ 2 1 β†’ 3 β†’ 4 1 β†’ 3 β†’ 5 2 β†’ 3 β†’ 4 2 β†’ 3 β†’ 5 4 β†’ 3 β†’ 5 We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. [Image] Illustration for the second sample. In the third example there are following triples: 1 β†’ 2 β†’ 3 2 β†’ 3 β†’ 4 3 β†’ 4 β†’ 5 We can see that one or two colors is not enough, but there is an answer that uses three colors only. [Image] Illustration for the third sample. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal. Given a string S, determine whether it is coffee-like. -----Constraints----- - S is a string of length 6 consisting of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S is coffee-like, print Yes; otherwise, print No. -----Sample Input----- sippuu -----Sample Output----- Yes In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 has decided to work on K days of his choice from the N days starting with tomorrow. You are given an integer C and a string S. Takahashi will choose his workdays as follows: - After working for a day, he will refrain from working on the subsequent C days. - If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on. Find all days on which Takahashi is bound to work. -----Constraints----- - 1 \leq N \leq 2 \times 10^5 - 1 \leq K \leq N - 0 \leq C \leq N - The length of S is N. - Each character of S is o or x. - Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied. -----Input----- Input is given from Standard Input in the following format: N K C S -----Output----- Print all days on which Takahashi is bound to work in ascending order, one per line. -----Sample Input----- 11 3 2 ooxxxoxxxoo -----Sample Output----- 6 Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days. There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11. Thus, he is bound to work on Day 6. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2n flowers, numbered from 1 to 2n clockwise, each of which has a colour among all n possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals n. Additionally, if flowers u and v are of the same colour, then flowers opposite to u and opposite to v should be of the same colour as well β€” symmetry is beautiful! Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with n = 6 that cover all possibilities. <image> The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1 Γ— 3 Γ— 1 Γ— 3 = 9 β€” the segments are {2}, {4, 5, 6}, {8} and {10, 11, 12}. While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998 244 353. Two arrangements are considered different, if a pair (u, v) (1 ≀ u, v ≀ 2n) exists such that flowers u and v are of the same colour in one of them, but not in the other. Input The first and only line of input contains a lonely positive integer n (3 ≀ n ≀ 50 000) β€” the number of colours present on the Floral Clock. Output Output one integer β€” the sum of beauty over all possible arrangements of flowers, modulo 998 244 353. Examples Input 3 Output 24 Input 4 Output 4 Input 7 Output 1316 Input 15 Output 3436404 Note With n = 3, the following six arrangements each have a beauty of 2 Γ— 2 = 4. <image> While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric. <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. ###Task: You have to write a function **pattern** which returns the following Pattern(See Examples) upto n rows, where n is parameter. ####Rules/Note: * If the Argument is 0 or a Negative Integer then it should return "" i.e. empty string. * The length of each line = (2n-1). * Range of n is (-∞,100] ###Examples: pattern(5): 12345 12345 12345 12345 12345 pattern(10): 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 pattern(15): 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 123456789012345 pattern(20): 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 telecomunications we use information coding to detect and prevent errors while sending data. A parity bit is a bit added to a string of binary code that indicates whether the number of 1-bits in the string is even or odd. Parity bits are used as the simplest form of error detecting code, and can detect a 1 bit error. In this case we are using even parity: the parity bit is set to `0` if the number of `1`-bits is even, and is set to `1` if odd. We are using them for the transfer of ASCII characters in binary (7-bit strings): the parity is added to the end of the 7-bit string, forming the 8th bit. In this Kata you are to test for 1-bit errors and return a new string consisting of all of the correct ASCII caracters **in 7 bit format** (removing the parity bit), or `"error"` in place of ASCII characters in which errors were detected. For more information on parity bits: https://en.wikipedia.org/wiki/Parity_bit ## Examples Correct 7 bit string with an even parity bit as the 8th bit: ``` "01011001" <-- The "1" on the right is the parity bit. ``` In this example, there are three 1-bits. Three is an odd number, and the parity bit is set to `1`. No errors are detected, so return `"0101100"` (7 bits). Example of a string of ASCII characters: ``` "01011001 01101110 01100000 01010110 10001111 01100011" ``` This should return: ``` "0101100 error 0110000 0101011 error 0110001" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him. The area looks like a strip of cells 1 Γ— n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” length of the strip. Next line contains a string of length n which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers d_{i} (1 ≀ d_{i} ≀ 10^9)Β β€” the length of the jump from the i-th cell. -----Output----- Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes). -----Examples----- Input 2 >< 1 2 Output FINITE Input 3 >>< 2 1 1 Output INFINITE -----Note----- In the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip. Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form : ``` "(p1**n1)(p2**n2)...(pk**nk)" ``` where ```a ** b``` means ```a``` to the power of ```b``` with the p(i) in increasing order and n(i) empty if n(i) is 1. ``` Example: n = 86240 should return "(2**5)(5)(7**2)(11)" ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. problem AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S β†’ S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S β†’ S $ satisfying $ g (f (x)) = h (f (x)) $ are $ g (x). ) = Determine if h (x) $ is satisfied, and if not, configure one counterexample. Example Input 5 1 2 3 4 5 3 4 2 5 1 Output Yes Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a string containing a list of integers separated by commas, write the function string_to_int_list(s) that takes said string and returns a new list containing all integers present in the string, preserving the order. For example, give the string "-1,2,3,4,5", the function string_to_int_list() should return [-1,2,3,4,5] Please note that there can be one or more consecutive commas whithout numbers, like so: "-1,-2,,,,,,3,4,5,,6" Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer β€” the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bandits appeared in the city! One of them is trying to catch as many citizens as he can. The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square. After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square. At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square. The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught? -----Input----- The first line contains a single integer $n$Β β€” the number of squares in the city ($2 \le n \le 2\cdot10^5$). The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$). The third line contains $n$ integers $a_1, a_2, \dots, a_n$Β β€” the number of citizens on each square initially ($0 \le a_i \le 10^9$). -----Output----- Print a single integerΒ β€” the number of citizens the bandit will catch if both sides act optimally. -----Examples----- Input 3 1 1 3 1 2 Output 3 Input 3 1 1 3 1 3 Output 4 -----Note----- In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each. In the second example no matter how citizens act the bandit can catch at least $4$ citizens. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There are n student groups at the university. During the study day, each group can take no more than 7 classes. Seven time slots numbered from 1 to 7 are allocated for the classes. The schedule on Monday is known for each group, i. e. time slots when group will have classes are known. Your task is to determine the minimum number of rooms needed to hold classes for all groups on Monday. Note that one room can hold at most one group class in a single time slot. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of groups. Each of the following n lines contains a sequence consisting of 7 zeroes and ones β€” the schedule of classes on Monday for a group. If the symbol in a position equals to 1 then the group has class in the corresponding time slot. In the other case, the group has no class in the corresponding time slot. -----Output----- Print minimum number of rooms needed to hold all groups classes on Monday. -----Examples----- Input 2 0101010 1010101 Output 1 Input 3 0101011 0011001 0110111 Output 3 -----Note----- In the first example one room is enough. It will be occupied in each of the seven time slot by the first group or by the second group. In the second example three rooms is enough, because in the seventh time slot all three groups have classes. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn't give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor's handwriting to make a counterfeit certificate of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor's signature is impossible to forge. Or is it? For simplicity, the signature is represented as an $n\times m$ grid, where every cell is either filled with ink or empty. Andrey's pen can fill a $3\times3$ square without its central cell if it is completely contained inside the grid, as shown below. xxx x.x xxx Determine whether is it possible to forge the signature on an empty $n\times m$ grid. -----Input----- The first line of input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$). Then $n$ lines follow, each contains $m$ characters. Each of the characters is either '.', representing an empty cell, or '#', representing an ink filled cell. -----Output----- If Andrey can forge the signature, output "YES". Otherwise output "NO". You can print each letter in any case (upper or lower). -----Examples----- Input 3 3 ### #.# ### Output YES Input 3 3 ### ### ### Output NO Input 4 3 ### ### ### ### Output YES Input 5 7 ....... .#####. .#.#.#. .#####. ....... Output YES -----Note----- In the first sample Andrey can paint the border of the square with the center in $(2, 2)$. In the second sample the signature is impossible to forge. In the third sample Andrey can paint the borders of the squares with the centers in $(2, 2)$ and $(3, 2)$: we have a clear paper: ... ... ... ... use the pen with center at $(2, 2)$. ### #.# ### ... use the pen with center at $(3, 2)$. ### ### ### ### In the fourth sample Andrey can paint the borders of the squares with the centers in $(3, 3)$ and $(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. In Omkar's last class of math, he learned about the least common multiple, or $LCM$. $LCM(a, b)$ is the smallest positive integer $x$ which is divisible by both $a$ and $b$. Omkar, having a laudably curious mind, immediately thought of a problem involving the $LCM$ operation: given an integer $n$, find positive integers $a$ and $b$ such that $a + b = n$ and $LCM(a, b)$ is the minimum value possible. Can you help Omkar solve his ludicrously challenging math problem? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10$). Description of the test cases follows. Each test case consists of a single integer $n$ ($2 \leq n \leq 10^{9}$). -----Output----- For each test case, output two positive integers $a$ and $b$, such that $a + b = n$ and $LCM(a, b)$ is the minimum possible. -----Example----- Input 3 4 6 9 Output 2 2 3 3 3 6 -----Note----- For the first test case, the numbers we can choose are $1, 3$ or $2, 2$. $LCM(1, 3) = 3$ and $LCM(2, 2) = 2$, so we output $2 \ 2$. For the second test case, the numbers we can choose are $1, 5$, $2, 4$, or $3, 3$. $LCM(1, 5) = 5$, $LCM(2, 4) = 4$, and $LCM(3, 3) = 3$, so we output $3 \ 3$. For the third test case, $LCM(3, 6) = 6$. It can be shown that there are no other pairs of numbers which sum to $9$ that have a lower $LCM$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem! You are given $n$ nonnegative integers $a_1, a_2, \dots, a_n$ arranged in a circle, where $n$ must be odd (ie. $n-1$ is divisible by $2$). Formally, for all $i$ such that $2 \leq i \leq n$, the elements $a_{i - 1}$ and $a_i$ are considered to be adjacent, and $a_n$ and $a_1$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value. Help Danny find the maximum possible circular value after some sequences of operations. -----Input----- The first line contains one odd integer $n$ ($1 \leq n < 2 \cdot 10^5$, $n$ is odd) Β β€” the initial size of the circle. The second line contains $n$ integers $a_{1},a_{2},\dots,a_{n}$ ($0 \leq a_{i} \leq 10^9$) Β β€” the initial numbers in the circle. -----Output----- Output the maximum possible circular value after applying some sequence of operations to the given circle. -----Examples----- Input 3 7 10 2 Output 17 Input 1 4 Output 4 -----Note----- For the first test case, here's how a circular value of $17$ is obtained: Pick the number at index $3$. The sum of adjacent elements equals $17$. Delete $7$ and $10$ from the circle and replace $2$ with $17$. Note that the answer may not fit in a $32$-bit integer. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, Tokitsukaze found an interesting game. Tokitsukaze had $n$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $m$ ($1 \le m \le n$) special items of them. These $n$ items are marked with indices from $1$ to $n$. In the beginning, the item with index $i$ is placed on the $i$-th position. Items are divided into several pages orderly, such that each page contains exactly $k$ positions and the last positions on the last page may be left empty. Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded. [Image] Consider the first example from the statement: $n=10$, $m=4$, $k=5$, $p=[3, 5, 7, 10]$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $3$ and $5$. After, the first page remains to be special. It contains $[1, 2, 4, 6, 7]$, Tokitsukaze discards the special item with index $7$. After, the second page is special (since it is the first page containing a special item). It contains $[9, 10]$, Tokitsukaze discards the special item with index $10$. Tokitsukaze wants to know the number of operations she would do in total. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^{18}$, $1 \le m \le 10^5$, $1 \le m, k \le n$)Β β€” the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $m$ distinct integers $p_1, p_2, \ldots, p_m$ ($1 \le p_1 < p_2 < \ldots < p_m \le n$)Β β€” the indices of special items which should be discarded. -----Output----- Print a single integerΒ β€” the number of operations that Tokitsukaze would do in total. -----Examples----- Input 10 4 5 3 5 7 10 Output 3 Input 13 4 5 7 8 9 10 Output 1 -----Note----- For the first example: In the first operation, Tokitsukaze would focus on the first page $[1, 2, 3, 4, 5]$ and discard items with indices $3$ and $5$; In the second operation, Tokitsukaze would focus on the first page $[1, 2, 4, 6, 7]$ and discard item with index $7$; In the third operation, Tokitsukaze would focus on the second page $[9, 10]$ and discard item with index $10$. For the second example, Tokitsukaze would focus on the second page $[6, 7, 8, 9, 10]$ and discard all special items at once. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 __moreZeros__ function which will __receive a string__ for input, and __return an array__ (or null terminated string in C) containing only the characters from that string whose __binary representation of its ASCII value__ consists of _more zeros than ones_. You should __remove any duplicate characters__, keeping the __first occurence__ of any such duplicates, so they are in the __same order__ in the final array as they first appeared in the input string. Examples All input will be valid strings of length > 0. Leading zeros in binary should not be counted. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 a secret agent from the Intelligence Center of Peacemaking Committee. You've just sneaked into a secret laboratory of an evil company, Automated Crime Machines. Your mission is to get a confidential document kept in the laboratory. To reach the document, you need to unlock the door to the safe where it is kept. You have to unlock the door in a correct way and with a great care; otherwise an alarm would ring and you would be caught by the secret police. The lock has a circular dial with N lights around it and a hand pointing to one of them. The lock also has M buttons to control the hand. Each button has a number Li printed on it. Initially, all the lights around the dial are turned off. When the i-th button is pressed, the hand revolves clockwise by Li lights, and the pointed light is turned on. You are allowed to press the buttons exactly N times. The lock opens only when you make all the lights turned on. <image> For example, in the case with N = 6, M = 2, L1 = 2 and L2 = 5, you can unlock the door by pressing buttons 2, 2, 2, 5, 2 and 2 in this order. There are a number of doors in the laboratory, and some of them don’t seem to be unlockable. Figure out which lock can be opened, given the values N, M, and Li's. Input The input starts with a line containing two integers, which represent N and M respectively. M lines follow, each of which contains an integer representing Li. It is guaranteed that 1 ≀ N ≀ 109, 1 ≀ M ≀ 105, and 1 ≀ Li ≀ N for each i = 1, 2, ... N. Output Output a line with "Yes" (without quotes) if the lock can be opened, and "No" otherwise. Examples Input 6 2 2 5 Output Yes Input 3 1 1 Output Yes Input 4 2 2 4 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. User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below. 1. At first, the score is 0. 2. For each block of contiguous "o"s with length x the score increases by x2. 3. For each block of contiguous "x"s with length y the score decreases by y2. For example, if a = 6, b = 3 and ainta have arranged the cards in the order, that is described by string "ooxoooxxo", the score of the deck equals 22 - 12 + 32 - 22 + 12 = 9. That is because the deck has 5 blocks in total: "oo", "x", "ooo", "xx", "o". User ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards. Input The first line contains two space-separated integers a and b (0 ≀ a, b ≀ 105; a + b β‰₯ 1) β€” the number of "o" cards and the number of "x" cards. Output In the first line print a single integer v β€” the maximum score that ainta can obtain. In the second line print a + b characters describing the deck. If the k-th card of the deck contains "o", the k-th character must be "o". If the k-th card of the deck contains "x", the k-th character must be "x". The number of "o" characters must be equal to a, and the number of "x " characters must be equal to b. If there are many ways to maximize v, print any. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 2 3 Output -1 xoxox Input 4 0 Output 16 oooo Input 0 4 Output -16 xxxx Read the inputs from stdin solve the problem and write the answer to stdout (do 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 loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 recent All-Berland Olympiad in Informatics featured n participants with each scoring a certain amount of points. As the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria: At least one participant should get a diploma. None of those with score equal to zero should get awarded. When someone is awarded, all participants with score not less than his score should also be awarded. Determine the number of ways to choose a subset of participants that will receive the diplomas. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 100)Β β€” the number of participants. The next line contains a sequence of n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 600)Β β€” participants' scores. It's guaranteed that at least one participant has non-zero score. -----Output----- Print a single integerΒ β€” the desired number of ways. -----Examples----- Input 4 1 3 3 2 Output 3 Input 3 1 1 1 Output 1 Input 4 42 0 0 42 Output 1 -----Note----- There are three ways to choose a subset in sample case one. Only participants with 3 points will get diplomas. Participants with 2 or 3 points will get diplomas. Everyone will get a diploma! The only option in sample case two is to award everyone. Note that in sample case three participants with zero scores cannot get anything. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. -----Input----- The first line of the input contains a single integer N (3 ≀ N ≀ 10). The following N lines each contain N space-separated integers. jth integer in ith line a_{ij} is the length of the edge that connects vertices i and j. a_{ij} = a_{ji}, a_{ii} = 0, 1 ≀ a_{ij} ≀ 100 for i β‰  j. -----Output----- Output the maximum length of the shortest path between any pair of vertices in the graph. -----Examples----- Input 3 0 1 1 1 0 4 1 4 0 Output 2 Input 4 0 1 2 3 1 0 4 5 2 4 0 6 3 5 6 0 Output 5 -----Note----- You're running short of keywords, so you can't use some of them:define do for foreach while repeat until if then else elif elsif elseif case switch Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp! Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years? According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321. Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10). Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful. Input The first line contains an integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of one line, which contains a positive integer n (1 ≀ n ≀ 10^9) β€” how many years Polycarp has turned. Output Print t integers β€” the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive. Example Input 6 18 1 9 100500 33 1000000000 Output 10 1 9 45 12 81 Note In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. On a clear night, a boy, Campanella looked up at the sky, there were many stars which have different colors such as red, yellow, green, blue, purple, etc. He was watching the stars and just lost track of time. Presently, he wondered, | <image> ---|--- "There are many stars in the space. What color is the universe if I look up it from outside?" Until he found the answer to this question, he couldn't sleep. After a moment's thought, he proposed a hypothesis, "When I observe the stars in the sky, if more than half of the stars have the same color, that is the color of the universe." He has collected data of stars in the universe after consistently observing them. However, when he try to count the number of star, he was in bed sick with a cold. You decided to help him by developing a program to identify the color of the universe. You are given an array A. Let |A| be the number of element in A, and let Nm be the number of m in A. For example, if A = {3, 1, 2, 3, 3, 1, 5, 3}, |A| = 8, N3 = 4, N1 = 2, N5 = 1. Your program have to find m such that: Nm > (|A| / 2 ) If there is no such m, you also have to report the fact. There is no such m for the above example, but for a array A = {5, 2, 5, 3, 4, 5, 5}, 5 is the answer. Input There are several test cases. For each test case, in the first line |A| is given. In the next line, there will be |A| integers. The integers are less than 231 and |A| < 1,000,000. The input terminate with a line which contains single 0. Output For each test case, output m in a line. If there is no answer, output "NO COLOR" in a line. Example Input 8 3 1 2 3 3 1 5 3 7 5 2 5 3 4 5 5 0 Output NO COLOR 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. Valera's finally decided to go on holiday! He packed up and headed for a ski resort. Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain. Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object. Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v_1, v_2, ..., v_{k} (k β‰₯ 1) and meet the following conditions: Objects with numbers v_1, v_2, ..., v_{k} - 1 are mountains and the object with number v_{k} is the hotel. For any integer i (1 ≀ i < k), there is exactly one ski track leading from object v_{i}. This track goes to object v_{i} + 1. The path contains as many objects as possible (k is maximal). Help Valera. Find such path that meets all the criteria of our hero! -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of objects. The second line contains n space-separated integers type_1, type_2, ..., type_{n} β€” the types of the objects. If type_{i} equals zero, then the i-th object is the mountain. If type_{i} equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel. The third line of the input contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ n) β€” the description of the ski tracks. If number a_{i} equals zero, then there is no such object v, that has a ski track built from v to i. If number a_{i} doesn't equal zero, that means that there is a track built from object a_{i} to object i. -----Output----- In the first line print k β€” the maximum possible path length for Valera. In the second line print k integers v_1, v_2, ..., v_{k} β€” the path. If there are multiple solutions, you can print any of them. -----Examples----- Input 5 0 0 0 0 1 0 1 2 3 4 Output 5 1 2 3 4 5 Input 5 0 0 1 0 1 0 1 2 2 4 Output 2 4 5 Input 4 1 0 0 0 2 3 4 2 Output 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. Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct. Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list. To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β€” when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way). What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way. Your program has to answer $t$ different test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then the test cases follow, each represented by three lines. The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β€” the number of presents in the stack and the number of presents Santa wants to send, respectively. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β€” the order of presents in the stack. The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β€” the ordered list of presents Santa has to send. The sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack. -----Example----- Input 2 3 3 3 1 2 3 2 1 7 2 2 1 7 3 4 5 6 3 1 Output 5 8 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Today is the ticket release date for Aizu Entertainment's recommended idol group "Akabeko & Koboushi". There are four types of tickets: S seat 6000 yen A seat 4000 yen B seat 3000 yen C seat 2000 yen You, the sales manager, are excitedly waiting for the launch. Finally on sale. It's selling very well! Shortly after the launch, I received a table summarizing the orders up to that point. Each row of the table shows the type and number of tickets sold so far. However, the ticket types do not always appear in the order of S, A, B, C. Create a program to find the sales amount for each row in this table. input Input data is given in the following format. t1 n1 t2 n2 t3 n3 t4 n4 The input consists of 4 lines. Line i is given the integer ti (1 ≀ ti ≀ 4) for the ticket type and the integer ni (0 ≀ ni ≀ 10000) for the number of tickets. The integers 1, 2, 3, and 4 representing the ticket types represent S seats, A seats, B seats, and C seats, respectively. Numbers from 1 to 4 always appear once as values ​​for t1, t2, t3, and t4, but they are not always given in the order of 1, 2, 3, 4. output Output the sales amount for each line. Example Input 3 10 1 4 4 1 2 5 Output 30000 24000 2000 20000 Read the inputs from stdin solve the problem and write the answer to stdout (do 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 speed skating badge test, grades are awarded when the time specified for two distances is exceeded. For example, to reach Class A, 500 M requires less than 40.0 seconds and 1000 M requires less than 1 minute and 23 seconds. Create a program that takes the time recorded in the speed skating competitions (500 M and 1000 M) as input and outputs what grade it corresponds to in the speed skating badge test. The table below shows the default badge test times for the 500 M and 1000 M. If it is less than class E, output NA. | 500 M | 1000 M --- | --- | --- AAA class | 35 seconds 50 | 1 minute 11 seconds 00 AA class | 37 seconds 50 | 1 minute 17 seconds 00 Class A | 40 seconds 00 | 1 minute 23 seconds 00 Class B | 43 seconds 00 | 1 minute 29 seconds 00 Class C | 50 seconds 00 | 1 minute 45 seconds 00 Class D | 55 seconds 00 | 1 minute 56 seconds 00 Class E | 1 minute 10 seconds 00 | 2 minutes 28 seconds 00 Input Given multiple datasets. For each dataset, real numbers t1, t2 (8.0 ≀ t1, t2 ≀ 360.0) representing 500 M time and 1000 M time, respectively, are given, separated by blanks. t1 and t2 are given in seconds as real numbers, including up to two digits after the decimal point. The number of datasets does not exceed 100. Output For each data set, output the judgment result AAA ~ E or NA on one line. Example Input 40.0 70.0 72.5 140.51 Output B NA Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Imp is in a magic forest, where xorangles grow (wut?) [Image] A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest. Formally, for a given integer n you have to find the number of such triples (a, b, c), that: 1 ≀ a ≀ b ≀ c ≀ n; $a \oplus b \oplus c = 0$, where $x \oplus y$ denotes the bitwise xor of integers x and y. (a, b, c) form a non-degenerate (with strictly positive area) triangle. -----Input----- The only line contains a single integer n (1 ≀ n ≀ 2500). -----Output----- Print the number of xorangles of order n. -----Examples----- Input 6 Output 1 Input 10 Output 2 -----Note----- The only xorangle in the first sample is (3, 5, 6). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi, who works at DISCO, is standing before an iron bar. The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters. Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length. However, this may not be possible as is, so he will do the following operations some number of times before he does the cut: * Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan). * Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen. Find the minimum amount of money needed before cutting the bar into two parts with the same length. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 2020202020 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 A_3 ... A_N Output Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length. Examples Input 3 2 4 3 Output 3 Input 12 100 104 102 105 103 103 101 105 104 102 104 101 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^{18})$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 -----Note----- Answers on queries from the first example are described in the problem statement. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a program of the square picking method, which is one of the classical random number generation methods. The square harvesting method was proposed by von Neumann in the mid-1940s. In the square picking method, when the number of digits of the generated random number is n, the square of the initial value s is calculated, and the value is regarded as a 2n digit number (the number of squared digits as shown in the example below). If is not enough, make up for 0.) Let the n numbers in the center be the first random number. Then square this random number and take the n numbers in the center in the same way to get the next random number. For example, if 123 is the initial value 1232 = 00015129 β†’ 0151 1512 = 00022801 β†’ 0228 2282 = 00051984 β†’ 0519 5192 = 00269361 β†’ 2693 26932 = 07252249 β†’ 2522 It will be like. Use this method to create a program that takes the initial value s (a positive integer less than 10000) as an input and generates and outputs 10 random numbers when n = 4. Input Given multiple datasets. The first line gives the number of datasets d (d ≀ 10). For each dataset, one row is given the initial value s (integer, 1 ≀ s <10000). Output For each dataset Case x: (x is a dataset number starting with 1) The first generated random number (integer) The second generated random number (integer) : : 10th generated random number (integer) Please output. Example Input 2 123 567 Output Case 1: 151 228 519 2693 2522 3604 9888 7725 6756 6435 Case 2: 3214 3297 8702 7248 5335 4622 3628 1623 6341 2082 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Inaka has a disc, the circumference of which is n units. The circumference is equally divided by n points numbered clockwise from 1 to n, such that points i and i + 1 (1 ≀ i < n) are adjacent, and so are points n and 1. There are m straight segments on the disc, the endpoints of which are all among the aforementioned n points. Inaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer k (1 ≀ k < n), such that if all segments are rotated clockwise around the center of the circle by k units, the new image will be the same as the original one. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 200 000) β€” the number of points and the number of segments, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n, a_i β‰  b_i) that describe a segment connecting points a_i and b_i. It is guaranteed that no segments coincide. Output Output one line β€” "Yes" if the image is rotationally symmetrical, and "No" otherwise (both excluding quotation marks). You can output each letter in any case (upper or lower). Examples Input 12 6 1 3 3 7 5 7 7 11 9 11 11 3 Output Yes Input 9 6 4 5 5 6 7 8 8 9 1 2 2 3 Output Yes Input 10 3 1 2 3 2 7 2 Output No Input 10 2 1 6 2 7 Output Yes Note The first two examples are illustrated below. Both images become the same as their respective original ones after a clockwise rotation of 120 degrees around the center. <image> Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned. If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible. Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up. Input The first line contains a single integer n (1 ≀ n ≀ 50) β€” the number of tasks. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 108), where ai represents the amount of power required for the i-th task. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 100), where bi is the number of processors that i-th task will utilize. Output Print a single integer value β€” the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up. Examples Input 6 8 10 9 9 8 10 1 1 1 1 1 1 Output 9000 Input 6 8 10 9 9 8 10 1 10 5 5 1 10 Output 1160 Note In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9. In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vova has taken his summer practice this year and now he should write a report on how it went. Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly $n$ pages and the $i$-th page will include $x_i$ tables and $y_i$ formulas. The pages are numbered from $1$ to $n$. Vova fills the pages one after another, he can't go filling page $i + 1$ before finishing page $i$ and he can't skip pages. However, if he draws strictly more than $k$ tables in a row or writes strictly more than $k$ formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page. Note that the count doesn't reset on the start of the new page. For example, if the page ends with $3$ tables and the next page starts with $5$ tables, then it's counted as $8$ tables in a row. Help Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 3 \cdot 10^5$, $1 \le k \le 10^6$). The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^6$) β€” the number of tables on the $i$-th page. The third line contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^6$) β€” the number of formulas on the $i$-th page. -----Output----- Print "YES" if Vova can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row. Otherwise print "NO". -----Examples----- Input 2 2 5 5 2 2 Output YES Input 2 2 5 6 2 2 Output NO Input 4 1 4 1 10 1 3 2 10 1 Output YES -----Note----- In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page $1$: "TTFTTFT" page $2$: "TFTTFTT" That way all blocks of tables have length $2$. In the second example there is no way to fit everything in such a way that there are no more than $2$ tables in a row and $2$ formulas in a row. Read the inputs from stdin solve the problem and write the answer to stdout (do 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, your task is to identify the pattern underlying a sequence of numbers. For example, if the sequence is [1, 2, 3, 4, 5], then the pattern is [1], since each number in the sequence is equal to the number preceding it, plus 1. See the test cases for more examples. A few more rules : pattern may contain negative numbers. sequence will always be made of a whole number of repetitions of the pattern. Your answer must correspond to the shortest form of the pattern, e.g. if the pattern is [1], then [1, 1, 1, 1] will not be considered a correct answer. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor. Doctors have a strange working schedule. The doctor i goes to work on the s_{i}-th day and works every d_{i} day. So, he works on days s_{i}, s_{i} + d_{i}, s_{i} + 2d_{i}, .... The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors? -----Input----- First line contains an integer n β€” number of doctors (1 ≀ n ≀ 1000). Next n lines contain two numbers s_{i} and d_{i} (1 ≀ s_{i}, d_{i} ≀ 1000). -----Output----- Output a single integer β€” the minimum day at which Borya can visit the last doctor. -----Examples----- Input 3 2 2 1 2 2 2 Output 4 Input 2 10 1 6 5 Output 11 -----Note----- In the first sample case, Borya can visit all doctors on days 2, 3 and 4. In the second sample case, Borya can visit all doctors on days 10 and 11. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly a_{i} kilograms of meat. [Image] There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for p_{i} dollars per kilogram. Malek knows all numbers a_1, ..., a_{n} and p_1, ..., p_{n}. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future. Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days. -----Input----- The first line of input contains integer n (1 ≀ n ≀ 10^5), the number of days. In the next n lines, i-th line contains two integers a_{i} and p_{i} (1 ≀ a_{i}, p_{i} ≀ 100), the amount of meat Duff needs and the cost of meat in that day. -----Output----- Print the minimum money needed to keep Duff happy for n days, in one line. -----Examples----- Input 3 1 3 2 2 3 1 Output 10 Input 3 1 3 2 1 3 2 Output 8 -----Note----- In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day. In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Converting a normal (12-hour) time like "8:30 am" or "8:30 pm" to 24-hour time (like "0830" or "2030") sounds easy enough, right? Well, let's see if you can do it! You will have to define a function named "to24hourtime", and you will be given an hour (always in the range of 1 to 12, inclusive), a minute (always in the range of 0 to 59, inclusive), and a period (either "am" or "pm") as input. Your task is to return a four-digit string that encodes that time in 24-hour time. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." β€” The Night's Watch oath. With that begins the watch of Jon Snow. He is assigned the task to support the stewards. This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him. Can you find how many stewards will Jon support? -----Input----- First line consists of a single integer n (1 ≀ n ≀ 10^5) β€” the number of stewards with Jon Snow. Second line consists of n space separated integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9) representing the values assigned to the stewards. -----Output----- Output a single integer representing the number of stewards which Jon will feed. -----Examples----- Input 2 1 5 Output 0 Input 3 1 2 5 Output 1 -----Note----- In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5. In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) Γ— 1000 = 8000 yen. Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ . Constraints * $2 \leq n \leq 200,000$ * $1 \leq R_t \leq 10^9$ Input The first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order. Output Print the maximum value in a line. Examples Input 6 5 3 1 3 4 3 Output 3 Input 3 4 3 2 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Remember the game 2048? The main part of this game is merging identical tiles in a row. * Implement a function that models the process of merging all of the tile values in a single row. * This function takes the array line as a parameter and returns a new array with the tile values from line slid towards the front of the array (index 0) and merged. * A given tile can only merge once. * Empty grid squares are represented as zeros. * Your function should work on arrays containing arbitrary number of elements. ## Examples ``` merge([2,0,2,2]) --> [4,2,0,0] ``` Another example with repeated merges: ``` merge([4,4,8,16]) --> [8,8,16,0] merge([8,8,16,0]) --> [16,16,0,0] merge([16,16,0,0]) --> [32,0,0,0] ``` Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In JavaScript, ```if..else``` is the most basic condition statement, it consists of three parts:```condition, statement1, statement2```, like this: ```python if condition: statementa else: statementb ``` It means that if the condition is true, then execute the statementa, otherwise execute the statementb.If the statementa or statementb more than one line, you need to add ```{``` and ```}``` at the head and tail of statement in JS, to keep the same indentation on Python and to put a `end` in Ruby where it indeed ends. An example, if we want to judge whether a number is odd or even, we can write code like this: ```python def odd_even(n): if n%2: return "odd number" else: return "even number" ``` If there is more than one condition to judge, we can use the compound if...else statement. an example: ```python def old_young(age): if age<16: return "children" elif (age<50): return "young man" #use "else if" if needed else: return "old man" ``` This function returns a different value depending on the parameter age. Looks very complicated? Well, JS and Ruby also support the ```ternary operator``` and Python has something similar too: ```python statementa if condition else statementb ``` Condition and statement separated by "?", different statement separated by ":" in both Ruby and JS; in Python you put the condition in the middle of two alternatives. The two examples above can be simplified with ternary operator: ```python def odd_even(n): return "odd number" if n%2 else "even number" def old_young(age): return "children" if age<16 else "young man" if age<50 else "old man" ``` ## Task: Complete function `saleHotdogs`/`SaleHotDogs`/`sale_hotdogs`, function accept 1 parameters:`n`, n is the number of customers to buy hotdogs, different numbers have different prices (refer to the following table), return a number that the customer need to pay how much money. ``` +---------------+-------------+ | numbers n | price(cents)| +---------------+-------------+ |n<5 | 100 | +---------------+-------------+ |n>=5 and n<10 | 95 | +---------------+-------------+ |n>=10 | 90 | +---------------+-------------+ ``` You can use if..else or ternary operator to complete it. When you have finished the work, click "Run Tests" to see if your code is working properly. In the end, click "Submit" to submit your code pass this kata. ## [Series](http://github.com/myjinxin2015/Katas-list-of-Training-JS-series): ( ↑↑↑ Click the link above can get my newest kata list, Please add it to your favorites) - [#1: create your first JS function helloWorld](http://www.codewars.com/kata/571ec274b1c8d4a61c0000c8) - [#2: Basic data types--Number](http://www.codewars.com/kata/571edd157e8954bab500032d) - [#3: Basic data types--String](http://www.codewars.com/kata/571edea4b625edcb51000d8e) - [#4: Basic data types--Array](http://www.codewars.com/kata/571effabb625ed9b0600107a) - [#5: Basic data types--Object](http://www.codewars.com/kata/571f1eb77e8954a812000837) - [#6: Basic data types--Boolean and conditional statements if..else](http://www.codewars.com/kata/571f832f07363d295d001ba8) - [#7: if..else and ternary operator](http://www.codewars.com/kata/57202aefe8d6c514300001fd) - [#8: Conditional statement--switch](http://www.codewars.com/kata/572059afc2f4612825000d8a) - [#9: loop statement --while and do..while](http://www.codewars.com/kata/57216d4bcdd71175d6000560) - [#10: loop statement --for](http://www.codewars.com/kata/5721a78c283129e416000999) - [#11: loop statement --break,continue](http://www.codewars.com/kata/5721c189cdd71194c1000b9b) - [#12: loop statement --for..in and for..of](http://www.codewars.com/kata/5722b3f0bd5583cf44001000) - [#13: Number object and its properties](http://www.codewars.com/kata/5722fd3ab7162a3a4500031f) - [#14: Methods of Number object--toString() and toLocaleString()](http://www.codewars.com/kata/57238ceaef9008adc7000603) - [#15: Methods of Number object--toFixed(), toExponential() and toPrecision()](http://www.codewars.com/kata/57256064856584bc47000611) - [#16: Methods of String object--slice(), substring() and substr()](http://www.codewars.com/kata/57274562c8dcebe77e001012) - [#17: Methods of String object--indexOf(), lastIndexOf() and search()](http://www.codewars.com/kata/57277a31e5e51450a4000010) - [#18: Methods of String object--concat() split() and its good friend join()](http://www.codewars.com/kata/57280481e8118511f7000ffa) - [#19: Methods of String object--toUpperCase() toLowerCase() and replace()](http://www.codewars.com/kata/5728203b7fc662a4c4000ef3) - [#20: Methods of String object--charAt() charCodeAt() and fromCharCode()](http://www.codewars.com/kata/57284d23e81185ae6200162a) - [#21: Methods of String object--trim() and the string template](http://www.codewars.com/kata/5729b103dd8bac11a900119e) - [#22: Unlock new skills--Arrow function,spread operator and deconstruction](http://www.codewars.com/kata/572ab0cfa3af384df7000ff8) - [#23: methods of arrayObject---push(), pop(), shift() and unshift()](http://www.codewars.com/kata/572af273a3af3836660014a1) - [#24: methods of arrayObject---splice() and slice()](http://www.codewars.com/kata/572cb264362806af46000793) - [#25: methods of arrayObject---reverse() and sort()](http://www.codewars.com/kata/572df796914b5ba27c000c90) - [#26: methods of arrayObject---map()](http://www.codewars.com/kata/572fdeb4380bb703fc00002c) - [#27: methods of arrayObject---filter()](http://www.codewars.com/kata/573023c81add650b84000429) - [#28: methods of arrayObject---every() and some()](http://www.codewars.com/kata/57308546bd9f0987c2000d07) - [#29: methods of arrayObject---concat() and join()](http://www.codewars.com/kata/5731861d05d14d6f50000626) - [#30: methods of arrayObject---reduce() and reduceRight()](http://www.codewars.com/kata/573156709a231dcec9000ee8) - [#31: methods of arrayObject---isArray() indexOf() and toString()](http://www.codewars.com/kata/5732b0351eb838d03300101d) - [#32: methods of Math---round() ceil() and floor()](http://www.codewars.com/kata/5732d3c9791aafb0e4001236) - [#33: methods of Math---max() min() and abs()](http://www.codewars.com/kata/5733d6c2d780e20173000baa) - [#34: methods of Math---pow() sqrt() and cbrt()](http://www.codewars.com/kata/5733f948d780e27df6000e33) - [#35: methods of Math---log() and its family](http://www.codewars.com/kata/57353de879ccaeb9f8000564) - [#36: methods of Math---kata author's lover:random()](http://www.codewars.com/kata/5735956413c2054a680009ec) - [#37: Unlock new weapon---RegExp Object](http://www.codewars.com/kata/5735e39313c205fe39001173) - [#38: Regular Expression--"^","$", "." and test()](http://www.codewars.com/kata/573975d3ac3eec695b0013e0) - [#39: Regular Expression--"?", "*", "+" and "{}"](http://www.codewars.com/kata/573bca07dffc1aa693000139) - [#40: Regular Expression--"|", "[]" and "()"](http://www.codewars.com/kata/573d11c48b97c0ad970002d4) - [#41: Regular Expression--"\"](http://www.codewars.com/kata/573e6831e3201f6a9b000971) - [#42: Regular Expression--(?:), (?=) and (?!)](http://www.codewars.com/kata/573fb9223f9793e485000453) Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In this problem, we only consider strings consisting of lowercase English letters. Strings s and t are said to be isomorphic when the following conditions are satisfied: * |s| = |t| holds. * For every pair i, j, one of the following holds: * s_i = s_j and t_i = t_j. * s_i \neq s_j and t_i \neq t_j. For example, `abcac` and `zyxzx` are isomorphic, while `abcac` and `ppppp` are not. A string s is said to be in normal form when the following condition is satisfied: * For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison. For example, `abcac` is in normal form, but `zyxzx` is not since it is isomorphic to `abcac`, which is lexicographically smaller than `zyxzx`. You are given an integer N. Print all strings of length N that are in normal form, in lexicographically ascending order. Constraints * 1 \leq N \leq 10 * All values in input are integers. Input Input is given from Standard Input in the following format: N Output Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order. Output should be in the following format: w_1 : w_K Output Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order. Output should be in the following format: w_1 : w_K Examples Input 1 Output a Input 2 Output aa ab Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. Input The first line of the input contains a single integer n (1 ≀ n ≀ 3000) β€” the length of the array. Next line contains n integer ai (1 ≀ ai ≀ 109). Output Print the minimum number of operation required to make the array strictly increasing. Examples Input 7 2 1 5 11 5 9 11 Output 9 Input 5 5 4 3 2 1 Output 12 Note In the first sample, the array is going to look as follows: 2 3 5 6 7 9 11 |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9 And for the second sample: 1 2 3 4 5 |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 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. Consider the following arithmetic progression with n terms: * x, x + d, x + 2d, \ldots, x + (n-1)d What is the product of all terms in this sequence? Compute the answer modulo 1\ 000\ 003. You are given Q queries of this form. In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i. Constraints * 1 \leq Q \leq 10^5 * 0 \leq x_i, d_i \leq 1\ 000\ 002 * 1 \leq n_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: Q x_1 d_1 n_1 : x_Q d_Q n_Q Output Print Q lines. In the i-th line, print the answer for the i-th query. Example Input 2 7 2 4 12345 67890 2019 Output 9009 916936 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Rabbits and cats are competing. The rules are as follows. First, each of the two animals wrote n2 integers on a piece of paper in a square with n rows and n columns, and drew one card at a time. Shuffle two cards and draw them one by one alternately. Each time a card is drawn, the two will mark it if the same number as the card is written on their paper. The winning condition is that the number of "a set of n numbers with a mark and is in a straight line" is equal to or greater than the number of playing cards drawn at the beginning. Answer which of the rabbit and the cat wins up to the mth card given. However, the victory or defeat is when only one of the two cards meets the victory condition when a certain card is drawn and marked. In other cases, it is a draw. Cards may be drawn even after one of them meets the victory conditions, but this does not affect the victory or defeat. Input Line 1: β€œnuvm” (square size, number of rabbit playing cards, number of cat playing cards, number of cards drawn) 2- (N + 1) Line: n2 numbers that the rabbit writes on paper ( N + 2)-(2N + 1) Line: n2 numbers that the cat writes on paper (2N + 2)-(2N + M + 1) Line: m cards drawn 1 ≀ n ≀ 500 1 ≀ u, v ≀ 13 1 ≀ m ≀ 100 000 1 ≀ (number written) ≀ 1 000 000 The number of n2 rabbits write on paper, the number of n2 cats write on paper, and the number of m cards drawn are different. Output Output "USAGI" if the rabbit wins, "NEKO" if the cat wins, and "DRAW" if the tie, each in one line. Examples Input 3 2 2 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Output USAGI Input 3 2 1 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Output 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. Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches. The current state of the wall can be respresented by a sequence $a$ of $n$ integers, with $a_i$ being the height of the $i$-th part of the wall. Vova can only use $2 \times 1$ bricks to put in the wall (he has infinite supply of them, however). Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $i$ the current height of part $i$ is the same as for part $i + 1$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $1$ of the wall or to the right of part $n$ of it). The next paragraph is specific to the version 1 of the problem. Vova can also put bricks vertically. That means increasing height of any part of the wall by 2. Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of parts in the wall. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β€” the initial heights of the parts of the wall. -----Output----- Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise. -----Examples----- Input 5 2 1 1 2 5 Output YES Input 3 4 5 3 Output YES Input 2 10 10 Output YES Input 3 1 2 3 Output NO -----Note----- In the first example Vova can put a brick on parts 2 and 3 to make the wall $[2, 2, 2, 2, 5]$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $[5, 5, 5, 5, 5]$. In the second example Vova can put a brick vertically on part 3 to make the wall $[4, 5, 5]$, then horizontally on parts 2 and 3 to make it $[4, 6, 6]$ and then vertically on part 1 to make it $[6, 6, 6]$. In the third example the wall is already complete. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of $7$ segments, which can be turned on or off to display different numbers. The picture shows how all $10$ decimal digits are displayed: [Image] After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly $k$ segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly $k$ sticks (which are off now)? It is allowed that the number includes leading zeros. -----Input----- The first line contains integer $n$ $(1 \leq n \leq 2000)$ Β β€” the number of digits on scoreboard and $k$ $(0 \leq k \leq 2000)$ Β β€” the number of segments that stopped working. The next $n$ lines contain one binary string of length $7$, the $i$-th of which encodes the $i$-th digit of the scoreboard. Each digit on the scoreboard consists of $7$ segments. We number them, as in the picture below, and let the $i$-th place of the binary string be $0$ if the $i$-th stick is not glowing and $1$ if it is glowing. Then a binary string of length $7$ will specify which segments are glowing now. [Image] Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from $0$ to $9$ inclusive. -----Output----- Output a single number consisting of $n$ digits Β β€” the maximum number that can be obtained if you turn on exactly $k$ sticks or $-1$, if it is impossible to turn on exactly $k$ sticks so that a correct number appears on the scoreboard digits. -----Examples----- Input 1 7 0000000 Output 8 Input 2 5 0010010 0010010 Output 97 Input 3 5 0100001 1001001 1010011 Output -1 -----Note----- In the first test, we are obliged to include all $7$ sticks and get one $8$ digit on the scoreboard. In the second test, we have sticks turned on so that units are formed. For $5$ of additionally included sticks, you can get the numbers $07$, $18$, $34$, $43$, $70$, $79$, $81$ and $97$, of which we choose the maximum Β β€” $97$. In the third test, it is impossible to turn on exactly $5$ sticks so that a sequence of numbers appears on the scoreboard. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct. Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. Can you determine who will be each person’s teammate? -----Input----- There are 2n lines in the input. The first line contains an integer n (1 ≀ n ≀ 400) β€” the number of teams to be formed. The i-th line (i > 1) contains i - 1 numbers a_{i}1, a_{i}2, ... , a_{i}(i - 1). Here a_{ij} (1 ≀ a_{ij} ≀ 10^6, all a_{ij} are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.) -----Output----- Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person. -----Examples----- Input 2 6 1 2 3 4 5 Output 2 1 4 3 Input 3 487060 3831 161856 845957 794650 976977 83847 50566 691206 498447 698377 156232 59015 382455 626960 Output 6 5 4 3 2 1 -----Note----- In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given two integers K and S. Three variable X, Y and Z takes integer values satisfying 0≀X,Y,Z≀K. How many different assignments of values to X, Y and Z are there such that X + Y + Z = S? -----Constraints----- - 2≀K≀2500 - 0≀S≀3K - K and S are integers. -----Input----- The input is given from Standard Input in the following format: K S -----Output----- Print the number of the triples of X, Y and Z that satisfy the condition. -----Sample Input----- 2 2 -----Sample Output----- 6 There are six triples of X, Y and Z that satisfy the condition: - X = 0, Y = 0, Z = 2 - X = 0, Y = 2, Z = 0 - X = 2, Y = 0, Z = 0 - X = 0, Y = 1, Z = 1 - X = 1, Y = 0, Z = 1 - X = 1, Y = 1, Z = 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. Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: Alex and Bob play the first game, and Carl is spectating; When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner. Alex, Bob and Carl play in such a way that there are no draws. Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it! -----Input----- The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of games Alex, Bob and Carl played. Then n lines follow, describing the game log. i-th line contains one integer a_{i} (1 ≀ a_{i} ≀ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game. -----Output----- Print YES if the situation described in the log was possible. Otherwise print NO. -----Examples----- Input 3 1 1 2 Output YES Input 2 1 2 Output NO -----Note----- In the first example the possible situation is: Alex wins, Carl starts playing instead of Bob; Alex wins, Bob replaces Carl; Bob wins. The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants. -----Input----- The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b < p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime. -----Output----- Print a single integer: the number of possible answers $n$. -----Examples----- Input 2 3 5 8 Output 2 Input 4 6 7 13 Output 1 Input 233 233 10007 1 Output 1 -----Note----- In the first sample, we can see that $n=2$ and $n=8$ are possible answers. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Student Dima from Kremland has a matrix $a$ of size $n \times m$ filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence $c_1, c_2, \ldots, c_n$ ($1 \leq c_j \leq m$) so that the inequality $a_{1, c_1} \oplus a_{2, c_2} \oplus \ldots \oplus a_{n, c_n} > 0$ holds, where $a_{i, j}$ is the matrix element from the $i$-th row and the $j$-th column. Here $x \oplus y$ denotes the bitwise XOR operation of integers $x$ and $y$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 500$)Β β€” the number of rows and the number of columns in the matrix $a$. Each of the next $n$ lines contains $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $a$, i.e. $a_{i, j}$ ($0 \leq a_{i, j} \leq 1023$). -----Output----- If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print $n$ integers $c_1, c_2, \ldots c_n$ ($1 \leq c_j \leq m$), so that the inequality $a_{1, c_1} \oplus a_{2, c_2} \oplus \ldots \oplus a_{n, c_n} > 0$ holds. If there is more than one possible answer, you may output any. -----Examples----- Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 -----Note----- In the first example, all the numbers in the matrix are $0$, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are $7$ (the first number in the first line) and $10$ (the third number in the second line), $7 \oplus 10 = 13$, $13$ is more than $0$, so the answer is found. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 `String#ipv4_address?`, which should return true if given object is an IPv4 address - four numbers (0-255) separated by dots. It should only accept addresses in canonical representation, so no leading `0`s, spaces etc. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. As a token of his gratitude, Takahashi has decided to give his mother an integer sequence. The sequence A needs to satisfy the conditions below: - A consists of integers between X and Y (inclusive). - For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i. Find the maximum possible length of the sequence. -----Constraints----- - 1 \leq X \leq Y \leq 10^{18} - All input values are integers. -----Input----- Input is given from Standard Input in the following format: X Y -----Output----- Print the maximum possible length of the sequence. -----Sample Input----- 3 20 -----Sample Output----- 3 The sequence 3,6,18 satisfies the conditions. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Mr. Frugal bought a new house. He feels deeply in love with his new house because it has a comfortable living room in which he can put himself completely at ease. He thinks his new house is a really good buy. But, to his disappointment, the floor of its living room has some scratches on it. The floor has a rectangle shape, covered with square panels. He wants to replace all the scratched panels with flawless panels, but he cannot afford to do so. Then, he decides to cover all the scratched panels with carpets. The features of the carpets he can use are as follows. 1. Carpets are square-shaped. 2. Carpets may overlap each other. 3. Carpets cannot be folded. 4. Different sizes of carpets are available. Lengths of sides of carpets are multiples of that of the panels. The carpets must cover all the scratched panels, but must not cover any of the flawless ones. For example, if the scratched panels are as shown in Figure 1, at least 6 carpets are needed. <image> --- Figure 1: Example Covering As carpets cost the same irrespective of their sizes, Mr. Frugal would like to use as few number of carpets as possible. Your job is to write a program which tells the minimum number of the carpets to cover all the scratched panels. Input The input consists of multiple data sets. As in the following, the end of the input is indicated by a line containing two zeros. > DataSet1 > DataSet2 > ... > DataSetn > `0` `0` Each data set (DataSeti) represents the state of a floor. The format of a data set is as follows. > W H > P11 P12 P13 ... P1W > P21 P22 P23 ... P2W > ... > PH1 PH2 PH3 ... PHW > The positive integers W and H are the numbers of panels on the living room in the x- and y- direction, respectively. The values of W and H are no more than 10. The integer Pyx represents the state of the panel. The value of Pyx means, > `0`: flawless panel (must not be covered), > `1`: scratched panel (must be covered). Output For each data set, your program should output a line containing one integer which represents the minimum number of the carpets to cover all of the scratched panels. Example Input 4 3 0 1 1 1 1 1 1 1 1 1 1 1 8 5 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 8 8 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 Output 2 6 14 29 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A Narcissistic Number is a number of length n in which the sum of its digits to the power of n is equal to the original number. If this seems confusing, refer to the example below. Ex: 153, where n = 3 (number of digits in 153) 1^(3) + 5^(3) + 3^(3) = 153 Write a method is_narcissistic(i) (in Haskell: isNarcissistic :: Integer -> Bool) which returns whether or not i is a Narcissistic Number. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment. For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place. Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima. -----Input----- The first line contains integer n (1 ≀ n ≀ 100) β€” the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show. The numbers in the lines are separated by a single space. -----Output----- In a single line print the answer to the problem. -----Examples----- Input 1 1 Output 3 Input 1 2 Output 2 Input 2 3 5 Output 3 -----Note----- In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend. In the second sample Dima can show 2 or 4 fingers. Read the inputs from stdin solve the problem and write the answer to stdout (do 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 tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = W represents the vertex is white; c_i = B represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: - Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. - Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? -----Constraints----- - 1 ≀ N ≀ 10^5 - 1 ≀ x_i,y_i ≀ N (1 ≀ i ≀ N-1) - The given graph is a tree. - c_i = W or c_i = B. -----Input----- Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N -----Output----- Print the minimum number of seconds required to achieve the objective. -----Sample Input----- 5 1 2 2 3 2 4 4 5 WBBWW -----Sample Output----- 5 The objective can be achieved in five seconds, for example, as follows: - Start at Vertex 1. Change the color of Vertex 1 to black. - Move to Vertex 2, then change the color of Vertex 2 to white. - Change the color of Vertex 2 to black. - Move to Vertex 4, then change the color of Vertex 4 to black. - Move to Vertex 5, then change the color of Vertex 5 to black. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≀ n ≀ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≀ s_i ≀ 100) β€” the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 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.