question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Taro is planning a long trip by train during the summer vacation. However, in order for Taro, who is a high school student, to travel as far as possible during the summer vacation, which has only one month, he cannot make a good plan unless he finds the cheapest and the fastest way. Let's create a program to help Taro's plan so that he can enjoy a wonderful trip. <image> Create a program that outputs the minimum amount or the shortest time in response to inquiries by inputting track information and the number of stations. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: n m a1 b1 cost1 time1 a2 b2 cost2 time2 :: an bn costn timen k p1 q1 r1 p2 q2 r2 :: pk qk rk The first line gives the number of track information n (1 ≀ n ≀ 3000) and the number of stations m (1 ≀ m ≀ 100). The following n lines give information on the i-th line. As information on each line, the numbers ai, bi (1 ≀ ai, bi ≀ m) of the two stations connecting the lines, the toll costi (1 ≀ costi ≀ 1000), and the travel time timei (1 ≀ timei ≀ 1000) are given. I will. However, each station shall be numbered in order from 1 to m. If ai and bi are connected by railroad tracks, both ai to bi and bi to ai can be moved at the same rate and time. The following line is given the number of queries k (1 ≀ k ≀ 200). The next k line is given the i-th query. For each query, the departure station pi, the arrival station qi, and the type of value to output ri (0 or 1) are given. Inquiries must have a route. The number of datasets does not exceed 50. Output Outputs the minimum amount or minimum time on one line for each data set. When ri is 0, the minimum amount is output, and when ri is 1, the minimum time is output. Example Input 6 5 1 2 200 10 1 4 400 15 1 3 250 25 2 4 100 10 4 5 150 20 3 5 300 20 2 1 5 0 1 5 1 0 0 Output 450 35 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation periods are allowed to overlap each other. Write a program to report if a new reservation overlaps with any of the existing reservations. Note that the coincidence of start and end times is not considered to constitute an overlap. All the temporal data is given as the elapsed time from the moment at which the L system starts operation. Input The input is given in the following format. a b N s_1 f_1 s_2 f_2 : s_N f_N The first line provides new reservation information, i.e., the start time a and end time b (0 ≀ a < b ≀ 1000) in integers. The second line specifies the number of existing reservations N (0 ≀ N ≀ 100). Subsequent N lines provide temporal information for the i-th reservation: start time s_i and end time f_i (0 ≀ s_i < f_i ≀ 1000) in integers. No two existing reservations overlap. Output Output "1" if the new reservation temporally overlaps with any of the existing ones, or "0" otherwise. Examples Input 5 7 3 1 4 4 5 7 10 Output 0 Input 3 7 3 7 10 1 4 4 5 Output 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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. Denjiro is a science teacher. Today he has just received a specially ordered water tank that will certainly be useful for his innovative experiments on water flow. <image> --- Figure 1: The water tank The size of the tank is 100cm (Width) * 50cm (Height) * 30cm (Depth) (see Figure 1). For the experiments, he fits several partition boards inside of the tank parallel to the sideboards. The width of each board is equal to the depth of the tank, i.e. 30cm. The height of each board is less than that of the tank, i.e. 50 cm, and differs one another. The boards are so thin that he can neglect the thickness in the experiments. <image> --- Figure 2: The front view of the tank The front view of the tank is shown in Figure 2. There are several faucets above the tank and he turns them on at the beginning of his experiment. The tank is initially empty. Your mission is to write a computer program to simulate water flow in the tank. Input The input consists of multiple data sets. D is the number of the data sets. D DataSet1 DataSet2 ... DataSetD The format of each data set (DataSetd , 1 <= d <= D) is as follows. N B1 H1 B2 H2 ... BN HN M F1 A1 F2 A2 ... FM AM L P1 T1 P2 T2 ... PL TL Each line in the data set contains one or two integers. N is the number of the boards he sets in the tank . Bi and Hi are the x-position (cm) and the height (cm) of the i-th board, where 1 <= i <= N . Hi s differ from one another. You may assume the following. > 0 < N < 10 , > 0 < B1 < B2 < ... < BN < 100 , > 0 < H1 < 50 , 0 < H2 < 50 , ..., 0 < HN < 50. M is the number of the faucets above the tank . Fj and Aj are the x-position (cm) and the amount of water flow (cm3/second) of the j-th faucet , where 1 <= j <= M . There is no faucet just above any boards . Namely, none of Fj is equal to Bi . You may assume the following . > 0 < M <10 , > 0 < F1 < F2 < ... < FM < 100 , > 0 < A1 < 100, 0 < A2 < 100, ... 0 < AM < 100. L is the number of observation time and location. Pk is the x-position (cm) of the k-th observation point. Tk is the k-th observation time in seconds from the beginning. None of Pk is equal to Bi . You may assume the following . > 0 < L < 10 , > 0 < P1 < 100, 0 < P2 < 100, ..., 0 < PL < 100 , > 0 < T1 < 1000000, 0 < T2 < 1000000, ... , 0 < TL < 1000000. Output For each data set, your program should output L lines each containing one real number which represents the height (cm) of the water level specified by the x-position Pk at the time Tk. Each answer may not have an error greater than 0.001. As long as this condition is satisfied, you may output any number of digits after the decimal point. After the water tank is filled to the brim, the water level at any Pk is equal to the height of the tank, that is, 50 cm. Example Input 2 5 15 40 35 20 50 45 70 30 80 10 3 20 3 60 2 65 2 6 40 4100 25 7500 10 18000 90 7000 25 15000 25 22000 5 15 40 35 20 50 45 70 30 80 10 2 60 4 75 1 3 60 6000 75 6000 85 6000 Output 0.666667 21.4286 36.6667 11.1111 40.0 50.0 30.0 13.3333 13.3333 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbered 1, ... , n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k - 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1. <image> Figure 1: An example game Initial state: Eight stones are arranged on a circle. Step 1: Stone 3 is removed since m = 3. Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8. Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case. Steps 4-7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7. Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1. Input The input consists of multiple datasets each of which is formatted as follows. n k m The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions. 2 ≀ n ≀ 10000, 1 ≀ k ≀ 10000, 1 ≀ m ≀ n The number of datasets is less than 100. Output For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output. Example Input 8 5 3 100 9999 98 10000 10000 10000 0 0 0 Output 1 93 2019 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Scores of Final Examination I am a junior high school teacher. The final examination has just finished, and I have all the students' scores of all the subjects. I want to know the highest total score among the students, but it is not an easy task as the student scores are listed separately for each subject. I would like to ask you, an excellent programmer, to help me by writing a program that finds the total score of a student with the highest total score. Input The input consists of multiple datasets, each in the following format. > n m > p1,1 p1,2 … p1,n > p2,1 p2,2 … p2,n > … > pm,1 pm,2 … pm,n > The first line of a dataset has two integers n and m. n is the number of students (1 ≀ n ≀ 1000). m is the number of subjects (1 ≀ m ≀ 50). Each of the following m lines gives n students' scores of a subject. pj,k is an integer representing the k-th student's score of the subject j (1 ≀ j ≀ m and 1 ≀ k ≀ n). It satisfies 0 ≀ pj,k ≀ 1000. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, output the total score of a student with the highest total score. The total score sk of the student k is defined by sk = p1,k + … + pm,k. Sample Input 5 2 10 20 30 40 50 15 25 35 45 55 6 3 10 20 30 15 25 35 21 34 11 52 20 18 31 15 42 10 21 19 4 2 0 0 0 0 0 0 0 0 0 0 Output for the Sample Input 105 83 0 Example Input 5 2 10 20 30 40 50 15 25 35 45 55 6 3 10 20 30 15 25 35 21 34 11 52 20 18 31 15 42 10 21 19 4 2 0 0 0 0 0 0 0 0 0 0 Output 105 83 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well. Today's training is to quickly solve popular puzzles and train your instantaneous power. Today's challenge is a puzzle of colorful tiles lined up and erasing them well. In the initial state, tiles are placed on some squares on the grid. Each tile is colored. After the game starts, the player can perform the operations shown in the following procedure many times. 1. Select one square without tiles and hit that square. 2. Follow from the hit square to the top, and pay attention to the tile when you reach the square where the tile is placed. If you get to the edge of the board without the squares on which the tiles are placed, you will not pay attention to anything. 3. Perform the same operation from the square you hit to the bottom, left, and right. Up to 4 tiles will be the focus of attention. 4. If any of the tiles of interest have the same color, remove those tiles from the board. If there are two pairs of tiles of the same color, remove both. 5. The score will be the same as the number of tiles removed. 6. Stop paying attention. For example, consider the following situation. The squares without tiles are periods, and the tile colors are represented by uppercase letters. ..A ...... ....... B .. .......... ..B ...... ..A.CC .... Now consider the operation of hitting the squares in the second row from the top and the third column from the left. Since there are three tiles of interest, `A`,` B`, and `B`, the two of` B` disappear and the board becomes as follows, and two points are obtained. ..A ...... .......... .......... .......... ..A.CC .... If this puzzle is slow, the time will run out, and you will not be able to see part of the board and you will not know how much training you lacked. Two tiles of each color are placed, but it is not always possible to erase all of them, so let the program calculate the maximum score in advance. Input M N C1,1 C1,2 ... C1, N C2,1 C2,2 ... C2, N ... CM, 1CM, 2 ... CM, N The integers M and N indicate that the board is a grid of vertical M x horizontal N. Ci and j are uppercase letters or periods (`.`), And for the cells in the i-th row from the top and the j-th column from the left, the uppercase letters indicate the color of the tiles placed, and the period indicates the color of the tile. Indicates that no tile is placed on this square. Satisfy 1 ≀ M ≀ 500 and 1 ≀ N ≀ 500. Each uppercase letter appears as 0 or 2 as you type. Output Output the maximum score on one line. Examples Input 5 10 ..A....... .......B.. .......... ..B....... ..A.CC.... Output 4 Input 3 3 ABC D.D CBA Output 4 Input 5 7 NUTUBOR QT.SZRQ SANAGIP LMDGZBM KLKIODP Output 34 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 an English learner, sometimes you cannot remember the entire spelling of English words perfectly, but you can only remember their prefixes and suffixes. For example, you may want to use a word which begins with 'appr' and ends with 'iate', but forget the middle part of the word. It may be 'appreciate', 'appropriate', or something like them. By using an ordinary dictionary, you can look up words beginning with a certain prefix, but it is inconvenient for further filtering words ending with a certain suffix. Thus it is helpful to achieve dictionary functionality which can be used for finding words with a given prefix and suffix. In the beginning, let's count the number of such words instead of explicitly listing them. More formally, you are given a list of $N$ words. Next, you are given $Q$ queries consisting of two strings. Your task is to write a program which outputs the number of words with the prefix and suffix for each query in the given list. Input The input consists of a single test case in the following format. $N$ $Q$ $w_1$ $...$ $w_N$ $p_1$ $s_1$ $...$ $p_Q$ $s_Q$ The first line contains two integers $N$ and $Q$, where $N$ ($1 \leq N \leq 10^5$) is the number of words in the list, and $Q$ ($1 \leq Q \leq 10^5$) is the number of queries. The $i$-th line of the following $N$ lines contains a string $w_i$. The $i$-th of the following $Q$ lines contains two strings $p_i$ and $s_i$, which are a prefix and suffix of words to be searched, respectively. You can assume the followings: * All the strings in an input are non-empty and consist only of lowercase English letters. * The total length of input strings does not exceed $2,500,000$. * Words in the given list are unique: $w_i \ne w_j$ if $i \ne j$. * Pairs of a prefix and suffix are unique: $(p_i, s_i) \ne (p_j, s_j)$ if $i \ne j$. Output For each query, output the number of words with a given prefix and suffix in the given list per a line. Examples Input 6 7 appreciate appropriate acceptance ace acm acetylene appr iate a e a a ac ce ace e acceptance acceptance no match Output 2 5 0 2 2 1 0 Input 5 5 d dd ddd dddd ddddd d d dd dd ddd ddd d dddd ddddd dd Output 5 4 3 2 1 Input 7 4 connected disconnected graph directed diameter distance minor c ed di ed dis ed dis e Output 1 2 1 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Den, the phone number of Ukunikia Co., Ltd., enters a very long phone number into the phone every day. One day, too tired, Den came up with a surprising idea. "Isn't it even a little easier if you rearrange the arrangement of the buttons on the phone ?!" The phone has squares evenly spaced at $ 3 \ times 3 $, and each of the nine squares has one button from 1 to 9 that can be sorted. When typing a phone number, Den can do two things with just one hand: * Move your index finger to touch one of the adjacent buttons on the side of the button you are currently touching. * Press the button that your index finger is touching. Initially, the index finger can be placed to touch any of the buttons 1-9. Mr. Den thinks that the arrangement that can minimize the number of movements of the index finger from pressing the first button to the end of pressing the last button is efficient. Now, here is the phone number of the customer with a length of $ N $. What kind of arrangement is most efficient when considering only the customer's phone number? Make the arrangement by rearranging the buttons. Constraints The input satisfies the following conditions. * $ 1 \ leq N \ leq 10 ^ 5 $ * $ S $ is a string consisting of any number from 1 to 9. Input The input is given in the following format. $ N $ $ S $ The first line gives the customer's phone number length $ N $. The customer's phone number is given to the first line on the second line. Output Output the most efficient placement with no blanks on the 3 lines. However, if there are multiple possible answers, start from the upper left frame. one two Three 456 789 When arranging the numbers in the order of, output the one that is the smallest in the dictionary order. Examples Input 10 1236547896 Output 123 456 789 Input 11 31415926535 Output 137 456 892 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For given three integers $a, b, c$, print the minimum value and the maximum value. Constraints * $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$ Input The input is given in the following format. $a \; b \; c\;$ Three integers $a, b, c$ are given in a line. Output Print the minimum and maximum values separated by a space in a line. Example Input 4 5 3 Output 3 5 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 description As a holiday gift, Tojo received a probability problem. The problem read as follows Consider an N by M grid. Rows are numbered 1 to N, from top to bottom. Columns are numbered 1 to M, from left to right. You are initially at cell (1, 1) and want to go to cell (N, M). From any cell you can move to the cell below it or to the cell right to it. You should never go out of the grid. At any point you should consider all the possibilities of movement with equal probability Let P[i][j] be the probability of visiting cell (i, j). You need to calculate the sum of P[i][j] for 1 ≀ i ≀ N, 1 ≀ i ≀ M. As we all know, Tojo really hates probability related problems. He wants you to solve this task Input The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.Only line of each test case has two integer N and M. Output For each test case, output a single line containing the required answer. Answers within an absolute or relative error of 10^-6 will be accepted. Constraints 1 ≀ T ≀ 1000 1 ≀ N ≀ 1000 1 ≀ M ≀ 1000 Example Input: 2 2 2 1 6 Output: 3.000000 6.000000 Explanation Example case 1 Probability matrix P for N=2, M=2 is 1.0 0.5 0.5 1.0 You are at (1, 1) initially. So the probablity of visiting (1, 1) is 1. At (1, 1) you have 2 options, move below to (2, 1) or to right cell (1, 2). Probablity of going to (1, 2) is 0.5. Probability of going to (2, 1) is 0.5. You always end up at (2, 2), so P[2][2] is 1. Required sum = 1.0 + 0.5 + 0.5 + 1.0 = 3.0 Example case 2 Probability matrix P for N=1, M=6 is 1.0 1.0 1.0 1.0 1.0 1.0 Because at any position there is only one possible next position. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Vasya found a golden ticket β€” a sequence which consists of n digits a_1a_2... a_n. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket 350178 is lucky since it can be divided into three segments 350, 17 and 8: 3+5+0=1+7=8. Note that each digit of sequence should belong to exactly one segment. Help Vasya! Tell him if the golden ticket he found is lucky or not. Input The first line contains one integer n (2 ≀ n ≀ 100) β€” the number of digits in the ticket. The second line contains n digits a_1 a_2 ... a_n (0 ≀ a_i ≀ 9) β€” the golden ticket. Digits are printed without spaces. Output If the golden ticket is lucky then print "YES", otherwise print "NO" (both case insensitive). Examples Input 5 73452 Output YES Input 4 1248 Output NO Note In the first example the ticket can be divided into 7, 34 and 52: 7=3+4=5+2. In the second example it is impossible to divide ticket into segments with equal sum. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5. Lyft has become so popular so that it is now used by all m taxi drivers in the city, who every day transport the rest of the city residents β€” n riders. Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same). The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected. But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver i the number a_{i} β€” the number of riders that would call the i-th taxi driver when all drivers and riders are at their home? The taxi driver can neither transport himself nor other taxi drivers. Input The first line contains two integers n and m (1 ≀ n,m ≀ 10^5) β€” number of riders and taxi drivers. The second line contains n + m integers x_1, x_2, …, x_{n+m} (1 ≀ x_1 < x_2 < … < x_{n+m} ≀ 10^9), where x_i is the coordinate where the i-th resident lives. The third line contains n + m integers t_1, t_2, …, t_{n+m} (0 ≀ t_i ≀ 1). If t_i = 1, then the i-th resident is a taxi driver, otherwise t_i = 0. It is guaranteed that the number of i such that t_i = 1 is equal to m. Output Print m integers a_1, a_2, …, a_{m}, where a_i is the answer for the i-th taxi driver. The taxi driver has the number i if among all the taxi drivers he lives in the i-th smallest coordinate (see examples for better understanding). Examples Input 3 1 1 2 3 10 0 0 1 0 Output 3 Input 3 2 2 3 4 5 6 1 0 0 0 1 Output 2 1 Input 1 4 2 4 6 10 15 1 1 1 1 0 Output 0 0 0 1 Note In the first example, we have only one taxi driver, which means an order from any of n riders will go to him. In the second example, the first taxi driver lives at the point with the coordinate 2, and the second one lives at the point with the coordinate 6. Obviously, the nearest taxi driver to the rider who lives on the 3 coordinate is the first one, and to the rider who lives on the coordinate 5 is the second one. The rider who lives on the 4 coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver. In the third example, we have one rider and the taxi driver nearest to him is the fourth one. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an angle ang. The Jury asks You to find such regular n-gon (regular polygon with n vertices) that it has three vertices a, b and c (they can be non-consecutive) with \angle{abc} = ang or report that there is no such n-gon. <image> If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed 998244353. Input The first line contains single integer T (1 ≀ T ≀ 180) β€” the number of queries. Each of the next T lines contains one integer ang (1 ≀ ang < 180) β€” the angle measured in degrees. Output For each query print single integer n (3 ≀ n ≀ 998244353) β€” minimal possible number of vertices in the regular n-gon or -1 if there is no such n. Example Input 4 54 50 2 178 Output 10 18 90 180 Note The answer for the first query is on the picture above. The answer for the second query is reached on a regular 18-gon. For example, \angle{v_2 v_1 v_6} = 50^{\circ}. The example angle for the third query is \angle{v_{11} v_{10} v_{12}} = 2^{\circ}. In the fourth query, minimal possible n is 180 (not 90). The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Reziba has many magic gems. Each magic gem can be split into M normal gems. The amount of space each magic (and normal) gem takes is 1 unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is N units. If a magic gem is chosen and split, it takes M units of space (since it is split into M gems); if a magic gem is not split, it takes 1 unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is N units? Print the answer modulo 1000000007 (10^9+7). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. Input The input contains a single line consisting of 2 integers N and M (1 ≀ N ≀ 10^{18}, 2 ≀ M ≀ 100). Output Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is N units. Print the answer modulo 1000000007 (10^9+7). Examples Input 4 2 Output 5 Input 3 2 Output 3 Note In the first example each magic gem can split into 2 normal gems, and we know that the total amount of gems are 4. Let 1 denote a magic gem, and 0 denote a normal gem. The total configurations you can have is: * 1 1 1 1 (None of the gems split); * 0 0 1 1 (First magic gem splits into 2 normal gems); * 1 0 0 1 (Second magic gem splits into 2 normal gems); * 1 1 0 0 (Third magic gem splits into 2 normal gems); * 0 0 0 0 (First and second magic gems split into total 4 normal gems). Hence, answer is 5. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Vasya learned that, given two points with different x coordinates, you can draw through them exactly one parabola with equation of type y = x^2 + bx + c, where b and c are reals. Let's call such a parabola an U-shaped one. Vasya drew several distinct points with integer coordinates on a plane and then drew an U-shaped parabola through each pair of the points that have different x coordinates. The picture became somewhat messy, but Vasya still wants to count how many of the parabolas drawn don't have any drawn point inside their internal area. Help Vasya. The internal area of an U-shaped parabola is the part of the plane that lies strictly above the parabola when the y axis is directed upwards. Input The first line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of points. The next n lines describe the points, the i-th of them contains two integers x_i and y_i β€” the coordinates of the i-th point. It is guaranteed that all points are distinct and that the coordinates do not exceed 10^6 by absolute value. Output In the only line print a single integer β€” the number of U-shaped parabolas that pass through at least two of the given points and do not contain any of the given points inside their internal area (excluding the parabola itself). Examples Input 3 -1 0 0 2 1 0 Output 2 Input 5 1 0 1 -1 0 -1 -1 0 -1 -1 Output 1 Note On the pictures below all U-shaped parabolas that pass through at least two given points are drawn for each of the examples. The U-shaped parabolas that do not have any given point inside their internal area are drawn in red. <image> The first example. <image> The second example. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen n distinct positive integers and put all of them in a set S. Now he defines a magical permutation to be: * A permutation of integers from 0 to 2^x - 1, where x is a non-negative integer. * The [bitwise xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of any two consecutive elements in the permutation is an element in S. Since Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer x such that there is a magical permutation of integers from 0 to 2^x - 1. Since he is a newbie in the subject, he wants you to help him find this value of x and also the magical permutation for that x. Input The first line contains the integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of elements in the set S. The next line contains n distinct integers S_1, S_2, …, S_n (1 ≀ S_i ≀ 2 β‹… 10^5) β€” the elements in the set S. Output In the first line print the largest non-negative integer x, such that there is a magical permutation of integers from 0 to 2^x - 1. Then print 2^x integers describing a magical permutation of integers from 0 to 2^x - 1. If there are multiple such magical permutations, print any of them. Examples Input 3 1 2 3 Output 2 0 1 3 2 Input 2 2 3 Output 2 0 2 1 3 Input 4 1 2 3 4 Output 3 0 1 3 2 6 7 5 4 Input 2 2 4 Output 0 0 Input 1 20 Output 0 0 Input 1 1 Output 1 0 1 Note In the first example, 0, 1, 3, 2 is a magical permutation since: * 0 βŠ• 1 = 1 ∈ S * 1 βŠ• 3 = 2 ∈ S * 3 βŠ• 2 = 1 ∈ S Where βŠ• denotes [bitwise xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Heidi and Doctor Who hopped out of the TARDIS and found themselves at EPFL in 2018. They were surrounded by stormtroopers and Darth Vader was approaching. Miraculously, they managed to escape to a nearby rebel base but the Doctor was very confused. Heidi reminded him that last year's HC2 theme was Star Wars. Now he understood, and he's ready to face the evils of the Empire! The rebels have s spaceships, each with a certain attacking power a. They want to send their spaceships to destroy the empire bases and steal enough gold and supplies in order to keep the rebellion alive. The empire has b bases, each with a certain defensive power d, and a certain amount of gold g. A spaceship can attack all the bases which have a defensive power less than or equal to its attacking power. If a spaceship attacks a base, it steals all the gold in that base. The rebels are still undecided which spaceship to send out first, so they asked for the Doctor's help. They would like to know, for each spaceship, the maximum amount of gold it can steal. Input The first line contains integers s and b (1 ≀ s, b ≀ 10^5), the number of spaceships and the number of bases, respectively. The second line contains s integers a (0 ≀ a ≀ 10^9), the attacking power of each spaceship. The next b lines contain integers d, g (0 ≀ d ≀ 10^9, 0 ≀ g ≀ 10^4), the defensive power and the gold of each base, respectively. Output Print s integers, the maximum amount of gold each spaceship can steal, in the same order as the spaceships are given in the input. Example Input 5 4 1 3 5 2 4 0 1 4 2 2 8 9 4 Output 1 9 11 9 11 Note The first spaceship can only attack the first base. The second spaceship can attack the first and third bases. The third spaceship can attack the first, second and third bases. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 binary strings x and y, which are binary representations of some two integers (let's denote these integers as f(x) and f(y)). You can choose any integer k β‰₯ 0, calculate the expression s_k = f(x) + f(y) β‹… 2^k and write the binary representation of s_k in reverse order (let's denote it as rev_k). For example, let x = 1010 and y = 11; you've chosen k = 1 and, since 2^1 = 10_2, so s_k = 1010_2 + 11_2 β‹… 10_2 = 10000_2 and rev_k = 00001. For given x and y, you need to choose such k that rev_k is lexicographically minimal (read notes if you don't know what does "lexicographically" means). It's guaranteed that, with given constraints, k exists and is finite. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of queries. Next 2T lines contain a description of queries: two lines per query. The first line contains one binary string x, consisting of no more than 10^5 characters. Each character is either 0 or 1. The second line contains one binary string y, consisting of no more than 10^5 characters. Each character is either 0 or 1. It's guaranteed, that 1 ≀ f(y) ≀ f(x) (where f(x) is the integer represented by x, and f(y) is the integer represented by y), both representations don't have any leading zeroes, the total length of x over all queries doesn't exceed 10^5, and the total length of y over all queries doesn't exceed 10^5. Output Print T integers (one per query). For each query print such k that rev_k is lexicographically minimal. Example Input 4 1010 11 10001 110 1 1 1010101010101 11110000 Output 1 3 0 0 Note The first query was described in the legend. In the second query, it's optimal to choose k = 3. The 2^3 = 1000_2 so s_3 = 10001_2 + 110_2 β‹… 1000_2 = 10001 + 110000 = 1000001 and rev_3 = 1000001. For example, if k = 0, then s_0 = 10111 and rev_0 = 11101, but rev_3 = 1000001 is lexicographically smaller than rev_0 = 11101. In the third query s_0 = 10 and rev_0 = 01. For example, s_2 = 101 and rev_2 = 101. And 01 is lexicographically smaller than 101. The quote from Wikipedia: "To determine which of two strings of characters comes when arranging in lexicographical order, their first letters are compared. If they differ, then the string whose first letter comes earlier in the alphabet comes before the other string. If the first letters are the same, then the second letters are compared, and so on. If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order." The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left. Initially, the cursor is in the first (leftmost) character. Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position. Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence. Formally, correct text (CT) must satisfy the following rules: * any line without brackets is CT (the line can contain whitespaces); * If the first character of the string β€” is (, the last β€” is ), and all the rest form a CT, then the whole line is a CT; * two consecutively written CT is also CT. Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me). The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command. The correspondence of commands and characters is as follows: * L β€” move the cursor one character to the left (remains in place if it already points to the first character); * R β€” move the cursor one character to the right; * any lowercase Latin letter or bracket (( or )) β€” write the entered character to the position where the cursor is now. For a complete understanding, take a look at the first example and its illustrations in the note below. You are given a string containing the characters that the user entered. For the brackets coloring module's work, after each command you need to: * check if the current text in the editor is a correct text; * if it is, print the least number of colors that required, to color all brackets. If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is 2, and for the bracket sequence (()(()())())(()) β€” is 3. Write a program that prints the minimal number of colors after processing each command. Input The first line contains an integer n (1 ≀ n ≀ 10^6) β€” the number of commands. The second line contains s β€” a sequence of commands. The string s consists of n characters. It is guaranteed that all characters in a string are valid commands. Output In a single line print n integers, where the i-th number is: * -1 if the line received after processing the first i commands is not valid text, * the minimal number of colors in the case of the correct text. Examples Input 11 (RaRbR)L)L( Output -1 -1 -1 -1 -1 -1 1 1 -1 -1 2 Input 11 (R)R(R)Ra)c Output -1 -1 1 1 -1 -1 1 1 1 -1 1 Note In the first example, the text in the editor will take the following form: 1. ( ^ 2. ( ^ 3. (a ^ 4. (a ^ 5. (ab ^ 6. (ab ^ 7. (ab) ^ 8. (ab) ^ 9. (a)) ^ 10. (a)) ^ 11. (()) ^ The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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, Mezo is playing a game. Zoma, a character in that game, is initially at position x = 0. Mezo starts sending n commands to Zoma. There are two possible commands: * 'L' (Left) sets the position x: =x - 1; * 'R' (Right) sets the position x: =x + 1. Unfortunately, Mezo's controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position x doesn't change and Mezo simply proceeds to the next command. For example, if Mezo sends commands "LRLR", then here are some possible outcomes (underlined commands are sent successfully): * "LRLR" β€” Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position 0; * "LRLR" β€” Zoma recieves no commands, doesn't move at all and ends up at position 0 as well; * "LRLR" β€” Zoma moves to the left, then to the left again and ends up in position -2. Mezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at. Input The first line contains n (1 ≀ n ≀ 10^5) β€” the number of commands Mezo sends. The second line contains a string s of n commands, each either 'L' (Left) or 'R' (Right). Output Print one integer β€” the number of different positions Zoma may end up at. Example Input 4 LRLR Output 5 Note In the example, Zoma may end up anywhere between -2 and 2. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: t_i β€” the time (in minutes) when the i-th customer visits the restaurant, l_i β€” the lower bound of their preferred temperature range, and h_i β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the i-th customer is satisfied if and only if the temperature is between l_i and h_i (inclusive) in the t_i-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. Input Each test contains one or more test cases. The first line contains the number of test cases q (1 ≀ q ≀ 500). Description of the test cases follows. The first line of each test case contains two integers n and m (1 ≀ n ≀ 100, -10^9 ≀ m ≀ 10^9), where n is the number of reserved customers and m is the initial temperature of the restaurant. Next, n lines follow. The i-th line of them contains three integers t_i, l_i, and h_i (1 ≀ t_i ≀ 10^9, -10^9 ≀ l_i ≀ h_i ≀ 10^9), where t_i is the time when the i-th customer visits, l_i is the lower bound of their preferred temperature range, and h_i is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is 0. Output For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO Note In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: * At 0-th minute, change the state to heating (the temperature is 0). * At 2-nd minute, change the state to off (the temperature is 2). * At 5-th minute, change the state to heating (the temperature is 2, the 1-st customer is satisfied). * At 6-th minute, change the state to off (the temperature is 3). * At 7-th minute, change the state to cooling (the temperature is 3, the 2-nd customer is satisfied). * At 10-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at 0-th minute and leave it be. Then all customers will be satisfied. Note that the 1-st customer's visit time equals the 2-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way... The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s). In one move he can choose two consecutive characters s_i and s_{i+1}, and if s_i is 1 and s_{i + 1} is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing. Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string s as clean as possible. He thinks for two different strings x and y, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner. Now you should answer t test cases: for the i-th test case, print the cleanest possible string that Lee can get by doing some number of moves. Small reminder: if we have two strings x and y of the same length then x is lexicographically smaller than y if there is a position i such that x_1 = y_1, x_2 = y_2,..., x_{i - 1} = y_{i - 1} and x_i < y_i. Input The first line contains the integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Next 2t lines contain test cases β€” one per two lines. The first line of each test case contains the integer n (1 ≀ n ≀ 10^5) β€” the length of the string s. The second line contains the binary string s. The string s is a string of length n which consists only of zeroes and ones. It's guaranteed that sum of n over test cases doesn't exceed 10^5. Output Print t answers β€” one per test case. The answer to the i-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero). Example Input 5 10 0001111111 4 0101 8 11001101 10 1110000000 1 1 Output 0001111111 001 01 0 1 Note In the first test case, Lee can't perform any moves. In the second test case, Lee should erase s_2. In the third test case, Lee can make moves, for example, in the following order: 11001101 β†’ 1100101 β†’ 110101 β†’ 10101 β†’ 1101 β†’ 101 β†’ 01. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). For a positive integer n, we call a permutation p of length n good if the following condition holds for every pair i and j (1 ≀ i ≀ j ≀ n) β€” * (p_i OR p_{i+1} OR … OR p_{j-1} OR p_{j}) β‰₯ j-i+1, where OR denotes the [bitwise OR operation.](https://en.wikipedia.org/wiki/Bitwise_operation#OR) In other words, a permutation p is good if for every subarray of p, the OR of all elements in it is not less than the number of elements in that subarray. Given a positive integer n, output any good permutation of length n. We can show that for the given constraints such a permutation always exists. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first and only line of every test case contains a single integer n (1 ≀ n ≀ 100). Output For every test, output any good permutation of length n on a separate line. Example Input 3 1 3 7 Output 1 3 1 2 4 3 5 2 7 1 6 Note For n = 3, [3,1,2] is a good permutation. Some of the subarrays are listed below. * 3 OR 1 = 3 β‰₯ 2 (i = 1,j = 2) * 3 OR 1 OR 2 = 3 β‰₯ 3 (i = 1,j = 3) * 1 OR 2 = 3 β‰₯ 2 (i = 2,j = 3) * 1 β‰₯ 1 (i = 2,j = 2) Similarly, you can verify that [4,3,5,2,7,1,6] is also good. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tenten runs a weapon shop for ninjas. Today she is willing to sell n shurikens which cost 1, 2, ..., n ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase. Tenten keeps a record for all events, and she ends up with a list of the following types of records: * + means that she placed another shuriken on the showcase; * - x means that the shuriken of price x was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! Input The first line contains the only integer n (1≀ n≀ 10^5) standing for the number of shurikens. The following 2n lines describe the events in the format described above. It's guaranteed that there are exactly n events of the first type, and each price from 1 to n occurs exactly once in the events of the second type. Output If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain n space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. Examples Input 4 + + - 2 + - 3 + - 1 - 4 Output YES 4 2 3 1 Input 1 - 1 + Output NO Input 3 + + + - 2 - 1 - 3 Output NO Note In the first example Tenten first placed shurikens with prices 4 and 2. After this a customer came in and bought the cheapest shuriken which costed 2. Next, Tenten added a shuriken with price 3 on the showcase to the already placed 4-ryo. Then a new customer bought this 3-ryo shuriken. After this she added a 1-ryo shuriken. Finally, the last two customers bought shurikens 1 and 4, respectively. Note that the order [2, 4, 3, 1] is also valid. In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible. In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price 2. This is impossible since the shuriken was not the cheapest, we know that the 1-ryo shuriken was also there. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 a consisting of n integers a_1, a_2, ..., a_n, and an integer x. Your task is to make the sequence a sorted (it is considered sorted if the condition a_1 ≀ a_2 ≀ a_3 ≀ ... ≀ a_n holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer i such that 1 ≀ i ≀ n and a_i > x, and swap the values of a_i and x. For example, if a = [0, 2, 3, 5, 4], x = 1, the following sequence of operations is possible: 1. choose i = 2 (it is possible since a_2 > x), then a = [0, 1, 3, 5, 4], x = 2; 2. choose i = 3 (it is possible since a_3 > x), then a = [0, 1, 2, 5, 4], x = 3; 3. choose i = 4 (it is possible since a_4 > x), then a = [0, 1, 2, 3, 4], x = 5. Calculate the minimum number of operations you have to perform so that a becomes sorted, or report that it is impossible. Input The first line contains one integer t (1 ≀ t ≀ 500) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers n and x (1 ≀ n ≀ 500, 0 ≀ x ≀ 500) β€” the number of elements in the sequence and the initial value of x. The second line contains n integers a_1, a_2, ..., a_n (0 ≀ a_i ≀ 500). The sum of values of n over all test cases in the input does not exceed 500. Output For each test case, print one integer β€” the minimum number of operations you have to perform to make a sorted, or -1, if it is impossible. Example Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array a of length n consisting of integers. You can apply the following operation, consisting of several steps, on the array a zero or more times: * you select two different numbers in the array a_i and a_j; * you remove i-th and j-th elements from the array. For example, if n=6 and a=[1, 6, 1, 1, 4, 4], then you can perform the following sequence of operations: * select i=1, j=5. The array a becomes equal to [6, 1, 1, 4]; * select i=1, j=2. The array a becomes equal to [1, 4]. What can be the minimum size of the array after applying some sequence of operations to it? Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) is length of the array a. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output the minimum possible size of the array after applying some sequence of operations to it. Example Input 5 6 1 6 1 1 4 4 2 1 2 2 1 1 5 4 5 4 5 4 6 2 3 2 1 3 1 Output 0 0 2 1 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written. As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≀ i < j ≀ n, 1 ≀ k ≀ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD". You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7). Input The first input line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters. Output Print the single number β€” the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7). Examples Input 2 3 AAB BAA Output 4 Input 4 5 ABABA BCGDG AAAAA YABSA Output 216 Note In the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 studies divisibility rules at school. Here are some of them: * Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even. * Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3. * Divisibility by 4. A number is divisible by 4 if and only if its last two digits form a number that is divisible by 4. * Divisibility by 5. A number is divisible by 5 if and only if its last digit equals 5 or 0. * Divisibility by 6. A number is divisible by 6 if and only if it is divisible by 2 and 3 simultaneously (that is, if the last digit is even and the sum of all digits is divisible by 3). * Divisibility by 7. Vasya doesn't know such divisibility rule. * Divisibility by 8. A number is divisible by 8 if and only if its last three digits form a number that is divisible by 8. * Divisibility by 9. A number is divisible by 9 if and only if the sum of its digits is divisible by 9. * Divisibility by 10. A number is divisible by 10 if and only if its last digit is a zero. * Divisibility by 11. A number is divisible by 11 if and only if the sum of digits on its odd positions either equals to the sum of digits on the even positions, or they differ in a number that is divisible by 11. Vasya got interested by the fact that some divisibility rules resemble each other. In fact, to check a number's divisibility by 2, 4, 5, 8 and 10 it is enough to check fulfiling some condition for one or several last digits. Vasya calls such rules the 2-type rules. If checking divisibility means finding a sum of digits and checking whether the sum is divisible by the given number, then Vasya calls this rule the 3-type rule (because it works for numbers 3 and 9). If we need to find the difference between the sum of digits on odd and even positions and check whether the difference is divisible by the given divisor, this rule is called the 11-type rule (it works for number 11). In some cases we should divide the divisor into several factors and check whether rules of different types (2-type, 3-type or 11-type) work there. For example, for number 6 we check 2-type and 3-type rules, for number 66 we check all three types. Such mixed divisibility rules are called 6-type rules. And finally, there are some numbers for which no rule works: neither 2-type, nor 3-type, nor 11-type, nor 6-type. The least such number is number 7, so we'll say that in such cases the mysterious 7-type rule works, the one that Vasya hasn't discovered yet. Vasya's dream is finding divisibility rules for all possible numbers. He isn't going to stop on the decimal numbers only. As there are quite many numbers, ha can't do it all by himself. Vasya asked you to write a program that determines the divisibility rule type in the b-based notation for the given divisor d. Input The first input line contains two integers b and d (2 ≀ b, d ≀ 100) β€” the notation system base and the divisor. Both numbers are given in the decimal notation. Output On the first output line print the type of the rule in the b-based notation system, where the divisor is d: "2-type", "3-type", "11-type", "6-type" or "7-type". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line the least number of the last b-based digits that we will need to use to check the divisibility. Examples Input 10 10 Output 2-type 1 Input 2 3 Output 11-type Note The divisibility rule for number 3 in binary notation looks as follows: "A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3". That's an 11-type rule. For example, 2110 = 101012. For it the sum of digits on odd positions equals 1 + 1 + 1 = 3, an on even positions β€” 0 + 0 = 0. The rule works and the number is divisible by 3. In some notations a number can fit into the 3-type rule and the 11-type rule. In this case the correct answer is "3-type". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You've got two rectangular tables with sizes na Γ— ma and nb Γ— mb cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th row and the j-th column, as ai, j; we will define the element of the second table, located at the intersection of the i-th row and the j-th column, as bi, j. We will call the pair of integers (x, y) a shift of the second table relative to the first one. We'll call the overlap factor of the shift (x, y) value: <image> where the variables i, j take only such values, in which the expression ai, jΒ·bi + x, j + y makes sense. More formally, inequalities 1 ≀ i ≀ na, 1 ≀ j ≀ ma, 1 ≀ i + x ≀ nb, 1 ≀ j + y ≀ mb must hold. If there are no values of variables i, j, that satisfy the given inequalities, the value of the sum is considered equal to 0. Your task is to find the shift with the maximum overlap factor among all possible shifts. Input The first line contains two space-separated integers na, ma (1 ≀ na, ma ≀ 50) β€” the number of rows and columns in the first table. Then na lines contain ma characters each β€” the elements of the first table. Each character is either a "0", or a "1". The next line contains two space-separated integers nb, mb (1 ≀ nb, mb ≀ 50) β€” the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table. It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1". Output Print two space-separated integers x, y (|x|, |y| ≀ 109) β€” a shift with maximum overlap factor. If there are multiple solutions, print any of them. Examples Input 3 2 01 10 00 2 3 001 111 Output 0 1 Input 3 3 000 010 000 1 1 1 Output -1 -1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar. Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating). Input The first line contains two integers n and m (2 ≀ n, m ≀ 100) β€” the number of employees and the number of languages. Then n lines follow β€” each employee's language list. At the beginning of the i-th line is integer ki (0 ≀ ki ≀ m) β€” the number of languages the i-th employee knows. Next, the i-th line contains ki integers β€” aij (1 ≀ aij ≀ m) β€” the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces. Output Print a single integer β€” the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating). Examples Input 5 5 1 2 2 2 3 2 3 4 2 4 5 1 5 Output 0 Input 8 7 0 3 1 2 3 1 1 2 5 4 2 6 7 1 3 2 7 4 1 1 Output 2 Input 2 2 1 2 0 Output 1 Note In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4. In the third sample employee 2 must learn language 2. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately. There are n stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter. Input The first line contains integer n (1 ≀ n ≀ 105) β€” amount of mail stamps on the envelope. Then there follow n lines with two integers each β€” description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city. Output Output n + 1 numbers β€” indexes of cities in one of the two possible routes of the letter. Examples Input 2 1 100 100 2 Output 2 100 1 Input 3 3 1 100 2 3 2 Output 100 2 3 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 cube of size k Γ— k Γ— k, which consists of unit cubes. Two unit cubes are considered neighbouring, if they have common face. Your task is to paint each of k3 unit cubes one of two colours (black or white), so that the following conditions must be satisfied: * each white cube has exactly 2 neighbouring cubes of white color; * each black cube has exactly 2 neighbouring cubes of black color. Input The first line contains integer k (1 ≀ k ≀ 100), which is size of the cube. Output Print -1 if there is no solution. Otherwise, print the required painting of the cube consequently by layers. Print a k Γ— k matrix in the first k lines, showing how the first layer of the cube should be painted. In the following k lines print a k Γ— k matrix β€” the way the second layer should be painted. And so on to the last k-th layer. Note that orientation of the cube in the space does not matter. Mark a white unit cube with symbol "w" and a black one with "b". Use the format of output data, given in the test samples. You may print extra empty lines, they will be ignored. Examples Input 1 Output -1 Input 2 Output bb ww bb ww The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF. You are given two strings s1, s2 and another string called virus. Your task is to find the longest common subsequence of s1 and s2, such that it doesn't contain virus as a substring. Input The input contains three strings in three separate lines: s1, s2 and virus (1 ≀ |s1|, |s2|, |virus| ≀ 100). Each string consists only of uppercase English letters. Output Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted. If there is no valid common subsequence, output 0. Examples Input AJKEQSLOBSROFGZ OVGURWZLWVLUXTH OZ Output ORZ Input AA A A Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1. Only one disk can be moved at a time. 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3. No disk may be placed on top of a smaller disk. With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks. (c) Wikipedia. SmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all n disks are on the first rod. Moving a disk from rod i to rod j (1 ≀ i, j ≀ 3) costs tij units of money. The goal of the puzzle is to move all the disks to the third rod. In the problem you are given matrix t and an integer n. You need to count the minimal cost of solving SmallY's puzzle, consisting of n disks. Input Each of the first three lines contains three integers β€” matrix t. The j-th integer in the i-th line is tij (1 ≀ tij ≀ 10000; i β‰  j). The following line contains a single integer n (1 ≀ n ≀ 40) β€” the number of disks. It is guaranteed that for all i (1 ≀ i ≀ 3), tii = 0. Output Print a single integer β€” the minimum cost of solving SmallY's puzzle. Examples Input 0 1 1 1 0 1 1 1 0 3 Output 7 Input 0 2 2 1 0 100 1 2 0 3 Output 19 Input 0 2 1 1 0 100 1 2 0 5 Output 87 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points. Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way. Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109. Input The first line of input contains two space-separated integers n, k (1 ≀ n ≀ 105; 0 ≀ k ≀ 108). Output If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Examples Input 5 2 Output 1 2 3 4 5 Input 5 3 Output 2 4 3 7 1 Input 7 2 Output -1 Note gcd(x, y) is greatest common divisor of x and y. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view. In Cyberland, it is believed that the number "8" is a lucky number (that's why Giga Tower has 8 888 888 888 floors above the ground), and, an integer is lucky, if and only if its decimal notation contains at least one digit "8". For example, 8, - 180, 808 are all lucky while 42, - 10 are not. In the Giga Tower, if you write code at a floor with lucky floor number, good luck will always be with you (Well, this round is #278, also lucky, huh?). Tourist Henry goes to the tower to seek good luck. Now he is at the floor numbered a. He wants to find the minimum positive integer b, such that, if he walks b floors higher, he will arrive at a floor with a lucky number. Input The only line of input contains an integer a ( - 109 ≀ a ≀ 109). Output Print the minimum b in a line. Examples Input 179 Output 1 Input -1 Output 9 Input 18 Output 10 Note For the first sample, he has to arrive at the floor numbered 180. For the second sample, he will arrive at 8. Note that b should be positive, so the answer for the third sample is 10, not 0. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old. They will have dinner around some round tables. You want to distribute foxes such that: 1. Each fox is sitting at some table. 2. Each table has at least 3 foxes sitting around it. 3. The sum of ages of any two adjacent foxes around each table should be a prime number. If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≀ i ≀ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent. If it is possible to distribute the foxes in the desired manner, find out a way to do that. Input The first line contains single integer n (3 ≀ n ≀ 200): the number of foxes in this party. The second line contains n integers ai (2 ≀ ai ≀ 104). Output If it is impossible to do this, output "Impossible". Otherwise, in the first line output an integer m (<image>): the number of tables. Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers β€” indices of fox sitting around that table in clockwise order. If there are several possible arrangements, output any of them. Examples Input 4 3 4 8 9 Output 1 4 1 2 4 3 Input 5 2 2 2 2 2 Output Impossible Input 12 2 3 4 5 6 7 8 9 10 11 12 13 Output 1 12 1 2 3 6 5 12 9 8 7 10 11 4 Input 24 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Output 3 6 1 2 3 6 5 4 10 7 8 9 12 15 14 13 16 11 10 8 17 18 23 22 19 20 21 24 Note In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes. In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Karafs is some kind of vegetable in shape of an 1 Γ— h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. <image> Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) Γ— B. For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero. Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≀ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. Input The first line of input contains three integers A, B and n (1 ≀ A, B ≀ 106, 1 ≀ n ≀ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≀ l, t, m ≀ 106) for i-th query. Output For each query, print its answer in a single line. Examples Input 2 1 4 1 5 3 3 3 10 7 10 2 6 4 8 Output 4 -1 8 -1 Input 1 5 2 1 5 10 2 7 4 Output 1 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 Γ— b1 rectangle, the paintings have shape of a a2 Γ— b2 and a3 Γ— b3 rectangles. Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough? Input The first line contains two space-separated numbers a1 and b1 β€” the sides of the board. Next two lines contain numbers a2, b2, a3 and b3 β€” the sides of the paintings. All numbers ai, bi in the input are integers and fit into the range from 1 to 1000. Output If the paintings can be placed on the wall, print "YES" (without the quotes), and if they cannot, print "NO" (without the quotes). Examples Input 3 2 1 3 2 1 Output YES Input 5 5 3 3 3 3 Output NO Input 4 2 2 3 1 2 Output YES Note That's how we can place the pictures in the first test: <image> And that's how we can do it in the third one. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 mobile application store has a new game called "Subway Roller". The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field. All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel. Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column. <image> Input Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≀ t ≀ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) β€” the number of sets. Then follows the description of t sets of the input data. The first line of the description of each set contains two integers n, k (2 ≀ n ≀ 100, 1 ≀ k ≀ 26) β€” the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains. Output For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise. Examples Input 2 16 4 ...AAAAA........ s.BBB......CCCCC ........DDDDD... 16 4 ...AAAAA........ s.BBB....CCCCC.. .......DDDDD.... Output YES NO Input 2 10 4 s.ZZ...... .....AAABB .YYYYYY... 10 4 s.ZZ...... ....AAAABB .YYYYYY... Output YES NO Note In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel. Note that in this problem the challenges are restricted to tests that contain only one testset. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side. One example of a grid path is (0, 0) β†’ (0, 1) β†’ (0, 2) β†’ (1, 2) β†’ (1, 1) β†’ (0, 1) β†’ ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections. Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares. To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) β†’ (0, 1) β†’ (0, 0) cannot occur in a valid grid path. One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east. Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it. Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths. Input The first line of the input contains a single integer n (2 ≀ n ≀ 1 000 000) β€” the length of the paths. The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') β€” the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW". The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') β€” the second grid path. Output Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive. Examples Input 7 NNESWW SWSWSW Output YES Input 3 NN SS Output NO Note In the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW. In the second sample, no sequence of moves can get both marbles to the end. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. <image> You can preview the image in better quality by the link: [http://assets.codeforces.com/files/656/without-text.png](//assets.codeforces.com/files/656/without-text.png) Input The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character or a full stop ".". Output Output the required answer. Examples Input Codeforces Output -87 Input APRIL.1st Output 17 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given n points on the straight line β€” the positions (x-coordinates) of the cities and m points on the same line β€” the positions (x-coordinates) of the cellular towers. All towers work in the same way β€” they provide cellular network for all cities, which are located at the distance which is no more than r from this tower. Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r. If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower. Input The first line contains two positive integers n and m (1 ≀ n, m ≀ 105) β€” the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≀ bj ≀ 109) β€” the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order. Output Print minimal r so that each city will be covered by cellular network. Examples Input 3 2 -2 2 4 -3 0 Output 4 Input 5 3 1 5 10 14 17 4 11 15 Output 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a string s, consisting of lowercase English letters, and the integer m. One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves. Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order. Formally, we choose a subsequence of indices 1 ≀ i1 < i2 < ... < it ≀ |s|. The selected sequence must meet the following condition: for every j such that 1 ≀ j ≀ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, j + m - 1], i.e. there should exist a k from 1 to t, such that j ≀ ik ≀ j + m - 1. Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt. Find the lexicographically smallest string, that can be obtained using this procedure. Input The first line of the input contains a single integer m (1 ≀ m ≀ 100 000). The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s. Output Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above. Examples Input 3 cbabc Output a Input 2 abcab Output aab Input 3 bcabcbaccba Output aaabb Note In the first sample, one can choose the subsequence {3} and form a string "a". In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black. Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once. Input The first line contains four integers n, k, a and b (1 ≀ k ≀ n ≀ 105, 0 ≀ a, b ≀ n) β€” the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n. Output If it is impossible to drink n cups of tea, print "NO" (without quotes). Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black. If there are multiple answers, print any of them. Examples Input 5 1 3 2 Output GBGBG Input 7 2 2 5 Output BBGBGBB Input 4 3 4 0 Output NO The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits. Vasya wants to know how many pairs of indexes (i, j) are in his sequence so that i < j and the pair of integers ai and aj is k-interesting. Your task is to help Vasya and determine this number. Input The first line contains two integers n and k (2 ≀ n ≀ 105, 0 ≀ k ≀ 14) β€” the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ. The second line contains the sequence a1, a2, ..., an (0 ≀ ai ≀ 104), which Vasya has. Output Print the number of pairs (i, j) so that i < j and the pair of integers ai and aj is k-interesting. Examples Input 4 1 0 3 2 1 Output 4 Input 6 0 200 100 100 100 200 200 Output 6 Note In the first test there are 4 k-interesting pairs: * (1, 3), * (1, 4), * (2, 3), * (2, 4). In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: * (1, 5), * (1, 6), * (2, 3), * (2, 4), * (3, 4), * (5, 6). The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office. The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes is known. Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly k offices, in each of them he wants to gift presents to the employees. The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the i-th lane passes near the office on the x-th crossroad if and only if min(ui, vi) < x < max(ui, vi))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly k - 1 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office. Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty. Input The first line contains two integers n and k (1 ≀ n, k ≀ 80) β€” the number of crossroads (and offices) and the number of offices Oleg wants to visit. The second line contains single integer m (0 ≀ m ≀ 2000) β€” the number of bicycle lanes in Bankopolis. The next m lines contain information about the lanes. The i-th of these lines contains three integers ui, vi and ci (1 ≀ ui, vi ≀ n, 1 ≀ ci ≀ 1000), denoting the crossroads connected by the i-th road and its difficulty. Output In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths. Examples Input 7 4 4 1 6 2 6 2 2 2 4 2 2 7 1 Output 6 Input 4 3 4 2 1 2 1 3 2 3 4 2 4 1 1 Output 3 Note In the first example Oleg visiting banks by path 1 β†’ 6 β†’ 2 β†’ 4. Path 1 β†’ 6 β†’ 2 β†’ 7 with smaller difficulity is incorrect because crossroad 2 β†’ 7 passes near already visited office on the crossroad 6. In the second example Oleg can visit banks by path 4 β†’ 1 β†’ 3. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed. To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity. Hitagi's sequence a has a length of n. Lost elements in it are denoted by zeros. Kaiki provides another sequence b, whose length k equals the number of lost elements in a (i.e. the number of zeros). Hitagi is to replace each zero in a with an element from b so that each element in b should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in a and b more than once in total. If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in a with an integer from b so that each integer from b is used exactly once, and the resulting sequence is not increasing. Input The first line of input contains two space-separated positive integers n (2 ≀ n ≀ 100) and k (1 ≀ k ≀ n) β€” the lengths of sequence a and b respectively. The second line contains n space-separated integers a1, a2, ..., an (0 ≀ ai ≀ 200) β€” Hitagi's broken sequence with exactly k zero elements. The third line contains k space-separated integers b1, b2, ..., bk (1 ≀ bi ≀ 200) β€” the elements to fill into Hitagi's sequence. Input guarantees that apart from 0, no integer occurs in a and b more than once in total. Output Output "Yes" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and "No" otherwise. Examples Input 4 2 11 0 0 14 5 4 Output Yes Input 6 1 2 3 0 8 9 10 5 Output No Input 4 1 8 94 0 4 89 Output Yes Input 7 7 0 0 0 0 0 0 0 1 2 3 4 5 6 7 Output Yes Note In the first sample: * Sequence a is 11, 0, 0, 14. * Two of the elements are lost, and the candidates in b are 5 and 4. * There are two possible resulting sequences: 11, 5, 4, 14 and 11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is "Yes". In the second sample, the only possible resulting sequence is 2, 3, 5, 8, 9, 10, which is an increasing sequence and therefore invalid. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical. Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide. He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements. Input The first line of the input data contains an integer n (1 ≀ n ≀ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109). Output Print on the single line the answer to the problem: the amount of subarrays, which are magical. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Examples Input 4 2 1 1 4 Output 5 Input 5 -2 -2 -2 0 1 Output 8 Note Notes to sample tests: Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end. In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3]. In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3]. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order. For each action several separate windows are singled out: k1 separate windows for the first action (the first type windows), k2 windows for the second one (the second type windows), and k3 for the third one (the third type windows). The service time for one person in any of the first type window equals to t1. Similarly, it takes t2 time to serve a person in any of the second type windows. And it takes t3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa. At some moment n people come to the embassy, the i-th person comes at the moment of time ci. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window. The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly. Input The first line contains three space-separated integers k1, k2, k3 (1 ≀ ki ≀ 109), they are the number of windows of the first, second and third type correspondingly. The second line contains three space-separated integers t1, t2, t3 (1 ≀ ti ≀ 105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly. The third line contains an integer n (1 ≀ n ≀ 105), it is the number of people. The fourth line contains n space-separated integers ci (1 ≀ ci ≀ 109) in the non-decreasing order; ci is the time when the person number i comes to the embassy. Output Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator). Examples Input 1 1 1 1 1 1 5 1 1 1 1 1 Output 7 Input 2 1 1 5 1 1 5 1 2 3 3 5 Output 13 Note In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time). Windows in the second test work like this: The first window of the first type: [1, 6) β€” the first person, [6, 11) β€” third person, [11, 16) β€” fifth person The second window of the first type: [2, 7) β€” the second person, [7, 12) β€” the fourth person The only second type window: [6, 7) β€” first, [7, 8) β€” second, [11, 12) β€” third, [12, 13) β€” fourth, [16, 17) β€” fifth The only third type window: [7, 8) β€” first, [8, 9) β€” second, [12, 13) β€” third, [13, 14) β€” fourth, [17, 18) β€” fifth We can see that it takes most time to serve the fifth person. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research. First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe. Input In first line there is one integer n (1 ≀ n ≀ 2Β·105) β€” number of cafes indices written by Vlad. In second line, n numbers a1, a2, ..., an (0 ≀ ai ≀ 2Β·105) are written β€” indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted. Output Print one integer β€” index of the cafe that Vlad hasn't visited for as long as possible. Examples Input 5 1 3 2 1 2 Output 3 Input 6 2 1 2 2 4 1 Output 2 Note In first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be <image> of them. You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis. Find the minimal number of layers you have to use for the given N. Input The only input line contains a single integer N (1 ≀ N ≀ 100). Output Output a single integer - the minimal number of layers required to draw the segments for the given N. Examples Input 2 Output 2 Input 3 Output 4 Input 4 Output 6 Note As an example, here are the segments and their optimal arrangement into layers for N = 4. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Bob comes to you for candies as you are his favorite coder! He wants X candies. You have N bags and the i^th bag contains A[i] candies. You can give him a set of one or more bags such that the sum of candies in those bags is EXACTLY equal to X. Bob wants to find smallest such set of bags. If there are multiple smallest such sets than Bob wants the lexicographically smallest set of such bags. See the sample test cases for more clarity. Input: The first line contains two space-separated integers N and X. The second line contains N integers, the number of candies in each bag. Output: Print the indices of the bags that you give to Bob. If it is not possible to form such a set, print -1. Constraints: 1 ≀ N ≀ 20 1 ≀ X ≀ 10^6 1 ≀ A[i] ≀ 10^6 SAMPLE INPUT 6 9 5 7 1 4 4 1 SAMPLE OUTPUT 1 4 Explanation There are 3 such sets possible whose sum is 9 : 1 (5) + 4 (4) = 9 1 (5) + 5 (4) = 9 2 (7) + 3 (1) + 6 (1) = 9 Now in the 3rd set there are 3 bags being given but in 1st and 2nd set we can give 2 bags which is less than 3. We don't allot 2nd set because for the second set bags allocated will be 1 and 5 which is lexicographically larger than 1 and 4 in the case of 1st set. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Amer cabs has released a scheme through which a user gets a free drive when he shares a reference code with another Amer app user. Given N number of app users, output total number of free drives gained by all of them. Two same users cannot share reference code more than once. Input Format The first line contains the number of test cases T, T lines follow. Each line then contains an integer N, the total number of Amer App Users. Output Format Print the number of Free Drives for each test-case in a new line. Constraints 1 ≀ T ≀ 1000 0 < N < 10^6 SAMPLE INPUT 2 1 2 SAMPLE OUTPUT 0 1 Explanation Case 1 : Single user shares no reference code, hence 0 free drives. Case 2 : There are 2 app users, so reference code is shared once, hence 1 free drive. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Peter visited Big Bazar and he was very delighted to know the Loot offer on marbles. The offer was that, If he buys a marble with price p', then he will get all other marbles whose price lies between [pβ€²,pβ€²+4] (both inclusive) in free of cost. Suppose there are N marbles and their prices are represented by an array P=[p_1,p_2,p_3......p_N]. So, help Peter to find out an optimal way in which he shall get all the marbles. Input Format: The first line contains an integer N i.e. number of marbles. Next line will contain N integers, p_1,p_2,…,p_N, representing the marble array. Output Format: Minimum units of marble with which Peter could take all marbles to his home. Constraints: 1≀N≀10 ^ 5 40≀ p_i ≀10 ^ 4, where i∈[1,N] SAMPLE INPUT 5 7 3 6 12 18 SAMPLE OUTPUT 3 Explanation When he choose marble of price 3 ,then he will get marbles of price 6 and 7 free with it as, and both (6 and 7) lies in the range (3 + 4 = 7). Now he choose 12 and will get no other marbles free with it as none of marbles price lies in the range (12+ 4 = 16). Now only 1 marble is left i.e., marble of price 17 so he will simply take that also. So, total 3 units are required to grab all marbles. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 are N integers A_1,\ldots,A_N. Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7). Constraints * 2 \leq N \leq 2\times 10^5 * 0 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 \ldots A_N Output Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7). Examples Input 3 1 2 3 Output 11 Input 4 141421356 17320508 22360679 244949 Output 437235829 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2. Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r. Constraints * 1 \leq r \leq 100 * r is an integer. Input Input is given from Standard Input in the following format: r Output Print an integer representing the area of the regular dodecagon. Examples Input 4 Output 48 Input 15 Output 675 Input 80 Output 19200 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 likes the sound when he buys a drink from a vending machine. That sound can be heard by spending A yen (the currency of Japan) each time. Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time. How many times will he hear the sound? Constraints * All values in input are integers. * 1 \leq A, B, C \leq 100 Input Input is given from Standard Input in the following format: A B C Output Print the number of times Takahashi will hear his favorite sound. Examples Input 2 11 4 Output 4 Input 3 9 5 Output 3 Input 100 1 10 Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 strings S and T consisting of lowercase English letters. You can perform the following operation on S any number of times: Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1. Determine if S and T can be made equal by performing the operation zero or more times. Constraints * 1 \leq |S| \leq 2 \times 10^5 * |S| = |T| * S and T consists of lowercase English letters. Input Input is given from Standard Input in the following format: S T Output If S and T can be made equal, print `Yes`; otherwise, print `No`. Examples Input azzel apple Output Yes Input chokudai redcoder Output No Input abcdefghijklmnopqrstuvwxyz ibyhqfrekavclxjstdwgpzmonu Output Yes The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below: * Each person simultaneously divides his cookies in half and gives one half to each of the other two persons. This action will be repeated until there is a person with odd number of cookies in hand. How many times will they repeat this action? Note that the answer may not be finite. Constraints * 1 ≀ A,B,C ≀ 10^9 Input Input is given from Standard Input in the following format: A B C Output Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print `-1` instead. Examples Input 4 12 20 Output 3 Input 14 14 14 Output -1 Input 454 414 444 Output 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Construct an N-gon that satisfies the following conditions: * The polygon is simple (see notes for the definition). * Each edge of the polygon is parallel to one of the coordinate axes. * Each coordinate is an integer between 0 and 10^9, inclusive. * The vertices are numbered 1 through N in counter-clockwise order. * The internal angle at the i-th vertex is exactly a_i degrees. In case there are multiple possible answers, you can output any. Constraints * 3 ≀ N ≀ 1000 * a_i is either 90 or 270. Input The input is given from Standard Input in the following format: N a_1 : a_N Output In case the answer exists, print the answer in the following format: x_1 y_1 : x_N y_N Here (x_i, y_i) are the coordinates of the i-th vertex. In case the answer doesn't exist, print a single `-1`. Examples Input 8 90 90 270 90 90 90 270 90 Output 0 0 2 0 2 1 3 1 3 2 1 2 1 1 0 1 Input 3 90 90 90 Output -1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L. She will concatenate all of the strings in some order, to produce a long string. Among all strings that she can produce in this way, find the lexicographically smallest one. Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds: * There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i. * s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m. Constraints * 1 ≦ N, L ≦ 100 * For each i, the length of S_i equals L. * For each i, S_i consists of lowercase letters. Input The input is given from Standard Input in the following format: N L S_1 S_2 : S_N Output Print the lexicographically smallest string that Iroha can produce. Example Input 3 3 dxx axx cxx Output axxcxxdxx The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Yuta is addicted to the popular game "Beat Panel" at a nearby arcade. The game consists of a total of 16 panel-type buttons, 4x4, arranged in a grid as shown. <image> As shown in the figure, the buttons are arranged in the order of button 1, button 2,…, button 16 from the upper left to the lower right. In the game, you will hear a beat sound at regular intervals and a final sound at the end. Multiple buttons light up at the same time as the beat sounds. In some cases, even one does not shine. The player can press multiple buttons at the same time using the fingers of both hands from immediately after the beat sound to the next sound. It is also possible not to press anything. The game ends as soon as the end sound is heard. Yuta has mastered c ways of pressing, and each time a beat sounds, he decides one of those pressing methods and presses the button. If the buttons you press are lit, those buttons will be lit and the number of buttons that have disappeared will be added to the player's score. Also, once a button is lit, the light will not go out until the button is pressed. A program that outputs the maximum score that Yuta can obtain by inputting how to illuminate the button when the beat sound is played n times and how to press the button in c ways that Yuta has learned. Please create. input The input consists of multiple datasets. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format. n c a1,1 a1,2 ... a1,16 a2,1 a2,2 ... a2,16 ... an, 1 an, 2 ... an, 16 b1,1 b1,2 ... b1,16 b2,1 b2,2 ... b2,16 ... bc,1 bc,2 ... bc,16 The first line consists of two integers separated by one space. n (1 ≀ n ≀ 30) indicates the number of beat sounds, and c (1 ≀ c ≀ 30) indicates the number of button presses that you have learned. The following n + c lines give how to illuminate the button and how to press the button. The input item in the line is separated by one blank. ak and i indicate how the button i shines when the kth beat sound is heard, and bj and i indicate whether the button i is pressed by pressing the jth button in the middle of c. As for the values ​​of ak and i, 0 means "does not shine" and 1 means "shines". For the values ​​of bj and i, 0 means "do not press" and 1 means "press". The number of datasets does not exceed 20. output For each data set, the maximum score is output on one line. Example Input 2 2 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 2 2 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 Output 8 7 16 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Brave Ponta has finally arrived at the final dungeon. This is a dark wilderness in front of the fort of the evil emperor Boromos, with fairly strong monsters guarding their territories. <image> Figure 1: Wilderness As shown in Fig. 1, the wilderness is represented by a 4 Γ— 4 square region with the southwest as the origin (0, 0). Inside this area are monsters, as shown by the dots in the figure. Brave Ponta must cross this area from west to east (W to E). That is, you must start from the point where the x coordinate is 0.0 and the y coordinate is 0.0 or more and 4.0 or less, and move to the point where the x coordinate is 4.0 and the y coordinate is 0.0 or more and 4.0 or less. When an enemy enters the area, the monster attacks the enemy if the distance between him and the enemy is shorter than the distance between any other monster and the enemy. Now that there are not enough items, it was Ponta who really wanted to avoid fighting as much as possible before fighting Boromos. There, Ponta noticed: If you follow the path where there are always two or more monsters with the shortest distance to you, you won't be attacked by monsters. Whether the brave Ponta can reach Boromos unscathed depends on your control. Create a program to find the shortest distance through the dark wilderness. For reference, the shortest path in FIG. 1 is shown in FIG. <image> Figure 2: Shortest path Input Multiple datasets are given as input. Each dataset is given in the following format: n (number of monsters: integer) x1 y1 (Position of the first monster: Real number separated by blanks) x2 y2 (Position of the second monster: Real number separated by blanks) .. .. xn yn (position of nth monster: real number separated by blanks) n is 1 or more and 20 or less. The x and y coordinate values ​​that indicate the position of the monster are 0.0 or more and 4.0 or less. When n is 0, it indicates the end of input. Output Output the shortest distance on one line for each dataset. If you can't cross the wilderness without being attacked by a monster, print "impossible". The distance output may have an error of 0.00001 or less. Example Input 2 1 1 3 3 4 1.0 0.5 1.0 2.5 3.0 1.5 3.0 3.5 1 2.0 2.0 0 Output 5.656854249492 4.618033988750 impossible The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap. You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point. The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy-plane, which is enough because, in this dataset, the z-coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed. <image> Figure G.1: First dataset of the sample input. Input The input is a sequence of datasets. Each dataset is formatted as follows. N M R S1x S1y S1z S1r ... SNx SNy SNz SNr T1x T1y T1z T1b ... TMx TMy TMz TMb Ex Ey Ez The first line of a dataset contains three positive integers, N, M and R, separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N. Each of the N lines following the first line contains four integers separated by a single space. (Six, Siy, Siz) means the center position of the i-th balloon and Sir means its radius. Each of the following M lines contains four integers separated by a single space. (Tjx, Tjy, Tjz) means the position of the j-th light source and Tjb means its brightness. The last line of a dataset contains three integers separated by a single space. (Ex, Ey, Ez) means the position of the objective point. Six, Siy, Siz, Tjx, Tjy, Tjz, Ex, Ey and Ez are greater than -500, and less than 500. Sir is greater than 0, and less than 500. Tjb is greater than 0, and less than 80000. At the objective point, the intensity of the light from the j-th light source is in inverse proportion to the square of the distance, namely Tjb / { (Tjx βˆ’ Ex)2 + (Tjy βˆ’ Ey)2 + (Tjz βˆ’ Ez)2 }, if there is no balloon interrupting the light. The total illumination intensity is the sum of the above. You may assume the following. 1. The distance between the objective point and any light source is not less than 1. 2. For every i and j, even if Sir changes by Ξ΅ (|Ξ΅| < 0.01), whether the i-th balloon hides the j-th light or not does not change. The end of the input is indicated by a line of three zeros. Output For each dataset, output a line containing a decimal fraction which means the highest possible illumination intensity at the objective point after removing R balloons. The output should not contain an error greater than 0.0001. Example Input 12 5 4 0 10 0 1 1 5 0 2 1 4 0 2 0 0 0 2 10 0 0 1 3 -1 0 2 5 -1 0 2 10 10 0 15 0 -10 0 1 10 -10 0 1 -10 -10 0 1 10 10 0 1 0 10 0 240 10 0 0 200 10 -2 0 52 -10 0 0 100 1 1 0 2 0 0 0 12 5 4 0 10 0 1 1 5 0 2 1 4 0 2 0 0 0 2 10 0 0 1 3 -1 0 2 5 -1 0 2 10 10 0 15 0 -10 0 1 10 -10 0 1 -10 -10 0 1 10 10 0 1 0 10 0 260 10 0 0 200 10 -2 0 52 -10 0 0 100 1 1 0 2 0 0 0 5 1 3 1 2 0 2 -1 8 -1 8 -2 -3 5 6 -2 1 3 3 -4 2 3 5 1 1 2 7 0 0 0 5 1 2 1 2 0 2 -1 8 -1 8 -2 -3 5 6 -2 1 3 3 -4 2 3 5 1 1 2 7 0 0 0 0 0 0 Output 3.5 3.6 1.1666666666666667 0.0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Backgorund The super popular game "Puzzle & Hexagons" has finally been released. This game is so funny that many people are addicted to it. There were a number of people who were certified as addicted by doctors because of their excessive enthusiasm. Volunteers from around the world have created a "Puzzle & Hexagons" simulator to help addicts in the game and try to encourage them to avoid playing on dangerous real machines. I want you to cooperate in making a simulator. Problem A board with H squares in the vertical direction and W in the horizontal direction is given. Fig.1 shows the board surface when H = 4 and W = 7, and the coordinates (x, y) of the corresponding squares. <image> Fig.1 In the initial state, each square has a colored block. The color of the block is expressed by one letter of the alphabet as follows. *'R' ・ ・ ・ Red *'G' ・ ・ ・ Green *'B' ・ ・ ・ Blue *'P' ・ ・ ・ Purple *'Y' ・ ・ ・ Yellow *'E' ・ ・ ・ Water Then the number Q of operations is given. Each operation is given the center coordinates of rotation (x, y), indicating that the six blocks around the square are rotated one clockwise. (See Fig.2). At this time, even if the cell does not have a block, it is considered that an empty block exists and is rotated one clockwise. However, if any one of the specified coordinates and the six squares around it does not exist on the H Γ— W board, the rotation is not performed. <image> Fig.2 Next, the process is repeated until the following processing cannot be performed. 1. In Fig.3, when there is no block in any of the squares B, C, and D from the position of block A, block A falls to the position of C. If the squares B and D do not exist, it is considered that the block does not exist, and if the square C does not exist, the fall process is not performed. 2. If there is a block that can process 1, it returns to 1. 3. If three or more blocks of the same color are connected, all the blocks disappear. Connecting two blocks means sharing one side of the square. Note: This series of processing is performed even when no operation is given (initial state). <image> Fig.3 Output the final board after performing all operations. Constraints * 3 ≀ H ≀ 50 * 3 ≀ W ≀ 50 * 0 ≀ x <W * 0 ≀ y <H * 1 ≀ Q ≀ 100 * Fi, j (0 ≀ i <W, 0 ≀ j <H) is one of'R',' G',' B',' P',' Y',' E'. Input The input is given in the following format. H W F0, Hβˆ’1 F1, Hβˆ’1… FWβˆ’1, Hβˆ’1 F0, H-2 F1, H-2 ... FW-1, H-2 .. .. .. F0,0 F1,0… FW-1,0 Q x0 y0 x1 y1 .. .. .. xQβˆ’1 yQβˆ’1 The first line is given two integers H and W that represent the vertical and horizontal sizes of the board. From the second line to the H + 1 line, a character string representing the color of the board corresponding to each subscript is given. The number Q of operations is given on the second line of H +. In the following Q line, x and y representing the coordinates of the cell at the center of rotation are given. Output Output the board surface in line H after performing all operations. However, cells without blocks should be represented by'.'. Examples Input 3 3 RGR RBP YEB 1 1 1 Output … YBG EBP Input 4 5 BYYGG RRRRR RRBRR YYGGB 2 3 1 3 1 Output ..... ..... ..... B.BGB Input 4 4 BEEP ERYY BBRP RBYP 1 1 2 Output .... .... .... .B.. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department. The following problems were described in the pamphlet. > How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5. If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input. Input The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≀ N ≀ 1000. The end of the input is indicated by a single line of zeros. Output The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output. Example Input 9 500 0 Output 2 3 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Example Input 2 2 1 2 0 3 4 1 Output 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Constraints * 1 ≀ |V| ≀ 100 * 0 ≀ |E| ≀ 9900 * -2 Γ— 107 ≀ di ≀ 2 Γ— 107 * There are no parallel edges * There are no self-loops Input An edge-weighted graph G (V, E). |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 |V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge. Output If the graph contains a negative cycle (a cycle whose sum of edge costs is a negative value), print NEGATIVE CYCLE in a line. Otherwise, print D0,0 D0,1 ... D0,|V|-1 D1,0 D1,1 ... D1,|V|-1 : D|V|-1,0 D1,1 ... D|V|-1,|V|-1 The output consists of |V| lines. For each ith line, print the cost of the shortest path from vertex i to each vertex j (j = 0, 1, ... |V|-1) respectively. If there is no path from vertex i to vertex j, print "INF". Print a space between the costs. Examples Input 4 6 0 1 1 0 2 5 1 2 2 1 3 4 2 3 1 3 2 7 Output 0 1 3 4 INF 0 2 3 INF INF 0 1 INF INF 7 0 Input 4 6 0 1 1 0 2 -5 1 2 2 1 3 4 2 3 1 3 2 7 Output 0 1 -5 -4 INF 0 2 3 INF INF 0 1 INF INF 7 0 Input 4 6 0 1 1 0 2 5 1 2 2 1 3 4 2 3 1 3 2 -7 Output NEGATIVE CYCLE The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are standing near a very strange machine. If you put C cents in the machine, the remaining money in your purse will transform in an unusual way. If you have A dollars and B cents remaining in your purse after depositing the C cents, then after the transformation you will have B dollars and A cents. You can repeat this procedure as many times as you want unless you don't have enough money for the machine. If at any point C > B and A > 0, then the machine will allow you to break one of the A dollars into 100 cents so you can place C cents in the machine. The machine will not allow you to exchange a dollar for 100 cents if B >= C. Of course, you want to do this to maximize your profit. For example if C=69 and you have 9 dollars and 77 cents then after you put 69 cents in the machine you will have 8 dollars and 9 cents (9.77 --> 9.08 --> 8.09). But I should warn you that you can't cheat. If you try to throw away 9 cents before the transformation (in order to obtain 99 dollars and 8 cents after), the machine will sense you are cheating and take away all of your money. You need to know how many times you should do this transformation in order to make a maximum profit. Since you are very busy man, you want to obtain the maximum possible profit in the minimum amount of time. Input The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains three nonnegative integers A, B and C where A, B, C < 100. It means that you have A dollars and B cents in your purse and you need to put C cents in the machine to make the transformation. Output For each test case, output a single line containing the minimal number of times you should do this transformation in order to make a maximal profit. It is guaranteed that the answer is less than 10000. Example Input: 2 9 77 69 98 99 69 Output: 4 0 Explanation In the first test we have the following sequence: 9.77, 8.09, 40.07, 38.39, 70.37, 68.69, 0.68. After last step we have not enough money for further transformations. The maximal profit will be after 4 transformations. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Colossal! β€” exclaimed Hawk-nose. β€” A programmer! That's exactly what we are looking for. Arkadi and Boris Strugatsky. Monday starts on Saturday Reading the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta found an interesting equation: a - (a βŠ• x) - x = 0 for some given a, where βŠ• stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some x, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help. Input Each test contains several possible values of a and your task is to find the number of equation's solution for each of them. The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of these values. The following t lines contain the values of parameter a, each value is an integer from 0 to 2^{30} - 1 inclusive. Output For each value of a print exactly one integer β€” the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of a appear in the input. One can show that the number of solutions is always finite. Example Input 3 0 2 1073741823 Output 1 2 1073741824 Note Let's define the bitwise exclusive OR (XOR) operation. Given two integers x and y, consider their binary representations (possibly with leading zeroes): x_k ... x_2 x_1 x_0 and y_k ... y_2 y_1 y_0. Here, x_i is the i-th bit of the number x and y_i is the i-th bit of the number y. Let r = x βŠ• y be the result of the XOR operation of x and y. Then r is defined as r_k ... r_2 r_1 r_0 where: $$$ r_i = \left\{ \begin{aligned} 1, ~ if ~ x_i β‰  y_i \\\ 0, ~ if ~ x_i = y_i \end{aligned} \right. $$$ For the first value of the parameter, only x = 0 is a solution of the equation. For the second value of the parameter, solutions are x = 0 and x = 2. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Hiasat registered a new account in NeckoForces and when his friends found out about that, each one of them asked to use his name as Hiasat's handle. Luckily for Hiasat, he can change his handle in some points in time. Also he knows the exact moments friends will visit his profile page. Formally, you are given a sequence of events of two types: * 1 β€” Hiasat can change his handle. * 2 s β€” friend s visits Hiasat's profile. The friend s will be happy, if each time he visits Hiasat's profile his handle would be s. Hiasat asks you to help him, find the maximum possible number of happy friends he can get. Input The first line contains two integers n and m (1 ≀ n ≀ 10^5, 1 ≀ m ≀ 40) β€” the number of events and the number of friends. Then n lines follow, each denoting an event of one of two types: * 1 β€” Hiasat can change his handle. * 2 s β€” friend s (1 ≀ |s| ≀ 40) visits Hiasat's profile. It's guaranteed, that each friend's name consists only of lowercase Latin letters. It's guaranteed, that the first event is always of the first type and each friend will visit Hiasat's profile at least once. Output Print a single integer β€” the maximum number of happy friends. Examples Input 5 3 1 2 motarack 2 mike 1 2 light Output 2 Input 4 3 1 2 alice 2 bob 2 tanyaromanova Output 1 Note In the first example, the best way is to change the handle to the "motarack" in the first event and to the "light" in the fourth event. This way, "motarack" and "light" will be happy, but "mike" will not. In the second example, you can choose either "alice", "bob" or "tanyaromanova" and only that friend will be happy. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp is going to participate in the contest. It starts at h_1:m_1 and ends at h_2:m_2. It is guaranteed that the contest lasts an even number of minutes (i.e. m_1 \% 2 = m_2 \% 2, where x \% y is x modulo y). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes. Polycarp wants to know the time of the midpoint of the contest. For example, if the contest lasts from 10:00 to 11:00 then the answer is 10:30, if the contest lasts from 11:10 to 11:12 then the answer is 11:11. Input The first line of the input contains two integers h_1 and m_1 in the format hh:mm. The second line of the input contains two integers h_2 and m_2 in the same format (hh:mm). It is guaranteed that 0 ≀ h_1, h_2 ≀ 23 and 0 ≀ m_1, m_2 ≀ 59. It is guaranteed that the contest lasts an even number of minutes (i.e. m_1 \% 2 = m_2 \% 2, where x \% y is x modulo y). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes. Output Print two integers h_3 and m_3 (0 ≀ h_3 ≀ 23, 0 ≀ m_3 ≀ 59) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ':'. Examples Input 10:00 11:00 Output 10:30 Input 11:10 11:12 Output 11:11 Input 01:02 03:02 Output 02:02 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all nodes for which v is the parent. A vertex is a leaf if it has no children. The rooted tree Serval owns has n nodes, node 1 is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation max or min written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. Assume that there are k leaves in the tree. Serval wants to put integers 1, 2, …, k to the k leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him? Input The first line contains an integer n (2 ≀ n ≀ 3β‹… 10^5), the size of the tree. The second line contains n integers, the i-th of them represents the operation in the node i. 0 represents min and 1 represents max. If the node is a leaf, there is still a number of 0 or 1, but you can ignore it. The third line contains n-1 integers f_2, f_3, …, f_n (1 ≀ f_i ≀ i-1), where f_i represents the parent of the node i. Output Output one integer β€” the maximum possible number in the root of the tree. Examples Input 6 1 0 1 1 0 1 1 2 2 2 2 Output 1 Input 5 1 0 1 0 1 1 1 1 1 Output 4 Input 8 1 0 0 1 0 1 1 0 1 1 2 2 3 3 3 Output 4 Input 9 1 1 0 0 1 0 1 0 1 1 1 2 2 3 3 4 4 Output 5 Note Pictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes. In the first example, no matter how you arrange the numbers, the answer is 1. <image> In the second example, no matter how you arrange the numbers, the answer is 4. <image> In the third example, one of the best solution to achieve 4 is to arrange 4 and 5 to nodes 4 and 5. <image> In the fourth example, the best solution is to arrange 5 to node 5. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. At first, there was a legend related to the name of the problem, but now it's just a formal statement. You are given n points a_1, a_2, ..., a_n on the OX axis. Now you are asked to find such an integer point x on OX axis that f_k(x) is minimal possible. The function f_k(x) can be described in the following way: * form a list of distances d_1, d_2, ..., d_n where d_i = |a_i - x| (distance between a_i and x); * sort list d in non-descending order; * take d_{k + 1} as a result. If there are multiple optimal answers you can print any of them. Input The first line contains single integer T ( 1 ≀ T ≀ 2 β‹… 10^5) β€” number of queries. Next 2 β‹… T lines contain descriptions of queries. All queries are independent. The first line of each query contains two integers n, k (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ k < n) β€” the number of points and constant k. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_1 < a_2 < ... < a_n ≀ 10^9) β€” points in ascending order. It's guaranteed that βˆ‘{n} doesn't exceed 2 β‹… 10^5. Output Print T integers β€” corresponding points x which have minimal possible value of f_k(x). If there are multiple answers you can print any of them. Example Input 3 3 2 1 2 5 2 1 1 1000000000 1 0 4 Output 3 500000000 4 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 picture consisting of n rows and m columns. Rows are numbered from 1 to n from the top to the bottom, columns are numbered from 1 to m from the left to the right. Each cell is painted either black or white. You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers x and y, where 1 ≀ x ≀ n and 1 ≀ y ≀ m, such that all cells in row x and all cells in column y are painted black. For examples, each of these pictures contain crosses: <image> The fourth picture contains 4 crosses: at (1, 3), (1, 5), (3, 3) and (3, 5). Following images don't contain crosses: <image> You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black. What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross? You are also asked to answer multiple independent queries. Input The first line contains an integer q (1 ≀ q ≀ 5 β‹… 10^4) β€” the number of queries. The first line of each query contains two integers n and m (1 ≀ n, m ≀ 5 β‹… 10^4, n β‹… m ≀ 4 β‹… 10^5) β€” the number of rows and the number of columns in the picture. Each of the next n lines contains m characters β€” '.' if the cell is painted white and '*' if the cell is painted black. It is guaranteed that βˆ‘ n ≀ 5 β‹… 10^4 and βˆ‘ n β‹… m ≀ 4 β‹… 10^5. Output Print q lines, the i-th line should contain a single integer β€” the answer to the i-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross. Example Input 9 5 5 ..*.. ..*.. ***** ..*.. ..*.. 3 4 **** .*.. .*.. 4 3 *** *.. *.. *.. 5 5 ***** *.*.* ***** ..*.* ..*** 1 4 **** 5 5 ..... ..*.. .***. ..*.. ..... 5 3 ... .*. .*. *** .*. 3 3 .*. *.* .*. 4 4 *.** .... *.** *.** Output 0 0 0 0 0 4 1 1 2 Note The example contains all the pictures from above in the same order. The first 5 pictures already contain a cross, thus you don't have to paint anything. You can paint (1, 3), (3, 1), (5, 3) and (3, 5) on the 6-th picture to get a cross in (3, 3). That'll take you 4 minutes. You can paint (1, 2) on the 7-th picture to get a cross in (4, 2). You can paint (2, 2) on the 8-th picture to get a cross in (2, 2). You can, for example, paint (1, 3), (3, 1) and (3, 3) to get a cross in (3, 3) but that will take you 3 minutes instead of 1. There are 9 possible crosses you can get in minimum time on the 9-th picture. One of them is in (1, 1): paint (1, 2) and (2, 1). The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out. You are given two strings s and t of the same length n. Their characters are numbered from 1 to n from left to right (i.e. from the beginning to the end). In a single move you can do the following sequence of actions: * choose any valid index i (1 ≀ i ≀ n), * move the i-th character of s from its position to the beginning of the string or move the i-th character of s from its position to the end of the string. Note, that the moves don't change the length of the string s. You can apply a move only to the string s. For example, if s="test" in one move you can obtain: * if i=1 and you move to the beginning, then the result is "test" (the string doesn't change), * if i=2 and you move to the beginning, then the result is "etst", * if i=3 and you move to the beginning, then the result is "stet", * if i=4 and you move to the beginning, then the result is "ttes", * if i=1 and you move to the end, then the result is "estt", * if i=2 and you move to the end, then the result is "tste", * if i=3 and you move to the end, then the result is "tets", * if i=4 and you move to the end, then the result is "test" (the string doesn't change). You want to make the string s equal to the string t. What is the minimum number of moves you need? If it is impossible to transform s to t, print -1. Input The first line contains integer q (1 ≀ q ≀ 100) β€” the number of independent test cases in the input. Each test case is given in three lines. The first line of a test case contains n (1 ≀ n ≀ 100) β€” the length of the strings s and t. The second line contains s, the third line contains t. Both strings s and t have length n and contain only lowercase Latin letters. There are no constraints on the sum of n in the test (i.e. the input with q=100 and all n=100 is allowed). Output For every test print minimum possible number of moves, which are needed to transform s into t, or -1, if it is impossible to do. Examples Input 3 9 iredppipe piedpiper 4 estt test 4 tste test Output 2 1 2 Input 4 1 a z 5 adhas dasha 5 aashd dasha 5 aahsd dasha Output -1 2 2 3 Note In the first example, the moves in one of the optimal answers are: * for the first test case s="iredppipe", t="piedpiper": "iredppipe" β†’ "iedppiper" β†’ "piedpiper"; * for the second test case s="estt", t="test": "estt" β†’ "test"; * for the third test case s="tste", t="test": "tste" β†’ "etst" β†’ "test". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a non-empty string s=s_1s_2... s_n, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string s if there is an integer j (1 ≀ j ≀ n-2), that s_{j}s_{j+1}s_{j+2}="one" or s_{j}s_{j+1}s_{j+2}="two". For example: * Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), * Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like s="onetwone", then if Polycarp selects two indices 3 and 6, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? Input The first line of the input contains an integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string s. Its length does not exceed 1.5β‹…10^5. The string s consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed 1.5β‹…10^6. Output Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain r (0 ≀ r ≀ |s|) β€” the required minimum number of positions to be removed, where |s| is the length of the given line. The second line of each answer should contain r different integers β€” the indices themselves for removal in any order. Indices are numbered from left to right from 1 to the length of the string. If r=0, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. Examples Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 Note In the first example, answers are: * "onetwone", * "testme" β€” Polycarp likes it, there is nothing to remove, * "oneoneone", * "twotwo". In the second example, answers are: * "onetwonetwooneooonetwooo", * "two", * "one", * "twooooo", * "ttttwo", * "ttwwoo" β€” Polycarp likes it, there is nothing to remove, * "ooone", * "onnne" β€” Polycarp likes it, there is nothing to remove, * "oneeeee", * "oneeeeeeetwooooo". The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Mishka wants to buy some food in the nearby shop. Initially, he has s burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1 ≀ x ≀ s, buy food that costs exactly x burles and obtain ⌊x/10βŒ‹ burles as a cashback (in other words, Mishka spends x burles and obtains ⌊x/10βŒ‹ back). The operation ⌊a/bβŒ‹ means a divided by b rounded down. It is guaranteed that you can always buy some food that costs x for any possible value of x. Your task is to say the maximum number of burles Mishka can spend if he buys food optimally. For example, if Mishka has s=19 burles then the maximum number of burles he can spend is 21. Firstly, he can spend x=10 burles, obtain 1 burle as a cashback. Now he has s=10 burles, so can spend x=10 burles, obtain 1 burle as a cashback and spend it too. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. The next t lines describe test cases. Each test case is given on a separate line and consists of one integer s (1 ≀ s ≀ 10^9) β€” the number of burles Mishka initially has. Output For each test case print the answer on it β€” the maximum number of burles Mishka can spend if he buys food optimally. Example Input 6 1 10 19 9876 12345 1000000000 Output 1 11 21 10973 13716 1111111111 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have a tree of n vertices. You are going to convert this tree into n rubber bands on infinitely large plane. Conversion rule follows: * For every pair of vertices a and b, rubber bands a and b should intersect if and only if there is an edge exists between a and b in the tree. * Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect. Now let's define following things: * Rubber band a includes rubber band b, if and only if rubber band b is in rubber band a's area, and they don't intersect each other. * Sequence of rubber bands a_{1}, a_{2}, …, a_{k} (k β‰₯ 2) are nested, if and only if for all i (2 ≀ i ≀ k), a_{i-1} includes a_{i}. <image> This is an example of conversion. Note that rubber bands 5 and 6 are nested. It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints. What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it. Input The first line contains integer n (3 ≀ n ≀ 10^{5}) β€” the number of vertices in tree. The i-th of the next n-1 lines contains two integers a_{i} and b_{i} (1 ≀ a_{i} < b_{i} ≀ n) β€” it means there is an edge between a_{i} and b_{i}. It is guaranteed that given graph forms tree of n vertices. Output Print the answer. Examples Input 6 1 3 2 3 3 4 4 5 4 6 Output 4 Input 4 1 2 2 3 3 4 Output 2 Note In the first sample, you can obtain a nested sequence of 4 rubber bands(1, 2, 5, and 6) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of 4 rubber bands. However, you cannot make sequence of 5 or more nested rubber bands with given tree. <image> You can see one of the possible conversions for the second sample below. <image> The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 call two numbers x and y similar if they have the same parity (the same remainder when divided by 2), or if |x-y|=1. For example, in each of the pairs (2, 6), (4, 3), (11, 7), the numbers are similar to each other, and in the pairs (1, 4), (3, 12), they are not. You are given an array a of n (n is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other. For example, for the array a = [11, 14, 16, 12], there is a partition into pairs (11, 12) and (14, 16). The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case consists of two lines. The first line contains an even positive integer n (2 ≀ n ≀ 50) β€” length of array a. The second line contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100). Output For each test case print: * YES if the such a partition exists, * NO otherwise. The letters in the words YES and NO can be displayed in any case. Example Input 7 4 11 14 16 12 2 1 8 4 1 1 1 1 4 1 2 5 6 2 12 13 6 1 6 3 10 5 8 6 1 12 3 10 5 8 Output YES NO YES YES YES YES NO Note The first test case was explained in the statement. In the second test case, the two given numbers are not similar. In the third test case, any partition is suitable. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 warriors in a row. The power of the i-th warrior is a_i. All powers are pairwise distinct. You have two types of spells which you may cast: 1. Fireball: you spend x mana and destroy exactly k consecutive warriors; 2. Berserk: you spend y mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be [2, 3, 7, 8, 11, 5, 4], and k = 3. If you cast Berserk on warriors with powers 8 and 11, the resulting sequence of powers becomes [2, 3, 7, 11, 5, 4]. Then, for example, if you cast Fireball on consecutive warriors with powers [7, 11, 5], the resulting sequence of powers becomes [2, 3, 4]. You want to turn the current sequence of warriors powers a_1, a_2, ..., a_n into b_1, b_2, ..., b_m. Calculate the minimum amount of mana you need to spend on it. Input The first line contains two integers n and m (1 ≀ n, m ≀ 2 β‹… 10^5) β€” the length of sequence a and the length of sequence b respectively. The second line contains three integers x, k, y (1 ≀ x, y, ≀ 10^9; 1 ≀ k ≀ n) β€” the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that all integers a_i are pairwise distinct. The fourth line contains m integers b_1, b_2, ..., b_m (1 ≀ b_i ≀ n). It is guaranteed that all integers b_i are pairwise distinct. Output Print the minimum amount of mana for turning the sequnce a_1, a_2, ..., a_n into b_1, b_2, ..., b_m, or -1 if it is impossible. Examples Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given an array a_1, a_2 ... a_n. Calculate the number of tuples (i, j, k, l) such that: * 1 ≀ i < j < k < l ≀ n; * a_i = a_k and a_j = a_l; Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. The first line of each test case contains a single integer n (4 ≀ n ≀ 3000) β€” the size of the array a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the array a. It's guaranteed that the sum of n in one test doesn't exceed 3000. Output For each test case, print the number of described tuples. Example Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 Note In the first test case, for any four indices i < j < k < l are valid, so the answer is the number of tuples. In the second test case, there are 2 valid tuples: * (1, 2, 4, 6): a_1 = a_4 and a_2 = a_6; * (1, 3, 4, 6): a_1 = a_4 and a_3 = a_6. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 decided to drop the legend about the power sockets but feel free to come up with your own :^) Define a chain: * a chain of length 1 is a single vertex; * a chain of length x is a chain of length x-1 with a new vertex connected to the end of it with a single edge. You are given n chains of lengths l_1, l_2, ..., l_n. You plan to build a tree using some of them. * Each vertex of the tree is either white or black. * The tree initially only has a white root vertex. * All chains initially consist only of white vertices. * You can take one of the chains and connect any of its vertices to any white vertex of the tree with an edge. The chain becomes part of the tree. Both endpoints of this edge become black. * Each chain can be used no more than once. * Some chains can be left unused. The distance between two vertices of the tree is the number of edges on the shortest path between them. If there is at least k white vertices in the resulting tree, then the value of the tree is the distance between the root and the k-th closest white vertex. What's the minimum value of the tree you can obtain? If there is no way to build a tree with at least k white vertices, then print -1. Input The first line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5, 2 ≀ k ≀ 10^9) β€” the number of chains and the minimum number of white vertices a tree should have to have a value. The second line contains n integers l_1, l_2, ..., l_n (3 ≀ l_i ≀ 2 β‹… 10^5) β€” the lengths of the chains. Output Print a single integer. If there is no way to build a tree with at least k white vertices, then print -1. Otherwise, print the minimum value the tree can have. Examples Input 1 2 3 Output 2 Input 3 3 4 3 3 Output 3 Input 3 5 4 3 4 Output 4 Input 2 10 5 7 Output -1 Note <image> You are allowed to not use all the chains, so it's optimal to only use chain of length 4 in the second example. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper. A permutation p is written from left to right on the paper. First Qingshan chooses an integer index x (1≀ x≀ n) and tells it to Daniel. After that, Daniel chooses another integer index y (1≀ y≀ n, y β‰  x). The game progresses turn by turn and as usual, Qingshan moves first. The rules follow: * If it is Qingshan's turn, Qingshan must change x to such an index x' that 1≀ x'≀ n, |x'-x|=1, x'β‰  y, and p_{x'}<p_x at the same time. * If it is Daniel's turn, Daniel must change y to such an index y' that 1≀ y'≀ n, |y'-y|=1, y'β‰  x, and p_{y'}>p_y at the same time. The person who can't make her or his move loses, and the other wins. You, as Qingshan's fan, are asked to calculate the number of possible x to make Qingshan win in the case both players play optimally. Input The first line contains a single integer n (2≀ n≀ 10^5) β€” the length of the permutation. The second line contains n distinct integers p_1,p_2,...,p_n (1≀ p_i≀ n) β€” the permutation. Output Print the number of possible values of x that Qingshan can choose to make her win. Examples Input 5 1 2 5 4 3 Output 1 Input 7 1 2 4 6 5 3 7 Output 0 Note In the first test case, Qingshan can only choose x=3 to win, so the answer is 1. In the second test case, if Qingshan will choose x=4, Daniel can choose y=1. In the first turn (Qingshan's) Qingshan chooses x'=3 and changes x to 3. In the second turn (Daniel's) Daniel chooses y'=2 and changes y to 2. Qingshan can't choose x'=2 because y=2 at this time. Then Qingshan loses. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created. We assume that Bajtek can only heap up snow drifts at integer coordinates. Input The first line of input contains a single integer n (1 ≀ n ≀ 100) β€” the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≀ xi, yi ≀ 1000) β€” the coordinates of the i-th snow drift. Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct. Output Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one. Examples Input 2 2 1 1 2 Output 1 Input 2 2 1 4 1 Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Overall there are m actors in Berland. Each actor has a personal identifier β€” an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors. Vasya looks at his records and wonders which movies may be his favourite, and which ones may not be. Once Vasya learns the exact cast of all movies, his favorite movies will be determined as follows: a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors. Help the boy to determine the following for each movie: * whether it surely will be his favourite movie; * whether it surely won't be his favourite movie; * can either be favourite or not. Input The first line of the input contains two integers m and k (1 ≀ m ≀ 100, 1 ≀ k ≀ m) β€” the number of actors in Berland and the number of Vasya's favourite actors. The second line contains k distinct integers ai (1 ≀ ai ≀ m) β€” the identifiers of Vasya's favourite actors. The third line contains a single integer n (1 ≀ n ≀ 100) β€” the number of movies in Vasya's list. Then follow n blocks of lines, each block contains a movie's description. The i-th movie's description contains three lines: * the first line contains string si (si consists of lowercase English letters and can have the length of from 1 to 10 characters, inclusive) β€” the movie's title, * the second line contains a non-negative integer di (1 ≀ di ≀ m) β€” the number of actors who starred in this movie, * the third line has di integers bi, j (0 ≀ bi, j ≀ m) β€” the identifiers of the actors who star in this movie. If bi, j = 0, than Vasya doesn't remember the identifier of the j-th actor. It is guaranteed that the list of actors for a movie doesn't contain the same actors. All movies have distinct names. The numbers on the lines are separated by single spaces. Output Print n lines in the output. In the i-th line print: * 0, if the i-th movie will surely be the favourite; * 1, if the i-th movie won't surely be the favourite; * 2, if the i-th movie can either be favourite, or not favourite. Examples Input 5 3 1 2 3 6 firstfilm 3 0 0 0 secondfilm 4 0 0 4 5 thirdfilm 1 2 fourthfilm 1 5 fifthfilm 1 4 sixthfilm 2 1 0 Output 2 2 1 1 1 2 Input 5 3 1 3 5 4 jumanji 3 0 0 0 theeagle 5 1 2 3 4 0 matrix 3 2 4 0 sourcecode 2 2 4 Output 2 0 1 1 Note Note to the second sample: * Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. * Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. * Movie matrix can have exactly one favourite actor. * Movie sourcecode doesn't have any favourite actors. Thus, movie theeagle will surely be favourite, movies matrix and sourcecode won't surely be favourite, and movie jumanji can be either favourite (if it has all three favourite actors), or not favourite. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good. Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions: * The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≀ i ≀ k - 1). * No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≀ i ≀ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q). * All elements of the sequence are good integers. Find the length of the longest good sequence. Input The input consists of two lines. The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≀ ai ≀ 105; ai < ai + 1). Output Print a single integer β€” the length of the longest good sequence. Examples Input 5 2 3 4 6 9 Output 4 Input 9 1 2 3 5 6 7 8 9 10 Output 4 Note In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≀ pi ≀ n). Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes to the house whose number is written on the plaque of house x (that is, to house px), then he goes to the house whose number is written on the plaque of house px (that is, to house ppx), and so on. We know that: 1. When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1. 2. When the penguin starts walking from any house indexed from k + 1 to n, inclusive, he definitely cannot walk to house number 1. 3. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house. You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7). Input The single line contains two space-separated integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ min(8, n)) β€” the number of the houses and the number k from the statement. Output In a single line print a single integer β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 5 2 Output 54 Input 7 4 Output 1728 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration. <image> Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information? Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places. Input The first line contains two integers n and m (2 ≀ n ≀ 105; 0 ≀ m ≀ 105) β€” the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≀ ui, vi ≀ n; ui β‰  vi), these integers describe a road between places ui and vi. The next line contains n integers: x1, x2, ..., xn (0 ≀ xi ≀ 1) β€” the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times. Output Output the number of visited places k in the first line (0 ≀ k ≀ 4n). Then output k integers β€” the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct. If there is no required path, output -1. If there multiple possible paths, you can output any of them. Examples Input 3 2 1 2 2 3 1 1 1 Output 3 1 2 3 Input 5 7 1 2 1 3 1 4 1 5 3 4 3 5 4 5 0 1 0 1 0 Output 10 2 1 3 4 5 4 5 4 3 1 Input 2 0 0 0 Output 0 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m. What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition? Input The single line contains two space separated integers n, m (0 < n ≀ 10000, 1 < m ≀ 10). Output Print a single integer β€” the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print - 1 instead. Examples Input 10 2 Output 6 Input 3 5 Output -1 Note For the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}. For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying a + b = ?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base p and regard the expression as written in the base p positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78 + 87 in the base 16 positional notation is equal to FF16, in the base 15 positional notation it is equal to 11015, in the base 10 one β€” to 16510, in the base 9 one β€” to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length. The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78 + 87 = ? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones. Input The first letter contains two space-separated numbers a and b (1 ≀ a, b ≀ 1000) which represent the given summands. Output Print a single number β€” the length of the longest answer. Examples Input 78 87 Output 3 Input 1 1 Output 2 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network. We know that each server takes one second to recompress a one minute fragment. Thus, any server takes m seconds to recompress a m minute video. We know the time when each of the n videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time s, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video. For each video find the moment it stops being recompressed. Input The first line of the input contains integers n and k (1 ≀ n, k ≀ 5Β·105) β€” the number of videos and servers, respectively. Next n lines contain the descriptions of the videos as pairs of integers si, mi (1 ≀ si, mi ≀ 109), where si is the time in seconds when the i-th video appeared and mi is its duration in minutes. It is guaranteed that all the si's are distinct and the videos are given in the chronological order of upload, that is in the order of increasing si. Output Print n numbers e1, e2, ..., en, where ei is the time in seconds after the servers start working, when the i-th video will be recompressed. Examples Input 3 2 1 5 2 5 3 5 Output 6 7 11 Input 6 1 1 1000000000 2 1000000000 3 1000000000 4 1000000000 5 1000000000 6 3 Output 1000000001 2000000001 3000000001 4000000001 5000000001 5000000004 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4). The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length d3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house. <image> Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled. Input The first line of the input contains three integers d1, d2, d3 (1 ≀ d1, d2, d3 ≀ 108) β€” the lengths of the paths. * d1 is the length of the path connecting Patrick's house and the first shop; * d2 is the length of the path connecting Patrick's house and the second shop; * d3 is the length of the path connecting both shops. Output Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house. Examples Input 10 20 30 Output 60 Input 1 1 5 Output 4 Note The first sample is shown on the picture in the problem statement. One of the optimal routes is: house <image> first shop <image> second shop <image> house. In the second sample one of the optimal routes is: house <image> first shop <image> house <image> second shop <image> house. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep". Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number. In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait β€” powers of all the people in it are distinct. Help Shapur find out how weak the Romans are. Input The first line of input contains a single number n (3 ≀ n ≀ 106) β€” the number of men in Roman army. Next line contains n different positive integers ai (1 ≀ i ≀ n, 1 ≀ ai ≀ 109) β€” powers of men in the Roman army. Output A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 3 3 2 1 Output 1 Input 3 2 3 1 Output 0 Input 4 10 8 3 1 Output 4 Input 4 1 5 4 3 Output 1 The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island. The island can be conventionally represented as two rectangles a Γ— b and c Γ— d unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of a and one of the sides with the length of c lie on one line. You can see this in more details on the picture. <image> The i-th party is entitled to a part of the island equal to xi unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A "connected figure" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party. Your task is to divide the island between parties. Input The first line contains 5 space-separated integers β€” a, b, c, d and n (1 ≀ a, b, c, d ≀ 50, b β‰  d, 1 ≀ n ≀ 26). The second line contains n space-separated numbers. The i-th of them is equal to number xi (1 ≀ xi ≀ a Γ— b + c Γ— d). It is guaranteed that <image>. Output If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print max(b, d) lines each containing a + c characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use "a", for the second one use "b" and so on. Use "." for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle a Γ— b. The last symbol of the second line should correspond to the square that belongs to the rectangle c Γ— d. If there are several solutions output any. Examples Input 3 4 2 2 3 5 8 3 Output YES aaabb aabbb cbb.. ccb.. Input 3 2 1 4 4 1 2 3 4 Output YES abbd cccd ...d ...d The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do 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 di (1 ≀ di ≀ 109) β€” 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 &gt;&lt; 1 2 Output FINITE Input 3 &gt;&gt;&lt; 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. The input will be give Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.