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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
111 | B | 111B | B. Petya and Divisors | 1,900 | binary search; data structures; number theory | Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:You are given n queries in the form ""xi yi"". For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1. Help him. | The first line contains an integer n (1 β€ n β€ 105). Each of the following n lines contain two space-separated integers xi and yi (1 β€ xi β€ 105, 0 β€ yi β€ i - 1, where i is the query's ordinal number; the numeration starts with 1). If yi = 0 for the query, then the answer to the query will be the number of divisors of th... | For each query print the answer on a single line: the number of positive integers k such that | Let's write out the divisors that give answers for the first 5 queries:1) 1, 2, 4 2) 33) 54) 2, 65) 9, 18 | Input: 64 03 15 26 218 410000 3 | Output: 3112222 | Hard | 3 | 281 | 412 | 93 | 1 |
177 | F2 | 177F2 | F2. Script Generation | 2,300 | The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series.There are n single men and n single women among the main characters. An opinion poll showed that viewers like ... | The first input line contains integers n, k and t (1 β€ k β€ min(100, n2), 1 β€ t β€ 2Β·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 β€ h, w β€ n; 1 β€ r β€ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t does... | Print a single number β the value of the t-th acceptable variant. | The figure shows 7 acceptable sets of marriages that exist in the first sample. | Input: 2 4 31 1 11 2 22 1 32 2 7 | Output: 2 | Expert | 0 | 1,782 | 541 | 65 | 1 | |
1,398 | E | 1398E | E. Two Types of Spells | 2,200 | binary search; data structures; greedy; implementation; math; sortings | Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.There are two types of spells: fire spell of power \(x\) deals \(x\) damage to the monster, and lightning spell of power \(y\) deals \(y\) damage to the monster and doubles the damage of the next spell Polycarp casts. Each s... | The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of changes to the spell set.Each of the next \(n\) lines contains two integers \(tp\) and \(d\) (\(0 \le tp_i \le 1\); \(-10^9 \le d \le 10^9\); \(d_i \neq 0\)) β the description of the change. If \(tp_i\) if equal to \(0\), then Poly... | After each change, print the maximum damage Polycarp can deal with his current set of spells. | Input: 6 1 5 0 10 1 -5 0 5 1 11 0 -10 | Output: 5 25 10 15 36 21 | Hard | 6 | 1,334 | 741 | 93 | 13 | |
166 | C | 166C | C. Median | 1,500 | greedy; math; sortings | A median in an array with the length of n is an element which occupies position number after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) β the number 17.We define an expres... | The first input line contains two space-separated integers n and x (1 β€ n β€ 500, 1 β€ x β€ 105) β the initial array's length and the required median's value. The second line contains n space-separated numbers β the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily... | Print the only integer β the minimum number of elements Petya needs to add to the array so that its median equals x. | In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position , that is, 10.In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. | Input: 3 1010 20 30 | Output: 1 | Medium | 3 | 1,084 | 331 | 116 | 1 |
2,063 | A | 2063A | A. Minimal Coprime | 800 | math; number theory | Today, Little John used all his savings to buy a segment. He wants to build a house on this segment. A segment of positive integers \([l,r]\) is called coprime if \(l\) and \(r\) are coprime\(^{\text{β}}\).A coprime segment \([l,r]\) is called minimal coprime if it does not contain\(^{\text{β }}\) any coprime segment no... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The only line of each test case consists of two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^9\)). | For each test case, output the number of minimal coprime segments contained in \([l,r]\), on a separate line. | On the first test case, the given segment is \([1,2]\). The segments contained in \([1,2]\) are as follows. \([1,1]\): This segment is coprime, since the numbers \(1\) and \(1\) are coprime, and this segment does not contain any other segment inside. Thus, \([1,1]\) is minimal coprime. \([1,2]\): This segment is coprim... | Input: 61 21 1049 4969 4201 19982 44353 | Output: 1 9 0 351 1 34371 | Beginner | 2 | 942 | 263 | 109 | 20 |
1,620 | F | 1620F | F. Bipartite Array | 2,800 | dp; greedy | You are given a permutation \(p\) consisting of \(n\) integers \(1, 2, \dots, n\) (a permutation is an array where each element from \(1\) to \(n\) occurs exactly once).Let's call an array \(a\) bipartite if the following undirected graph is bipartite: the graph consists of \(n\) vertices; two vertices \(i\) and \(j\) ... | The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the size of the permutation.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\).The sum of \(n\) over all test c... | For each test case, print the answer in the following format. If such an array \(a\) does not exist, print ""NO"" in a single line. Otherwise, print ""YES"" in the first line and \(n\) integers β array \(a\) in the second line. | Input: 4 3 1 2 3 6 1 3 2 6 5 4 4 4 1 3 2 8 3 2 1 6 7 8 5 4 | Output: YES 1 2 3 NO YES -4 -1 -3 -2 YES -3 -2 1 6 7 -8 -5 -4 | Master | 2 | 578 | 349 | 227 | 16 | |
1,601 | F | 1601F | F. Two Sorts | 3,400 | binary search; dfs and similar; math; meet-in-the-middle | Integers from \(1\) to \(n\) (inclusive) were sorted lexicographically (considering integers as strings). As a result, array \(a_1, a_2, \dots, a_n\) was obtained.Calculate value of \((\sum_{i = 1}^n ((i - a_i) \mod 998244353)) \mod 10^9 + 7\).\(x \mod y\) here means the remainder after division \(x\) by \(y\). This re... | The first line contains the single integer \(n\) (\(1 \leq n \leq 10^{12}\)). | Print one integer β the required sum. | A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\). For examp... | Input: 3 | Output: 0 | Master | 4 | 432 | 77 | 37 | 16 |
1,166 | D | 1166D | D. Cute Sequences | 2,200 | binary search; brute force; greedy; math | Given a positive integer \(m\), we say that a sequence \(x_1, x_2, \dots, x_n\) of positive integers is \(m\)-cute if for every index \(i\) such that \(2 \le i \le n\) it holds that \(x_i = x_{i - 1} + x_{i - 2} + \dots + x_1 + r_i\) for some positive integer \(r_i\) satisfying \(1 \le r_i \le m\).You will be given \(q... | The first line contains an integer number \(q\) (\(1 \le q \le 10^3\)) β the number of queries.Each of the following \(q\) lines contains three integers \(a\), \(b\), and \(m\) (\(1 \le a, b, m \le 10^{14}\), \(a \leq b\)), describing a single query. | For each query, if no \(m\)-cute sequence whose first term is \(a\) and whose last term is \(b\) exists, print \(-1\).Otherwise print an integer \(k\) (\(1 \le k \leq 50\)), followed by \(k\) integers \(x_1, x_2, \dots, x_k\) (\(1 \le x_i \le 10^{14}\)). These integers must satisfy \(x_1 = a\), \(x_k = b\), and that th... | Consider the sample. In the first query, the sequence \(5, 6, 13, 26\) is valid since \(6 = 5 + \bf{\color{blue} 1}\), \(13 = 6 + 5 + {\bf\color{blue} 2}\) and \(26 = 13 + 6 + 5 + {\bf\color{blue} 2}\) have the bold values all between \(1\) and \(2\), so the sequence is \(2\)-cute. Other valid sequences, such as \(5, 7... | Input: 2 5 26 2 3 9 1 | Output: 4 5 6 13 26 -1 | Hard | 4 | 605 | 250 | 589 | 11 |
253 | D | 253D | D. Table with Letters - 2 | 2,000 | brute force; two pointers | Vasya has recently started to learn English. Now he needs to remember how to write English letters. He isn't sure about some of them, so he decided to train a little.He found a sheet of squared paper and began writing arbitrary English letters there. In the end Vasya wrote n lines containing m characters each. Thus, he... | The first line contains three integers n, m, k (2 β€ n, m β€ 400; 0 β€ k β€ nΒ·m).Next n lines contain m characters each β the given table. Each character of the table is a lowercase English letter. | Print a single integer β the number of required subtables. | There are two suitable subtables in the first sample: the first one's upper left corner is cell (2, 2) and lower right corner is cell (3, 3), the second one's upper left corner is cell (2, 1) and lower right corner is cell (3, 4). | Input: 3 4 4aabbbaabbaab | Output: 2 | Hard | 2 | 1,336 | 193 | 58 | 2 |
2,006 | F | 2006F | F. Dora's Paint | 3,500 | brute force; combinatorics; constructive algorithms; graphs; implementation | Sadly, Dora poured the paint when painting the class mural. Dora considers the mural as the matrix \(b\) of size \(n \times n\). Initially, \(b_{i,j} = 0\) for all \(1 \le i, j \le n\).Dora has only two brushes which have two different colors. In one operation, she can paint the matrix with one of two brushes: The firs... | 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 two integers \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq m \leq \min(10^6, n^2)... | For each test case, output a single integer β the expected beauty of the real matrix \(b\), modulo \(998\,244\,353\). | In the first test case, the matrix \(a = \left[\begin{matrix}1&1\\2&2\end{matrix}\right]\). Let's consider changing the element \((1,1)\) to calculate the answer.It can be proved that the minimum steps to paint the initial matrix into \(\left[\begin{matrix}2&1\\2&2\end{matrix}\right]\) is \(3\). We can first paint the ... | Input: 72 21 11 22 11 13 21 13 36 05 101 11 21 32 12 35 15 25 35 45 53 51 11 32 23 13 34 31 12 32 4 | Output: 1 499122178 665496236 120 79859554 776412275 1 | Master | 5 | 1,661 | 777 | 117 | 20 |
1,775 | B | 1775B | B. Gardener and the Array | 1,300 | bitmasks; constructive algorithms | The gardener Kazimir Kazimirovich has an array of \(n\) integers \(c_1, c_2, \dots, c_n\).He wants to check if there are two different subsequences \(a\) and \(b\) of the original array, for which \(f(a) = f(b)\), where \(f(x)\) is the bitwise OR of all of the numbers in the sequence \(x\).A sequence \(q\) is a subsequ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). 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 size of the array \(c\).The description of the array \(c\) in this pro... | For each set of input, print ""Yes"" if there exist two different subsequences for which \(f(a) = f(b)\), and ""No"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | It can be proven that in the first test case there are no two different subsequences \(a\) and \(b\) for which \(f(a) = f(b)\).In the second test case, one of the possible answers are following subsequences: the subsequence \(a\) formed by the element at position \(1\), and the subsequence \(b\) formed by the elements ... | Input: 532 1 52 2 42 2 322 1 21 243 1 2 42 2 44 1 2 5 62 2 553 3 1 23 2 5 35 7 2 3 1 45 1 2 6 3 53 2 6 321 11 2 | Output: No Yes Yes Yes No | Easy | 2 | 632 | 870 | 288 | 17 |
1,733 | A | 1733A | A. Consecutive Sum | 800 | greedy; sortings | You are given an array \(a\) with \(n\) integers. You can perform the following operation at most \(k\) times: Choose two indices \(i\) and \(j\), in which \(i \,\bmod\, k = j \,\bmod\, k\) (\(1 \le i < j \le n\)). Swap \(a_i\) and \(a_j\). After performing all operations, you have to select \(k\) consecutive elements,... | The first line contains one integer \(t\) (\(1 \le t \le 600\)) β the number of test cases.Each test case consists of two lines. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)) β the length of the array and the number in the statement above.The second line of each test... | For each test case, print the maximum score you can get, one per line. | In the first test case, we can get a score of \(11\) if we select \(a_1, a_2\) without performing any operations.In the third test case, we can get a score of \(15\) if we first swap \(a_1\) with \(a_4\) and then select \(a_3, a_4, a_5\). | Input: 53 25 6 01 175 37 0 4 0 44 22 7 3 43 31000000000 1000000000 999999997 | Output: 11 7 15 10 2999999997 | Beginner | 2 | 480 | 420 | 70 | 17 |
1,815 | D | 1815D | D. XOR Counting | 2,600 | bitmasks; combinatorics; dp; math | Given two positive integers \(n\) and \(m\). Find the sum of all possible values of \(a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m\), where \(a_1,a_2,\ldots,a_m\) are non-negative integers such that \(a_1+a_2+\ldots+a_m=n\).Note that all possible values \(a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m\) should be counted... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first and only line of each test case contains two integers \(n\) and \(m\) (\(0\le n\le 10^{18}, 1\le m\le 10^5\)) β the sum and the num... | For each test case, output the sum of all possible values of \(a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m\) among all non-negative integers \(a_1,a_2,\ldots,a_m\) with \(a_1+a_2+\ldots+a_m=n\). As the answer may be too large, output your answer modulo \(998244353\). | For the first test case, we must have \(a_1=69\), so it's the only possible value of \(a_1\), therefore our answer is \(69\).For the second test case, \((a_1,a_2)\) can be \((0,5), (1,4), (2,3), (3,2), (4,1)\) or \((5,0)\), in which \(a_1\bigoplus a_2\) are \(5,5,1,1,5,5\) respectively. So \(a_1\bigoplus a_2\) can be \... | Input: 769 15 20 10420 6912 2673 341000000000000000000 10 | Output: 69 6 0 44310 42 1369 216734648 | Expert | 4 | 471 | 361 | 268 | 18 |
1,616 | C | 1616C | C. Representative Edges | 1,500 | brute force; geometry; implementation; math | An array \(a_1, a_2, \ldots, a_n\) is good if and only if for every subsegment \(1 \leq l \leq r \leq n\), the following holds: \(a_l + a_{l + 1} + \ldots + a_r = \frac{1}{2}(a_l + a_r) \cdot (r - l + 1)\). You are given an array of integers \(a_1, a_2, \ldots, a_n\). In one operation, you can replace any one element o... | The first line of input contains one integer \(t\) (\(1 \leq t \leq 100\)): the number of test cases.Each of the next \(t\) lines contains the description of a test case.In the first line you are given one integer \(n\) (\(1 \leq n \leq 70\)): the number of integers in the array.The second line contains \(n\) integers ... | For each test case, print one integer: the minimum number of elements that you need to replace to make the given array good. | In the first test case, the array is good already.In the second test case, one of the possible good arrays is \([1, 1, \underline{1}, \underline{1}]\) (replaced elements are underlined).In the third test case, the array is good already.In the fourth test case, one of the possible good arrays is \([\underline{-2.5}, -2,... | Input: 5 4 1 2 3 4 4 1 1 2 2 2 0 -1 6 3 -2 4 -1 -4 0 1 -100 | Output: 0 2 0 3 0 | Medium | 4 | 426 | 394 | 124 | 16 |
1,669 | G | 1669G | G. Fall Down | 1,200 | dfs and similar; implementation | There is a grid with \(n\) rows and \(m\) columns, and three types of cells: An empty cell, denoted with '.'. A stone, denoted with '*'. An obstacle, denoted with the lowercase Latin letter 'o'. All stones fall down until they meet the floor (the bottom row), an obstacle, or other stone which is already immovable. (In ... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β 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\) (\(1 \leq n, m \leq 50\)) β the number of rows and the number of column... | For each test case, output a grid with \(n\) rows and \(m\) columns, showing the result of the process.You don't need to output a new line after each test, it is in the samples just for clarity. | Input: 3 6 10 .*.*....*. .*.......* ...o....o. .*.*....*. .......... .o......o* 2 9 ...***ooo .*o.*o.*o 5 5 ***** *.... ***** ....* ***** | Output: .......... ...*....*. .*.o....o. .*........ .*......** .o.*....o* ....**ooo .*o**o.*o ..... *...* ***** ***** ***** | Easy | 2 | 450 | 514 | 194 | 16 | |
1,380 | F | 1380F | F. Strange Addition | 2,600 | data structures; dp; matrices | Let \(a\) and \(b\) be some non-negative integers. Let's define strange addition of \(a\) and \(b\) as following: write down the numbers one under another and align them by their least significant digit; add them up digit by digit and concatenate the respective sums together. Assume that both numbers have an infinite n... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 5 \cdot 10^5\)) β the length of the number \(c\) and the number of updates.The second line contains a string \(c\), consisting of exactly \(n\) digits from \(0\) to \(9\).Each of the next \(m\) lines contains two integers \(x\) and \(d\) (\(1 \le x ... | Print \(m\) integers β the \(i\)-th value should be equal to the number of pairs \((a, b)\) such that both \(a\) and \(b\) are non-negative integers and the result of a strange addition of \(a\) and \(b\) is equal to \(c\) after \(i\) updates are applied.Note that the numbers of pairs can be quite large, so print them ... | After the first update \(c\) is equal to \(14\). The pairs that sum up to \(14\) are: \((0, 14)\), \((1, 13)\), \((2, 12)\), \((3, 11)\), \((4, 10)\), \((5, 9)\), \((6, 8)\), \((7, 7)\), \((8, 6)\), \((9, 5)\), \((10, 4)\), \((11, 3)\), \((12, 2)\), \((13, 1)\), \((14, 0)\).After the second update \(c\) is equal to \(1... | Input: 2 3 14 2 4 2 1 1 0 | Output: 15 12 2 | Expert | 3 | 972 | 378 | 341 | 13 |
1,335 | C | 1335C | C. Two Teams Composing | 1,100 | binary search; greedy; implementation; sortings | You have \(n\) students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the \(i\)-th student skill is denoted by an integer \(a_i\) (different students can have the same skills).So, about the teams. Firstly, these two teams should ... | 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 first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of students. The second line of the test case contains \(n\) integers \(a_1, a_2, ... | For each test case, print the answer β the maximum possible size \(x\) for which it is possible to compose a valid pair of teams, where each team size is \(x\). | In the first test case of the example, it is possible to construct two teams of size \(3\): the first team is \([1, 2, 4]\) and the second team is \([4, 4, 4]\). Note, that there are some other ways to construct two valid teams of size \(3\). | Input: 4 7 4 2 4 1 4 3 4 5 2 1 5 4 3 1 1 4 1 1 1 3 | Output: 3 1 0 2 | Easy | 4 | 1,528 | 574 | 160 | 13 |
360 | A | 360A | A. Levko and Array Recovery | 1,700 | greedy; implementation | Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that m... | The first line contains two integers n and m (1 β€ n, m β€ 5000) β the size of the array and the number of operations in Levko's records, correspondingly.Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 β€ ti β€ 2) that describes the opera... | In the first line print ""YES"" (without the quotes), if the solution exists and ""NO"" (without the quotes) otherwise.If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| β€ 109) β the recovered array. | Input: 4 51 2 3 12 1 2 82 3 4 71 1 3 32 3 4 8 | Output: YES4 7 4 7 | Medium | 2 | 845 | 713 | 236 | 3 | |
1,392 | E | 1392E | E. Omkar and Duck | 2,100 | bitmasks; constructive algorithms; interactive; math | This is an interactive problem.Omkar has just come across a duck! The duck is walking on a grid with \(n\) rows and \(n\) columns (\(2 \leq n \leq 25\)) so that the grid contains a total of \(n^2\) cells. Let's denote by \((x, y)\) the cell in the \(x\)-th row from the top and the \(y\)-th column from the left. Right n... | The duck's three journeys are illustrated below.\(1 + 2 + 3 + 2 + 10 + 3 + 2 = 23\) \(1 + 4 + 9 + 0 + 7 + 3 + 2 = 26\) \(1 + 2 + 3 + 6 + 10 + 3 + 2 = 27\) | Input: 4 3 23 26 27 | Output: 1 2 3 6 4 6 2 10 9 0 7 3 2 8 8 2 1 1 1 2 1 3 2 3 2 4 3 4 4 4 1 1 2 1 3 1 3 2 3 3 3 4 4 4 1 1 1 2 1 3 1 4 2 4 3 4 4 4 | Hard | 4 | 1,736 | 0 | 0 | 13 | ||
1,578 | K | 1578K | K. Kingdom of Islands | 2,800 | brute force; graphs; implementation | The Kingdom of Islands consists of \(p\) islands. As the king, you rule over the whole kingdom, while each island is ruled over by one or several jarls under your rule. In total, there are \(n\) jarls under your jurisdiction.Each island of the kingdom has its own strong traditions, so jarls that rule over the same isla... | The first line of the input consists of two integers \(p\) and \(n\) (\(1 \le p \le n \le 10^5\); \(1 \le p \le 10^4\)).The second line consists of \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le p\)). The integer \(s_i\) denotes that the \(i\)-th jarl rules over the island number \(s_i\). It is guaranteed th... | In the first line print a single integer \(q\) between \(1\) and \(n\) β the largest possible number of jarls in a pairwise conflicting group. In the second line print \(q\) distinct integers between \(1\) and \(n\) β the numbers of jarls in the group. The numbers of jarls can be printed in any order. | The conflict graph for the last sample testcase is given below. Each circle represents an island. | Input: 4 4 1 2 3 4 1 2 3 | Output: 3 1 4 2 | Master | 3 | 1,249 | 743 | 302 | 15 |
1,635 | D | 1635D | D. Infinite Set | 1,800 | bitmasks; dp; math; matrices; number theory; strings | You are given an array \(a\) consisting of \(n\) distinct positive integers.Let's consider an infinite integer set \(S\) which contains all integers \(x\) that satisfy at least one of the following conditions: \(x = a_i\) for some \(1 \leq i \leq n\). \(x = 2y + 1\) and \(y\) is in \(S\). \(x = 4y\) and \(y\) is in \(S... | The first line contains two integers \(n\) and \(p\) \((1 \leq n, p \leq 2 \cdot 10^5)\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) \((1 \leq a_i \leq 10^9)\).It is guaranteed that all the numbers in \(a\) are distinct. | Print a single integer, the number of elements in \(S\) that are strictly smaller than \(2^p\). Remember to print it modulo \(10^9 + 7\). | In the first example, the elements smaller than \(2^4\) are \(\{1, 3, 4, 6, 7, 9, 12, 13, 15\}\).In the second example, the elements smaller than \(2^7\) are \(\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}\). | Input: 2 4 6 1 | Output: 9 | Medium | 6 | 576 | 239 | 137 | 16 |
217 | E | 217E | E. Alien DNA | 2,800 | data structures; dsu; trees | Professor Bajtocy is conducting experiments on alien DNA. He has discovered that it is subject to repetitive mutations β each mutation happens in the same way: some continuous subsequence of the alien DNA becomes active, copies itself, the copy gets mangled and inserts itself right after the original subsequence. The m... | The first line of input contains the original DNA sequence, consisting only of letters ""A"", ""C"", ""T"" and ""G"" and not exceeding 3Β·106 in length. The second line contains a single integer k (1 β€ k β€ 3Β·106).The third line contains a single integer n (0 β€ n β€ 5000) β the number of mutations. The next n lines descri... | Output a single line, containing the first k letters of the mutated DNA sequence. | In the second example, after the first mutation the sequence is ""ACCAGTACGT"". After the second mutation it's ""ACCAGTACCGACATCGT"". | Input: GAGA40 | Output: GAGA | Master | 3 | 1,122 | 994 | 81 | 2 |
1,144 | E | 1144E | E. Median String | 1,900 | bitmasks; math; number theory; strings | You are given two strings \(s\) and \(t\), both consisting of exactly \(k\) lowercase Latin letters, \(s\) is lexicographically less than \(t\).Let's consider list of all strings consisting of exactly \(k\) lowercase Latin letters, lexicographically not less than \(s\) and not greater than \(t\) (including \(s\) and \(... | The first line of the input contains one integer \(k\) (\(1 \le k \le 2 \cdot 10^5\)) β the length of strings.The second line of the input contains one string \(s\) consisting of exactly \(k\) lowercase Latin letters.The third line of the input contains one string \(t\) consisting of exactly \(k\) lowercase Latin lette... | Print one string consisting exactly of \(k\) lowercase Latin letters β the median (the middle element) of list of strings of length \(k\) lexicographically not less than \(s\) and not greater than \(t\). | Input: 2 az bf | Output: bc | Hard | 4 | 712 | 509 | 203 | 11 | |
1,868 | E | 1868E | E. Min-Sum-Max | 3,500 | constructive algorithms; dp; greedy | Tom is waiting for his results of Zhongkao examination. To ease the tense atmosphere, his friend, Daniel, decided to play a game with him. This game is called ""Divide the Array"".The game is about the array \(a\) consisting of \(n\) integers. Denote \([l,r]\) as the subsegment consisting of integers \(a_l,a_{l+1},\ldo... | The first line of input contains a single integer \(t\) (\(1\le t\le 50\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 300\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_... | For each test case, output a single integer β the maximum number of subsegments among all possible ways to divide the array \(a\). | In the first test case, Daniel can divide the array into \([-1]\) and \([5,4]\), and \(s=[-1,9]\). It can be shown that for any \(i=j\), the condition in the statement is already satisfied, and for \(i=1,j=2\), we have \(\min(-1,9)\le (-1)+9\le \max(-1,9)\).In the second test case, if Daniel divides the array into \([2... | Input: 83-1 5 422023 204361 4 7 -1 5 -45-4 0 3 -18 10199824485310-4 2 5 -10 4 8 2 9 -15 77-7 3 8 -9 -2 2 44-5 5 -2 -5 | Output: 2 1 3 2 1 6 5 3 | Master | 3 | 1,108 | 484 | 130 | 18 |
1,579 | G | 1579G | G. Minimal Coverage | 2,200 | dp | You are given \(n\) lengths of segments that need to be placed on an infinite axis with coordinates.The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate \(0\). Let's call this endpoint the ""start"" of the first segment and let's call its ""end"" as that endpoint that i... | The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases. The first line of each test case description contains an integer \(n\) (\(1 \le n \le 10^4\)) β the number of segments. The second line of the description contains \... | Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β the minimal possible length of the axis coverage. | In the third sample test case the segments should be arranged as follows: \([0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]\). As you can see, the last segment \([-2, 7]\) covers all the previous ones, and the total length of coverage is \(9\).In the fourth sample test case the segments should be arran... | Input: 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 | Output: 3 3 9 9 7 8 | Hard | 1 | 1,122 | 545 | 187 | 15 |
484 | B | 484B | B. Maximum Value | 2,100 | binary search; math; sortings; two pointers | You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 β€ i, j β€ n and ai β₯ aj. | The first line contains integer n β the length of the sequence (1 β€ n β€ 2Β·105). The second line contains n space-separated integers ai (1 β€ ai β€ 106). | Print the answer to the problem. | Input: 33 4 5 | Output: 2 | Hard | 4 | 160 | 150 | 32 | 4 | |
1,900 | F | 1900F | F. Local Deletions | 2,800 | binary search; data structures; implementation | For an array \(b_1, b_2, \ldots, b_m\), for some \(i\) (\(1 < i < m\)), element \(b_i\) is said to be a local minimum if \(b_i < b_{i-1}\) and \(b_i < b_{i+1}\). Element \(b_1\) is said to be a local minimum if \(b_1 < b_2\). Element \(b_m\) is said to be a local minimum if \(b_m < b_{m-1}\).For an array \(b_1, b_2, \l... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)) β the length of the permutation \(a\) and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements of permutation \(a\).The \(i\)-th of the next \(q\) lines contains two int... | For each query, output a single integer β the answer to that query. | In the first query of the first example, the only number in the subarray is \(1\), therefore it is the answer.In the second query of the first example, our subarray initially looks like \([1, 4]\). After performing type \(1\) operation we get \([1]\).In the third query of the first example, our subarray initially looks... | Input: 7 5 1 4 3 6 2 7 5 1 1 1 2 2 3 1 4 1 7 | Output: 1 1 3 3 3 | Master | 3 | 1,693 | 412 | 67 | 19 |
633 | C | 633C | C. Spy Syndrome 2 | 1,900 | data structures; dp; hashing; implementation; sortings; string suffix structures; strings | After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhantβs sentences, Yash determined a new cipher technique.For a given sentence, the cipher ... | The first line of the input contains a single integer n (1 β€ n β€ 10 000) β the length of the ciphered text. The second line consists of n lowercase English letters β the ciphered text t.The third line contains a single integer m (1 β€ m β€ 100 000) β the number of words which will be considered while deciphering the text... | Print one line β the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those. | In sample case 2 there may be multiple accepted outputs, ""HI there HeLLo"" and ""HI there hello"" you may output any of them. | Input: 30ariksihsidlihcdnaehsetahgnisol10KirahatesishelosingdeathchildishLandNote | Output: Kira is childish and he hates losing | Hard | 7 | 850 | 527 | 153 | 6 |
1,144 | C | 1144C | C. Two Shuffled Sequences | 1,000 | constructive algorithms; sortings | Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.Strictly increasing sequence is a sequence of integers \([x_1 < x_2 < \dots < x_k]\). And strictly decreasing sequence is a sequence of integers \([y_1 > y_2 > \dots > y_l]\). Note that the empty seque... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\). | If there is a contradiction in the input and it is impossible to split the given sequence \(a\) to increasing and decreasing sequences, print ""NO"" in the first line.Otherwise print ""YES"" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be ... | Input: 7 7 2 7 3 3 1 4 | Output: YES 2 3 7 5 7 4 3 2 1 | Beginner | 2 | 1,160 | 279 | 1,375 | 11 | |
644 | C | 644C | C. Hostname Aliases | 2,100 | *special; binary search; data structures; implementation; sortings; strings | There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru.You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://<hostname>[/<... | The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of page queries. Then follow n lines each containing exactly one address. Each address is of the form http://<hostname>[/<path>], where: <hostname> consists of lowercase English letters and dots, there are no two consecutive dots, <h... | First print k β the number of groups of server names that correspond to one website. You should count only groups of size greater than one.Next k lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups an... | Input: 10http://abacaba.ru/testhttp://abacaba.ru/http://abacaba.comhttp://abacaba.com/testhttp://abacaba.de/http://abacaba.ru/testhttp://abacaba.de/testhttp://abacaba.com/http://abacaba.com/thttp://abacaba.com/test | Output: 1http://abacaba.de http://abacaba.ru | Hard | 6 | 1,055 | 639 | 364 | 6 | |
2,049 | C | 2049C | C. MEX Cycle | 1,500 | brute force; constructive algorithms; greedy; implementation | Evirir the dragon has many friends. They have 3 friends! That is one more than the average dragon.You are given integers \(n\), \(x\), and \(y\). There are \(n\) dragons sitting in a circle. The dragons are numbered \(1, 2, \ldots, n\). For each \(i\) (\(1 \le i \le n\)), dragon \(i\) is friends with dragon \(i - 1\) 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 and only line of each test case contains three integers \(n\), \(x\), \(y\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le x < y \le n\)).It is guaranteed t... | For each test case, output \(n\) space-separated non-negative integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) on a line that satisfy the condition in the statement. If there are multiple solutions, print any of them. It can be proven that under the problem constraints, a solution with \(0 \le a_i \le 10^9\)... | For the first test case: \(i = 1\): Dragon \(1\)'s friends are dragons \(2, 3, 5\). \(\operatorname{mex}(a_2, a_3, a_5) = \operatorname{mex}(2, 1, 1) = 0 = a_1\), so the condition for dragon \(1\) is satisfied. \(i = 2\): Dragon \(2\)'s friends are dragons \(1, 3\). \(\operatorname{mex}(a_1, a_3) = \operatorname{mex}(0... | Input: 75 1 34 2 46 3 57 3 63 2 35 1 56 2 5 | Output: 0 2 1 0 1 1 2 1 0 1 2 0 1 2 0 0 1 2 0 1 0 1 2 0 1 1 0 2 1 0 0 1 2 0 2 1 | Medium | 4 | 1,068 | 394 | 335 | 20 |
1,851 | E | 1851E | E. Nastya and Potions | 1,500 | dfs and similar; dp; graphs; sortings | Alchemist Nastya loves mixing potions. There are a total of \(n\) types of potions, and one potion of type \(i\) can be bought for \(c_i\) coins.Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be... | The first line of each test contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case is described as follows:The first line contains two integers \(n\) and \(k\) (\(1 \le k < n \le 2 \cdot 10^5\)) β the total number of potion types and the number of potion types Nastya already has.The ... | For each test case, output \(n\) integers β the minimum number of coins Nastya needs to spend to obtain a potion of each type. | In the first test case of the first sample, it is optimal: Get a potion of the first type by buying and mixing \(2\), \(4\) and \(5\); a potion of the second type can only be obtained by purchasing it; Nastya already has an unlimited number of potions of the third type; a potion of the fourth type is more profitable to... | Input: 45 130 8 3 5 1033 2 4 50 0 2 3 50 3 25 143 31 31 20 2 1 25 15 4 1 3 422 4 53 3 5 42 1 41 50 4 21 1 5 42 43 2 4 30 2 2 41 2 | Output: 23 8 0 5 10 0 143 0 5 0 1 3 4 0 0 0 0 | Medium | 4 | 694 | 1,400 | 126 | 18 |
2,072 | A | 2072A | A. New World, New Me, New Array | 800 | greedy; implementation; math | Natsume Akito has just woken up in a new world and immediately received his first quest! The system provided him with an array \(a\) of \(n\) zeros, an integer \(k\), and an integer \(p\).In one operation, Akito chooses two integers \(i\) and \(x\) such that \(1 \le i \le n\) and \(-p \le x \le p\), and performs the as... | The first line of input contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains three integers \(n\), \(k\), \(p\) (\(1 \le n \le 50\), \(-2500 \le k \le 2500\), \(1 \le p \le 50\)) β the length of the array, the required sum, and the boundary of the segment... | For each test case, output the minimum number of operations to achieve the final sum \(k\) in the array, or \(-1\) if it is impossible to achieve the sum \(k\). | In the fifth example, the sum of the array is initially zero, so no operations are needed.In the sixth example, the maximum sum in the array that we can achieve is \(9\) (by assigning the number \(9\) to the single element), so the sum \(10\) cannot be obtained by any operations.In the seventh example, only one operati... | Input: 821 100 109 -420 425 -7 213 37 710 0 491 10 97 -7 720 31 1 | Output: 10 -1 4 6 0 -1 1 -1 | Beginner | 3 | 565 | 356 | 160 | 20 |
1,070 | H | 1070H | H. BerOS File Suggestion | 1,500 | brute force; implementation | Polycarp is working on a new operating system called BerOS. He asks you to help with implementation of a file suggestion feature.There are \(n\) files on hard drive and their names are \(f_1, f_2, \dots, f_n\). Any file name contains between \(1\) and \(8\) characters, inclusive. All file names are unique.The file sugg... | The first line of the input contains integer \(n\) (\(1 \le n \le 10000\)) β the total number of files.The following \(n\) lines contain file names, one per line. The \(i\)-th line contains \(f_i\) β the name of the \(i\)-th file. Each file name contains between \(1\) and \(8\) characters, inclusive. File names contain... | Print \(q\) lines, one per query. The \(j\)-th line should contain the response on the \(j\)-th query β two values \(c_j\) and \(t_j\), where \(c_j\) is the number of matched files for the \(j\)-th query, \(t_j\) is the name of any file matched by the \(j\)-th query. If there is no such file, print a single character '... | Input: 4testconteststest..test6ts.st..testcontes.st | Output: 1 contests2 .test1 test.1 .test0 -4 test. | Medium | 2 | 832 | 857 | 379 | 10 | |
288 | D | 288D | D. Polo the Penguin and Trees | 2,400 | combinatorics; dfs and similar; trees | Little penguin Polo has got a tree β a non-directed connected acyclic graph, containing n nodes and n - 1 edges. We will consider the tree nodes numbered by integers from 1 to n.Today Polo wonders, how to find the number of pairs of paths that don't have common nodes. More formally, he should find the number of groups ... | The first line contains integer n (1 β€ n β€ 80000) β the number of tree nodes. Each of the following n - 1 lines contains a pair of integers ui and vi (1 β€ ui, vi β€ n; ui β vi) β the i-th edge of the tree.It is guaranteed that the given graph is a tree. | In a single line print a single integer β the answer to the problem.Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator. | Input: 41 22 33 4 | Output: 2 | Expert | 3 | 615 | 252 | 221 | 2 | |
1,571 | D | 1571D | D. Sweepstake | 1,800 | *special; brute force; constructive algorithms; implementation; math | Kotlin Heroes competition is nearing completion. This time \(n\) programmers took part in the competition. Now organizers are thinking how to entertain spectators as well. One of the possibilities is holding sweepstakes. So for now they decided to conduct a survey among spectators.In total, organizers asked \(m\) viewe... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 1000\); \(1 \le m \le 2 \cdot 10^5\)) β the number of programmers participating in the competition and the number of surveyed spectators.Next \(m\) lines contain answers of spectators. The \(i\)-th line contains two integers \(f_i\) and \(l_i\) (\(1 \l... | Print the single integer β the worst rank among spectators you can possibly get according to organizers' ranking system (bigger rank β worse, of course). | In the first example, if the second programmer takes first place, while the first programmer takes last place, you'll have \(0\) right answers while the other two spectators β \(2\) right answers. That's why your rank (in the worst case) will be \(c_2 + c_1 + 1\) \(=\) \(2 + 0 + 1 = 3\).In the second example, for examp... | Input: 2 3 1 2 2 1 2 1 | Output: 3 | Medium | 5 | 1,088 | 552 | 153 | 15 |
1,292 | C | 1292C | C. Xenon's Attack on the Gangs | 2,300 | combinatorics; dfs and similar; dp; greedy; trees | INSPION FullBand Master - INSPION INSPION - IOLITE-SUNSTONEOn another floor of the A.R.C. Markland-N, the young man Simon ""Xenon"" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker ""X"" instinct and fight against the gangs of the... | The first line contains an integer \(n\) (\(2 \leq n \leq 3000\)), the number of gangs in the network.Each of the next \(n - 1\) lines contains integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\); \(u_i \neq v_i\)), indicating there's a direct link between gangs \(u_i\) and \(v_i\).It's guaranteed that links are p... | Print the maximum possible value of \(S\) β the number of password layers in the gangs' network. | In the first example, one can achieve the maximum \(S\) with the following assignment: With this assignment, \(mex(1, 2) = 0\), \(mex(1, 3) = 2\) and \(mex(2, 3) = 1\). Therefore, \(S = 0 + 2 + 1 = 3\).In the second example, one can achieve the maximum \(S\) with the following assignment: With this assignment, all non-... | Input: 3 1 2 2 3 | Output: 3 | Expert | 5 | 1,605 | 409 | 96 | 12 |
1,546 | B | 1546B | B. AquaMoon and Stolen String | 1,200 | interactive; math | AquaMoon had \(n\) strings of length \(m\) each. \(n\) is an odd number.When AquaMoon was gone, Cirno tried to pair these \(n\) strings together. After making \(\frac{n-1}{2}\) pairs, she found out that there was exactly one string without the pair!In her rage, she disrupted each pair of strings. For each pair, she sel... | This problem is made as interactive. It means, that your solution will read the input, given by the interactor. But the interactor will give you the full input at the beginning and after that, you should print the answer. So you should solve the problem, like as you solve the usual, non-interactive problem because you ... | For each test case print a single line with the stolen string. | In the first test case, ""aaaaa"" and ""bbbbb"" exchanged all positions, and ""ccccc"" is the stolen string.In the second test case, ""aaaa"" and ""bbbb"" exchanged two first positions, and ""cccc"" is the stolen string.This is the first test in the hack format: 33 5aaaaabbbbbccccc1 2 5 1 2 3 4 52 1 33 4aaaabbbbcccc1 2... | Input: 3 3 5 aaaaa bbbbb ccccc aaaaa bbbbb 3 4 aaaa bbbb cccc aabb bbaa 5 6 abcdef uuuuuu kekeke ekekek xyzklm xbcklf eueueu ayzdem ukukuk | Output: ccccc cccc kekeke | Easy | 2 | 1,019 | 2,287 | 62 | 15 |
677 | D | 677D | D. Vanya and Treasure | 2,300 | data structures; dp; graphs; shortest paths | Vanya is in the palace that can be represented as a grid n Γ m. Each room contains a single chest, an the room located in the i-th row and j-th columns contains the chest of type aij. Each chest of type x β€ p - 1 contains a key that can open any chest of type x + 1, and all chests of type 1 are not locked. There is exa... | The first line of the input contains three integers n, m and p (1 β€ n, m β€ 300, 1 β€ p β€ nΒ·m) β the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.Each of the following n lines contains m integers aij (1 β€ aij β€ p) β the types of the chests i... | Print one integer β the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type p. | Input: 3 4 32 1 1 11 1 1 12 1 1 3 | Output: 5 | Expert | 4 | 632 | 554 | 128 | 6 | |
64 | H | 64H | H. Table Bowling | 2,300 | *special; sortings | Table bowling tournament participant completed the competition according to the given final standings table. The table is given as a sequence of lines, each line has a format ""name score"". Your task is to prepare another table consisting of lines in the form ""place name"". Sort participant by score (desc.) and by th... | The first line contains n (1 β€ n β€ 100) β the number of rows in the table. Following n lines contain the given table. Each line has the form ""name score"", where ""name"" is a sequence of lowercase Latin letters, and ""score"" β is an integer number between 0 and 1000, inclusive. All the names are distinct. The length... | Print the required table. Look at the sample outputs for clarifications. | Input: 5vasya 10ted 11petya 10katya 33mike 44 | Output: 1 mike2 katya3 ted4-5 petya4-5 vasya | Expert | 2 | 570 | 443 | 72 | 0 | |
1,879 | E | 1879E | E. Interactive Game with Coloring | 2,400 | brute force; constructive algorithms; dfs and similar; graphs; implementation; interactive; trees | This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You m... | The first line contains one integer \(n\) (\(3 \le n \le 100\)) β the number of vertices in the tree.The second line contains \(n-1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i < i\)), where \(p_i\) is the parent of the \(i\)-th vertex in the tree. | In the first example, every vertex from \(2\) to \(n\) is connected to the root. So, we can paint all edges into the same color \(1\), and when the game starts, there will be only one edge incident to the current vertex (and it will lead to the root).In the second example, the tree is a path of \(4\) vertices. We have ... | Input: 5 1 1 1 1 0 1 1 | Output: 1 1 1 1 1 1 | Expert | 7 | 2,445 | 252 | 0 | 18 | |
207 | D3 | 207D3 | D3. The Beaver's Problem - 3 | 2,000 | The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj... | The first line contains integer id (0 β€ id β€ 106) β the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in... | Print an integer from 1 to 3, inclusive β the number of the subject the given document corresponds to. | Hard | 0 | 1,472 | 653 | 102 | 2 | |||
985 | E | 985E | E. Pencils and Boxes | 2,100 | binary search; data structures; dp; greedy; two pointers | Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to ... | The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β ... | Print ""YES"" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print ""NO"". | In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put... | Input: 6 3 107 2 7 7 4 2 | Output: YES | Hard | 5 | 937 | 355 | 127 | 9 |
1,571 | G | 1571G | G. A Battle Against a Dragon | 2,500 | *special; data structures; dp | A squad of \(n\) warriors is defending a castle from a dragon attack. There are \(m\) barricades between the castle and the dragon.The warriors are numbered from \(1\) to \(n\). The \(i\)-th warrior knows \(k_i\) attacks: the \(j\)-th of them deals \(a_{i,j}\) damage to the dragon and can only be applied if there are e... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of warriors and the initial number of barricades.Then the descriptions of attacks for each warrior follow. The \(i\)-th description consists of three lines.The first line contains a single integer \(k_i\) (\(1 \le k_i \l... | Print a single integer β the maximum total damage the warriors can deal to the dragon. | In the first example, the optimal choice is the following: warrior \(1\) destroys a barricade, now there are \(3\) barricades left; warrior \(2\) deploys his first attack (he can do it because \(b_{1,1}=3\), which is the current number of barricades). The total damage is \(10\).If the first warrior used his attack or s... | Input: 2 4 1 2 4 2 10 5 3 4 | Output: 10 | Expert | 3 | 894 | 931 | 86 | 15 |
1,211 | I | 1211I | I. Unusual Graph | 3,000 | *special; graphs | Ivan on his birthday was presented with array of non-negative integers \(a_1, a_2, \ldots, a_n\). He immediately noted that all \(a_i\) satisfy the condition \(0 \leq a_i \leq 15\).Ivan likes graph theory very much, so he decided to transform his sequence to the graph.There will be \(n\) vertices in his graph, and vert... | The first line of input contain two integers \(n,m\) (\(1 \leq n \leq 500, 0 \leq m \leq \frac{n(n-1)}{2}\)): number of vertices and edges in Ivan's graph.Next \(m\) lines contain the description of edges: \(i\)-th line contain two integers \(u_i, v_i\) (\(1 \leq u_i, v_i \leq n; u_i \neq v_i\)), describing undirected ... | Output \(n\) space-separated integers, \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 15\)). Printed numbers should satisfy the constraints: edge between vertices \(u\) and \(v\) present in the graph if and only if \(a_u \oplus a_v = 2^k\) for some integer \(k \geq 0\).It is guaranteed that there exists some solution for... | Input: 4 4 1 2 2 3 3 4 4 1 | Output: 0 1 0 1 | Master | 2 | 851 | 511 | 399 | 12 | |
993 | A | 993A | A. Two Squares | 1,600 | geometry; implementation | You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the tw... | The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.The first line contains the coordinates of the square with sides parallel to the co... | Print ""Yes"" if squares intersect, otherwise print ""No"".You can print each letter in any case (upper or lower). | In the first example the second square lies entirely within the first square, so they do intersect.In the second sample squares do not have any points in common.Here are images corresponding to the samples: | Input: 0 0 6 0 6 6 0 61 3 3 5 5 3 3 1 | Output: YES | Medium | 2 | 397 | 464 | 114 | 9 |
64 | G | 64G | G. Path Canonization | 2,200 | *special | A path in some Unix-similar file system is given. The path consists of elements separated with characters ""/"". For example: ""/usr/share/mysql/../tomcat6/conf/server.xml"". The path starts with the root directory (i.e. starts with the character ""/""). Each element means a name of file or directory, or it is one of t... | The only line contains the given path. The path starts with ""/"" and consists of elements separated with ""/"". No two ""/"" follow one after another (consecutively). The only path which can end with ""/"" is the root directory path equal to ""/"".Each element may contain ""a""-""z"", ""0""-""9"" and dots. Any element... | Print the required path or ""-1"". | Input: /usr/share/mysql/../tomcat6/conf/server.xml | Output: /usr/share/tomcat6/conf/server.xml | Hard | 1 | 846 | 468 | 34 | 0 | |
949 | E | 949E | E. Binary Cards | 2,700 | brute force | It is never too late to play the fancy ""Binary Cards"" game!There is an infinite amount of cards of positive and negative ranks that are used in the game. The absolute value of any card rank is a power of two, i.e. each card has a rank of either 2k or - 2k for some integer k β₯ 0. There is an infinite amount of cards o... | The first line of input contains an integer n (1 β€ n β€ 100 000), the number of rounds in the game.The second line of input contains n integers a1, a2, ..., an ( - 100 000 β€ ai β€ 100 000), the numbers that jury is going to tell in each round. | In the first line print the integer k (0 β€ k β€ 100 000), the minimum number of cards you have to pick in your deck in ordered to win the ""Binary Cards"".In the second line print k integers b1, b2, ..., bk ( - 220 β€ bi β€ 220, |bi| is a power of two), the ranks of the cards in your deck. You may output ranks in any orde... | In the first sample there is the only round in the game, in which you may simply draw both your cards. Note that this sample test is the only one satisfying the first test group constraints.In the second sample you may draw the only card - 1 in the first round, cards 4 and - 1 in the second round, nothing in the third ... | Input: 19 | Output: 21 8 | Master | 1 | 1,310 | 241 | 492 | 9 |
1,837 | F | 1837F | F. Editorial for Two | 2,400 | binary search; data structures; greedy; implementation | Berland Intercollegiate Contest has just finished. Monocarp and Polycarp, as the jury, are going to conduct an editorial. Unfortunately, the time is limited, since they have to finish before the closing ceremony.There were \(n\) problems in the contest. The problems are numbered from \(1\) to \(n\). The editorial for t... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 3 \cdot 10^5\)) β the number of problems in the full problemset and the number of problems Monocarp and Polycarp are going to conduct... | For each testcase, print a single integer β the smallest amount of time the editorial takes, if Monocarp and Polycarp can choose which \(k\) of \(n\) problems to conduct an editorial for and how to split them among themselves. | Input: 65 41 10 1 1 15 31 20 5 15 35 31 20 3 15 510 610 8 20 14 3 8 6 4 16 1110 59 9 2 13 15 19 4 9 13 121 11 | Output: 2 6 5 21 18 1 | Expert | 4 | 1,123 | 528 | 226 | 18 | |
1,325 | E | 1325E | E. Ehab's REAL Number Theory Problem | 2,600 | brute force; dfs and similar; graphs; number theory; shortest paths | You are given an array \(a\) of length \(n\) that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.A sequence \(a\) is a subsequence of an array \(b\) if \(a\) can be obtained fr... | The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) β the length of \(a\).The second line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_{n}\) (\(1 \le a_i \le 10^6\)) β the elements of the array \(a\). | Output the length of the shortest non-empty subsequence of \(a\) product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print ""-1"". | In the first sample, you can choose a subsequence \([1]\).In the second sample, you can choose a subsequence \([6, 6]\).In the third sample, you can choose a subsequence \([6, 15, 10]\).In the fourth sample, there is no such subsequence. | Input: 3 1 4 6 | Output: 1 | Expert | 5 | 385 | 225 | 227 | 13 |
413 | D | 413D | D. 2048 | 2,000 | bitmasks; dp | The programmers from the R2 company love playing 2048. One day, they decided to invent their own simplified version of this game β 2k on a stripe.Imagine an infinite in one direction stripe, consisting of unit squares (the side of each square is equal to the height of the stripe). Each square can either be empty or con... | The first line contains two integers n and k (1 β€ n β€ 2000; 3 β€ k β€ 11). The next line contains sequence of n integers, each of them is either 0, or 2, or 4. | Print a single integer β the number of ways to replace zeroes by numbers 2 or 4 to get a winning sequence. As this number can be rather large, print it modulo 1000000007 (109 + 7). | Consider the first example. The beginning of the strip will look as follows: 2 β 4 β 8 β 8 2 β 8 4 β 8 4 2 β 16.To better understand the game, you can see the original game on http://gabrielecirulli.github.io/2048/. Please note that the game that is described on the strip is slightly different from the original game (w... | Input: 7 42 2 4 2 2 2 2 | Output: 1 | Hard | 2 | 1,779 | 157 | 180 | 4 |
1,735 | B | 1735B | B. Tea with Tangerines | 900 | greedy; math | There are \(n\) pieces of tangerine peel, the \(i\)-th of them has size \(a_i\). In one step it is possible to divide one piece of size \(x\) into two pieces of positive integer sizes \(y\) and \(z\) so that \(y + z = x\).You want that for each pair of pieces, their sizes differ strictly less than twice. In other words... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The first line of each test case contains the integer \(n\) (\(1 \le n \le 100\)).Then one line follows, containing \(n\) integers \(a_1 \le a_2 \le \ldots \le a_n\) (\(1 \... | For each test case, output a single line containing the minimum number of steps. | In the first test case, we initially have a piece of size \(1\), so all final pieces must have size \(1\). The total number of steps is: \(0 + 1 + 2 + 3 + 4 = 10\).In the second test case, we have just one piece, so we don't need to do anything, and the answer is \(0\) steps.In the third test case, one of the possible ... | Input: 351 2 3 4 5110335600 900 1300 2000 2550 | Output: 10 0 4 | Beginner | 2 | 479 | 339 | 80 | 17 |
628 | B | 628B | B. New Skateboard | 1,300 | dp | Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to press the keys randomly. Unfortunately Max has forgotten the number which... | The only line contains string s (1 β€ |s| β€ 3Β·105). The string s contains only digits from 0 to 9. | Print integer a β the number of substrings of the string s that are divisible by 4.Note that the answer can be huge, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. | Input: 124 | Output: 4 | Easy | 1 | 1,111 | 97 | 256 | 6 | |
1,027 | A | 1027A | A. Palindromic Twist | 1,000 | implementation; strings | You are given a string \(s\) consisting of \(n\) lowercase Latin letters. \(n\) is even.For each position \(i\) (\(1 \le i \le n\)) in string \(s\) you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these opti... | The first line contains a single integer \(T\) (\(1 \le T \le 50\)) β the number of strings in a testcase.Then \(2T\) lines follow β lines \((2i - 1)\) and \(2i\) of them describe the \(i\)-th string. The first line of the pair contains a single integer \(n\) (\(2 \le n \le 100\), \(n\) is even) β the length of the cor... | Print \(T\) lines. The \(i\)-th line should contain the answer to the \(i\)-th string of the input. Print ""YES"" if it's possible to make the \(i\)-th string a palindrome by applying the aforementioned changes to every position. Print ""NO"" otherwise. | The first string of the example can be changed to ""bcbbcb"", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.The second string can be changed to ""be"", ""bg"", ""de"", ""dg"", but none of these resulting strings are palindromes.The... | Input: 56abccba2cf4adfa8abaazaba2ml | Output: YESNOYESNONO | Beginner | 2 | 1,372 | 436 | 253 | 10 |
1,938 | H | 1938H | 1,600 | Medium | 0 | 0 | 0 | 0 | 19 | |||||||
1,990 | D | 1990D | D. Grid Puzzle | 1,800 | bitmasks; brute force; dp; greedy; implementation | You are given an array \(a\) of size \(n\).There is an \(n \times n\) grid. In the \(i\)-th row, the first \(a_i\) cells are black and the other cells are white. In other words, note \((i,j)\) as the cell in the \(i\)-th row and \(j\)-th column, cells \((i,1), (i,2), \ldots, (i,a_i)\) are black, and cells \((i,a_i+1), ... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.For each test case: The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the size of the array \(a\). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq n\)). It's guaran... | For each test case, output a single integer β the minimum number of operations to dye all cells white. | In the first test case, you don't need to do any operation.In the second test case, you can do: Dye \((1,1), (1,2), (2,1)\), and \((2,2)\) white; Dye \((2,3), (2,4), (3,3)\), and \((3,4)\) white; Dye \((3,1), (3,2), (4,1)\), and \((4,2)\) white. It can be proven \(3\) is the minimum number of operations.In the third te... | Input: 101042 4 4 243 2 1 030 3 030 1 333 1 043 1 0 340 2 2 261 3 4 2 0 482 2 5 2 3 4 2 4 | Output: 0 3 2 1 2 2 3 2 4 6 | Medium | 5 | 579 | 400 | 102 | 19 |
1,941 | B | 1941B | B. Rudolf and 121 | 1,000 | brute force; dp; greedy; math | Rudolf has an array \(a\) of \(n\) integers, the elements are numbered from \(1\) to \(n\).In one operation, he can choose an index \(i\) (\(2 \le i \le n - 1\)) and assign: \(a_{i - 1} = a_{i - 1} - 1\) \(a_i = a_i - 2\) \(a_{i + 1} = a_{i + 1} - 1\) Rudolf can apply this operation any number of times. Any index \(i\)... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The first line of each case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of elements in the array.The second line of each case contains \(n\) integers \(a_1, a_2, \dot... | For each test case, output ""YES"" if it is possible to make all the elements of the array zero using the described operations. Otherwise, output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first example, the original array is \([1, 3, 5, 5, 2]\), to make all its elements zero, Rudolf can act as follows: apply the operation at \(i=4\) and get the array \([1, 3, 4, 3, 1]\); apply the operation at \(i=3\) and get the array \([1, 2, 2, 2, 1]\); apply the operation at \(i=2\) and get the array \([0, 0,... | Input: 751 3 5 5 252 4 4 5 150 1 3 3 165 6 0 2 3 041 2 7 237 1 041 1 1 1 | Output: YES NO YES NO NO NO NO | Beginner | 4 | 429 | 488 | 320 | 19 |
769 | D | 769D | D. k-Interesting Pairs Of Integers | 1,700 | *special; bitmasks; brute force; meet-in-the-middle | Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs ... | The first line contains two integers n and k (2 β€ n β€ 105, 0 β€ k β€ 14) β the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.The second line contains the sequence a1, a2, ..., an (0 β€ ai β€ 104), which Vasya has. | Print the number of pairs (i, j) so that i < j and the pair of integers ai and aj is k-interesting. | In the first test there are 4 k-interesting pairs: (1, 3), (1, 4), (2, 3), (2, 4). In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: (1, 5), (1, 6), (2, 3), (2, 4), (3, 4), (5, 6). | Input: 4 10 3 2 1 | Output: 4 | Medium | 4 | 533 | 279 | 99 | 7 |
853 | A | 853A | A. Planning | 1,500 | greedy | Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today, the i-th of them is planned to depart at the i-th minute of the day.Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exa... | The first line contains two integers n and k (1 β€ k β€ n β€ 300 000), here n is the number of flights, and k is the number of minutes in the beginning of the day that the flights did not depart.The second line contains n integers c1, c2, ..., cn (1 β€ ci β€ 107), here ci is the cost of delaying the i-th flight for one minu... | The first line must contain the minimum possible total cost of delaying the flights.The second line must contain n different integers t1, t2, ..., tn (k + 1 β€ ti β€ k + n), here ti is the minute when the i-th flight must depart. If there are several optimal schedules, print any of them. | Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3 - 1)Β·4 + (4 - 2)Β·2 + (5 - 3)Β·1 + (6 - 4)Β·10 + (7 - 5)Β·2 = 38 burles. However, the better schedule is shown in the sample answer, its cost is (3 - 1)Β·4 + (6 - 2)Β·2 + (7 - ... | Input: 5 24 2 1 10 2 | Output: 203 6 7 4 5 | Medium | 1 | 1,079 | 323 | 286 | 8 |
1,615 | D | 1615D | D. X(or)-mas Tree | 2,200 | bitmasks; dfs and similar; dsu; graphs; trees | 'Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are \(n\) nodes in the tree, connected by \(n-1\) edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation. He has \(m\) elves come over and admir... | The first line contains one integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains two integers, \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\); \(1 \leq m \leq 2 \cdot 10^5\)) β the size of tree and the number of elves respectively.... | For each test case, first print either YES or NO (in any case), whether there's a tree consistent with Santa's memory or not. If the answer is YES, print \(n-1\) lines each containing three integers: \(x\), \(y\), and \(v\) (\(1 \le x, y \le n\); \(0 \le v < 2^{30}\)) β the edge and the integer on that edge. The set of... | The first test case is the image in the statement.One possible answer is assigning the value of the edge \((1, 2)\) to \(5\), and the value of the edge \((2, 5)\) to \(3\). This is correct because: The first elf goes from node \(2\) to node \(3\). This elf's favorite number is \(4\), so he remembers the value \(1\) (as... | Input: 4 6 5 1 2 -1 1 3 1 4 2 7 6 3 0 2 5 -1 2 3 1 2 5 0 5 6 1 6 1 1 4 5 1 5 3 1 2 -1 1 3 -1 1 4 1 4 5 -1 2 4 0 3 4 1 2 3 1 3 3 1 2 -1 1 3 -1 1 2 0 1 3 1 2 3 0 2 1 1 2 1 1 2 0 | Output: YES 1 2 0 1 3 1 2 4 7 3 6 0 2 5 0 YES 1 2 1 1 3 0 1 4 1 4 5 1 NO NO | Hard | 5 | 1,241 | 1,082 | 511 | 16 |
1,442 | E | 1442E | E. Black, White and Grey Tree | 3,000 | binary search; constructive algorithms; dfs and similar; dp; greedy; trees | You are given a tree with each vertex coloured white, black or grey. You can remove elements from the tree by selecting a subset of vertices in a single connected component and removing them and their adjacent edges from the graph. The only restriction is that you are not allowed to select a subset containing a white a... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 100\,000\)), denoting the number of test cases, followed by a description of the test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 200\,000\)): the number of vertices in the tree.The second... | For each test case, print one integer: the minimum number of operations to solve the problem. | In the first test case, both vertices are white, so you can remove them at the same time.In the second test case, three operations are enough. First, we need to remove both black vertices (2 and 4), then separately remove vertices 1 and 3. We can't remove them together because they end up in different connectivity comp... | Input: 4 2 1 1 1 2 4 1 2 1 2 1 2 2 3 3 4 5 1 1 0 1 2 1 2 2 3 3 4 3 5 8 1 2 1 2 2 2 1 2 1 3 2 3 3 4 4 5 5 6 5 7 5 8 | Output: 1 3 2 3 | Master | 6 | 432 | 672 | 93 | 14 |
153 | E | 153E | E. Euclidean Distance | 2,100 | *special | You are given a multiset of points on the plane with integer coordinates. Find the maximum distance between two points from this multiset. | The first line of input contains the number of points n (2 β€ n β€ 50). Next, n pairs of lines follow, each describing a single point: the first line contains x-coordinate, the second one β the y-coordinate ( - 50 β€ x, y β€ 50). Some of the points can have identical coordinates. | Output the maximum distance between two points from this multiset. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 4. | In the first case the maximum distance is between points (0, 1) and (4, 5). In the second case two of the points are the same, so the maximum distance is between one of them and the third point. | Input: 3012345 | Output: 5.656854249 | Hard | 1 | 138 | 276 | 163 | 1 |
1,498 | D | 1498D | D. Bananas in a Microwave | 2,200 | dfs and similar; dp; graphs; implementation | You have a malfunctioning microwave in which you want to put some bananas. You have \(n\) time-steps before the microwave stops working completely. At each time-step, it displays a new operation.Let \(k\) be the number of bananas in the microwave currently. Initially, \(k = 0\). In the \(i\)-th operation, you are given... | The first line contains two space-separated integers \(n\) \((1 \le n \le 200)\) and \(m\) \((2 \le m \le 10^5)\).Then, \(n\) lines follow, where the \(i\)-th line denotes the operation for the \(i\)-th timestep. Each such line contains three space-separated integers \(t_i\), \(x'_i\) and \(y_i\) (\(1 \le t_i \le 2\), ... | Print \(m\) integers, where the \(i\)-th integer is the earliest time-step when you can obtain exactly \(i\) bananas (or \(-1\) if it is impossible). | In the first sample input, let us see how to create \(16\) number of bananas in three timesteps. Initially, \(k=0\). In timestep 1, we choose \(a_1=2\), so we apply the type 1 update β \(k := \lceil(k+3)\rceil\) β two times. Hence, \(k\) is now 6. In timestep 2, we choose \(a_2=0\), hence value of \(k\) remains unchang... | Input: 3 20 1 300000 2 2 400000 2 1 1000000 3 | Output: -1 -1 1 -1 -1 1 -1 -1 -1 3 -1 2 3 -1 -1 3 -1 -1 -1 3 | Hard | 4 | 1,181 | 588 | 149 | 14 |
1,718 | A1 | 1718A1 | A1. Burenka and Traditions (easy version) | 1,800 | dp; greedy | This is the easy version of this problem. The difference between easy and hard versions is only the constraints on \(a_i\) and on \(n\). You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the \(n\)-th queen of the country. There is an a... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β 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 \le n \le 5000\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2,... | For each test case, output a single number β the minimum time that Burenka will need. | In the first test case, Burenka can choose segment \(l = 1\), \(r = 4\), and \(x=5\). so it will fill the array with zeros in \(2\) seconds.In the second test case, Burenka first selects segment \(l = 1\), \(r = 2\), and \(x = 1\), after which \(a = [0, 2, 2]\), and then the segment \(l = 2\), \(r = 3\), and \(x=2\), w... | Input: 745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55 | Output: 2 2 0 2 4 7 4 | Medium | 2 | 1,158 | 462 | 85 | 17 |
1,819 | C | 1819C | C. The Fox and the Complete Tree Traversal | 2,400 | constructive algorithms; dp; implementation; math; trees | The fox Yae climbed the tree of the Sacred Sakura. A tree is a connected undirected graph that does not contain cycles.The fox uses her magical powers to move around the tree. Yae can jump from vertex \(v\) to another vertex \(u\) if and only if the distance between these vertices does not exceed \(2\). In other words,... | The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) βthe number of vertices of the tree.Each of the following \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β vertices connected by an edge. It is guaranteed that these edges form a tree. | On the first line, print ""Yes"" (without quotes) if the required route of the tree exists, or ""No"" (without quotes) otherwise.If the required tree traversal exists, on the second line print \(n\) integers of different integers \(v_1, v_2, \ldots, v_n\) (\(1 \le v_i \le n\)) β the vertices of the tree in traversal or... | The tree from the first example is shown below. The bold arrows indicate the fox's route. In the second example, any sequence of three different vertices is a correct route, because the fox can jump from any vertex to any vertex.The tree from the third example is shown below. It can be shown that there is no required r... | Input: 5 1 2 1 3 3 4 3 5 | Output: Yes 4 5 1 2 3 | Expert | 5 | 929 | 300 | 384 | 18 |
1,174 | F | 1174F | F. Ehab and the Big Finale | 2,400 | constructive algorithms; divide and conquer; graphs; implementation; interactive; trees | This is an interactive problem.You're given a tree consisting of \(n\) nodes, rooted at node \(1\). A tree is a connected graph with no cycles.We chose a hidden node \(x\). In order to find this node, you can ask queries of two types: d \(u\) (\(1 \le u \le n\)). We will answer with the distance between nodes \(u\) and... | The first line contains the integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of nodes in the tree.Each of the next \(n-1\) lines contains two space-separated integers \(u\) and \(v\) (\(1 \le u,v \le n\)) that mean there's an edge between nodes \(u\) and \(v\). It's guaranteed that the given graph is a tree. | To print the answer, print ""! x"" (without quotes). | In the first example, the hidden node is node \(5\). We first ask about the distance between node \(x\) and node \(2\). The answer is \(3\), so node \(x\) is either \(4\) or \(5\). We then ask about the second node in the path from node \(3\) to node \(x\). Note here that node \(3\) is an ancestor of node \(5\). We rec... | Input: 5 1 2 1 3 3 4 3 5 3 5 | Output: d 2 s 3 ! 5 | Expert | 6 | 964 | 318 | 52 | 11 |
1,560 | F2 | 1560F2 | F2. Nearest Beautiful Number (hard version) | 2,100 | bitmasks; brute force; constructive algorithms; dfs and similar; dp; greedy | It is a complicated version of problem F1. The difference between them is the constraints (F1: \(k \le 2\), F2: \(k \le 10\)).You are given an integer \(n\). Find the minimum integer \(x\) such that \(x \ge n\) and the number \(x\) is \(k\)-beautiful.A number is called \(k\)-beautiful if its decimal representation havi... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of one line containing two integers \(n\) and \(k\) (\(1 \le n \le 10^9\), \(1 \le k \le 10\)). | For each test case output on a separate line \(x\) β the minimum \(k\)-beautiful integer such that \(x \ge n\). | Input: 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 | Output: 2021 181111 34533 724542 999999999 12345678 | Hard | 6 | 547 | 241 | 111 | 15 | |
1,477 | F | 1477F | F. Nezzar and Chocolate Bars | 3,500 | combinatorics; fft; math; probabilities | Nezzar buys his favorite snack β \(n\) chocolate bars with lengths \(l_1,l_2,\ldots,l_n\). However, chocolate bars might be too long to store them properly! In order to solve this problem, Nezzar designs an interesting process to divide them into small pieces. Firstly, Nezzar puts all his chocolate bars into a black bo... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 50, 1 \le k \le 2000\)).The second line contains \(n\) integers \(l_1, l_2, \ldots, l_n\) (\(1 \le l_i\), \(\sum_{i=1}^{n} l_i \le 2000\)). | Print a single integer β the expected number of operations Nezzar will perform to divide his chocolate bars into small pieces modulo \(998\,244\,353\). | Input: 1 1 2 | Output: 4 | Master | 4 | 1,120 | 207 | 151 | 14 | |
949 | B | 949B | B. A Leapfrog in the Array | 1,700 | constructive algorithms; math | Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.Let's consider that ini... | The first line contains two integers n and q (1 β€ n β€ 1018, 1 β€ q β€ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.Next q lines contain integers xi (1 β€ xi β€ n), the indices of cells for which it is necessary to output their content after Dima's algorit... | For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. | The first example is shown in the picture.In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | Input: 4 3234 | Output: 324 | Medium | 2 | 961 | 332 | 140 | 9 |
1,980 | F1 | 1980F1 | F1. Field Division (easy version) | 1,900 | data structures; math; sortings | This is an easy version of the problem; it differs from the hard version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.Alice and Bob are dividing the field. The field is a rectangle of size \(n \times m\) (\(2... | 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 three integers \(n\), \(m\), and \(k\) (\(2 \le n, m \le 10^9\), \(2 \le k \le 2 \cdot 10^5\)) β the field sizes and the number of fountains, respectively.Then follow \(k\) lines, e... | For each test case, first output \(\alpha\) β the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output \(k\) non-negative integers \(a_1, a_2, \dots, a_k\), where: \(a_i=0\), if after Bob gives Alice the \(i\)-th fountain, the maximum possible size of Alice's plot... | Below are the images for the second example: The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain \(1\) or fountain \(3\), then that fountain cannot be on Alice's plot. | Input: 52 2 31 11 22 25 5 41 22 23 44 32 5 91 21 51 12 22 42 51 42 31 36 4 46 21 31 41 23 4 52 13 21 41 32 4 | Output: 1 1 0 1 11 0 1 0 1 1 0 0 1 1 0 0 0 0 0 6 1 0 0 0 1 1 1 0 0 0 | Hard | 3 | 1,990 | 636 | 810 | 19 |
1,705 | C | 1705C | C. Mark and His Unfinished Essay | 1,400 | brute force; implementation | One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.More formally, the prompt is a string \(s\) of initial length \(n\). Mark will perform the copy-pasting operation \(c\) times. Each operation ... | The first line contains a single integer \(t\) (\(1\leq t\leq 1000\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(c\), and \(q\) (\(1\leq n\leq 2\cdot 10^5\), \(1\leq c\leq 40\), and \(1\leq q\leq 10^4\)) β the length of the initial string \(s\), the number of copy-pasti... | For each query, print the \(k\)-th letter of the final string \(s\). | In the first test case, the copy-paste process is as follows. The first step is pasting string \(\texttt{mark}\) at the end, yielding the string \(\texttt{mark}\color{red}{\texttt{mark}}\). The second step is pasting string \(\texttt{mar}\) at the end, yielding the string \(\texttt{markmark}\color{red}{\texttt{mar}}\).... | Input: 24 3 3mark1 45 73 8110127 3 3creamii2 33 42 991112 | Output: m a r e a r | Easy | 2 | 711 | 1,081 | 68 | 17 |
1,100 | A | 1100A | A. Roman and Browser | 1,000 | implementation | This morning, Roman woke up and opened the browser with \(n\) opened tabs numbered from \(1\) to \(n\). There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.He decided to accom... | The first line contains two integers \(n\) and \(k\) (\(2 \leq k < n \leq 100\)) β the amount of tabs opened currently and the distance between the tabs closed.The second line consists of \(n\) integers, each of them equal either to \(1\) or to \(-1\). The \(i\)-th integer denotes the type of the \(i\)-th tab: if it is... | Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types \(|e - s|\). | In the first example we can choose \(b = 1\) or \(b = 3\). We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, \(e = 2\) and \(s = 0\) and \(|e - s| = 2\).In the second example, on the contrary, we can leave opened only tabs that have social networks opened in th... | Input: 4 2 1 1 -1 1 | Output: 2 | Beginner | 1 | 1,208 | 437 | 127 | 11 |
1,491 | E | 1491E | E. Fib-tree | 2,400 | brute force; dfs and similar; divide and conquer; number theory; trees | Let \(F_k\) denote the \(k\)-th term of Fibonacci sequence, defined as below: \(F_0 = F_1 = 1\) for any integer \(n \geq 0\), \(F_{n+2} = F_{n+1} + F_n\)You are given a tree with \(n\) vertices. Recall that a tree is a connected undirected graph without cycles.We call a tree a Fib-tree, if its number of vertices equals... | The first line of the input contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of vertices in the tree.Then \(n-1\) lines follow, each of which contains two integers \(u\) and \(v\) (\(1\leq u,v \leq n\), \(u \neq v\)), representing an edge between vertices \(u\) and \(v\). It's guaranteed th... | Print ""YES"" if the given tree is a Fib-tree, or ""NO"" otherwise.You can print your answer in any case. For example, if the answer is ""YES"", then the output ""Yes"" or ""yeS"" will also be considered as correct answer. | In the first sample, we can cut the edge \((1, 2)\), and the tree will be split into \(2\) trees of sizes \(1\) and \(2\) correspondently. Any tree of size \(2\) is a Fib-tree, as it can be split into \(2\) trees of size \(1\).In the second sample, no matter what edge we cut, the tree will be split into \(2\) trees of ... | Input: 3 1 2 2 3 | Output: YES | Expert | 5 | 563 | 347 | 222 | 14 |
990 | B | 990B | B. Micro-World | 1,200 | greedy; sortings | You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.You know that you have \(n\) bacteria in the Petri dish and size of the \(i\)-th bacteria is \(a_i\). Also you know intergalactic positive integ... | The first line contains two space separated positive integers \(n\) and \(K\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le K \le 10^6\)) β number of bacteria and intergalactic constant \(K\).The second line contains \(n\) space separated integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) β sizes of bacteria you have. | Print the only integer β minimal possible number of bacteria can remain. | The first example is clarified in the problem statement.In the second example an optimal possible sequence of swallows is: \([20, 15, 10, 15, \underline{20}, 25]\) \(\to\) \([20, 15, 10, \underline{15}, 25]\) \(\to\) \([20, 15, \underline{10}, 25]\) \(\to\) \([20, \underline{15}, 25]\) \(\to\) \([\underline{20}, 25]\) ... | Input: 7 1101 53 42 102 101 55 54 | Output: 3 | Easy | 2 | 1,327 | 320 | 72 | 9 |
306 | C | 306C | C. White, Black and White Again | 2,100 | combinatorics; number theory | Polycarpus is sure that his life fits the description: ""first there is a white stripe, then a black one, then a white one again"". So, Polycarpus is sure that this rule is going to fulfill during the next n days. Polycarpus knows that he is in for w good events and b not-so-good events. At least one event is going to ... | The single line of the input contains integers n, w and b (3 β€ n β€ 4000, 2 β€ w β€ 4000, 1 β€ b β€ 4000) β the number of days, the number of good events and the number of not-so-good events. It is guaranteed that w + b β₯ n. | Print the required number of ways modulo 1000000009 (109 + 9). | We'll represent the good events by numbers starting from 1 and the not-so-good events β by letters starting from 'a'. Vertical lines separate days.In the first sample the possible ways are: ""1|a|2"" and ""2|a|1"". In the second sample the possible ways are: ""1|a|b|2"", ""2|a|b|1"", ""1|b|a|2"" and ""2|b|a|1"". In the... | Input: 3 2 1 | Output: 2 | Hard | 2 | 1,378 | 219 | 62 | 3 |
1,824 | C | 1824C | C. LuoTianyi and XOR-Tree | 2,500 | data structures; dfs and similar; dp; dsu; greedy; trees | LuoTianyi gives you a tree with values in its vertices, and the root of the tree is vertex \(1\).In one operation, you can change the value in one vertex to any non-negative integer.Now you need to find the minimum number of operations you need to perform to make each path from the root to leaf\(^{\dagger}\) has a bitw... | The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the number of vertices in the tree.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), the \(i\)-th number represents the value in the \(i\)-th vertex.Next \(nβ1\) lines describe the edges of the tree. The \(... | Print a single integer β the minimum number of operations. | The tree in the first example: If we change the value in the vertex \(2\) to \(3\), the value in the vertex \(5\) to \(4\), and the value in the vertex \(6\) to \(6\), then the tree will be ok.The bitwise XOR from the root to the leaf \(2\) will be \(3 \oplus 3=0\).The bitwise XOR from the root to the leaf \(5\) will b... | Input: 6 3 5 7 5 8 4 1 2 1 3 1 4 3 5 4 6 | Output: 3 | Expert | 6 | 440 | 512 | 58 | 18 |
152 | D | 152D | D. Frames | 2,600 | brute force | One day Vasya got hold of a sheet of checkered paper n Γ m squares in size. Our Vasya adores geometrical figures, so he painted two rectangles on the paper. The rectangles' sides are parallel to the coordinates' axes, also the length of each side of each rectangle is no less than 3 squares and the sides are painted by ... | The first input line contains two integers n and m (3 β€ n, m β€ 1000) β the sizes of the sheet of paper Vasya found. Next n lines, each consisting of m symbols ""."" (dot) and ""#"" (number sign), describe the found sheet of paper. The symbol ""#"" represents a hatched square and the symbol ""."" represents a non-hatche... | In the first line print the single word ""YES"" or ""NO"", meaning whether it is true that the found sheet of paper has two frames painted on it. If the answer is positive, then print in the second line 4 integers: the coordinates of the upper left and lower right corners of the first frame. In the third line print 4 i... | In the first sample there are two frames on the picture. The first one is:###..#.#..###.......The second one is:######...##...######In the second sample the painted figures are not frames. Note that the height and width of valid frames is no less than 3. | Input: 4 5######.#.####.###### | Output: YES1 1 3 31 1 4 5 | Expert | 1 | 1,235 | 329 | 461 | 1 |
1,394 | B | 1394B | B. Boboniu Walks on Graph | 2,300 | brute force; dfs and similar; graphs; hashing | Boboniu has a directed graph with \(n\) vertices and \(m\) edges.The out-degree of each vertex is at most \(k\).Each edge has an integer weight between \(1\) and \(m\). No two edges have equal weights.Boboniu likes to walk on the graph with some specific rules, which is represented by a tuple \((c_1,c_2,\ldots,c_k)\). ... | The first line contains three integers \(n\), \(m\) and \(k\) (\(2\le n\le 2\cdot 10^5\), \(2\le m\le \min(2\cdot 10^5,n(n-1) )\), \(1\le k\le 9\)).Each of the next \(m\) lines contains three integers \(u\), \(v\) and \(w\) \((1\le u,v\le n,u\ne v,1\le w\le m)\), denoting an edge from \(u\) to \(v\) with weight \(w\). ... | Print one integer: the number of tuples. | For the first example, there are two tuples: \((1,1,3)\) and \((1,2,3)\). The blue edges in the picture denote the \(c_i\)-th smallest edges for each vertex, which Boboniu chooses to go through. For the third example, there's only one tuple: \((1,2,2,2)\). The out-degree of vertex \(u\) means the number of edges outgoi... | Input: 4 6 3 4 2 1 1 2 2 2 4 3 4 1 4 4 3 5 3 1 6 | Output: 2 | Expert | 4 | 790 | 547 | 40 | 13 |
1,292 | A | 1292A | A. NEKO's Maze Game | 1,400 | data structures; dsu; implementation | 3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#Ξ¦ΟΞ¦ has just got a new maze game on her PC!The game's main puzzle is a maze, in the forms of a \(2 \times n\) rectangle grid. NEKO's task is to lead a Nekomimi girl from cell \((1, 1)\) to the gate at \((2, n)\) and escape the maze. The girl can... | The first line contains integers \(n\), \(q\) (\(2 \le n \le 10^5\), \(1 \le q \le 10^5\)).The \(i\)-th of \(q\) following lines contains two integers \(r_i\), \(c_i\) (\(1 \le r_i \le 2\), \(1 \le c_i \le n\)), denoting the coordinates of the cell to be flipped at the \(i\)-th moment.It is guaranteed that cells \((1, ... | For each moment, if it is possible to travel from cell \((1, 1)\) to cell \((2, n)\), print ""Yes"", otherwise print ""No"". There should be exactly \(q\) answers, one after every update.You can print the words in any case (either lowercase, uppercase or mixed). | We'll crack down the example test here: After the first query, the girl still able to reach the goal. One of the shortest path ways should be: \((1,1) \to (1,2) \to (1,3) \to (1,4) \to (1,5) \to (2,5)\). After the second query, it's impossible to move to the goal, since the farthest cell she could reach is \((1, 3)\). ... | Input: 5 5 2 3 1 4 2 4 2 3 1 4 | Output: Yes No No No Yes | Easy | 3 | 1,131 | 371 | 262 | 12 |
771 | A | 771A | A. Bear and Friendship Condition | 1,500 | dfs and similar; dsu; graphs | Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.Let A-B denote that members A... | The first line of the input contain two integers n and m (3 β€ n β€ 150 000, ) β the number of members and the number of pairs of members that are friends.The i-th of the next m lines contains two distinct integers ai and bi (1 β€ ai, bi β€ n, ai β bi). Members ai and bi are friends with each other. No pair of members will... | If the given network is reasonable, print ""YES"" in a single line (without the quotes). Otherwise, print ""NO"" in a single line (without the quotes). | The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is ""NO"" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. | Input: 4 31 33 41 4 | Output: YES | Medium | 3 | 740 | 356 | 151 | 7 |
909 | B | 909B | B. Segments | 1,300 | constructive algorithms; math | You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be of them.You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints thou... | The only input line contains a single integer N (1 β€ N β€ 100). | Output a single integer - the minimal number of layers required to draw the segments for the given N. | As an example, here are the segments and their optimal arrangement into layers for N = 4. | Input: 2 | Output: 2 | Easy | 2 | 469 | 62 | 101 | 9 |
139 | B | 139B | B. Wallpaper | 1,600 | implementation; math | Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has n rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height).Boris chose... | The first line contains a positive integer n (1 β€ n β€ 500) β the number of rooms in Boris's apartment.Each of the next n lines contains three space-separated positive integers β the length, width and height of the walls in a given room in meters, respectively.The next line contains a positive integer m (1 β€ m β€ 500) β ... | Print a single number β the minimum total cost of the rolls. | Note to the sample:The total length of the walls (the perimeter) of the room is 20 m.One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700.A roll of the second type can be cut into pieces to get five 2 meter wide strips, we ... | Input: 15 5 3310 1 10015 2 3203 19 500 | Output: 640 | Medium | 2 | 1,311 | 660 | 60 | 1 |
39 | I | 39I | I. Tram | 2,500 | In a Berland city S*** there is a tram engine house and only one tram. Three people work in the house β the tram driver, the conductor and the head of the engine house. The tram used to leave the engine house every morning and drove along his loop route. The tram needed exactly c minutes to complete the route. The head... | The first line contains integers n and m (2 β€ n, m β€ 105) β the number of crossroads and tramlines in S*** respectively. The next m lines contain the descriptions of the tramlines in ""u v"" format, where u is the initial tramline crossroad and v is its final crossroad. The crossroads are numbered with integers from 1 ... | In the first line output the value of t. In the next line output the value of k β the required number of the cameras. In the next line output space-separated numbers of the crossroads, where the cameras should be installed. Output the numbers in increasing order. | Input: 4 51 22 33 44 11 4 | Output: 221 3 | Expert | 0 | 2,930 | 376 | 263 | 0 | ||
1,844 | F1 | 1844F1 | F1. Min Cost Permutation (Easy Version) | 2,600 | brute force; constructive algorithms; greedy; math | The only difference between this problem and the hard version is the constraints on \(t\) and \(n\).You are given an array of \(n\) positive integers \(a_1,\dots,a_n\), and a (possibly negative) integer \(c\).Across all permutations \(b_1,\dots,b_n\) of the array \(a_1,\dots,a_n\), consider the minimum possible value o... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(c\) (\(1 \le n \le 5 \cdot 10^3\), \(-10^9 \le c \le 10^9\)).The second line of each test ca... | For each test case, output \(n\) integers \(b_1,\dots,b_n\), the lexicographically smallest permutation of \(a\) that achieves the minimum \(\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|\). | In the first test case, it can be proven that the minimum possible value of \(\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|\) is \(27\), and the permutation \(b = [9,3,1,4,5,1]\) is the lexicographically smallest permutation of \(a\) that achieves this minimum: \(|3-9-(-7)|+|1-3-(-7)|+|4-1-(-7)|+|5-4-(-7)|+|1-5-(-7)| = 1+5+1... | Input: 36 -73 1 4 1 5 93 21 3 51 27182818 | Output: 9 3 1 4 5 1 1 3 5 2818 | Expert | 4 | 755 | 482 | 183 | 18 |
758 | C | 758C | C. Unfair Poll | 1,700 | binary search; constructive algorithms; implementation; math | On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.Seating in the class looks like a rectangle, where n rows with m pupils in each. The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their... | The first and the only line contains five integers n, m, k, x and y (1 β€ n, m β€ 100, 1 β€ k β€ 1018, 1 β€ x β€ n, 1 β€ y β€ m). | Print three integers: the maximum number of questions a particular pupil is asked, the minimum number of questions a particular pupil is asked, how many times the teacher asked Sergei. | The order of asking pupils in the first test: the pupil from the first row who seats at the first table, it means it is Sergei; the pupil from the first row who seats at the second table; the pupil from the first row who seats at the third table; the pupil from the first row who seats at the first table, it means it is... | Input: 1 3 8 1 1 | Output: 3 2 3 | Medium | 4 | 1,316 | 121 | 184 | 7 |
1,270 | E | 1270E | E. Divide Points | 2,300 | constructive algorithms; geometry; math | You are given a set of \(n\ge 2\) pairwise different points with integer coordinates. Your task is to partition these points into two nonempty groups \(A\) and \(B\), such that the following condition holds:For every two points \(P\) and \(Q\), write the Euclidean distance between them on the blackboard: if they belong... | The first line contains one integer \(n\) \((2 \le n \le 10^3)\) β the number of points.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^6 \le x_i, y_i \le 10^6\)) β the coordinates of the \(i\)-th point. It is guaranteed that all \(n\) points are pairwise different. | In the first line, output \(a\) (\(1 \le a \le n-1\)) β the number of points in a group \(A\).In the second line, output \(a\) integers β the indexes of points that you include into group \(A\).If there are multiple answers, print any. | In the first example, we set point \((0, 0)\) to group \(A\) and points \((0, 1)\) and \((1, 0)\) to group \(B\). In this way, we will have \(1\) yellow number \(\sqrt{2}\) and \(2\) blue numbers \(1\) on the blackboard.In the second example, we set points \((0, 1)\) and \((0, -1)\) to group \(A\) and points \((-1, 0)\... | Input: 3 0 0 0 1 1 0 | Output: 1 1 | Expert | 3 | 612 | 304 | 235 | 12 |
573 | C | 573C | C. Bear and Drawing | 2,300 | constructive algorithms; dfs and similar; trees | Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.Recall that tree is a connected graph consisting of n vertices and n - 1 edges.Limak chose a tree with n vertices. He has infinite strip of p... | The first line contains single integer n (1 β€ n β€ 105).Next n - 1 lines contain description of a tree. i-th of them contains two space-separated integers ai and bi (1 β€ ai, bi β€ n, ai β bi) denoting an edge between vertices ai and bi. It's guaranteed that given description forms a tree. | Print ""Yes"" (without the quotes) if Limak can draw chosen tree. Otherwise, print ""No"" (without the quotes). | Input: 81 21 31 66 46 76 57 8 | Output: Yes | Expert | 3 | 636 | 287 | 111 | 5 | |
435 | A | 435A | A. Queue on Bus Stop | 1,000 | implementation | It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.The bus stop queue has n groups of people. The i-th group from the beginning has ai... | The first line contains two integers n and m (1 β€ n, m β€ 100). The next line contains n integers: a1, a2, ..., an (1 β€ ai β€ m). | Print a single integer β the number of buses that is needed to transport all n groups to the dacha countryside. | Input: 4 32 3 2 1 | Output: 3 | Beginner | 1 | 851 | 127 | 111 | 4 | |
1,558 | F | 1558F | F. Strange Sort | 3,300 | data structures; sortings | You have a permutation: an array \(a = [a_1, a_2, \ldots, a_n]\) of distinct integers from \(1\) to \(n\). The length of the permutation \(n\) is odd.Consider the following algorithm of sorting the permutation in increasing order.A helper procedure of the algorithm, \(f(i)\), takes a single argument \(i\) (\(1 \le i \l... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5 - 1\); \(n\) is odd) β the length of the permutation.The second line c... | For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time.If the given permutation is already sorted, print \(0\). | In the first test case, the permutation will be changing as follows: after the \(1\)-st iteration: \([2, 3, 1]\); after the \(2\)-nd iteration: \([2, 1, 3]\); after the \(3\)-rd iteration: \([1, 2, 3]\). In the second test case, the permutation will be changing as follows: after the \(1\)-st iteration: \([4, 5, 1, 7, 2... | Input: 3 3 3 2 1 7 4 5 7 1 3 2 6 5 1 2 3 4 5 | Output: 3 5 0 | Master | 2 | 909 | 522 | 192 | 15 |
1,883 | E | 1883E | E. Look Back | 1,700 | bitmasks; greedy | You are given an array of integers \(a_1, a_2, \ldots, a_n\). You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following: Choose an index \(1 \leq i \leq n\), Set \(a_i = a_i \cdot 2\).An array \(b_1, b_2, \ldots, b_n\) is non-decreasing if \(b_i \leq b_{i+1}\) for ... | 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. This is followed by their description.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the size of the array \(a\).The second line of each t... | For each test case, output the minimum number of operations needed to make the array non-decreasing. | No operations are needed in the first test case.In the second test case, we need to choose \(i = 2\), after which the array will be \([2, 2]\).In the third test case, we can apply the following operations: Choose \(i = 3\), after which the array will be \([3, 2, 2]\), Choose \(i = 3\), after which the array will be \([... | Input: 91122 133 2 147 1 5 3511 2 15 7 1061 8 2 16 8 162624323799 708290323122 1 1 3 3 11 12 22 45 777 777 15001212 11 10 9 8 7 6 5 4 3 2 1 | Output: 0 1 3 6 10 3 0 2 66 | Medium | 2 | 341 | 498 | 100 | 18 |
533 | A | 533A | A. Berland Miners | 3,000 | binary search; data structures; dfs and similar; greedy; trees | The biggest gold mine in Berland consists of n caves, connected by n - 1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions. The mine is being developed by the InMine Inc., k miners work for it. Each day the co... | The first line contains integer n (1 β€ n β€ 5Β·105) β the number of caves in the mine.Then follows a line consisting of n positive integers h1, h2, ..., hn (1 β€ hi β€ 109), where hi is the height of the ceiling in the i-th cave.Next n - 1 lines contain the descriptions of transitions between the caves. Each line has the f... | In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print - 1. If it is initially possible and there's no need to raise any ceiling, print 0. | In the first sample test we should increase ceiling height in the first cave from 5 to 11. After that we can distribute miners as following (first goes index of a miner, then index of a cave): .In the second sample test there is no need to do anything since it is already possible to distribute miners as following: .In ... | Input: 65 8 4 6 3 121 21 34 22 56 367 4 2 5 3 11 | Output: 6 | Master | 5 | 1,582 | 570 | 307 | 5 |
1,775 | A2 | 1775A2 | A2. Gardener and the Capybaras (hard version) | 900 | constructive algorithms; greedy | This is an hard version of the problem. The difference between the versions is that the string can be longer than in the easy version. You can only do hacks if both versions of the problem are passed.Kazimir Kazimirovich is a Martian gardener. He has a huge orchard of binary balanced apple trees.Recently Casimir decide... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of a test case contains the string \(s\) (\(3 \le |s| \le 2 \cdot 10^5\)) β the names of the capybaras, written together. The string consists of ... | For each test case, print three strings \(a\), \(b\) and \(c\) on a single line, separated by spaces β names of capybaras, such that writing them without spaces results in a line \(s\). Either \(a \le b\) and \(c \le b\), or \(b \le a\) and \(b \le c\) must be satisfied.If there are several ways to restore the names, p... | A string \(x\) is lexicographically smaller than a string \(y\) if and only if one of the following holds: \(x\) is a prefix of \(y\), but \(x \ne y\); in the first position where \(x\) and \(y\) differ, the string \(x\) has the letter 'a', and the string \(y\) has the letter 'b'. Now let's move on to the examples.In t... | Input: 5bbbaabaaaaabbaabbb | Output: b bb a a b a a a a ab b a a bb b | Beginner | 2 | 1,814 | 454 | 402 | 17 |
988 | D | 988D | D. Points and Powers of Two | 1,800 | brute force; math | There are \(n\) distinct points on a coordinate line, the coordinate of \(i\)-th point equals to \(x_i\). Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that a... | The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of points.The second line contains \(n\) pairwise distinct integers \(x_1, x_2, \dots, x_n\) (\(-10^9 \le x_i \le 10^9\)) β the coordinates of points. | In the first line print \(m\) β the maximum possible number of points in a subset that satisfies the conditions described above.In the second line print \(m\) integers β the coordinates of points in the subset you have chosen.If there are multiple answers, print any of them. | In the first example the answer is \([7, 3, 5]\). Note, that \(|7-3|=4=2^2\), \(|7-5|=2=2^1\) and \(|3-5|=2=2^1\). You can't find a subset having more points satisfying the required property. | Input: 63 5 4 7 10 12 | Output: 37 3 5 | Medium | 2 | 756 | 236 | 275 | 9 |
666 | C | 666C | C. Codeword | 2,500 | combinatorics; strings | The famous sculptor Cicasso is a Reberlandian spy!These is breaking news in Berlandian papers today. And now the sculptor is hiding. This time you give the shelter to the maestro. You have a protected bunker and you provide it to your friend. You set the security system in such way that only you can open the bunker. To... | The first line contains integer m (1 β€ m β€ 105) β the number of the events in the test case.The second line contains nonempty string s β the string generated by the bunker for the current day.The next m lines contain the description of the events. The description starts from integer t β the type of the event.If t = 1 c... | For each query of the type 2 print the answer modulo 109 + 7 on the separate line. | In the first event words of the form ""a?"" and ""?a"" are counted, where ? is an arbitrary symbol. There are 26 words of each of these types, but the word ""aa"" satisfies both patterns, so the answer is 51. | Input: 3a2 21 bc2 5 | Output: 51162626 | Expert | 2 | 1,116 | 711 | 82 | 6 |
1,557 | B | 1557B | B. Moamen and k-subarrays | 1,100 | greedy; sortings | Moamen has an array of \(n\) distinct integers. He wants to sort that array in non-decreasing order by doing the following operations in order exactly once: Split the array into exactly \(k\) non-empty subarrays such that each element belongs to exactly one subarray. Reorder these subarrays arbitrary. Merge the subarra... | The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le |a_i|... | For each test case, you should output a single string.If Moamen can sort the array in non-decreasing order, output ""YES"" (without quotes). Otherwise, output ""NO"" (without quotes).You can print each letter of ""YES"" and ""NO"" in any case (upper or lower). | In the first test case, \(a = [6, 3, 4, 2, 1]\), and \(k = 4\), so we can do the operations as follows: Split \(a\) into \(\{ [6], [3, 4], [2], [1] \}\). Reorder them: \(\{ [1], [2], [3,4], [6] \}\). Merge them: \([1, 2, 3, 4, 6]\), so now the array is sorted. In the second test case, there is no way to sort the array ... | Input: 3 5 4 6 3 4 2 1 4 2 1 -4 0 -2 5 1 1 2 3 4 5 | Output: Yes No Yes | Easy | 2 | 678 | 471 | 260 | 15 |
1,704 | H1 | 1704H1 | H1. Game of AI (easy version) | 3,200 | combinatorics; constructive algorithms; dp; fft; math | This is the easy version of this problem. The difference between easy and hard versions is the constraint on \(k\) and the time limit. Also, in this version of the problem, you only need to calculate the answer when \(n=k\). You can make hacks only if both versions of the problem are solved.Cirno is playing a war simul... | The only line contains two positive integers \(k\) and \(M\) (\(1\le k\le 5000\), \(2\le M\le 10^9\) ). It is guaranteed that \(2^{18}\) is a divisor of \(M-1\) and \(M\) is a prime number.You need to calculate the answer for \(n=k\). | Output a single integer β the number of different pairs of sequences for \(n=k\) modulo \(M\). | For \(n=1\), no valid sequence \(a\) exists. We regard the answer as \(0\).For \(n=2\), there is only one possible array \(a\): \([2, 1]\). For array \(a\) is \([2, 1]\) and permutation \(p\) is \([1, 2]\), the sequence \(b\) will be \([1, 1]\) after all rounds have finished. The details for each rounds: In the first r... | Input: 1 998244353 | Output: 0 | Master | 5 | 1,972 | 234 | 94 | 17 |
1,986 | D | 1986D | D. Mathematical Problem | 1,400 | brute force; dp; greedy; implementation; math; two pointers | You are given a string \(s\) of length \(n > 1\), consisting of digits from \(0\) to \(9\). You must insert exactly \(n - 2\) symbols \(+\) (addition) or \(\times\) (multiplication) into this string to form a valid arithmetic expression.In this problem, the symbols cannot be placed before the first or after the last ch... | 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. Then follows their description.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 20\)) β the length of the string \(s\).The second line of each t... | For each test case, output the minimum result of the arithmetic expression that can be obtained by inserting exactly \(n - 2\) addition or multiplication symbols into the given string. | In the first four test cases, we cannot add symbols, so the answer will be the original number.In the fifth test case, the optimal answer looks as follows: \(9 \times 01 = 9 \times 1 = 9\).In the sixth test case, the optimal answer looks as follows: \(1 \times 01 = 1 \times 1 = 1\).In the seventh test case, the optimal... | Input: 182102742002013901310152331169870097111111120999999999999999999992000000000000000000000402121805723528362134539541112201981167848732178412141121422213011 | Output: 10 74 0 1 9 1 19 0 11 261 0 0 0 12 93 12 24 0 | Easy | 6 | 1,349 | 411 | 184 | 19 |
1,854 | B | 1854B | B. Earn or Unlock | 2,200 | bitmasks; brute force; dp | Andrea is playing the game Tanto Cuore. He has a deck of \(n\) cards with values \(a_1, \ldots, a_n\) from top to bottom. Each card can be either locked or unlocked. Initially, only the topmost card is unlocked. The game proceeds in turns. In each turn, Andrea chooses an unlocked card in the deck β the value written on... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of cards in the deck.The second line contains \(n\) integers \(a_1, \ldots, a_n\) (\(0 \leq a_1, \ldots, a_n \leq n\)) β the values of the cards in the deck. | Output a single integer β the maximum number of victory points Andrea can earn. | In the first sample test, the deck starts as [unlocked, locked]. Then, Andrea uses the first card to unlock the second card. Then he uses the second card to earn \(2\) victory points.In the second sample test, Andrea can use the first card to unlock the second and the third card. Then he uses the second and the third c... | Input: 2 1 2 | Output: 2 | Hard | 3 | 825 | 242 | 79 | 18 |
772 | A | 772A | A. Voltage Keepsake | 1,800 | binary search; math | You have n devices that you want to use simultaneously.The i-th device uses ai units of power per second. This usage is continuous. That is, in Ξ» seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power stored. All devices can store an arbitrary amount of power.You have a single... | The first line contains two integers, n and p (1 β€ n β€ 100 000, 1 β€ p β€ 109) β the number of devices and the power of the charger.This is followed by n lines which contain two integers each. Line i contains the integers ai and bi (1 β€ ai, bi β€ 100 000) β the power of the device and the amount of power stored in the dev... | If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.Namely, let's assume that your answer is a and the answer of the jury is b. The checker progra... | In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.In sample test 2, you can use the device indefinitely.In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge t... | Input: 2 12 22 1000 | Output: 2.0000000000 | Medium | 2 | 927 | 341 | 360 | 7 |
1,997 | F | 1997F | F. Chips on a Line | 2,700 | brute force; combinatorics; dp; greedy; math | You have \(n\) chips, and you are going to place all of them in one of \(x\) points, numbered from \(1\) to \(x\). There can be multiple chips in each point.After placing the chips, you can perform the following four operations (in any order, any number of times): choose a chip in point \(i \ge 3\), remove it and place... | The only line contains three integers \(n\), \(x\) and \(m\) (\(1 \le m \le n \le 1000\); \(2 \le x \le 10\)). | Print one integer β the number of placements with cost equal to \(m\), taken modulo \(998244353\). | In the first example, there are five ways to place \(2\) chips in points from \(1\) to \(3\) so that the cost is \(1\): \((1, 1)\); \((1, 2)\); \((1, 3)\); \((2, 2)\); \((2, 3)\). | Input: 2 3 1 | Output: 5 | Master | 5 | 1,601 | 110 | 98 | 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.