Problem ID stringlengths 2 6 | Problem Description stringlengths 0 7.52k | Rating float64 800 3.5k | math bool 2 classes | greedy bool 2 classes | implementation bool 2 classes | dp bool 2 classes | data structures bool 2 classes | constructive algorithms bool 2 classes | brute force bool 2 classes | binary search bool 2 classes | sortings bool 2 classes | graphs bool 2 classes | __index_level_0__ int64 3 9.98k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1931C | Problem - 1931C - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force greedy math *1000 No tag edit access β Contest materials ") and assign all elements of the array with indexes from $$$i$$$ to $$$j$$$ the value $$$x$$$. The price of this operation depends on the selected indices and is equal to $$$(j - i + 1)$$$ burles. For example, the array is equal to $$$ | 1,000 | true | true | false | false | false | false | true | false | false | false | 699 |
1346H | Alice and Bob are playing a game (yet again). They have two sequences of segments of the coordinate axis: a sequence of $$$n$$$ initial segments: $$$[l_1, r_1]$$$, $$$[l_2, r_2]$$$, ..., $$$[l_n, r_n]$$$, and a sequence of $$$m$$$ terminal segments: $$$[L_1, R_1]$$$, $$$[L_2, R_2]$$$, ..., $$$[L_m, R_m]$$$. At the beginning of the game, they choose one of the initial segments and set it as the current segment. Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must shrink the current segment either by increasing its left endpoint by $$$1$$$, or by decreasing its right endpoint by $$$1$$$. So, if the current segment is $$$[c_l, c_r]$$$, it becomes either $$$[c_l + 1, c_r]$$$, or $$$[c_l, c_r - 1]$$$. If at the beginning of the game or after Bob's move the current segment coincides with one of the terminal segments, Bob wins. If the current segment becomes degenerate ($$$c_l = c_r$$$), and Bob hasn't won yet, Alice wins. If the current segment coincides with one of the terminal segments after Alice's move, nothing happens β the game continues. Both players play optimally β if they can win, they always use a strategy that leads them to victory in the minimum number of turns, and if they cannot win, they try to prolong the game, using the strategy allowing them to make the maximum possible number of moves until their defeat. For each of the initial segments you have to determine who will win the game if this segment is chosen as the current segment at the beginning of the game. If Bob wins, you also have to calculate the number of moves Alice will make before her defeat. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) β the number of initial segments and terminal segments, respectively. Then $$$n$$$ lines follow, the $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i < r_i le 10^6$$$) β the endpoints of the $$$i$$$-th initial segment. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$1 le L_i < R_i le 10^6$$$) β the endpoints of the $$$i$$$-th terminal segment. Note that some of the segments given in the input may coincide. | 2,700 | false | false | false | false | true | false | false | false | false | false | 3,972 |
964B | There are _n_ incoming messages for Vasya. The _i_-th message is going to be received after _t__i_ minutes. Each message has a cost, which equals to _A_ initially. After being received, the cost of a message decreases by _B_ each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0. Also, each minute Vasya's bank account receives _C_Β·_k_, where _k_ is the amount of received but unread messages. Vasya's messages are very important to him, and because of that he wants to have all messages read after _T_ minutes. Determine the maximum amount of money Vasya's bank account can hold after _T_ minutes. Input The first line contains five integers _n_, _A_, _B_, _C_ and _T_ (1u2009β€u2009_n_,u2009_A_,u2009_B_,u2009_C_,u2009_T_u2009β€u20091000). The second string contains _n_ integers _t__i_ (1u2009β€u2009_t__i_u2009β€u2009_T_). Output Output one integer xa0β the answer to the problem. Note In the first sample the messages must be read immediately after receiving, Vasya receives _A_ points for each message, _n_Β·_A_u2009=u200920 in total. In the second sample the messages can be read at any integer moment. In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5u2009-u20094Β·3)u2009+u2009(5u2009-u20093Β·3)u2009+u2009(5u2009-u20092Β·3)u2009+u2009(5u2009-u20091Β·3)u2009+u20095u2009=u2009u2009-u20095 points. This is 35 in total. | 1,300 | true | false | false | false | false | false | false | false | false | false | 5,843 |
1539D | Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store xa0β "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $$$2$$$ rubles per item. For every product $$$i$$$ there is a discount for experienced buyers: if you buy $$$b_i$$$ items of products (of any type, not necessarily type $$$i$$$), then for all future purchases of the $$$i$$$-th product there is a $$$50%$$$ discount (so you can buy an item of the $$$i$$$-th product for $$$1$$$ ruble!). Lena needs to buy $$$n$$$ products: she must purchase at least $$$a_i$$$ items of the $$$i$$$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 100,000$$$)xa0β the number of products. Each of next $$$n$$$ lines contains a product description. Each description consists of two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i leq 10^{14}$$$, $$$1 leq b_i leq 10^{14}$$$)xa0β the required number of the $$$i$$$-th product and how many products you need to buy to get the discount on the $$$i$$$-th product. The sum of all $$$a_i$$$ does not exceed $$$10^{14}$$$. Output Output the minimum sum that Lena needs to make all purchases. Examples Input 5 2 7 2 8 1 2 2 4 1 8 Note In the first example, Lena can purchase the products in the following way: 1. one item of product $$$3$$$ for $$$2$$$ rubles, 2. one item of product $$$1$$$ for $$$2$$$ rubles, 3. one item of product $$$1$$$ for $$$2$$$ rubles, 4. one item of product $$$2$$$ for $$$1$$$ ruble (she can use the discount because $$$3$$$ items are already purchased), 5. one item of product $$$1$$$ for $$$1$$$ ruble (she can use the discount because $$$4$$$ items are already purchased). In total, she spends $$$8$$$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: 1. one item of product $$$1$$$ for $$$2$$$ rubles, 2. two items of product $$$2$$$ for $$$2$$$ rubles for each, 3. one item of product $$$5$$$ for $$$2$$$ rubles, 4. one item of product $$$3$$$ for $$$1$$$ ruble, 5. two items of product $$$4$$$ for $$$1$$$ ruble for each, 6. one item of product $$$1$$$ for $$$1$$$ ruble. In total, she spends $$$12$$$ rubles. | 1,600 | false | true | true | false | false | false | false | true | true | false | 2,945 |
1957B | Given integers $$$n$$$ and $$$k$$$, construct a sequence of $$$n$$$ non-negative (i.e. $$$geq 0$$$) integers $$$a_1, a_2, ldots, a_n$$$ such that 1. $$$sumlimits_{i = 1}^n a_i = k$$$ 2. The number of $$$1$$$s in the binary representation of $$$a_1 a_2 ldots a_n$$$ is maximized, where $$$$$$ denotes the xa0β the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$1 leq k leq 10^9$$$)xa0β the number of non-negative integers to be printed and the sum respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a sequence $$$a_1, a_2, ldots, a_n$$$ on a new line that satisfies the conditions given above. If there are multiple solutions, print any of them. Example Output 5 1 2 5 0 3 1 1 32 2 12 Note In the first test case, we have to print exactly one integer, hence we can only output $$$5$$$ as the answer. In the second test case, we output $$$1, 2$$$ which sum up to $$$3$$$, and $$$1 2 = (11)_2$$$ has two $$$1$$$s in its binary representation, which is the maximum we can achieve in these constraints. In the fourth test case, we output $$$3, 1, 1, 32, 2, 12$$$ which sum up to $$$51$$$, and $$$3 1 1 32 2 12 = (101,111)_2$$$ has five $$$1$$$s in its binary representation, which is the maximum we can achieve in these constraints. | 1,100 | false | true | true | false | false | true | false | false | false | false | 532 |
773F | Problem - 773F - Codeforces =============== xa0 ") xa0β the size of the set. The second line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009_max__a_)xa0β the elements of the set in increasing order. If you don't pay attention to the problem solution, it looks fairly easy to generate a good test case for this problem. Let _n_u2009=u2009_max__n_, take random distinct _a__i_ from 1 to _max__a_, sort them... Soon you understand that it's not that easy. Here is the actual problem solution. Let _g_ be the greatest common divisor of _a_1,u2009_a_2,u2009...,u2009_a__n_. Let _x_u2009=u2009_a__n_u2009/u2009_g_u2009-u2009_n_. Then the correct solution outputs "Alice" if _x_ is odd, and "Bob" if _x_ is even. Consider two wrong solutions to this problem which differ from the correct one only in the formula for calculating _x_. The first wrong solution calculates _x_ as _x_u2009=u2009_a__n_u2009/u2009_g_ (without subtracting _n_). The second wrong solution calculates _x_ as _x_u2009=u2009_a__n_u2009-u2009_n_ (without dividing by _g_). A test case is interesting if it makes both wrong solutions output an incorrect answer. Given _max__n_, _max__a_ and _q_, find the number of interesting test cases satisfying the constraints, and output it modulo _q_. Input The only line contains three integers _max__n_, _max__a_ and _q_ (1u2009β€u2009_max__n_u2009β€u200930u2009000; _max__n_u2009β€u2009_max__a_u2009β€u2009109; 104u2009β€u2009_q_u2009β€u2009105u2009+u2009129). Output Output a single integerxa0β the number of test cases which satisfy the constraints and make both wrong solutions output an incorrect answer, modulo _q_. Examples Input 3 6 100000 Output 4 Input 6 21 100129 Output 154 Input 58 787788 50216 Output 46009 Note In the first example, interesting test cases look as follows: 1 1 1 3 2 4 6 2 4 6 | 3,400 | true | false | false | true | false | false | false | false | false | false | 6,682 |
1702A | At the store, the salespeople want to make all prices round. In this problem, a number that is a power of $$$10$$$ is called a round number. For example, the numbers $$$10^0 = 1$$$, $$$10^1 = 10$$$, $$$10^2 = 100$$$ are round numbers, but $$$20$$$, $$$110$$$ and $$$256$$$ are not round numbers. So, if an item is worth $$$m$$$ bourles (the value of the item is not greater than $$$10^9$$$), the sellers want to change its value to the nearest round number that is not greater than $$$m$$$. They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $$$10^k$$$ bourles, where the value of $$$k$$$xa0β is the maximum possible ($$$k$$$xa0β any non-negative integer). For example, let the item have a value of $$$178$$$-bourles. Then the new price of the item will be $$$100$$$, and the answer will be $$$178-100=78$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases . Each test case is a string containing a single integer $$$m$$$ ($$$1 le m le 10^9$$$)xa0β the price of the item. Output For each test case, output on a separate line a single integer $$$d$$$ ($$$0 le d < m$$$) such that if you reduce the cost of the item by $$$d$$$ bourles, the cost of the item will be the maximal possible round number. More formally: $$$m - d = 10^k$$$, where $$$k$$$xa0β the maximum possible non-negative integer. Example Input 7 1 2 178 20 999999999 9000 987654321 Output 0 1 78 10 899999999 8000 887654321 Note In the example: $$$1 - 0 = 10^0$$$, $$$2 - 1 = 10^0$$$, $$$178 - 78 = 10^2$$$, $$$20 - 10 = 10^1$$$, $$$999999999 - 899999999 = 10^8$$$, $$$9000 - 8000 = 10^3$$$, $$$987654321 - 887654321 = 10^8$$$. Note that in each test case, we get the maximum possible round number. | 800 | false | false | false | false | false | true | false | false | false | false | 2,070 |
660E | Problem - 660E - Codeforces =============== xa0 ]( "Educational Codeforces Round 11") as the number of distinct subsequences of _a_ (including the empty subsequence). You are given two positive integers _n_ and _m_. Let _S_ be the set of all sequences of length _n_ consisting of numbers from 1 to _m_. Compute the sum _f_(_a_) over all _a_ in _S_ modulo 109u2009+u20097. Input The only line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009106) β the number of elements in arrays and the upper bound for elements. Output Print the only integer _c_ β the desired sum modulo 109u2009+u20097. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 | 2,300 | true | false | false | false | false | false | false | false | false | false | 7,178 |
1775E | Petya and his friend, the robot Petya++, went to BFDMONCON, where the costume contest is taking place today. While walking through the festival, they came across a scientific stand named after Professor Oak and Golfball, where they were asked to solve an interesting problem. Given a sequence of numbers $$$a_1, a_2, dots, a_n$$$ you can perform several operations on this sequence. Each operation should look as follows. You choose some subsequence$$$^dagger$$$. Then you call all the numbers at odd positions in this subsequence northern, and all the numbers at even positions in this subsequence southern. In this case, only the position of the number in the subsequence is taken into account, not in the original sequence. For example, consider the sequence $$$1, 4, 2, 8, 5, 7, 3, 6, 9$$$ and its subsequence (shown in bold) $$$1, mathbf{4}, mathbf{2}, 8, mathbf{5}, 7, 3, mathbf{6}, 9$$$. Then the numbers $$$4$$$ and $$$5$$$ are northern, and the numbers $$$2$$$ and $$$6$$$ are southern. After that, you can do one of the following: add $$$1$$$ to all northern numbers and subtract $$$1$$$ from all south numbers; or add $$$1$$$ to all southern numbers and subtract $$$1$$$ from all northern numbers. Thus, from the sequence $$$1, mathbf{4}, mathbf{2}, 8, mathbf{5}, 7, 3, mathbf{6}, 9$$$, if you choose the subsequence shown in bold, you can get either $$$1, mathbf{5}, mathbf{1}, 8, mathbf{6}, 7, 3, mathbf{5}, 9$$$ or $$$1, mathbf{3}, mathbf{3}, 8, mathbf{4}, 7, 3, mathbf{7}, 9$$$. Then the operation ends. Note also that all operations are independent, i.xa0e. the numbers are no longer called northern or southern when one operation ends. It is necessary to turn all the numbers of the sequence into zeros using the operations described above. Since there is very little time left before the costume contest, the friends want to know, what is the minimum number of operations required for this. The friends were unable to solve this problem, so can you help them? $$$^dagger$$$ A sequence $$$c$$$ is a subsequence of a sequence $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by the deletion of several (possibly, zero or all) elements. Note In the first test case, the sequence of operations is as follows: $$$mathbf{1}, 2, mathbf{-3} longrightarrow 0, mathbf{2}, mathbf{-2} longrightarrow 0, mathbf{1}, mathbf{-1} longrightarrow 0, 0, 0$$$. In the second test case, the sequence looks like this: $$$mathbf{1}, 0, 0, mathbf{-1}, -1 longrightarrow 0, 0, 0, 0, mathbf{-1} longrightarrow 0, 0, 0, 0, 0$$$. In the fourth test case, simply select the entire sequence as a subsequence, then subtract one from the northern numbers and add one to the southern numbers. Thus, the sequence will be nulled in one operation. In the fifth test case, you don't need to do any operations, since the sequence already consists of zeros. | 2,100 | false | true | true | false | false | false | false | false | false | false | 1,630 |
1989B | You are given two strings $$$a$$$ and $$$b$$$, both consisting of lowercase Latin letters. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string. For example, consider the string abac: a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences; a, b, c, ab, ba, ac, aba, bac and abac are its substrings. Your task is to calculate the minimum possible length of the string that contains $$$a$$$ as a substring and $$$b$$$ as a subsequence. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$)xa0β the number of test cases. The first line of each test case contains a string $$$a$$$ ($$$1 le a le 100$$$), consisting of lowercase Latin letters. The second line of each test case contains a string $$$b$$$ ($$$1 le b le 100$$$), consisting of lowercase Latin letters. Output For each test case, print a single integerxa0β the minimum possible length of the string that contains $$$a$$$ as a substring and $$$b$$$ as a subsequence. Example Input 5 aba cb er cf mmm mmm contest test cde abcefg Note In the examples below, the characters that correspond to the subsequence equal to $$$b$$$ are bolded. In the first example, one of the possible answers is caba. In the second example, one of the possible answers is ercf. In the third example, one of the possible answers is mmm. In the fourth example, one of the possible answers is contest. In the fifth example, one of the possible answers is abcdefg. | 1,200 | false | true | false | false | false | false | true | false | false | false | 340 |
714A | Problem - 714A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation math *1100 No tag edit access β Contest materials , providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks. Output Print one integerxa0β the number of minutes Sonya and Filya will be able to spend together. Examples Input 1 10 9 20 1 Output 2 Input 1 100 50 200 75 Output 50 Note In the first sample, they will be together during minutes 9 and 10. In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100. | 1,100 | true | false | true | false | false | false | false | false | false | false | 6,961 |
1626B | You are given a decimal representation of an integer $$$x$$$ without leading zeros. You have to perform the following reduction on it exactly once: take two neighboring digits in $$$x$$$ and replace them with their sum without leading zeros (if the sum is $$$0$$$, it's represented as a single $$$0$$$). For example, if $$$x = 10057$$$, the possible reductions are: choose the first and the second digits $$$1$$$ and $$$0$$$, replace them with $$$1+0=1$$$; the result is $$$1057$$$; choose the second and the third digits $$$0$$$ and $$$0$$$, replace them with $$$0+0=0$$$; the result is also $$$1057$$$; choose the third and the fourth digits $$$0$$$ and $$$5$$$, replace them with $$$0+5=5$$$; the result is still $$$1057$$$; choose the fourth and the fifth digits $$$5$$$ and $$$7$$$, replace them with $$$5+7=12$$$; the result is $$$10012$$$. What's the largest number that can be obtained? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. Each testcase consists of a single integer $$$x$$$ ($$$10 le x < 10^{200000}$$$). $$$x$$$ doesn't contain leading zeros. The total length of the decimal representations of $$$x$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0β the largest number that can be obtained after the reduction is applied exactly once. The number should not contain leading zeros. Note The first testcase of the example is already explained in the statement. In the second testcase, there is only one possible reduction: the first and the second digits. | 1,100 | false | true | false | false | false | false | false | false | false | false | 2,486 |
168A | Some country is populated by wizards. They want to organize a demonstration. There are _n_ people living in the city, _x_ of them are the wizards who will surely go to the demonstration. Other city people (_n_u2009-u2009_x_ people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least _y_ percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration. So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only _n_ people and not containing any clone puppets. Help the wizards and find the minimum number of clones to create to that the demonstration had no less than _y_ percent of the city people. Input The first line contains three space-separated integers, _n_, _x_, _y_ (1u2009β€u2009_n_,u2009_x_,u2009_y_u2009β€u2009104,u2009_x_u2009β€u2009_n_) β the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly. Please note that _y_ can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city (u2009>u2009_n_). Output Print a single integer β the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than _y_ percent of _n_ (the real total city population). Note In the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone. In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones. | 900 | true | false | true | false | false | false | false | false | false | false | 9,190 |
906B | Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating." The class can be represented as a matrix with _n_ rows and _m_ columns with a student in each cell. Two students are neighbors if cells in which they sit have a common side. Let's enumerate students from 1 to _n_Β·_m_ in order of rows. So a student who initially sits in the cell in row _i_ and column _j_ has a number (_i_u2009-u20091)Β·_m_u2009+u2009_j_. You have to find a matrix with _n_ rows and _m_ columns in which all numbers from 1 to _n_Β·_m_ appear exactly once and adjacent numbers in the original matrix are not adjacent in it, or determine that there is no such matrix. Input The only line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009105; _n_Β·_m_u2009β€u2009105)xa0β the number of rows and the number of columns in the required matrix. Output If there is no such matrix, output "NO" (without quotes). Otherwise in the first line output "YES" (without quotes), and in the next _n_ lines output _m_ integers which form the required matrix. Note In the first test case the matrix initially looks like this: 1 2 3 4 5 6 7 8 It's easy to see that there are no two students that are adjacent in both matrices. In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors. | 2,200 | true | false | false | false | false | true | true | false | false | false | 6,126 |
1367F2 | # Flying Sort (Hard Version) Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on n are greater than in the easy version of the problem. You are given an array a of n integers (the given array can contain equal elements) . You can perform the following operations on array elements: 1. choose any index i (1 β€ i β€ n) and move the element a[i] to the begin of the array; 2. choose any index i (1 β€ i β€ n) and move the element a[i] to the end of the array. For example, if n = 5 , a = [4 , 7, 2, 2, 9] , then the following sequence of operations can be performed: β’ after performing the operation of the first type to the second element, the array a will become [7 , 4, 2, 2, 9] ; β’ after performing the operation of the second type to the second element, the array a will become [7 , 2, 2, 9, 4] .You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the a array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities a[1] β€ a[2] β€ . . . β€ a[n]. # Input The first line contains a single integer t (1 β€ t β€ 10 4) x16 the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing an integer n (1 β€ n β€ 2 Β· 10 5) x16 the size of the array a.Then follow n integers a1, a 2, . . . , a n (0 β€ ai β€ 10 9) x16 an array that needs to be sorted by the given operations. The given array can contain equal elements .The sum of n for all test cases in one test does not exceed 2 Β· 10 5. # Output For each test case output one integer x16 the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. Page 1 of 2 Example standard input standard output 954 7 2 2 9 53 5 8 1 7 51 2 2 4 5 20 1 30 1 0 40 1 0 0 40 1 0 1 40 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 2200111116 # Note In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: [4 , 7, 2, 2, 9] β [2 , 4, 7, 2, 9] β [2 , 2, 4, 7, 9] .In the second test case, you need to move the 1 to the beginning of the array, and the 8 x16 to the end. Therefore, the desired sequence of operations: [3 , 5, 8, 1, 7] β [1 , 3, 5, 8, 7] β [1 , 3, 5, 7, 8] .In the third test case, the array is already sorted. Page 2 of 2 | 2,400 | false | true | false | true | true | false | false | true | true | false | 3,837 |
1650D | Petya got an array $$$a$$$ of numbers from $$$1$$$ to $$$n$$$, where $$$a[i]=i$$$. He performed $$$n$$$ operations sequentially. In the end, he received a new state of the $$$a$$$ array. At the $$$i$$$-th operation, Petya chose the first $$$i$$$ elements of the array and cyclically shifted them to the right an arbitrary number of times (elements with indexes $$$i+1$$$ and more remain in their places). One cyclic shift to the right is such a transformation that the array $$$a=[a_1, a_2, dots, a_n]$$$ becomes equal to the array $$$a = [a_i, a_1, a_2, dots, a_{i-2}, a_{i-1}, a_{i+1}, a_{i+2}, dots, a_n]$$$. For example, if $$$a = [5,4,2,1,3]$$$ and $$$i=3$$$ (that is, this is the third operation), then as a result of this operation, he could get any of these three arrays: $$$a = [5,4,2,1,3]$$$ (makes $$$0$$$ cyclic shifts, or any number that is divisible by $$$3$$$); $$$a = [2,5,4,1,3]$$$ (makes $$$1$$$ cyclic shift, or any number that has a remainder of $$$1$$$ when divided by $$$3$$$); $$$a = [4,2,5,1,3]$$$ (makes $$$2$$$ cyclic shifts, or any number that has a remainder of $$$2$$$ when divided by $$$3$$$). Let's look at an example. Let $$$n=6$$$, i.e. initially $$$a=[1,2,3,4,5,6]$$$. A possible scenario is described below. $$$i=1$$$: no matter how many cyclic shifts Petya makes, the array $$$a$$$ does not change. $$$i=2$$$: let's say Petya decided to make a $$$1$$$ cyclic shift, then the array will look like $$$a = [ extbf{2}, extbf{1}, 3, 4, 5, 6]$$$. $$$i=3$$$: let's say Petya decided to make $$$1$$$ cyclic shift, then the array will look like $$$a = [ extbf{3}, extbf{2}, extbf{1}, 4, 5, 6]$$$. $$$i=4$$$: let's say Petya decided to make $$$2$$$ cyclic shifts, the original array will look like $$$a = [ extbf{1}, extbf{4}, extbf{3}, extbf{2}, 5, 6]$$$. $$$i=5$$$: let's say Petya decided to make $$$0$$$ cyclic shifts, then the array won't change. $$$i=6$$$: let's say Petya decided to make $$$4$$$ cyclic shifts, the array will look like $$$a = [ extbf{3}, extbf{2}, extbf{5}, extbf{6}, extbf{1}, extbf{4}]$$$. You are given a final array state $$$a$$$ after all $$$n$$$ operations. Determine if there is a way to perform the operation that produces this result. In this case, if an answer exists, print the numbers of cyclical shifts that occurred during each of the $$$n$$$ operations. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases in the test. The descriptions of the test cases follow. The first line of the description of each test case contains one integer $$$n$$$ ($$$2 le n le 2cdot10^3$$$)xa0β the length of the array $$$a$$$. The next line contains the final state of the array $$$a$$$: $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) are written. All $$$a_i$$$ are distinct. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2cdot10^3$$$. Output For each test case, print the answer on a separate line. Print -1 if the given final value $$$a$$$ cannot be obtained by performing an arbitrary number of cyclic shifts on each operation. Otherwise, print $$$n$$$ non-negative integers $$$d_1, d_2, dots, d_n$$$ ($$$d_i ge 0$$$), where $$$d_i$$$ means that during the $$$i$$$-th operation the first $$$i$$$ elements of the array were cyclic shifted to the right $$$d_i$$$ times. If there are several possible answers, print the one where the total number of shifts is minimal (that is, the sum of $$$d_i$$$ values is the smallest). If there are several such answers, print any of them. Note The first test case matches the example from the statement. The second set of input data is simple. Note that the answer $$$[3, 2, 1]$$$ also gives the same permutation, but since the total number of shifts $$$3+2+1$$$ is greater than $$$0+0+1$$$, this answer is not correct. | 1,300 | true | false | true | false | false | true | true | false | false | false | 2,372 |
663A | Problem - 663A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms expression parsing greedy math *1800 No tag edit access β Contest materials if rebus has a solution and "Impossible" (without quotes) otherwise. If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to _n_. Follow the format given in the samples. Examples Input ? + ? - ? + ? + ? = 42 Output Possible 9 + 13 - 39 + 28 + 31 = 42 Input ? - ? = 1 Output Impossible Input ? = 1000000 Output Possible 1000000 = 1000000 | 1,800 | true | true | false | false | false | true | false | false | false | false | 7,163 |
451B | Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array _a_ consisting of _n_ distinct integers. Unfortunately, the size of _a_ is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array _a_ (in increasing order) by reversing exactly one segment of _a_? See definitions of segment and reversing in the notes. Input The first line of the input contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the size of array _a_. The second line contains _n_ distinct space-separated integers: _a_[1],u2009_a_[2],u2009...,u2009_a_[_n_] (1u2009β€u2009_a_[_i_]u2009β€u2009109). Output Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them. Note Sample 1. You can reverse the entire array to get [1,u20092,u20093], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [_l_,u2009_r_] of array _a_ is the sequence _a_[_l_],u2009_a_[_l_u2009+u20091],u2009...,u2009_a_[_r_]. If you have an array _a_ of size _n_ and you reverse its segment [_l_,u2009_r_], the array will become: _a_[1],u2009_a_[2],u2009...,u2009_a_[_l_u2009-u20092],u2009_a_[_l_u2009-u20091],u2009_a_[_r_],u2009_a_[_r_u2009-u20091],u2009...,u2009_a_[_l_u2009+u20091],u2009_a_[_l_],u2009_a_[_r_u2009+u20091],u2009_a_[_r_u2009+u20092],u2009...,u2009_a_[_n_u2009-u20091],u2009_a_[_n_]. | 1,300 | false | false | true | false | false | false | false | false | true | false | 8,036 |
476C | Problem - 476C - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags math *1600 No tag edit access β Contest materials . Can you compute it faster than Dreamoon? Input The single line of the input contains two integers _a_, _b_ (1u2009β€u2009_a_,u2009_b_u2009β€u2009107). Output Print a single integer representing the answer modulo 1u2009000u2009000u2009007 (109u2009+u20097). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because is always zero. For the second sample, the set of nice integers is {3,u20095}. | 1,600 | true | false | false | false | false | false | false | false | false | false | 7,926 |
65B | The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates. So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes. Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it. Input The first input line contains an integer _n_ (1u2009β€u2009_n_u2009β€u20091000). It represents the number of dates in Harry's notes. Next _n_ lines contain the actual dates _y_1, _y_2, ..., _y__n_, each line contains a date. Each date is a four-digit integer (1000u2009β€u2009_y__i_u2009β€u20099999). Output Print _n_ numbers _z_1, _z_2, ..., _z__n_ (1000u2009β€u2009_z__i_u2009β€u20092011). They are Ron's resulting dates. Print each number on a single line. Numbers _z__i_ must form the non-decreasing sequence. Each number _z__i_ should differ from the corresponding date _y__i_ in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print "No solution" (without the quotes). | 1,700 | false | true | true | false | false | false | true | false | false | false | 9,645 |
10A | Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes _P_1 watt per minute. _T_1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to _P_2 watt per minute. Finally, after _T_2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes _P_3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into _n_ time periods [_l_1,u2009_r_1],u2009[_l_2,u2009_r_2],u2009...,u2009[_l__n_,u2009_r__n_]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [_l_1,u2009_r__n_]. Input The first line contains 6 integer numbers _n_, _P_1, _P_2, _P_3, _T_1, _T_2 (1u2009β€u2009_n_u2009β€u2009100,u20090u2009β€u2009_P_1,u2009_P_2,u2009_P_3u2009β€u2009100,u20091u2009β€u2009_T_1,u2009_T_2u2009β€u200960). The following _n_ lines contain description of Tom's work. Each _i_-th of these lines contains two space-separated integers _l__i_ and _r__i_ (0u2009β€u2009_l__i_u2009<u2009_r__i_u2009β€u20091440, _r__i_u2009<u2009_l__i_u2009+u20091 for _i_u2009<u2009_n_), which stand for the start and the end of the _i_-th period of work. Output Output the answer to the problem. Examples Input 2 8 4 2 5 10 20 30 50 100 | 900 | false | false | true | false | false | false | false | false | false | false | 9,945 |
998B | There are a lot of things which could be cutxa0β trees, paper, "the rope". In this problem you are going to cut a sequence of integers. There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers. Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, $$$[4, 1, 2, 3, 4, 5, 4, 4, 5, 5]$$$ $$$ o$$$ two cuts $$$ o$$$ $$$[4, 1 2, 3, 4, 5 4, 4, 5, 5]$$$. On each segment the number of even elements should be equal to the number of odd elements. The cost of the cut between $$$x$$$ and $$$y$$$ numbers is $$$x - y$$$ bitcoins. Find the maximum possible number of cuts that can be made while spending no more than $$$B$$$ bitcoins. Input First line of the input contains an integer $$$n$$$ ($$$2 le n le 100$$$) and an integer $$$B$$$ ($$$1 le B le 100$$$)xa0β the number of elements in the sequence and the number of bitcoins you have. Second line contains $$$n$$$ integers: $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 100$$$)xa0β elements of the sequence, which contains the equal number of even and odd numbers Note In the first sample the optimal answer is to split sequence between $$$2$$$ and $$$5$$$. Price of this cut is equal to $$$3$$$ bitcoins. In the second sample it is not possible to make even one cut even with unlimited number of bitcoins. In the third sample the sequence should be cut between $$$2$$$ and $$$3$$$, and between $$$4$$$ and $$$5$$$. The total price of the cuts is $$$1 + 1 = 2$$$ bitcoins. | 1,200 | false | true | false | true | false | false | false | false | true | false | 5,712 |
1896C | You are given two arrays $$$a$$$ and $$$b$$$ of size $$$n$$$. The beauty of the arrays $$$a$$$ and $$$b$$$ is the number of indices $$$i$$$ such that $$$a_i > b_i$$$. You are also given an integer $$$x$$$. Determine whether it is possible to rearrange the elements of $$$b$$$ such that the beauty of the arrays becomes $$$x$$$. If it is possible, output one valid rearrangement of $$$b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 2cdot 10^5$$$, $$$0 le x le n$$$)xa0β the size of arrays $$$a$$$ and $$$b$$$ and the desired beauty of the arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2n$$$)xa0β the elements of array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 2n$$$)xa0β the elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output "NO" if it is not possible to rearrange $$$b$$$ to make the beauty of the arrays equal to $$$x$$$. Otherwise, output "YES". Then, on the next line, output $$$n$$$ integers which represent the rearrangement of $$$b$$$. If there are multiple solutions, you may output any of them. You can output "YES" and "NO" in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 7 1 0 1 2 1 1 1 2 3 0 2 4 3 4 1 2 3 1 2 4 3 4 1 2 3 2 2 4 3 4 1 2 3 3 2 4 3 4 1 2 5 2 6 4 5 6 2 9 7 9 1 1 Output YES 2 NO NO YES 2 4 1 YES 4 1 2 NO YES 1 9 9 7 1 Note In test cases 1 and 2, the beauty of the arrays has to be $$$0$$$ since $$$a_1 = 1 le 2 = b_1$$$. In test cases 3, 4, 5 and 6, the only possible beauty of the arrays is $$$x = 1$$$ and $$$x = 2$$$. In particular, if $$$b$$$ is rearranged to $$$[2, 4, 1]$$$, then $$$a_3 = 3 > 1 = b_3$$$, so the beauty of the arrays is $$$1$$$. If $$$b$$$ is kept in the same order as given the input, then $$$a_2 = 4 > b_2 = 1$$$ and $$$a_3 = 3 > 2 = b_3$$$, so the beauty of the arrays is $$$2$$$. | 1,400 | false | true | false | false | false | true | false | true | true | false | 935 |
1105B | Given a string $$$s$$$ of length $$$n$$$ and integer $$$k$$$ ($$$1 le k le n$$$). The string $$$s$$$ has a level $$$x$$$, if $$$x$$$ is largest non-negative integer, such that it's possible to find in $$$s$$$: $$$x$$$ non-intersecting (non-overlapping) substrings of length $$$k$$$, all characters of these $$$x$$$ substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings). A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers $$$i$$$ and $$$j$$$ ($$$1 le i le j le n$$$), denoted as $$$s[i dots j]$$$ = "$$$s_{i}s_{i+1} dots s_{j}$$$". For example, if $$$k = 2$$$, then: the string "aabb" has level $$$1$$$ (you can select substring "aa"), the strings "zzzz" and "zzbzz" has level $$$2$$$ (you can select two non-intersecting substrings "zz" in each of them), the strings "abed" and "aca" have level $$$0$$$ (you can't find at least one substring of the length $$$k=2$$$ containing the only distinct character). Zuhair gave you the integer $$$k$$$ and the string $$$s$$$ of length $$$n$$$. You need to find $$$x$$$, the level of the string $$$s$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$)xa0β the length of the string and the value of $$$k$$$. The second line contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. Output Print a single integer $$$x$$$xa0β the level of the string. Note In the first example, we can select $$$2$$$ non-intersecting substrings consisting of letter 'a': "(aa)ac(aa)bb", so the level is $$$2$$$. In the second example, we can select either substring "a" or "b" to get the answer $$$1$$$. | 1,100 | false | false | true | false | false | false | true | false | false | false | 5,179 |
187B | PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features. AlgoRace is a special league of car racing where different teams compete in a country of _n_ cities. Cities are numbered 1 through _n_. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars. The competition is held in _r_ rounds. In _i_-th round, drivers will start at city _s__i_ and finish at city _t__i_. Drivers are allowed to change their cars at most _k__i_ times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed _k__i_. Drivers can freely choose their path to destination. PMP has prepared _m_ type of purpose-built cars. Beside for PMPβs driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times. PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it. Input The first line contains three space-separated integers _n_,u2009_m_,u2009_r_ (2u2009β€u2009_n_u2009β€u200960,u20091u2009β€u2009_m_u2009β€u200960,u20091u2009β€u2009_r_u2009β€u2009105) β the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly. Next _m_ sets of _n_u2009Γu2009_n_ matrices of integers between 0 to 106 (inclusive) will follow β describing the time one car requires to traverse different roads. The _k_-th integer in _j_-th line of the _i_-th set is the time that _i_-th car requires to traverse the road from _j_-th city to _k_-th city. These matrices are not necessarily symmetric, but their diagonal is always zero. Next _r_ lines contain description of the rounds. The _i_-th of these lines contains space-separated integers _s__i_,u2009_t__i_,u2009_k__i_ (1u2009β€u2009_s__i_,u2009_t__i_u2009β€u2009_n_,u2009_s__i_u2009β u2009_t__i_,u20090u2009β€u2009_k__i_u2009β€u20091000) β the number of starting city, finishing city and the number of possible car changes in _i_-th round, correspondingly. Output For each round you should print the minimum required time to complete the round in a single line. Examples Input 4 2 3 0 1 5 6 2 0 3 6 1 3 0 1 6 6 7 0 0 3 5 6 2 0 1 6 1 3 0 2 6 6 7 0 1 4 2 1 4 1 1 4 3 Input 4 2 3 0 7 3 3 8 0 10 5 1 1 0 4 8 9 2 0 0 3 3 9 7 0 4 9 3 8 0 4 4 8 9 0 2 3 3 2 1 3 1 2 2 Note In the first sample, in all rounds PMP goes from city #1 to city #2, then city #3 and finally city #4. But the sequences of types of the cars he uses are (1, 2, 1) in the first round and (1, 2, 2) in the second round. In the third round, although he can change his car three times, he uses the same strategy as the first round which only needs two car changes. | 1,800 | false | false | false | true | false | false | false | false | false | false | 9,096 |
1070K | Polycarp took $$$n$$$ videos, the duration of the $$$i$$$-th video is $$$a_i$$$ seconds. The videos are listed in the chronological order, i.e. the $$$1$$$-st video is the earliest, the $$$2$$$-nd video is the next, ..., the $$$n$$$-th video is the last. Now Polycarp wants to publish exactly $$$k$$$ ($$$1 le k le n$$$) posts in Instabram. Each video should be a part of a single post. The posts should preserve the chronological order, it means that the first post should contain one or more of the earliest videos, the second post should contain a block (one or more videos) going next and so on. In other words, if the number of videos in the $$$j$$$-th post is $$$s_j$$$ then: $$$s_1+s_2+dots+s_k=n$$$ ($$$s_i>0$$$), the first post contains the videos: $$$1, 2, dots, s_1$$$; the second post contains the videos: $$$s_1+1, s_1+2, dots, s_1+s_2$$$; the third post contains the videos: $$$s_1+s_2+1, s_1+s_2+2, dots, s_1+s_2+s_3$$$; ... the $$$k$$$-th post contains videos: $$$n-s_k+1,n-s_k+2,dots,n$$$. Polycarp is a perfectionist, he wants the total duration of videos in each post to be the same. Help Polycarp to find such positive integer values $$$s_1, s_2, dots, s_k$$$ that satisfy all the conditions above. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^5$$$). The next line contains $$$n$$$ positive integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^4$$$), where $$$a_i$$$ is the duration of the $$$i$$$-th video. Output If solution exists, print "Yes" in the first line. Print $$$k$$$ positive integers $$$s_1, s_2, dots, s_k$$$ ($$$s_1+s_2+dots+s_k=n$$$) in the second line. The total duration of videos in each post should be the same. It can be easily proven that the answer is unique (if it exists). If there is no solution, print a single line "No". | 1,100 | false | false | true | false | false | false | false | false | false | false | 5,357 |
1098B | Problem - 1098B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force constructive algorithms greedy math *2100 No tag edit access β Contest materials , that differs from the given table in the minimum number of characters. Input First line contains two positive integers $$$n$$$ and $$$m$$$xa0β number of rows and columns in the table you are given ($$$2 leq n, m, n imes m leq 300,000$$$). Then, $$$n$$$ lines describing the table follow. Each line contains exactly $$$m$$$ characters Β«AΒ», Β«GΒ», Β«CΒ», Β«TΒ». Output Output $$$n$$$ lines, $$$m$$$ characters each. This table must be nice and differ from the input table in the minimum number of characters. Examples Input 2 2 AG CT Output AG CT Input 3 5 AGCAG AGCAG AGCAG Output TGCAT CATGC TGCAT Note In the first sample, the table is already nice. In the second sample, you can change 9 elements to make the table nice. | 2,100 | true | true | false | false | false | true | true | false | false | false | 5,215 |
1534B | Little Dormi received a histogram with $$$n$$$ bars of height $$$a_1, a_2, ldots, a_n$$$ for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking. To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times: Select an index $$$i$$$ ($$$1 le i le n$$$) where $$$a_i>0$$$, and assign $$$a_i := a_i-1$$$. Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations. However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness. Consider the following example where the histogram has $$$4$$$ columns of heights $$$4,8,9,6$$$: The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is $$$4+4+1+3+6 = 18$$$, so if Little Dormi does not modify the histogram at all, the ugliness would be $$$18$$$. However, Little Dormi can apply the operation once on column $$$2$$$ and twice on column $$$3$$$, resulting in a histogram with heights $$$4,7,7,6$$$: Now, as the total vertical length of the outline (red lines) is $$$4+3+1+6=14$$$, the ugliness is $$$14+3=17$$$ dollars. It can be proven that this is optimal. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Note Example $$$1$$$ is the example described in the statement. The initial histogram for example $$$2$$$ is given below: The ugliness is currently $$$2+1+6+3+4=16$$$. By applying the operation once on column $$$1$$$, six times on column $$$3$$$, and three times on column $$$4$$$, we can end up with a histogram with heights $$$1,1,1,1,0,0$$$: The vertical length of the outline is now $$$1+1=2$$$ and Little Dormi made $$$1+6+3=10$$$ operations, so the final ugliness is $$$2+10=12$$$, which can be proven to be optimal. | 1,100 | true | true | true | false | false | false | false | false | false | false | 2,982 |
679B | Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side _a_ has volume _a_3. A tower consisting of blocks with sides _a_1,u2009_a_2,u2009...,u2009_a__k_ has the total volume _a_13u2009+u2009_a_23u2009+u2009...u2009+u2009_a__k_3. Limak is going to build a tower. First, he asks you to tell him a positive integer _X_xa0β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed _X_. Limak asks you to choose _X_ not greater than _m_. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize _X_. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum _X_u2009β€u2009_m_ that results this number of blocks. Input The only line of the input contains one integer _m_ (1u2009β€u2009_m_u2009β€u20091015), meaning that Limak wants you to choose _X_ between 1 and _m_, inclusive. Output Print two integersxa0β the maximum number of blocks in the tower and the maximum required total volume _X_, resulting in the maximum number of blocks. Note In the first sample test, there will be 9 blocks if you choose _X_u2009=u200923 or _X_u2009=u200942. Limak wants to maximize _X_ secondarily so you should choose 42. In more detail, after choosing _X_u2009=u200942 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42u2009-u200927u2009=u200915. The second added block has side 2, so the remaining volume is 15u2009-u20098u2009=u20097. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 33u2009+u200923u2009+u20097Β·13u2009=u200927u2009+u20098u2009+u20097u2009=u200942. | 2,200 | false | true | false | true | false | false | false | true | false | false | 7,109 |
1216B | Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed $$$n$$$ cans in a row on a table. Cans are numbered from left to right from $$$1$$$ to $$$n$$$. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he will knock the cans down. Vasya knows that the durability of the $$$i$$$-th can is $$$a_i$$$. It means that if Vasya has already knocked $$$x$$$ cans down and is now about to start shooting the $$$i$$$-th one, he will need $$$(a_i cdot x + 1)$$$ shots to knock it down. You can assume that if Vasya starts shooting the $$$i$$$-th can, he will be shooting it until he knocks it down. Your task is to choose such an order of shooting so that the number of shots required to knock each of the $$$n$$$ given cans down exactly once is minimum possible. Input The first line of the input contains one integer $$$n$$$ $$$(2 le n le 1,000)$$$ β the number of cans. The second line of the input contains the sequence $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 1,000)$$$, where $$$a_i$$$ is the durability of the $$$i$$$-th can. Output In the first line print the minimum number of shots required to knock each of the $$$n$$$ given cans down exactly once. In the second line print the sequence consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ β the order of indices of cans that minimizes the number of shots required. If there are several answers, you can print any of them. Note In the first example Vasya can start shooting from the first can. He knocks it down with the first shot because he haven't knocked any other cans down before. After that he has to shoot the third can. To knock it down he shoots $$$20 cdot 1 + 1 = 21$$$ times. After that only second can remains. To knock it down Vasya shoots $$$10 cdot 2 + 1 = 21$$$ times. So the total number of shots is $$$1 + 21 + 21 = 43$$$. In the second example the order of shooting does not matter because all cans have the same durability. | 900 | false | true | true | false | false | false | false | false | true | false | 4,596 |
1983D | Given two arrays of distinct positive integers $$$a$$$ and $$$b$$$ of length $$$n$$$, we would like to make both the arrays the same. Two arrays $$$x$$$ and $$$y$$$ of length $$$k$$$ are said to be the same when for all $$$1 le i le k$$$, $$$x_i = y_i$$$. Now in one move, you can choose some index $$$l$$$ and $$$r$$$ in $$$a$$$ ($$$l le r$$$) and swap $$$a_l$$$ and $$$a_r$$$, then choose some $$$p$$$ and $$$q$$$ ($$$p le q$$$) in $$$b$$$ such that $$$r-l=q-p$$$ and swap $$$b_p$$$ and $$$b_q$$$. Is it possible to make both arrays the same? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the length of the arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ distinct integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$)xa0β the integers in the array $$$a$$$. The third line of each test case contains $$$n$$$ distinct integers $$$b_1,b_2,b_3,ldots,b_n$$$ ($$$1 le b_i le 2 cdot 10^5$$$)xa0β the integers in the array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each testcase, print "YES" if the arrays $$$a$$$ and $$$b$$$ can be made the same. Otherwise, print "NO". can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 6 4 1 2 3 4 1 2 3 4 5 1 3 4 2 5 7 1 2 5 4 4 1 2 3 4 4 3 2 1 3 1 2 3 1 3 2 5 1 5 7 1000 4 4 1 7 5 1000 3 1 4 2 1 3 2 Output YES NO YES NO NO NO Note In the first testcase, you don't need to perform any operations since the arrays are same. In the second testcase, it can be proven there exists no way to make the arrays same. In the third testcase, one of the ways to make the arrays same is to first choose $$$l=1$$$, $$$r=3$$$, $$$p=1$$$, $$$q=3$$$ then choose $$$l=1$$$, $$$r=2$$$, $$$p=3$$$, $$$q=4$$$. | 1,700 | true | true | false | false | true | true | false | false | true | false | 387 |
886C | A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really experienced, his exploration is just walking through the catacombs. Catacombs consist of several rooms and bidirectional passages between some pairs of them. Some passages can connect a room to itself and since the passages are built on different depths they do not intersect each other. Every minute Petya arbitrary chooses a passage from the room he is currently in and then reaches the room on the other end of the passage in exactly one minute. When he enters a room at minute _i_, he makes a note in his logbook with number _t__i_: If Petya has visited this room before, he writes down the minute he was in this room last time; Otherwise, Petya writes down an arbitrary non-negative integer strictly less than current minute _i_. Initially, Petya was in one of the rooms at minute 0, he didn't write down number _t_0. At some point during his wandering Petya got tired, threw out his logbook and went home. Vasya found his logbook and now he is curious: what is the minimum possible number of rooms in Paris catacombs according to Petya's logbook? Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u20092Β·105) β then number of notes in Petya's logbook. The second line contains _n_ non-negative integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (0u2009β€u2009_t__i_u2009<u2009_i_) β notes in the logbook. Output In the only line print a single integer β the minimum possible number of rooms in Paris catacombs. Note In the first sample, sequence of rooms Petya visited could be, for example 1u2009βu20091u2009βu20092, 1u2009βu20092u2009βu20091 or 1u2009βu20092u2009βu20093. The minimum possible number of rooms is 2. In the second sample, the sequence could be 1u2009βu20092u2009βu20093u2009βu20091u2009βu20092u2009βu20091. | 1,300 | false | true | true | false | false | false | false | false | false | false | 6,206 |
148D | The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains _w_ white and _b_ black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers _w_ and _b_ (0u2009β€u2009_w_,u2009_b_u2009β€u20091000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10u2009-u20099. Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 2/3 = 1/2. After this there are two mice left in the bag β one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. | 1,800 | true | false | false | true | false | false | false | false | false | false | 9,283 |
1093A | Mishka got a six-faced dice. It has integer numbers from $$$2$$$ to $$$7$$$ written on its faces (all numbers on faces are different, so this is an almost usual dice). Mishka wants to get exactly $$$x$$$ points by rolling his dice. The number of points is just a sum of numbers written at the topmost face of the dice for all the rolls Mishka makes. Mishka doesn't really care about the number of rolls, so he just wants to know any number of rolls he can make to be able to get exactly $$$x$$$ points for them. Mishka is very lucky, so if the probability to get $$$x$$$ points with chosen number of rolls is non-zero, he will be able to roll the dice in such a way. Your task is to print this number. It is guaranteed that at least one answer exists. Mishka is also very curious about different number of points to score so you have to answer $$$t$$$ independent queries. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) β the number of queries. Each of the next $$$t$$$ lines contains one integer each. The $$$i$$$-th line contains one integer $$$x_i$$$ ($$$2 le x_i le 100$$$) β the number of points Mishka wants to get. Output Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query (i.e. any number of rolls Mishka can make to be able to get exactly $$$x_i$$$ points for them). It is guaranteed that at least one answer exists. Note In the first query Mishka can roll a dice once and get $$$2$$$ points. In the second query Mishka can roll a dice $$$3$$$ times and get points $$$5$$$, $$$5$$$ and $$$3$$$ (for example). In the third query Mishka can roll a dice $$$8$$$ times and get $$$5$$$ points $$$7$$$ times and $$$2$$$ points with the remaining roll. In the fourth query Mishka can roll a dice $$$27$$$ times and get $$$2$$$ points $$$11$$$ times, $$$3$$$ points $$$6$$$ times and $$$6$$$ points $$$10$$$ times. | 800 | true | false | false | false | false | false | false | false | false | false | 5,244 |
1585D | Petya has an array of integers $$$a_1, a_2, ldots, a_n$$$. He only likes sorted arrays. Unfortunately, the given array could be arbitrary, so Petya wants to sort it. Petya likes to challenge himself, so he wants to sort array using only $$$3$$$-cycles. More formally, in one operation he can pick $$$3$$$ pairwise distinct indices $$$i$$$, $$$j$$$, and $$$k$$$ ($$$1 leq i, j, k leq n$$$) and apply $$$i o j o k o i$$$ cycle to the array $$$a$$$. It simultaneously places $$$a_i$$$ on position $$$j$$$, $$$a_j$$$ on position $$$k$$$, and $$$a_k$$$ on position $$$i$$$, without changing any other element. For example, if $$$a$$$ is $$$[10, 50, 20, 30, 40, 60]$$$ and he chooses $$$i = 2$$$, $$$j = 1$$$, $$$k = 5$$$, then the array becomes $$$[underline{50}, underline{40}, 20, 30, underline{10}, 60]$$$. Petya can apply arbitrary number of $$$3$$$-cycles (possibly, zero). You are to determine if Petya can sort his array $$$a$$$, i.xa0e. make it non-decreasing. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 5 cdot 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 5 cdot 10^5$$$)xa0β the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, print "YES" (without quotes) if Petya can sort the array $$$a$$$ using $$$3$$$-cycles, and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Note In the $$$6$$$-th test case Petya can use the $$$3$$$-cycle $$$1 o 3 o 2 o 1$$$ to sort the array. In the $$$7$$$-th test case Petya can apply $$$1 o 3 o 2 o 1$$$ and make $$$a = [1, 4, 2, 3]$$$. Then he can apply $$$2 o 4 o 3 o 2$$$ and finally sort the array. | 1,900 | true | false | false | false | true | false | false | false | false | false | 2,699 |
306D | Problem - 306D - Codeforces =============== xa0 β the number of the polygon vertexes. Output Print _n_ lines, containing the coordinates of the vertexes of the _n_-gon "_x__i_ _y__i_" in the counter clockwise order. The coordinates of the vertexes shouldn't exceed 106 in their absolute value. The side lengths should fit within limits | 2,300 | false | false | false | false | false | true | false | false | false | false | 8,608 |
1100B | Arkady coordinates rounds on some not really famous competitive programming platform. Each round features $$$n$$$ problems of distinct difficulty, the difficulties are numbered from $$$1$$$ to $$$n$$$. To hold a round Arkady needs $$$n$$$ new (not used previously) problems, one for each difficulty. As for now, Arkady creates all the problems himself, but unfortunately, he can't just create a problem of a desired difficulty. Instead, when he creates a problem, he evaluates its difficulty from $$$1$$$ to $$$n$$$ and puts it into the problems pool. At each moment when Arkady can choose a set of $$$n$$$ new problems of distinct difficulties from the pool, he holds a round with these problems and removes them from the pool. Arkady always creates one problem at a time, so if he can hold a round after creating a problem, he immediately does it. You are given a sequence of problems' difficulties in the order Arkady created them. For each problem, determine whether Arkady held the round right after creating this problem, or not. Initially the problems pool is empty. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^5$$$)xa0β the number of difficulty levels and the number of problems Arkady created. The second line contains $$$m$$$ integers $$$a_1, a_2, ldots, a_m$$$ ($$$1 le a_i le n$$$)xa0β the problems' difficulties in the order Arkady created them. Output Print a line containing $$$m$$$ digits. The $$$i$$$-th digit should be $$$1$$$ if Arkady held the round after creation of the $$$i$$$-th problem, and $$$0$$$ otherwise. Examples Input 3 11 2 3 1 2 2 2 3 2 2 3 1 Note In the first example Arkady held the round after the first three problems, because they are of distinct difficulties, and then only after the last problem. | 1,300 | false | false | true | false | true | false | false | false | false | false | 5,205 |
542D | It's tough to be a superhero. And it's twice as tough to resist the supervillain who is cool at math. Suppose that you're an ordinary Batman in an ordinary city of Gotham. Your enemy Joker mined the building of the city administration and you only have several minutes to neutralize the charge. To do that you should enter the cancel code on the bomb control panel. However, that mad man decided to give you a hint. This morning the mayor found a playing card under his pillow. There was a line written on the card: The bomb has a note saying "_J_(_x_)u2009=u2009_A_", where _A_ is some positive integer. You suspect that the cancel code is some integer _x_ that meets the equation _J_(_x_)u2009=u2009_A_. Now in order to decide whether you should neutralize the bomb or run for your life, you've got to count how many distinct positive integers _x_ meet this equation. Input The single line of the input contains a single integer _A_ (1u2009β€u2009_A_u2009β€u20091012). Output Print the number of solutions of the equation _J_(_x_)u2009=u2009_A_. Note Record _x__n_ means that number _n_ divides number _x_. is defined as the largest positive integer that divides both _a_ and _b_. In the first sample test the only suitable value of _x_ is 2. Then _J_(2)u2009=u20091u2009+u20092. In the second sample test the following values of _x_ match: _x_u2009=u200914, _J_(14)u2009=u20091u2009+u20092u2009+u20097u2009+u200914u2009=u200924 _x_u2009=u200915, _J_(15)u2009=u20091u2009+u20093u2009+u20095u2009+u200915u2009=u200924 _x_u2009=u200923, _J_(23)u2009=u20091u2009+u200923u2009=u200924 | 2,600 | true | false | false | true | false | false | false | false | false | false | 7,672 |
631C | Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are _n_ commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of _m_ managers. Each of them may reorder the elements in some order. Namely, the _i_-th manager either sorts first _r__i_ numbers in non-descending or non-ascending order and then passes the report to the manager _i_u2009+u20091, or directly to Blake (if this manager has number _i_u2009=u2009_m_). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence _a__i_ of length _n_ and the description of each manager, that is value _r__i_ and his favourite order. You are asked to speed up the process and determine how the final report will look like. Input The first line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009200u2009000)xa0β the number of commodities in the report and the number of managers, respectively. The second line contains _n_ integers _a__i_ (_a__i_u2009β€u2009109)xa0β the initial report before it gets to the first manager. Then follow _m_ lines with the descriptions of the operations managers are going to perform. The _i_-th of these lines contains two integers _t__i_ and _r__i_ (, 1u2009β€u2009_r__i_u2009β€u2009_n_), meaning that the _i_-th manager sorts the first _r__i_ numbers either in the non-descending (if _t__i_u2009=u20091) or non-ascending (if _t__i_u2009=u20092) order. Output Print _n_ integersxa0β the final report, which will be passed to Blake by manager number _m_. Note In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | 1,700 | false | false | false | false | true | false | false | false | true | false | 7,284 |
44B | Problem - 44B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1500 No tag edit access β Contest materials ") : tutorial of A-E, H, J.") : tutorial of F, G and I.") . Thus, while the organizers are having the argument, discussing different variants of buying cola, the Winter School can't start. Your task is to count the number of all the possible ways to buy exactly _n_ liters of cola and persuade the organizers that this number is too large, and if they keep on arguing, then the Winter Computer School will have to be organized in summer. All the bottles of cola are considered indistinguishable, i.e. two variants of buying are different from each other only if they differ in the number of bottles of at least one kind. Input The first line contains four integers β _n_, _a_, _b_, _c_ (1u2009β€u2009_n_u2009β€u200910000, 0u2009β€u2009_a_,u2009_b_,u2009_c_u2009β€u20095000). Output Print the unique number β the solution to the problem. If it is impossible to buy exactly _n_ liters of cola, print 0. Examples Input 10 5 5 5 Output 9 Input 3 0 0 2 Output 0 | 1,500 | false | false | true | false | false | false | false | false | false | false | 9,768 |
1621I | Consider an array of integers $$$C = [c_1, c_2, ldots, c_n]$$$ of length $$$n$$$. Let's build the sequence of arrays $$$D_0, D_1, D_2, ldots, D_{n}$$$ of length $$$n+1$$$ in the following way: The first element of this sequence will be equals $$$C$$$: $$$D_0 = C$$$. For each $$$1 leq i leq n$$$ array $$$D_i$$$ will be constructed from $$$D_{i-1}$$$ in the following way: Let's find the lexicographically smallest subarray of $$$D_{i-1}$$$ of length $$$i$$$. Then, the first $$$n-i$$$ elements of $$$D_i$$$ will be equals to the corresponding $$$n-i$$$ elements of array $$$D_{i-1}$$$ and the last $$$i$$$ elements of $$$D_i$$$ will be equals to the corresponding elements of the found subarray of length $$$i$$$. Array $$$x$$$ is subarray of array $$$y$$$, if $$$x$$$ can be obtained by deletion of several (possibly, zero or all) elements from the beginning of $$$y$$$ and several (possibly, zero or all) elements from the end of $$$y$$$. For array $$$C$$$ let's denote array $$$D_n$$$ as $$$op(C)$$$. Alice has an array of integers $$$A = [a_1, a_2, ldots, a_n]$$$ of length $$$n$$$. She will build the sequence of arrays $$$B_0, B_1, ldots, B_n$$$ of length $$$n+1$$$ in the following way: The first element of this sequence will be equals $$$A$$$: $$$B_0 = A$$$. For each $$$1 leq i leq n$$$ array $$$B_i$$$ will be equals $$$op(B_{i-1})$$$, where $$$op$$$ is the transformation described above. She will ask you $$$q$$$ queries about elements of sequence of arrays $$$B_0, B_1, ldots, B_n$$$. Each query consists of two integers $$$i$$$ and $$$j$$$, and the answer to this query is the value of the $$$j$$$-th element of array $$$B_i$$$. Input The first line contains the single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0β the length of array $$$A$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$)xa0β the array $$$A$$$. The third line contains the single integer $$$q$$$ ($$$1 leq q leq 10^6$$$)xa0β the number of queries. Each of the next $$$q$$$ lines contains two integers $$$i$$$, $$$j$$$ ($$$1 leq i, j leq n$$$)xa0β parameters of queries. Note In the first test case $$$B_0 = A = [2, 1, 3, 1]$$$. $$$B_1$$$ is constructed in the following way: Initially, $$$D_0 = [2, 1, 3, 1]$$$. For $$$i=1$$$ the lexicographically smallest subarray of $$$D_0$$$ of length $$$1$$$ is $$$[1]$$$, so $$$D_1$$$ will be $$$[2, 1, 3, 1]$$$. For $$$i=2$$$ the lexicographically smallest subarray of $$$D_1$$$ of length $$$2$$$ is $$$[1, 3]$$$, so $$$D_2$$$ will be $$$[2, 1, 1, 3]$$$. For $$$i=3$$$ the lexicographically smallest subarray of $$$D_2$$$ of length $$$3$$$ is $$$[1, 1, 3]$$$, so $$$D_3$$$ will be $$$[2, 1, 1, 3]$$$. For $$$i=4$$$ the lexicographically smallest subarray of $$$D_3$$$ of length $$$4$$$ is $$$[2, 1, 1, 3]$$$, so $$$D_4$$$ will be $$$[2, 1, 1, 3]$$$. So, $$$B_1 = op(B_0) = op([2, 1, 3, 1]) = [2, 1, 1, 3]$$$. | 3,500 | false | false | false | false | true | false | false | false | false | false | 2,512 |
901C | You are given an undirected graph with _n_ vertices. There are no edge-simple cycles with the even length in it. In other words, there are no cycles of even length that pass each edge at most once. Let's enumerate vertices from 1 to _n_. You have to answer _q_ queries. Each query is described by a segment of vertices [_l_;u2009_r_], and you have to count the number of its subsegments [_x_;u2009_y_] (_l_u2009β€u2009_x_u2009β€u2009_y_u2009β€u2009_r_), such that if we delete all vertices except the segment of vertices [_x_;u2009_y_] (including _x_ and _y_) and edges between them, the resulting graph is bipartite. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u20093Β·105, 1u2009β€u2009_m_u2009β€u20093Β·105)xa0β the number of vertices and the number of edges in the graph. The next _m_ lines describe edges in the graph. The _i_-th of these lines contains two integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_; _a__i_u2009β u2009_b__i_), denoting an edge between vertices _a__i_ and _b__i_. It is guaranteed that this graph does not contain edge-simple cycles of even length. The next line contains a single integer _q_ (1u2009β€u2009_q_u2009β€u20093Β·105)xa0β the number of queries. The next _q_ lines contain queries. The _i_-th of these lines contains two integers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_)xa0β the query parameters. Output Print _q_ numbers, each in new line: the _i_-th of them should be the number of subsegments [_x_;u2009_y_] (_l__i_u2009β€u2009_x_u2009β€u2009_y_u2009β€u2009_r__i_), such that the graph that only includes vertices from segment [_x_;u2009_y_] and edges between them is bipartite. Examples Input 6 6 1 2 2 3 3 1 4 5 5 6 6 4 3 1 3 4 6 1 6 Input 8 9 1 2 2 3 3 1 4 5 5 6 6 7 7 8 8 4 7 2 3 1 8 1 4 3 8 Note The first example is shown on the picture below: For the first query, all subsegments of [1;u20093], except this segment itself, are suitable. For the first query, all subsegments of [4;u20096], except this segment itself, are suitable. For the third query, all subsegments of [1;u20096] are suitable, except [1;u20093], [1;u20094], [1;u20095], [1;u20096], [2;u20096], [3;u20096], [4;u20096]. The second example is shown on the picture below: | 2,300 | false | false | false | false | true | false | false | true | false | true | 6,139 |
1804H | Lara has a safe that is locked with a circle-shaped code lock that consists of a rotating arrow, a static circumference around the arrow, an input screen, and an input button. The circumference of the lock is split into $$$k$$$ equal sections numbered from $$$1$$$ to $$$k$$$ in clockwise order. Arrow always points to one of the sections. Each section is marked with one of the first $$$k$$$ letters of the English alphabet. No two sections are marked with the same letter. Due to the lock limitations, the safe's password is a string of length $$$n$$$ that consists of first $$$k$$$ letters of the English alphabet only. Lara enters the password by rotating the lock's arrow and pressing the input button. Initially, the lock's arrow points to section $$$1$$$ and the input screen is empty. In one second she can do one of the following actions. Rotate the arrow one section clockwise. If the arrow was pointing at section $$$x < k$$$ it will now point at section $$$x + 1$$$. If the arrow was pointing at section $$$k$$$ it will now point at section $$$1$$$. Rotate the arrow one section counter-clockwise. If the arrow was pointing at section $$$x > 1$$$ it will now point at section $$$x - 1$$$. If the arrow was pointing at section $$$1$$$ it will now point at section $$$k$$$. Press the input button. The letter marked on the section that the arrow points to will be added to the content of the input screen. As soon as the content of the input screen matches the password, the safe will open. Lara always enters her password in the minimum possible time. Lara has recently found out that the safe can be re-programmed. She can take the first $$$k$$$ letters of the English alphabet and assign them to the sectors in any order she likes. Now she wants to re-arrange the letters in a way that will minimize the number of seconds it takes her to input the password. Compute this minimum number of seconds and the number of ways to assign letters, for which this minimum number of seconds is achieved. Two ways to assign letters to sectors are considered to be distinct if there exists at least one sector $$$i$$$ that is assigned different letters. Input The first line of the input contains two integers $$$k$$$ and $$$n$$$ ($$$2 leq k leq 16$$$, $$$2 leq n leq 100,000$$$)xa0β the number of sectors on the lock's circumference and the length of Lara's password, respectively. The second line of the input contains a string of length $$$n$$$ that consists of the first $$$k$$$ lowercase letters of the English alphabet only. This string is the password. Output On the first line print minimum possible number of seconds it can take Lara to enter the password and open the safe if she assigns letters to sectors optimally. On the second line print the number of ways to assign letters optimally. Note The initial states of optimal arrangements for the first example are shown in the figure below. The initial states of optimal arrangements for the second example are shown in the figure below. The initial states of optimal arrangements for the third example are shown in the figure below. | 3,300 | false | false | false | true | false | false | false | false | false | false | 1,450 |
1884A | A positive integer is called $$$k$$$-beautiful, if the digit sum of the decimal representation of this number is divisible by $$$k^{dagger}$$$. For example, $$$9272$$$ is $$$5$$$-beautiful, since the digit sum of $$$9272$$$ is $$$9 + 2 + 7 + 2 = 20$$$. You are given two integers $$$x$$$ and $$$k$$$. Please find the smallest integer $$$y ge x$$$ which is $$$k$$$-beautiful. $$$^{dagger}$$$ An integer $$$n$$$ is divisible by $$$k$$$ if there exists an integer $$$m$$$ such that $$$n = k cdot m$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The only line of each test case contains two integers $$$x$$$ and $$$k$$$ ($$$1 le x le 10^9$$$, $$$1 le k le 10$$$). Output For each test case, output the smallest integer $$$y ge x$$$ which is $$$k$$$-beautiful. Example Input 6 1 5 10 8 37 9 777 3 1235 10 1 10 Output 5 17 45 777 1243 19 Note In the first test case, numbers from $$$1$$$ to $$$4$$$ consist of a single digit, thus the digit sum is equal to the number itself. None of the integers from $$$1$$$ to $$$4$$$ are divisible by $$$5$$$. In the fourth test case, the digit sum of $$$777$$$ is $$$7 + 7 + 7 = 21$$$ which is already divisible by $$$3$$$. | 800 | true | true | false | false | false | false | true | false | false | false | 983 |
217E | Professor Bajtocy is conducting experiments on alien DNA. He has discovered that it is subject to repetitive mutations β each mutation happens in the same way: some continuous subsequence of the alien DNA becomes active, copies itself, the copy gets mangled and inserts itself right after the original subsequence. The mangled copy of the activated continuous subsequence is formed by first joining all the elements at the even positions in that subsequence, and then joining all the elements at the odd ones at the end. That is, if the activated subsequence consists of 11 elements and represented as _s_1_s_2... _s_11, its mangled copy is _s_2_s_4_s_6_s_8_s_10_s_1_s_3_s_5_s_7_s_9_s_11. For example, if the original sequence was "ACTGG" and the mutation happened on the segment [2,u20094] (that is the activated subsequence is "CTG"), the mutated DNA is: "ACTGTCGG". The mangled copy of the activated subsequence is marked with bold font. Professor Bajtocy has written down the original DNA sequence and the mutations that sequentially happened to it, and he now asks you to recover the first _k_ elements of the DNA sequence after all the mutations. Input The first line of input contains the original DNA sequence, consisting only of letters "A", "C", "T" and "G" and not exceeding 3Β·106 in length. The second line contains a single integer _k_ (1u2009β€u2009_k_u2009β€u20093Β·106). The third line contains a single integer _n_ (0u2009β€u2009_n_u2009β€u20095000) β the number of mutations. The next _n_ lines describe the mutations in chronological order β each mutation is described by two numbers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009109), meaning that the continuous subsequence [_l__i_,u2009_r__i_] has become active and cloned itself, joining itself with the mangled copy. It is guaranteed that the input data is correct, that is, no mutation acts on non-existing elements of the DNA sequence, and the resulting DNA sequence has at least _k_ elements. Assume that the DNA elements are indexed starting from 1 and that the notation [_l_,u2009_r_] meaning the continuous subsequence of DNA sequence that consists of _r_u2009-u2009_l_u2009+u20091 elements starting at the _l_-th DNA sequence element and ending at the _r_-th DNA sequence element. Output Output a single line, containing the first _k_ letters of the mutated DNA sequence. Note In the second example, after the first mutation the sequence is "ACCAGTACGT". After the second mutation it's "ACCAGTACCGACATCGT". | 2,800 | false | false | false | false | true | false | false | false | false | false | 8,967 |
982C | Problem - 982C - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags dfs and similar dp graphs greedy trees *1500 No tag edit access β Contest materials ") ") denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 le u, v le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. Output Output a single integer $$$k$$$ β the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. Examples Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 Note In the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | 1,500 | false | true | false | true | false | false | false | false | false | true | 5,790 |
981G | In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons. Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer $$$2$$$ to the multiset $$${1, 2, 3, 3}$$$, you will get $$${1, 1, 2, 2, 3, 3, 3, 3}$$$. If you try to add an integer that is not presented in the multiset, it is simply added to it. For example, if you try to add the integer $$$4$$$ to the multiset $$${1, 2, 3, 3}$$$, you will get $$${1, 2, 3, 3, 4}$$$. Also consider an array of $$$n$$$ initially empty magic multisets, enumerated from $$$1$$$ to $$$n$$$. You are to answer $$$q$$$ queries of the form "add an integer $$$x$$$ to all multisets with indices $$$l, l + 1, ldots, r$$$" and "compute the sum of sizes of multisets with indices $$$l, l + 1, ldots, r$$$". The answers for the second type queries can be large, so print the answers modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n, q leq 2 cdot 10^{5}$$$)xa0β the number of magic multisets in the array and the number of queries, respectively. The next $$$q$$$ lines describe queries, one per line. Each line starts with an integer $$$t$$$ ($$$1 leq t leq 2$$$)xa0β the type of the query. If $$$t$$$ equals $$$1$$$, it is followed by three integers $$$l$$$, $$$r$$$, $$$x$$$ ($$$1 leq l leq r leq n$$$, $$$1 leq x leq n$$$) meaning that you should add $$$x$$$ to all multisets with indices from $$$l$$$ to $$$r$$$ inclusive. If $$$t$$$ equals $$$2$$$, it is followed by two integers $$$l$$$, $$$r$$$ ($$$1 leq l leq r leq n$$$) meaning that you should compute the sum of sizes of all multisets with indices from $$$l$$$ to $$$r$$$ inclusive. Output For each query of the second type print the sum of sizes of multisets on the given segment. The answers can be large, so print them modulo $$$998244353$$$. Examples Input 4 4 1 1 2 1 1 1 2 2 1 1 4 1 2 1 4 Input 3 7 1 1 1 3 1 1 1 3 1 1 1 2 1 1 1 1 2 1 1 1 1 1 2 2 1 1 Note In the first example after the first two queries the multisets are equal to $$$[{1, 2},{1, 2},{},{}]$$$, after the third query they are equal to $$$[{1, 1, 2, 2},{1, 1, 2, 2},{1},{1}]$$$. In the second example the first multiset evolves as follows: $$${} o {3} o {3, 3} o {2, 3, 3} o {1, 2, 3, 3} o {1, 1, 2, 2, 3, 3, 3, 3}$$$. | 2,500 | false | false | false | false | true | false | false | false | false | false | 5,794 |
1362B | 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 . 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 In the first line of input, there is a single integer $$$t$$$ ($$$1 leq t leq 1024$$$), the number of test cases. In the next lines, $$$t$$$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $$$n$$$ ($$$1 leq n leq 1024$$$) denoting the number of elements in set $$$S$$$. Second line consists of $$$n$$$ distinct integers $$$s_i$$$ ($$$0 leq s_i < 1024$$$), elements of $$$S$$$. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$1024$$$. | 1,200 | false | false | false | false | false | false | true | false | false | false | 3,870 |
58C | On Bertown's main street _n_ trees are growing, the tree number _i_ has the height of _a__i_ meters (1u2009β€u2009_i_u2009β€u2009_n_). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the _n_-th one) should be equal to each other, the heights of the 2-nd and the (_n_u2009-u20091)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful. Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105) which is the number of trees. The second line contains integers _a__i_ (1u2009β€u2009_a__i_u2009β€u2009105) which are the heights of the trees. Output Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful. | 1,800 | false | false | false | false | false | false | true | false | false | false | 9,683 |
818C | Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss? Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same! The storehouse is represented as matrix _n_u2009Γu2009_m_. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells. Sofa _A_ is standing to the left of sofa _B_ if there exist two such cells _a_ and _b_ that _x__a_u2009<u2009_x__b_, _a_ is covered by _A_ and _b_ is covered by _B_. Sofa _A_ is standing to the top of sofa _B_ if there exist two such cells _a_ and _b_ that _y__a_u2009<u2009_y__b_, _a_ is covered by _A_ and _b_ is covered by _B_. Right and bottom conditions are declared the same way. Note that in all conditions _A_u2009β u2009_B_. Also some sofa _A_ can be both to the top of another sofa _B_ and to the bottom of it. The same is for left and right conditions. The note also stated that there are _cnt__l_ sofas to the left of Grandpa Maks's sofa, _cnt__r_ β to the right, _cnt__t_ β to the top and _cnt__b_ β to the bottom. Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions. Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1. Input The first line contains one integer number _d_ (1u2009β€u2009_d_u2009β€u2009105) β the number of sofas in the storehouse. The second line contains two integer numbers _n_, _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009105) β the size of the storehouse. Next _d_ lines contains four integer numbers _x_1, _y_1, _x_2, _y_2 (1u2009β€u2009_x_1,u2009_x_2u2009β€u2009_n_, 1u2009β€u2009_y_1,u2009_y_2u2009β€u2009_m_) β coordinates of the _i_-th sofa. It is guaranteed that cells (_x_1,u2009_y_1) and (_x_2,u2009_y_2) have common side, (_x_1,u2009_y_1) u2009β u2009 (_x_2,u2009_y_2) and no cell is covered by more than one sofa. The last line contains four integer numbers _cnt__l_, _cnt__r_, _cnt__t_, _cnt__b_ (0u2009β€u2009_cnt__l_,u2009_cnt__r_,u2009_cnt__t_,u2009_cnt__b_u2009β€u2009_d_u2009-u20091). Output Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through _d_ as given in input. If there is no such sofa then print -1. Examples Input 2 3 2 3 1 3 2 1 2 2 2 1 0 0 1 Input 3 10 10 1 2 1 1 5 5 6 5 6 4 5 4 2 1 2 0 Input 2 2 2 2 1 1 1 1 2 2 2 1 0 0 0 Note Let's consider the second example. The first sofa has 0 to its left, 2 sofas to its right ((1,u20091) is to the left of both (5,u20095) and (5,u20094)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). The second sofa has _cnt__l_u2009=u20092, _cnt__r_u2009=u20091, _cnt__t_u2009=u20092 and _cnt__b_u2009=u20090. The third sofa has _cnt__l_u2009=u20092, _cnt__r_u2009=u20091, _cnt__t_u2009=u20091 and _cnt__b_u2009=u20091. So the second one corresponds to the given conditions. In the third example The first sofa has _cnt__l_u2009=u20091, _cnt__r_u2009=u20091, _cnt__t_u2009=u20090 and _cnt__b_u2009=u20091. The second sofa has _cnt__l_u2009=u20091, _cnt__r_u2009=u20091, _cnt__t_u2009=u20091 and _cnt__b_u2009=u20090. And there is no sofa with the set (1,u20090,u20090,u20090) so the answer is -1. | 2,000 | false | false | true | false | false | false | true | false | false | false | 6,485 |
1873G | You are given a string $$$s$$$ made up of characters $$$ exttt{A}$$$ and $$$ exttt{B}$$$. Initially you have no coins. You can perform two types of operations: Pick a substring$$$^dagger$$$ $$$ exttt{AB}$$$, change it to $$$ exttt{BC}$$$, and get a coin. Pick a substring$$$^dagger$$$ $$$ exttt{BA}$$$, change it to $$$ exttt{CB}$$$, and get a coin. What is the most number of coins you can obtain? $$$^dagger$$$ A substring of length $$$2$$$ is a sequence of two adjacent characters of a string. Input The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β the number of test cases. The only line of each test case contains the string $$$s$$$ ($$$1 leq s leq 2 cdot 10^5$$$). All characters of $$$s$$$ are either $$$ exttt{A}$$$ or $$$ exttt{B}$$$. The sum of the lengths of $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β the maximum number of coins you can obtain. Example Input 8 ABBA ABA BAABA ABB AAAAAAB BABA B AAA Note In the first test case you can perform the following operations to get $$$2$$$ coins: $$$$$$color{red}{ exttt{AB}} exttt{BA} o exttt{BC}color{red}{ exttt{BA}} o exttt{BCCB}$$$$$$ In the second test case you can perform the following operation to get $$$1$$$ coin: $$$$$$color{red}{ exttt{AB}} exttt{A} o exttt{BCA}$$$$$$ In the third test case you can perform the following operations to get $$$3$$$ coins: $$$$$$color{red}{ exttt{BA}} exttt{ABA} o exttt{CBA}color{red}{ exttt{BA}} o exttt{C}color{red}{ exttt{BA}} exttt{CB} o exttt{CCBCB}$$$$$$ | 1,500 | false | true | false | false | false | true | false | false | false | false | 1,038 |
69B | In Chelyabinsk lives a much respected businessman Nikita with a strange nickname "Boss". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor. To begin with friends learned the rules: in the race there are _n_ sections of equal length and _m_ participants. The participants numbered from 1 to _m_. About each participant the following is known: _l__i_ β the number of the starting section, _r__i_ β the number of the finishing section (_l__i_u2009β€u2009_r__i_), _t__i_ β the time a biathlete needs to complete an section of the path, _c__i_ β the profit in roubles. If the _i_-th sportsman wins on one of the sections, the profit will be given to the man who had placed a bet on that sportsman. The _i_-th biathlete passes the sections from _l__i_ to _r__i_ inclusive. The competitor runs the whole way in (_r__i_u2009-u2009_l__i_u2009+u20091)Β·_t__i_ time units. It takes him exactly _t__i_ time units to pass each section. In case of the athlete's victory on _k_ sections the man who has betted on him receives _k_Β·_c__i_ roubles. In each section the winner is determined independently as follows: if there is at least one biathlete running this in this section, then among all of them the winner is the one who has ran this section in minimum time (spent minimum time passing this section). In case of equality of times the athlete with the smaller index number wins. If there are no participants in this section, then the winner in this section in not determined. We have to say that in the summer biathlon all the participants are moving at a constant speed. We should also add that Nikita can bet on each section and on any contestant running in this section. Help the friends find the maximum possible profit. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100). Then follow _m_ lines, each containing 4 integers _l__i_, _r__i_, _t__i_, _c__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_, 1u2009β€u2009_t__i_,u2009_c__i_u2009β€u20091000). Output Print a single integer, the maximal profit in roubles that the friends can get. In each of _n_ sections it is not allowed to place bets on more than one sportsman. Examples Input 4 4 1 4 20 5 1 3 21 10 3 3 4 30 3 4 4 20 Input 8 4 1 5 24 10 2 4 6 15 4 6 30 50 6 7 4 20 Note In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles. In the second test the optimal bet is: on 1 and 5 sections on biathlete 1, in the 2-4 sections on biathlete 2, in the 6-7 sections on athlete 4. There is no winner in the 8 section. Total: profit of 10 rubles for 1 section, the profit of 15 rubles for 2,3,4 section, profit of 10 rubles for a 5 section, profit of 20 rubles for 6, 7 section. Total profit 105 rubles. | 1,200 | false | true | true | false | false | false | false | false | false | false | 9,625 |
1793F | Kostya and Zhenya β the creators of the band "Paper" β after the release of the legendary album decided to create a new band "Day movers", for this they need to find two new people. They invited $$$n$$$ people to the casting. The casting will last $$$q$$$ days. On the $$$i$$$th of the days, Kostya and Zhenya want to find two people on the segment from $$$l_i$$$ to $$$r_i$$$ who are most suitable for their band. Since "Day movers" are doing a modern art, musical skills are not important to them and they look only at other signs: they want the height difference between two people to be as small as possible. Help them, and for each day, find the minimum difference in the growth of people from the casting on this segment! Input In the first line you are given two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 3 cdot 10^5, 1 leq q leq 10^6$$$)xa0β the number of people who came to the casting and the number of casting days. In the second line, you are given $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$ ($$$1 leq a_i leq n$$$) β the growth of each of the candidates. It is also guaranteed that all $$$a_i$$$ are different. The following $$$q$$$ lines each contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i < r_i leq n$$$)xa0β a segment of people on the $$$i$$$th day of casting. Output Output $$$q$$$ lines. In the $$$i$$$-th line there should be a minimum height difference between the two candidates on the segment on the $$$i$$$-th day of casting. Examples Input 5 3 4 1 5 3 2 1 2 3 4 2 4 Input 7 4 2 6 1 7 3 5 4 4 6 1 2 3 6 1 3 Note In the first example, the minimum difference on the segment $$$[1, 2]$$$ is $$$2$$$, on the segment $$$[2, 3]$$$ β $$$1$$$, on the segment $$$[1, 3]$$$ is also $$$1$$$. In the third example, the numbers with the minimum difference on the segment $$$[4, 6]$$$ are $$$3$$$ and $$$5$$$ ($$$5 - 3 = 2$$$). On the segment $$$[1, 2]$$$, the numbers with the minimum difference are $$$2$$$ and $$$6$$$ ($$$6 - 2 = 4$$$). On the segment $$$[3, 6]$$$, the numbers with the minimum difference are $$$1$$$ and $$$3$$$ ($$$3 - 1 = 2$$$). On the segment $$$[1, 3]$$$, the minimum difference is formed by the numbers $$$1$$$ and $$$2$$$ ($$$2 - 1 = 1$$$). | 2,600 | false | false | true | false | true | false | true | false | false | false | 1,515 |
2023F | In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck. The road is divided into $$$n$$$ sections, numbered from $$$1$$$ to $$$n$$$ from left to right. The height of the surface in the $$$i$$$-th section is equal to $$$a_i$$$. If the height of the $$$i$$$-th section is greater than $$$0$$$, then the dump truck must take sand from the $$$i$$$-th section of the road, and if the height of the $$$i$$$-th section is less than $$$0$$$, the dump truck must fill the pit in the $$$i$$$-th section of the road with sand. It is guaranteed that the initial heights are not equal to $$$0$$$. When the dump truck is in the $$$i$$$-th section of the road, it can either take away $$$x$$$ units of sand, in which case the height of the surface in the $$$i$$$-th section will decrease by $$$x$$$, or it can fill in $$$x$$$ units of sand (provided that it currently has at least $$$x$$$ units of sand in its bed), in which case the height of the surface in the $$$i$$$-th section of the road will increase by $$$x$$$. The dump truck can start its journey from any section of the road. Moving to an adjacent section on the left or right takes $$$1$$$ minute, and the time for loading and unloading sand can be neglected. The dump truck has an infinite capacity and is initially empty. You need to find the minimum time required for the dump truck to level the sand so that the height in each section becomes equal to $$$0$$$. Note that after all movements, the dump truck may still have sand left in its bed. You need to solve this problem independently for the segments numbered from $$$l_i$$$ to $$$r_i$$$. Sand outside the segment cannot be used. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 3 cdot 10^5$$$) β the number of sections and the number of queries. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$, $$$a_i eq 0$$$) β the initial height in each section. The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) β the boundaries of the segment of sections for which the minimum time needs to be determined. It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$q$$$ over all test cases do not exceed $$$3 cdot 10^5$$$. Output For each query, output the minimum time required to level the sand in the segment $$$[l_i, r_i]$$$, or $$$-1$$$ if it is impossible. Example Input 5 1 1 -179 1 1 5 3 -2 2 -1 3 -1 2 4 1 5 1 3 7 1 1 1 1 -4 1 1 1 1 7 7 2 2 -2 2 -2 1 2 -1 1 7 2 7 4 4 1000000000 1000000000 999999999 -1000000000 2 4 3 4 2 3 1 3 Output -1 2 5 -1 8 6 6 2 -1 1 2 Note In the first test case, $$$179$$$ units of sand need to be added to the only section. However, there is nowhere to take it from, so this is impossible. In the second test case: In the first query, the dump truck can start its journey at the second section. It can take $$$2$$$ units of sand, after which the height in the second section will become $$$0$$$. Then the dump truck can move to the third section. It can pour $$$1$$$ unit of sand there, after which the height in the third section will become $$$0$$$. Then the dump truck can move to the fourth section. There it can take $$$3$$$ units of sand, after which the height in the fourth section will become $$$0$$$. In total, the dump truck will spend $$$2$$$ minutes on movements. In the second query, the dump truck can start its journey at the fourth section. It can take $$$3$$$ units of sand, after which the height in the fourth section will become $$$0$$$. Then the dump truck can move to the fifth section. It can pour $$$1$$$ unit of sand there, after which the height in the fifth section will become $$$0$$$. Then the dump truck can move back to the fourth section and then to the third. It can pour $$$1$$$ unit of sand there, after which the height in the third section will become $$$0$$$. Then the dump truck can move to the second section. It can take $$$2$$$ units of sand. Then it can move to the first section. It can pour $$$2$$$ units of sand there, after which the height in the first section will become $$$0$$$. In total, the dump truck will spend $$$5$$$ minutes on movements. In the third query, the dump truck will not be able to make the height in each section equal to $$$0$$$. | 3,500 | true | true | false | false | true | false | false | false | false | false | 126 |
1968B | You are given two binary strings $$$a$$$ and $$$b$$$. A binary string is a string consisting of the characters '0' and '1'. Your task is to determine the maximum possible number $$$k$$$ such that a prefix of string $$$a$$$ of length $$$k$$$ is a subsequence of string $$$b$$$. A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) elements. Input The first line consists of a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1le n,m le 2 cdot 10^5$$$)xa0β the length of string $$$a$$$ and the length of string $$$b$$$, respectively. The second line of each test case contains a binary string $$$a$$$ of length $$$n$$$. The third line of each test case contains a binary string $$$b$$$ of length $$$m$$$. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Similarly, the sum of values $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single number β the maximum $$$k$$$, such that the first $$$k$$$ characters of $$$a$$$ form a subsequence of $$$b$$$. Example Input 6 5 4 10011 1110 3 3 100 110 1 3 1 111 4 4 1011 1111 3 5 100 11010 3 1 100 0 Note In the first example, the string '$$$10$$$' is a subsequence of '$$$1color{red}11color{red}0$$$' but the string '$$$100$$$' is not. So the answer is $$$2$$$. In the fifth example, $$$a$$$='$$$100$$$', $$$b$$$='$$$1color{red}{10}1color{red}0$$$', whole string $$$a$$$ is a subsequence of string $$$b$$$. So the answer is $$$3$$$. In the sixth example, string $$$b$$$ does not contain '$$$1$$$' so the answer is $$$0$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 499 |
1928B | Vasya has two hobbiesxa0β adding permutations$$$^{dagger}$$$ to arrays and finding the most frequently occurring element. Recently, he found an array $$$a$$$ and decided to find out the maximum number of elements equal to the same number in the array $$$a$$$ that he can obtain after adding some permutation to the array $$$a$$$. More formally, Vasya must choose exactly one permutation $$$p_1, p_2, p_3, ldots, p_n$$$ of length $$$n$$$, and then change the elements of the array $$$a$$$ according to the rule $$$a_i := a_i + p_i$$$. After that, Vasya counts how many times each number occurs in the array $$$a$$$ and takes the maximum of these values. You need to determine the maximum value he can obtain. $$$^{dagger}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$)xa0β the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single numberxa0β the maximum number of elements equal to the same number after the operation of adding a permutation. Example Input 7 2 1 2 4 7 1 4 1 3 103 102 104 5 1 101 1 100 1 5 1 10 100 1000 1 2 3 1 3 1000000000 999999997 999999999 Note In the first test case, it is optimal to choose $$$p = [2, 1]$$$. Then after applying the operation, the array $$$a$$$ will be $$$[3, 3]$$$, in which the number $$$3$$$ occurs twice, so the answer is $$$2$$$. In the second test case, one of the optimal options is $$$p = [2, 3, 1, 4]$$$. After applying the operation, the array $$$a$$$ will be $$$[9, 4, 5, 5]$$$. Since the number $$$5$$$ occurs twice, the answer is $$$2$$$. | 1,200 | false | true | false | false | false | false | false | true | true | false | 722 |
1349F1 | 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 leq i < j leq 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: $$$$$$left(sum_{pin s_n} f_p(k) ight) extrm{mod} 998,244,353$$$$$$ Input The first line contains one integer $$$n (1le nle 5000)$$$. Output Print $$$n$$$ integers, the $$$i$$$-th of them should be equal to $$$left(sum_{pin s_n} f_p(i) ight) extrm{mod} 998,244,353$$$. Note In the first example, $$$s={[1,1],[1,2]}$$$. In the second example, $$$s={[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,2],[1,2,3]}$$$. In the third example, $$$s={[1]}$$$. | 3,100 | true | false | false | true | false | false | false | false | false | false | 3,959 |
13C | Problem - 13C - Codeforces =============== xa0 β the length of the initial sequence. The following _N_ lines contain one integer each β elements of the sequence. These numbers do not exceed 109 by absolute value. Output Output one integer β minimum number of steps required to achieve the goal. Examples Input 5 3 2 -1 2 11 Output 4 Input 5 2 1 1 1 1 Output 1 | 2,200 | false | false | false | true | false | false | false | false | true | false | 9,928 |
1424M | While exploring the old caves, researchers found a book, or more precisely, a stash of mixed pages from a book. Luckily, all of the original pages are present and each page contains its number. Therefore, the researchers can reconstruct the book. After taking a deeper look into the contents of these pages, linguists think that this may be some kind of dictionary. What's interesting is that this ancient civilization used an alphabet which is a subset of the English alphabet, however, the order of these letters in the alphabet is not like the one in the English language. Given the contents of pages that researchers have found, your task is to reconstruct the alphabet of this ancient civilization using the provided pages from the dictionary. Input First-line contains two integers: $$$n$$$ and $$$k$$$ ($$$1 le n, k le 10^3$$$) β the number of pages that scientists have found and the number of words present at each page. Following $$$n$$$ groups contain a line with a single integer $$$p_i$$$ ($$$0 le n lt 10^3$$$) β the number of $$$i$$$-th page, as well as $$$k$$$ lines, each line containing one of the strings (up to $$$100$$$ characters) written on the page numbered $$$p_i$$$. Output Output a string representing the reconstructed alphabet of this ancient civilization. If the book found is not a dictionary, output "IMPOSSIBLE" without quotes. In case there are multiple solutions, output any of them. Example Input 3 3 2 b b bbac 0 a aca acba 1 ab c ccb | 2,200 | false | false | false | false | false | false | false | false | true | true | 3,548 |
698A | Vasya has _n_ days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this _n_ days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the _i_-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer _n_ (1u2009β€u2009_n_u2009β€u2009100) β the number of days of Vasya's vacations. The second line contains the sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u20093) separated by space, where: _a__i_ equals 0, if on the _i_-th day of vacations the gym is closed and the contest is not carried out; _a__i_ equals 1, if on the _i_-th day of vacations the gym is closed, but the contest is carried out; _a__i_ equals 2, if on the _i_-th day of vacations the gym is open and the contest is not carried out; _a__i_ equals 3, if on the _i_-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | 1,400 | false | false | false | true | false | false | false | false | false | false | 7,033 |
1497B | You are given an array $$$a_1, a_2, ldots, a_n$$$ consisting of $$$n$$$ positive integers and a positive integer $$$m$$$. You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want. Let's call an array $$$m$$$-divisible if for each two adjacent numbers in the array (two numbers on the positions $$$i$$$ and $$$i+1$$$ are called adjacent for each $$$i$$$) their sum is divisible by $$$m$$$. An array of one element is $$$m$$$-divisible. Find the smallest number of $$$m$$$-divisible arrays that $$$a_1, a_2, ldots, a_n$$$ is possible to divide into. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 1000)$$$ xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ $$$(1 le n le 10^5, 1 le m le 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 le a_i le 10^9)$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$10^5$$$. Output For each test case print the answer to the problem. Example Input 4 6 4 2 2 8 6 9 4 10 8 1 1 1 5 2 4 4 8 6 7 1 1 666 2 2 2 4 Note In the first test case we can divide the elements as follows: $$$[4, 8]$$$. It is a $$$4$$$-divisible array because $$$4+8$$$ is divisible by $$$4$$$. $$$[2, 6, 2]$$$. It is a $$$4$$$-divisible array because $$$2+6$$$ and $$$6+2$$$ are divisible by $$$4$$$. $$$[9]$$$. It is a $$$4$$$-divisible array because it consists of one element. | 1,200 | true | true | false | false | false | true | false | false | false | false | 3,187 |
1106F | Lunar New Year is approaching, and Bob received a gift from his friend recently β a recursive sequence! He loves this sequence very much and wants to play with it. Let $$$f_1, f_2, ldots, f_i, ldots$$$ be an infinite sequence of positive integers. Bob knows that for $$$i > k$$$, $$$f_i$$$ can be obtained by the following recursive equation: $$$$$$f_i = left(f_{i - 1} ^ {b_1} cdot f_{i - 2} ^ {b_2} cdot cdots cdot f_{i - k} ^ {b_k} ight) bmod p,$$$$$$ which in short is $$$$$$f_i = left(prod_{j = 1}^{k} f_{i - j}^{b_j} ight) bmod p,$$$$$$ where $$$p = 998,244,353$$$ (a widely-used prime), $$$b_1, b_2, ldots, b_k$$$ are known integer constants, and $$$x bmod y$$$ denotes the remainder of $$$x$$$ divided by $$$y$$$. Bob lost the values of $$$f_1, f_2, ldots, f_k$$$, which is extremely troublesome β these are the basis of the sequence! Luckily, Bob remembers the first $$$k - 1$$$ elements of the sequence: $$$f_1 = f_2 = ldots = f_{k - 1} = 1$$$ and the $$$n$$$-th element: $$$f_n = m$$$. Please find any possible value of $$$f_k$$$. If no solution exists, just tell Bob that it is impossible to recover his favorite sequence, regardless of Bob's sadness. Input The first line contains a positive integer $$$k$$$ ($$$1 leq k leq 100$$$), denoting the length of the sequence $$$b_1, b_2, ldots, b_k$$$. The second line contains $$$k$$$ positive integers $$$b_1, b_2, ldots, b_k$$$ ($$$1 leq b_i < p$$$). The third line contains two positive integers $$$n$$$ and $$$m$$$ ($$$k < n leq 10^9$$$, $$$1 leq m < p$$$), which implies $$$f_n = m$$$. Output Output a possible value of $$$f_k$$$, where $$$f_k$$$ is a positive integer satisfying $$$1 leq f_k < p$$$. If there are multiple answers, print any of them. If no such $$$f_k$$$ makes $$$f_n = m$$$, output $$$-1$$$ instead. It is easy to show that if there are some possible values of $$$f_k$$$, there must be at least one satisfying $$$1 leq f_k < p$$$. Examples Input 5 4 7 1 5 6 7 14187219 Input 8 2 3 5 6 1 7 9 10 23333 1 Input 3 998244352 998244352 998244352 4 2 Input 10 283 463 213 777 346 201 463 283 102 999 2333333 6263423 Note In the first sample, we have $$$f_4 = f_3^2 cdot f_2^3 cdot f_1^5$$$. Therefore, applying $$$f_3 = 4$$$, we have $$$f_4 = 16$$$. Note that there can be multiple answers. In the third sample, applying $$$f_7 = 1$$$ makes $$$f_{23333} = 1$$$. In the fourth sample, no such $$$f_1$$$ makes $$$f_{88888} = 66666$$$. Therefore, we output $$$-1$$$ instead. | 2,400 | true | false | false | false | false | false | false | false | false | false | 5,170 |
1539F | Vasya has an array of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. Vasya thinks that all numbers in his array are strange for some reason. To calculate how strange the $$$i$$$-th number is, Vasya created the following algorithm. He chooses a subsegment $$$a_l, a_{l+1}, ldots, a_r$$$, such that $$$1 le l le i le r le n$$$, sort its elements in increasing order in his head (he can arrange equal elements arbitrary). After that he finds the center of the segment. The center of a segment is the element at position $$$(l + r) / 2$$$, if the length of the segment is odd, and at position $$$(l + r + 1) / 2$$$ otherwise. Now Vasya finds the element that was at position $$$i$$$ before the sorting, and calculates the distance between its current position and the center of the subsegment (the distance between the elements with indices $$$j$$$ and $$$k$$$ is $$$j - k$$$). The strangeness of the number at position $$$i$$$ is the maximum distance among all suitable choices of $$$l$$$ and $$$r$$$. Vasya wants to calculate the strangeness of each number in his array. Help him to do it. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0β the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β Vasya's array. Output Print a single line with $$$n$$$ numbers. The $$$i$$$-th of them must be equal to the strangeness of the $$$i$$$-th element of the array. Note In the first example: 1. For the first position we choose the segment from $$$1$$$ to $$$5$$$. After sorting, it looks like $$$[1, 2, 3, 4, 5]$$$, the center is $$$3$$$. The distance from the center to $$$5$$$ is $$$2$$$. 2. For the second position we choose the segment from $$$2$$$ to $$$4$$$. 3. For the third position we choose the segment from $$$3$$$ to $$$5$$$. 4. For the fourth position we choose the segment from $$$1$$$ to $$$4$$$. After sorting, it looks like $$$[2, 3, 4, 5]$$$, the center is $$$4$$$. The distance from the center to $$$2$$$ is $$$2$$$. 5. For the fifth position we choose the segment from $$$1$$$ to $$$5$$$. | 2,600 | false | true | false | false | true | false | false | false | true | false | 2,943 |
1973A | Three friends gathered to play a few games of chess together. In every game, two of them play against each other. The winner gets $$$2$$$ points while the loser gets $$$0$$$, and in case of a draw, both players get $$$1$$$ point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all. You've been told that their scores after all the games were played were $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$. Additionally, it is guaranteed that $$$p_1 leq p_2 leq p_3$$$ holds. Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ as a result of a non-negative number of games between the three players, print $$$-1$$$ instead. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains three integers $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ ($$$0 leq p_1 leq p_2 leq p_3 leq 30$$$) β the scores of the three players, sorted non-decreasingly. Output For each testcase, print one number β the maximum possible number of draws that could've happened, or $$$-1$$$ if the scores aren't consistent with any valid set of games and results. Example Input 7 0 0 0 0 1 1 1 1 1 1 1 2 3 3 3 3 4 5 1 1 10 Note In the first example, no games were played at all, so no draws could occur either. For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is $$$1$$$. It's easy to see that there's no set of games achieving the scores in third example, so the answer for it is $$$-1$$$. | 900 | true | false | true | true | false | false | true | false | false | false | 455 |
1800E1 | This is an easy version of the problem. In this version, $$$k$$$ is always $$$3$$$. The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell $$$s$$$ from his student Harry. The spellxa0β is a $$$n$$$-length string of lowercase Latin letters. Drahyrt wants to replace spell with an unforgivable cursexa0β string $$$t$$$. Drahyrt, using ancient magic, can swap letters at a distance $$$k$$$ or $$$k+1$$$ in spell as many times as he wants. In this version of the problem, you can swap letters at a distance of $$$3$$$ or $$$4$$$. In other words, Drahyrt can change letters in positions $$$i$$$ and $$$j$$$ in spell $$$s$$$ if $$$i-j=3$$$ or $$$i-j=4$$$. For example, if $$$s = $$$ "talant" and $$$t = $$$ "atltna", Drahyrt can act as follows: swap the letters at positions $$$1$$$ and $$$4$$$ to get spell "aaltnt". swap the letters at positions $$$2$$$ and $$$6$$$ to get spell "atltna". You are given spells $$$s$$$ and $$$t$$$. Can Drahyrt change spell $$$s$$$ to $$$t$$$? Input The first line of input gives a single integer $$$T$$$ ($$$1 le T le 10^4$$$)xa0β the number of test cases in the test. Descriptions of the test cases are follow. The first line contains two integers $$$n, k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$k = 3$$$)xa0β the length spells and the number $$$k$$$ such that Drahyrt can change letters in a spell at a distance $$$k$$$ or $$$k+1$$$. The second line gives spell $$$s$$$xa0β a string of length $$$n$$$ consisting of lowercase Latin letters. The third line gives spell $$$t$$$xa0β a string of length $$$n$$$ consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Note that there is no limit on the sum of $$$k$$$ values over all test cases. Output For each test case, output on a separate line "YES" if Drahyrt can change spell $$$s$$$ to $$$t$$$ and "NO" otherwise. You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer). Example Input 7 6 3 talant atltna 7 3 abacaba aaaabbc 12 3 abracadabraa avadakedavra 5 3 accio cicao 5 3 lumos molus 4 3 uwjt twju 4 3 kvpx vxpk Output YES YES NO YES NO YES NO Note The first example is explained in the condition. In the second example we can proceed as follows: Swap the letters at positions $$$2$$$ and $$$5$$$ (distance $$$3$$$), then we get the spell "aaacbba". Swap the letters at positions $$$4$$$ and $$$7$$$ (distance $$$3$$$), then you get the spell "aaaabbc". In the third example, we can show that it is impossible to get the string $$$t$$$ from the string $$$s$$$ by swapping the letters at a distance of $$$3$$$ or $$$4$$$. In the fourth example, for example, the following sequence of transformations is appropriate: "accio" $$$ ightarrow$$$ "aocic" $$$ ightarrow$$$ "cocia" $$$ ightarrow$$$ "iocca" $$$ ightarrow$$$ "aocci" $$$ ightarrow$$$ "aicco" $$$ ightarrow$$$ "cicao" In the fifth example, you can show that it is impossible to get the string $$$s$$$ from the string $$$t$$$. In the sixth example, it is enough to swap the two outermost letters. | 1,400 | false | true | false | false | false | true | true | false | false | true | 1,470 |
505E | Mr. Kitayuta's garden is planted with _n_ bamboos. (Bamboos are tall, fast-growing tropical plants with hollow stems.) At the moment, the height of the _i_-th bamboo is _h__i_ meters, and it grows _a__i_ meters at the end of each day. Actually, Mr. Kitayuta hates these bamboos. He once attempted to cut them down, but failed because their stems are too hard. Mr. Kitayuta have not given up, however. He has crafted Magical Hammer with his intelligence to drive them into the ground. He can use Magical Hammer at most _k_ times during each day, due to his limited Magic Power. Each time he beat a bamboo with Magical Hammer, its height decreases by _p_ meters. If the height would become negative by this change, it will become 0 meters instead (it does not disappear). In other words, if a bamboo whose height is _h_ meters is beaten with Magical Hammer, its new height will be _max_(0,u2009_h_u2009-u2009_p_) meters. It is possible to beat the same bamboo more than once in a day. Mr. Kitayuta will fight the bamboos for _m_ days, starting today. His purpose is to minimize the height of the tallest bamboo after _m_ days (that is, _m_ iterations of "Mr. Kitayuta beats the bamboos and then they grow"). Find the lowest possible height of the tallest bamboo after _m_ days. Input The first line of the input contains four space-separated integers _n_, _m_, _k_ and _p_ (1u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_m_u2009β€u20095000,u20091u2009β€u2009_k_u2009β€u200910,u20091u2009β€u2009_p_u2009β€u2009109). They represent the number of the bamboos in Mr. Kitayuta's garden, the duration of Mr. Kitayuta's fight in days, the maximum number of times that Mr. Kitayuta beat the bamboos during each day, and the power of Magic Hammer, respectively. The following _n_ lines describe the properties of the bamboos. The _i_-th of them (1u2009β€u2009_i_u2009β€u2009_n_) contains two space-separated integers _h__i_ and _a__i_ (0u2009β€u2009_h__i_u2009β€u2009109,u20091u2009β€u2009_a__i_u2009β€u2009109), denoting the initial height and the growth rate of the _i_-th bamboo, respectively. | 2,900 | false | true | false | false | false | false | false | true | false | false | 7,819 |
1119D | Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day. In return, the fleas made a bigger ukulele for her: it has $$$n$$$ strings, and each string has $$$(10^{18} + 1)$$$ frets numerated from $$$0$$$ to $$$10^{18}$$$. The fleas use the array $$$s_1, s_2, ldots, s_n$$$ to describe the ukulele's tuning, that is, the pitch of the $$$j$$$-th fret on the $$$i$$$-th string is the integer $$$s_i + j$$$. Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them. Each question is in the form of: "How many different pitches are there, if we consider frets between $$$l$$$ and $$$r$$$ (inclusive) on all strings?" Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task! Formally, you are given a matrix with $$$n$$$ rows and $$$(10^{18}+1)$$$ columns, where the cell in the $$$i$$$-th row and $$$j$$$-th column ($$$0 le j le 10^{18}$$$) contains the integer $$$s_i + j$$$. You are to answer $$$q$$$ queries, in the $$$k$$$-th query you have to answer the number of distinct integers in the matrix from the $$$l_k$$$-th to the $$$r_k$$$-th columns, inclusive. Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 100,000$$$)xa0β the number of strings. The second line contains $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ ($$$0 leq s_i leq 10^{18}$$$)xa0β the tuning of the ukulele. The third line contains an integer $$$q$$$ ($$$1 leq q leq 100,000$$$)xa0β the number of questions. The $$$k$$$-th among the following $$$q$$$ lines contains two integers $$$l_k$$$οΌ$$$r_k$$$ ($$$0 leq l_k leq r_k leq 10^{18}$$$)xa0β a question from the fleas. Output Output one number for each question, separated by spacesxa0β the number of different pitches. Examples Input 6 3 1 4 1 5 9 3 7 7 0 2 8 17 Input 2 1 500000000000000000 2 1000000000000000000 1000000000000000000 0 1000000000000000000 Output 2 1500000000000000000 Note For the first example, the pitches on the $$$6$$$ strings are as follows. $$$$$$ begin{matrix} extbf{Fret} & extbf{0} & extbf{1} & extbf{2} & extbf{3} & extbf{4} & extbf{5} & extbf{6} & extbf{7} & ldots s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & dots s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & dots s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & dots s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & dots s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & dots s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & dots end{matrix} $$$$$$ There are $$$5$$$ different pitches on fret $$$7$$$xa0β $$$8, 10, 11, 12, 16$$$. There are $$$10$$$ different pitches on frets $$$0, 1, 2$$$xa0β $$$1, 2, 3, 4, 5, 6, 7, 9, 10, 11$$$. | 1,800 | false | false | false | false | false | false | false | true | true | false | 5,091 |
1174C | Problem - 1174C - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms number theory *1300 No tag edit access β Contest materials $$$, if $$$i$$$ and $$$j$$$ are coprime, $$$a_i eq a_j$$$. The maximal value of all $$$a_i$$$ should be minimized (that is, as small as possible). A pair of integers is called . Output Print $$$n-1$$$ integers, $$$a_2$$$, $$$a_3$$$, $$$ldots$$$, $$$a_n$$$ ($$$1 leq a_i leq n$$$). If there are multiple solutions, print any of them. Examples Input 4 Output 1 2 1 Input 3 Output 2 1 Note In the first example, notice that $$$3$$$ and $$$4$$$ are coprime, so $$$a_3 eq a_4$$$. Also, notice that $$$a= | 1,300 | false | false | false | false | false | true | false | false | false | false | 4,849 |
496B | Problem - 496B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force constructive algorithms implementation *1500 No tag edit access β Contest materials ") , and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068. You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u20091000)xa0β the number of digits on the display. The second line contains _n_ digitsxa0β the initial state of the display. Output Print a single line containing _n_ digitsxa0β the desired state of the display containing the smallest possible number. Examples Input 3 579 Output 024 Input 4 2014 Output 0142 | 1,500 | false | false | true | false | false | true | true | false | false | false | 7,850 |
1791A | Problem - 1791A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation strings *800 No tag edit access β Contest materials ") , check if it appears in the string $$$ exttt{codeforces}$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 26$$$)xa0β the number of test cases. The only line of each test case contains a character $$$c$$$xa0β a single lowercase Latin character (letter). Output For each test case, output "YES" (without quotes) if $$$c$$$ satisfies the condition, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 10 a b c d e f g h i j Output NO NO YES YES YES YES NO NO NO NO | 800 | false | false | true | false | false | false | false | false | false | false | 1,535 |
952G | In this problem you will write a simple code generator for a 2D programming language derived from , and each 'X' character is converted a Brainfuck instruction to be executed. The instruction is defined by the left, top and right neighbors of the 'X' character using the following conversion table: You are given a string. Output a program in the described language which prints this string. You can download the language interpreter used for judging here: [ (use C++11 to compile the code). Note several implementation details: The first step of the language interpretation is conversion to a Brainfuck program, which is then executed. The code must be rectangular, with all lines of the same length. It can have at most 10,000 lines and 10,000 columns, and can have at most 500,000 'X' characters. The code has toroidal topology, i.e. the 'X' on the first line will have top neighbor in the last line. Brainfuck interpreter has 30000 memory cells which store integers from 0 to 255 with increment/decrement done modulo 256. Console input (, command) is allowed in Brainfuck code but has no effect when executed. Input The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive. Output Output a program in the described language which, when executed, will print the given message. Example Output .......X....... ......XXX...... .....XXXXX..... ....XXXXXXX.... ...XXXXXXXXX... ..XXXXXXXXXXX.. .XXXXXXXXXXXXX. ............... X.............X X.............. X.............. X.............. Note The example corresponds to the following Brainfuck program: - >+< >+++< >+++++< >+++++++< >+++++++++< >+++++++++++<< > . . . The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines print the '$' character three times. | 2,200 | false | false | false | false | false | true | false | false | false | false | 5,910 |
601B | A function is called Lipschitz continuous if there is a real constant _K_ such that the inequality _f_(_x_)u2009-u2009_f_(_y_)u2009β€u2009_K_Β·_x_u2009-u2009_y_ holds for all . We'll deal with a more... discrete version of this term. For an array , we define it's Lipschitz constant as follows: In other words, is the smallest non-negative integer such that _h_[_i_]u2009-u2009_h_[_j_]u2009β€u2009_L_Β·_i_u2009-u2009_j_ holds for all 1u2009β€u2009_i_,u2009_j_u2009β€u2009_n_. You are given an array of size _n_ and _q_ queries of the form [_l_,u2009_r_]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of . Input The first line of the input contains two space-separated integers _n_ and _q_ (2u2009β€u2009_n_u2009β€u2009100u2009000 and 1u2009β€u2009_q_u2009β€u2009100)xa0β the number of elements in array and the number of queries respectively. The second line contains _n_ space-separated integers (). The following _q_ lines describe queries. The _i_-th of those lines contains two space-separated integers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009<u2009_r__i_u2009β€u2009_n_). Output Print the answers to all queries in the order in which they are given in the input. For the _i_-th query, print one line containing a single integerxa0β the sum of Lipschitz constants of all subarrays of . Examples Input 10 4 1 5 2 9 1 3 4 2 1 7 2 4 3 8 7 10 1 9 Input 7 6 5 7 7 4 6 6 2 1 2 2 3 2 6 1 7 4 7 3 5 Note In the first query of the first sample, the Lipschitz constants of subarrays of with length at least 2 are: The answer to the query is their sum. | 2,100 | true | false | false | false | true | false | false | false | false | false | 7,439 |
1784C | This is the hard version of the problem. In this version, you need to find the answer for every prefix of the monster array. In a computer game, you are fighting against $$$n$$$ monsters. Monster number $$$i$$$ has $$$a_i$$$ health points, all $$$a_i$$$ are integers. A monster is alive while it has at least $$$1$$$ health point. You can cast spells of two types: 1. Deal $$$1$$$ damage to any single alive monster of your choice. 2. Deal $$$1$$$ damage to all alive monsters. If at least one monster dies (ends up with $$$0$$$ health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time). Dealing $$$1$$$ damage to a monster reduces its health by $$$1$$$. Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game. For every $$$k = 1, 2, ldots, n$$$, answer the following question. Suppose that only the first $$$k$$$ monsters, with numbers $$$1, 2, ldots, k$$$, are present in the game. What is the smallest number of times you need to cast spells of type 1 to kill all $$$k$$$ monsters? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. Each test case consists of two lines. The first line contains a single integer $$$n$$$xa0($$$1 le n le 2 cdot 10^5$$$)xa0β the number of monsters. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0($$$1 le a_i le n$$$)xa0β monsters' health points. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers. The $$$k$$$-th of these integers must be equal to the smallest number of times you need to cast spells of type 1 to kill all $$$k$$$ monsters, if only monsters with numbers $$$1, 2, ldots, k$$$ are present in the game. Note In the first test case, for $$$k = n$$$, the initial health points of the monsters are $$$[3, 1, 2]$$$. It is enough to cast a spell of type 2: Monsters' health points change to $$$[2, 0, 1]$$$. Since monster number $$$2$$$ dies, the spell is repeated. Monsters' health points change to $$$[1, 0, 0]$$$. Since monster number $$$3$$$ dies, the spell is repeated. Monsters' health points change to $$$[0, 0, 0]$$$. Since monster number $$$1$$$ dies, the spell is repeated. Monsters' health points change to $$$[0, 0, 0]$$$. Since it is possible to use no spells of type 1 at all, the answer is $$$0$$$. In the second test case, for $$$k = n$$$, the initial health points of the monsters are $$$[4, 1, 5, 4, 1, 1]$$$. Here is one of the optimal action sequences: Using a spell of type 1, deal $$$1$$$ damage to monster number $$$1$$$. Monsters' health points change to $$$[3, 1, 5, 4, 1, 1]$$$. Using a spell of type 1, deal $$$1$$$ damage to monster number $$$4$$$. Monsters' health points change to $$$[3, 1, 5, 3, 1, 1]$$$. Using a spell of type 1, deal $$$1$$$ damage to monster number $$$4$$$ again. Monsters' health points change to $$$[3, 1, 5, 2, 1, 1]$$$. Use a spell of type 2: Monsters' health points change to $$$[2, 0, 4, 1, 0, 0]$$$. Since monsters number $$$2$$$, $$$5$$$, and $$$6$$$ die, the spell is repeated. Monsters' health points change to $$$[1, 0, 3, 0, 0, 0]$$$. Since monster number $$$4$$$ dies, the spell is repeated. Monsters' health points change to $$$[0, 0, 2, 0, 0, 0]$$$. Since monster number $$$1$$$ dies, the spell is repeated. Monsters' health points change to $$$[0, 0, 1, 0, 0, 0]$$$. Using a spell of type 1, deal $$$1$$$ damage to monster number $$$3$$$. Monsters' health points change to $$$[0, 0, 0, 0, 0, 0]$$$. Spells of type 1 are cast $$$4$$$ times in total. It can be shown that this is the smallest possible number. | 2,200 | false | true | false | false | true | false | false | false | false | false | 1,570 |
1689C | Byteland is a beautiful land known because of its beautiful trees. Misha has found a binary tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. A binary tree is an acyclic connected bidirectional graph containing $$$n$$$ vertices and $$$n - 1$$$ edges. Each vertex has a degree at most $$$3$$$, whereas the root is the vertex with the number $$$1$$$ and it has a degree at most $$$2$$$. Unfortunately, the root got infected. The following process happens $$$n$$$ times: Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing. Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected). As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved). Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1leq tleq 5000$$$)xa0β the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $$$n$$$ ($$$2leq nleq 3cdot 10^5$$$)xa0β the number of vertices of the tree. The $$$i$$$-th of the following $$$n-1$$$ lines in the test case contains two positive integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$), meaning that there exists an edge between them in the graph. It is guaranteed that the graph is a binary tree rooted at $$$1$$$. It is also guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$3cdot 10^5$$$. Output For each test case, output the maximum number of vertices Misha can save. Example Input 4 2 1 2 4 1 2 2 3 2 4 7 1 2 1 5 2 3 2 4 5 6 5 7 15 1 2 2 3 3 4 4 5 4 6 3 7 2 8 1 9 9 10 9 11 10 12 10 13 11 14 11 15 Note In the first test case, the only possible action is to delete vertex $$$2$$$, after which we save $$$0$$$ vertices in total. In the second test case, if we delete vertex $$$2$$$, we can save vertices $$$3$$$ and $$$4$$$. | 1,600 | false | false | false | true | false | false | false | false | false | false | 2,146 |
1840D | In a small town, there is a workshop specializing in woodwork. Since the town is small, only three carvers work there. Soon, a wooden toy festival is planned in the town. The workshop employees want to prepare for it. They know that $$$n$$$ people will come to the workshop with a request to make a wooden toy. People are different and may want different toys. For simplicity, let's denote the pattern of the toy that the $$$i$$$-th person wants as $$$a_i$$$ ($$$1 le a_i le 10^9$$$). Each of the carvers can choose an integer pattern $$$x$$$ ($$$1 le x le 10^9$$$) in advance, different carvers can choose different patterns. $$$x$$$ is the integer. During the preparation for the festival, the carvers will perfectly work out the technique of making the toy of the chosen pattern, which will allow them to cut it out of wood instantly. To make a toy of pattern $$$y$$$ for a carver who has chosen pattern $$$x$$$, it will take $$$x - y$$$ time, because the more the toy resembles the one he can make instantly, the faster the carver will cope with the work. On the day of the festival, when the next person comes to the workshop with a request to make a wooden toy, the carvers can choose who will take on the job. At the same time, the carvers are very skilled people and can work on orders for different people simultaneously. Since people don't like to wait, the carvers want to choose patterns for preparation in such a way that the maximum waiting time over all people is as small as possible. Output the best maximum waiting time that the carvers can achieve. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follow the descriptions of the test cases. The first line of a test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of people who will come to the workshop. The second line of a test case contains $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the patterns of toys. The sum of all $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first example, the carvers can choose patterns $$$1$$$, $$$7$$$, $$$9$$$ for preparation. In the second example, the carvers can choose patterns $$$3$$$, $$$30$$$, $$$60$$$ for preparation. In the third example, the carvers can choose patterns $$$14$$$, $$$50$$$, $$$85$$$ for preparation. | 1,400 | false | true | false | false | false | false | false | true | true | false | 1,250 |
907A | A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size _a_ can climb into some car with size _b_ if and only if _a_u2009β€u2009_b_, he or she likes it if and only if he can climb into this car and 2_a_u2009β₯u2009_b_. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers _V_1, _V_2, _V_3, _V__m_(1u2009β€u2009_V__i_u2009β€u2009100)xa0β sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that _V_1u2009>u2009_V_2u2009>u2009_V_3. Output Output three integersxa0β sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. | 1,300 | false | false | true | false | false | false | true | false | false | false | 6,122 |
797D | Let _T_ be arbitrary binary tree β tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree _T_: 1. Set pointer to the root of a tree. 2. Return success if the value in the current vertex is equal to the number you are looking for 3. Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for 4. Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for 5. Return fail if you try to go to the vertex that doesn't exist Here is the pseudo-code of the described algorithm: bool find(TreeNode t, int x) { if (t == null) return false; if (t.value == x) return true; if (x < t.value) return find(t.left, x); else return find(t.right, x); } find(root, x); The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree. Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree. If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately. Input First line contains integer number _n_ (1u2009β€u2009_n_u2009β€u2009105) β number of vertices in the tree. Each of the next _n_ lines contains 3 numbers _v_, _l_, _r_ (0u2009β€u2009_v_u2009β€u2009109) β value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number u2009-u20091 is set instead. Note that different vertices of the tree may contain the same values. | 2,100 | false | false | false | false | true | false | false | false | false | false | 6,584 |
628E | A tourist wants to visit country Zeydabad for Zbazi (a local game in Zeydabad). The country Zeydabad is a rectangular table consisting of _n_ rows and _m_ columns. Each cell on the country is either 'z' or '.'. The tourist knows this country is named Zeydabad because there are lots of ''Z-pattern"s in the country. A ''Z-pattern" is a square which anti-diagonal is completely filled with 'z' and its upper and lower rows are also completely filled with 'z'. All other cells of a square can be arbitrary. Note that a ''Z-pattern" can consist of only one cell (see the examples). So he wants to count the number of ''Z-pattern"s in the country (a necessary skill for Zbazi). Now your task is to help tourist with counting number of ''Z-pattern"s. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers _n_,u2009_m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20093000) β the number of rows and columns respectively. Each of the next _n_ lines contains _m_ characters 'z' or '.' β the description of Zeydabad. Output Print the only integer _a_ β the number of ''Z-pattern"s in Zeydabad. | 2,300 | false | false | true | false | true | false | false | false | false | false | 7,311 |
1346G | 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, dots, p_k$$$ which are chosen by the camera's manufacturer. You have $$$n$$$ moments of interest $$$x_1, x_2, dots, 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, dots, p_k$$$). It's not a problem for you that cameras can take photos at other moments of timexa0β you only care about moments of interest. Input The first line contains two integers $$$k$$$ and $$$n$$$ ($$$1 le k le 10^5$$$; $$$2 le n le 10^5$$$)xa0β the number of periods to choose and the number of moments of interest. The second line contains $$$k$$$ integers $$$p_1, p_2, dots, p_k$$$ ($$$1 le p_1 < p_2 < dots < p_k le 10^6$$$)xa0β the periods to choose in the ascending order. The third line contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_1 < x_2 < dots < x_n le 10^6$$$)xa0β the moments of interest in the ascending order. Output Print YES (case insensitive) in the first line if there is a way to configure cameras. In the second line, print two integers $$$s_1$$$ and $$$cp_1$$$ ($$$1 le s_1 le 10^6$$$; $$$1 le cp_1 le 10^6$$$; $$$cp_1 in {p_1, dots, p_k}$$$)xa0β the starting moment and the period for the first camera. The period should be one of the given periods. In the third line, print two integers $$$s_2$$$ and $$$cp_2$$$ ($$$1 le s_2 le 10^6$$$; $$$1 le cp_2 le 10^6$$$; $$$cp_2 in {p_1, dots, p_k}$$$)xa0β the starting moment and the period for the second camera. The period should be one of the given periods. If there is no way to configure cameras, print NO (case insensitive). If there are multiple ways, you may print any of them. Examples Input 3 5 3 5 7 1 4 5 7 12 Input 3 4 10 20 100 2 3 4 7 | 2,300 | true | false | false | false | false | false | false | false | false | false | 3,973 |
993D | You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned. If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible. Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u200950) β the number of tasks. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009108), where _a__i_ represents the amount of power required for the _i_-th task. The third line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009β€u2009_b__i_u2009β€u2009100), where _b__i_ is the number of processors that _i_-th task will utilize. Output Print a single integer value β the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up. Examples Input 6 8 10 9 9 8 10 1 1 1 1 1 1 Input 6 8 10 9 9 8 10 1 10 5 5 1 10 Note In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9. In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round. | 2,500 | false | true | false | true | false | false | false | true | false | false | 5,731 |
460E | Roland loves growing flowers. He has recently grown a beautiful rose at point (0,u20090) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it. To protect the rose, Roland wants to build _n_ watch towers. Let's assume that a tower is a point on the plane at the distance of at most _r_ from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0,u20090). Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points. Input The first line contains two integers, _n_ and _r_ (2u2009β€u2009_n_u2009β€u20098;xa01u2009β€u2009_r_u2009β€u200930). Output In the first line print an integer β the maximum possible sum of squared distances. In the _i_-th of the following _n_ lines print two integers, _x__i_,u2009_y__i_ β the coordinates of the _i_-th tower. Each tower must be inside or on the border of the circle with radius _r_. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0,u20090). If there are multiple valid optimal arrangements, choose any of them. | 2,700 | true | false | false | false | false | false | true | false | true | false | 7,997 |
1515G | In Fire City, there are $$$n$$$ intersections and $$$m$$$ one-way roads. The $$$i$$$-th road goes from intersection $$$a_i$$$ to $$$b_i$$$ and has length $$$l_i$$$ miles. There are $$$q$$$ cars that may only drive along those roads. The $$$i$$$-th car starts at intersection $$$v_i$$$ and has an odometer that begins at $$$s_i$$$, increments for each mile driven, and resets to $$$0$$$ whenever it reaches $$$t_i$$$. Phoenix has been tasked to drive cars along some roads (possibly none) and return them to their initial intersection with the odometer showing $$$0$$$. For each car, please find if this is possible. A car may visit the same road or intersection an arbitrary number of times. The odometers don't stop counting the distance after resetting, so odometers may also be reset an arbitrary number of times. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le m le 2 cdot 10^5$$$)xa0β the number of intersections and the number of roads, respectively. Each of the next $$$m$$$ lines contain three integers $$$a_i$$$, $$$b_i$$$, and $$$l_i$$$ ($$$1 le a_i, b_i le n$$$; $$$a_i eq b_i$$$; $$$1 le l_i le 10^9$$$)xa0β the information about the $$$i$$$-th road. The graph is not necessarily connected. It is guaranteed that between any two intersections, there is at most one road for each direction. The next line contains an integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0β the number of cars. Each of the next $$$q$$$ lines contains three integers $$$v_i$$$, $$$s_i$$$, and $$$t_i$$$ ($$$1 le v_i le n$$$; $$$0 le s_i < t_i le 10^9$$$)xa0β the initial intersection of the $$$i$$$-th car, the initial number on the $$$i$$$-th odometer, and the number at which the $$$i$$$-th odometer resets, respectively. Output Print $$$q$$$ answers. If the $$$i$$$-th car's odometer may be reset to $$$0$$$ by driving through some roads (possibly none) and returning to its starting intersection $$$v_i$$$, print YES. Otherwise, print NO. Examples Input 4 4 1 2 1 2 3 1 3 1 2 1 4 3 3 1 1 3 1 2 4 4 0 1 Input 4 5 1 2 1 2 3 1 3 1 2 1 4 1 4 3 2 2 1 2 4 4 3 5 Note The illustration for the first example is below: In the first query, Phoenix can drive through the following cities: $$$1$$$ $$$ ightarrow$$$ $$$2$$$ $$$ ightarrow$$$ $$$3$$$ $$$ ightarrow$$$ $$$1$$$ $$$ ightarrow$$$ $$$2$$$ $$$ ightarrow$$$ $$$3$$$ $$$ ightarrow$$$ $$$1$$$. The odometer will have reset $$$3$$$ times, but it displays $$$0$$$ at the end. In the second query, we can show that there is no way to reset the odometer to $$$0$$$ and return to intersection $$$1$$$. In the third query, the odometer already displays $$$0$$$, so there is no need to drive through any roads. Below is the illustration for the second example: | 2,700 | true | false | false | false | false | false | false | false | false | true | 3,085 |
1559B | As their story unravels, a timeless tale is told once again... Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow. There are $$$n$$$ squares arranged in a row, and each of them can be painted either red or blue. Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square. Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color. For example, the imperfectness of "BRRRBBR" is $$$3$$$, with "BB" occurred once and "RR" occurred twice. Your goal is to minimize the imperfectness and print out the colors of the squares after painting. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. Each test case consists of two lines. The first line of each test case contains an integer $$$n$$$ ($$$1leq nleq 100$$$) β the length of the squares row. The second line of each test case contains a string $$$s$$$ with length $$$n$$$, containing characters 'B', 'R' and '?'. Here 'B' stands for a blue square, 'R' for a red square, and '?' for a blank square. Output For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them. Note In the first test case, if the squares are painted "BRRBRBR", the imperfectness is $$$1$$$ (since squares $$$2$$$ and $$$3$$$ have the same color), which is the minimum possible imperfectness. | 900 | false | true | false | true | false | false | false | false | false | false | 2,835 |
1211H | There are $$$n$$$ cities and $$$n-1$$$ two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from $$$1$$$ to $$$n$$$. Yes, of course, you recognized an undirected tree in this description. The government plans to repair all the roads. Each road will be repaired by some private company. In total, the country has $$$10^6$$$ private companies that are numbered from $$$1$$$ to $$$10^6$$$. It is possible that some companies will not repair roads at all, and some will repair many roads. To simplify the control over the work of private companies, the following restriction was introduced: for each city, we calculate the number of different companies that repair roads that have this city at one end. This number for each city should not exceed $$$2$$$. In other words, for each city, there should be no more than two different companies that repair roads related to this city. The National Anti-Corruption Committee of Treeland raises concerns that all (or almost all) of the work will go to one private company. For this reason, the committee requires that roads be distributed among companies in such a way as to minimize the value of $$$r$$$. For each company, we calculate the number of roads assigned to it, the maximum among all companies is called the number $$$r$$$. Help the government find such a way to distribute all roads among companies in the required way. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of input cases in the input. Next, the input cases themselves are given. The first line of each test case set contains an integer $$$n$$$ ($$$2 le n le 3000$$$) β the number of cities in Treeland. Next, in the $$$n-1$$$ lines the roads are written: a line contains two integers $$$x_i$$$, $$$y_i$$$ ($$$1 le x_i, y_i le n$$$), indicating that the $$$i$$$-th road connects the cities $$$x_i$$$ and $$$y_i$$$. It is guaranteed that the sum of all values $$$n$$$ u200bu200bfor all test cases in the input does not exceed $$$3000$$$. Output Print the answers for all $$$t$$$ test cases in the input. Each test case must begin with a line that contains $$$r$$$ β the minimum possible number of roads assigned to the most used company. Next, in the next line print $$$n-1$$$ the number $$$c_1, c_2, dots, c_{n-1}$$$ ($$$1 le c_i le 10^6$$$), where $$$c_i$$$ indicates the company to repair the $$$i$$$-th road. If there are several optimal assignments, print any of them. | 3,100 | false | false | false | true | false | false | false | true | false | false | 4,621 |
1995C | ikrpprpp found an array $$$a$$$ consisting of integers. He likes justice, so he wants to make $$$a$$$ fairxa0β that is, make it non-decreasing. To do that, he can perform an act of justice on an index $$$1 le i le n$$$ of the array, which will replace $$$a_i$$$ with $$$a_i ^ 2$$$ (the element at position $$$i$$$ with its square). For example, if $$$a = [2,4,3,3,5,3]$$$ and ikrpprpp chooses to perform an act of justice on $$$i = 4$$$, $$$a$$$ becomes $$$[2,4,3,9,5,3]$$$. What is the minimum number of acts of justice needed to make the array non-decreasing? Input First line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. It is followed by the description of test cases. For each test case, the first line contains an integer $$$n$$$xa0β size of the array $$$a$$$. The second line contains $$$n$$$ ($$$1 le n le 2 cdot 10 ^5$$$) integers $$$a_1, a_2,ldots, a_n$$$ ($$$1 le a_i le 10 ^ 6$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot {10}^5$$$. Output For each testcase, print an integerxa0β minimum number of acts of justice required to make the array $$$a$$$ non-decreasing. If it is impossible to do that, print $$$-1$$$. Example Input 7 3 1 2 3 2 3 2 3 3 1 5 4 1 1 2 3 3 4 3 2 9 16 2 4 2 256 2 4 2 8 11 10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000 Note In the first test case, there's no need to perform acts of justice. The array is fair on its own! In the third test case, it can be proven that the array cannot become non-decreasing. In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet another act of justice on index 3. After that, $$$a$$$ will become $$$[4, 9, 16]$$$. | 1,800 | true | true | true | false | false | true | true | false | false | false | 294 |
413C | Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have _n_ questions, _m_ of them are auction questions and _n_u2009-u2009_m_ of them are regular questions. Each question has a price. The price of the _i_-th question is _a__i_ points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price. The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again. All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve. Input The first line contains two space-separated integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100;xa0_m_u2009β€u2009_min_(_n_,u200930)) β the total number of questions and the number of auction questions, correspondingly. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009107) β the prices of the questions. The third line contains _m_ distinct integers _b__i_ (1u2009β€u2009_b__i_u2009β€u2009_n_) β the numbers of auction questions. Assume that the questions are numbered from 1 to _n_. Output In the single line, print the answer to the problem β the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type. | 1,400 | true | true | false | false | false | false | false | false | false | false | 8,172 |
919D | You are given a graph with $$$n$$$ nodes and $$$m$$$ directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is $$$3$$$. Your task is find a path whose value is the largest. Input The first line contains two positive integers $$$n, m$$$ ($$$1 leq n, m leq 300,000$$$), denoting that the graph has $$$n$$$ nodes and $$$m$$$ directed edges. The second line contains a string $$$s$$$ with only lowercase English letters. The $$$i$$$-th character is the letter assigned to the $$$i$$$-th node. Then $$$m$$$ lines follow. Each line contains two integers $$$x, y$$$ ($$$1 leq x, y leq n$$$), describing a directed edge from $$$x$$$ to $$$y$$$. Note that $$$x$$$ can be equal to $$$y$$$ and there can be multiple edges between $$$x$$$ and $$$y$$$. Also the graph can be not connected. Output Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead. Examples Input 5 4 abaca 1 2 1 3 3 4 4 5 Input 6 6 xzyabc 1 2 3 1 2 3 5 4 4 3 6 4 Input 10 14 xzyzyzyzqx 1 2 2 4 3 5 4 5 2 6 6 8 6 5 2 10 3 9 10 9 4 6 1 10 2 8 3 7 Note In the first sample, the path with largest value is $$$1 o 3 o 4 o 5$$$. The value is $$$3$$$ because the letter 'a' appears $$$3$$$ times. | 1,700 | false | false | false | true | false | false | false | false | false | true | 6,053 |
1062C | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into $$$n$$$ parts, places them on a row and numbers them from $$$1$$$ through $$$n$$$. For each part $$$i$$$, he defines the deliciousness of the part as $$$x_i in {0, 1}$$$. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the $$$i$$$-th part then his enjoyment of the Banh-mi will increase by $$$x_i$$$ and the deliciousness of all the remaining parts will also increase by $$$x_i$$$. The initial enjoyment of JATC is equal to $$$0$$$. For example, suppose the deliciousness of $$$3$$$ parts are $$$[0, 1, 0]$$$. If JATC eats the second part then his enjoyment will become $$$1$$$ and the deliciousness of remaining parts will become $$$[1, _, 1]$$$. Next, if he eats the first part then his enjoyment will become $$$2$$$ and the remaining parts will become $$$[_, _, 2]$$$. After eating the last part, JATC's enjoyment will become $$$4$$$. However, JATC doesn't want to eat all the parts but to save some for later. He gives you $$$q$$$ queries, each of them consisting of two integers $$$l_i$$$ and $$$r_i$$$. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range $$$[l_i, r_i]$$$ in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo $$$10^9+7$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 100,000$$$). The second line contains a string of $$$n$$$ characters, each character is either '0' or '1'. The $$$i$$$-th character defines the deliciousness of the $$$i$$$-th part. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0β the segment of the corresponding query. | 1,600 | true | true | true | false | false | false | false | false | false | false | 5,400 |
1580E | Because the railway system in Gensokyo is often congested, as an enthusiastic engineer, Kawasiro Nitori plans to construct more railway to ease the congestion. There are $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$ and $$$m$$$ two-way railways in Gensokyo. Every two-way railway connects two different stations and has a positive integer length $$$d$$$. No two two-way railways connect the same two stations. Besides, it is possible to travel from any station to any other using those railways. Among these $$$n$$$ stations, station $$$1$$$ is the main station. You can get to any station from any other station using only two-way railways. Because of the technological limitation, Nitori can only construct one-way railways, whose length can be arbitrary positive integer. Constructing a one-way railway from station $$$u$$$ will costs $$$w_u$$$ units of resources, no matter where the railway ends. To ease the congestion, Nitori plans that after construction there are at least two shortest paths from station $$$1$$$ to any other station, and these two shortest paths do not pass the same station except station $$$1$$$ and the terminal. Besides, Nitori also does not want to change the distance of the shortest path from station $$$1$$$ to any other station. Due to various reasons, sometimes the cost of building a new railway will increase uncontrollably. There will be a total of $$$q$$$ occurrences of this kind of incident, and the $$$i$$$-th event will add additional amount of $$$x_i$$$ to the cost of building a new railway from the station $$$k_i$$$. To save resources, before all incidents and after each incident, Nitori wants you to help her calculate the minimal cost of railway construction. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le m le 3 cdot 10^5$$$, $$$0 le q le 2cdot10^5$$$). The second line contains $$$n$$$ integers $$$w_1,w_2,ldots,w_n$$$ ($$$1 le w_i le 10^9$$$). Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$, $$$d$$$ ($$$1 le u,v le n$$$, $$$u e v$$$, $$$1 le d le 10^9$$$), denoting a two-way railway connecting station $$$u$$$ and station $$$v$$$, with length $$$d$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$k_i,x_i$$$ ($$$1 le k_i le n, 1 le x_i le 4 imes 10^8$$$). Output Print $$$q+1$$$ lines, and the $$$i$$$-th of these lines contains one integer, denoting the minimal cost of railway construction after the $$$i-1$$$-th incident (especially, the $$$0$$$-th incident means no incident occurred). Examples Input 5 5 1 1 1 1 1 1 1 2 1 2 3 1 2 4 1 3 5 1 4 5 1 1 2 Input 8 11 0 14 4 16 15 1 3 1 14 4 2 1 1 2 3 7 5 4 2 3 1 8 6 2 8 5 5 5 4 5 7 6 7 3 5 5 1 6 6 8 1 4 Input 10 16 8 29 1 75 73 51 69 24 17 1 97 1 2 18 2 3 254 2 4 546 2 5 789 5 6 998 6 7 233 7 8 433 1 9 248 5 10 488 2 6 1787 10 8 1176 3 8 2199 4 8 1907 2 10 1277 4 10 731 9 10 1047 1 11 1 9 8 8 1 3 2 19 9 5 9 4 7 6 Output 34 45 54 54 57 76 96 112 112 Note In the second example, Nitori can build railways as follows: $$$1 ightarrow 2$$$, $$$1 ightarrow 3$$$, $$$1 ightarrow 4$$$, $$$2 ightarrow 8$$$, and the cost is $$$14 + 14 + 14 + 4 = 46$$$. | 3,400 | false | false | false | false | true | true | true | false | false | true | 2,729 |
875A | Problem - 875A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force math *1200 No tag edit access β Contest materials ") . Output In the first line print one integer _k_xa0β number of different values of _x_ satisfying the condition. In next _k_ lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case _x_u2009=u200915 there is only one variant: 15u2009+u20091u2009+u20095u2009=u200921. In the second test case there are no such _x_. | 1,200 | true | false | false | false | false | false | true | false | false | false | 6,248 |
1850F | Mihai and Slavic were looking at a group of $$$n$$$ frogs, numbered from $$$1$$$ to $$$n$$$, all initially located at point $$$0$$$. Frog $$$i$$$ has a hop length of $$$a_i$$$. Each second, frog $$$i$$$ hops $$$a_i$$$ units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to catch all frogs that will ever pass through the corresponding coordinate. However, the children can't go far away from their home so they can only place a trap in the first $$$n$$$ points (that is, in a point with a coordinate between $$$1$$$ and $$$n$$$) and the children can't place a trap in point $$$0$$$ since they are scared of frogs. Can you help Slavic and Mihai find out what is the maximum number of frogs they can catch using a trap? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) β the number of frogs, which equals the distance Slavic and Mihai can travel to place a trap. The second line of each test case contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) β the lengths of the hops of the corresponding frogs. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output a single integerxa0β the maximum number of frogs Slavic and Mihai can catch using a trap. Example Input 7 5 1 2 3 4 5 3 2 2 2 6 3 1 3 4 9 10 9 1 3 2 4 2 3 7 8 5 1 10 8 7 11 6 8 12 4 4 8 10 9 11 9 12 1 7 2 5 8 10 Note In the first test case, the frogs will hop as follows: Frog 1: $$$0 o 1 o 2 o 3 o mathbf{color{red}{4}} o cdots$$$ Frog 2: $$$0 o 2 o mathbf{color{red}{4}} o 6 o 8 o cdots$$$ Frog 3: $$$0 o 3 o 6 o 9 o 12 o cdots$$$ Frog 4: $$$0 o mathbf{color{red}{4}} o 8 o 12 o 16 o cdots$$$ Frog 5: $$$0 o 5 o 10 o 15 o 20 o cdots$$$ Therefore, if Slavic and Mihai put a trap at coordinate $$$4$$$, they can catch three frogs: frogs 1, 2, and 4. It can be proven that they can't catch any more frogs. In the second test case, Slavic and Mihai can put a trap at coordinate $$$2$$$ and catch all three frogs instantly. | 1,300 | true | false | true | false | false | false | true | false | false | false | 1,177 |
1946B | You have an array $$$a$$$ of $$$n$$$ integers. You perform exactly $$$k$$$ operations on it. In one operation, you select any contiguous subarray of the array $$$a$$$ (possibly empty) and insert the sum of this subarray anywhere in the array. Your task is to find the maximum possible sum of the array after $$$k$$$ such operations. As this number can be very large, output the answer modulo $$$10^9 + 7$$$. Reminder: the remainder of a number $$$x$$$ modulo $$$p$$$ is the smallest non-negative $$$y$$$ such that there exists an integer $$$q$$$ and $$$x = p cdot q + y$$$. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 2 cdot 10^5$$$)xa0β the length of the array $$$a$$$ and the number of operations, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0β the array $$$a$$$ itself. It is guaranteed that the sum of the values of $$$n$$$ and $$$k$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be $$$(-4) + (-7) + 0 + 0 = -11$$$, modulo $$$10^9 + 7$$$ this is $$$999,999,996$$$. In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [$$$2, 2, 8$$$] $$$ ightarrow$$$ [$$$2, 2, 8, 12$$$] $$$ ightarrow$$$ [$$$2, 2, 8, 12, 24$$$] $$$ ightarrow$$$ [$$$2, 2, 8, 12, 24, 48$$$], the sum of the final array is $$$2 + 2 + 8 + 12 + 24 + 48 = 96$$$. In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers $$$4, -2$$$ and $$$8$$$) and insert its sum at the beginning of the array, thereby obtaining the array [$$$10, 4, -2, 8, -12, 9$$$], the sum of this array is $$$17$$$. In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken moduloxa0β $$$42$$$, because $$$(-6 cdot (10^9 + 7) + 42 = -6,000,000,000)$$$. | 1,100 | true | true | false | true | false | false | false | false | false | false | 604 |
1748B | A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it. For example: string "7" is diverse because 7 appears in it $$$1$$$ time and the number of distinct characters in it is $$$1$$$; string "77" is not diverse because 7 appears in it $$$2$$$ times and the number of distinct characters in it is $$$1$$$; string "1010" is diverse because both 0 and 1 appear in it $$$2$$$ times and the number of distinct characters in it is $$$2$$$; string "6668" is not diverse because 6 appears in it $$$3$$$ times and the number of distinct characters in it is $$$2$$$. You are given a string $$$s$$$ of length $$$n$$$, consisting of only digits $$$0$$$ to $$$9$$$. Find how many of its $$$frac{n(n+1)}{2}$$$ substrings are diverse. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Note that if the same diverse string appears in $$$s$$$ multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "77" both equal to "7", so the answer for the string "77" is $$$2$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the length of the string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$. It is guaranteed that all characters of $$$s$$$ are digits from $$$0$$$ to $$$9$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case, the diverse substring is "7". In the second test case, the only diverse substring is "7", which appears twice, so the answer is $$$2$$$. In the third test case, the diverse substrings are "0" ($$$2$$$ times), "01", "010", "1" ($$$2$$$ times), "10" ($$$2$$$ times), "101" and "1010". In the fourth test case, the diverse substrings are "0" ($$$3$$$ times), "01", "011", "0110", "1" ($$$2$$$ times), "10", "100", "110" and "1100". In the fifth test case, the diverse substrings are "3", "39", "399", "6", "9" ($$$4$$$ times), "96" and "996". In the sixth test case, all $$$15$$$ non-empty substrings of "23456" are diverse. | 1,400 | false | false | true | false | false | false | true | false | false | false | 1,796 |
712B | Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string _s_ with his directions for motion: An 'L' indicates he should move one unit left. An 'R' indicates he should move one unit right. A 'U' indicates he should move one unit up. A 'D' indicates he should move one unit down. But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in _s_ with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string. Input The first and only line contains the string _s_ (1u2009β€u2009_s_u2009β€u2009100u2009000)xa0β the instructions Memory is given. Output If there is a string satisfying the conditions, output a single integerxa0β the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1. Note In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk. In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change _s_ to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin. | 1,100 | false | false | true | false | false | false | false | false | false | false | 6,970 |
662C | Problem - 662C - Codeforces =============== xa0 ]( "The Final Round of CROC 2016 and Codeforces Round #347") xa0β the number of rows and the number of columns, respectively. Then _n_ lines follows with the descriptions of the rows. Each line has length _m_ and contains only digits '0' and '1'. Output Output a single integerxa0β the minimum possible number of ones you can get after applying some sequence of operations. Example Input 3 4 0110 1010 0111 Output 2 | 2,600 | true | false | false | true | false | false | true | false | false | false | 7,166 |
898C | Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers. Vasya decided to organize information about the phone numbers of friends. You will be given _n_ strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record. Vasya also believes that if the phone number _a_ is a suffix of the phone number _b_ (that is, the number _b_ ends up with _a_), and both numbers are written by Vasya as the phone numbers of the same person, then _a_ is recorded without the city code and it should not be taken into account. The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers _x_ and _y_, and _x_ is a suffix of _y_ (that is, _y_ ends in _x_), then you shouldn't print number _x_. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once. Read the examples to understand statement and format of the output better. Input First line contains the integer _n_ (1u2009β€u2009_n_u2009β€u200920)xa0β number of entries in Vasya's phone books. The following _n_ lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros. Output Print out the ordered information about the phone numbers of Vasya's friends. First output _m_xa0β number of friends that are found in Vasya's phone books. The following _m_ lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend. Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order. Examples Input 2 ivan 1 00123 masha 1 00123 Output 2 masha 1 00123 ivan 1 00123 Input 3 karl 2 612 12 petr 1 12 katya 1 612 Output 3 katya 1 612 petr 1 12 karl 1 612 Input 4 ivan 3 123 123 456 ivan 2 456 456 ivan 8 789 3 23 6 56 9 89 2 dasha 2 23 789 Output 2 dasha 2 23 789 ivan 4 789 123 2 456 | 1,400 | false | false | true | false | false | false | false | false | false | false | 6,156 |
319C | Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut _n_ trees with heights _a_1,u2009_a_2,u2009...,u2009_a__n_. They bought a chain saw from a shop. Each time they use the chain saw on the tree number _i_, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is _i_ (the tree that have height _a__i_ in the beginning), then the cost of charging the chain saw would be _b__i_. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each _i_ < _j_, _a__i_u2009<u2009_a__j_ and _b__i_u2009>u2009_b__j_ and also _b__n_u2009=u20090 and _a_1u2009=u20091. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? Input The first line of input contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009105). The second line of input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109). The third line of input contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009β€u2009_b__i_u2009β€u2009109). It's guaranteed that _a_1u2009=u20091, _b__n_u2009=u20090, _a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_ and _b_1u2009>u2009_b_2u2009>u2009...u2009>u2009_b__n_. Output The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 6 1 2 3 10 20 30 6 5 4 3 2 0 | 2,100 | false | false | false | true | false | false | false | false | false | false | 8,556 |
1034B | Problem - 1034B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force constructive algorithms flows graph matchings *2200 No tag edit access β Contest materials $$$. Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly $$$3$$$. The Manhattan distance between two cells $$$(x_i,y_i)$$$ and $$$(x_j,y_j)$$$ is defined as $$$x_i-x_j+y_i-y_j$$$. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n,m leq 10^9$$$) β the number of rows and the number of columns of the chessboard. Output Print one integer β the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than $$$3$$$, so the answer is $$$0$$$. In the second example, a possible solution is $$$(1,1)(3,2)$$$, $$$(1,2)(3,3)$$$, $$$(2,1)(1,3)$$$, $$$(3,1)(2,3)$$$. | 2,200 | false | false | false | false | false | true | true | false | false | false | 5,527 |
1945A | The organizing committee plans to take the participants of the Olympiad on a hike after the tour. Currently, the number of tents needed to be taken is being calculated. It is known that each tent can accommodate up to $$$3$$$ people. Among the participants, there are $$$a$$$ introverts, $$$b$$$ extroverts, and $$$c$$$ universals: Each introvert wants to live in a tent alone. Thus, a tent with an introvert must contain exactly one personxa0β only the introvert himself. Each extrovert wants to live in a tent with two others. Thus, the tent with an extrovert must contain exactly three people. Each universal is fine with any option (living alone, with one other person, or with two others). The organizing committee respects the wishes of each participant very much, so they want to fulfill all of them. Tell us the minimum number of tents needed to be taken so that all participants can be accommodated according to their preferences. If it is impossible to accommodate the participants in a way that fulfills all the wishes, output $$$-1$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. This is followed by the descriptions of the test cases. Each test case is described by a single line containing three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 le a, b, c le 10^9$$$)xa0β the number of introverts, extroverts, and universals, respectively. Output For each test case, output a single integerxa0β the minimum number of tents, or $$$-1$$$ if it is impossible to accommodate the participants. Example Input 10 1 2 3 1 4 1 1 4 2 1 1 1 1 3 2 19 7 18 0 0 0 7 0 0 0 24 0 1000000000 1000000000 1000000000 Output 3 -1 3 -1 3 28 0 7 8 1666666667 Note In the first test case, $$$1$$$ tent will be given to the introverts, $$$1$$$ tent will be shared by two extroverts and one universal, and the last tent will be shared by two universals. In total, $$$3$$$ tents are needed. In the second test case, three extroverts will take $$$1$$$ tent, and $$$1$$$ tent will be taken by an introvert. Then, one extrovert and one universal will be left. This extrovert will not be able to live with two others. | 800 | true | true | false | false | false | false | false | false | false | false | 613 |
2033F | Problem - 2033F - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force math number theory *1800 No tag edit access β Contest materials =f(2)=1$$$. $$$f(n)=f(n-1)+f(n-2)$$$ $$$(3le n)$$$ We denote $$$G(n,k)$$$ as an index of the $$$n$$$-th Fibonacci number that is divisible by $$$k$$$. For given $$$n$$$ and $$$k$$$, compute $$$G(n,k)$$$.As this number can be too big, output it by modulo $$$10^9+7$$$. For example: $$$G(3,2)=9$$$ because the $$$3$$$-rd Fibonacci number that is divisible by $$$2$$$ is $$$34$$$. $$$ | 1,800 | true | false | false | false | false | false | true | false | false | false | 63 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.