question
stringlengths
31
7.12k
source
stringclasses
5 values
dataset
stringclasses
3 values
index
stringlengths
1
5
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls. Each class must present two couples to the ball. In Vasya's class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs. Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair. For example, if a=3, b=4, k=4 and the couples (1, 2), (1, 3), (2, 2), (3, 4) are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below): * (1, 3) and (2, 2); * (3, 4) and (1, 3); But the following combinations are not possible: * (1, 3) and (1, 2) β€” the first boy enters two pairs; * (1, 2) and (2, 2) β€” the second girl enters two pairs; Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers a, b and k (1 ≀ a, b, k ≀ 2 β‹… 10^5) β€” the number of boys and girls in the class and the number of couples ready to dance together. The second line of each test case contains k integers a_1, a_2, … a_k. (1 ≀ a_i ≀ a), where a_i is the number of the boy in the pair with the number i. The third line of each test case contains k integers b_1, b_2, … b_k. (1 ≀ b_i ≀ b), where b_i is the number of the girl in the pair with the number i. It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2 β‹… 10^5. It is guaranteed that each pair is specified at most once in one test case. Output For each test case, on a separate line print one integer β€” the number of ways to choose two pairs that match the condition above. Example Input 3 3 4 4 1 1 2 3 2 3 2 4 1 1 1 1 1 2 2 4 1 1 2 2 1 2 1 2 Output 4 0 2 Note In the first test case, the following combinations of pairs fit: * (1, 2) and (3, 4); * (1, 3) and (2, 2); * (1, 3) and (3, 4); * (2, 2) and (3, 4). There is only one pair in the second test case. In the third test case, the following combinations of pairs fit: * (1, 1) and (2, 2); * (1, 2) and (2, 1).
codeforces
code_contests
1385
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array a. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices x, y, z, w such that a_x + a_y = a_z + a_w. Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery? Input The first line contains the single integer n (4 ≀ n ≀ 200 000) β€” the size of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 2.5 β‹… 10^6). Output Print "YES" if there are such four indices, and "NO" otherwise. If such indices exist, print these indices x, y, z and w (1 ≀ x, y, z, w ≀ n). If there are multiple answers, print any of them. Examples Input 6 2 1 5 2 7 4 Output YES 2 3 1 6 Input 5 1 3 1 9 20 Output NO Note In the first example a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6. Note that there are other answer, for example, 2 3 4 6. In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3
codeforces
code_contests
1386
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: * the final tournament features n teams (n is always even) * the first n / 2 teams (according to the standings) come through to the knockout stage * the standings are made on the following principle: for a victory a team gets 3 points, for a draw β€” 1 point, for a defeat β€” 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place β€” in decreasing order of the difference between scored and missed goals; in the third place β€” in the decreasing order of scored goals * it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage. Input The first input line contains the only integer n (1 ≀ n ≀ 50) β€” amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following nΒ·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 β€” names of the teams; num1, num2 (0 ≀ num1, num2 ≀ 100) β€” amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once. Output Output n / 2 lines β€” names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity. Examples Input 4 A B C D A-B 1:1 A-C 2:2 A-D 1:0 B-C 1:0 B-D 0:3 C-D 0:3 Output A D Input 2 a A a-A 2:1 Output a
codeforces
taco
20218
Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique. We call the man with a number a a 1-ancestor of the man with a number b, if the man with a number a is a direct ancestor of the man with a number b. We call the man with a number a a k-ancestor (k > 1) of the man with a number b, if the man with a number b has a 1-ancestor, and the man with a number a is a (k - 1)-ancestor of the 1-ancestor of the man with a number b. In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an x-ancestor of himself, for some x, x > 0). We call a man with a number a the k-son of the man with a number b, if the man with a number b is a k-ancestor of the man with a number a. Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote m pairs of numbers vi, ki. Help him to learn for each pair vi, ki the number of distinct names among all names of the ki-sons of the man with number vi. Input The first line of the input contains a single integer n (1 ≀ n ≀ 105) β€” the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string si and integer ri (0 ≀ ri ≀ n), where si is the name of the man with a number i, and ri is either the number of the direct ancestor of the man with a number i or 0, if the man with a number i has no direct ancestor. The next line contains a single integer m (1 ≀ m ≀ 105) β€” the number of Polycarpus's records. Next m lines contain space-separated pairs of integers. The i-th line contains integers vi, ki (1 ≀ vi, ki ≀ n). It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters. Output Print m whitespace-separated integers β€” the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input. Examples Input 6 pasha 0 gerald 1 gerald 1 valera 2 igor 3 olesya 1 5 1 1 1 2 1 3 3 1 6 1 Output 2 2 0 1 0 Input 6 valera 0 valera 1 valera 1 gerald 0 valera 4 kolya 4 7 1 1 1 2 2 1 2 2 4 1 5 1 6 1 Output 1 0 0 0 2 0 0
codeforces
taco
21535
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment. For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place. Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima. -----Input----- The first line contains integer n (1 ≀ n ≀ 100) β€” the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show. The numbers in the lines are separated by a single space. -----Output----- In a single line print the answer to the problem. -----Examples----- Input 1 1 Output 3 Input 1 2 Output 2 Input 2 3 5 Output 3 -----Note----- In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend. In the second sample Dima can show 2 or 4 fingers.
codeforces
apps
883
One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms. Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k. Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat. Help Greg with this problem. Input The first line contains two integers n, k (1 ≀ n ≀ 50, 1 ≀ k ≀ 5000) β€” the number of people, including Greg, and the boat's weight limit. The next line contains n integers β€” the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some way. Output In the first line print an integer β€” the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109 + 7). If transporting everyone to the other bank is impossible, print integer 0. Examples Input 1 50 50 Output 1 1 Input 3 100 50 50 100 Output 5 2 Input 2 50 50 50 Output -1 0 Note In the first test Greg walks alone and consequently, he needs only one ride across the river. In the second test you should follow the plan: 1. transport two 50 kg. people; 2. transport one 50 kg. person back; 3. transport one 100 kg. person; 4. transport one 50 kg. person back; 5. transport two 50 kg. people. That totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways.
codeforces
taco
1886
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member of NFC. One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC. The complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c and b > d. You are given a binary number of length n named x. We know that member i from MDC dances with member <image> from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109 + 7). Expression <image> denotes applying Β«XORΒ» to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as Β«^Β», in Pascal β€” Β«xorΒ». Input The first line of input contains a binary number x of lenght n, (1 ≀ n ≀ 100). This number may contain leading zeros. Output Print the complexity of the given dance assignent modulo 1000000007 (109 + 7). Examples Input 11 Output 6 Input 01 Output 2 Input 1 Output 1
codeforces
taco
282
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements: 1. one resistor; 2. an element and one resistor plugged in sequence; 3. an element and one resistor plugged in parallel. <image> With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals <image>. In this case Re equals the resistance of the element being connected. Mike needs to assemble an element with a resistance equal to the fraction <image>. Determine the smallest possible number of resistors he needs to make such an element. Input The single input line contains two space-separated integers a and b (1 ≀ a, b ≀ 1018). It is guaranteed that the fraction <image> is irreducible. It is guaranteed that a solution always exists. Output Print a single number β€” the answer to the problem. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specifier. Examples Input 1 1 Output 1 Input 3 2 Output 3 Input 199 200 Output 200 Note In the first sample, one resistor is enough. In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance <image>. We cannot make this element using two resistors.
codeforces
taco
1477
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, <image> , where aj is the taste of the j-th chosen fruit and bj is its calories. Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem β€” now the happiness of a young couple is in your hands! Inna loves Dima very much so she wants to make the salad from at least one fruit. Input The first line of the input contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ 10). The second line of the input contains n integers a1, a2, ..., an (1 ≀ ai ≀ 100) β€” the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 100) β€” the fruits' calories. Fruit number i has taste ai and calories bi. Output If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer β€” the maximum possible sum of the taste values of the chosen fruits. Examples Input 3 2 10 8 1 2 7 1 Output 18 Input 5 3 4 4 4 4 4 2 2 2 2 2 Output -1 Note In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition <image> fulfills, that's exactly what Inna wants. In the second test sample we cannot choose the fruits so as to follow Inna's principle.
codeforces
code_contests
1397
Two chess pieces, a rook and a knight, stand on a standard chessboard 8 Γ— 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one. Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board beat another one. A new piece can only be placed on an empty square. Input The first input line contains the description of the rook's position on the board. This description is a line which is 2 in length. Its first symbol is a lower-case Latin letter from a to h, and its second symbol is a number from 1 to 8. The second line contains the description of the knight's position in a similar way. It is guaranteed that their positions do not coincide. Output Print a single number which is the required number of ways. Examples Input a1 b2 Output 44 Input a8 d4 Output 38
codeforces
taco
10261
Not so long ago as a result of combat operations the main Berland place of interest β€” the magic clock β€” was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture: <image> The picture shows only the central part of the clock. This coloring naturally extends to infinity. The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball. All the points located on the border of one of the areas have to be considered painted black. Input The first and single line contains two integers x and y β€” the coordinates of the hole made in the clock by the ball. Each of the numbers x and y has an absolute value that does not exceed 1000. Output Find the required color. All the points between which and the origin of coordinates the distance is integral-value are painted black. Examples Input -2 1 Output white Input 2 1 Output black Input 4 3 Output black
codeforces
taco
24119
In Berland prime numbers are fashionable β€” the respectable citizens dwell only on the floors with numbers that are prime numbers. The numismatists value particularly high the coins with prime nominal values. All the prime days are announced holidays! Yet even this is not enough to make the Berland people happy. On the main street of the capital stand n houses, numbered from 1 to n. The government decided to paint every house a color so that the sum of the numbers of the houses painted every color is a prime number. However it turned out that not all the citizens approve of this decision β€” many of them protest because they don't want many colored houses on the capital's main street. That's why it is decided to use the minimal possible number of colors. The houses don't have to be painted consecutively, but every one of n houses should be painted some color. The one-colored houses should not stand consecutively, any way of painting is acceptable. There are no more than 5 hours left before the start of painting, help the government find the way when the sum of house numbers for every color is a prime number and the number of used colors is minimal. Input The single input line contains an integer n (2 ≀ n ≀ 6000) β€” the number of houses on the main streets of the capital. Output Print the sequence of n numbers, where the i-th number stands for the number of color for house number i. Number the colors consecutively starting from 1. Any painting order is allowed. If there are several solutions to that problem, print any of them. If there's no such way of painting print the single number -1. Examples Input 8 Output 1 2 2 1 1 1 1 2
codeforces
taco
6198
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime. You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≀ a < b < c ≀ r. More specifically, you need to find three numbers (a, b, c), such that l ≀ a < b < c ≀ r, pairs (a, b) and (b, c) are coprime, and pair (a, c) is not coprime. -----Input----- The single line contains two positive space-separated integers l, r (1 ≀ l ≀ r ≀ 10^18; r - l ≀ 50). -----Output----- Print three positive space-separated integers a, b, cΒ β€” three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order. If the counterexample does not exist, print the single number -1. -----Examples----- Input 2 4 Output 2 3 4 Input 10 11 Output -1 Input 900000000000000009 900000000000000029 Output 900000000000000009 900000000000000010 900000000000000021 -----Note----- In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are. In the second sample you cannot form a group of three distinct integers, so the answer is -1. In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.
codeforces
apps
563
Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea. Amr has n instruments, it takes a_{i} days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments. Amr asked for your help to distribute his free days between instruments so that he can achieve his goal. -----Input----- The first line contains two numbers n, k (1 ≀ n ≀ 100, 0 ≀ k ≀ 10 000), the number of instruments and number of days respectively. The second line contains n integers a_{i} (1 ≀ a_{i} ≀ 100), representing number of days required to learn the i-th instrument. -----Output----- In the first line output one integer m representing the maximum number of instruments Amr can learn. In the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order. if there are multiple optimal solutions output any. It is not necessary to use all days for studying. -----Examples----- Input 4 10 4 3 1 2 Output 4 1 2 3 4 Input 5 6 4 3 1 1 2 Output 3 1 3 4 Input 1 3 4 Output 0 -----Note----- In the first test Amr can learn all 4 instruments. In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}. In the third test Amr doesn't have enough time to learn the only presented instrument.
codeforces
apps
636
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on. Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active. Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake. -----Input----- The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of gears. The second line contains n digits a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ n - 1) β€” the sequence of active teeth: the active tooth of the i-th gear contains number a_{i}. -----Output----- In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise. -----Examples----- Input 3 1 0 0 Output Yes Input 5 4 2 1 4 3 Output Yes Input 4 0 2 3 1 Output No -----Note----- In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
codeforces
apps
923
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties. Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares. The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even. To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number. Input The first line of the input contains a single integer n (2 ≀ n ≀ 5000) β€” the number of squares in the capital of Berland. Next n - 1 lines contain the pairs of integers x, y (1 ≀ x, y ≀ n, x β‰  y) β€” the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even. Output Print a single number β€” the minimum number of roads connecting the squares with demonstrations of different parties. Examples Input 8 1 4 2 4 3 4 6 5 7 5 8 5 4 5 Output 1 Input 5 1 2 1 3 1 4 1 5 Output 2
codeforces
taco
5643
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≀ n, m ≀ 1 000 000, n + m > 0)Β β€” the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
codeforces
apps
186
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi). They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula <image>. The success of the operation relies on the number of pairs (i, j) (1 ≀ i < j ≀ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs. Input The first line of the input contains the single integer n (1 ≀ n ≀ 200 000) β€” the number of watchmen. Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≀ 109). Some positions may coincide. Output Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel. Examples Input 3 1 1 7 5 1 5 Output 2 Input 6 0 0 0 1 0 2 -1 1 0 1 1 1 Output 11 Note In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and <image> for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
codeforces
taco
9377
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help. You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process. 1. First element a_1 becomes the root of the tree. 2. Elements a_2, a_3, …, a_n are added one by one. To add element a_i one needs to traverse the tree starting from the root and using the following rules: 1. The pointer to the current node is set to the root. 2. If a_i is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node. 3. If at some point there is no required child, the new node is created, it is assigned value a_i and becomes the corresponding child of the current node. Input The first line of the input contains a single integer n (2 ≀ n ≀ 100 000) β€” the length of the sequence a. The second line contains n distinct integers a_i (1 ≀ a_i ≀ 10^9) β€” the sequence a itself. Output Output n - 1 integers. For all i > 1 print the value written in the node that is the parent of the node with value a_i in it. Examples Input 3 1 2 3 Output 1 2 Input 5 4 2 3 1 6 Output 4 2 2 4
codeforces
code_contests
1410
While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used). Suppose the cache may store no more than k objects. At the beginning of the workflow the cache is empty. When some object is queried we check if it is present in the cache and move it here if it's not. If there are more than k objects in the cache after this, the least recently used one should be removed. In other words, we remove the object that has the smallest time of the last query. Consider there are n videos being stored on the server, all of the same size. Cache can store no more than k videos and caching algorithm described above is applied. We know that any time a user enters the server he pick the video i with probability pi. The choice of the video is independent to any events before. The goal of this problem is to count for each of the videos the probability it will be present in the cache after 10100 queries. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 20) β€” the number of videos and the size of the cache respectively. Next line contains n real numbers pi (0 ≀ pi ≀ 1), each of them is given with no more than two digits after decimal point. It's guaranteed that the sum of all pi is equal to 1. Output Print n real numbers, the i-th of them should be equal to the probability that the i-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 3 1 0.3 0.2 0.5 Output 0.3 0.2 0.5 Input 2 1 0.0 1.0 Output 0.0 1.0 Input 3 2 0.3 0.2 0.5 Output 0.675 0.4857142857142857 0.8392857142857143 Input 3 3 0.2 0.3 0.5 Output 1.0 1.0 1.0
codeforces
taco
15313
Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle. Vasya has an edge cactus, each edge of this graph has some color. Vasya would like to remove the minimal number of edges in such way that his cactus turned to a tree. Vasya wants to make it in such a way that there were edges of as many different colors in the resulting tree, as possible. Help him to find how many different colors can the resulting tree have. Input The first line contains two integers: n, m (2 ≀ n ≀ 10 000) β€” the number of vertices and the number of edges in Vasya's graph, respectively. The following m lines contain three integers each: u, v, c (1 ≀ u, v ≀ n, u β‰  v, 1 ≀ c ≀ m) β€” the numbers of vertices connected by the corresponding edge, and its color. It is guaranteed that the described graph is indeed an edge cactus. Output Output one integer: the maximal number of different colors that the resulting tree can have. Examples Input 4 4 1 2 4 2 3 1 3 4 2 4 2 3 Output 3 Input 7 9 1 2 1 2 3 4 3 1 5 1 4 5 4 5 2 5 1 6 1 6 4 6 7 6 7 1 3 Output 6
codeforces
taco
11317
Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins. Across all the sets Misha scored a points in total, and Vanya scored b points. Given this information, determine the maximum number of sets they could have played, or that the situation is impossible. Note that the game consisted of several complete sets. -----Input----- The first line contains three space-separated integers k, a and b (1 ≀ k ≀ 10^9, 0 ≀ a, b ≀ 10^9, a + b > 0). -----Output----- If the situation is impossible, print a single number -1. Otherwise, print the maximum possible number of sets. -----Examples----- Input 11 11 5 Output 1 Input 11 2 3 Output -1 -----Note----- Note that the rules of the game in this problem differ from the real table tennis game, for example, the rule of "balance" (the winning player has to be at least two points ahead to win a set) has no power within the present problem.
codeforces
apps
3750
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle... Everybody was upset. The beautiful Belle was upset, so was the Beast, so was Lumiere the candlestick. But the worst thing was that Cogsworth was upset. Cogsworth is not a human, but is the mantel clock, which was often used as an alarm clock. Due to Cogsworth's frustration all the inhabitants of the castle were in trouble: now they could not determine when it was time to drink morning tea, and when it was time for an evening stroll. Fortunately, deep in the basement are lying digital clock showing the time in the format HH:MM. Now the residents of the castle face a difficult task. They should turn Cogsworth's hour and minute mustache hands in such a way, that Cogsworth began to show the correct time. Moreover they need to find turn angles in degrees for each mustache hands. The initial time showed by Cogsworth is 12:00. You can only rotate the hands forward, that is, as is shown in the picture: <image> As since there are many ways too select such angles because of full rotations, choose the smallest angles in the right (non-negative) direction. Note that Cogsworth's hour and minute mustache hands move evenly and continuously. Hands are moving independently, so when turning one hand the other hand remains standing still. Input The only line of input contains current time according to the digital clock, formatted as HH:MM (00 ≀ HH ≀ 23, 00 ≀ MM ≀ 59). The mantel clock initially shows 12:00. Pretests contain times of the beginning of some morning TV programs of the Channel One Russia. Output Print two numbers x and y β€” the angles of turning the hour and minute hands, respectively (0 ≀ x, y < 360). The absolute or relative error in the answer should not exceed 10 - 9. Examples Input 12:00 Output 0 0 Input 04:30 Output 135 180 Input 08:17 Output 248.5 102 Note A note to the second example: the hour hand will be positioned exactly in the middle, between 4 and 5.
codeforces
taco
990
<image> Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner. Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As for other digits, Krakozyabra dislikes them; moreover, they often cause it indigestion! So, as a necessary precaution, Krakozyabra prefers to sort the digits of a number in non-descending order before proceeding to feast. Then, the leading zeros of the resulting number are eaten and the remaining part is discarded as an inedible tail. For example, if Krakozyabra is to have the number 57040 for dinner, its inedible tail would be the number 457. Slastyona is not really fond of the idea of Krakozyabra living in her castle. Hovewer, her natural hospitality prevents her from leaving her guest without food. Slastyona has a range of natural numbers from L to R, which she is going to feed the guest with. Help her determine how many distinct inedible tails are going to be discarded by Krakozyabra by the end of the dinner. Input In the first and only string, the numbers L and R are given – the boundaries of the range (1 ≀ L ≀ R ≀ 1018). Output Output the sole number – the answer for the problem. Examples Input 1 10 Output 9 Input 40 57 Output 17 Input 157 165 Output 9 Note In the first sample case, the inedible tails are the numbers from 1 to 9. Note that 10 and 1 have the same inedible tail – the number 1. In the second sample case, each number has a unique inedible tail, except for the pair 45, 54. The answer to this sample case is going to be (57 - 40 + 1) - 1 = 17.
codeforces
taco
18080