contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
835 | F | 835F | F. Roads in the Kingdom | 2,500 | dfs and similar; dp; graphs; trees | In the Kingdom K., there are n towns numbered with integers from 1 to n. The towns are connected by n bi-directional roads numbered with integers from 1 to n. The i-th road connects the towns ui and vi and its length is li. There is no more than one road between two towns. Also, there are no roads that connect the town... | The first line contains the integer n (3 β€ n β€ 2Β·105) β the number of towns and roads.The next n lines contain the roads description. The i-th from these lines contains three integers ui, vi, li (1 β€ ui, vi β€ n, 1 β€ li β€ 109) β the numbers of towns connected by the i-th road and the length of the i-th road. No road con... | Print a single integer β the minimum possible inconvenience of the roads after the refusal from one of the roads. | Input: 31 2 42 3 51 3 1 | Output: 5 | Expert | 4 | 695 | 511 | 113 | 8 | |
724 | C | 724C | C. Ray Tracing | 1,800 | greedy; hashing; implementation; math; number theory; sortings | There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.At the... | The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors ar... | Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. | In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1)... | Input: 3 3 41 11 22 12 2 | Output: 1-1-12 | Medium | 6 | 937 | 348 | 187 | 7 |
1,838 | B | 1838B | B. Minimize Permutation Subarrays | 1,100 | constructive algorithms; math | You are given a permutation \(p\) of size \(n\). You want to minimize the number of subarrays of \(p\) that are permutations. In order to do so, you must perform the following operation exactly once: Select integers \(i\), \(j\), where \(1 \le i, j \le n\), then Swap \(p_i\) and \(p_j\). For example, if \(p = [5, 1, 4,... | The first line of the input 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 a single integer \(n\) (\(3 \le n \le 2\cdot 10^5\)) β the size of the permutation.The next line of each test case contains \... | For each test case, output two integers \(i\) and \(j\) (\(1 \le i, j \le n\)) β the indices to swap in \(p\).If there are multiple solutions, print any of them. | For the first test case, there are four possible arrays after the swap: If we swap \(p_1\) and \(p_2\), we get the array \([2, 1, 3]\), which has 3 subarrays that are permutations (\([1]\), \([2, 1]\), \([2, 1, 3]\)). If we swap \(p_1\) and \(p_3\), we get the array \([3, 2, 1]\), which has 3 subarrays that are permuta... | Input: 831 2 331 3 251 3 2 5 464 5 6 1 2 398 7 6 3 2 1 4 5 9107 10 5 1 9 8 3 2 6 4108 5 10 9 2 1 3 4 6 7102 3 5 7 10 1 8 6 4 9 | Output: 2 3 1 1 5 2 1 4 9 5 8 8 6 10 5 4 | Easy | 2 | 1,134 | 538 | 161 | 18 |
921 | 11 | 92111 | 11. Labyrinth-11 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 | |||||
587 | F | 587F | F. Duff is Mad | 3,000 | data structures; strings | Duff is mad at her friends. That's why she sometimes makes Malek to take candy from one of her friends for no reason! She has n friends. Her i-th friend's name is si (their names are not necessarily unique). q times, she asks Malek to take candy from her friends. She's angry, but also she acts with rules. When she want... | The first line of input contains two integers n and q (1 β€ n, q β€ 105).The next n lines contain the names. i-th of them contains an string si, consisting of lowercase English letters ().The next q lines contain the requests. Each of them contains three integers, l, r and k (says that Malek should take candies from Duff... | Print the answer to each request in one line. | Input: 5 5aababababababb1 5 43 5 41 5 21 5 31 4 1 | Output: 126371 | Master | 2 | 708 | 336 | 45 | 5 | |
1,838 | E | 1838E | E. Count Supersequences | 2,500 | combinatorics; dp; math | You are given an array \(a\) of \(n\) integers, where all elements \(a_i\) lie in the range \([1, k]\). How many different arrays \(b\) of \(m\) integers, where all elements \(b_i\) lie in the range \([1, k]\), contain \(a\) as a subsequence? Two arrays are considered different if they differ in at least one position.A... | The first line of the input 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 three integers \(n\), \(m\), \(k\) (\(1 \le n \le 2\cdot 10^5\), \(n \le m \le 10^9\), \(1 \le k \le 10^9\)) β the size of \(... | For each test case, output a single integer β the number of suitable arrays \(b\), modulo \(10^9+7\). | For the first example, since \(k=1\), there is only one array of size \(m\) consisting of the integers \([1, k]\). This array (\([1, 1, \ldots, 1]\)) contains the original array as a subsequence, so the answer is 1.For the second example, the \(9\) arrays are \([1, 1, 2, 2]\), \([1, 2, 1, 2]\), \([1, 2, 2, 1]\), \([1, ... | Input: 71 1000000 113 4 31 2 25 7 81 2 3 4 16 6 1818 2 2 5 2 161 10 218 10 12345671 1 2 1 2 2 2 15 1000000000 1000000000525785549 816356460 108064697 194447117 725595511 | Output: 1 9 1079 1 1023 906241579 232432822 | Expert | 3 | 529 | 629 | 101 | 18 |
1,637 | E | 1637E | E. Best Pair | 2,100 | binary search; brute force; implementation | You are given an array \(a\) of length \(n\). Let \(cnt_x\) be the number of elements from the array which are equal to \(x\). Let's also define \(f(x, y)\) as \((cnt_x + cnt_y) \cdot (x + y)\).Also you are given \(m\) bad pairs \((x_i, y_i)\). Note that if \((x, y)\) is a bad pair, then \((y, x)\) is also bad.Your tas... | The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\), \(0 \le m \le 3 \cdot 10^5\)) β the length of the array and the number of bad pairs.The second line of each test case ... | For each test case print a single integer β the answer to the problem. | In the first test case \(3\), \(6\), \(7\) occur in the array. \(f(3, 6) = (cnt_3 + cnt_6) \cdot (3 + 6) = (3 + 2) \cdot (3 + 6) = 45\). But \((3, 6)\) is bad so we ignore it. \(f(3, 7) = (cnt_3 + cnt_7) \cdot (3 + 7) = (3 + 1) \cdot (3 + 7) = 40\). \(f(6, 7) = (cnt_6 + cnt_7) \cdot (6 + 7) = (2 + 1) \cdot (6 + 7) = 39... | Input: 36 16 3 6 7 3 33 62 03 47 41 2 2 3 1 5 11 53 51 32 5 | Output: 40 14 15 | Hard | 3 | 546 | 950 | 70 | 16 |
600 | D | 600D | D. Area of Two Circles' Intersection | 2,000 | geometry | You are given two circles. Find the area of their intersection. | The first line contains three integers x1, y1, r1 ( - 109 β€ x1, y1 β€ 109, 1 β€ r1 β€ 109) β the position of the center and the radius of the first circle.The second line contains three integers x2, y2, r2 ( - 109 β€ x2, y2 β€ 109, 1 β€ r2 β€ 109) β the position of the center and the radius of the second circle. | Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. | Input: 0 0 46 0 4 | Output: 7.25298806364175601379 | Hard | 1 | 63 | 306 | 145 | 6 | |
108 | B | 108B | B. Datatypes | 1,400 | math; sortings | Tattah's youngest brother, Tuftuf, is new to programming.Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava.Today, Tuftuf was introduced to Gava's unsigned integer datatypes. Gava ... | The first line contains integer n (2 β€ n β€ 105) β the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 β€ ai β€ 109) β sizes of datatypes in bits. Some datatypes may have equal sizes. | Print ""YES"" if Tuftuf will stop using Gava, and ""NO"" otherwise. | In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits. | Input: 364 16 32 | Output: NO | Easy | 2 | 775 | 256 | 67 | 1 |
1,817 | D | 1817D | D. Toy Machine | 2,700 | constructive algorithms; games; implementation | There is a toy machine with toys arranged in two rows of \(n\) cells each (\(n\) is odd). Initial state for \(n=9\). Initially, \(n-2\) toys are placed in the non-corner cells of the top row. The bottom row is initially empty, and its leftmost, rightmost, and central cells are blocked. There are \(4\) buttons to contro... | The first and only line contains two integers, \(n\) and \(k\) (\(5 \le n \le 100\,000\), \(n\) is odd, \(1 \le k \le n-2\)) β the number of cells in a row, and the index of the toy that has to be moved to the leftmost cell of the top row. | On a single line, output a description of the button presses as a string of at most \(1\,000\,000\) characters. The string should only contain the characters L, R, U, and D. The \(i\)-th character in the string is the \(i\)-th button that is pressed. After all the button presses are performed, the \(k\)-th toy should b... | In the first example, there will be \(5-2 = 3\) toys. The first toy needs to end up in the leftmost cell of the top row. The moves RDL will achieve this, see the picture for a better understanding. Another possible solution would be to do one button press L. Visualization of the moves for the first example. | Input: 5 1 | Output: RDL | Master | 3 | 921 | 239 | 461 | 18 |
1,993 | C | 1993C | C. Light Switches | 1,400 | implementation; math | There is an apartment consisting of \(n\) rooms, each with its light initially turned off.To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represent... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of rooms in the apartment and the period of the chips.The second line contains \(n\) distinct integers... | For each test case, print a single integer β the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print \(-1\) instead. | In the first test case, all lights will be on by the minute \(5\) without any of them being turned off by the chips. The answer is \(5\).In the second test case, due to \(k=3\), the \(1\)-st light will be on at minutes \(2, 3, 4, 8, 9, 10, 14, \ldots\); meanwhile, the \(4\)-th light will be on at minutes \(5, 6, 7, 11,... | Input: 94 42 3 4 54 32 3 4 54 33 4 8 93 36 2 11 117 514 34 6 25 46 7 176 540 80 99 60 90 506 564 40 50 68 70 102 11 1000000000 | Output: 5 -1 10 8 1 47 100 -1 -1 | Easy | 2 | 926 | 507 | 182 | 19 |
1,357 | E2 | 1357E2 | E2. Root of quantum Fourier transform | 0 | *special | Implement an operation that is equivalent to the operation QFT\(^{1/P}\), where QFT is the quantum Fourier transform. In other words, your operation, applied \(P\) times, should have the same effect as applying QFT. You can implement the required transformation up to a global phase.Your operation should take the follow... | Beginner | 1 | 1,355 | 0 | 0 | 13 | ||||
1,353 | A | 1353A | A. Most Unstable Array | 800 | constructive algorithms; greedy; math | You are given two integers \(n\) and \(m\). You have to construct the array \(a\) of length \(n\) consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly \(m\) and the value \(\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|\) is the maximum possib... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^9\)) β the length of the array and its sum correspondingly. | For each test case, print the answer β the maximum possible value of \(\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|\) for the array \(a\) consisting of \(n\) non-negative integers with the sum \(m\). | In the first test case of the example, the only possible array is \([100]\) and the answer is obviously \(0\).In the second test case of the example, one of the possible arrays is \([2, 0]\) and the answer is \(|2-0| = 2\).In the third test case of the example, one of the possible arrays is \([0, 2, 0, 3, 0]\) and the ... | Input: 5 1 100 2 2 5 5 2 1000000000 1000000000 1000000000 | Output: 0 2 10 1000000000 2000000000 | Beginner | 3 | 811 | 284 | 194 | 13 |
509 | D | 509D | D. Restoring Numbers | 2,200 | constructive algorithms; math | Vasya had two arrays consisting of non-negative integers: a of size n and b of size m. Vasya chose a positive integer k and created an n Γ m matrix v using the following formula:Vasya wrote down matrix v on a piece of paper and put it in the table.A year later Vasya was cleaning his table when he found a piece of paper... | The first line contains integers n and m (1 β€ n, m β€ 100), separated by a space β the number of rows and columns in the found matrix, respectively. The i-th of the following lines contains numbers wi, 1, wi, 2, ..., wi, m (0 β€ wi, j β€ 109), separated by spaces β the elements of the i-th row of matrix w. | If the matrix w could not have been obtained in the manner described above, print ""NO"" (without quotes) in the single line of output.Otherwise, print four lines.In the first line print ""YES"" (without quotes).In the second line print an integer k (1 β€ k β€ 1018). Note that each element of table w should be in range b... | By we denote the remainder of integer division of b by c.It is guaranteed that if there exists some set of numbers k, a1, ..., an, b1, ..., bm, that you could use to make matrix w, then there also exists a set of numbers that meets the limits 1 β€ k β€ 1018, 1 β€ ai β€ 1018, 1 β€ bi β€ 1018 in the output format. In other wor... | Input: 2 31 2 32 3 4 | Output: YES10000000070 1 1 2 3 | Hard | 2 | 694 | 304 | 528 | 5 |
1,473 | A | 1473A | A. Replacing Elements | 800 | greedy; implementation; math; sortings | You have an array \(a_1, a_2, \dots, a_n\). All \(a_i\) are positive integers.In one step you can choose three distinct indices \(i\), \(j\), and \(k\) (\(i \neq j\); \(i \neq k\); \(j \neq k\)) and assign the sum of \(a_j\) and \(a_k\) to \(a_i\), i. e. make \(a_i = a_j + a_k\).Can you make all \(a_i\) lower or equal ... | The first line contains a single integer \(t\) (\(1 \le t \le 2000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(d\) (\(3 \le n \le 100\); \(1 \le d \le 100\)) β the number of elements in the array \(a\) and the value \(d\).The second line contains \(n\) integers \(a_1... | For each test case, print YES, if it's possible to make all elements \(a_i\) less or equal than \(d\) using the operation above. Otherwise, print NO.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). | In the first test case, we can prove that we can't make all \(a_i \le 3\).In the second test case, all \(a_i\) are already less or equal than \(d = 4\).In the third test case, we can, for example, choose \(i = 5\), \(j = 1\), \(k = 2\) and make \(a_5 = a_1 + a_2 = 2 + 1 = 3\). Array \(a\) will become \([2, 1, 5, 3, 3]\... | Input: 3 5 3 2 3 2 5 4 3 4 2 4 4 5 4 2 1 5 3 6 | Output: NO YES YES | Beginner | 4 | 392 | 382 | 263 | 14 |
543 | E | 543E | E. Listening to Music | 3,200 | constructive algorithms; data structures | Please note that the memory limit differs from the standard.You really love to listen to music. During the each of next s days you will listen to exactly m songs from the playlist that consists of exactly n songs. Let's number the songs from the playlist with numbers from 1 to n, inclusive. The quality of song number i... | The first line contains two positive integers n, m (1 β€ m β€ n β€ 2Β·105). The second line contains n positive integers a1, a2, ..., an (0 β€ ai < 230) β the description of songs from the playlist. The next line contains a single number s (1 β€ s β€ 2Β·105) β the number of days that you consider.The next s lines contain three... | Print exactly s integers ans1, ans2, ..., anss, where ansi is the minimum displeasure that you can get on day i. | Input: 5 31 2 1 2 351 1 21 3 21 3 31 3 51 3 1 | Output: 20231 | Master | 2 | 616 | 571 | 112 | 5 | |
273 | E | 273E | E. Dima and Game | 2,600 | dp; games | Dima and Anya love playing different games. Now Dima has imagined a new game that he wants to play with Anya.Dima writes n pairs of integers on a piece of paper (li, ri) (1 β€ li < ri β€ p). Then players take turns. On his turn the player can do the following actions: choose the number of the pair i (1 β€ i β€ n), such tha... | The first line contains two integers n, p (1 β€ n β€ 1000, 1 β€ p β€ 109). The numbers are separated by a single space. | In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7). | Input: 2 2 | Output: 0 | Expert | 2 | 899 | 115 | 109 | 2 | |
2,119 | C | 2119C | C. A Good Problem | 1,300 | bitmasks; constructive algorithms; math | Juggernaut. - Lost Dream feat.ζεγ―γ You are given four positive integers \(n, l, r, k\). You need to find the lexicographically smallest\(^{\text{β}}\) array \(a\) of length \(n\), consisting of integers, such that: For every \(1 \leq i \leq n\), \(l \leq a_i \leq r\). \(a_1 \, \& \, a_2 \, \& \, \ldots \, \& \, a_n = a... | 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 contains four positive integers \(n,l,r,k\) (\(1 \le k \le n \le 10^{18}\), \(1 \le l \le r \le 10^{18}\)). | For each test case, output \(a_k\) or \(-1\) if no array meets the conditions. | In the first test case, the array \(a = [4]\). It can be proven that there is no array that meets the above requirements and has a smaller lexicographic order.In the second test case, the array \(a= [1,1,1]\). It can be proven that there is no array that meets the above requirements and has a smaller lexicographic orde... | Input: 91 4 4 13 1 3 34 6 9 24 6 9 34 6 7 42 5 5 12 3 6 2999999999999999999 1000000000000000000 1000000000000000000 9999999999999999991000000000000000000 1 999999999999999999 1000000000000000000 | Output: 4 1 6 8 -1 -1 -1 1000000000000000000 2 | Easy | 3 | 883 | 284 | 78 | 21 |
1,679 | A | 1679A | A. AvtoBus | 900 | brute force; greedy; math; number theory | Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses.You own a small bus service business and you have just received an order to replace \(n\) tires. You know that the bus fleet owns two types of buses: with two axles (these buses have \... | The first line contains an integer \(t\) (\(1 \le t \le 1\,000\)) β the number of test cases. The following lines contain description of test cases.The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^{18}\)) β the total number of wheels for all buses. | For each test case print the answer in a single line using the following format.Print two integers \(x\) and \(y\) (\(1 \le x \le y\)) β the minimum and the maximum possible number of buses that can be in the bus fleet.If there is no suitable number of buses for the given \(n\), print the number \(-1\) as the answer. | In the first test case the total number of wheels is \(4\). It means that there is the only one bus with two axles in the bus fleet.In the second test case it's easy to show that there is no suitable number of buses with \(7\) wheels in total.In the third test case the total number of wheels is \(24\). The following op... | Input: 44724998244353998244352 | Output: 1 1 -1 4 6 166374058999707392 249561088499561088 | Beginner | 4 | 667 | 276 | 318 | 16 |
1,746 | B | 1746B | B. Rebellion | 800 | constructive algorithms; greedy; two pointers | You have an array \(a\) of size \(n\) consisting only of zeroes and ones. You can do the following operation: choose two indices \(1 \le i , j \le n\), \(i \ne j\), add \(a_{i}\) to \(a_{j}\), remove \(a_{i}\) from \(a\). Note that elements of \(a\) can become bigger than \(1\) after performing some operations. Also no... | 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 an integer \(n\) (\(1 \le n \le 10^5\)), the size of array \(a\).Next line contains \(n\) integers \(a_{1}, a_{2}, \l... | For each test case print a single integer, minimum number of operations needed to make \(a\) non-decreasing. | In the first test case, \(a\) is already non-decreasing, so you don't need to do any operations and the answer is \(0\).In the second test case, you can perform an operation for \(i = 1\) and \(j = 5\), so \(a\) will be equal to \([0, 0, 1, 2]\) and it becomes non-decreasing.In the third test case, you can perform an o... | Input: 480 0 1 1 1 1 1 151 0 0 1 121 0111 1 0 0 1 0 0 1 1 1 0 | Output: 0 1 1 3 | Beginner | 3 | 513 | 472 | 108 | 17 |
1,633 | E | 1633E | E. Spanning Tree Queries | 2,400 | binary search; data structures; dfs and similar; dsu; graphs; greedy; math; sortings; trees | You are given a connected weighted undirected graph, consisting of \(n\) vertices and \(m\) edges.You are asked \(k\) queries about it. Each query consists of a single integer \(x\). For each query, you select a spanning tree in the graph. Let the weights of its edges be \(w_1, w_2, \dots, w_{n-1}\). The cost of a span... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 50\); \(n - 1 \le m \le 300\)) β the number of vertices and the number of edges in the graph.Each of the next \(m\) lines contains a description of an undirected edge: three integers \(v\), \(u\) and \(w\) (\(1 \le v, u \le n\); \(v \neq u\); \(0 \le w... | Print a single integer β the xor of answers to all queries. | The queries in the first example are \(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0\). The answers are \(11, 9, 7, 3, 1, 5, 8, 7, 5, 7, 11\). The queries in the second example are \(3, 0, 2, 1, 6, 0, 3, 5, 4, 1\). The answers are \(14, 19, 15, 16, 11, 19, 14, 12, 13, 16\). The queries in the third example are \(75, 0, 0, \dots\). T... | Input: 5 8 4 1 4 3 1 0 3 5 3 2 5 4 3 4 8 4 3 4 4 2 8 5 3 9 3 11 1 1 10 0 1 2 | Output: 4 | Expert | 9 | 751 | 858 | 59 | 16 |
1,869 | A | 1869A | A. Make It Zero | 900 | constructive algorithms | During Zhongkao examination, Reycloer met an interesting problem, but he cannot come up with a solution immediately. Time is running out! Please help him.Initially, you are given an array \(a\) consisting of \(n \ge 2\) integers, and you want to change all elements in it to \(0\).In one operation, you select two indice... | The first line of input contains a single integer \(t\) (\(1\le t\le 500\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a... | For each test case, in the first line output a single integer \(k\) (\(0\le k\le 8\)) β the number of operations you use.Then print \(k\) lines, in the \(i\)-th line output two integers \(l_i\) and \(r_i\) (\(1\le l_i\le r_i\le n\)) representing that you select \(l_i\) and \(r_i\) in the \(i\)-th operation. Note that y... | In the first test case, since \(1\oplus2\oplus3\oplus0=0\), after performing the operation on segment \([1,4]\), all the elements in the array are equal to \(0\).In the second test case, after the first operation, the array becomes equal to \([3,1,4,15,15,15,15,6]\), after the second operation, the array becomes equal ... | Input: 641 2 3 083 1 4 1 5 9 2 661 5 4 1 4 750 0 0 0 071 1 9 9 0 1 83100 100 0 | Output: 1 1 4 2 4 7 1 8 6 1 2 3 4 5 6 1 3 4 6 1 6 0 4 1 2 6 7 3 4 6 7 1 1 2 | Beginner | 1 | 795 | 396 | 414 | 18 |
18 | B | 18B | B. Platforms | 1,700 | brute force; math | In one one-dimensional world there are n platforms. Platform with index k (platforms are numbered from 1) is a segment with coordinates [(k - 1)m, (k - 1)m + l], and l < m. Grasshopper Bob starts to jump along the platforms from point 0, with each jump he moves exactly d units right. Find out the coordinate of the poin... | The first input line contains 4 integer numbers n, d, m, l (1 β€ n, d, m, l β€ 106, l < m) β respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers m and l needed to find coordinates of the k-th platform: [(k - 1)m, (k - 1)m + l]. | Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down. | Input: 2 2 5 3 | Output: 4 | Medium | 2 | 492 | 258 | 157 | 0 | |
1,313 | A | 1313A | A. Fast Food Restaurant | 900 | brute force; greedy; implementation | Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made \(a\) portions of dumplings, \(b\) portions of cranberry juice and \(c\) pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set o... | The first line contains an integer \(t\) (\(1 \le t \le 500\)) β the number of test cases to solve.Each of the remaining \(t\) lines contains integers \(a\), \(b\) and \(c\) (\(0 \leq a, b, c \leq 10\)) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pan... | For each test case print a single integer β the maximum number of visitors Denis can feed. | In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can ser... | Input: 71 2 10 0 09 1 72 2 32 3 23 2 24 4 4 | Output: 3045557 | Beginner | 3 | 824 | 337 | 90 | 13 |
400 | A | 400A | A. Inna and Choose Options | 1,000 | implementation | There always is something to choose from! And now, instead of ""Noughts and Crosses"", Inna choose a very unusual upgrade of this game. The rules of the game are given below:There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: ""X... | The first line of the input contains integer t (1 β€ t β€ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line.The description of each test is a string consisting of 12 characters, each character is either ""X"", or ""O"". The i-th cha... | For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespac... | Input: 4OXXXOXOOXOOXOXOXOXOXOXOXXXXXXXXXXXXXOOOOOOOOOOOO | Output: 3 1x12 2x6 4x34 1x12 2x6 3x4 6x26 1x12 2x6 3x4 4x3 6x2 12x10 | Beginner | 1 | 996 | 409 | 323 | 4 | |
814 | E | 814E | E. An unavoidable detour for home | 2,600 | combinatorics; dp; graphs; shortest paths | Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be... | The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even. | Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7. | In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1. In the second example, the following two structures satisfy the constraints. | Input: 43 2 3 2 | Output: 1 | Expert | 4 | 1,488 | 294 | 114 | 8 |
847 | J | 847J | J. Students Initiation | 2,400 | binary search; flows; graphs | Soon the first year students will be initiated into students at the University of Berland. The organizers of the initiation come up with a program for this holiday. In their opinion, it would be good if the first-year students presented small souvenirs to each other. When they voiced this idea to the first-year student... | The first line contains two integers n and m (1 β€ n β€ 5000, 0 β€ m β€ min(5000, nΒ·(n - 1) / 2)) β the number of the first year students and the number of pairs of the students that know each other. The students are numbered from 1 to n.Each of the following m lines contains two integers xi, yi (1 β€ xi, yi β€ n, xi β yi) β... | Print a single integer into the first line β the smallest number of souvenirs that the unluckiest student will have to present.Following should be m lines, each containing two integers β the students which are familiar with each other. The first number in the pair must be the student that will present the souvenir to t... | Input: 5 42 11 32 32 5 | Output: 11 22 33 15 2 | Expert | 3 | 1,376 | 515 | 432 | 8 | |
1,107 | G | 1107G | G. Vasya and Maximum Profit | 2,400 | binary search; constructive algorithms; data structures; dp; dsu | Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit.Vasya has \(n\) problems to choose from. They are numbered from \(1\) to \(n\). The difficulty of the \(i\)-th problem is \(d_i\). Moreover, the problems are given in the incre... | The first line contains two integers \(n\) and \(a\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le a \le 10^9\)) β the number of proposed tasks and the profit for a single problem, respectively.Each of the next \(n\) lines contains two integers \(d_i\) and \(c_i\) (\(1 \le d_i, c_i \le 10^9, d_i < d_{i+1}\)). | Print one integer β maximum amount of burles Vasya can earn. | Input: 5 10 1 15 5 3 6 11 7 2 11 22 | Output: 13 | Expert | 5 | 1,158 | 303 | 60 | 11 | |
1,684 | F | 1684F | F. Diverse Segments | 2,600 | data structures; two pointers | You are given an array \(a\) of \(n\) integers. Also you are given \(m\) subsegments of that array. The left and the right endpoints of the \(j\)-th segment are \(l_j\) and \(r_j\) respectively.You are allowed to make no more than one operation. In that operation you choose any subsegment of the array \(a\) and replace... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the size of the array and the numbe... | For each test case output a single integer β the smallest length of a segment you can apply an operation on making the elements on all given segments distinct. If it is not needed to use the operation, output \(0\). | In the first test case you can perform the operation on the segment \([1, 2]\) and make \(a = [5, 6, 2, 1, 3, 3, 5]\). Then the elements on each segment are distinct. On the segment \([1, 4]\) there are \([5, 6, 2, 1]\). On the segment \([4, 5]\) there are \([1, 3]\). On the segment \([2, 4]\) there are \([6, 2, 1, 3]\... | Input: 57 31 1 2 1 3 3 51 44 52 45 210 1 6 14 14 52 44 55 7 5 62 21 32 43 33 47 32 2 2 7 8 2 24 44 45 51 11231 1 | Output: 2 0 1 0 0 | Expert | 2 | 925 | 729 | 215 | 16 |
1,505 | C | 1505C | C. Fibonacci Words | 1,400 | *special; implementation | The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive. | Output ""YES"" or ""NO"". | Input: HELP | Output: YES | Easy | 2 | 0 | 131 | 25 | 15 | ||
47 | B | 47B | B. Coins | 1,200 | implementation | One day Vasya came across three Berland coins. They didn't have any numbers that's why Vasya didn't understand how their denominations differ. He supposed that if one coin is heavier than the other one, then it should be worth more. Vasya weighed all the three pairs of coins on pan balance scales and told you the resul... | The input data contains the results of all the weighting, one result on each line. It is guaranteed that every coin pair was weighted exactly once. Vasya labelled the coins with letters Β«AΒ», Β«BΒ» and Β«CΒ». Each result is a line that appears as (letter)(> or < sign)(letter). For example, if coin ""A"" proved lighter than ... | It the results are contradictory, print Impossible. Otherwise, print without spaces the rearrangement of letters Β«AΒ», Β«BΒ» and Β«CΒ» which represent the coins in the increasing order of their weights. | Input: A>BC<BA>C | Output: CBA | Easy | 1 | 448 | 367 | 197 | 0 | |
1,391 | D | 1391D | D. 505 | 2,000 | bitmasks; brute force; constructive algorithms; dp; greedy; implementation | A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix \(a\) consisting of \(n\) rows and \(m\) columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above ha... | The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n \leq m \leq 10^6\) and \(n\cdot m \leq 10^6\)) β the number of rows and columns in \(a\), respectively. The following \(n\) lines each contain \(m\) characters, each of which is one of 0 and 1. If the \(j\)-th character on the \(i\)-th line is 1,... | Output the minimum number of cells you need to change to make \(a\) good, or output \(-1\) if it's not possible at all. | In the first case, changing \(a_{1,1}\) to \(0\) and \(a_{2,2}\) to \(1\) is enough. You can verify that there is no way to make the matrix in the second case good. Some definitions β A binary matrix is one in which every element is either \(1\) or \(0\). A sub-matrix is described by \(4\) parameters β \(r_1\), \(r_2\)... | Input: 3 3 101 001 110 | Output: 2 | Hard | 6 | 402 | 428 | 119 | 13 |
852 | A | 852A | A. Digits | 2,500 | brute force; implementation; math | John gave Jack a very hard problem. He wrote a very big positive integer A0 on a piece of paper. The number is less than 10200000 . In each step, Jack is allowed to put ' + ' signs in between some of the digits (maybe none) of the current number and calculate the sum of the expression. He can perform the same procedure... | First line contains a positive integer N (1 β€ N β€ 200000), representing the number of digits of A0.Second line contains a string of length N representing positive integer number A0. Each character is digit. There will be no leading zeros. | Output exactly three lines, the steps Jack needs to perform to solve the problem. You can output any sequence of steps which results in a single digit number (and is logically consistent).Every step consists of digits and ' + ' signs. Steps should not contain several ' + ' signs in a row, whitespaces, or ' + ' signs as... | In the first sample, Jack can't put ' + ' signs anywhere, so he just writes 1 in each line and solves the problem. Here, solution is unique.In the second sample, Jack first puts ' + ' between every two consecutive digits, thus getting the result 5 + 8 + 0 + 6 = 19. He does the same on the second step, getting 1 + 9 = 1... | Input: 11 | Output: 111 | Expert | 3 | 884 | 238 | 459 | 8 |
1,141 | F1 | 1141F1 | F1. Same Sum Blocks (Easy) | 1,900 | greedy | This problem is given in two editions, which differ exclusively in the constraints on the number \(n\).You are given an array of integers \(a[1], a[2], \dots, a[n].\) A block is a sequence of contiguous (consecutive) elements \(a[l], a[l+1], \dots, a[r]\) (\(1 \le l \le r \le n\)). Thus, a block is defined by a pair of... | The first line contains integer \(n\) (\(1 \le n \le 50\)) β the length of the given array. The second line contains the sequence of elements \(a[1], a[2], \dots, a[n]\) (\(-10^5 \le a_i \le 10^5\)). | In the first line print the integer \(k\) (\(1 \le k \le n\)). The following \(k\) lines should contain blocks, one per line. In each line print a pair of indices \(l_i, r_i\) (\(1 \le l_i \le r_i \le n\)) β the bounds of the \(i\)-th block. You can print blocks in any order. If there are multiple answers, print any of... | Input: 7 4 1 2 2 1 5 3 | Output: 3 7 7 2 3 4 5 | Hard | 1 | 1,106 | 199 | 326 | 11 | |
2,006 | C | 2006C | C. Eri and Expanded Sets | 2,300 | data structures; divide and conquer; math; number theory; two pointers | Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers \(x\neq y\) from the set such that their average \(\frac{x+y}2\) is still a positive integer and isn't contained in the set, and add it to the set. The integers \(x\) and \... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 4 \cdot 10^5\)) β length of the array \(a\).The secon... | For each test case, output a single integer β the number of brilliant subarrays. | In the first test case, the array \(a = [2, 2]\) has \(3\) subarrays: \([2]\), \([2]\), and \([2, 2]\). For all of them, the set only contains a single integer \(2\), therefore it's always consecutive. All these subarrays are brilliant, so the answer is \(3\).In the second test case, let's consider the subarray \([3, 6... | Input: 622 261 3 6 10 15 2156 30 18 36 91100000000061 1 4 5 1 41270 130 90 90 90 108 612 500 451 171 193 193 | Output: 3 18 5 1 18 53 | Expert | 5 | 1,224 | 546 | 80 | 20 |
33 | B | 33B | B. String Problem | 1,800 | shortest paths | Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary ch... | The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 β€ n β€ 500) β amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin lette... | If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any. | Input: uayduxxd3a x 8x y 13d c 3 | Output: 21uxyd | Medium | 1 | 717 | 469 | 160 | 0 | |
1,901 | D | 1901D | D. Yet Another Monster Fight | 1,700 | binary search; dp; greedy; implementation; math | Vasya is a sorcerer that fights monsters. Again. There are \(n\) monsters standing in a row, the amount of health points of the \(i\)-th monster is \(a_i\).Vasya is a very powerful sorcerer who knows many overpowered spells. In this fight, he decided to use a chain lightning spell to defeat all the monsters. Let's see ... | The first line of the input contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of monsters.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the amount of health points of the \(i\)-th monster. | Print one integer β the minimum spell power required to kill all the monsters if Vasya chooses the first target optimally, and the order of spell hits can be any possible within the given rules. | Input: 6 2 1 5 6 4 3 | Output: 8 | Medium | 5 | 1,843 | 285 | 194 | 19 | |
1,348 | E | 1348E | E. Phoenix and Berries | 2,400 | brute force; dp; greedy; math | Phoenix is picking berries in his backyard. There are \(n\) shrubs, and each shrub has \(a_i\) red berries and \(b_i\) blue berries.Each basket can contain \(k\) berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, al... | The first line contains two integers \(n\) and \(k\) (\( 1\le n, k \le 500\)) β the number of shrubs and the basket capacity, respectively.The \(i\)-th of the next \(n\) lines contain two integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)) β the number of red and blue berries in the \(i\)-th shrub, respectively. | Output one integer β the maximum number of baskets that Phoenix can fill completely. | The first example is described above.In the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub.In the third example, Phoenix cannot fill any basket completely because there are less than \(5\) berries in each shrub, less than \(5\) total red berries, and less than \(... | Input: 2 4 5 2 2 1 | Output: 2 | Expert | 4 | 904 | 320 | 84 | 13 |
1,434 | E | 1434E | E. A Convex Game | 3,500 | dsu; games | Shikamaru and Asuma like to play different games, and sometimes they play the following: given an increasing list of numbers, they take turns to move. Each move consists of picking a number from the list.Assume the picked numbers are \(v_{i_1}\), \(v_{i_2}\), \(\ldots\), \(v_{i_k}\). The following conditions must hold:... | The first line contains the only integer \(n\) (\(1 \leq n \leq 1000\)) standing for the number of games Shikamaru and Asuma play at once. Next lines describe the games.Each description starts from a line with the only number \(m\) (\(m\geq 1\)) denoting the length of the number list. The second line contains the incre... | Print ""YES"" if Shikamaru can secure the victory, and ""NO"" otherwise. | In the first example Shikamaru can pick the last number, and Asuma cannot do anything because of the first constraint.In the second sample test Asuma can follow the symmetric strategy, repeating Shikamaru's moves in the other instance each time, and therefore win. | Input: 1 10 1 2 3 4 5 6 7 8 9 10 | Output: YES | Master | 2 | 808 | 507 | 72 | 14 |
1,932 | C | 1932C | C. LR-remainders | 1,400 | brute force; data structures; implementation; math; two pointers | You are given an array \(a\) of length \(n\), a positive integer \(m\), and a string of commands of length \(n\). Each command is either the character 'L' or the character 'R'.Process all \(n\) commands in the order they are written in the string \(s\). Processing a command is done as follows: First, output the remaind... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Then descriptions of \(t\) test cases follow.Each test case of the input is given by three lines.The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2\cdot10^5, 1 \le m \le 10^4\)) β the initial leng... | For each test case, output \(n\) integers \(b_1, b_2, \dots, b_n\), where \(b_i\) is the remainder when dividing the product of all elements of the current state of the array \(a\) by \(m\) at the beginning of the execution of the \(i\)-th command. | In the first test case of the example: \(3 \cdot 1 \cdot 4 \cdot 2 \bmod 6 = 24 \bmod 6 = 0\); \(s_1 = \text{L}\), so we remove the first element and get the array \([1, 4, 2]\); \(1 \cdot 4 \cdot 2 \bmod 6 = 8 \bmod 6 = 2\); \(s_2 = \text{R}\), so we remove the last element and get the array \([1, 4]\); \(1 \cdot 4 \b... | Input: 44 63 1 4 2LRRL5 11 1 1 1 1LLLLL6 81 2 3 4 5 6RLLLRR1 1000010000R | Output: 0 2 4 1 0 0 0 0 0 0 0 0 4 4 4 0 | Easy | 5 | 796 | 700 | 248 | 19 |
1,359 | C | 1359C | C. Mixing Water | 1,700 | binary search; math | There are two infinite sources of water: hot water of temperature \(h\); cold water of temperature \(c\) (\(c < h\)). You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep bar... | The first line contains a single integer \(T\) (\(1 \le T \le 3 \cdot 10^4\)) β the number of testcases.Each of the next \(T\) lines contains three integers \(h\), \(c\) and \(t\) (\(1 \le c < h \le 10^6\); \(c \le t \le h\)) β the temperature of the hot water, the temperature of the cold water and the desired temperat... | For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to \(t\). | In the first testcase the temperature after \(2\) poured cups: \(1\) hot and \(1\) cold is exactly \(20\). So that is the closest we can achieve.In the second testcase the temperature after \(7\) poured cups: \(4\) hot and \(3\) cold is about \(29.857\). Pouring more water won't get us closer to \(t\) than that.In the ... | Input: 3 30 10 20 41 15 30 18 13 18 | Output: 2 7 1 | Medium | 2 | 1,030 | 338 | 161 | 13 |
1,974 | G | 1974G | G. Money Buys Less Happiness Now | 2,000 | data structures; greedy; sortings | You can never buy enough happiness, so here we go again! In this version, you can only buy \(h_i = 1\) unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.Being a physicist, Charlie likes to plan his life in simple and precise terms. For... | The first line of the input contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The first line of each test case contains two integers, \(m\) and \(x\) (\(1 \le m \le 2 \cdot 10^5\), \(1 \le x \le 10^3\)) β the total number of months and the monthly salary. The second line of each test case contains \(m... | For each test case, output one integer β the maximal amount of happiness Charlie can get. | Input: 63 32 2 26 52 2 8 2 6 86 44 10 3 8 6 102 11 14 14 1 3 14 21 3 4 3 | Output: 2 4 3 1 2 1 | Hard | 3 | 812 | 523 | 89 | 19 | |
1,553 | H | 1553H | H. XOR and Distance | 2,900 | bitmasks; divide and conquer; trees | You are given an array \(a\) consisting of \(n\) distinct elements and an integer \(k\). Each element in the array is a non-negative integer not exceeding \(2^k-1\).Let's define the XOR distance for a number \(x\) as the value of $$$\(f(x) = \min\limits_{i = 1}^{n} \min\limits_{j = i + 1}^{n} |(a_i \oplus x) - (a_j \op... | The first line contains two integers \(n\) and \(k\) (\(1 \le k \le 19\); \(2 \le n \le 2^k\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2^k-1\)). All these integers are distinct. | Print \(2^k\) integers. The \(i\)-th of them should be equal to \(f(i-1)\). | Consider the first example: for \(x = 0\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([6, 0, 3]\), and the minimum absolute difference of two elements is \(3\); for \(x = 1\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([7, 1, 2]\), and th... | Input: 3 3 6 0 3 | Output: 3 1 1 2 2 1 1 3 | Master | 3 | 464 | 219 | 75 | 15 |
1,481 | E | 1481E | E. Sorting Books | 2,500 | data structures; dp; greedy | One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are \(n\) books standing in a row on the shelf, the \(i\)-th book has color \(a_i\).You'd like to rearrange the books to make the shelf look beautif... | The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the number of books.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the book colors. | Output the minimum number of operations to make the shelf beautiful. | In the first example, we have the bookshelf \([1, 2, 2, 1, 3]\) and can, for example: take a book on position \(4\) and move to the right end: we'll get \([1, 2, 2, 3, 1]\); take a book on position \(1\) and move to the right end: we'll get \([2, 2, 3, 1, 1]\). In the second example, we can move the first book to the e... | Input: 5 1 2 2 1 3 | Output: 2 | Expert | 3 | 602 | 200 | 68 | 14 |
471 | A | 471A | A. MUH and Sticks | 1,100 | implementation | Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: Fou... | The single line contains six space-separated integers li (1 β€ li β€ 9) β the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. | If you can make a bear from the given set, print string ""Bear"" (without the quotes). If you can make an elephant, print string ""Elephant"" (wΔ±thout the quotes). If you can make neither a bear nor an elephant, print string ""Alien"" (without the quotes). | If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. | Input: 4 2 5 4 4 4 | Output: Bear | Easy | 1 | 896 | 193 | 256 | 4 |
1,598 | D | 1598D | D. Training Session | 1,700 | combinatorics; data structures; geometry; implementation; math | Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has \(n\) problems that none of his students have seen yet. The \(i\)-th problem has a topic \(a_i\) (an integer from \(1\) to \(n\)) and a difficulty \(b_i\) (an int... | The first line contains a single integer \(t\) (\(1 \le t \le 50000\)) β the number of testcases.The first line of each testcase contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of problems that Monocarp have.In the \(i\)-th of the following \(n\) lines, there are two integers \(a_i\) and \(b_i\) (... | Print the number of ways to select three training problems that meet either of the requirements described in the statement. | In the first example, you can take the following sets of three problems: problems \(1\), \(2\), \(4\); problems \(1\), \(3\), \(4\); problems \(2\), \(3\), \(4\). Thus, the number of ways is equal to three. | Input: 2 4 2 4 3 4 2 1 1 3 5 1 5 2 4 3 3 4 2 5 1 | Output: 3 10 | Medium | 5 | 840 | 574 | 123 | 15 |
1,121 | C | 1121C | C. System Testing | 1,600 | implementation | Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.There are \(n\) solutions, the \(i\)-th of them should be tested on \(a_i\) tests, testing one solution on one test takes \(1\) second. The solutions are judged in the order from \(1\) to \(n\).... | The first line contains two positive integers \(n\) and \(k\) (\(1 \le n \le 1000\), \(1 \le k \le 100\)) standing for the number of submissions and the number of testing processes respectively.The second line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 150\)), where \(a_i\) is equal to ... | Output the only integer β the number of interesting submissions. | Consider the first example. At time moment \(0\) both solutions start testing. At time moment \(49\) the first solution is fully tested, so at time moment \(49.5\) the second solution is being tested on the test \(50\), and the caption says ""System testing: \(50\)%"" (because there is one fully tested solution out of ... | Input: 2 2 49 100 | Output: 1 | Medium | 1 | 1,747 | 380 | 64 | 11 |
1,548 | A | 1548A | A. Web of Lies | 1,400 | brute force; graphs; greedy | When you play the game of thrones, you win, or you die. There is no middle ground.Cersei Lannister, A Game of Thrones by George R. R. MartinThere are \(n\) nobles, numbered from \(1\) to \(n\). Noble \(i\) has a power of \(i\). There are also \(m\) ""friendships"". A friendship between nobles \(a\) and \(b\) is always ... | The first line contains the integers \(n\) and \(m\) (\(1 \le n \le 2\cdot 10^5\), \(0 \le m \le 2\cdot 10^5\)) β the number of nobles and number of original friendships respectively.The next \(m\) lines each contain the integers \(u\) and \(v\) (\(1 \le u,v \le n\), \(u \ne v\)), describing a friendship. No friendship... | For each type \(3\) query print one integer to a new line. It is guaranteed that there will be at least one type \(3\) query. | Consider the first example. In the first type 3 query, we have the diagram below.In the first round of the process, noble \(1\) is weaker than all of his friends (\(2\) and \(3\)), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble \(3\) is weaker than his only friend, noble \(4\), and is th... | Input: 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 | Output: 2 1 | Easy | 3 | 1,197 | 941 | 125 | 15 |
1,268 | A | 1268A | A. Long Beautiful Integer | 1,700 | constructive algorithms; greedy; implementation; strings | You are given an integer \(x\) of \(n\) digits \(a_1, a_2, \ldots, a_n\), which make up its decimal notation in order from left to right.Also, you are given a positive integer \(k < n\).Let's call integer \(b_1, b_2, \ldots, b_m\) beautiful if \(b_i = b_{i+k}\) for each \(i\), such that \(1 \leq i \leq m - k\).You need... | The first line of input contains two integers \(n, k\) (\(2 \leq n \leq 200\,000, 1 \leq k < n\)): the number of digits in \(x\) and \(k\).The next line of input contains \(n\) digits \(a_1, a_2, \ldots, a_n\) (\(a_1 \neq 0\), \(0 \leq a_i \leq 9\)): digits of \(x\). | In the first line print one integer \(m\): the number of digits in \(y\).In the next line print \(m\) digits \(b_1, b_2, \ldots, b_m\) (\(b_1 \neq 0\), \(0 \leq b_i \leq 9\)): digits of \(y\). | Input: 3 2 353 | Output: 3 353 | Medium | 4 | 390 | 267 | 192 | 12 | |
1,368 | F | 1368F | F. Lamps on a Circle | 2,600 | games; implementation; interactive; math | This is an interactive problem.John and his imaginary friend play a game. There are \(n\) lamps arranged in a circle. Lamps are numbered \(1\) through \(n\) in clockwise order, that is, lamps \(i\) and \(i + 1\) are adjacent for any \(i = 1, \ldots, n - 1\), and also lamps \(n\) and \(1\) are adjacent. Initially all la... | When \(n = 3\), any John's move can be reversed, thus \(R(3) = 0\), and terminating the game immediately is correct.\(R(4) = 1\), and one strategy to achieve this result is shown in the second sample case.Blank lines in sample interactions are for clarity and should not be printed. | Input: 3 | Output: 0 | Expert | 4 | 1,784 | 0 | 0 | 13 | ||
297 | D | 297D | D. Color the Carpet | 2,500 | constructive algorithms | Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height h and width w. Then the grid is divided into h Γ w squares. Alice is going to assign one of k different colors to each square. The colors are numbered from 1 to k. Sh... | The first line contains three integers h, w, k (2 β€ h, w β€ 1000, 1 β€ k β€ wΒ·h).The next 2h - 1 lines describe the color constraints from top to bottom, left to right. They contain w - 1, w, w - 1, w, ..., w - 1 characters respectively. Each color constraint is represented by a character ""E"" or ""N"", where ""E"" means... | If there is a coloring that satisfies at least of the color constraints, print ""YES"" (without quotes) in the first line. In each of the next h lines, print w integers describing the coloring.Otherwise, print ""NO"" (without quotes). | Input: 3 4 4ENENNEENEEENENENN | Output: YES1 1 2 23 4 1 13 3 2 4 | Expert | 1 | 1,021 | 428 | 234 | 2 | |
426 | A | 426A | A. Sereja and Mugs | 800 | implementation | Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that... | The first line contains integers n and s (2 β€ n β€ 100; 1 β€ s β€ 1000) β the number of mugs and the volume of the cup. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 10). Number ai means the volume of the i-th mug. | In a single line, print ""YES"" (without the quotes) if his friends can play in the described manner, and ""NO"" (without the quotes) otherwise. | Input: 3 41 1 1 | Output: YES | Beginner | 1 | 701 | 225 | 144 | 4 | |
1,559 | A | 1559A | A. Mocha and Math | 900 | bitmasks; constructive algorithms; math | Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation.This day, Mocha got a sequence \(a\) of length \(n\). In each operation, she can select an arbitr... | 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 a single integer \(n\) (\(1 \le n \le 100\)) β the length of the sequence.The second line of each test c... | For each test case, print one integer β the minimal value of the maximum value in the sequence. | In the first test case, Mocha can choose the interval \([1,2]\), then the sequence becomes \([ 0, 0]\), where the first element is \(1\,\&\,2\), and the second element is \(2\,\&\,1\).In the second test case, Mocha can choose the interval \([1,3]\), then the sequence becomes \([ 1,1,1]\), where the first element is \(1... | Input: 4 2 1 2 3 1 1 3 4 3 11 3 7 5 11 7 15 3 7 | Output: 0 1 3 3 | Beginner | 3 | 879 | 399 | 95 | 15 |
1,875 | C | 1875C | C. Jellyfish and Green Apple | 1,400 | bitmasks; greedy; math; number theory | Jellyfish has \(n\) green apple pieces. Each green apple piece weighs \(1~\text{kg}\). Jellyfish wants to divide these green apple pieces equally among \(m\) people.Jellyfish has a magic knife. Each time Jellyfish can choose one piece of green apple and divide it into two smaller pieces, with each piece having half the... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)). The description of the test cases follows.The first and only line of each test case contains two integers, \(n\) and \(m\) (\(1 \leq n, m \leq 10^9\)) β the number of the green apple pieces i... | For each test case, output a single integer β the minimum number of operations required to divide all the green apples equally among the \(m\) people. If this cannot be achieved using a finite number of operations, output \(-1\) instead. | In the first test case, we do not need to divide the apple pieces. Each person will receive \(2\) pieces of \(1~\text{kg}\) and the total weight of apple pieces received by each person is \(2~\text{kg}\).In the second test case, it is impossible to divide the apple equally using a finite number of operations.In the thi... | Input: 410 51 510 43 4 | Output: 0 -1 2 5 | Easy | 4 | 541 | 358 | 237 | 18 |
1,681 | C | 1681C | C. Double Sort | 1,200 | implementation; sortings | You are given two arrays \(a\) and \(b\), both consisting of \(n\) integers.In one move, you can choose two indices \(i\) and \(j\) (\(1 \le i, j \le n\); \(i \neq j\)) and swap \(a_i\) with \(a_j\) and \(b_i\) with \(b_j\). You have to perform the swap in both arrays.You are allowed to perform at most \(10^4\) moves (... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 100\)) β the number of elements in both arrays.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the first ... | For each testcase, print the answer. If it's impossible to make both arrays sorted in a non-decreasing order in at most \(10^4\) moves, print -1. Otherwise, first, print the number of moves \(k\) \((0 \le k \le 10^4)\). Then print \(i\) and \(j\) for each move \((1 \le i, j \le n\); \(i \neq j)\).If there are multiple ... | Input: 321 21 222 11 242 3 1 22 3 2 3 | Output: 0 -1 3 3 1 3 2 4 3 | Easy | 2 | 477 | 431 | 400 | 16 | |
1,682 | C | 1682C | C. LIS or Reverse LIS? | 1,400 | constructive algorithms; greedy; implementation; math | You are given an array \(a\) of \(n\) positive integers. Let \(\text{LIS}(a)\) denote the length of longest strictly increasing subsequence of \(a\). For example, \(\text{LIS}([2, \underline{1}, 1, \underline{3}])\) = \(2\). \(\text{LIS}([\underline{3}, \underline{5}, \underline{10}, \underline{20}])\) = \(4\). \(\text... | The input consists of multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 10^4)\) β the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) \((1 \leq n \leq 2\cdot 10^5)\) β the length of array \(a\).The second lin... | For each test case, output a single integer β the maximum possible beauty of \(a\) after rearranging its elements arbitrarily. | In the first test case, \(a\) = \([6, 6, 6]\) and \(a'\) = \([6, 6, 6]\). \(\text{LIS}(a) = \text{LIS}(a')\) = \(1\). Hence the beauty is \(min(1, 1) = 1\).In the second test case, \(a\) can be rearranged to \([2, 5, 4, 5, 4, 2]\). Then \(a'\) = \([2, 4, 5, 4, 5, 2]\). \(\text{LIS}(a) = \text{LIS}(a') = 3\). Hence the ... | Input: 336 6 662 5 4 5 2 441 3 2 2 | Output: 1 3 2 | Easy | 4 | 707 | 541 | 126 | 16 |
429 | D | 429D | D. Tricky Function | 2,200 | data structures; divide and conquer; geometry | Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be t... | The first line of input contains a single integer n (2 β€ n β€ 100000). Next line contains n integers a[1], a[2], ..., a[n] ( - 104 β€ a[i] β€ 104). | Output a single integer β the value of mini β j f(i, j). | Input: 41 0 0 -1 | Output: 1 | Hard | 3 | 776 | 144 | 56 | 4 | |
450 | A | 450A | A. Jzzhu and Children | 1,000 | implementation | There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He... | The first line contains two integers n, m (1 β€ n β€ 100; 1 β€ m β€ 100). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 100). | Output a single integer, representing the number of the last child. | Let's consider the first sample. Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end ... | Input: 5 21 3 1 4 2 | Output: 4 | Beginner | 1 | 680 | 137 | 67 | 4 |
1,811 | G2 | 1811G2 | G2. Vlad and the Nice Paths (hard version) | 2,200 | binary search; combinatorics; data structures; dp; math; two pointers | This is hard version of the problem, it differs from the easy one only by constraints on \(n\) and \(k\).Vlad found a row of \(n\) tiles and the integer \(k\). The tiles are indexed from left to right and the \(i\)-th tile has the color \(c_i\). After a little thought, he decided what to do with it.You can start from a... | The first line of each test contains the integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 5000\)) β the number of tiles in a row and the length of the block.The second line of each test case contains \... | Print \(t\) numbers, each of which is the answer to the corresponding test case β the number of nice paths of maximum length modulo \(10^9 + 7\). | In the first sample, it is impossible to make a nice path with a length greater than \(0\).In the second sample, we are interested in the following paths: \(1 \rightarrow 3 \rightarrow 4 \rightarrow 5\) \(2 \rightarrow 4 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \ri... | Input: 55 21 2 3 4 57 21 3 1 3 3 1 311 41 1 1 1 1 1 1 1 1 1 15 21 1 2 2 25 11 2 3 4 5 | Output: 1 4 165 3 1 | Hard | 6 | 830 | 495 | 145 | 18 |
457 | E | 457E | E. Flow Optimality | 3,000 | constructive algorithms; flows; math | There is a computer network consisting of n nodes numbered 1 through n. There are links in the network that connect pairs of nodes. A pair of nodes may have multiple links between them, but no node has a link to itself.Each link supports unlimited bandwidth (in either direction), however a link may only transmit in a s... | Input will begin with two integers n and m (2 β€ n β€ 200000; 0 β€ m β€ 200000), the number of nodes and number of known links in the network, respectively. Following this are m lines with four integers each: f, t, w, b (1 β€ f β€ n; 1 β€ t β€ n; f β t; 1 β€ w β€ 100; 0 β€ b β€ 100). This indicates there is a link between nodes f ... | If the intern's solution is definitely not optimal, print ""BAD x"", where x is the first link in the input that violates the optimality of the solution. If the intern's solution may be optimal, print the efficiency of the solution if it can be determined rounded to the nearest integer, otherwise print ""UNKNOWN"". | Although the known weights and bandwidths happen to always be integers, the weights and bandwidths of the remaining links are not restricted to integers. | Input: 4 51 2 1 21 3 4 12 3 2 12 4 4 13 4 1 2 | Output: 6 | Master | 3 | 1,782 | 408 | 316 | 4 |
1,063 | B | 1063B | B. Labyrinth | 1,800 | graphs; shortest paths | You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not o... | The first line contains two integers n, m (1 β€ n, m β€ 2000) β the number of rows and the number columns in the labyrinth respectively.The second line contains two integers r, c (1 β€ r β€ n, 1 β€ c β€ m) β index of the row and index of the column that define the starting cell.The third line contains two integers x, y (0 β€ ... | Print exactly one integer β the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself. | Cells, reachable in the corresponding example, are marked with '+'.First example: +++..+***.+++***+++. Second example: .++..+*..++..++. | Input: 4 53 21 2......***....***.... | Output: 10 | Medium | 2 | 850 | 772 | 141 | 10 |
75 | A | 75A | A. Life Without Zeros | 1,000 | implementation | Can you imagine our life if we removed all zeros from it? For sure we will have many problems.In this problem we will have a simple example if we removed all zeros from our life, it's the addition operation. Let's assume you are given this equation a + b = c, where a and b are positive integers, and c is the sum of a a... | The input will consist of two lines, the first line will contain the integer a, and the second line will contain the integer b which are in the equation as described above (1 β€ a, b β€ 109). There won't be any leading zeros in both. The value of c should be calculated as c = a + b. | The output will be just one line, you should print ""YES"" if the equation will remain correct after removing all zeros, and print ""NO"" otherwise. | Input: 101102 | Output: YES | Beginner | 1 | 682 | 281 | 148 | 0 | |
1,662 | D | 1662D | D. Evolution of Weasels | 0 | greedy; implementation; strings | A wild basilisk just appeared at your doorstep. You are not entirely sure what a basilisk is and you wonder whether it evolved from your favorite animal, the weasel. How can you find out whether basilisks evolved from weasels? Certainly, a good first step is to sequence both of their DNAs. Then you can try to check whe... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 100\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a string \(u\) (\(1\le |u|\le 200\)) β the DNA of the weasel.The second line of each test case contai... | For each test case, print YES if there is a sequence of mutations to get from \(u\) to \(v\) and NO otherwise. | Input: 8ABBCCAAABBBBCCCCAAABABBCBCABCCBA | Output: NO NO NO YES YES YES YES NO | Beginner | 3 | 1,119 | 551 | 110 | 16 | |
1,916 | E | 1916E | E. Happy Life in University | 2,300 | data structures; dfs and similar; greedy; trees | Egor and his friend Arseniy are finishing school this year and will soon enter university. And since they are very responsible guys, they have started preparing for admission already.First of all, they decided to take care of where they will live for the long four years of study, and after visiting the university's web... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β 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 3 \cdot 10^{5}\)).The second line of each test case c... | For each test case, output the maximum value of \(f(u, v)\) for all pairs of recreations \((u, v)\). | Consider the fourth test case. The tree has the following structure: All recreations are colored. The same colors mean that the activities in the recreations match. Consider the pair of vertices \((11, 12)\), \(lca(11, 12) = 1\). Write down all activities on the path from \(11\) to \(1\) β \([11, 5, 1, 11]\), among the... | Input: 4211 271 1 2 2 3 36 5 2 3 6 5 6131 1 1 2 2 2 3 3 4 5 6 62 2 2 1 4 9 7 2 5 2 1 11 2121 1 1 2 2 3 4 4 7 7 611 2 1 11 12 8 5 8 8 5 11 7 | Output: 2 9 9 12 | Expert | 4 | 1,174 | 710 | 100 | 19 |
354 | A | 354A | A. Vasya and Robot | 1,500 | brute force; greedy; math | Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.Vasya needs to collect all these items, however he won't do it by himself. He uses... | The first line contains five integers n, l, r, Ql, Qr (1 β€ n β€ 105; 1 β€ l, r β€ 100; 1 β€ Ql, Qr β€ 104).The second line contains n integers w1, w2, ..., wn (1 β€ wi β€ 100). | In the single line print a single number β the answer to the problem. | Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4Β·42 + 4Β·99 + 4Β·3 = 576 energy units.The second sample. The optimal solution is to take one item from the right, then one item from the left and two... | Input: 3 4 4 19 142 3 99 | Output: 576 | Medium | 3 | 1,155 | 169 | 69 | 3 |
853 | D | 853D | D. Michael and Charging Stations | 2,400 | binary search; dp; greedy | Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs.Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to the first job, and 2000 burles if he... | The first line of input contains a single integer n (1 β€ n β€ 300 000), the number of days Michael has planned.Next line contains n integers a1, a2, ..., an (ai = 1000 or ai = 2000) with ai denoting the charging cost at the day i. | Output the minimum amount of burles Michael has to spend. | In the first sample case the most optimal way for Michael is to pay for the first two days spending 3000 burles and get 300 bonus burles as return. After that he is able to pay only 700 burles for the third days, covering the rest of the price with bonus burles.In the second sample case the most optimal way for Michael... | Input: 31000 2000 1000 | Output: 3700 | Expert | 3 | 1,384 | 229 | 57 | 8 |
1,532 | B | 1532B | B. Frog Jumping | 0 | *special; math | A frog is currently at the point \(0\) on a coordinate axis \(Ox\). It jumps by the following algorithm: the first jump is \(a\) units to the right, the second jump is \(b\) units to the left, the third jump is \(a\) units to the right, the fourth jump is \(b\) units to the left, and so on.Formally: if the frog has jum... | The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of queries.Each of the next \(t\) lines contain queries (one query per line).The query is described as three space-separated integers \(a, b, k\) (\(1 \le a, b, k \le 10^9\)) β the lengths of two types of jumps and the number of ... | Print \(t\) integers. The \(i\)-th integer should be the answer for the \(i\)-th query. | In the first query frog jumps \(5\) to the right, \(2\) to the left and \(5\) to the right so the answer is \(5 - 2 + 5 = 8\).In the second query frog jumps \(100\) to the right, \(1\) to the left, \(100\) to the right and \(1\) to the left so the answer is \(100 - 1 + 100 - 1 = 198\).In the third query the answer is \... | Input: 6 5 2 3 100 1 4 1 10 5 1000000000 1 6 1 1 1000000000 1 1 999999999 | Output: 8 198 -17 2999999997 0 1 | Beginner | 2 | 688 | 340 | 87 | 15 |
1,886 | E | 1886E | E. I Wanna be the Team Leader | 2,400 | bitmasks; constructive algorithms; dp; greedy; math; sortings; two pointers | Monocarp is a team leader in a massive IT company.There are \(m\) projects his team of programmers has to complete, numbered from \(1\) to \(m\). The \(i\)-th project has a difficulty level \(b_i\).There are \(n\) programmers in the team, numbered from \(1\) to \(n\). The \(j\)-th programmer has a stress tolerance leve... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 20\)) β the number of programmers and the number of projects.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the stress tolerance level of each programmer.The third line contains... | If there is no valid assignment, print ""NO"".Otherwise, in the first line, print ""YES"". In the \(i\)-th of the next \(m\) lines, print the list of the programmers assigned to the \(i\)-th project: first, the number of programmers, then their indices in an arbitrary order.If there are multiple answers, print any of t... | Input: 5 3 4 6 100 5 1 50 1 12 | Output: YES 1 3 1 5 3 2 4 1 | Expert | 7 | 787 | 425 | 324 | 18 | |
725 | F | 725F | F. Family Photos | 2,900 | games; greedy | Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.There are n stacks of photos. Each stack conta... | The first line of input contains a single integer n (1 β€ n β€ 100 000) β the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, whi... | Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally. | Input: 212 3 4 71 15 9 1 | Output: 1 | Master | 2 | 1,137 | 372 | 102 | 7 | |
1,668 | B | 1668B | B. Social Distance | 900 | greedy; math; sortings | \(m\) chairs are arranged in a circle sequentially. The chairs are numbered from \(0\) to \(m-1\). \(n\) people want to sit in these chairs. The \(i\)-th of them wants at least \(a[i]\) empty chairs both on his right and left side. More formally, if the \(i\)-th person sits in the \(j\)-th chair, then no one else shoul... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 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 \(m\) (\(2 \leq n \leq 10^5\), \(1 \leq m \leq 10^9\)) β the n... | For each test case print ""YES"" (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and ""NO"" (without quotes) otherwise.You may print every letter in any case you want (so, for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will all be recognized as positive answers). | Test case \(1\): \(n>m\), so they can not sit down.Test case \(2\): the first person can sit \(2\)-nd and the second person can sit in the \(0\)-th chair. Both of them want at least \(1\) empty chair on both sides, chairs \(1\) and \(3\) are free, so this is a good solution.Test case \(3\): if the second person sits do... | Input: 6 3 2 1 1 1 2 4 1 1 2 5 2 1 3 8 1 2 1 4 12 1 2 1 3 4 19 1 2 1 3 | Output: NO YES NO YES NO YES | Beginner | 3 | 528 | 616 | 320 | 16 |
409 | C | 409C | C. Magnum Opus | 1,700 | *special | Salve, mi amice.Et tu quidem de lapis philosophorum. Barba non facit philosophum. Labor omnia vincit. Non potest creatio ex nihilo. Necesse est partibus.Rp: I Aqua Fortis I Aqua Regia II Amalgama VII Minium IV VitriolMisce in vitro et Γ¦stus, et nil admirari. Festina lente, et nulla tenaci invia est via.Fac et spera,Val... | The first line of input contains several space-separated integers ai (0 β€ ai β€ 100). | Print a single integer. | Input: 2 4 6 8 10 | Output: 1 | Medium | 1 | 336 | 84 | 23 | 4 | |
650 | B | 650B | B. Image Preview | 1,900 | binary search; brute force; dp; two pointers | Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a sec... | The first line of the input contains 4 integers n, a, b, T (1 β€ n β€ 5Β·105, 1 β€ a, b β€ 1000, 1 β€ T β€ 109) β the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.Second line of the input contains a string of length n containing symb... | Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. | In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.Note that in the last sample test the time is not enough even to watch the first photo,... | Input: 4 2 3 10wwhw | Output: 2 | Hard | 4 | 1,085 | 549 | 100 | 6 |
1,301 | F | 1301F | F. Super Jaber | 2,600 | dfs and similar; graphs; implementation; shortest paths | Jaber is a superhero in a large country that can be described as a grid with \(n\) rows and \(m\) columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between \(1\) and \(k\). In one second he can go from the current city to any of the cities adjacent b... | The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n, m \leq 1000\), \(1 \leq k \leq min(40 , n \cdot m)\)) β the number of rows, columns and colors.Each of the next \(n\) lines contains \(m\) integers. In the \(i\)-th line, the \(j\)-th integer is \(a_{ij}\) (\(1 \leq a_{ij} \leq k\)), which is th... | For every mission print the minimum possible time to reach city at the cell \((r_2, c_2)\) starting from city at the cell \((r_1, c_1)\). | In the first example: mission \(1\): Jaber should go from the cell \((1,1)\) to the cell \((3,3)\) because they have the same colors, then from the cell \((3,3)\) to the cell \((3,4)\) because they are adjacent by side (two moves in total); mission \(2\): Jaber already starts in the finishing cell. In the second exampl... | Input: 3 4 5 1 2 1 3 4 4 5 5 1 2 1 3 2 1 1 3 4 2 2 2 2 | Output: 2 0 | Expert | 4 | 701 | 827 | 137 | 13 |
1,156 | D | 1156D | D. 0-1-Tree | 2,200 | dfs and similar; divide and conquer; dp; dsu; trees | You are given a tree (an undirected connected acyclic graph) consisting of \(n\) vertices and \(n - 1\) edges. A number is written on each edge, each number is either \(0\) (let's call such edges \(0\)-edges) or \(1\) (those are \(1\)-edges).Let's call an ordered pair of vertices \((x, y)\) (\(x \ne y\)) valid if, whil... | The first line contains one integer \(n\) (\(2 \le n \le 200000\)) β the number of vertices in the tree.Then \(n - 1\) lines follow, each denoting an edge of the tree. Each edge is represented by three integers \(x_i\), \(y_i\) and \(c_i\) (\(1 \le x_i, y_i \le n\), \(0 \le c_i \le 1\), \(x_i \ne y_i\)) β the vertices ... | Print one integer β the number of valid pairs of vertices. | The picture corresponding to the first example: | Input: 7 2 1 1 3 2 0 4 2 1 5 2 0 6 7 1 7 2 1 | Output: 34 | Hard | 5 | 501 | 436 | 58 | 11 |
1,017 | A | 1017A | A. The Rank | 800 | implementation | John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.There are \(n\) students, each of them has a unique id (from \(1\) to \(n\)). Thomas's id is \... | The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the number of students.Each of the next \(n\) lines contains four integers \(a_i\), \(b_i\), \(c_i\), and \(d_i\) (\(0\leq a_i, b_i, c_i, d_i\leq 100\)) β the grades of the \(i\)-th student on English, German, Math, and History. The id of the \(i\)... | Print the rank of Thomas Smith. Thomas's id is \(1\). | In the first sample, the students got total scores: \(398\), \(400\), \(398\), \(379\), and \(357\). Among the \(5\) students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is \(2\).In the second sample, the students got total scores: \(369\), \(240\), \(310\), \(3... | Input: 5100 98 100 100100 100 100 100100 100 99 9990 99 90 100100 98 60 99 | Output: 2 | Beginner | 1 | 769 | 350 | 53 | 10 |
1,152 | F2 | 1152F2 | F2. Neko Rules the Catniverse (Large Version) | 3,000 | bitmasks; dp; matrices | This problem is same as the previous one, but has larger constraints.Aki is playing a new video game. In the video game, he will control Neko, the giant cat, to fly between planets in the Catniverse.There are \(n\) planets in the Catniverse, numbered from \(1\) to \(n\). At the beginning of the game, Aki chooses the pl... | The only line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le k \le \min(n, 12)\), \(1 \le m \le 4\)) β the number of planets in the Catniverse, the number of planets Neko needs to visit and the said constant \(m\). | Print exactly one integer β the number of different ways Neko can visit exactly \(k\) planets. Since the answer can be quite large, print it modulo \(10^9 + 7\). | In the first example, there are \(4\) ways Neko can visit all the planets: \(1 \rightarrow 2 \rightarrow 3\) \(2 \rightarrow 3 \rightarrow 1\) \(3 \rightarrow 1 \rightarrow 2\) \(3 \rightarrow 2 \rightarrow 1\) In the second example, there are \(9\) ways Neko can visit exactly \(2\) planets: \(1 \rightarrow 2\) \(2 \ri... | Input: 3 3 1 | Output: 4 | Master | 3 | 994 | 245 | 161 | 11 |
2,060 | F | 2060F | F. Multiplicative Arrays | 2,200 | combinatorics; dp; number theory | You're given integers \(k\) and \(n\). For each integer \(x\) from \(1\) to \(k\), count the number of integer arrays \(a\) such that all of the following are satisfied: \(1 \leq |a| \leq n\) where \(|a|\) represents the length of \(a\). \(1 \leq a_i \leq k\) for all \(1 \leq i \leq |a|\). \(a_1 \times a_2 \times \dots... | The first line contains an integer \(t\) (\(1 \leq t\leq 10^3\)) β the number of independent test cases.The only line of each test case contains two integers \(k\) and \(n\) (\(1 \leq k \leq 10^5, 1\leq n \leq 9\cdot 10^8\)).It is guaranteed that the sum of \(k\) over all test cases does not exceed \(10^5\). | For each test case, output \(k\) space-separated integers on a new line: the number of arrays for \(x=1,2,\ldots,k\), modulo \(998\,244\,353\). | In the first test case, there are \(2\) arrays \(a\) with \(|a|\leq 2\) and the product of elements equal to \(1\): \([1]\) \([1,1]\) There are \(3\) arrays \(a\) with \(|a|\leq 2\) and the product of elements equal to \(2\): \([2]\) \([1,2]\) \([2,1]\) | Input: 32 24 310 6969420 | Output: 2 3 3 6 6 10 6969420 124188773 124188773 729965558 124188773 337497990 124188773 50981194 729965558 337497990 | Hard | 3 | 595 | 309 | 143 | 20 |
15 | B | 15B | B. Laser | 1,800 | math | Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n Γ m cells and a robotic arm. Each cell of the field is a 1 Γ 1 square. The robotic arm has two lasers po... | The first line contains one integer number t (1 β€ t β€ 10000) β the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 β€ n, m β€ 109, 1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m). Cells (x1, y1) and (x2, y2) are distinct. | Each of the t lines of the output should contain the answer to the corresponding input test set. | Input: 24 4 1 1 3 34 3 1 1 2 2 | Output: 82 | Medium | 1 | 1,491 | 311 | 96 | 0 | |
1,204 | B | 1204B | B. Mislove Has Lost an Array | 900 | greedy; math | Mislove had an array \(a_1\), \(a_2\), \(\cdots\), \(a_n\) of \(n\) positive integers, but he has lost it. He only remembers the following facts about it: The number of different numbers in the array is not less than \(l\) and is not greater than \(r\); For each array's element \(a_i\) either \(a_i = 1\) or \(a_i\) is ... | The only input line contains three integers \(n\), \(l\) and \(r\) (\(1 \leq n \leq 1\,000\), \(1 \leq l \leq r \leq \min(n, 20)\)) β an array's size, the minimal number and the maximal number of distinct elements in an array. | Output two numbers β the minimal and the maximal possible sums of all elements in an array. | In the first example, an array could be the one of the following: \([1,1,1,2]\), \([1,1,2,2]\) or \([1,2,2,2]\). In the first case the minimal sum is reached and in the last case the maximal sum is reached.In the second example, the minimal sum is reached at the array \([1,1,1,1,1]\), and the maximal one is reached at ... | Input: 4 2 2 | Output: 5 7 | Beginner | 2 | 896 | 226 | 91 | 12 |
1,010 | C | 1010C | C. Border | 1,800 | number theory | Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.There are \... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 100\,000\), \(2 \le k \le 100\,000\)) β the number of denominations of banknotes and the base of the number system on Mars.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β denominations of banknotes on Mars.A... | On the first line output the number of values \(d\) for which Natasha can make the Martians happy.In the second line, output all these values in increasing order.Print all numbers in decimal notation. | Consider the first test case. It uses the octal number system.If you take one banknote with the value of \(12\), you will get \(14_8\) in octal system. The last digit is \(4_8\).If you take one banknote with the value of \(12\) and one banknote with the value of \(20\), the total value will be \(32\). In the octal syst... | Input: 2 812 20 | Output: 20 4 | Medium | 1 | 991 | 361 | 200 | 10 |
1,312 | G | 1312G | G. Autocompletion | 2,600 | data structures; dfs and similar; dp | You are given a set of strings \(S\). Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the follow... | The first line contains one integer \(n\) (\(1 \le n \le 10^6\)).Then \(n\) lines follow, the \(i\)-th line contains one integer \(p_i\) (\(0 \le p_i < i\)) and one lowercase Latin character \(c_i\). These lines form some set of strings such that \(S\) is its subset as follows: there are \(n + 1\) strings, numbered fro... | Print \(k\) integers, the \(i\)-th of them should be equal to the minimum number of seconds required to type the string \(s_{a_i}\). | In the first example, \(S\) consists of the following strings: ieh, iqgp, i, iqge, ier. | Input: 10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 | Output: 2 4 1 3 3 | Expert | 3 | 1,144 | 973 | 132 | 13 |
734 | F | 734F | F. Anton and School | 2,500 | bitmasks; constructive algorithms; implementation; math | Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n, find array a, such that:where a and b means bitwise AND, while a or b means bitwise OR.Usually Anton is good in arraystud... | The first line of the input contains a single integers n (1 β€ n β€ 200 000) β the size of arrays b and c.The second line contains n integers bi (0 β€ bi β€ 109) β elements of the array b.Third line contains n integers ci (0 β€ ci β€ 109) β elements of the array c. | If there is no solution, print - 1.Otherwise, the only line of the output should contain n non-negative integers ai β elements of the array a. If there are multiple possible solutions, you may print any of them. | Input: 46 8 4 416 22 10 10 | Output: 3 5 1 1 | Expert | 4 | 382 | 259 | 211 | 7 | |
434 | D | 434D | D. Nanami's Power Plant | 2,900 | flows | Nanami likes playing games, and is also really good at it. This day she was playing a new game which involved operating a power plant. Nanami's job is to control the generators in the plant and produce maximum output.There are n generators in the plant. Each generator should be set to a generating level. Generating lev... | The first line contains two integers n and m (1 β€ n β€ 50; 0 β€ m β€ 100) β the number of generators and the number of restrictions.Then follow n lines, each line contains three integers ai, bi, and ci (|ai| β€ 10; |bi|, |ci| β€ 1000) β the coefficients of the function fi(x). That is, fi(x) = aix2 + bix + ci.Then follow ano... | Print a single line containing a single integer β the maximum output of all the generators. It is guaranteed that there exists at least one valid configuration. | In the first sample, f1(x) = x, f2(x) = x + 1, and f3(x) = x + 2, so we are to maximize the sum of the generating levels. The restrictions are x1 β€ x2, x2 β€ x3, and x3 β€ x1, which gives us x1 = x2 = x3. The optimal configuration is x1 = x2 = x3 = 2, which produces an output of 9.In the second sample, restrictions are e... | Input: 3 30 1 00 1 10 1 20 31 2-100 1001 2 02 3 03 1 0 | Output: 9 | Master | 1 | 1,123 | 575 | 160 | 4 |
924 | F | 924F | F. Minimal Subset Difference | 3,200 | dp | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should bel... | The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9). | For each query print a single number β the answer to the query. | If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.100 is k-beautiful if and only if k β₯ 1. | Input: 101 100 01 100 11 100 21 100 31 100 41 100 51 100 61 100 71 100 81 100 9 | Output: 92844587080889498100 | Master | 1 | 541 | 204 | 63 | 9 |
2,053 | I1 | 2053I1 | I1. Affectionate Arrays (Easy Version) | 2,800 | data structures; dp; greedy | You are the beginning of the letter, the development of a poem, and the end of a fairy tale.β ilem, Pinky PromiseThis is the easy version of the problem. The difference between the versions is that in this version, you need to compute the minimum length of the arrays. You can hack only if you solved all versions of thi... | Each test contains multiple test cases. The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) β 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 3\cdot 10^6\)) β the length of the array \(a_1, a_2, \ldo... | For each test case, output a single line containing an integer: the length \(m\) of a valid array \(b\). | In the first test case, \(a=[1, 2, 3, 4]\). The only array \(b\) which satisfies all the properties above is \([1, 2, 3, 4]\), so we should output \(4\).In the second test case, \(a=[2, -3, 2, 2]\). The possible arrays \(b\) are \([1, 2, -3, 2, -1, 2]\) and \([2, 1, -3, 2, -1, 2]\), so we should output \(6\). | Input: 441 2 3 442 -3 2 2102 -7 6 3 -1 4 2 -5 8 -4204 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1 | Output: 4 6 14 25 | Master | 3 | 2,339 | 620 | 104 | 20 |
1,619 | D | 1619D | D. New Year's Problem | 1,800 | binary search; greedy; sortings | Vlad has \(n\) friends, for each of whom he wants to buy one gift for the New Year.There are \(m\) shops in the city, in each of which he can buy a gift for any of his friends. If the \(j\)-th friend (\(1 \le j \le n\)) receives a gift bought in the shop with the number \(i\) (\(1 \le i \le m\)), then the friend receiv... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input.An empty line is written before each test case. Then there is a line containing integers \(m\) and \(n\) (\(2 \le n\), \(2 \le n \cdot m \le 10^5\)) separated by a space β the number of shops and the num... | Print \(t\) lines, each line must contain the answer to the corresponding test case β the maximum possible value of \(\alpha\), where \(\alpha\) is the minimum of the joys from a gift for all of Vlad's friends. | Input: 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 | Output: 3 2 4 8 2 | Medium | 3 | 1,552 | 734 | 210 | 16 | |
474 | C | 474C | C. Captain Marmot | 2,000 | brute force; geometry | Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.Initially, each mole i (1 β€ i β€ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compa... | The first line contains one integer n (1 β€ n β€ 100), the number of regiments.The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 β€ xi, yi, ai, bi β€ 104). | Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print ""-1"" (without quotes). | In the first regiment we can move once the second or the third mole.We can't make the second regiment compact.In the third regiment, from the last 3 moles we can move once one and twice another one.In the fourth regiment, we can move twice the first mole and once the third mole. | Input: 41 1 0 0-1 1 0 0-1 1 0 01 -1 0 01 1 0 0-2 1 0 0-1 1 0 01 -1 0 01 1 0 0-1 1 0 0-1 1 0 0-1 1 0 02 2 0 1-1 0 0 -23 0 0 -2-1 1 -2 0 | Output: 1-133 | Hard | 2 | 760 | 161 | 215 | 4 |
1,296 | D | 1296D | D. Fight with Monsters | 1,500 | greedy; sortings | There are \(n\) monsters standing in a row numbered from \(1\) to \(n\). The \(i\)-th monster has \(h_i\) health points (hp). You have your attack power equal to \(a\) hp and your opponent has his attack power equal to \(b\) hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the ... | The first line of the input contains four integers \(n, a, b\) and \(k\) (\(1 \le n \le 2 \cdot 10^5, 1 \le a, b, k \le 10^9\)) β the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.The second line of the input contains \(n\) integers \(h_1, h_... | Print one integer β the maximum number of points you can gain if you use the secret technique optimally. | Input: 6 2 3 3 7 10 50 12 1 8 | Output: 5 | Medium | 2 | 1,298 | 421 | 104 | 12 | |
201 | E | 201E | E. Thoroughly Bureaucratic Organization | 2,600 | binary search; combinatorics | Once n people simultaneously signed in to the reception at the recently opened, but already thoroughly bureaucratic organization (abbreviated TBO). As the organization is thoroughly bureaucratic, it can accept and cater for exactly one person per day. As a consequence, each of n people made an appointment on one of the... | The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Each of the following t lines contains two integers n and m (1 β€ n, m β€ 109) β the number of people who have got an appointment at TBO and the number of empty lines in the request form, correspondingly. | Print t lines, each containing an answer for the corresponding test case (in the order they are given in the input) β the minimum number of requests to submit to TBO. | In the first sample, you need to submit three requests to TBO with three different names. When you learn the appointment dates of three people out of four, you can find out the fourth person's date by elimination, so you do not need a fourth request.In the second sample you need only two requests. Let's number the pers... | Input: 54 14 27 31 142 7 | Output: 323011 | Expert | 2 | 1,441 | 288 | 166 | 2 |
1,145 | B | 1145B | B. Kanban Numbers | 0 | *special; brute force | The input contains a single integer \(a\) (\(1 \le a \le 99\)). | Output ""YES"" or ""NO"". | Input: 5 | Output: YES | Beginner | 2 | 0 | 63 | 25 | 11 | ||
1,835 | E | 1835E | E. Old Mobile | 3,500 | combinatorics; dp; probabilities | During the latest mission of the starship U.S.S. Coder, Captain Jan Bitovsky was accidentally teleported to the surface of an unknown planet. Trying to find his way back, Jan found an artifact from planet Earth's ancient civilization β a mobile device capable of interstellar calls created by Byterola. Unfortunately, th... | In the first line of input, there are two integer numbers \(n\) and \(m\) (\(1 \le n \le 10^6\), \(2 \le m \le 10^3\)) β the length of the number to U.S.S. Coder and the base of the numerical system.In the next and the last input line, there are \(n\) integers between \(0\) and \(m - 1\): the number to type in the base... | Output the expected number of button presses modulo \(1\,000\,000\,007\).Formally, let \(M = 1\,000\,000\,007\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} ... | In the first example, two digits (\(0\) and \(1\)) and a backspace button are available on the keyboard. Jan has no way of knowing which one is which, so he presses a random one. With probability \(\frac{1}{3}\), he presses \(0\) and manages to type the crew's number. With probability \(\frac{1}{3}\), he presses backsp... | Input: 1 2 0 | Output: 666666674 | Master | 3 | 1,224 | 344 | 433 | 18 |
1,004 | C | 1004C | C. Sonya and Robots | 1,400 | constructive algorithms; implementation | Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.Sonya has drawn \(n\) numbers in a row, \(a_i\) is located in the \(i\)-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sony... | The first line contains a single integer \(n\) (\(1\leq n\leq 10^5\)) β the number of numbers in a row.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\leq a_i\leq 10^5\)) β the numbers in a row. | Print one number β the number of possible pairs that Sonya can give to robots so that they will not meet. | In the first example, Sonya can give pairs (\(1\), \(1\)), (\(1\), \(3\)), (\(1\), \(4\)), (\(1\), \(5\)), (\(4\), \(1\)), (\(4\), \(3\)), (\(5\), \(1\)), (\(5\), \(3\)), and (\(5\), \(4\)).In the second example, Sonya can give pairs (\(1\), \(1\)), (\(1\), \(2\)), (\(1\), \(3\)), (\(2\), \(1\)), (\(2\), \(2\)), (\(2\)... | Input: 51 5 4 1 3 | Output: 9 | Easy | 2 | 2,136 | 217 | 105 | 10 |
605 | B | 605B | B. Lazy Student | 1,700 | constructive algorithms; data structures; graphs | Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph β something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following defin... | The first line of the input contains two integers n and m () β the number of vertices and the number of edges in the graph.Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 β€ aj β€ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second numbe... | If Vladislav has made a mistake and such graph doesn't exist, print - 1.Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 β€ uj, vj β€ n, uj β vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must b... | Input: 4 52 13 14 01 15 0 | Output: 2 41 43 43 13 2 | Medium | 3 | 1,006 | 543 | 498 | 6 | |
707 | A | 707A | A. Brain's Photos | 800 | implementation | Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).Brain took a lot ... | The first line of the input contains two integers n and m (1 β€ n, m β€ 100) β the number of photo pixel matrix rows and columns respectively.Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', '... | Print the ""#Black&White"" (without quotes), if the photo is black-and-white and ""#Color"" (without quotes), if it is colored, in the only line. | Input: 2 2C MY Y | Output: #Color | Beginner | 1 | 1,120 | 345 | 145 | 7 | |
2,110 | F | 2110F | F. Faculty | 2,400 | brute force; greedy; math; number theory | In 2077, after the world was enslaved by robots, the robots decided to implement an educational reform, and now the operation of taking the modulus is only taught in the faculty of ""Ancient World History"". Here is one of the entrance tasks for this faculty:We define the beauty of an array of positive integers \(b\) a... | 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 a single integer \(n\) (\(1 \leq n \leq 10^6\)) β the size of the array \(a\).The second line of each test case cont... | For each test case, output \(n\) integers β the beauties of all prefixes of the array \(a\). | The beauty of the array \(3\) is \(0\).The beauty of the array \(3, 1\) is \(f(3, 1) = 1\).The beauty of the array \(3, 1, 4\) is \(f(3, 4) = 4\).The beauty of the array \(3, 1, 4, 1\) is \(f(4, 3) = 4\).The beauty of the array \(3, 1, 4, 1, 5\) is \(f(4, 5) = 5\). | Input: 253 1 4 1 575 11 11 4 2 1 10 | Output: 0 1 4 4 5 0 6 6 7 7 7 11 | Expert | 4 | 682 | 479 | 92 | 21 |
1,884 | B | 1884B | B. Haunted House | 1,100 | binary search; greedy; math; two pointers | You are given a number in binary representation consisting of exactly \(n\) bits, possibly, with leading zeroes. For example, for \(n = 5\) the number \(6\) will be given as \(00110\), and for \(n = 4\) the number \(9\) will be given as \(1001\).Let's fix some integer \(i\) such that \(1 \le i \le n\). In one operation... | 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 one integer \(n\) (\(1 \le n \le 10^5\)) β the length of the binary representation of the number.The second line of e... | For each test case, for each \(1 \le i \le n\) output the smallest number of operations required to make the number divisible by \(2^i\), or output \(-1\) if it is impossible. | In the first test case, we cannot swap any elements, and the number \(1\) is not divisible by \(2\).In the second test case, the initial number is \(1\). It is not divisible by \(2\), but if we perform the operation, then we obtain the number with binary representation \(10\), which is equal to \(2\) in decimal represe... | Input: 61120130105101017000011112001011000110 | Output: -1 1 -1 0 1 -1 1 3 -1 -1 -1 3 6 9 12 -1 -1 -1 0 2 4 6 10 15 20 -1 -1 -1 -1 -1 | Easy | 4 | 625 | 534 | 175 | 18 |
1,328 | B | 1328B | B. K-th Beautiful String | 1,300 | binary search; brute force; combinatorics; implementation; math | For the given integer \(n\) (\(n > 2\)) let's write down all the strings of length \(n\) which contain \(n-2\) letters 'a' and two letters 'b' in lexicographical (alphabetical) order.Recall that the string \(s\) of length \(n\) is lexicographically less than string \(t\) of length \(n\), if there exists such \(i\) (\(1... | The input contains one or more test cases.The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test. Then \(t\) test cases follow.Each test case is written on the the separate line containing two integers \(n\) and \(k\) (\(3 \le n \le 10^5, 1 \le k \le \min(2\cdot10^9, \fr... | For each test case print the \(k\)-th string from the list of all described above strings of length \(n\). Strings in the list are sorted lexicographically (alphabetically). | Input: 7 5 1 5 2 5 8 5 10 3 1 3 2 20 100 | Output: aaabb aabab baaba bbaaa abb bab aaaaabaaaaabaaaaaaaa | Easy | 5 | 867 | 424 | 173 | 13 | |
540 | D | 540D | D. Bad Luck Island | 1,900 | dp; probabilities | The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors and p papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper... | The single line contains three integers r, s and p (1 β€ r, s, p β€ 100) β the original number of individuals in the species of rock, scissors and paper, respectively. | Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10 - 9. | Input: 2 2 2 | Output: 0.333333333333 0.333333333333 0.333333333333 | Hard | 2 | 510 | 165 | 267 | 5 | |
584 | B | 584B | B. Kolya and Tanya | 1,500 | combinatorics | Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circ... | A single line contains number n (1 β€ n β€ 105) β the number of the gnomes divided by three. | Print a single number β the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7. | 20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): | Input: 1 | Output: 20 | Medium | 1 | 837 | 90 | 120 | 5 |
710 | E | 710E | E. Generate a String | 2,000 | dfs and similar; dp | zscoder wants to generate an input file for some programming competition problem.His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.Initially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' fro... | The only line contains three integers n, x and y (1 β€ n β€ 107, 1 β€ x, y β€ 109) β the number of letters 'a' in the input file and the parameters from the problem statement. | Print the only integer t β the minimum amount of time needed to generate the input file. | Input: 8 1 1 | Output: 4 | Hard | 2 | 598 | 171 | 88 | 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.