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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,736 | C1 | 1736C1 | C1. Good Subarrays (Easy Version) | 1,300 | binary search; data structures; schedules; two pointers | This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.An array \(b\) of length \(m\) is good if for all \(i\) the \(i\)-th element is greater than or equal to \(i\). In ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2 \cdot 10^5\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)), the length of the array \(a\).The second line of each test c... | For each test case, print the number of suitable pairs of indices. | In the first test case, all subarrays of \(a\) are good, so all pairs are suitable.In the second test case, the pairs \((1, 1)\), \((2, 2)\), and \((3, 3)\) are suitable. For example, when \((l, r) = (1, 2)\), the array \(b=[1,1]\) is not good because \(b_2 < 2\). | Input: 331 2 331 1 142 1 4 3 | Output: 6 3 7 | Easy | 4 | 620 | 533 | 66 | 17 |
1,958 | G | 1958G | G. Observation Towers | 2,400 | *special | Consider a segment of an OX axis from \(1\) to \(n\). There are \(k\) observation towers on this segment.Each tower has two parameters β the coordinate \(x_i\) and the height \(h_i\). The coordinates of all towers are distinct. From tower \(i\), you can see point \(j\) if \(|x_i - j| \le h_i\) (where \(|a|\) is an abso... | The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le k \le n\)) β the length of the segment of an OX axis and the number of observation towers.The second line contains \(k\) integers \(x_1, x_2, \dots, x_k\) (\(1 \le x_i \le n\)) β the coordinates of the observation towers. The co... | For each query, output a single integer β the minimum number of coins required to be able to see both points (from one tower or from different towers). | Input: 20 32 15 106 2 031 2010 113 4 | Output: 3 1 0 | Expert | 1 | 742 | 713 | 151 | 19 | |
711 | C | 711C | C. Coloring Trees | 1,700 | dp | ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m diffe... | The first line contains three integers, n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100) β the number of trees, number of colors and beauty of the resulting coloring respectively.The second line contains n integers c1, c2, ..., cn (0 β€ ci β€ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored,... | Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print - 1. | In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).In the second sample cas... | Input: 3 2 20 0 01 23 45 6 | Output: 10 | Medium | 1 | 1,420 | 657 | 139 | 7 |
1,552 | D | 1552D | D. Array Differentiation | 1,800 | bitmasks; brute force; constructive algorithms; dfs and similar; dp; graphs; math | You are given a sequence of \(n\) integers \(a_1, \, a_2, \, \dots, \, a_n\).Does there exist a sequence of \(n\) integers \(b_1, \, b_2, \, \dots, \, b_n\) such that the following property holds? For each \(1 \le i \le n\), there exist two (not necessarily distinct) indices \(j\) and \(k\) (\(1 \le j, \, k \le n\)) su... | The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10\)).The second line of each test case contains the \(n\) integers \(a_1, \, \dots, \, a_n\) (\(-10^5 \le a_i \le 10^5\... | For each test case, output a line containing YES if a sequence \(b_1, \, \dots, \, b_n\) satisfying the required property exists, and NO otherwise. | In the first test case, the sequence \(b = [-9, \, 2, \, 1, \, 3, \, -2]\) satisfies the property. Indeed, the following holds: \(a_1 = 4 = 2 - (-2) = b_2 - b_5\); \(a_2 = -7 = -9 - (-2) = b_1 - b_5\); \(a_3 = -1 = 1 - 2 = b_3 - b_2\); \(a_4 = 5 = 3 - (-2) = b_4 - b_5\); \(a_5 = 10 = 1 - (-9) = b_3 - b_1\). In the seco... | Input: 5 5 4 -7 -1 5 10 1 0 3 1 10 100 4 -3 2 10 2 9 25 -171 250 174 152 242 100 -205 -258 | Output: YES YES NO YES YES | Medium | 7 | 348 | 323 | 147 | 15 |
1,156 | C | 1156C | C. Match Points | 2,000 | binary search; greedy; sortings; ternary search; two pointers | You are given a set of points \(x_1\), \(x_2\), ..., \(x_n\) on the number line.Two points \(i\) and \(j\) can be matched with each other if the following conditions hold: neither \(i\) nor \(j\) is matched with any other point; \(|x_i - x_j| \ge z\). What is the maximum number of pairs of points you can match with eac... | The first line contains two integers \(n\) and \(z\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le z \le 10^9\)) β the number of points and the constraint on the distance between matched points, respectively.The second line contains \(n\) integers \(x_1\), \(x_2\), ..., \(x_n\) (\(1 \le x_i \le 10^9\)). | Print one integer β the maximum number of pairs of points you can match with each other. | In the first example, you may match point \(1\) with point \(2\) (\(|3 - 1| \ge 2\)), and point \(3\) with point \(4\) (\(|7 - 3| \ge 2\)).In the second example, you may match point \(1\) with point \(3\) (\(|5 - 10| \ge 5\)). | Input: 4 2 1 3 3 7 | Output: 2 | Hard | 5 | 328 | 297 | 88 | 11 |
961 | B | 961B | B. Lecture Sleep | 1,200 | data structures; dp; implementation; two pointers | Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th min... | The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th mi... | Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up. | In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | Input: 6 31 3 5 2 5 41 1 0 1 0 0 | Output: 16 | Easy | 4 | 993 | 470 | 141 | 9 |
1,113 | B | 1113B | B. Sasha and Magnetic Machines | 1,300 | greedy; number theory | One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by \(n\) machines, and the power of the \(i\)-th machine is \(a_i\). This year 2D decided to cultivate a new culture, but what ex... | The first line contains one integer \(n\) (\(2 \le n \le 5 \cdot 10^4\)) β the number of machines.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) β the powers of the machines. | Print one integer β minimum total power. | In the first example, the farmer can reduce the power of the \(4\)-th machine by \(2\) times, and increase the power of the \(1\)-st machine by \(2\) times, then the powers will be: \([2, 2, 3, 2, 5]\).In the second example, the farmer can reduce the power of the \(3\)-rd machine by \(2\) times, and increase the power ... | Input: 5 1 2 3 4 5 | Output: 14 | Easy | 2 | 1,143 | 217 | 40 | 11 |
100 | A | 100A | A. Carpeting the Room | 1,100 | *special; implementation | Soroush's room is a square with side length n. Before this contest he bought k fine Persian carpets to carpet his room for celebrating the 100th contest on his favorite site. Each Persian carpet is a square of side length n1.Soroush wants to cover all the area of his room. Carpets can be put over each other but it is n... | The input consists of three integer numbers n, k and n1 (10 β€ n β€ 12, 1 β€ k β€ 10, ). | Write a single YES or NO. Write YES if and only if Sorush can carpet his room completely. | Input: 10 4 6 | Output: YES | Easy | 2 | 393 | 84 | 89 | 1 | |
1,950 | A | 1950A | A. Stair, Peak, or Neither? | 800 | implementation | You are given three digits \(a\), \(b\), and \(c\). Determine whether they form a stair, a peak, or neither. A stair satisfies the condition \(a<b<c\). A peak satisfies the condition \(a<b>c\). | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The only line of each test case contains three digits \(a\), \(b\), \(c\) (\(0 \leq a\), \(b\), \(c \leq 9\)). | For each test case, output ""STAIR"" if the digits form a stair, ""PEAK"" if the digits form a peak, and ""NONE"" otherwise (output the strings without quotes). | Input: 71 2 33 2 11 5 33 4 10 0 04 1 74 5 7 | Output: STAIR NONE PEAK PEAK NONE NONE STAIR | Beginner | 1 | 193 | 209 | 160 | 19 | |
144 | D | 144D | D. Missile Silos | 1,900 | data structures; dfs and similar; graphs; shortest paths | A country called Berland consists of n cities, numbered with integer numbers from 1 to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missile... | The first line contains three integers n, m and s (2 β€ n β€ 105, , 1 β€ s β€ n) β the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. s. Then m lines contain the descriptions of roads. Each of them is described by three integers vi, ui, wi (1 β€ v... | Print the single number β the number of Super Duper Secret Missile Silos that are located in Berland. | In the first sample the silos are located in cities 3 and 4 and on road (1, 3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3).In the second sample one missile silo is located right in the middle of the road (1, 2). Two more silos are on the road (4, 5) at a distance 3 from city 4 in the dire... | Input: 4 6 11 2 11 3 32 3 12 4 13 4 11 4 22 | Output: 3 | Hard | 4 | 966 | 736 | 101 | 1 |
264 | A | 264A | A. Escape from Stones | 1,200 | constructive algorithms; data structures; implementation; two pointers | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interva... | The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either ""l"" or ""r"". | Output n lines β on the i-th line you should print the i-th stone's number from the left. | In the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Input: llrlr | Output: 35421 | Easy | 4 | 824 | 140 | 89 | 2 |
1,119 | D | 1119D | D. Frets On Fire | 1,800 | binary search; sortings | Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.In return, the fleas made a bigger ukulele for her: it has \(n\) strings, and each string has \((10^{18} + 1)\) frets numerated from \(0\) to \(10^{18}\). The fleas use the arr... | The first line contains an integer \(n\) (\(1 \leq n \leq 100\,000\)) β the number of strings.The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(0 \leq s_i \leq 10^{18}\)) β the tuning of the ukulele.The third line contains an integer \(q\) (\(1 \leq q \leq 100\,000\)) β the number of questions.The \(... | Output one number for each question, separated by spaces β the number of different pitches. | For the first example, the pitches on the \(6\) strings are as follows.$$$\( \begin{matrix} \textbf{Fret} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \ldots \\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \dots \\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \dots \\ s... | Input: 63 1 4 1 5 937 70 28 17 | Output: 5 10 18 | Medium | 2 | 1,221 | 464 | 91 | 11 |
1,090 | D | 1090D | D. Similar Arrays | 1,800 | constructive algorithms | Vasya had an array of \(n\) integers, each element of the array was from \(1\) to \(n\). He chose \(m\) pairs of different positions and wrote them down to a sheet of paper. Then Vasya compared the elements at these positions, and wrote down the results of the comparisons to another sheet of paper. For each pair he wro... | The first line of input contains two integers \(n\), \(m\) β the number of elements in the array and number of comparisons made by Vasya (\(1 \le n \le 100\,000\), \(0 \le m \le 100\,000\)).Each of the following \(m\) lines contains two integers \(a_i\), \(b_i\) β the positions of the \(i\)-th comparison (\(1 \le a_i, ... | The first line of output must contain ""YES"" if there exist two arrays, such that the results of comparisons would be the same, and all numbers in the first one are distinct, and the second one contains two equal numbers. Otherwise it must contain ""NO"".If the arrays exist, the second line must contain the array of d... | Input: 1 0 | Output: NO | Medium | 1 | 1,308 | 426 | 488 | 10 | |
1,906 | G | 1906G | G. Grid Game 2 | 2,900 | games; number theory | You are playing ""Grid Game 2"" with your friend. There is a grid with \(10^9\) rows (numbered from \(1\) to \(10^9\)) and \(10^9\) columns (numbered from \(1\) to \(10^9\)). The cell at row \(r\) and column \(c\) is denoted as \((r, c)\).Each cell can have a colour of either black or white. Initially, there are exactl... | The first line consists of an integer \(N\) (\(1 \le N \le 200\,000\)).Each of the next \(N\) lines consists of two integers \(R_i\) \(C_i\) (\(1 \leq R_i, C_i \leq 10^9)\). For \(1 \leq i < j \leq N\), \((R_i, C_i) \neq (R_j, C_j)\). | Output FIRST if you will win the game, or SECOND otherwise. | Explanation for the sample input/output #1You can start your move by choosing \((2, 4)\), whose effect was demonstrated in the following illustration. The remaining black cells are \((1, 3)\) and \((1, 4)\), each of which will only toggle itself when chosen. Whichever your friend chooses on the next move, the you can c... | Input: 2 2 3 2 4 | Output: FIRST | Master | 2 | 1,156 | 234 | 59 | 19 |
1,580 | F | 1580F | F. Problems for Codeforces | 3,300 | combinatorics; fft; math | XYMXYM and CQXYM will prepare \(n\) problems for Codeforces. The difficulty of the problem \(i\) will be an integer \(a_i\), where \(a_i \geq 0\). The difficulty of the problems must satisfy \(a_i+a_{i+1}<m\) (\(1 \leq i < n\)), and \(a_1+a_n<m\), where \(m\) is a fixed integer. XYMXYM wants to know how many plans of t... | A single line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 50\,000\), \(1 \leq m \leq 10^9\)). | Print a single integer β the number of different plans. | In the first test case, the valid \(a\) are: \([0,0,0]\), \([0,0,1]\), \([0,1,0]\), \([1,0,0]\).\([1,0,1]\) is invalid since \(a_1+a_n \geq m\). | Input: 3 2 | Output: 4 | Master | 3 | 523 | 104 | 55 | 15 |
1,691 | E | 1691E | E. Number of Groups | 2,300 | data structures; dfs and similar; dsu; graphs; greedy; sortings | You are given \(n\) colored segments on the number line. Each segment is either colored red or blue. The \(i\)-th segment can be represented by a tuple \((c_i, l_i, r_i)\). The segment contains all the points in the range \([l_i, r_i]\), inclusive, and its color denoted by \(c_i\): if \(c_i = 0\), it is a red segment; ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of segments. Each of the next \(n\) lines contains three int... | For each test case, print a single integer \(k\), the number of groups of segments. | In the first example there are \(5\) segments. The segments \(1\) and \(2\) are connected, because they are of different colors and share a point. Also, the segments \(2\) and \(3\) are connected, and so are segments \(4\) and \(5\). Thus, there are two groups: one containing segments \(\{1, 2, 3\}\), and the other one... | Input: 250 0 51 2 120 4 71 9 160 13 1931 0 11 1 20 3 4 | Output: 2 3 | Expert | 6 | 629 | 517 | 83 | 16 |
1,617 | E | 1617E | E. Christmas Chocolates | 2,700 | dfs and similar; dp; games; graphs; implementation; math; number theory; shortest paths; trees | Christmas is coming, Icy has just received a box of chocolates from her grandparents! The box contains \(n\) chocolates. The \(i\)-th chocolate has a non-negative integer type \(a_i\).Icy believes that good things come in pairs. Unfortunately, all types of chocolates are distinct (all \(a_i\) are distinct). Icy wants t... | The first line of the input contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of chocolates.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^9\)).It is guaranteed that all \(a_i\) are distinct. | Output three integers \(x\), \(y\), and \(m\).\(x\) and \(y\) are indices of the optimal chocolates to perform exchanges on. Your output must satisfy \(1 \le x, y \le n\), \(x \ne y\).\(m\) is the number of exchanges needed to obtain \(a_x = a_y\). We can show that \(m \le 10^9\) for any pair of chocolates.If there are... | In the first test case, the minimum number of exchanges needed to exchange a chocolate of type \(6\) to a chocolate of type \(9\) is \(5\). The sequence of exchanges is as follows: \(6 \rightarrow 2 \rightarrow 0 \rightarrow 1 \rightarrow 7 \rightarrow 9\).In the second test case, the minimum number of exchanges needed... | Input: 5 5 6 7 8 9 | Output: 2 5 5 | Master | 9 | 1,448 | 268 | 352 | 16 |
228 | D | 228D | D. Zigzag | 2,100 | data structures | The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.The Zigag's sequence with the z... | The first line contains integer n (1 β€ n β€ 105) β The number of elements in array a. The second line contains n space-separated integers: a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array. The third line contains integer m (1 β€ m β€ 105) β the number of operations. Next m lines contain the operations' descripti... | For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d spec... | Explanation of the sample test: Result of the first operation is Z(2, 3, 2) = 3Β·1 + 1Β·2 = 5. Result of the second operation is Z(1, 5, 3) = 2Β·1 + 3Β·2 + 1Β·3 + 5Β·2 + 5Β·1 = 26. After the third operation array a is equal to 2, 3, 5, 5, 5. Result of the forth operation is Z(1, 5, 3) = 2Β·1 + 3Β·2 + 5Β·3 + 5Β·2 + 5Β·1 = 38. | Input: 52 3 1 5 542 2 3 22 1 5 31 3 52 1 5 3 | Output: 52638 | Hard | 1 | 1,415 | 831 | 326 | 2 |
833 | A | 833A | A. The Meaningless Game | 1,700 | math; number theory | Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is mul... | In the first string, the number of games n (1 β€ n β€ 350000) is given.Each game is represented by a pair of scores a, b (1 β€ a, b β€ 109) β the results of Slastyona and Pushok, correspondingly. | For each pair of scores, answer ""Yes"" if it's possible for a game to finish with given score, and ""No"" otherwise.You can output each letter in arbitrary case (upper or lower). | First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | Input: 62 475 458 816 16247 9941000000000 1000000 | Output: YesYesYesNoNoYes | Medium | 2 | 835 | 191 | 179 | 8 |
1,090 | A | 1090A | A. Company Merging | 1,300 | greedy | A conglomerate consists of \(n\) companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.But anti-monopol... | The first line contains a single integer \(n\) β the number of companies in the conglomerate (\(1 \le n \le 2 \cdot 10^5\)). Each of the next \(n\) lines describes a company. A company description start with an integer \(m_i\) β the number of its employees (\(1 \le m_i \le 2 \cdot 10^5\)). Then \(m_i\) integers follow:... | Output a single integer β the minimal total increase of all employees that allows to merge all companies. | One of the optimal merging strategies is the following. First increase all salaries in the second company by \(2\), and merge the first and the second companies. Now the conglomerate consists of two companies with salaries \([4, 3, 4, 3]\) and \([1, 1, 1]\). To merge them, increase the salaries in the second of those b... | Input: 3 2 4 3 2 2 1 3 1 1 1 | Output: 13 | Easy | 1 | 992 | 486 | 105 | 10 |
2,117 | C | 2117C | C. Cool Partition | 1,200 | data structures; greedy | Yousef has an array \(a\) of size \(n\). He wants to partition the array into one or more contiguous segments such that each element \(a_i\) belongs to exactly one segment.A partition is called cool if, for every segment \(b_j\), all elements in \(b_j\) also appear in \(b_{j + 1}\) (if it exists). That is, every elemen... | The first line of the input contains integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) ... | For each test case, print one integer β the maximum number of segments that make a cool partition. | The first test case is explained in the statement. We can partition it into \(b_1 = [1, 2]\), \(b_2 = [2, 3, 1, 5]\). It can be shown there is no other partition with more segments.In the second test case, we can partition the array into \(b_1 = [1, 2]\), \(b_2 = [1, 3, 2]\), \(b_3 = [1, 3, 2]\). The maximum number of ... | Input: 861 2 2 3 1 581 2 1 3 2 1 3 255 4 3 2 1105 8 7 5 8 5 7 8 10 931 2 293 3 1 4 3 2 4 1 264 5 4 5 6 481 2 1 2 1 2 1 2 | Output: 2 3 1 3 1 3 3 4 | Easy | 2 | 1,060 | 439 | 98 | 21 |
1,910 | J | 1910J | J. Two Colors | 2,900 | *special | You are given a tree consisting of \(n\) vertices. Some vertices of the tree are red, all other vertices are blue.Each edge of the tree has a positive weight. Let's define \(d(x, y)\) as the distance between the vertices \(x\) and \(y\), i. e. the sum of weights of edges belonging to the simple path between \(x\) and \... | The first line contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le 1\)). If the \(i\)-th vertex is red, \(c_i = 1\), otherwise \(c_i = 0\).Then \(n-1\) lines follow. Each line contains three integers \(x_i\), \(y_i\) and \(w_i\) (\(... | If the value of \(\sum \limits_{i=1}^{n} v_i\) can be as big as possible, print Infinity. Otherwise, print one integer β the maximum possible value of \(\sum \limits_{i=1}^{n} v_i\) you can get. | In the first example, you can assign \(v_1 = 120, v_2 = 20, v_3 = 80, v_4 = 130\). | Input: 4 1 1 0 0 3 4 50 3 2 100 2 1 100 | Output: 350 | Master | 1 | 643 | 493 | 194 | 19 |
2,049 | D | 2049D | D. Shift + Esc | 1,900 | brute force; dp | After having fun with a certain contraption and getting caught, Evirir the dragon decides to put their magical skills to good use β warping reality to escape fast!You are given a grid with \(n\) rows and \(m\) columns of non-negative integers and an integer \(k\). Let \((i, j)\) denote the cell in the \(i\)-th row from... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line contains three space-separated integers \(n\), \(m\), and \(k\) (\(1 \leq n, m \leq 200\), \(0 \leq k \leq 10^9\)).Then, \(n\) lines follow. Th... | For each test case, output a single integer, the minimum cost to move from \((1, 1)\) to \((n, m)\). | In the first test case, the minimum cost of \(113\) can be achieved as follows: Cyclically shift row 3 once. The grid now becomes $$$\(\begin{bmatrix}3 & 4 & 9\\5 & 2 & 4\\101 & 101 & 0\end{bmatrix}.\)\( Move as follows: \)(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (3, 3)\(. \)x = 1\( operation is done before moving. ... | Input: 53 3 1003 4 95 2 40 101 1013 4 110 0 0 100 0 10 010 10 0 101 1 343 2 31 23 65 410 10 1458 49 25 12 89 69 8 49 71 2345 27 65 59 36 100 73 23 5 8482 91 54 92 53 15 43 46 11 6561 69 71 87 67 72 51 42 55 801 64 8 54 61 70 47 100 84 5086 93 43 51 47 35 56 20 33 61100 59 5 68 15 55 69 8 8 6033 61 20 79 69 51 23 24 56 ... | Hard | 2 | 1,383 | 549 | 100 | 20 |
554 | B | 554B | B. Ohana Cleans Up | 1,200 | brute force; greedy; strings | Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it ... | The first line of input will be a single integer n (1 β€ n β€ 100).The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' ... | The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean. | In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.In the second sample, everything is already clean, so Ohana doesn't need to do anything. | Input: 40101100011110101 | Output: 2 | Easy | 3 | 604 | 335 | 126 | 5 |
680 | B | 680B | B. Bear and Finding Criminals | 1,000 | constructive algorithms; implementation | There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there ... | The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city. | Print the number of criminals Limak will catch. | In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red. Using the BCD gives Limak the following information: There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city. There is one cr... | Input: 6 31 1 1 0 1 0 | Output: 3 | Beginner | 2 | 711 | 245 | 47 | 6 |
435 | B | 435B | B. Pasha Maximizes | 1,400 | greedy | Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.Help Pasha count the maximum number he can get if he has the time to make at most k swap... | The single line contains two integers a and k (1 β€ a β€ 1018; 0 β€ k β€ 100). | Print the maximum number that Pasha can get if he makes at most k swaps. | Input: 1990 1 | Output: 9190 | Easy | 1 | 322 | 74 | 72 | 4 | |
464 | C | 464C | C. Substitutes in Number | 2,100 | dp | Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type ""di β ti"", that means ""replace all digits di in string s with substrings equal to ti"". For example, if s = 123123, then query ""2 β 00"" transforms s to 10031003, and query ""3 β ... | The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.The next n lines contain the descriptions of the queries. The i-th query is described by string ""di->ti"", where di is... | Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7). | Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | Input: 12312312->00 | Output: 10031003 | Hard | 1 | 805 | 552 | 95 | 4 |
1,475 | B | 1475B | B. New Year's Number | 900 | brute force; dp; math | Polycarp remembered the \(2020\)-th year, and he is happy with the arrival of the new \(2021\)-th year. To remember such a wonderful moment, Polycarp wants to represent the number \(n\) as the sum of a certain number of \(2020\) and a certain number of \(2021\).For example, if: \(n=4041\), then the number \(n\) can be ... | The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case contains one integer \(n\) (\(1 \leq n \leq 10^6\)) β the number that Polycarp wants to represent as the sum of the numbers \(2020\) and \(2021\). | For each test case, output on a separate line: ""YES"" if the number \(n\) is representable as the sum of a certain number of \(2020\) and a certain number of \(2021\); ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). | Input: 5 1 4041 4042 8081 8079 | Output: NO YES YES YES NO | Beginner | 3 | 803 | 285 | 313 | 14 | |
1,918 | G | 1918G | G. Permutation of Given | 2,700 | constructive algorithms; math | You were given only one number, \(n\). It didn't seem interesting to you, so you wondered if it's possible to come up with an array of length \(n\) consisting of non-zero integers, such that if each element of the array is replaced by the sum of its neighbors (the elements on the ends are replaced by their only neighbo... | Each test case contains only one number, \(n\) (\(2 \leq n \leq 10^6\)). | If a solution exists, output ""YES"" (without quotes), followed by an array \(a\) (\(-10^9 \leq a_i \leq 10^9, a_i \neq 0\)) that satisfies the condition of the problem. If there are multiple possible answers, output any of them.If there is no suitable array, output ""NO"" (without quotes).The words ""YES"" and ""NO"" ... | In the first test, the array [\(1, 2, -2, -1\)] is suitable, because if each element is replaced by the sum of its neighbors, the resulting array is [\(2, -1, 1, -2\)], which is a permutation of the original array.In the second test, it can be shown that there is no solution. | Input: 4 | Output: YES 1 2 -2 -1 | Master | 2 | 387 | 72 | 397 | 19 |
530 | I | 530I | I. Different variables | 2,500 | *special | N variables X1, ..., XN can have positive integer values. You are given K constraints for these value that look like ""the values of variables Xi1, Xi2, ..., XiM are different"". Among all possible lists of values of these variables that satisfy these constraints select the ones which have minimum possible max(Xi). Out... | The first line of input contains two integers n and k (2 β€ N β€ 10, 1 β€ K β€ 100) β the number of variables and the number of constraints.The following K lines contain the constraints, formatted as follows: the first number in the line M (2 β€ M β€ N) gives the number of variables in the constraint. After it follow M space... | Output the values of X1, X2, ..., XN in a single line, separated with single spaces. | Input: 2 12 1 2 | Output: 1 2 | Expert | 1 | 363 | 459 | 84 | 5 | |
1,111 | A | 1111A | A. Superhero Transformation | 1,000 | implementation; strings | We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name \(s\) can transform to another superhero with name \(t\) if \(s\) can be made equal to \(t\) by changing any vowel in \(s\) to any other vowel and any consonant in... | The first line contains the string \(s\) having length between \(1\) and \(1000\), inclusive.The second line contains the string \(t\) having length between \(1\) and \(1000\), inclusive.Both strings \(s\) and \(t\) are guaranteed to be different and consist of lowercase English letters only. | Output ""Yes"" (without quotes) if the superhero with name \(s\) can be transformed to the superhero with name \(t\) and ""No"" (without quotes) otherwise.You can print each letter in any case (upper or lower). | In the first sample, since both 'a' and 'u' are vowels, it is possible to convert string \(s\) to \(t\).In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string \(s\) to \(t\). | Input: a u | Output: Yes | Beginner | 2 | 635 | 293 | 210 | 11 |
136 | B | 136B | B. Ternary Logic | 1,100 | implementation; math | Little Petya very much likes computers. Recently he has received a new ""Ternatron IV"" as a gift from his mother. Unlike other modern computers, ""Ternatron IV"" operates with ternary and not binary logic. Petya immediately wondered how the xor operation is performed on this computer (and whether there is anything lik... | The first line contains two integers a and c (0 β€ a, c β€ 109). Both numbers are written in decimal notation. | Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation. | Input: 14 34 | Output: 50 | Easy | 2 | 1,147 | 108 | 164 | 1 | |
1,234 | E | 1234E | E. Special Permutations | 2,000 | math | Let's define \(p_i(n)\) as the following permutation: \([i, 1, 2, \dots, i - 1, i + 1, \dots, n]\). This means that the \(i\)-th permutation is almost identity (i.e. which maps every element to itself) permutation but the element \(i\) is on the first position. Examples: \(p_1(4) = [1, 2, 3, 4]\); \(p_2(4) = [2, 1, 3, ... | The first line of the input contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)) β the number of elements in each permutation and the number of elements in \(x\).The second line of the input contains \(m\) integers (\(m\), not \(n\)) \(x_1, x_2, \dots, x_m\) (\(1 \le x_i \le n\)), where \(x_i\) is th... | Print \(n\) integers: \(f(p_1(n)), f(p_2(n)), \dots, f(p_n(n))\). | Consider the first example:\(x = [1, 2, 3, 4]\), so for the permutation \(p_1(4) = [1, 2, 3, 4]\) the answer is \(|1 - 2| + |2 - 3| + |3 - 4| = 3\); for the permutation \(p_2(4) = [2, 1, 3, 4]\) the answer is \(|2 - 1| + |1 - 3| + |3 - 4| = 4\); for the permutation \(p_3(4) = [3, 1, 2, 4]\) the answer is \(|2 - 3| + |3... | Input: 4 4 1 2 3 4 | Output: 3 4 6 5 | Hard | 1 | 886 | 408 | 65 | 12 |
1,238 | E | 1238E | E. Keyboard Purchase | 2,200 | bitmasks; dp | You have a password which you often type β a string \(s\) of length \(n\). Every character of this string is one of the first \(m\) lowercase Latin letters.Since you spend a lot of time typing it, you want to buy a new keyboard.A keyboard is a permutation of the first \(m\) Latin letters. For example, if \(m = 3\), the... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5, 1 \le m \le 20\)).The second line contains the string \(s\) consisting of \(n\) characters. Each character is one of the first \(m\) Latin letters (lowercase). | Print one integer β the minimum slowness a keyboard can have. | The first test case is considered in the statement.In the second test case the slowness of any keyboard is \(0\).In the third test case one of the most suitable keyboards is bacd. | Input: 6 3 aacabc | Output: 5 | Hard | 2 | 1,298 | 233 | 61 | 12 |
672 | B | 672B | B. Different is Good | 1,000 | constructive algorithms; implementation; strings | A wise man told Kerem ""Different is good"" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some num... | The first line of the input contains an integer n (1 β€ n β€ 100 000) β the length of the string s.The second line contains the string s of length n consisting of only lowercase English letters. | If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes. | In the first sample one of the possible solutions is to change the first character to 'b'.In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes ""abko"". | Input: 2aa | Output: 1 | Beginner | 3 | 860 | 192 | 148 | 6 |
1,695 | C | 1695C | C. Zero Path | 1,700 | brute force; data structures; dp; graphs; greedy; shortest paths | You are given a grid with \(n\) rows and \(m\) columns. We denote the square on the \(i\)-th (\(1\le i\le n\)) row and \(j\)-th (\(1\le j\le m\)) column by \((i, j)\) and the number there by \(a_{ij}\). All numbers are equal to \(1\) or to \(-1\). You start from the square \((1, 1)\) and can move one square down or one... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the size of the grid.Each of the following \(n\) lines contain... | For each test case, print ""YES"" if there exists a path from the top left to the bottom right that adds up to \(0\), and ""NO"" otherwise. You can output each letter in any case. | One possible path for the fourth test case is given in the picture in the statement. | Input: 51 111 21 -11 41 -1 1 -13 41 -1 -1 -1-1 1 1 -11 1 1 -13 41 -1 1 1-1 1 -1 11 -1 1 1 | Output: NO YES YES YES NO | Medium | 6 | 555 | 549 | 179 | 16 |
478 | A | 478A | A. Initial Bet | 1,100 | implementation | There are five people playing a game called ""Generosity"". Each person gives some non-zero number of coins b as an initial bet. After all players make their bets of b coins, the following operation is repeated for several times: a coin is passed from one player to some other player.Your task is to write a program that... | The input consists of a single line containing five integers c1, c2, c3, c4 and c5 β the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0 β€ c1, c2, c3, c4, c5 β€ 100). | Print the only line containing a single positive integer b β the number of coins in the initial bet of each player. If there is no such value of b, then print the only value ""-1"" (quotes for clarity). | In the first sample the following sequence of operations is possible: One coin is passed from the fourth player to the second player; One coin is passed from the fourth player to the fifth player; One coin is passed from the first player to the third player; One coin is passed from the fourth player to the second playe... | Input: 2 5 4 0 4 | Output: 3 | Easy | 1 | 547 | 233 | 202 | 4 |
703 | A | 703A | A. Mishka and Game | 800 | implementation | Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.Rules of the game are very simple: at first number of rounds n is defined. In ... | The first line of the input contains single integer n n (1 β€ n β€ 100) β the number of game rounds.The next n lines contains rounds description. i-th of them contains pair of integers mi and ci (1 β€ mi, ci β€ 6) β values on dice upper face after Mishka's and Chris' throws in i-th round respectively. | If Mishka is the winner of the game, print ""Mishka"" (without quotes) in the only line.If Chris is the winner of the game, print ""Chris"" (without quotes) in the only line.If the result of the game is draw, print ""Friendship is magic!^^"" (without quotes) in the only line. | In the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game.In the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1.In the third sample case Chris wins the first round, but there is ... | Input: 33 52 14 2 | Output: Mishka | Beginner | 1 | 870 | 298 | 276 | 7 |
566 | E | 566E | E. Restoring Map | 3,200 | bitmasks; constructive algorithms; trees | Archaeologists found some information about an ancient land of Treeland. We know for sure that the Treeland consisted of n cities connected by the n - 1 road, such that you can get from each city to any other one along the roads. However, the information about the specific design of roads in Treeland has been lost. The... | The first line contains integer n (2 β€ n β€ 1000) β the number of cities in the country. Next n lines describe the found lists of near cities. Each list starts from number k (1 β€ k β€ n), representing the number of cities in the list followed by k city numbers. All numbers in each list are distinct.It is guaranteed that ... | Print n - 1 pairs of numbers representing the roads of the country. The i-th line must contain two integers ai, bi (1 β€ ai, bi β€ n, ai β bi), showing that there is a road between cities ai and bi.The answer you print must satisfy the description of close cities from the input. You may print the roads of the countries i... | Input: 54 3 2 4 15 5 3 2 4 15 4 2 1 5 34 2 1 4 33 1 4 5 | Output: 1 41 21 34 5 | Master | 3 | 1,027 | 384 | 468 | 5 | |
372 | D | 372D | D. Choosing Subtree is Fun | 2,600 | binary search; data structures; dfs and similar; trees; two pointers | There is a tree consisting of n vertices. The vertices are numbered from 1 to n.Let's define the length of an interval [l, r] as the value r - l + 1. The score of a subtree of this tree is the maximum length of such an interval [l, r] that, the vertices with numbers l, l + 1, ..., r belong to the subtree.Considering al... | There are two integers in the first line, n and k (1 β€ k β€ n β€ 105). Each of the next n - 1 lines contains integers ai and bi (1 β€ ai, bi β€ n, ai β bi). That means ai and bi are connected by a tree edge.It is guaranteed that the input represents a tree. | Output should contain a single integer β the maximum possible score. | For the first case, there is some subtree whose size is at most 6, including 3 consecutive numbers of vertices. For example, the subtree that consists of {1, 3, 4, 5, 7, 8} or of {1, 4, 6, 7, 8, 10} includes 3 consecutive numbers of vertices. But there is no subtree whose size is at most 6, which includes 4 or more con... | Input: 10 64 1010 62 99 68 57 14 77 31 8 | Output: 3 | Expert | 5 | 518 | 253 | 68 | 3 |
515 | A | 515A | A. Drazil and Date | 1,000 | math | Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. Drazil's home is located in point (0, 0) and Varda's home is located in point (a, b). In each step, he can move in a unit distance in horizontal or vertical direction. In other words, from position (x, y) he can go to positions (... | You are given three integers a, b, and s ( - 109 β€ a, b β€ 109, 1 β€ s β€ 2Β·109) in a single line. | If you think Drazil made a mistake and it is impossible to take exactly s steps and get from his home to Varda's home, print ""No"" (without quotes).Otherwise, print ""Yes"". | In fourth sample case one possible route is: . | Input: 5 5 11 | Output: No | Beginner | 1 | 952 | 95 | 174 | 5 |
1,567 | A | 1567A | A. Domino Disaster | 800 | implementation; strings | Alice has a grid with \(2\) rows and \(n\) columns. She fully covers the grid using \(n\) dominoes of size \(1 \times 2\) β Alice may place them vertically or horizontally, and each cell should be covered by exactly one domino.Now, she decided to show one row of the grid to Bob. Help Bob and figure out what the other r... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)) β the width of the grid.The second line of each test case ... | For each test case, output one string β the other row of the grid, using the same format as the input string. If there are multiple answers, print any. | In the first test case, Alice shows Bob the top row, the whole grid may look like: In the second test case, Alice shows Bob the bottom row, the whole grid may look like: In the third test case, Alice shows Bob the bottom row, the whole grid may look like: In the fourth test case, Alice shows Bob the top row, the whole ... | Input: 4 1 U 2 LR 5 LRDLR 6 UUUUUU | Output: D LR LRULR DDDDDD | Beginner | 2 | 346 | 669 | 151 | 15 |
1,744 | A | 1744A | A. Number Replacement | 800 | greedy; implementation | An integer array \(a_1, a_2, \ldots, a_n\) is being transformed into an array of lowercase English letters using the following prodecure:While there is at least one number in the array: Choose any number \(x\) from the array \(a\), and any letter of the English alphabet \(y\). Replace all occurrences of number \(x\) wi... | The first line contains a single integer \(t\) \((1 \leq t \leq 10^3\)) β the number of test cases.Then the description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 50\)) β the length of the array \(a\) and the string \(s\).The second line of each test case... | For each test case, output ""YES"", if we can get the string \(s\) from the array \(a\), and ""NO"" otherwise. You can output each letter in any case. | The first test case corresponds to the sample described in the statement.In the second test case we can choose the number \(50\) and the letter a.In the third test case we can choose the number \(11\) and the letter a, after that \(a = [a, 22]\). Then we choose the number \(22\) and the letter b and get \(a = [a, b]\).... | Input: 752 3 2 4 1cacta150a211 22ab41 2 2 1aaab51 2 3 2 1aaaaa61 10 2 9 3 8azzfdb71 2 3 4 1 1 2abababb | Output: YES YES YES NO YES YES NO | Beginner | 2 | 1,015 | 553 | 150 | 17 |
741 | D | 741D | D. Arpaβs letter-marked tree and Mehrdadβs Dokhtar-kosh paths | 2,900 | data structures; dfs and similar; trees | Just in case somebody missed it: we have wonderful girls in Arpaβs land.Arpa has a rooted tree (connected acyclic graph) consisting of n vertices. The vertices are numbered 1 through n, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call a stri... | The first line contains integer n (1 β€ n β€ 5Β·105) β the number of vertices in the tree.(n - 1) lines follow, the i-th of them contain an integer pi + 1 and a letter ci + 1 (1 β€ pi + 1 β€ i, ci + 1 is lowercase English letter, between a and v, inclusively), that mean that there is an edge between nodes pi + 1 and i + 1 a... | Print n integers. The i-th of them should be the length of the longest simple path in subtree of the i-th vertex that form a Dokhtar-kosh string. | Input: 41 s2 a3 s | Output: 3 1 1 0 | Master | 3 | 540 | 369 | 145 | 7 | |
982 | B | 982B | B. Bus of Characters | 1,300 | data structures; greedy; implementation | In the Bus of Characters there are \(n\) rows of seat, each having \(2\) seats. The width of both seats in the \(i\)-th row is \(w_i\) centimeters. All integers \(w_i\) are distinct.Initially the bus is empty. On each of \(2n\) stops one passenger enters the bus. There are two types of passengers: an introvert always c... | The first line contains a single integer \(n\) (\(1 \le n \le 200\,000\)) β the number of rows in the bus.The second line contains the sequence of integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 10^{9}\)), where \(w_i\) is the width of each of the seats in the \(i\)-th row. It is guaranteed that all \(w_i\) are dist... | Print \(2n\) integers β the rows the passengers will take. The order of passengers should be the same as in input. | In the first example the first passenger (introvert) chooses the row \(2\), because it has the seats with smallest width. The second passenger (introvert) chooses the row \(1\), because it is the only empty row now. The third passenger (extrovert) chooses the row \(1\), because it has exactly one occupied seat and the ... | Input: 23 10011 | Output: 2 1 1 2 | Easy | 3 | 779 | 858 | 114 | 9 |
425 | B | 425B | B. Sereja and Table | 2,200 | bitmasks; greedy | Sereja has an n Γ m rectangular table a, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a co... | The first line contains integers n, m and k (1 β€ n, m β€ 100; 1 β€ k β€ 10). Next n lines describe the table a: the i-th of them contains m integers ai1, ai2, ..., aim (0 β€ ai, j β€ 1) β the values in the cells of the i-th row. | Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed. | Input: 5 5 21 1 1 1 11 1 1 1 11 1 0 1 11 1 1 1 11 1 1 1 1 | Output: 1 | Hard | 2 | 977 | 223 | 124 | 4 | |
2,081 | D | 2081D | D. MST in Modulo Graph | 2,600 | constructive algorithms; dsu; graphs; greedy; math; number theory; sortings; trees | You are given a complete graph with \(n\) vertices, where the \(i\)-th vertex has a weight \(p_i\). The weight of the edge connecting vertex \(x\) and vertex \(y\) is equal to \(\operatorname{max}(p_x, p_y) \bmod \operatorname{min}(p_x, p_y)\).Find the smallest total weight of a set of \(n - 1\) edges that connect all ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test contains an integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The next line of each test contains \(n\) integers \(p_1, p_2, \ldots, p_... | For each test case, output a single integer β the weight of the minimum spanning tree. | In the first test case, one of the possible ways to connect the edges is to draw the edges \((1, 2)\), \((1, 4)\), \((1, 5)\), \((2, 3)\). The weight of the first edge is \(\operatorname{max}(p_1, p_2) \bmod \operatorname{min}(p_1, p_2)=4 \bmod 3 = 1\), and the weights of all other edges are \(0\). | Input: 454 3 3 4 4102 10 3 2 9 9 4 6 4 61233 56 48 41 89 73 99 150 55 100 111 130711 45 14 19 19 8 10 | Output: 1 0 44 10 | Expert | 8 | 349 | 520 | 86 | 20 |
1,196 | D1 | 1196D1 | D1. RGB Substring (easy version) | 1,500 | implementation | The only difference between easy and hard versions is the size of the input.You are given a string \(s\) consisting of \(n\) characters, each character is 'R', 'G' or 'B'.You are also given an integer \(k\). Your task is to change the minimum number of characters in the initial string \(s\) so that after the changes th... | The first line of the input contains one integer \(q\) (\(1 \le q \le 2000\)) β the number of queries. Then \(q\) queries follow.The first line of the query contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)) β the length of the string \(s\) and the length of the substring.The second line of the query co... | For each query print one integer β the minimum number of characters you need to change in the initial string \(s\) so that after changing there will be a substring of length \(k\) in \(s\) that is also a substring of the infinite string ""RGBRGBRGB ..."". | In the first example, you can change the first character to 'R' and obtain the substring ""RG"", or change the second character to 'R' and obtain ""BR"", or change the third, fourth or fifth character to 'B' and obtain ""GB"".In the second example, the substring is ""BRG"". | Input: 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR | Output: 1 0 3 | Medium | 1 | 830 | 493 | 255 | 11 |
2,074 | G | 2074G | G. Game With Triangles: Season 2 | 2,100 | dp; geometry | The Frontman greets you to this final round of the survival game. There is a regular polygon with \(n\) sides (\(n \ge 3\)). The vertices are indexed as \(1,2,\ldots,n\) in clockwise order. On each vertex \(i\), the pink soldiers have written a positive integer \(a_i\). With this regular polygon, you will play a game d... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) β the number of vertices (\(3 \le n \le 400\)).The second line of each test case contains \(a... | For each test case, output the maximum score you can get on a separate line. | On the first test case, you can draw only one triangle. The maximum score \(6\) is found by drawing the triangle with \(i=1\), \(j=2\), \(k=3\).On the second test case, you can draw only one triangle. The maximum score \(24\) is found by drawing the triangle with \(i=1\), \(j=3\), \(k=4\).On the third test case, you ca... | Input: 631 2 342 1 3 462 1 2 1 1 161 2 1 3 1 599 9 8 2 4 4 3 5 399 9 3 2 4 4 8 5 3 | Output: 6 24 5 30 732 696 | Hard | 2 | 1,031 | 487 | 76 | 20 |
125 | D | 125D | D. Two progressions | 2,200 | constructive algorithms; greedy | An arithmetic progression is such a non-empty sequence of numbers where the difference between any two successive numbers is constant. This constant number is called common difference. For example, the sequence 3, 7, 11, 15 is an arithmetic progression. The definition implies that any sequences whose length equals 1 or... | The first line contains a positive integer n (2 β€ n β€ 30000), n is the length of the given sequence. The second line contains elements of the given sequence a1, a2, ..., an ( - 108 β€ ai β€ 108). The elements of the progression are different integers. | Print the required arithmetic progressions, one per line. The progressions can be positioned in any order. Each progression should contain at least one number. If there's no solution, then print ""No solution"" (without the quotes)in the only line of the input file. If there are several solutions, print any of them. | In the second sample another solution is also possible (number three can be assigned to the second progression): 1, 2 and 3, -2, -7. | Input: 64 1 2 7 3 10 | Output: 1 2 3 4 7 10 | Hard | 2 | 754 | 249 | 317 | 1 |
1,856 | B | 1856B | B. Good Arrays | 900 | implementation; math | You are given an array of positive integers \(a\) of length \(n\).Let's call an array of positive integers \(b\) of length \(n\) good if: \(a_i \neq b_i\) for all \(i\) from \(1\) to \(n\), \(a_1 + a_2 +\ldots + a_n = b_1 + b_2 + \ldots + b_n\). Does a good array exist? | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array \(a\).The second ... | For each test case, output ""YES"" (without quotes) if there exists a good array, and ""NO"" (without quotes) 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. | In the first test case, a possible good array is \([3, 3, 3]\). Some examples of not good arrays are: \([8, 0, 1]\) β the array does not consist of only positive integers, \([5, 2, 4]\) β the array does not have the same sum as the given array, \([5, 2, 2]\) β the third element is equal to the third element of the give... | Input: 636 1 221 143 1 2 411751 2 1 1 13618343152 819343431 1000000000 | Output: YES NO YES NO NO YES | Beginner | 2 | 270 | 537 | 280 | 18 |
1,599 | C | 1599C | C. Bubble Strike | 2,000 | combinatorics; math; probabilities; ternary search | Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players.Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game ... | The first line contains two integers \(N\) (\(3\) \(\leq\) \(N\) \(\leq\) \(10^{3}\)) and \(P\) (\(0\) \(\leq\) \(P\) \(\leq\) \(1\)) β total number of maps in the game and probability to play map Johnny has studied. \(P\) will have at most four digits after the decimal point. | Output contains one integer number β minimum number of maps Johnny has to study. | Input: 7 1.0000 | Output: 6 | Hard | 4 | 1,146 | 277 | 80 | 15 | |
698 | C | 698C | C. LRU | 2,400 | bitmasks; dp; math; probabilities | While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used).Suppose the cache may store no more than k objects. At the beginning of the workflow the cache is empty. When some object is queried we... | The first line of the input contains two integers n and k (1 β€ k β€ n β€ 20) β the number of videos and the size of the cache respectively. Next line contains n real numbers pi (0 β€ pi β€ 1), each of them is given with no more than two digits after decimal point.It's guaranteed that the sum of all pi is equal to 1. | Print n real numbers, the i-th of them should be equal to the probability that the i-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The... | Input: 3 10.3 0.2 0.5 | Output: 0.3 0.2 0.5 | Expert | 4 | 1,016 | 313 | 376 | 6 | |
148 | A | 148A | A. Insomnia cure | 800 | constructive algorithms; implementation; math | Β«One dragon. Two dragon. Three dragonΒ», β the princess was counting. She had trouble falling asleep, and she got bored of counting lambs when she was nine.However, just counting dragons was boring as well, so she entertained herself at best she could. Tonight she imagined that all dragons were here to steal her, and sh... | Input data contains integer numbers k, l, m, n and d, each number in a separate line (1 β€ k, l, m, n β€ 10, 1 β€ d β€ 105). | Output the number of damaged dragons. | In the first case every first dragon got punched with a frying pan. Some of the dragons suffered from other reasons as well, but the pan alone would be enough.In the second case dragons 1, 7, 11, 13, 17, 19 and 23 escaped unharmed. | Input: 123412 | Output: 12 | Beginner | 3 | 722 | 120 | 37 | 1 |
2,114 | D | 2114D | D. Come a Little Closer | 1,400 | brute force; greedy; implementation; math | The game field is a matrix of size \(10^9 \times 10^9\), with a cell at the intersection of the \(a\)-th row and the \(b\)-th column denoted as (\(a, b\)).There are \(n\) monsters on the game field, with the \(i\)-th monster located in the cell (\(x_i, y_i\)), while the other cells are empty. No more than one monster c... | 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 a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of monsters on the field.The following \(n\) lines contain two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le ... | For each test case, output a single integer β the minimum cost to destroy all \(n\) monsters. | Below are examples of optimal moves, with the cells of the rectangle to be selected highlighted in green. Required move for the first example. Required move for the fifth example. | Input: 731 11 22 151 12 66 43 38 241 11 10000000001000000000 11000000000 100000000011 151 24 24 33 13 231 12 52 244 33 14 41 2 | Output: 3 32 1000000000000000000 1 6 4 8 | Easy | 4 | 716 | 517 | 93 | 21 |
195 | B | 195B | B. After Training | 1,300 | data structures; implementation; math | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The ... | The first line contains two space-separated integers n, m (1 β€ n, m β€ 105) β the number of balls and baskets, correspondingly. | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | Input: 4 3 | Output: 2132 | Easy | 3 | 1,071 | 126 | 101 | 1 | |
780 | C | 780C | C. Andryusha and Colored Balloons | 1,600 | dfs and similar; graphs; greedy; trees | Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored b... | The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.It is guaranteed that any square is reachable from any other using the paths. | In the first line print single integer k β the minimum number of colors Andryusha has to use.In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k. | In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct. Illustration for the first sample. In the second example there are following triples of consequently connected squares: 1 β 3 β 2 1 β 3 β 4 1 β 3 β 5 2 β 3 β 4 2 β 3 β 5 4 β 3 β 5 We can see that each pair o... | Input: 32 31 3 | Output: 31 3 2 | Medium | 4 | 842 | 304 | 261 | 7 |
644 | A | 644A | A. Parliament of Berland | 1,000 | *special; constructive algorithms | There are n parliamentarians in Berland. They are numbered with integers from 1 to n. It happened that all parliamentarians with odd indices are Democrats and all parliamentarians with even indices are Republicans.New parliament assembly hall is a rectangle consisting of a Γ b chairs β a rows of b chairs each. Two chai... | The first line of the input contains three integers n, a and b (1 β€ n β€ 10 000, 1 β€ a, b β€ 100) β the number of parliamentarians, the number of rows in the assembly hall and the number of seats in each row, respectively. | If there is no way to assigns seats to parliamentarians in a proper way print -1.Otherwise print the solution in a lines, each containing b integers. The j-th integer of the i-th line should be equal to the index of parliamentarian occupying this seat, or 0 if this seat should remain empty. If there are multiple possib... | In the first sample there are many other possible solutions. For example, 3 20 1and 2 13 0The following assignment 3 21 0is incorrect, because parliamentarians 1 and 3 are both from Democrats party but will occupy neighbouring seats. | Input: 3 2 2 | Output: 0 31 2 | Beginner | 2 | 998 | 220 | 359 | 6 |
131 | E | 131E | E. Yet Another Task with Queens | 1,700 | sortings | A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.There are m queens on a square n Γ n chessboard. You kn... | The first line of the input contains a pair of integers n, m (1 β€ n, m β€ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 β€ ri, ci β€ n) β the queen's position. No two q... | Print the required sequence t0, t1, ..., t8, separating the numbers with spaces. | Input: 8 44 34 86 51 6 | Output: 0 3 0 1 0 0 0 0 0 | Medium | 1 | 1,062 | 351 | 80 | 1 | |
1,971 | H | 1971H | H. Β±1 | 2,100 | 2-sat; dfs and similar; graphs | Bob has a grid with \(3\) rows and \(n\) columns, each of which contains either \(a_i\) or \(-a_i\) for some integer \(1 \leq i \leq n\). For example, one possible grid for \(n=4\) is shown below:$$$\(\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\ -a_4 & a_4 & -a_1 & -a_3 \\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}\)\(Alice a... | 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 a single integer \(n\) (\(2 \leq n \leq 500\)) β the number of columns of Bob's grid.The next three lines each contain \(n\) integers, the \(i\)-th of which contains \(g_{i,1}, g_... | For each test case, output ""YES"" (without quotes) if Alice can win, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"", and ""Yes"" will be recognized as a positive response). | The first test case is described in the statement.In the second test case, Bob's grid is as follows:$$$\(\begin{bmatrix} a_1 & a_2 \\ -a_1 & -a_2 \\ a_2 & -a_2 \end{bmatrix}\)\(For the last column to have \)1\( in the middle row when sorted, Alice must pick \)a_2 = -1\(. However, it is then impossible to choose \)a_1\(... | Input: 441 -2 -3 -2-4 4 -1 -31 2 -2 421 2-1 -22 -251 2 3 4 5-2 3 -4 -5 -13 -5 1 2 261 3 -6 2 5 21 3 -2 -3 -6 -5-2 -1 -3 2 3 1 | Output: YES NO YES NO | Hard | 3 | 1,696 | 642 | 249 | 19 |
2,052 | D | 2052D | D. DAG Serialization | 2,100 | brute force; graphs | Consider a simple single-bit boolean register that supports two operations: set β sets the register to true if it was false, and returns true; otherwise, it returns false; unset β sets the register to false if it was true, and returns true; otherwise, it returns false. The initial state of the register is false. Suppos... | In the first line, you are given an integer \(n\) β the number of operations (\(1 \le n \le 10^5\)). In the following \(n\) lines, you are given operations in the format ""type result"", where type is either ""set"" or ""unset"" and result is either ""true"" or ""false"". It is guaranteed that at most two operations ha... | Print any linear order of operations that satisfies the DAG constraints and ensures the results of the operations match the ones given in the input. If a correct operation order does not exist, print \(-1\). | Input: 5set trueunset trueset falseunset falseunset false21 45 2 | Output: 5 1 3 2 4 | Hard | 2 | 826 | 649 | 207 | 20 | |
400 | E | 400E | E. Inna and Binary Logic | 2,100 | binary search; bitmasks; data structures | Inna is fed up with jokes about female logic. So she started using binary logic instead.Inna has an array of n elements a1[1], a1[2], ..., a1[n]. Girl likes to train in her binary logic, so she does an exercise consisting of n stages: on the first stage Inna writes out all numbers from array a1, on the i-th (i β₯ 2) sta... | The first line contains two integers n and m (1 β€ n, m β€ 105) β size of array a1 and number of Dima's questions. Next line contains n integers a1[1], a1[2], ..., a1[n] (0 β€ ai β€ 105) β initial array elements.Each of next m lines contains two integers β Dima's question description. Each question consists of two integers... | For each question print Inna's answer on a single line. | Input: 3 41 1 11 12 23 21 2 | Output: 64712 | Hard | 3 | 729 | 502 | 55 | 4 | |
1,359 | A | 1359A | A. Berland Poker | 1,000 | brute force; greedy; math | The game of Berland poker is played with a deck of \(n\) cards, \(m\) of which are jokers. \(k\) players play this game (\(n\) is divisible by \(k\)).At the beginning of the game, each player takes \(\frac{n}{k}\) cards from the deck (so each card is taken by exactly one player). The player who has the maximum number o... | The first line of the input contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.Then the test cases follow. Each test case contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 50\), \(0 \le m \le n\), \(2 \le k \le n\), \(k\) is a divisors of \(n\)). | For each test case, print one integer β the maximum number of points a player can get for winning the game. | Test cases of the example are described in the statement. | Input: 4 8 3 2 4 2 4 9 6 3 42 0 7 | Output: 3 0 1 0 | Beginner | 3 | 1,535 | 283 | 107 | 13 |
1,890 | B | 1890B | B. Qingshan Loves Strings | 800 | constructive algorithms; implementation | Qingshan has a string \(s\), while Daniel has a string \(t\). Both strings only contain \(\texttt{0}\) and \(\texttt{1}\).A string \(a\) of length \(k\) is good if and only if \(a_i \ne a_{i+1}\) for all \(i=1,2,\ldots,k-1\). For example, \(\texttt{1}\), \(\texttt{101}\), \(\texttt{0101}\) are good, while \(\texttt{11}... | The input consists of multiple test cases. The first line contains a single integer \(T\) (\(1\le T\le 2000\)) β 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 \le n,m \le 50\)) β the length of the strings \(s\) and \(t\), ... | For each test case, print ""YES"" (without quotes), if it is possible to make \(s\) good, and ""NO"" (without quotes) otherwise.You can print letters in any case (upper or lower). | In the first test case, \(s\) is good initially, so you can get a good \(s\) by doing zero operations.In the second test case, you can do the following two operations (the inserted string \(t\) is underlined): \(\texttt{1}\underline{\texttt{010}}\texttt{11}\) \(\texttt{10101}\underline{\texttt{010}}\texttt{1}\) and get... | Input: 51 1103 31110103 2111006 7101100101010110 2100100100010 | Output: Yes Yes No No No | Beginner | 2 | 615 | 565 | 179 | 18 |
1,282 | E | 1282E | E. The Cake Is a Lie | 2,400 | constructive algorithms; data structures; dfs and similar; graphs | We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake.Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then there are \(t\) independent sets of input data.The first line of each set consists of a single integer \(n\) (\(3 \le n \le 10^5\)) β the number of vertices in the cake.The following \(n - 2\) lines describe the numbe... | Print \(2t\) lines β answers to given \(t\) test cases in the order in which they are written in the input. Each answer should consist of \(2\) lines.In the first line of an answer on a test case print \(n\) distinct numbers \(p_1, p_2, \dots, p_n\)(\(1 \le p_i \le n\)) β the numbers of the cake vertices in clockwise o... | Input: 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 | Output: 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1 | Expert | 4 | 2,300 | 687 | 699 | 12 | |
2,050 | A | 2050A | A. Line Breaks | 800 | implementation | Kostya has a text \(s\) consisting of \(n\) words made up of Latin alphabet letters. He also has two strips on which he must write the text. The first strip can hold \(m\) characters, while the second can hold as many as needed.Kostya must choose a number \(x\) and write the first \(x\) words from \(s\) on the first st... | The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 50\); \(1 \le m \le 500\)) β the number of words in the list and the maximum number of characters that can be on the first strip.The next \(n\)... | For each test case, output the maximum number of words \(x\) such that the first \(x\) words have a total length of no more than \(m\). | Input: 53 1abc2 9alphabeta4 12helloworldandcodeforces3 2abcd3 2abcaba | Output: 1 2 2 1 0 | Beginner | 1 | 677 | 431 | 135 | 20 | |
771 | D | 771D | D. Bear and Company | 2,500 | dp | Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.Limak has a string s tha... | The first line of the input contains an integer n (1 β€ n β€ 75) β the length of the string.The second line contains a string s, consisting of uppercase English letters. The length of the string is equal to n. | Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring ""VK"". | In the first sample, the initial string is ""VKVK"". The minimum possible number of moves is 3. One optimal sequence of moves is: Swap two last letters. The string becomes ""VKKV"". Swap first two letters. The string becomes ""KVKV"". Swap the second and the third letter. The string becomes ""KKVV"". Indeed, this strin... | Input: 4VKVK | Output: 3 | Expert | 1 | 764 | 207 | 134 | 7 |
1,663 | B | 1663B | B. Mike's Sequence | 0 | *special; divide and conquer; implementation; math | You won't find this sequence on OEIS. | One integer \(r\) (\(-45 \le r \le 2999\)). | One integer. | Input: 2999 | Output: 3000 | Beginner | 4 | 37 | 43 | 12 | 16 | |
1,510 | H | 1510H | H. Hard Optimization | 3,200 | dp | You are given a set of \(n\) segments on a line \([L_i; R_i]\). All \(2n\) segment endpoints are pairwise distinct integers.The set is laminar β any two segments are either disjoint or one of them contains the other.Choose a non-empty subsegment \([l_i, r_i]\) with integer endpoints in each segment (\(L_i \le l_i < r_i... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^3\)) β the number of segments.The \(i\)-th of the next \(n\) lines contains two integers \(L_i\) and \(R_i\) (\(0 \le L_i < R_i \le 10^9\)) β the endpoints of the \(i\)-th segment.All the given \(2n\) segment endpoints are distinct. The set of seg... | On the first line, output the maximum possible sum of subsegment lengths.On the \(i\)-th of the next \(n\) lines, output two integers \(l_i\) and \(r_i\) (\(L_i \le l_i < r_i \le R_i\)), denoting the chosen subsegment of the \(i\)-th segment. | The example input and the example output are illustrated below. | Input: 4 1 10 2 3 5 9 6 7 | Output: 7 3 6 2 3 7 9 6 7 | Master | 1 | 502 | 337 | 242 | 15 |
28 | A | 28A | A. Bender Problem | 1,600 | implementation | Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate ... | The first line contains two positive integers n and m (4 β€ n β€ 500, 2 β€ m β€ 500, n is even) β the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The ... | If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers β i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod.If there are multiple solutions, print any of them. | Input: 4 20 00 22 22 04 4 | Output: YES1 -1 2 -1 | Medium | 1 | 805 | 641 | 312 | 0 | |
2,041 | N | 2041N | N. Railway Construction | 3,300 | The country of Truckski is located in a rugged, mountainous region, and the geological condition has engendered a wide range of issues. The challenging terrain separates the different states in the country, resulting in an extremely inconvenient inter-state commute and more crucially a lack of central governmental cont... | The first line of the input contains two integers \(n\) and \(m\), the number of states in Truckski and the number of pairs for which railroad construction is not feasible. The next line contains \(n\) integers \(a_1, \ldots, a_n\), the construction fee the government needs to pay to the \(i\)-th state. Then, \(m\) lin... | Output \(n\) integers in one line. The \(i\)-th integer is the minimum construction cost when the \(i\)-th state is where the prison is built. If it is impossible to find a feasible railroad construction, output -1 instead. | Input: 5 3 1 2 1 1 1 1 4 1 5 2 5 | Output: 7 6 8 7 7 | Master | 0 | 2,787 | 621 | 223 | 20 | ||
1,426 | B | 1426B | B. Symmetric Matrix | 900 | implementation | Masha has \(n\) types of tiles of size \(2 \times 2\). Each cell of the tile contains one integer. Masha has an infinite number of tiles of each type.Masha decides to construct the square of size \(m \times m\) consisting of the given tiles. This square also has to be a symmetric with respect to the main diagonal matri... | The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(1 \le m \le 100\)) β the number of types of tiles and the size of the square Masha wants to ... | For each test case print the answer: ""YES"" (without quotes) if Masha can construct the square of size \(m \times m\) which is a symmetric matrix. Otherwise, print ""NO"" (withtout quotes). | The first test case of the input has three types of tiles, they are shown on the picture below. Masha can construct, for example, the following square of size \(4 \times 4\) which is a symmetric matrix: | Input: 6 3 4 1 2 5 6 5 7 7 4 8 9 9 8 2 5 1 1 1 1 2 2 2 2 1 100 10 10 10 10 1 2 4 5 8 4 2 2 1 1 1 1 1 2 3 4 1 2 1 1 1 1 | Output: YES NO YES NO YES YES | Beginner | 1 | 1,289 | 1,087 | 190 | 14 |
549 | E | 549E | E. Sasha Circle | 2,700 | geometry; math | Berlanders like to eat cones after a hard day. Misha Square and Sasha Circle are local authorities of Berland. Each of them controls its points of cone trade. Misha has n points, Sasha β m. Since their subordinates constantly had conflicts with each other, they decided to build a fence in the form of a circle, so that ... | The first line contains two integers n and m (1 β€ n, m β€ 10000), numbers of Misha's and Sasha's trade points respectively.The next n lines contains pairs of space-separated integers Mx, My ( - 104 β€ Mx, My β€ 104), coordinates of Misha's trade points.The next m lines contains pairs of space-separated integers Sx, Sy ( -... | The only output line should contain either word ""YES"" without quotes in case it is possible to build a such fence or word ""NO"" in the other case. | In the first sample there is no possibility to separate points, because any circle that contains both points ( - 1, 0), (1, 0) also contains at least one point from the set (0, - 1), (0, 1), and vice-versa: any circle that contains both points (0, - 1), (0, 1) also contains at least one point from the set ( - 1, 0), (1... | Input: 2 2-1 01 00 -10 1 | Output: NO | Master | 2 | 577 | 430 | 149 | 5 |
1,353 | F | 1353F | F. Decreasing Heights | 2,200 | brute force; dp | You are playing one famous sandbox game with the three-dimensional world. The map of the world can be represented as a matrix of size \(n \times m\), where the height of the cell \((i, j)\) is \(a_{i, j}\).You are in the cell \((1, 1)\) right now and want to get in the cell \((n, m)\). You can move only down (from the ... | The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 100\)) β the number of rows and the number of columns in the map of the world. The next \(n\) line... | For each test case, print the answer β the minimum number of operations you have to perform to obtain at least one suitable path from the cell \((1, 1)\) to the cell \((n, m)\). It is guaranteed that the answer exists. | Input: 5 3 4 1 2 3 4 5 6 7 8 9 10 11 12 5 5 2 5 4 8 3 9 10 11 5 1 12 8 4 2 5 2 2 5 4 1 6 8 2 4 2 2 2 100 10 10 1 1 2 123456789876543 987654321234567 1 1 42 | Output: 9 49 111 864197531358023 0 | Hard | 2 | 1,147 | 634 | 218 | 13 | |
4 | D | 4D | D. Mysterious Present | 1,700 | dp; sortings | Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1, a2, ..., an}, where the width and the height of the i-th envelope is strictly higher than the width and the height o... | The first line contains integers n, w, h (1 β€ n β€ 5000, 1 β€ w, h β€ 106) β amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi β width and height of the i-th envelope (1 β€ wi, hi β€ 106). | In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, pr... | Input: 2 1 12 22 2 | Output: 11 | Medium | 2 | 839 | 283 | 429 | 0 | |
1,714 | A | 1714A | A. Everyone Loves to Sleep | 900 | implementation; math | Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do \(n\) things, each at a certain time. For each of these things, he has an alarm clock set, the \(i\)-th of them is triggered on \(h_i\) hours \(m_i\) minutes every day (\(0 \le h_i < 24, 0 \le m_i < 60\)). Vlad uses the \(24\)-hour time format,... | The first line of input data contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the test.The first line of the case contains three integers \(n\), \(H\) and \(M\) (\(1 \le n \le 10, 0 \le H < 24, 0 \le M < 60\)) β the number of alarms and the time Vlad went to bed.The following \(n\) lines co... | Output \(t\) lines, each containing the answer to the corresponding test case. As an answer, output two numbers β the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0. | Input: 31 6 138 03 6 012 3014 456 02 23 3520 1510 30 | Output: 1 47 0 0 10 55 | Beginner | 2 | 697 | 555 | 263 | 17 | |
1,257 | B | 1257B | B. Magic Stick | 1,000 | math | Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number \(a\) is even, then the spell will turn it into \(\frac{3a}{2}\); If ... | The first line contains single integer \(T\) (\(1 \le T \le 10^4\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains two integers \(x\) and \(y\) (\(1 \le x, y \le 10^9\)) β the current number and the number that Petya wants to get. | For the \(i\)-th test case print the answer on it β YES if Petya can get the number \(y\) from the number \(x\) using known spells, and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). | Input: 7 2 3 1 1 3 6 6 8 1 2 4 1 31235 6578234 | Output: YES YES NO YES NO YES YES | Beginner | 1 | 763 | 290 | 292 | 12 | |
1,895 | C | 1895C | C. Torn Lucky Ticket | 1,400 | brute force; dp; hashing; implementation; math | A ticket is a non-empty string of digits from \(1\) to \(9\).A lucky ticket is such a ticket that: it has an even length; the sum of digits in the first half is equal to the sum of digits in the second half. You are given \(n\) ticket pieces \(s_1, s_2, \dots, s_n\). How many pairs \((i, j)\) (for \(1 \le i, j \le n\))... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of ticket pieces.The second line contains \(n\) non-empty strings \(s_1, s_2, \dots, s_n\), each of length at most \(5\) and consisting only of digits from \(1\) to \(9\). | Print a single integer β the number of pairs \((i, j)\) (for \(1 \le i, j \le n\)) such that \(s_i + s_j\) is a lucky ticket. | Input: 105 93746 59 3746 593 746 5937 46 59374 6 | Output: 20 | Easy | 5 | 555 | 262 | 125 | 18 | |
962 | A | 962A | A. Equator | 1,300 | implementation | Polycarp has created his own training plan to prepare for the programming contests. He will train for \(n\) days, all days are numbered from \(1\) to \(n\), beginning from the first.On the \(i\)-th day Polycarp will necessarily solve \(a_i\) problems. One evening Polycarp plans to celebrate the equator. He will celebra... | The first line contains a single integer \(n\) (\(1 \le n \le 200\,000\)) β the number of days to prepare for the programming contests.The second line contains a sequence \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10\,000\)), where \(a_i\) equals to the number of problems, which Polycarp will solve on the \(i\)-th day. | Print the index of the day when Polycarp will celebrate the equator. | In the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve \(4\) out of \(7\) scheduled problems on four days of the training.In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day... | Input: 41 3 2 1 | Output: 2 | Easy | 1 | 542 | 319 | 68 | 9 |
1,343 | D | 1343D | D. Constant Palindrome Sum | 1,700 | brute force; data structures; greedy; two pointers | You are given an array \(a\) consisting of \(n\) integers (it is guaranteed that \(n\) is even, i.e. divisible by \(2\)). All \(a_i\) does not exceed some integer \(k\).Your task is to replace the minimum number of elements (replacement is the following operation: choose some index \(i\) from \(1\) to \(n\) and replace... | 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 two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5, 1 \le k \le 2 \cdot 10^5\)) β the length of \(a\) and the maximum possible value of so... | For each test case, print the answer β the minimum number of elements you have to replace in \(a\) to satisfy the conditions from the problem statement. | Input: 4 4 2 1 2 1 2 4 3 1 2 2 1 8 7 6 1 1 7 6 3 4 6 6 6 5 2 6 1 3 4 | Output: 0 1 4 2 | Medium | 4 | 714 | 745 | 152 | 13 | |
734 | C | 734C | C. Anton and Making Potions | 1,600 | binary search; dp; greedy; two pointers | Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions.Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions. Spells of this ty... | The first line of the input contains three integers n, m, k (1 β€ n β€ 2Β·109, 1 β€ m, k β€ 2Β·105) β the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.The second line of the input contains two integers x and s (2 β€ x β€ 2Β·109, 1 β€ s β€ 2Β·109) β the ini... | Print one integer β the minimum time one has to spent in order to prepare n potions. | In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints... | Input: 20 3 210 992 4 320 10 404 1510 80 | Output: 20 | Medium | 4 | 1,087 | 1,117 | 84 | 7 |
203 | C | 203C | C. Photographer | 1,400 | greedy; sortings | Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve.The camera's memory is d megabytes. Valera's camera can take photos of high and low... | The first line contains two integers n and d (1 β€ n β€ 105, 1 β€ d β€ 109) β the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 β€ a β€ b β€ 104) β the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clien... | On the first line print the answer to the problem β the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in ... | Input: 3 102 31 42 11 0 | Output: 23 2 | Easy | 2 | 1,333 | 544 | 361 | 2 | |
1,800 | A | 1800A | A. Is It a Cat? | 800 | implementation; strings | You were walking down the street and heard a sound. The sound was described by the string \(s\) consisting of lowercase and uppercase Latin characters. Now you want to find out if the sound was a cat meowing.For the sound to be a meowing, the string can only contain the letters 'm', 'e', 'o' and 'w', in either uppercas... | The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 50\)) β the length of the string describing the sound.The second line of each test case conta... | For each test case, output on a separate line: YES if the sound was a cat meowing; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). | In the first test case, the string consists of a sequence of characters 'm', 'e', 'O', 'w', which satisfies the definition of meowing.In the second test case, the string consists of a sequence of \(3\) characters 'm' and 'M', one 'e', a sequence of \(3\) characters 'o' and 'O' and a sequence of \(7\) characters 'w' and... | Input: 74meOw14mMmeoOoWWWwwwW3mew7MmeEeUw4MEOW6MmyaVW5meowA | Output: YES YES NO NO YES NO NO | Beginner | 2 | 1,007 | 460 | 220 | 18 |
1,353 | C | 1353C | C. Board Moves | 1,000 | math | You are given a board of size \(n \times n\), where \(n\) is odd (not divisible by \(2\)). Initially, each cell of the board contains one figure.In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell \((i... | The first line of the input contains one integer \(t\) (\(1 \le t \le 200\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(1 \le n < 5 \cdot 10^5\)) β the size of the board. It is guaranteed that \(n\) is odd (not divisible by \(2\)).It is guarante... | For each test case print the answer β the minimum number of moves needed to get all the figures into one cell. | Input: 3 1 5 499993 | Output: 0 40 41664916690999888 | Beginner | 1 | 879 | 428 | 110 | 13 | |
1,225 | E | 1225E | E. Rock Is Push | 2,200 | binary search; dp | You are at the top left cell \((1, 1)\) of an \(n \times m\) labyrinth. Your goal is to get to the bottom right cell \((n, m)\). You can only move right or down, one cell per step. Moving right from a cell \((x, y)\) takes you to the cell \((x, y + 1)\), while moving down takes you to the cell \((x + 1, y)\).Some cells... | The first line contains two integers \(n, m\) β dimensions of the labyrinth (\(1 \leq n, m \leq 2000\)).Next \(n\) lines describe the labyrinth. Each of these lines contains \(m\) characters. The \(j\)-th character of the \(i\)-th of these lines is equal to ""R"" if the cell \((i, j)\) contains a rock, or ""."" if the ... | Print a single integer β the number of different legal paths from \((1, 1)\) to \((n, m)\) modulo \(10^9 + 7\). | In the first sample case we can't (and don't have to) move, hence the only path consists of a single cell \((1, 1)\).In the second sample case the goal is blocked and is unreachable.Illustrations for the third sample case can be found here: https://assets.codeforces.com/rounds/1225/index.html | Input: 1 1 . | Output: 1 | Hard | 2 | 882 | 405 | 111 | 12 |
396 | C | 396C | C. On Changing Tree | 0 | data structures; graphs; trees | You are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is a vertex number 1.Initially all vertices contain number 0. Then come q queries, each query has one of the two types: The format of the query: 1 v x k. In response to the query, you need to add to the number at vertex v nu... | The first line contains integer n (1 β€ n β€ 3Β·105) β the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ... pn (1 β€ pi < i), where pi is the number of the vertex that is the parent of vertex i in the tree.The third line contains integer q (1 β€ q β€ 3Β·105) β the number of queries. Next q l... | For each query of the second type print on a single line the number written in the vertex from the query. Print the number modulo 1000000007 (109 + 7). | You can read about a rooted tree here: http://en.wikipedia.org/wiki/Tree_(graph_theory). | Input: 31 131 1 2 12 12 2 | Output: 21 | Beginner | 3 | 777 | 658 | 151 | 3 |
585 | A | 585A | A. Gennady the Dentist | 1,800 | brute force; implementation | Gennady is one of the best child dentists in Berland. Today n children got an appointment with him, they lined up in front of his office.All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to n in the order they go in the line. Every child is associated with t... | The first line of the input contains a positive integer n (1 β€ n β€ 4000) β the number of kids in the line. Next n lines contain three integers each vi, di, pi (1 β€ vi, di, pi β€ 106) β the volume of the cry in the doctor's office, the volume of the cry in the hall and the confidence of the i-th child. | In the first line print number k β the number of children whose teeth Gennady will cure.In the second line print k integers β the numbers of the children who will make it to the end of the line in the increasing order. | In the first example, Gennady first treats the teeth of the first child who will cry with volume 4. The confidences of the remaining children will get equal to - 2, 1, 3, 1, respectively. Thus, the second child also cries at the volume of 1 and run to the exit. The confidence of the remaining children will be equal to ... | Input: 54 2 24 1 25 2 43 3 55 1 2 | Output: 21 3 | Medium | 2 | 1,496 | 301 | 218 | 5 |
391 | F1 | 391F1 | F1. Stock Trading | 0 | dp | This problem consists of three subproblems: for solving subproblem F1 you will receive 8 points, for solving subproblem F2 you will receive 15 points, and for solving subproblem F3 you will receive 10 points.Manao has developed a model to predict the stock price of a company over the next n days and wants to design a p... | The first line contains two integers n and k, separated by a single space, with . The i-th of the following n lines contains a single integer pi (0 β€ pi β€ 1012), where pi represents the price at which someone can either buy or sell one share of stock on day i.The problem consists of three subproblems. The subproblems h... | For this problem, the program will only report the amount of the optimal profit, rather than a list of trades that can achieve this profit.Therefore, the program should print one line containing a single integer, the maximum profit Manao can achieve over the next n days with the constraints of starting with no shares o... | In the first example, the best trade overall is to buy at a price of 1 on day 9 and sell at a price of 9 on day 10 and the second best trade overall is to buy at a price of 2 on day 1 and sell at a price of 9 on day 4. Since these two trades do not overlap, both can be made and the profit is the sum of the profits of t... | Input: 10 22739879719 | Output: 15 | Beginner | 1 | 1,954 | 690 | 470 | 3 |
1,553 | B | 1553B | B. Reverse String | 1,300 | brute force; dp; hashing; implementation; strings | You have a string \(s\) and a chip, which you can place onto any character of this string. After placing the chip, you move it to the right several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is \(i\), you move it to the position \(i + 1\). Of course,... | The first line contains one integer \(q\) (\(1 \le q \le 500\)) β the number of test cases.Each test case consists of two lines. The first line contains the string \(s\) (\(1 \le |s| \le 500\)), the second line contains the string \(t\) (\(1 \le |t| \le 2 \cdot |s| - 1\)). Both strings consist of lowercase English char... | For each test case, print ""YES"" if you can obtain the string \(t\) by performing the process mentioned in the statement with the string \(s\), or ""NO"" if you cannot.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer... | Consider the examples.The first test case is described in the statement.In the second test case, you can place the chip on the \(1\)-st position, move it twice to the right, and then move it twice to the left.In the fourth test case, you can place the chip on the \(2\)-nd position, and then don't move it at all.In the ... | Input: 6 abcdef cdedcb aaa aaaaa aab baaa ab b abcdef abcdef ba baa | Output: YES YES NO YES YES NO | Easy | 5 | 1,208 | 412 | 322 | 15 |
2,086 | F | 2086F | F. Online Palindrome | 3,000 | brute force; constructive algorithms; interactive | This is an interactive problem.The jury has a string \(s\) consisting of lowercase Latin letters. The following constraints apply to this string: the string has an odd length that does not exceed \(99\); the string consists only of the characters ""a"" and ""b"". There is also a string \(t\), which is initially empty. ... | Input: a a b 0 | Output: 0 0 1 2 2 3 | Master | 3 | 648 | 0 | 0 | 20 | |||
1,994 | A | 1994A | A. Diverse Game | 800 | constructive algorithms; greedy; implementation | Petr, watching Sergey's stream, came up with a matrix \(a\), consisting of \(n\) rows and \(m\) columns (the number in the \(i\)-th row and \(j\)-th column is denoted as \(a_{i, j}\)), which contains all integers from \(1\) to \(n \cdot m\). But he didn't like the arrangement of the numbers, and now he wants to come up... | Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10\)) β the number of rows and columns of mat... | For each test case, output \(n \cdot m\) integers β any suitable matrix \(b\), or \(-1\) if such a matrix does not exist. | In the first test case, there is only one element in the matrix, so matrix \(b\) is the only matrix and it does not fit.In the second test case \(a_{1, 1} = 2 \neq 1 = b_{1, 1}\), \(a_{2, 1} = 1 \neq 2 = b_{2, 1}\). | Input: 51 112 1211 52 4 5 3 12 41 2 3 45 6 7 83 34 2 19 8 36 7 5 | Output: -1 1 2 4 5 3 1 2 6 7 8 5 2 3 4 1 8 3 9 7 5 6 2 1 4 | Beginner | 3 | 780 | 708 | 121 | 19 |
929 | B | 929B | B. ΠΠ΅ΡΡΠ° Π² ΡΠ°ΠΌΠΎΠ»ΡΡΠ΅ | 1,300 | *special; implementation | Π ΡΠ°ΠΌΠΎΠ»ΡΡΠ΅ Π΅ΡΡΡ n ΡΡΠ΄ΠΎΠ² ΠΌΠ΅ΡΡ. ΠΡΠ»ΠΈ ΡΠΌΠΎΡΡΠ΅ΡΡ Π½Π° ΡΡΠ΄Ρ ΡΠ²Π΅ΡΡ
Ρ, ΡΠΎ Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΡΠ΄Ρ Π΅ΡΡΡ 3 ΠΌΠ΅ΡΡΠ° ΡΠ»Π΅Π²Π°, Π·Π°ΡΠ΅ΠΌ ΠΏΡΠΎΡ
ΠΎΠ΄ ΠΌΠ΅ΠΆΠ΄Ρ ΡΡΠ΄Π°ΠΌΠΈ, Π·Π°ΡΠ΅ΠΌ 4 ΡΠ΅Π½ΡΡΠ°Π»ΡΠ½ΡΡ
ΠΌΠ΅ΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π΅ΡΡ ΠΎΠ΄ΠΈΠ½ ΠΏΡΠΎΡ
ΠΎΠ΄ ΠΌΠ΅ΠΆΠ΄Ρ ΡΡΠ΄Π°ΠΌΠΈ, Π° Π·Π°ΡΠ΅ΠΌ Π΅ΡΡ 3 ΠΌΠ΅ΡΡΠ° ΡΠΏΡΠ°Π²Π°.ΠΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΡΠΎ Π½Π΅ΠΊΠΎΡΠΎΡΡΠ΅ ΠΌΠ΅ΡΡΠ° ΡΠΆΠ΅ Π·Π°Π½ΡΡΡ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠ°ΠΌΠΈ. ΠΡΠ΅Π³ΠΎ Π΅ΡΡΡ Π΄Π²Π° Π²ΠΈΠ΄Π° ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ² β ΡΡΠ°ΡΡΡΠ½ΡΠ΅ (ΡΠ΅, ΠΊΠΎΡΠΎ... | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΡΡ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° n ΠΈ k (1 β€ n β€ 100, 1 β€ k β€ 10Β·n) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠ΄ΠΎΠ² ΠΌΠ΅ΡΡ Π² ΡΠ°ΠΌΠΎΠ»ΡΡΠ΅ ΠΈ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΡ
Π½ΡΠΆΠ½ΠΎ ΡΠ°ΡΡΠ°Π΄ΠΈΡΡ.ΠΠ°Π»Π΅Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΡΡΠ΄ΠΎΠ² ΠΌΠ΅ΡΡ ΡΠ°ΠΌΠΎΠ»ΡΡΠ° ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡ ΡΡΠ΄Ρ Π² ΡΡΡΠΎΠΊΠ΅. ΠΡΠ»ΠΈ ΠΎΡΠ΅ΡΠ΅Π΄Π½ΠΎΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΡΠ°Π²Π΅Π½ '-', ΡΠΎ ΡΡΠΎ ΠΏΡΠΎΡ
ΠΎΠ΄ ΠΌΠ΅ΠΆΠ΄Ρ ΡΡΠ΄Π°ΠΌΠΈ. ΠΡΠ»ΠΈ ΠΎΡΠ΅ΡΠ΅Π΄Π½ΠΎΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΡΠ°Π²Π΅Π½ '.', ... | Π ΠΏΠ΅ΡΠ²ΡΡ ΡΡΡΠΎΠΊΡ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΡΠΌΠΌΠ°ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠΎΡΠ΅Π΄Π΅ΠΉ Ρ ΡΡΠ°ΡΡΡΠ½ΡΡ
ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ².ΠΠ°Π»Π΅Π΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΏΠ»Π°Π½ ΡΠ°ΡΡΠ°Π΄ΠΊΠΈ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΠΉ ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·ΠΈΡΡΠ΅Ρ ΡΡΠΌΠΌΠ°ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΡΠ΅Π΄Π΅ΠΉ Ρ ΡΡΠ°ΡΡΡΠ½ΡΡ
ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ², Π² ΡΠΎΠΌ ΠΆΠ΅ ΡΠΎΡΠΌΠ°ΡΠ΅, ΡΡΠΎ ΠΈ Π²ΠΎ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
. ΠΡΠ»ΠΈ Π² ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΠΎΠ΅ ΠΌΠ΅ΡΡΠΎ Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΡΠ°Π΄ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· k ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ², Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΡΡΠΎΡΠ½Ρ... | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΡΠ°Π΄ΠΈΡΡ Π΅ΡΡ Π΄Π²ΡΡ
ΠΎΠ±ΡΡΠ½ΡΡ
ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ². ΠΠ»Ρ ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·Π°ΡΠΈΠΈ ΡΠΎΡΠ΅Π΄Π΅ΠΉ Ρ ΡΡΠ°ΡΡΡΠ½ΡΡ
ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ², Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΡΠ°Π΄ΠΈΡΡ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΈΠ· Π½ΠΈΡ
Π½Π° ΡΡΠ΅ΡΡΠ΅ ΡΠ»Π΅Π²Π° ΠΌΠ΅ΡΡΠΎ, Π° Π²ΡΠΎΡΠΎΠ³ΠΎ Π½Π° Π»ΡΠ±ΠΎΠ΅ ΠΈΠ· ΠΎΡΡΠ°Π²ΡΠΈΡ
ΡΡ Π΄Π²ΡΡ
ΠΌΠ΅ΡΡ, ΡΠ°ΠΊ ΠΊΠ°ΠΊ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎ ΠΎΡ Π²ΡΠ±ΠΎΡΠ° ΠΌΠ΅ΡΡΠ° ΠΎΠ½ ΡΡΠ°Π½Π΅Ρ ΡΠΎΡΠ΅Π΄ΠΎΠΌ Π΄Π²ΡΡ
ΡΡΠ°ΡΡΡΠ½ΡΡ
ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠΎΠ². ΠΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ, Ρ ΡΡΠ°ΡΡΡΠ½ΠΎΠ³ΠΎ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠ°... | Input: 1 2SP.-SS.S-S.S | Output: 5SPx-SSxS-S.S | Easy | 2 | 730 | 643 | 351 | 9 |
1,423 | H | 1423H | H. Virus | 2,500 | data structures; divide and conquer; dsu; graphs | In Bubbleland a group of special programming forces gets a top secret job to calculate the number of potentially infected people by a new unknown virus. The state has a population of \(n\) people and every day there is new information about new contacts between people. The job of special programming forces is to calcul... | The first line of input contains three integers \(n\) (\(1 \le n\le 10^5\)) the number of people in the state, \(q\) (\(1 \le q\le 5Γ10^5\)) number of queries and \(k\) (\(1 \le k\le 10^5\)) virus incubation time in days.Each of the next \(q\) lines starts with an integer \(t\) (\(1 \le t\le 3\)) the type of the query.... | For the queries of the second type print on a separate line the current number of people in contact with a given person. | Pay attention if persons \(1\) and \(2\) had contact first day and next day persons \(1\) and \(3\) had contact, for \(k\)>\(1\) number of contacts of person \(3\) is \(3\)(persons:1,2,3). | Input: 5 12 1 1 1 2 1 1 3 1 3 4 2 4 2 5 3 2 1 1 1 2 1 3 2 2 1 3 2 1 | Output: 4 1 1 3 1 | Expert | 4 | 1,280 | 564 | 120 | 14 |
1,680 | D | 1680D | D. Dog Walking | 2,400 | brute force; greedy; math | You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point \(0\) with your dog. You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have s... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n \le 3000; 1 \le k \le 10^9\)) β the number of minutes and the maximum possible speed of your dog during the minutes without records.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)), wh... | If the dog cannot return to the point \(0\) after \(n\) minutes regardless of the set of integers you place, print -1. Otherwise, print one integer β the maximum number of different integer points your dog could visit if you fill all the unknown values optimally and the dog will return to the point \(0\) at the end of ... | Input: 3 2 5 0 -4 | Output: 6 | Expert | 3 | 1,500 | 565 | 329 | 16 | |
865 | C | 865C | C. Gotta Go Fast | 2,400 | binary search; dp | You're trying to set the record on your favorite video game. The game consists of N levels, which must be completed sequentially in order to beat the game. You usually complete each level as fast as possible, but sometimes finish a level slower. Specifically, you will complete the i-th level in either Fi seconds or Si ... | The first line of input contains integers N and R , the number of levels and number of seconds you want to complete the game in, respectively. N lines follow. The ith such line contains integers Fi, Si, Pi (1 β€ Fi < Si β€ 100, 80 β€ Pi β€ 99), the fast time for level i, the slow time for level i, and the probability (as a... | Print the total expected time. Your answer must be correct within an absolute or relative error of 10 - 9.Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if . | In the first example, you never need to reset. There's an 81% chance of completing the level in 2 seconds and a 19% chance of needing 8 seconds, both of which are within the goal time. The expected time is 0.81Β·2 + 0.19Β·8 = 3.14.In the second example, you should reset after the first level if you complete it slowly. On... | Input: 1 82 8 81 | Output: 3.14 | Expert | 2 | 858 | 374 | 210 | 8 |
231 | E | 231E | E. Cactus | 2,100 | data structures; dfs and similar; dp; graphs; trees | A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle.A simple cycle in a undirected graph is a sequence of distinct vertices v1, v2, ..., vt (t > 2), such that for any i (1 β€ i < t) exists an edge between vertices vi and vi + 1, and also exists an edge... | The first line contains two space-separated integers n, m (2 β€ n β€ 105; 1 β€ m β€ 105) β the number of vertices and edges in the graph, correspondingly. Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 β€ ai, bi β€ n) β the indexes of the vertices connected by... | Print k lines: in the i-th line print a single integer β the number of distinct simple ways, starting at xi and ending at yi, modulo 1000000007 (109 + 7). | Input: 10 111 22 33 41 43 55 68 68 77 67 99 1061 23 56 99 29 39 10 | Output: 222441 | Hard | 5 | 1,235 | 821 | 154 | 2 | |
1,592 | C | 1592C | C. Bakry and Partitioning | 1,700 | bitmasks; constructive algorithms; dfs and similar; dp; graphs; trees | Bakry faced a problem, but since he's lazy to solve it, he asks for your help.You are given a tree of \(n\) nodes, the \(i\)-th node has value \(a_i\) assigned to it for each \(i\) from \(1\) to \(n\). As a reminder, a tree on \(n\) nodes is a connected graph with \(n-1\) edges.You want to delete at least \(1\), but at... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) \((1 \leq t \leq 5 \cdot 10^4)\). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) \((2 \leq k \leq n \leq 10^5)\).The second line of each test case contains \(n\) i... | For each test case, you should output a single string. If you can delete the edges according to the conditions written above, output ""YES"" (without quotes). Otherwise, output ""NO"" (without quotes).You can print each letter of ""YES"" and ""NO"" in any case (upper or lower). | It can be shown that the objection is not achievable for first, third, and fifth test cases.In the second test case, you can just remove all the edges. There will be \(5\) connected components, each containing only one node with value \(3\), so the bitwise XORs will be \(3\) for all of them.In the fourth test case, thi... | Input: 5 2 2 1 3 1 2 5 5 3 3 3 3 3 1 2 2 3 1 4 4 5 5 2 1 7 2 3 5 1 2 2 3 1 4 4 5 5 3 1 6 4 1 2 1 2 2 3 1 4 4 5 3 3 1 7 4 1 2 2 3 | Output: NO YES NO YES NO | Medium | 6 | 597 | 712 | 278 | 15 |
744 | B | 744B | B. Hongcow's Game | 1,900 | bitmasks; divide and conquer; interactive | This is an interactive problem. In the interaction section below you will see the information about flushing the output.In this problem, you will be playing a game with Hongcow. How lucky of you!Hongcow has a hidden n by n matrix M. Let Mi, j denote the entry i-th row and j-th column of the matrix. The rows and columns... | The first line of input will contain a single integer n (2 β€ n β€ 1, 000). | To print the final answer, print out the string -1 on its own line. Then, the next line should contain n integers. The i-th integer should be the minimum value of the i-th row of the matrix, excluding elements on the diagonal. Do not forget to flush your answer! | In the first sample, Hongcow has the hidden matrix [ [0, 3, 2], [5, 0, 7], [4, 8 ,0],]Here is a more readable version demonstrating the interaction. The column on the left represents Hongcow, while the column on the right represents the contestant. 3 3 1 2 30 0 0 1 32 7 0 2 1 20 0 4 1 23 0 8 1 10 5 4 -1 2 5 4For the se... | Input: 30 0 02 7 00 0 43 0 80 5 4 | Output: 31 2 31321 21211-12 5 4 | Hard | 3 | 1,713 | 73 | 262 | 7 |
912 | E | 912E | E. Prime Gift | 2,400 | binary search; dfs and similar; math; meet-in-the-middle; number theory; two pointers | Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manage to learn how to solve number theory problems in the past year. That's why instead of Ded Moroz he was visited by his teammate Andrew, who solemnly presented him with a set of n distinct prime numbers alongside with a s... | The first line contains a single integer n (1 β€ n β€ 16).The next line lists n distinct prime numbers p1, p2, ..., pn (2 β€ pi β€ 100) in ascending order.The last line gives a single integer k (1 β€ k). It is guaranteed that the k-th smallest integer such that all its prime divisors are in this set does not exceed 1018. | Print a single line featuring the k-th smallest integer. It's guaranteed that the answer doesn't exceed 1018. | The list of numbers with all prime divisors inside {2, 3, 5} begins as follows:(1, 2, 3, 4, 5, 6, 8, ...)The seventh number in this list (1-indexed) is eight. | Input: 32 3 57 | Output: 8 | Expert | 6 | 424 | 317 | 109 | 9 |
1,012 | E | 1012E | E. Cycle sort | 3,100 | dsu; math | You are given an array of \(n\) positive integers \(a_1, a_2, \dots, a_n\). You can perform the following operation any number of times: select several distinct indices \(i_1, i_2, \dots, i_k\) (\(1 \le i_j \le n\)) and move the number standing at the position \(i_1\) to the position \(i_2\), the number at the position... | The first line of the input contains two integers \(n\) and \(s\) (\(1 \leq n \leq 200\,000\), \(0 \leq s \leq 200\,000\))βthe number of elements in the array and the upper bound on the sum of cycle lengths.The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\)βelements of the array (\(1 \leq a_i \leq 10^9\)). | If it's impossible to sort the array using cycles of total length not exceeding \(s\), print a single number ""-1"" (quotes for clarity).Otherwise, print a single number \(q\)β the minimum number of operations required to sort the array.On the next \(2 \cdot q\) lines print descriptions of operations in the order they ... | In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle \(1 \to 4 \to 1\) (of length 2), then apply the cycle \(2 \to 3 \to 5 \to 2\) (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which i... | Input: 5 53 2 3 1 1 | Output: 151 4 2 3 5 | Master | 2 | 1,070 | 319 | 874 | 10 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.