question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of 5 = 101_2 and 14 = 1110_2 equals to 3, since 0101 and 1110 differ in 3 positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from 0 to n. That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers. | Input: ['5', '5', '7', '11', '1', '2000000000000', ''] Output:['8', '11', '19', '1', '3999999999987', ''] | [
2,
3
] |
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.There is a set S containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer k and replace each element s of the set S with s \oplus k (\oplus denotes the exclusive or operation). Help him choose such k that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set \{1, 2, 3\} equals to set \{2, 1, 3\}.Formally, find the smallest positive integer k such that \{s \oplus k | s \in S\} = S or report that there is no such number.For example, if S = \{1, 3, 4\} and k = 2, new set will be equal to \{3, 1, 6\}. If S = \{0, 1, 2, 3\} and k = 1, after playing set will stay the same. | Input: ['6', '4', '1 0 2 3', '6', '10 7 14 8 3 12', '2', '0 2', '3', '1 2 3', '6', '1 4 6 10 11 12', '2', '0 1023', ''] Output:['1', '4', '2', '-1', '-1', '1023', ''] | [
0
] |
Johnny has a new toy. As you may guess, it is a little bit extraordinary. The toy is a permutation P of numbers from 1 to n, written in one row next to each other. For each i from 1 to n - 1 between P_i and P_{i + 1} there is a weight W_i written, and those weights form a permutation of numbers from 1 to n - 1. There are also extra weights W_0 = W_n = 0.The instruction defines subsegment [L, R] as good if W_{L - 1} < W_i and W_R < W_i for any i in \{L, L + 1, ..., R - 1\}. For such subsegment it also defines W_M as minimum of set \{W_L, W_{L + 1}, ..., W_{R - 1}\}. Now the fun begins. In one move, the player can choose one of the good subsegments, cut it into [L, M] and [M + 1, R] and swap the two parts. More precisely, before one move the chosen subsegment of our toy looks like: W_{L - 1}, P_L, W_L, ..., W_{M - 1}, P_M, W_M, P_{M + 1}, W_{M + 1}, ..., W_{R - 1}, P_R, W_R and afterwards it looks like this: W_{L - 1}, P_{M + 1}, W_{M + 1}, ..., W_{R - 1}, P_R, W_M, P_L, W_L, ..., W_{M - 1}, P_M, W_R Such a move can be performed multiple times (possibly zero), and the goal is to achieve the minimum number of inversions in P. Johnny's younger sister Megan thinks that the rules are too complicated, so she wants to test her brother by choosing some pair of indices X and Y, and swapping P_X and P_Y (X might be equal Y). After each sister's swap, Johnny wonders, what is the minimal number of inversions that he can achieve, starting with current P and making legal moves?You can assume that the input is generated randomly. P and W were chosen independently and equiprobably over all permutations; also, Megan's requests were chosen independently and equiprobably over all pairs of indices. | Input: ['3', '3 2 1', '2 1', '3', '1 3', '3 2', '3 1', ''] Output:['0', '1', '0', ''] | [
3
] |
James Bond, Johnny's favorite secret agent, has a new mission. There are n enemy bases, each of them is described by its coordinates so that we can think about them as points in the Cartesian plane. The bases can communicate with each other, sending a signal, which is the ray directed from the chosen point to the origin or in the opposite direction. The exception is the central base, which lies at the origin and can send a signal in any direction. When some two bases want to communicate, there are two possible scenarios. If they lie on the same line with the origin, one of them can send a signal directly to the other one. Otherwise, the signal is sent from the first base to the central, and then the central sends it to the second base. We denote the distance between two bases as the total Euclidean distance that a signal sent between them has to travel.Bond can damage all but some k bases, which he can choose arbitrarily. A damaged base can't send or receive the direct signal but still can pass it between two working bases. In particular, James can damage the central base, and the signal can still be sent between any two undamaged bases as before, so the distance between them remains the same. What is the maximal sum of the distances between all pairs of remaining bases that 007 can achieve by damaging exactly n - k of them? | Input: ['6 2', '0 0', '1 1', '2 2', '3 3', '0 1', '0 2', ''] Output:['6.24264069', ''] | [
2,
3
] |
Johnny's younger sister Megan had a birthday recently. Her brother has bought her a box signed as "Your beautiful necklace — do it yourself!". It contains many necklace parts and some magic glue. The necklace part is a chain connecting two pearls. Color of each pearl can be defined by a non-negative integer. The magic glue allows Megan to merge two pearls (possibly from the same necklace part) into one. The beauty of a connection of pearls in colors u and v is defined as follows: let 2^k be the greatest power of two dividing u \oplus v — exclusive or of u and v. Then the beauty equals k. If u = v, you may assume that beauty is equal to 20.Each pearl can be combined with another at most once. Merging two parts of a necklace connects them. Using the glue multiple times, Megan can finally build the necklace, which is a cycle made from connected necklace parts (so every pearl in the necklace is combined with precisely one other pearl in it). The beauty of such a necklace is the minimum beauty of a single connection in it. The girl wants to use all available necklace parts to build exactly one necklace consisting of all of them with the largest possible beauty. Help her! | Input: ['5', '13 11', '11 1', '3 5', '17 1', '9 27', ''] Output:['3', '8 7 9 10 5 6 1 2 3 4 ', ''] | [
4
] |
Johnny has just found the new, great tutorial: "How to become a grandmaster?". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems. The boy has found an online judge with tasks divided by topics they cover. He has picked p^{k_i} problems from i-th category (p is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced.Formally, given n numbers p^{k_i}, the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo 10^{9}+7. | Input: ['4', '5 2', '2 3 4 4 3', '3 1', '2 10 1000', '4 5', '0 1 1 100', '1 8', '89', ''] Output:['4', '1', '146981438', '747093407', ''] | [
2,
3
] |
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you? | Input: ['3 3', '1 2', '2 3', '3 1', '2 1 3', ''] Output:['2 1 3', ''] | [
2
] |
Consider all binary strings of length m (1 <= m <= 60). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly 2^m such strings in total.The string s is lexicographically smaller than the string t (both have the same length m) if in the first position i from the left in which they differ, we have s[i] < t[i]. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second.We remove from this set n (1 <= n <= \min(2^m-1, 100)) distinct binary strings a_1, a_2, ..., a_n, each of length m. Thus, the set will have k=2^m-n strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary).We number all the strings after sorting from 0 to k-1. Print the string whose index is \lfloor \frac{k-1}{2} \rfloor (such an element is called median), where \lfloor x \rfloor is the rounding of the number down to the nearest integer.For example, if n=3, m=3 and a=[010, 111, 001], then after removing the strings a_i and sorting, the result will take the form: [000, 011, 100, 101, 110]. Thus, the desired median is 100. | Input: ['5', '3 3', '010', '001', '111', '4 3', '000', '111', '100', '011', '1 1', '1', '1 1', '0', '3 2', '00', '01', '10', ''] Output:['100', '010', '0', '1', '11', ''] | [
0,
4
] |
You are given four positive integers n, m, a, b (1 <= b <= n <= 50; 1 <= a <= m <= 50). Find any such rectangular matrix of size n * m that satisfies all of the following conditions: each row of the matrix contains exactly a ones; each column of the matrix contains exactly b ones; all other elements are zeros. If the desired matrix does not exist, indicate this.For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} | Input: ['5', '3 6 2 1', '2 2 2 1', '2 2 2 2', '4 4 2 2', '2 1 1 2', ''] Output:['YES', '010001', '100100', '001010', 'NO', 'YES', '11', '11', 'YES', '1100', '1100', '0011', '0011', 'YES', '1', '1', ''] | [
2,
3
] |
You are given n strings a_1, a_2, ..., a_n: all of them have the same length m. The strings consist of lowercase English letters.Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j]!=s[j].Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings.For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. | Input: ['5', '2 4', 'abac', 'zbab', '2 4', 'aaaa', 'bbbb', '3 3', 'baa', 'aaa', 'aab', '2 2', 'ab', 'bb', '3 1', 'a', 'b', 'c', ''] Output:['abab', '-1', 'aaa', 'ab', 'z'] | [
0
] |
Polycarp wants to buy exactly n shovels. The shop sells packages with shovels. The store has k types of packages: the package of the i-th type consists of exactly i shovels (1 <= i <= k). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly n shovels?For example, if n=8 and k=7, then Polycarp will buy 2 packages of 4 shovels.Help Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly n shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from 1 to k, inclusive. | Input: ['5', '8 7', '8 1', '6 10', '999999733 999999732', '999999733 999999733', ''] Output:['2', '8', '1', '999999733', '1', ''] | [
3
] |
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: ['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', ''] | [
2
] |
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete — the athlete number i has the strength s_i.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |\max(A) - \min(B)| is as small as possible, where \max(A) is the maximum strength of an athlete from team A, and \min(B) is the minimum strength of an athlete from team B.For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: first team: A = [1, 2, 4], second team: B = [3, 6]. In this case, the value |\max(A) - \min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.Print the minimum value |\max(A) - \min(B)|. | Input: ['5', '5', '3 1 2 6 4', '6', '2 1 3 2 4 3', '4', '7 9 3 1', '2', '1 1000', '3', '100 150 200', ''] Output:['1', '0', '2', '999', '50', ''] | [
2
] |
Find the minimum area of a square land on which you can place two identical rectangular a * b houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths a and b (1 <= a, b <= 100) — positive integers (you are given just the sizes, but not their positions). Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square. Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding. The picture shows a square that contains red and green rectangles. | Input: ['8', '3 2', '4 2', '1 1', '3 1', '4 7', '1 3', '7 4', '100 100', ''] Output:['16', '16', '4', '9', '64', '9', '64', '40000', ''] | [
2,
3
] |
You know, it's hard to conduct a show with lots of participants and spectators at the same place nowadays. Still, you are not giving up on your dream to make a car crash showcase! You decided to replace the real cars with remote controlled ones, call the event "Remote Control Kaboom Show" and stream everything online.For the preparation you arranged an arena — an infinite 2D-field. You also bought n remote controlled cars and set them up on the arena. Unfortunately, the cars you bought can only go forward without turning left, right or around. So you additionally put the cars in the direction you want them to go.To be formal, for each car i (1 <= i <= n) you chose its initial position (x_i, y_i) and a direction vector (dx_i, dy_i). Moreover, each car has a constant speed s_i units per second. So after car i is launched, it stars moving from (x_i, y_i) in the direction (dx_i, dy_i) with constant speed s_i.The goal of the show is to create a car collision as fast as possible! You noted that launching every car at the beginning of the show often fails to produce any collisions at all. Thus, you plan to launch the i-th car at some moment t_i. You haven't chosen t_i, that's yet to be decided. Note that it's not necessary for t_i to be integer and t_i is allowed to be equal to t_j for any i, j.The show starts at time 0. The show ends when two cars i and j (i!=j) collide (i. e. come to the same coordinate at the same time). The duration of the show is the time between the start and the end.What's the fastest crash you can arrange by choosing all t_i? If it's possible to arrange a crash then print the shortest possible duration of the show. Otherwise, report that it's impossible. | Input: ['4', '3 -1 -1 1 2', '2 3 -3 -2 10', '-4 2 1 -2 1', '-2 -2 -1 2 4', ''] Output:['0.585902082262898', ''] | [
0,
3,
4
] |
We define x \bmod y as the remainder of division of x by y (\% operator in C++ or Java, mod operator in Pascal).Let's call an array of positive integers [a_1, a_2, ..., a_k] stable if for every permutation p of integers from 1 to k, and for every non-negative integer x, the following condition is met: (((x \bmod a_1) \bmod a_2) ... \bmod a_{k - 1}) \bmod a_k = (((x \bmod a_{p_1}) \bmod a_{p_2}) ... \bmod a_{p_{k - 1}}) \bmod a_{p_k} That is, for each non-negative integer x, the value of (((x \bmod a_1) \bmod a_2) ... \bmod a_{k - 1}) \bmod a_k does not change if we reorder the elements of the array a.For two given integers n and k, calculate the number of stable arrays [a_1, a_2, ..., a_k] such that 1 <= a_1 < a_2 < ... < a_k <= n. | Input: ['7 3', ''] Output:['16', ''] | [
3
] |
There are two infinite sources of water: hot water of temperature h; cold water of temperature c (c < h). You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water ... and so on ... Note that you always start with the cup of hot water.The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. | Input: ['3', '30 10 20', '41 15 30', '18 13 18', ''] Output:['2', '7', '1', ''] | [
3,
4
] |
You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of n * m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement.You are given the picture of the squares: if a_{i,j} = "*", then the j-th square in the i-th row should be black; if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: 1 * 1 tiles — each tile costs x burles and covers exactly 1 square; 1 * 2 tiles — each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 * 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.What is the smallest total price of the tiles needed to cover all the white squares? | Input: ['4', '1 1 10 1', '.', '1 2 10 1', '..', '2 1 10 1', '.', '.', '3 3 3 7', '..*', '*..', '.*.', ''] Output:['10', '1', '20', '18', ''] | [
0,
2
] |
The game of Berland poker is played with a deck of n cards, m of which are jokers. k players play this game (n is divisible by k).At the beginning of the game, each player takes \frac{n}{k} cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to x - y, where x is the number of jokers in the winner's hand, and y is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get 0 points.Here are some examples: n = 8, m = 3, k = 2. If one player gets 3 jokers and 1 plain card, and another player gets 0 jokers and 4 plain cards, then the first player is the winner and gets 3 - 0 = 3 points; n = 4, m = 2, k = 4. Two players get plain cards, and the other two players get jokers, so both of them are winners and get 0 points; n = 9, m = 6, k = 3. If the first player gets 3 jokers, the second player gets 1 joker and 2 plain cards, and the third player gets 2 jokers and 1 plain card, then the first player is the winner, and he gets 3 - 2 = 1 point; n = 42, m = 0, k = 7. Since there are no jokers, everyone gets 0 jokers, everyone is a winner, and everyone gets 0 points. Given n, m and k, calculate the maximum number of points a player can get for winning the game. | Input: ['4', '8 3 2', '4 2 4', '9 6 3', '42 0 7', ''] Output:['3', '0', '1', '0', ''] | [
0,
2,
3
] |
Oh, no!The coronavirus has caught you, and now you're sitting in a dark cellar, with tied legs (but not hands). You have a delicious cookie, a laptop in front of you, and your ideal development environment is open. The coronavirus convinces you to solve the following problem.You are given two arrays A and B of size n. You can do operations of two types with array A: Reverse array A. That is the array [A_1,\ A_2,\ ...,\ A_n] transformes into [A_n,\ A_{n-1},\ ...,\ A_1]. Replace A with an array of its prefix sums. That is, the array [A_1,\ A_2,\ ...,\ A_n] goes to [A_1,\ (A_1+A_2),\ ...,\ (A_1+A_2+...+A_n)]. You need to understand if you can get an array B from the array A. If it is possible, you will have to restore the order of these operations by minimizing the number of operations of the second type. Fortunately, the coronavirus is good today, so he has allowed you not to restore actions if the minimum number of second type operations is more than 2\cdot 10^5. But coronavirus resents you, so if you restore the answer, the total number of operations should not exceed 5\cdot 10^5.Solve this problem and get the cookie, or the coronavirus will extend the quarantine for five years and make the whole economy collapse! | Input: ['2', '5 7', '5 7', ''] Output:['SMALL', '0', '', ''] | [
2,
4
] |
Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months — in the i-th month the company had income equal to a_i (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the first \lceil \tfrac{n}{2} \rceil months income might have been completely unstable, but then everything stabilized and for the last \lfloor \tfrac{n}{2} \rfloor months the income was the same.Levian decided to tell the directors n-k+1 numbers — the total income of the company for each k consecutive months. In other words, for each i between 1 and n-k+1 he will say the value a_i + a_{i+1} + ... + a_{i + k - 1}. For example, if a=[-1, 0, 1, 2, 2] and k=3 he will say the numbers 0, 3, 5.Unfortunately, if at least one total income reported by Levian is not a profit (income <= 0), the directors will get angry and fire the failed accountant.Save Levian's career: find any such k, that for each k months in a row the company had made a profit, or report that it is impossible. | Input: ['3', '2 -1', '2', ''] Output:['2'] | [
2
] |
You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly x days and that's the exact number of days you will spend visiting your friend. You will spend exactly x consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are n months in a year, i-th month lasts exactly d_i days. Days in the i-th month are numbered from 1 to d_i. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get j hugs if you visit Coronavirus-chan on the j-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | Input: ['3 2', '1 3 1', ''] Output:['5'] | [
0,
2,
4
] |
During the quarantine, Sicromoft has more free time to create the new functions in "Celex-2021". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows: The cell with coordinates (x, y) is at the intersection of x-th row and y-th column. Upper left cell (1,1) contains an integer 1.The developers of the SUM function don't sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell (x,y) in one step you can move to the cell (x+1, y) or (x, y+1). After another Dinwows update, Levian started to study "Celex-2021" (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell (x_1, y_1) to another given cell (x_2, y_2), if you can only move one cell down or right.Formally, consider all the paths from the cell (x_1, y_1) to cell (x_2, y_2) such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths. | Input: ['4', '1 1 2 2', '1 2 2 4', '179 1 179 100000', '5 7 5 7', ''] Output:['2', '3', '1', '1', ''] | [
3
] |
Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has n friends who are also grannies (Maria is not included in this number). The i-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least a_i other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny i agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to a_i.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is 1). All the remaining n grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least a_i other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the i-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than a_i other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if n=6 and a=[1,5,4,5,1,9], then: at the first step Maria can call grannies with numbers 1 and 5, each of them will see two grannies at the moment of going out into the yard (note that a_1=1 <= 2 and a_5=1 <= 2); at the second step, Maria can call grannies with numbers 2, 3 and 4, each of them will see five grannies at the moment of going out into the yard (note that a_2=5 <= 5, a_3=4 <= 5 and a_4=5 <= 5); the 6-th granny cannot be called into the yard — therefore, the answer is 6 (Maria herself and another 5 grannies). | Input: ['4', '5', '1 1 2 2 1', '6', '2 3 4 5 6 7', '6', '1 5 4 5 1 9', '5', '1 2 3 5 6', ''] Output:['6', '1', '6', '4', ''] | [
2
] |
Due to the coronavirus pandemic, city authorities obligated citizens to keep a social distance. The mayor of the city Semyon wants to light up Gluharniki park so that people could see each other even at night to keep the social distance.The park is a rectangular table with n rows and m columns, where the cells of the table are squares, and the boundaries between the cells are streets. External borders are also streets. Every street has length 1. For example, park with n=m=2 has 12 streets.You were assigned to develop a plan for lighting the park. You can put lanterns in the middle of the streets. The lamp lights two squares near it (or only one square if it stands on the border of the park). The park sizes are: n=4, m=5. The lighted squares are marked yellow. Please note that all streets have length 1. Lanterns are placed in the middle of the streets. In the picture not all the squares are lit. Semyon wants to spend the least possible amount of money on lighting but also wants people throughout the park to keep a social distance. So he asks you to find the minimum number of lanterns that are required to light all the squares. | Input: ['5', '1 1', '1 3', '2 2', '3 3', '5 3', ''] Output:['1', '2', '2', '5', '8', ''] | [
2,
3
] |
You have to restore the wall. The wall consists of N pillars of bricks, the height of the i-th pillar is initially equal to h_{i}, the height is measured in number of bricks. After the restoration all the N pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is A; remove a brick from the top of one non-empty pillar, the cost of this operation is R; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is M.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 0.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height? | Input: ['3 1 100 100', '1 3 8', ''] Output:['12', ''] | [
2,
3,
4,
4
] |
Petya and Vasya are competing with each other in a new interesting game as they always do.At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 <=q K <=q S.In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that. | Input: ['1 4', ''] Output:['YES', '4', '2'] | [
3
] |
Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where A <=q B <=q C <=q D. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that A <=q x <=q B <=q y <=q C <=q z <=q D holds?Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.The triangle is called non-degenerate if and only if its vertices are not collinear. | Input: ['1 2 3 4', ''] Output:['4', ''] | [
3,
4
] |
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter e_i — his inexperience. Russell decided that an explorer with inexperience e can only join the group of e or more people.Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him. | Input: ['2', '3', '1 1 1', '5', '2 3 1 2 2', ''] Output:['3', '2', ''] | [
2
] |
Let's define the following recurrence: a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.Your task is calculate a_{K} for given a_{1} and K. | Input: ['8', '1 4', '487 1', '487 2', '487 3', '487 4', '487 5', '487 6', '487 7', ''] Output:['42', '487', '519', '528', '544', '564', '588', '628', ''] | [
0,
3
] |
This is an interactive problem. Don't forget to flush output after printing queries using cout.flush() or fflush(stdout) in C++ or similar functions in other programming languages.There are n gift boxes in a row, numbered from 1 to n from left to right. It's known that exactly k of them contain valuable gifts — other boxes contain just lucky stones. All boxes look the same and differ only in weight. All boxes with stones have the same weight and are strictly heavier than boxes with valuable items. But valuable gifts may be different, so the boxes with valuable items may have different weights.You can ask no more than 50 queries (printing an answer doesn't count). By each query you can compare total weights of two non-intersecting subsets of boxes a_1, a_2, ..., a_{k_a} and b_1, b_2, ..., b_{k_b}. In return you'll get one of four results: FIRST, if subset a_1, a_2, ..., a_{k_a} is strictly heavier; SECOND, if subset b_1, b_2, ..., b_{k_b} is strictly heavier; EQUAL, if subsets have equal total weights; WASTED, if the query is incorrect or the limit of queries is exceeded. Using such queries (or, maybe, intuition) find the box with a valuable gift with the minimum index. | Input: ['2', '2 1', '-', '-', '-', 'FIRST', '-', '5 2', '-', '-', '-', 'FIRST', '-', '-', '-', 'SECOND', '-', '-', '-', 'EQUAL', '-'] Output:['-', '-', '? 1 1', '1', '2', '-', '! 2', '-', '? 1 1', '1', '2', '-', '? 2 3', '4 2', '1 3 5', '-', '? 1 1', '4', '5', '-', '! 1'] | [
4
] |
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other.Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order.Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once.Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed).Help Polycarp to make up a plan of actions to summon the strongest possible army! | Input: ['3', '5 2', '5 3', '7 0', '5 0', '4 0', '10 0', '2 1', '10 100', '50 10', '5 5', '1 5', '2 4', '3 3', '4 2', '5 1', ''] Output:['4', '2 1 -1 5', '1', '2', '5', '5 4 3 2 1', ''] | [
2
] |
Note that the memory limit is unusual.You are given a multiset consisting of n integers. You have to process queries of two types: add integer k into the multiset; find the k-th order statistics in the multiset and remove it. k-th order statistics in the multiset is the k-th element in the sorted list of all elements of the multiset. For example, if the multiset contains elements 1, 4, 2, 1, 4, 5, 7, and k = 3, then you have to find the 3-rd element in [1, 1, 2, 4, 4, 5, 7], which is 2. If you try to delete an element which occurs multiple times in the multiset, only one occurence is removed. After processing all queries, print any number belonging to the multiset, or say that it is empty. | Input: ['5 5', '1 2 3 4 5', '-1 -1 -1 -1 -1', ''] Output:['0', ''] | [
4
] |
The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, n is always even, and in C2, n is always odd.You are given a regular polygon with 2 \cdot n vertices (it's convex and has equal sides and equal angles) and all its sides have length 1. Let's name it as 2n-gon.Your task is to find the square of the minimum size such that you can embed 2n-gon in the square. Embedding 2n-gon in the square means that you need to place 2n-gon in the square in such way that each point which lies inside or on a border of 2n-gon should also lie inside or on a border of the square.You can rotate 2n-gon and/or the square. | Input: ['3', '3', '5', '199', ''] Output:['1.931851653', '3.196226611', '126.687663595', ''] | [
0,
3,
4
] |
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, n is always even, and in C2, n is always odd.You are given a regular polygon with 2 \cdot n vertices (it's convex and has equal sides and equal angles) and all its sides have length 1. Let's name it as 2n-gon.Your task is to find the square of the minimum size such that you can embed 2n-gon in the square. Embedding 2n-gon in the square means that you need to place 2n-gon in the square in such way that each point which lies inside or on a border of 2n-gon should also lie inside or on a border of the square.You can rotate 2n-gon and/or the square. | Input: ['3', '2', '4', '200', ''] Output:['1.000000000', '2.414213562', '127.321336469', ''] | [
3,
4,
4
] |
You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once.A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. | Input: ['7', '123', '12222133333332', '112233', '332211', '12121212', '333333', '31121', ''] Output:['3', '3', '4', '4', '0', '0', '4', ''] | [
4
] |
Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least a minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in b minutes.Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than a minutes in total, then he sets his alarm to go off in c minutes after it is reset and spends d minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another c minutes and tries to fall asleep for d minutes again.You just want to find out when will Polycarp get out of his bed or report that it will never happen.Please check out the notes for some explanations of the example. | Input: ['7', '10 3 6 4', '11 3 6 4', '5 9 4 10', '6 5 2 3', '1 1 1 1', '3947465 47342 338129 123123', '234123843 13 361451236 361451000', ''] Output:['27', '27', '9', '-1', '1', '6471793', '358578060125049', ''] | [
3
] |
You are playing one famous sandbox game with the three-dimensional world. The map of the world can be represented as a matrix of size n * m, where the height of the cell (i, j) is a_{i, j}.You are in the cell (1, 1) right now and want to get in the cell (n, m). You can move only down (from the cell (i, j) to the cell (i + 1, j)) or right (from the cell (i, j) to the cell (i, j + 1)). There is an additional restriction: if the height of the current cell is x then you can move only to the cell with height x+1.Before the first move you can perform several operations. During one operation, you can decrease the height of any cell by one. I.e. you choose some cell (i, j) and assign (set) a_{i, j} := a_{i, j} - 1. Note that you can make heights less than or equal to zero. Also note that you can decrease the height of the cell (1, 1).Your task is to find the minimum number of operations you have to perform to obtain at least one suitable path from the cell (1, 1) to the cell (n, m). It is guaranteed that the answer exists.You have to answer t independent test cases. | Input: ['5', '3 4', '1 2 3 4', '5 6 7 8', '9 10 11 12', '5 5', '2 5 4 8 3', '9 10 11 5 1', '12 8 4 2 5', '2 2 5 4 1', '6 8 2 4 2', '2 2', '100 10', '10 1', '1 2', '123456789876543 987654321234567', '1 1', '42', ''] Output:['9', '49', '111', '864197531358023', '0', ''] | [
0
] |
You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k.In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one.You have to answer t independent test cases. | Input: ['6', '9 2', '010001010', '9 3', '111100000', '7 4', '1111111', '10 3', '1001110101', '1 1', '1', '1 1', '0', ''] Output:['1', '2', '5', '4', '0', '0', ''] | [
0,
2
] |
You are given a board of size n * n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure.In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i, j) you can move the figure to cells: (i - 1, j - 1); (i - 1, j); (i - 1, j + 1); (i, j - 1); (i, j + 1); (i + 1, j - 1); (i + 1, j); (i + 1, j + 1); Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. n^2-1 cells should contain 0 figures and one cell should contain n^2 figures).You have to answer t independent test cases. | Input: ['3', '1', '5', '499993', ''] Output:['0', '40', '41664916690999888', ''] | [
3
] |
You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.In one move, you can choose two indices i and j (1 <= i, j <= n) and swap a_i and b_j (i.e. a_i becomes b_j and vice versa). Note that i and j can be equal or different (in particular, swap a_2 with b_2 or swap a_3 and b_9 both are acceptable moves).Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k such moves (swaps).You have to answer t independent test cases. | Input: ['5', '2 1', '1 2', '3 4', '5 5', '5 5 6 6 5', '1 2 5 4 3', '5 3', '1 2 3 4 5', '10 9 10 10 9', '4 0', '2 2 4 3', '2 4 2 3', '4 4', '1 2 2 1', '4 4 5 4', ''] Output:['6', '27', '39', '11', '17', ''] | [
2
] |
You are given two integers n and m. You have to construct the array a of length n consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly m and the value \sum\limits_{i=1}^{n-1} |a_i - a_{i+1}| is the maximum possible. Recall that |x| is the absolute value of x.In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array a=[1, 3, 2, 5, 5, 0] then the value above for this array is |1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.You have to answer t independent test cases. | Input: ['5', '1 100', '2 2', '5 5', '2 1000000000', '1000000000 1000000000', ''] Output:['0', '2', '10', '1000000000', '2000000000', ''] | [
2,
3
] |
For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated.You are given three numbers: n_0 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; n_1 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; n_2 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5.Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. | Input: ['7', '1 3 5', '1 1 1', '3 9 3', '0 1 0', '3 1 2', '0 0 3', '2 0 0', ''] Output:['1110011110', '0011', '0110001100101011', '10', '0000111', '1111', '000', ''] | [
3
] |
Pay attention to the non-standard memory limit in this problem.In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.The array a=[a_1, a_2, ..., a_n] (1 <= a_i <= n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 <= l < r <= n) such that a_i = a_l + a_{l+1} + ... + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).Print the number of special elements of the given array a.For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal — if several elements are equal and special, then all of them should be counted in the answer. | Input: ['5', '9', '3 1 4 1 5 9 2 6 5', '3', '1 1 2', '5', '1 1 1 1 1', '8', '8 7 6 5 4 3 2 1', '1', '1', ''] Output:['5', '1', '0', '4', '0', ''] | [
0
] |
You are given two positive integers n and k. Print the k-th positive integer that is not divisible by n.For example, if n=3, and k=7, then all numbers that are not divisible by 3 are: 1, 2, 4, 5, 7, 8, 10, 11, 13 .... The 7-th number among them is 10. | Input: ['6', '3 7', '4 12', '2 1000000000', '7 97', '1000000000 1000000000', '2 1', ''] Output:['10', '15', '1999999999', '113', '1000000001', '1', ''] | [
3,
4
] |
You are given two positive integers n (1 <= n <= 10^9) and k (1 <= k <= 100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 2).In other words, find a_1, a_2, ..., a_k such that all a_i>0, n = a_1 + a_2 + ... + a_k and either all a_i are even or all a_i are odd at the same time.If such a representation does not exist, then report it. | Input: ['8', '10 3', '100 4', '8 7', '97 2', '8 8', '3 10', '5 3', '1000000000 9', ''] Output:['YES', '4 2 4', 'YES', '55 5 5 35', 'NO', 'NO', 'YES', '1 1 1 1 1 1 1 1', 'NO', 'YES', '3 1 1', 'YES', '111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120', ''] | [
3
] |
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 1 to 9 (inclusive) are round.For example, the following numbers are round: 4000, 1, 9, 800, 90. The following numbers are not round: 110, 707, 222, 1001.You are given a positive integer n (1 <= n <= 10^4). Represent the number n as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number n as a sum of the least number of terms, each of which is a round number. | Input: ['5', '5009', '7', '9876', '10000', '10', ''] Output:['2', '5000 9', '1', '7 ', '4', '800 70 6 9000 ', '1', '10000 ', '1', '10 ', ''] | [
3
] |
Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles. | Input: ['3', '2 3', '3 1', '3 2', '1 3', '3 3', '1 3', ''] Output:['Yes', 'Yes', 'No', ''] | [
0,
3
] |
There are n models in the shop numbered from 1 to n, with sizes s_1, s_2, ..., s_n.Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices i_j and i_{j+1} (note that i_j < i_{j+1}, because Orac arranged them properly), i_{j+1} is divisible by i_j and s_{i_j} < s_{i_{j+1}}.For example, for 6 models with sizes \{3, 6, 7, 7, 7, 7\}, he can buy models with indices 1, 2, and 6, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times. | Input: ['4', '4', '5 3 4 6', '7', '1 4 2 3 6 4 9', '5', '5 4 3 2 1', '1', '9', ''] Output:['2', '3', '1', '1', ''] | [
3
] |
Orac is studying number theory, and he is interested in the properties of divisors.For two positive integers a and b, a is a divisor of b if and only if there exists an integer c, such that a\cdot c=b.For n >= 2, we will denote as f(n) the smallest positive divisor of n, except 1.For example, f(7)=7,f(10)=2,f(35)=5.For the fixed integer n, Orac decided to add f(n) to n. For example, if he had an integer n=5, the new value of n will be equal to 10. And if he had an integer n=6, n will be changed to 8.Orac loved it so much, so he decided to repeat this operation several times.Now, for two positive integers n and k, Orac asked you to add f(n) to n exactly k times (note that n will change after each operation, so f(n) may change too) and tell him the final value of n.For example, if Orac gives you n=5 and k=2, at first you should add f(5)=5 to n=5, so your new value of n will be equal to n=10, after that, you should add f(10)=2 to 10, so your new (and the final!) value of n will be equal to 12.Orac may ask you these queries many times. | Input: ['3', '5 1', '8 2', '3 4', ''] Output:['10', '12', '12', ''] | [
3
] |
Note that the only differences between easy and hard versions are the constraints on n and the time limit. You can make hacks only if all versions are solved.Slime is interested in sequences. He defined good positive integer sequences p of length n as follows: For each k>1 that presents in p, there should be at least one pair of indices i,j, such that 1 <=q i < j <=q n, p_i = k - 1 and p_j = k.For the given integer n, the set of all good sequences of length n is s_n. For the fixed integer k and the sequence p, let f_p(k) be the number of times that k appears in p. For each k from 1 to n, Slime wants to know the following value:<=ft(\sum_{p\in s_n} f_p(k)\right)\ \textrm{mod}\ 998\,244\,353 | Input: ['2', ''] Output:['3 1 ', ''] | [
3
] |
Note that the only differences between easy and hard versions are the constraints on n and the time limit. You can make hacks only if all versions are solved.Slime is interested in sequences. He defined good positive integer sequences p of length n as follows: For each k>1 that presents in p, there should be at least one pair of indices i,j, such that 1 <=q i < j <=q n, p_i = k - 1 and p_j = k.For the given integer n, the set of all good sequences of length n is s_n. For the fixed integer k and the sequence p, let f_p(k) be the number of times that k appears in p. For each k from 1 to n, Slime wants to know the following value:<=ft(\sum_{p\in s_n} f_p(k)\right)\ \textrm{mod}\ 998\,244\,353 | Input: ['2', ''] Output:['3 1 ', ''] | [
3
] |
Slime and Orac are holding a turn-based game. In a big room, there are n players sitting on the chairs, looking forward to a column and each of them is given a number: player 1 sits in the front of the column, player 2 sits directly behind him; player 3 sits directly behind player 2, and so on; player n sits directly behind player n-1. Each player wears a hat that is either black or white. As each player faces forward, player i knows the color of player j's hat if and only if i is larger than j.At the start of each turn, Orac will tell whether there exists a player wearing a black hat in the room.After Orac speaks, if the player can uniquely identify the color of his hat, he will put his hat on the chair, stand up and leave the room. All players are smart, so if it is possible to understand the color of their hat using the obtained information during this and previous rounds, they will understand it. In each turn, all players who know the color of their hats will leave at the same time in this turn, which means a player can only leave in the next turn if he gets to know the color of his hat only after someone left the room at this turn.Note that when the player needs to leave, he will put the hat on the chair before leaving, so the players ahead of him still cannot see his hat. The i-th player will know who exactly left the room among players 1,2,...,i-1, and how many players among i+1,i+2,...,n have left the room.Slime stands outdoor. He watches the players walking out and records the numbers of the players and the time they get out. Unfortunately, Slime is so careless that he has only recorded some of the data, and this given data is in the format "player x leaves in the y-th round".Slime asked you to tell him the color of each player's hat. If there are multiple solutions, you can find any of them. | Input: ['5', '0 1 1 0 0', ''] Output:['00000'] | [
2
] |
Slime and his n friends are at a party. Slime has designed a game for his friends to play.At the beginning of the game, the i-th player has a_i biscuits. At each second, Slime will choose a biscuit randomly uniformly among all a_1 + a_2 + ... + a_n biscuits, and the owner of this biscuit will give it to a random uniform player among n-1 players except himself. The game stops when one person will have all the biscuits.As the host of the party, Slime wants to know the expected value of the time that the game will last, to hold the next activity on time.For convenience, as the answer can be represented as a rational number \frac{p}{q} for coprime p and q, you need to find the value of (p \cdot q^{-1})\mod 998\,244\,353. You can prove that q\mod 998\,244\,353 \neq 0. | Input: ['2', '1 1', ''] Output:['1', ''] | [
3
] |
Slime has a sequence of positive integers a_1, a_2, ..., a_n.In one operation Orac can choose an arbitrary subsegment [l ... r] of this sequence and replace all values a_l, a_{l + 1}, ..., a_r to the value of median of \{a_l, a_{l + 1}, ..., a_r\}.In this problem, for the integer multiset s, the median of s is equal to the \lfloor \frac{|s|+1}{2}\rfloor-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.Slime wants Orac to make a_1 = a_2 = ... = a_n = k using these operations.Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times. | Input: ['5', '5 3', '1 5 2 6 1', '1 6', '6', '3 2', '1 2 3', '4 3', '3 1 2 3', '10 3', '1 2 3 4 5 6 7 8 9 10', ''] Output:['no', 'yes', 'yes', 'no', 'yes', ''] | [
2,
3
] |
For the multiset of positive integers s=\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow: \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x. \textrm{lcm}(s) is the minimum positive integer x, that divisible on all integers from s.For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and \textrm{lcm}(\{4,6\})=12. Note that for any positive integer x, \gcd(\{x\})=\textrm{lcm}(\{x\})=x.Orac has a sequence a with length n. He come up with the multiset t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence. | Input: ['2', '1 1', ''] Output:['1', ''] | [
3
] |
Phoenix is trying to take a photo of his n friends with labels 1, 2, ..., n who are lined up in a row in a special order. But before he can take the photo, his friends get distracted by a duck and mess up their order.Now, Phoenix must restore the order but he doesn't remember completely! He only remembers that the i-th friend from the left had a label between a_i and b_i inclusive. Does there exist a unique way to order his friends based of his memory? | Input: ['4', '4 4', '1 3', '2 4', '3 4', ''] Output:['YES', '4 1 2 3 ', ''] | [
2
] |
Phoenix is picking berries in his backyard. There are n shrubs, and each shrub has a_i red berries and b_i blue berries.Each basket can contain k berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color.For example, if there are two shrubs with 5 red and 2 blue berries in the first shrub and 2 red and 1 blue berries in the second shrub then Phoenix can fill 2 baskets of capacity 4 completely: the first basket will contain 3 red and 1 blue berries from the first shrub; the second basket will contain the 2 remaining red berries from the first shrub and 2 red berries from the second shrub. Help Phoenix determine the maximum number of baskets he can fill completely! | Input: ['2 4', '5 2', '2 1', ''] Output:['2', ''] | [
0,
2,
3
] |
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.Initially, on day 1, there is one bacterium with mass 1.Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass \frac{m}{2} each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.Also, every night, the mass of every bacteria will increase by one.Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist! | Input: ['3', '9', '11', '2', ''] Output:['3', '1 0 2 ', '3', '1 1 2', '1', '0 '] | [
2,
3,
4
] |
Phoenix has a string s consisting of lowercase Latin letters. He wants to distribute all the letters of his string into k non-empty strings a_1, a_2, ..., a_k such that every letter of s goes to exactly one of the strings a_i. The strings a_i do not need to be substrings of s. Phoenix can distribute letters of s and rearrange the letters within each string a_i however he wants.For example, if s = baba and k=2, Phoenix may distribute the letters of his string in many ways, such as: ba and ba a and abb ab and ab aa and bb But these ways are invalid: baa and ba b and ba baba and empty string (a_i should be non-empty) Phoenix wants to distribute the letters of his string s into k strings a_1, a_2, ..., a_k to minimize the lexicographically maximum string among them, i. e. minimize max(a_1, a_2, ..., a_k). Help him find the optimal distribution and print the minimal possible value of max(a_1, a_2, ..., a_k).String x is lexicographically less than string y if either x is a prefix of y and x!=y, or there exists an index i (1 <= i <= min(|x|, |y|)) such that x_i < y_i and for every j (1 <= j < i) x_j = y_j. Here |x| denotes the length of the string x. | Input: ['6', '4 2', 'baba', '5 2', 'baacb', '5 3', 'baacb', '5 3', 'aaaaa', '6 4', 'aaxxzz', '7 1', 'phoenix', ''] Output:['ab', 'abbc', 'b', 'aa', 'x', 'ehinopx', ''] | [
2
] |
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length k have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array a of length n. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 1 and n inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers. | Input: ['4', '4 2', '1 2 2 1', '4 3', '1 2 2 1', '3 2', '1 2 3', '4 4', '4 3 4 2', ''] Output:['5', '1 2 1 2 1', '4', '1 2 2 1', '-1', '7', '4 3 2 1 4 3 2'] | [
2
] |
Phoenix has n coins with weights 2^1, 2^2, ..., 2^n. He knows that n is even.He wants to split the coins into two piles such that each pile has exactly \frac{n}{2} coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a-b|, the absolute value of a-b. | Input: ['2', '2', '4', ''] Output:['2', '6', ''] | [
2,
3
] |
You have two IP cameras of the same model. Each camera can take photos starting from some moment of time with a fixed period. You can freely choose the starting moment but you can choose the period only as one of k values p_1, p_2, ..., p_k which are chosen by the camera's manufacturer.You have n moments of interest x_1, x_2, ..., x_n. You'd like to configure both cameras in such a way that at least one camera will take a photo in each of these moments. Configuring the camera means setting the moment when it takes the first photo and the gap between two consecutive photos (which should be one of the values p_1, p_2, ..., p_k). It's not a problem for you that cameras can take photos at other moments of time — you only care about moments of interest. | Input: ['3 5', '3 5 7', '1 4 5 7 12', ''] Output:['YES', '1 3', '5 7', ''] | [
3
] |
You're at the last mission in one very old and very popular strategy game Dune II: Battle For Arrakis. The map of the mission can be represented as a rectangular matrix of size n * m. Initially, there are a_{i, j} units of your army in the cell (i, j).You want to prepare for the final battle, so you want to move all your army into exactly one cell of the map (i.e. nm-1 cells should contain 0 units of the army and the remaining cell should contain the entire army).To do this, you can do some (possibly, zero) number of moves. During one move, you can select exactly one unit from some cell and move it to one of the adjacent by side cells. I.e. from the cell (i, j) you can move the unit to cells: (i - 1, j); (i, j - 1); (i + 1, j); (i, j + 1). Of course, you want to move all your army into exactly one cell as fast as possible. So, you want to know the minimum number of moves you need to do that.And, of course, life goes on, so the situation on the map changes. There are q updates, the i-th update is denoted by three integers x, y, z. This update affects the army in the cell (x, y): after this update, the number of units in the cell (x, y) becomes z (i.e. you replace a_{x, y} with z).Also, you want to determine, for each i, the minimum number of moves needed to move your entire army into exactly one cell with the first i updates applied to the initial map. In other words, the map after the i-th update equals the initial map with the first i updates applied to it. | Input: ['3 3 1', '1 2 3', '2 1 2', '1 1 2', '2 3 100', ''] Output:['21 22 ', ''] | [
2,
3
] |
Polycarp is developing an RPG game where the main character fights monsters and searches for treasure in dungeons. Now Polycarp is making one of the dungeons the character can explore.The dungeon consists of n rooms connected by m two-way tunnels, and it is possible to reach every room from every other room using tunnels. The rooms are guarded by monsters (the number of monsters in the i-th room is a_i), and the tunnels contain gold coins (the number of coins in the i-th tunnel is w_i). The i-th two-way tunnel connects rooms v_i and u_i.Polycarp has already fixed the number of coins in each tunnel (the values of w_i are already known), and now he tries to place the monsters in the rooms (the values of a_i are not known yet). Polycarp wants to choose the number of monsters in each room in such a way that the following two conditions are met: the number of coins for the tunnel connecting the rooms x and y should be equal to the minimum of a_x and a_y. That is, for each tunnel i, w_i = \min (a_{v_i}, a_{u_i}); the number of monsters in the dungeon is as small as possible. That is, the value of a_1 + a_2 + ... + a_n is minimum possible. Help Polycarp to choose the values a_1, a_2, ..., a_n, or tell him that it is impossible and he has to change something in his dungeon plan. | Input: ['3', '3 2', '1 2 1', '2 3 1', '5 7', '3 2 7', '3 4 9', '1 5 5', '1 2 5', '4 1 5', '4 2 7', '3 1 5', '4 4', '1 2 5', '3 2 2', '4 1 3', '3 4 4', ''] Output:['YES', '1 1 1', 'YES', '5 7 9 9 5', 'NO', ''] | [
2
] |
Tanya wants to organize her bookcase. There are n bookshelves in the bookcase, the i-th bookshelf contains a_i books on it. Tanya will be satisfied if each bookshelf contains no more than k books.Tanya can do one of the two following operations to achieve her goal: Choose exactly one bookshelf and put all the books from it in the storage room (i. e. choose some i and assign a_i := 0). During this operation she spends x seconds. Take all books from all n bookshelves and distribute them between all n bookshelves evenly (the definition of the term is given below). During this operation she spends y seconds. Consider the sequence a of n integers. Then its even distribution is such a sequence b of n integers that the sum of b equals the sum of a and the value max(b) - min(b) is the minimum possible.For example, if the array a=[5, 4, 3] then its even distribution is b=[4, 4, 4]. If a=[1, 2, 3, 4] then its even distribution is b=[2, 3, 3, 2] (or any permutation of this array).Your task is to find the minimum number of seconds Tanya has to spend to obtain the bookcase with no more than k books on each bookshelf. | Input: ['6', '5 4 3 5', '1 2 2 3 5', '5 3 4 5', '1 5 1 5 5', '5 4 5 6', '1 2 5 3 5', '4 3 2 10', '4 4 1 1', '4 3 10 2', '4 4 1 1', '4 1 5 4', '1 2 1 3', ''] Output:['3', '9', '6', '4', '2', '9', ''] | [
2
] |
Berland State University (BSU) is conducting a programming boot camp. The boot camp will last for n days, and the BSU lecturers are planning to give some number of lectures during these days.Some days of the boot camp are already planned as excursion days, and no lectures should be held during these days. To make sure the participants don't get too tired of learning to program, the number of lectures for each day should not exceed k_1, and the number of lectures for each pair of consecutive days should not exceed k_2.Can you calculate the maximum number of lectures that can be conducted during the boot camp? Formally, find the maximum integer m such that it is possible to choose n non-negative integers c_1, c_2, ..., c_n (where c_i is the number of lectures held during day i) so that: c_1 + c_2 + ... + c_n = m; for each excursion day d, c_d = 0; for each day i, c_i <= k_1; for each pair of consecutive days (i, i + 1), c_i + c_{i + 1} <= k_2. Note that there might be some non-excursion days without lectures (i. e., it is possible that c_i = 0 even if i is not an excursion day). | Input: ['4', '4 5 7', '1011', '4 4 10', '0101', '5 3 4', '11011', '6 4 6', '011101', ''] Output:['12', '8', '8', '14', ''] | [
2
] |
Hmm, how long has it been since the last color revolution? 5 years?! It's totally the time to make a new one!So the general idea is the following. Division 1 should have n_1 participants. Division 2 should have n_2 and be exactly k times bigger than division 1 (n_2 = k \cdot n_1). Division 3 should have n_3 = k \cdot n_2 participants. Finally, division 4 should have n_4 = k \cdot n_3 participants.There are n participants on Codeforces in total. So n_1 + n_2 + n_3 + n_4 should be exactly equal to n.You know the values of n and k. You also know that n and k are chosen in such a way that there exist values n_1, n_2, n_3 and n_4 such that all the conditions are satisfied.What will be the number of participants in each division (n_1, n_2, n_3 and n_4) after the revolution? | Input: ['4', '40 3', '1200 7', '320802005 400', '4 1', ''] Output:['1 3 9 27', '3 21 147 1029', '5 2000 800000 320000000', '1 1 1 1', ''] | [
3
] |
A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a base. A base consists of h pyramids of height 1, and h-1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows: You start with n cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed? | Input: ['5', '3', '14', '15', '24', '1', ''] Output:['1', '2', '1', '3', '0', ''] | [
0,
3,
4
] |
You are given a special jigsaw puzzle consisting of n\cdot m identical pieces. Every piece has three tabs and one blank, as pictured below. The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with n rows and m columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. | Input: ['3', '1 3', '100000 100000', '2 2', ''] Output:['YES', 'NO', 'YES', ''] | [
3
] |
Uh oh! Applications to tech companies are due soon, and you've been procrastinating by doing contests instead! (Let's pretend for now that it is actually possible to get a job in these uncertain times.)You have completed many programming projects. In fact, there are exactly n types of programming projects, and you have completed a_i projects of type i. Your résumé has limited space, but you want to carefully choose them in such a way that maximizes your chances of getting hired.You want to include several projects of the same type to emphasize your expertise, but you also don't want to include so many that the low-quality projects start slipping in. Specifically, you determine the following quantity to be a good indicator of your chances of getting hired: f(b_1,...,b_n)=\sum\limits_{i=1}^n b_i(a_i-b_i^2). Here, b_i denotes the number of projects of type i you include in your résumé. Of course, you cannot include more projects than you have completed, so you require 0<= b_i <= a_i for all i.Your résumé only has enough room for k projects, and you will absolutely not be hired if your résumé has empty space, so you require \sum\limits_{i=1}^n b_i=k.Find values for b_1,..., b_n that maximize the value of f(b_1,...,b_n) while satisfying the above two constraints. | Input: ['10 32', '1 2 3 4 5 5 5 5 5 5', ''] Output:['1 2 3 3 3 4 4 4 4 4 ', ''] | [
2,
3,
4
] |
Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal (\forall) and existential (\exists). You can read more about them here.The universal quantifier is used to make a claim that a statement holds for all real numbers. For example: \forall x,x<100 is read as: for all real numbers x, x is less than 100. This statement is false. \forall x,x>x-1 is read as: for all real numbers x, x is greater than x-1. This statement is true. The existential quantifier is used to make a claim that there exists some real number for which the statement holds. For example: \exists x,x<100 is read as: there exists a real number x such that x is less than 100. This statement is true. \exists x,x>x-1 is read as: there exists a real number x such that x is greater than x-1. This statement is true. Moreover, these quantifiers can be nested. For example: \forall x,\exists y,x<y is read as: for all real numbers x, there exists a real number y such that x is less than y. This statement is true since for every x, there exists y=x+1. \exists y,\forall x,x<y is read as: there exists a real number y such that for all real numbers x, x is less than y. This statement is false because it claims that there is a maximum real number: a number y larger than every x. Note that the order of variables and quantifiers is important for the meaning and veracity of a statement.There are n variables x_1,x_2,...,x_n, and you are given some formula of the form f(x_1,...,x_n):=(x_{j_1}<x_{k_1})\land (x_{j_2}<x_{k_2})\land \cdots\land (x_{j_m}<x_{k_m}), where \land denotes logical AND. That is, f(x_1,..., x_n) is true if every inequality x_{j_i}<x_{k_i} holds. Otherwise, if at least one inequality does not hold, then f(x_1,...,x_n) is false.Your task is to assign quantifiers Q_1,...,Q_n to either universal (\forall) or existential (\exists) so that the statement Q_1 x_1, Q_2 x_2, ..., Q_n x_n, f(x_1,..., x_n) is true, and the number of universal quantifiers is maximized, or determine that the statement is false for every possible assignment of quantifiers.Note that the order the variables appear in the statement is fixed. For example, if f(x_1,x_2):=(x_1<x_2) then you are not allowed to make x_2 appear first and use the statement \forall x_2,\exists x_1, x_1<x_2. If you assign Q_1=\exists and Q_2=\forall, it will only be interpreted as \exists x_1,\forall x_2,x_1<x_2. | Input: ['2 1', '1 2', ''] Output:['1', 'AE', ''] | [
3
] |
Hilbert's Hotel is a very unusual hotel since the number of rooms is infinite! In fact, there is exactly one room for every integer, including zero and negative integers. Even stranger, the hotel is currently at full capacity, meaning there is exactly one guest in every room. The hotel's manager, David Hilbert himself, decides he wants to shuffle the guests around because he thinks this will create a vacancy (a room without a guest).For any integer k and positive integer n, let k\bmod n denote the remainder when k is divided by n. More formally, r=k\bmod n is the smallest non-negative integer such that k-r is divisible by n. It always holds that 0<= k\bmod n<= n-1. For example, 100\bmod 12=4 and (-1337)\bmod 3=1.Then the shuffling works as follows. There is an array of n integers a_0,a_1,...,a_{n-1}. Then for each integer k, the guest in room k is moved to room number k+a_{k\bmod n}.After this shuffling process, determine if there is still exactly one guest assigned to each room. That is, there are no vacancies or rooms with multiple guests. | Input: ['6', '1', '14', '2', '1 -1', '4', '5 5 5 1', '3', '3 2 1', '2', '0 1', '5', '-239 -2 -100 -3 -11', ''] Output:['YES', 'YES', 'YES', 'NO', 'NO', 'YES', ''] | [
3
] |
We guessed a permutation p consisting of n integers. The permutation of length n is the array of length n where each element from 1 to n appears exactly once. This permutation is a secret for you.For each position r from 2 to n we chose some other index l (l < r) and gave you the segment p_l, p_{l + 1}, ..., p_r in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly n-1 segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order.For example, if the secret permutation is p=[3, 1, 4, 6, 2, 5] then the possible given set of segments can be: [2, 5, 6] [4, 6] [1, 3, 4] [1, 3] [1, 2, 4, 6] Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists).You have to answer t independent test cases. | Input: ['5', '6', '3 2 5 6', '2 4 6', '3 1 3 4', '2 1 3', '4 1 2 4 6', '5', '2 2 3', '2 1 2', '2 1 4', '2 4 5', '7', '3 1 2 6', '4 1 3 5 6', '2 1 2', '3 4 5 7', '6 1 2 3 4 5 6', '3 1 3 6', '2', '2 1 2', '5', '2 2 5', '3 2 3 5', '4 2 3 4 5', '5 1 2 3 4 5', ''] Output:['3 1 4 6 2 5 ', '3 2 1 4 5 ', '2 1 6 3 5 4 7 ', '1 2 ', '2 5 3 4 1 ', ''] | [
0,
2
] |
You are given an undirected unweighted graph consisting of n vertices and m edges (which represents the map of Bertown) and the array of prices p of length m. It is guaranteed that there is a path between each pair of vertices (districts).Mike has planned a trip from the vertex (district) a to the vertex (district) b and then from the vertex (district) b to the vertex (district) c. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used p is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road.You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip.You have to answer t independent test cases. | Input: ['2', '4 3 2 3 4', '1 2 3', '1 2', '1 3', '1 4', '7 9 1 5 7', '2 10 4 8 5 6 7 3 3', '1 2', '1 3', '1 4', '3 2', '3 5', '4 2', '5 6', '1 7', '6 7', ''] Output:['7', '12', ''] | [
0,
2
] |
You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All a_i does not exceed some integer k.Your task is to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace a_i with some integer in range [1; k]) to satisfy the following conditions: after all replacements, all a_i are positive integers not greater than k; for all i from 1 to \frac{n}{2} the following equation is true: a_i + a_{n - i + 1} = x, where x should be the same for all \frac{n}{2} pairs of elements. You have to answer t independent test cases. | Input: ['4', '4 2', '1 2 1 2', '4 3', '1 2 2 1', '8 7', '6 1 1 7 6 3 4 6', '6 6', '5 2 6 1 3 4', ''] Output:['0', '1', '4', '2', ''] | [
0,
2
] |
Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2].You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.You have to answer t independent test cases. | Input: ['4', '5', '1 2 3 -1 -2', '4', '-1 -2 -1 -3', '10', '-2 8 3 8 -4 -15 5 -2 -3 1', '6', '1 -1000000000 1 -1000000000 1 -1000000000', ''] Output:['2', '-1', '6', '-2999999997', ''] | [
2
] |
You are given a positive integer n, it is guaranteed that n is even (i.e. divisible by 2).You want to construct the array a of length n such that: The first \frac{n}{2} elements of a are even (divisible by 2); the second \frac{n}{2} elements of a are odd (not divisible by 2); all elements of a are distinct and positive; the sum of the first half equals to the sum of the second half (\sum\limits_{i=1}^{\frac{n}{2}} a_i = \sum\limits_{i=\frac{n}{2} + 1}^{n} a_i). If there are multiple answers, you can print any. It is not guaranteed that the answer exists.You have to answer t independent test cases. | Input: ['5', '2', '4', '6', '8', '10', ''] Output:['NO', 'YES', '2 4 1 5', 'NO', 'YES', '2 4 6 8 1 3 5 11', 'NO', ''] | [
3
] |
Recently Vova found n candy wrappers. He remembers that he bought x candies during the first day, 2x candies during the second day, 4x candies during the third day, ..., 2^{k-1} x candies during the k-th day. But there is an issue: Vova remembers neither x nor k but he is sure that x and k are positive integers and k > 1.Vova will be satisfied if you tell him any positive integer x so there is an integer k>1 that x + 2x + 4x + ... + 2^{k-1} x = n. It is guaranteed that at least one solution exists. Note that k > 1.You have to answer t independent test cases. | Input: ['7', '3', '6', '7', '21', '28', '999999999', '999999984', ''] Output:['1', '2', '1', '7', '4', '333333333', '333333328', ''] | [
0,
3
] |
You are given an array a consisting of n elements. You may apply several operations (possibly zero) to it.During each operation, you choose two indices i and j (1 <= i, j <= n; i!=j), increase a_j by a_i, and remove the i-th element from the array (so the indices of all elements to the right to it decrease by 1, and n also decreases by 1).Your goal is to make the array a strictly ascending. That is, the condition a_1 < a_2 < ... < a_n should hold (where n is the resulting size of the array).Calculate the minimum number of actions required to make the array strictly ascending. | Input: ['4', '8', '2 1 3 5 1 2 4 5', '15', '16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1', '2', '3 3', '14', '1 2 3 4 5 6 7 8 9 10 11 12 13 14', ''] Output:['3', '6 8', '1 6', '4 1', '7', '1 15', '1 13', '1 11', '1 9', '1 7', '1 5', '1 3', '1', '2 1', '0', ''] | [
0
] |
Calculate the number of ways to place n rooks on n * n chessboard so that both following conditions are met: each empty cell is under attack; exactly k pairs of rooks attack each other. An empty cell is under attack if there is at least one rook in the same row or at least one rook in the same column. Two rooks attack each other if they share the same row or column, and there are no other rooks between them. For example, there are only two pairs of rooks that attack each other in the following picture: One of the ways to place the rooks for n = 3 and k = 2 Two ways to place the rooks are considered different if there exists at least one cell which is empty in one of the ways but contains a rook in another way.The answer might be large, so print it modulo 998244353. | Input: ['3 2', ''] Output:['6', ''] | [
3
] |
So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests... Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem!Initially, each test in that problem is just an array. The maximum size of an array is k. For simplicity, the contents of arrays don't matter. You have n tests — the i-th test is an array of size m_i (1 <= m_i <= k).Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than c_1 arrays of size greater than or equal to 1 (>= 1), no more than c_2 arrays of size greater than or equal to 2, ..., no more than c_k arrays of size greater than or equal to k. Also, c_1 >= c_2 >= ... >= c_k.So now your goal is to create the new testcases in such a way that: each of the initial arrays appears in exactly one testcase; for each testcase the given conditions hold; the number of testcases is minimum possible. Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase. | Input: ['4 3', '1 2 2 3', '4 1 1', ''] Output:['3', '1 2', '2 1 3', '1 2', ''] | [
2,
4
] |
You are given two integers a and b, and q queries. The i-th query consists of two numbers l_i and r_i, and the answer to it is the number of integers x such that l_i <= x <= r_i, and ((x \bmod a) \bmod b)!=((x \bmod b) \bmod a). Calculate the answer for each query.Recall that y \bmod z is the remainder of the division of y by z. For example, 5 \bmod 3 = 2, 7 \bmod 8 = 7, 9 \bmod 4 = 1, 9 \bmod 9 = 0. | Input: ['2', '4 6 5', '1 1', '1 3', '1 5', '1 7', '1 9', '7 10 2', '7 8', '100 200', ''] Output:['0 0 0 2 4 ', '0 91 ', ''] | [
3
] |
You are given two integers x and y. You can perform two types of operations: Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: x = 0, y = 6; x = 0, y = 8; x = -1, y = 7; x = 1, y = 7. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: x = -1, y = 6; x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0.Calculate the minimum amount of dollars you have to spend on it. | Input: ['2', '1 3', '391 555', '0 0', '9 4', ''] Output:['1337', '0', ''] | [
2,
3
] |
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length k (k >= 3). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights a_1, a_2, ..., a_n in order from left to right (k <= n). It is guaranteed that neighboring heights are not equal to each other (that is, a_i!=a_{i+1} for all i from 1 to n-1).Peaks of mountains on the segment [l,r] (from l to r) are called indexes i such that l < i < r, a_{i - 1} < a_i and a_i > a_{i + 1}. It is worth noting that the boundary indexes l and r for the segment are not peaks. For example, if n=8 and a=[3,1,4,1,5,9,2,6], then the segment [1,8] has only two peaks (with indexes 3 and 6), and there are no peaks on the segment [3, 6].To break the door, Nastya throws it to a segment [l,l+k-1] of consecutive mountains of length k (1 <= l <= n-k+1). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to p+1, where p is the number of peaks on the segment [l,l+k-1].Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains [l, l+k-1] that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value l is minimal.Formally, you need to choose a segment of mountains [l, l+k-1] that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value l. | Input: ['5', '8 6', '1 2 4 1 2 4 1 2', '5 3', '3 2 3 2 1', '10 4', '4 3 4 3 2 3 2 1 0 1', '15 7', '3 7 4 8 2 3 4 5 21 2 3 4 2 1 3', '7 5', '1 2 3 4 5 6 1', ''] Output:['3 2', '2 2', '2 1', '3 1', '2 3', ''] | [
2
] |
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped n grains. Nastya read that each grain weighs some integer number of grams from a - b to a + b, inclusive (numbers a and b are known), and the whole package of n grains weighs from c - d to c + d grams, inclusive (numbers c and d are known). The weight of the package is the sum of the weights of all n grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the i-th grain weighs some integer number x_i (a - b <=q x_i <=q a + b), and in total they weigh from c - d to c + d, inclusive (c - d <=q \sum\limits_{i=1}^{n}{x_i} <=q c + d). | Input: ['5', '7 20 3 101 18', '11 11 10 234 2', '8 9 7 250 122', '19 41 21 321 10', '3 10 8 6 1', ''] Output:['Yes', 'No', 'Yes', 'No', 'Yes', ''] | [
3
] |
Nastya is a competitive programmer, but she is only studying now. Recently, Denis told her about the way to check if the string is correct bracket sequence. After that, unexpectedly, Nastya came up with a much more complex problem that Denis couldn't solve. Can you solve it? A string s is given. It consists of k kinds of pairs of brackets. Each bracket has the form t — it is an integer, such that 1 <=q |t| <=q k. If the bracket has a form t, then: If t > 0, then it's an opening bracket of the type t. If t < 0, then it's a closing bracket of the type -t. Thus, there are k types of pairs of brackets in total. The queries need to be answered: Replace the bracket at position i with the bracket of the form t. Check if the substring from the l-th to r-th position (including) is the correct bracket sequence. Recall the definition of the correct bracket sequence: An empty sequence is correct. If A and B are two correct bracket sequences, then their concatenation "A B" is also correct bracket sequence. If A is the correct bracket sequence, c (1 <=q c <=q k) is a type of brackets, then the sequence "c A -c" is also correct bracket sequence. | Input: ['2 1', '1 -1', '1', '2 1 2', ''] Output:['Yes', ''] | [
0
] |
Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of 7 segments, which can be turned on or off to display different numbers. The picture shows how all 10 decimal digits are displayed: After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly k segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly k sticks (which are off now)? It is allowed that the number includes leading zeros. | Input: ['1 7', '0000000', ''] Output:['8'] | [
2
] |
Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his luck.When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of n steps. At the i-th step, a place is chosen for the number i (1 <=q i <=q n). The position for the number i is defined as follows: For all j from 1 to n, we calculate r_j — the minimum index such that j <=q r_j <=q n, and the position r_j is not yet occupied in the permutation. If there are no such positions, then we assume that the value of r_j is not defined. For all t from 1 to n, we calculate count_t — the number of positions 1 <=q j <=q n such that r_j is defined and r_j = t. Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the count array is maximum. The generator selects one of these positions for the number i. The generator can choose any position. Let's have a look at the operation of the algorithm in the following example: Let n = 5 and the algorithm has already arranged the numbers 1, 2, 3 in the permutation. Consider how the generator will choose a position for the number 4: The values of r will be r = [3, 3, 3, 4, *], where * means an indefinite value. Then the count values will be count = [0, 0, 3, 1, 0]. There are only two unoccupied positions in the permutation: 3 and 4. The value in the count array for position 3 is 3, for position 4 it is 1. The maximum value is reached only for position 3, so the algorithm will uniquely select this position for number 4. Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind p_1, p_2, ..., p_n and decided to find out if it could be obtained as a result of the generator.Unfortunately, this task was too difficult for him, and he asked you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs. | Input: ['5', '5', '2 3 4 5 1', '1', '1', '3', '1 3 2', '4', '4 2 3 1', '5', '1 5 2 4 3', ''] Output:['Yes', 'Yes', 'No', 'Yes', 'No', ''] | [
0,
2
] |
You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.Please look at pictures below for better understanding. On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. | Input: ['2', '2', '1', ''] Output:['2', '1', ''] | [
0,
3
] |
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}. 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: ['6', '1 3', '2 3', '3 4', '4 5', '4 6', ''] Output:['4', ''] | [
3
] |
Consider the infinite sequence s of positive integers, created by repeating the following steps: Find the lexicographically smallest triple of positive integers (a, b, c) such that a \oplus b \oplus c = 0, where \oplus denotes the bitwise XOR operation. a, b, c are not in s. Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2]. Append a, b, c to s in this order. Go back to the first step. You have integer n. Find the n-th element of s.You have to answer t independent test cases.A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b. | Input: ['9', '1', '2', '3', '4', '5', '6', '7', '8', '9', ''] Output:['1', '2', '3', '4', '8', '12', '5', '10', '15', ''] | [
0,
3
] |
You have unweighted tree of n vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves v_{1} and v_{2} of this tree, bitwise XOR of weights of all edges on the simple path between v_{1} and v_{2} has to be equal to 0. Note that you can put very large positive integers (like 10^{(10^{10})}).It's guaranteed that such assignment always exists under given constraints. Now let's define f as the number of distinct weights in assignment. In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is 0. f value is 2 here, because there are 2 distinct edge weights(4 and 5). In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex 1 and vertex 6 (3, 4, 5, 4) is not 0. What are the minimum and the maximum possible values of f for the given tree? Find and print both. | Input: ['6', '1 3', '2 3', '3 4', '4 5', '5 6', ''] Output:['1 4', ''] | [
2,
3
] |
You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: Select some distinct indices i_{1}, i_{2}, ..., i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, ..., k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds.Array a is nondecreasing if and only if a_{1} <= a_{2} <= ... <= a_{n}.You have to answer t independent test cases. | Input: ['3', '4', '1 7 6 5', '5', '1 2 3 4 5', '2', '0 -4', ''] Output:['2', '0', '3', ''] | [
2,
3
] |
Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of x initially. When its hit point goes to 0 or under 0, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void Absorption Assume that the dragon's current hit point is h, after casting this spell its hit point will become <=ft\lfloor \frac{h}{2} \right\rfloor + 10. Here <=ft\lfloor \frac{h}{2} \right\rfloor denotes h divided by two, rounded down. Lightning Strike This spell will decrease the dragon's hit point by 10. Assume that the dragon's current hit point is h, after casting this spell its hit point will be lowered to h-10.Due to some reasons Kana can only cast no more than n Void Absorptions and m Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon. | Input: ['7', '100 3 4', '189 3 4', '64 2 3', '63 2 3', '30 27 7', '10 9 1', '69117 21 2', ''] Output:['YES', 'NO', 'NO', 'YES', 'YES', 'YES', 'YES', ''] | [
2,
3
] |
Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem. You are given four positive integers a, b, c, d, such that a <=q b <=q c <=q d. Your task is to find three integers x, y, z, satisfying the following conditions: a <=q x <=q b. b <=q y <=q c. c <=q z <=q d. There exists a triangle with a positive non-zero area and the lengths of its three sides are x, y, and z.Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her? | Input: ['4', '1 3 5 7', '1 5 5 7', '100000 200000 300000 400000', '1 1 977539810 977539810', ''] Output:['3 4 5', '5 5 5', '182690 214748 300999', '1 977539810 977539810', ''] | [
3
] |
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.Chiori loves dolls and now she is going to decorate her bedroom! As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 <=q i < m, such that <=ft\lfloor \frac{x}{2^i} \right\rfloor is odd.Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998\,244\,353. | Input: ['4 4', '3 5 8 14', ''] Output:['2 2 6 6 0 '] | [
0,
3
] |
This is the easy version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.Chiori loves dolls and now she is going to decorate her bedroom! As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 <=q i < m, such that <=ft\lfloor \frac{x}{2^i} \right\rfloor is odd.Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998\,244\,353. | Input: ['4 4', '3 5 8 14', ''] Output:['2 2 6 6 0 '] | [
0,
3
] |
Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight.Now, she is going to pick three gems.Xenia loves colorful things, so she will pick exactly one gem of each color.Xenia loves balance, so she will try to pick gems with little difference in weight.Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? | Input: ['5', '2 2 3', '7 8', '6 3', '3 1 4', '1 1 1', '1', '1', '1000000000', '2 2 2', '1 2', '5 4', '6 7', '2 2 2', '1 2', '3 4', '6 7', '3 4 1', '3 2 1', '7 3 3 4', '6', ''] Output:['14', '1999999996000000002', '24', '24', '14', ''] | [
2,
3,
4
] |
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it. There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her? | Input: ['7 4', '1 2', '1 3', '1 4', '3 5', '3 6', '4 7', ''] Output:['7'] | [
2
] |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.