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,635 | A | 1635A | A. Min Or Sum | 800 | bitmasks; greedy | You are given an array \(a\) of size \(n\).You can perform the following operation on the array: Choose two different integers \(i, j\) \((1 \leq i < j \leq n\)), replace \(a_i\) with \(x\) and \(a_j\) with \(y\). In order not to break the array, \(a_i | a_j = x | y\) must be held, where \(|\) denotes the bitwise OR op... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) \((1 \leq t \leq 1000)\). Description of the test cases follows.The first line of each test case contains an integer \(n\) \((2 \leq n \leq 100)\) β the size of array \(a\).The second line of each test case contains \(n\) int... | For each test case, print one number in a line β the minimum possible sum of the array. | In the first example, you can perform the following operations to obtain the array \([1, 0, 2]\):1. choose \(i = 1, j = 2\), change \(a_1 = 1\) and \(a_2 = 2\), it's valid since \(1 | 3 = 1 | 2\). The array becomes \([1, 2, 2]\).2. choose \(i = 2, j = 3\), change \(a_2 = 0\) and \(a_3 = 2\), it's valid since \(2 | 2 = ... | Input: 431 3 251 2 4 8 1626 633 5 6 | Output: 3 31 6 7 | Beginner | 2 | 491 | 378 | 87 | 16 |
575 | B | 575B | B. Bribes | 2,200 | dfs and similar; graphs; trees | Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way β however, we kno... | The first line contains N, the number of towns in Ruritania. The following N - 1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (a,b,x), which are separated with a single whitespace character. The numbers a and b represent the cities connected by this pa... | The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109 + 7. | Borna first takes the route 1 β 5 and has to pay 1000 dinars. After that, he takes the route 5 β 1 β 2 β 3 β 4 and pays nothing this time. However, when he has to return via 4 β 3 β 2 β 1 β 5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5 β 1 β 2 will cost him nothing. Finally, he doesn't ... | Input: 51 2 02 3 05 1 13 4 155 4 5 2 2 | Output: 4 | Hard | 3 | 1,456 | 699 | 143 | 5 |
303 | D | 303D | D. Rotatable Number | 2,600 | math; number theory | Bike is a smart boy who loves math very much. He invented a number called ""Rotatable Number"" inspired by 142857. As you can see, 142857 is a magic number because any of its rotatings can be got by multiplying that number by 1, 2, ..., 6 (numbers from one to number's length). Rotating a number means putting its last s... | The only line contains two space-separated integers n, x (1 β€ n β€ 5Β·106, 2 β€ x β€ 109). | Print a single integer β the largest b you found. If no such b exists, print -1 instead. | Input: 6 11 | Output: 10 | Expert | 2 | 1,329 | 86 | 88 | 3 | |
979 | D | 979D | D. Kuro and GCD and XOR and SUM | 2,200 | binary search; bitmasks; brute force; data structures; dp; dsu; greedy; math; number theory; strings; trees | Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.Sadly, he's going on a vacation for a day, and he isn't able to continue his solving strea... | The first line contains one integer \(q\) (\(2 \leq q \leq 10^{5}\)) β the number of tasks the game wants you to perform.\(q\) lines follow, each line begins with an integer \(t_i\) β the type of the task: If \(t_i = 1\), an integer \(u_i\) follow (\(1 \leq u_i \leq 10^{5}\)) β you have to add \(u_i\) to the array \(a\... | For each task of type \(2\), output on one line the desired number \(v\), or -1 if no such numbers are found. | In the first example, there are 5 tasks: The first task requires you to add \(1\) into \(a\). \(a\) is now \(\left\{1\right\}\). The second task requires you to add \(2\) into \(a\). \(a\) is now \(\left\{1, 2\right\}\). The third task asks you a question with \(x = 1\), \(k = 1\) and \(s = 3\). Taking both \(1\) and \... | Input: 51 11 22 1 1 32 1 1 22 1 1 1 | Output: 21-1 | Hard | 11 | 1,133 | 782 | 109 | 9 |
1,623 | C | 1623C | C. Balanced Stone Heaps | 1,600 | binary search; greedy | There are \(n\) heaps of stone. The \(i\)-th heap has \(h_i\) stones. You want to change the number of stones in the heap by performing the following process once: You go through the heaps from the \(3\)-rd heap to the \(n\)-th heap, in this order. Let \(i\) be the number of the current heap. You can choose a number \(... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^5\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)).The second lines of each test case contains \(n\) integers \... | For each test case, print the maximum number of stones that the smallest heap can contain. | In the first test case, the initial heap sizes are \([1, 2, 10, 100]\). We can move the stones as follows. move \(3\) stones and \(6\) from the \(3\)-rd heap to the \(2\)-nd and \(1\) heap respectively. The heap sizes will be \([7, 5, 1, 100]\); move \(6\) stones and \(12\) stones from the last heap to the \(3\)-rd and... | Input: 441 2 10 1004100 100 100 155 1 1 1 861 2 3 4 5 6 | Output: 7 1 1 3 | Medium | 2 | 840 | 467 | 90 | 16 |
1,372 | D | 1372D | D. Omkar and Circle | 2,100 | brute force; dp; games; greedy | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given \(n\) nonnegative integers \(a_1, a_2, \dots, a_n\) arranged in a circle, where \(n\) must be odd (ie. \(n-1\) is divisible by \(2\)). Formally, for all \(i\) such that \(2 \leq i \leq ... | The first line contains one odd integer \(n\) (\(1 \leq n < 2 \cdot 10^5\), \(n\) is odd) β the initial size of the circle.The second line contains \(n\) integers \(a_{1},a_{2},\dots,a_{n}\) (\(0 \leq a_{i} \leq 10^9\)) β the initial numbers in the circle. | Output the maximum possible circular value after applying some sequence of operations to the given circle. | For the first test case, here's how a circular value of \(17\) is obtained:Pick the number at index \(3\). The sum of adjacent elements equals \(17\). Delete \(7\) and \(10\) from the circle and replace \(2\) with \(17\).Note that the answer may not fit in a \(32\)-bit integer. | Input: 3 7 10 2 | Output: 17 | Hard | 4 | 807 | 256 | 106 | 13 |
743 | E | 743E | E. Vladik and cards | 2,200 | binary search; bitmasks; brute force; dp | Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions: the number of occurren... | The first line contains single integer n (1 β€ n β€ 1000) β the number of cards in Vladik's sequence.The second line contains the sequence of n positive integers not exceeding 8 β the description of Vladik's sequence. | Print single integer β the length of the longest subsequence of Vladik's sequence that satisfies both conditions. | In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition. | Input: 31 1 1 | Output: 1 | Hard | 4 | 1,088 | 215 | 113 | 7 |
2,063 | B | 2063B | B. Subsequence Update | 1,100 | constructive algorithms; data structures; greedy; sortings | After Little John borrowed expansion screws from auntie a few hundred times, eventually she decided to come and take back the unused ones.But as they are a crucial part of home design, Little John decides to hide some in the most unreachable places β under the eco-friendly wood veneers. You are given an integer sequenc... | 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 three integers \(n\), \(l\), \(r\) (\(1 \le l \le r \le n \le 10^5\)) β the length of \(a\), and the segment \([l,r]... | For each test case, output the minimum value of \(a_l+a_{l+1}+\ldots+a_{r-1}+a_r\) on a separate line. | On the second test case, the array is \(a=[1,2,3]\) and the segment is \([2,3]\).After choosing the subsequence \(a_1,a_3\) and reversing it, the sequence becomes \([3,2,1]\). Then, the sum \(a_2+a_3\) becomes \(3\). It can be shown that the minimum possible value of the sum is \(3\). | Input: 62 1 12 13 2 31 2 33 1 33 1 24 2 31 2 2 25 2 53 3 2 3 56 1 33 6 6 4 3 2 | Output: 1 3 6 3 11 8 | Easy | 4 | 1,131 | 515 | 102 | 20 |
1,083 | B | 1083B | B. The Fair Nut and Strings | 2,000 | greedy; strings | Recently, the Fair Nut has written \(k\) strings of length \(n\), consisting of letters ""a"" and ""b"". He calculated \(c\) β the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.Then, he lost his sheet with strings. He remembers that all written string... | The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 5 \cdot 10^5\), \(1 \leq k \leq 10^9\)).The second line contains a string \(s\) (\(|s| = n\)) β the string consisting of letters ""a"" and ""b.The third line contains a string \(t\) (\(|t| = n\)) β the string consisting of letters ""a"" and ""b.It is... | Print one number β maximal value of \(c\). | In the first example, Nut could write strings ""aa"", ""ab"", ""ba"", ""bb"". These \(4\) strings are prefixes of at least one of the written strings, as well as ""a"" and ""b"". Totally, \(6\) strings.In the second example, Nut could write strings ""aba"", ""baa"", ""bba"".In the third example, there are only two diff... | Input: 2 4 aa bb | Output: 6 | Hard | 2 | 790 | 393 | 42 | 10 |
1,866 | F | 1866F | F. Freak Joker Process | 3,100 | binary search; data structures; sortings | After the success of the basketball teams formed and trained by Pak Chanek last year (Basketball Together), Pak Chanek wants to measure the performance of each player that is considered as a superstar.There are \(N\) superstar players that have been trained by Pak Chanek. At the end of the season, some calculations wil... | The first line contains a single integer \(N\) (\(1\leq N\leq10^5\)) β the number of superstar players.The second line contains \(N\) integers \(A_1, A_2, A_3 \ldots, A_N\) (\(1 \leq A_i \leq 10^5\)) β the offensive value of each player.The third line contains \(N\) integers \(B_1, B_2, B_3 \ldots, B_N\) (\(1 \leq B_i ... | For each event of type \(3\), output a line containing an integer representing the value of \(\text{RankOverall}(k)\) at that moment. | At the \(8\)-th event, \(A=[3,2,1,3,2]\) and \(B=[3,6,1,4,1]\). It can be obtained that the values of \(\text{RankA}\) and \(\text{RankB}\) for each player are as follows: \(\text{RankA}(1)=1\), \(\text{RankB}(1)=3\) \(\text{RankA}(2)=3\), \(\text{RankB}(2)=1\) \(\text{RankA}(3)=5\), \(\text{RankB}(3)=4\) \(\text{RankA... | Input: 5 3 3 1 3 2 3 7 1 3 1 8 3 1 3 2 2 4 + 1 2 - 3 2 3 3 2 2 - 3 1 | Output: 2 1 2 5 2 | Master | 3 | 1,670 | 681 | 133 | 18 |
1,009 | A | 1009A | A. Game Shopping | 800 | implementation | Maxim wants to buy some games at the local game shop. There are \(n\) games in the shop, the \(i\)-th game costs \(c_i\).Maxim has a wallet which can be represented as an array of integers. His wallet contains \(m\) bills, the \(j\)-th bill has value \(a_j\).Games in the shop are ordered from left to right, Maxim tries... | The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of games and the number of bills in Maxim's wallet.The second line of the input contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le 1000\)), where \(c_i\) is the cost of the \(i\)-th game.The third lin... | Print a single integer β the number of games Maxim will buy. | The first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he... | Input: 5 42 4 5 2 45 3 4 6 | Output: 3 | Beginner | 1 | 1,646 | 482 | 60 | 10 |
1,843 | F1 | 1843F1 | F1. Omsk Metro (simple version) | 1,800 | data structures; dfs and similar; dp; graphs; greedy; math; trees | This is the simple version of the problem. The only difference between the simple and hard versions is that in this version \(u = 1\).As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and b... | The first line contains a single number \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of events.Then there are \(n\) lines describing the events. In the \(i\)-th line, one of the following options is pos... | For each of Alex's questions, output ""Yes"" (without quotes) if the subsegment described in the condition exists, otherwise output ""No"" (without quotes).You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer). | Explanation of the first sample.The answer to the second question is ""Yes"", because there is a path \(1\).In the fourth question, we can choose the \(1\) path again.In the fifth query, the answer is ""Yes"", since there is a path \(1-3\).In the sixth query, we can choose an empty path because the sum of the weights o... | Input: 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0 | Output: NO YES NO YES YES YES | Medium | 7 | 1,739 | 1,137 | 299 | 18 |
625 | A | 625A | A. Guest From the Past | 1,700 | implementation; math | Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plasti... | First line of the input contains a single integer n (1 β€ n β€ 1018) β the number of rubles Kolya has at the beginning.Then follow three lines containing integers a, b and c (1 β€ a β€ 1018, 1 β€ c < b β€ 1018) β the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by return... | Print the only integer β maximum number of liters of kefir, that Kolya can drink. | In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he wi... | Input: 101198 | Output: 2 | Medium | 2 | 714 | 360 | 81 | 6 |
337 | D | 337D | D. Book of Evil | 2,000 | dfs and similar; divide and conquer; dp; trees | Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to r... | The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integer... | Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0. | Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5. | Input: 6 2 31 21 52 33 44 55 6 | Output: 3 | Hard | 4 | 1,106 | 367 | 227 | 3 |
443 | A | 443A | A. Anton and Letters | 800 | constructive algorithms; implementation | Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time... | The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space. | Print a single number β the number of distinct letters in Anton's set. | Input: {a, b, c} | Output: 3 | Beginner | 2 | 449 | 316 | 70 | 4 | |
234 | F | 234F | F. Fence | 1,800 | dp | Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1, 2, ..., n from left to right. The height of the i-th board is hi centimeters.Vasya has a 1 centimeter wi... | The first line contains a single integer n (1 β€ n β€ 200) β the number of boards in Vasya's fence.The second line contains two integers a and b (0 β€ a, b β€ 4Β·104) β the area that can be painted red and the area that can be painted green, correspondingly.The third line contains a sequence of n integers h1, h2, ..., hn (1... | Print a single number β the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print - 1. | Input: 45 73 3 4 1 | Output: 3 | Medium | 1 | 1,692 | 423 | 145 | 2 | |
23 | E | 23E | E. Tree | 2,500 | dp | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can... | The first input line contains integer number n (1 β€ n β€ 700) β amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 β€ ai, bi β€ n). It's guaranteed that the graph described in the input is a tree. | Output the only number β the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | Input: 51 22 33 44 5 | Output: 6 | Expert | 1 | 358 | 317 | 140 | 0 | |
594 | A | 594A | A. Warrior and Archer | 2,300 | games | In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.Vov... | The first line on the input contains a single integer n (2 β€ n β€ 200 000, n is even) β the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 β€ xi β€ 109), giving the coordinates of the corresponding positions. | Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally. | In the first sample one of the optimum behavior of the players looks like that: Vova bans the position at coordinate 15; Lesha bans the position at coordinate 3; Vova bans the position at coordinate 31; Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, and the distance bet... | Input: 60 1 3 7 15 31 | Output: 7 | Expert | 1 | 1,932 | 263 | 134 | 5 |
1,353 | B | 1353B | B. Two Arrays And Swaps | 800 | greedy; sortings | You are given two arrays \(a\) and \(b\) both consisting of \(n\) positive (greater than zero) integers. You are also given an integer \(k\).In one move, you can choose two indices \(i\) and \(j\) (\(1 \le i, j \le n\)) and swap \(a_i\) and \(b_j\) (i.e. \(a_i\) becomes \(b_j\) and vice versa). Note that \(i\) and \(j\... | 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 first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 30; 0 \le k \le n\)) β the number of elements in \(a\) and \(b\) and the maximum number of moves you ... | For each test case, print the answer β the maximum possible sum you can obtain in the array \(a\) if you can do no more than (i.e. at most) \(k\) swaps. | In the first test case of the example, you can swap \(a_1 = 1\) and \(b_2 = 4\), so \(a=[4, 2]\) and \(b=[3, 1]\).In the second test case of the example, you don't need to swap anything.In the third test case of the example, you can swap \(a_1 = 1\) and \(b_1 = 10\), \(a_3 = 3\) and \(b_3 = 10\) and \(a_2 = 2\) and \(b... | Input: 5 2 1 1 2 3 4 5 5 5 5 6 6 5 1 2 5 4 3 5 3 1 2 3 4 5 10 9 10 10 9 4 0 2 2 4 3 2 4 2 3 4 4 1 2 2 1 4 4 5 4 | Output: 6 27 39 11 17 | Beginner | 2 | 640 | 634 | 152 | 13 |
1,028 | C | 1028C | C. Rectangles | 1,600 | geometry; implementation; sortings | You are given \(n\) rectangles on a plane with coordinates of their bottom left and upper right points. Some \((n-1)\) of the given \(n\) rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.Find any point with integer coordinates th... | The first line contains a single integer \(n\) (\(2 \le n \le 132\,674\)) β the number of given rectangles.Each the next \(n\) lines contains four integers \(x_1\), \(y_1\), \(x_2\) and \(y_2\) (\(-10^9 \le x_1 < x_2 \le 10^9\), \(-10^9 \le y_1 < y_2 \le 10^9\)) β the coordinates of the bottom left and upper right corn... | Print two integers \(x\) and \(y\) β the coordinates of any point that belongs to at least \((n-1)\) given rectangles. | The picture below shows the rectangles in the first and second samples. The possible answers are highlighted. The picture below shows the rectangles in the third and fourth samples. | Input: 30 0 1 11 1 2 23 0 4 1 | Output: 1 1 | Medium | 3 | 370 | 339 | 118 | 10 |
277 | E | 277E | E. Binary Tree on Plane | 2,400 | flows; trees | A root tree is a directed acyclic graph that contains one node (root), from which there is exactly one path to any other node.A root tree is binary if each node has at most two outgoing arcs.When a binary tree is painted on the plane, all arcs should be directed from top to bottom. That is, each arc going from u to v m... | The first line contains a single integer n (2 β€ n β€ 400) β the number of nodes in the tree. Then follow n lines, two integers per line: xi, yi (|xi|, |yi| β€ 103) β coordinates of the nodes. It is guaranteed that all points are distinct. | If it is impossible to build a binary root tree on the given points, print ""-1"". Otherwise, print a single real number β the total length of the arcs in the minimum binary tree. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. | Input: 30 01 02 1 | Output: 3.650281539872885 | Expert | 2 | 592 | 236 | 274 | 2 | |
1,299 | A | 1299A | A. Anu Has a Function | 1,500 | brute force; greedy; math | Anu has created her own function \(f\): \(f(x, y) = (x | y) - y\) where \(|\) denotes the bitwise OR operation. For example, \(f(11, 6) = (11|6) - 6 = 15 - 6 = 9\). It can be proved that for any nonnegative numbers \(x\) and \(y\) value of \(f(x, y)\) is also nonnegative. She would like to research more about this func... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)). Elements of the array are not guaranteed to be different. | Output \(n\) integers, the reordering of the array with maximum value. If there are multiple answers, print any. | In the first testcase, value of the array \([11, 6, 4, 0]\) is \(f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9\).\([11, 4, 0, 6]\) is also a valid answer. | Input: 4 4 0 11 6 | Output: 11 6 4 0 | Medium | 3 | 743 | 219 | 112 | 12 |
2,043 | F | 2043F | F. Nim | 2,700 | bitmasks; brute force; combinatorics; dp; games; greedy; implementation; shortest paths | Recall the rules of the game ""Nim"". There are \(n\) piles of stones, where the \(i\)-th pile initially contains some number of stones. Two players take turns choosing a non-empty pile and removing any positive (strictly greater than \(0\)) number of stones from it. The player unable to make a move loses the game.You ... | The first line of input contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)) β the size of the array and the number of segments for which the answers need to be calculated.The second line of input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 50\)) β the elements of the initial array.The ... | For each round: if Ruslan can win, print two integers β the maximum number of piles that can be removed, and the number of ways to remove the maximum number of piles, taken modulo \(998\,244\,353\); otherwise print -1. | Input: 9 50 1 2 1 3 4 5 6 01 52 53 54 51 9 | Output: 4 1 2 1 0 1 -1 8 2 | Master | 8 | 1,576 | 512 | 218 | 20 | |
1,829 | E | 1829E | E. The Lakes | 1,100 | dfs and similar; dsu; graphs; implementation | You are given an \(n \times m\) grid \(a\) of non-negative integers. The value \(a_{i,j}\) represents the depth of water at the \(i\)-th row and \(j\)-th column. A lake is a set of cells such that: each cell in the set has \(a_{i,j} > 0\), and there exists a path between any pair of cells in the lake by going up, down,... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n, m\) (\(1 \leq n, m \leq 1000\)) β the number of rows and columns of the grid, respectively.Then \(n\) lines follow each with \(m\) integers \(a_{i,j}\) (\(0 \leq... | For each test case, output a single integer β the largest volume of a lake in the grid. | Input: 53 31 2 03 4 00 0 51 103 30 1 11 0 11 1 15 51 1 1 1 11 0 0 0 11 0 5 0 11 0 0 0 11 1 1 1 15 51 1 1 1 11 0 0 0 11 1 4 0 11 0 0 0 11 1 1 1 1 | Output: 10 0 7 16 21 | Easy | 4 | 524 | 472 | 87 | 18 | |
331 | C3 | 331C3 | C3. The Great Julya Calendar | 2,500 | dp | Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: ""May the Great Beaver bless you! May your chacres open and... | The single line contains the magic integer n, 0 β€ n. to get 20 points, you need to solve the problem with constraints: n β€ 106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: n β€ 1012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: n β€ 1018 (sub... | Print a single integer β the minimum number of subtractions that turns the magic number to a zero. | In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24 β 20 β 18 β 10 β 9 β 0 | Input: 24 | Output: 5 | Expert | 1 | 891 | 339 | 98 | 3 |
1,415 | B | 1415B | B. Repainting Street | 1,100 | brute force; brute force; greedy | There is a street with \(n\) houses in a line, numbered from \(1\) to \(n\). The house \(i\) is initially painted in color \(c_i\). The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom's painting capacity is defined by an inte... | The first line of input contains a single integer \(t\) (\( 1 \le t \le 10^4\)), the number of test cases. Description of test cases follows.In the first line of a test case description there are two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)).The second line contains \(n\) space-separated integers. The \(i\)... | Print \(t\) lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case. | In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.In the third test case Tom can paint the ... | Input: 3 10 2 1 1 2 2 1 1 2 2 2 1 7 1 1 2 3 4 5 6 7 10 3 1 3 3 3 3 1 2 1 3 3 | Output: 3 6 2 | Easy | 3 | 864 | 522 | 127 | 14 |
370 | C | 370C | C. Mittens | 1,800 | constructive algorithms; greedy; sortings | A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th... | The first line contains two integers n and m β the number of the children and the number of possible mitten colors (1 β€ n β€ 5000, 1 β€ m β€ 100). The second line contains n integers c1, c2, ... cn, where ci is the color of the mittens of the i-th child (1 β€ ci β€ m). | In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th c... | Input: 6 31 3 2 2 1 1 | Output: 62 11 22 11 31 23 1 | Medium | 3 | 1,410 | 264 | 394 | 3 | |
1,519 | F | 1519F | F. Chests and Keys | 3,200 | bitmasks; brute force; dfs and similar; dp; flows | Alice and Bob play a game. Alice has got \(n\) treasure chests (the \(i\)-th of which contains \(a_i\) coins) and \(m\) keys (the \(j\)-th of which she can sell Bob for \(b_j\) coins).Firstly, Alice puts some locks on the chests. There are \(m\) types of locks, the locks of the \(j\)-th type can only be opened with the... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 6\)) β the number of chests and the number of keys, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 4\)), where \(a_i\) is the number of coins in the \(i\)-th chest.The third line contains \(m\) integer... | If Alice cannot ensure her victory (no matter which locks she puts on which chests, Bob always has a way to gain positive profit), print \(-1\).Otherwise, print one integer β the minimum number of dollars Alice has to spend to win the game regardless of Bob's actions. | In the first example, Alice should put locks of types \(1\) and \(3\) on the first chest, and locks of type \(2\) and \(3\) on the second chest.In the second example, Alice should put locks of types \(1\) and \(2\) on the first chest, and a lock of type \(3\) on the second chest. | Input: 2 3 3 3 1 1 4 10 20 100 20 15 80 | Output: 205 | Master | 5 | 1,292 | 715 | 268 | 15 |
818 | E | 818E | E. Card Game Again | 1,900 | binary search; data structures; number theory; two pointers | Vova again tries to play some computer card game.The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number ai is written on the i-th card in the deck.After receiving the de... | The first line contains two integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 109).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the numbers written on the cards. | Print the number of ways to choose x and y so the resulting deck is valid. | In the first example the possible values of x and y are: x = 0, y = 0; x = 1, y = 0; x = 2, y = 0; x = 0, y = 1. | Input: 3 46 2 8 | Output: 4 | Hard | 4 | 1,026 | 178 | 74 | 8 |
1,006 | B | 1006B | B. Polycarp's Practice | 1,200 | greedy; implementation; sortings | Polycarp is practicing his problem solving skill. He has a list of \(n\) problems with difficulties \(a_1, a_2, \dots, a_n\), respectively. His plan is to practice for exactly \(k\) days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, h... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)) β the number of problems and the number of days, respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2000\)) β difficulties of problems in Polycarp's list, in the order ... | In the first line of the output print the maximum possible total profit.In the second line print exactly \(k\) positive integers \(t_1, t_2, \dots, t_k\) (\(t_1 + t_2 + \dots + t_k\) must equal \(n\)), where \(t_j\) means the number of problems Polycarp will solve during the \(j\)-th day in order to achieve the maximum... | The first example is described in the problem statement.In the second example there is only one possible distribution.In the third example the best answer is to distribute problems in the following way: \([1, 2000], [2000, 2]\). The total profit of this distribution is \(2000 + 2000 = 4000\). | Input: 8 35 4 2 6 5 1 9 2 | Output: 203 2 3 | Easy | 3 | 1,495 | 393 | 421 | 10 |
158 | A | 158A | A. Next Round | 800 | *special; implementation | ""Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..."" β an excerpt from contest rules.A total of n participants took part in the contest (n β₯ k), and you already know their scores. Calculate how many ... | The first line of the input contains two integers n and k (1 β€ k β€ n β€ 50) separated by a single space.The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from... | Output the number of participants who advance to the next round. | In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.In the second example nobody got a positive score. | Input: 8 510 9 8 7 7 7 5 5 | Output: 6 | Beginner | 2 | 364 | 383 | 64 | 1 |
2,087 | H | 2087H | H. Nim with Special Numbers | 0 | *special; *special | Let's recall the rules of the game ""Nim"". The game is played by two players who take turns. They have a certain number of piles of stones (the piles can be of the same size or different sizes; the size is defined as the number of stones in a pile). On their turn, a player must choose any non-empty pile of stones and ... | The first line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.Each query is given in one line in one of the following formats: \(1\) \(s_i\) (\(1 \le s_i \le 3 \cdot 10^5\)) β if the number \(s_i\) is in the set \(S\), remove it; otherwise, add it to \(S\); \(2\) \(k_i\) \(a_{i,1... | For each query of type \(2\), output First if the player making the first move will win, or Second if they will lose. | Input: 112 2 1 41 22 2 1 42 3 3 1 41 32 3 3 1 42 2 1 41 42 2 1 51 22 2 1 5 | Output: First Second Second Second Second First Second | Beginner | 2 | 1,272 | 531 | 117 | 20 | |
2,089 | B2 | 2089B2 | B2. Canteen (Hard Version) | 2,300 | binary search; data structures; dp; flows; greedy; two pointers | This is the hard version of the problem. The difference between the versions is that in this version, there are no additional limits on \(k\). You can hack only if you solved all versions of this problem. Ecrade has two sequences \(a_0, a_1, \ldots, a_{n - 1}\) and \(b_0, b_1, \ldots, b_{n - 1}\) consisting of integers... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(k\) (\(1\le n\le 2\cdot 10^5\), \(0\le k\le 2\cdot 10^{14}\)).The second line of each t... | For each test case, output the minimum number of rounds required for all elements in \(a\) to become equal to \(0\) after exactly \(k\) changes to \(a\). | In the fifth test case, all elements in \(a\) become \(0\) after exactly \(6\) changes.In the sixth test case, Ecrade can do exactly one change to \(a_3\), then \(a\) will become \([1,2,2,4]\). After the first round, \(a=[3,0,0,0],b=[3,1,0,0]\); After the second round, \(a=[0,0,0,0],b=[0,1,0,0]\). In the seventh test c... | Input: 83 01 1 45 1 44 01 2 3 44 3 2 14 02 1 1 21 2 2 18 01 2 3 4 5 6 7 88 7 6 5 4 3 2 13 61 1 45 1 44 11 2 3 44 3 2 14 12 1 1 21 2 2 14 22 1 1 21 2 2 1 | Output: 1 4 4 8 0 2 2 1 | Expert | 6 | 1,193 | 762 | 153 | 20 |
155 | A | 155A | A. I_love_%username% | 800 | brute force | Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For eac... | The first line contains the single integer n (1 β€ n β€ 1000) β the number of contests where the coder participated.The next line contains n space-separated non-negative integer numbers β they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000. | Print the single number β the number of amazing performances the coder has had during his whole history of participating in the contests. | In the first sample the performances number 2 and 3 are amazing.In the second sample the performances number 2, 4, 9 and 10 are amazing. | Input: 5100 50 200 150 200 | Output: 2 | Beginner | 1 | 1,259 | 314 | 137 | 1 |
1,437 | G | 1437G | G. Death DBMS | 2,600 | data structures; string suffix structures; strings; trees | For the simplicity, let's say that the ""Death Note"" is a notebook that kills a person when their name is written in it.It's easy to kill with it, but it's pretty hard to keep track of people you haven't killed and still plan to. You decided to make a ""Death Database Management System"" β a computer program that prov... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of victims and the number of queries, respectively.Each of the next \(n\) lines contains a single string \(s_i\) β the name of the \(i\)-th victim. Each name consists only of lowercase Latin letters.Each of the next \(m\... | For each query of the second type print an integer value. If there is no victim name that is a contiguous substring of \(q\), then print \(-1\). Otherwise, print the maximum suspicion value of a victim whose name is a contiguous substring of \(q\). | Input: 5 8 kurou takuo takeshi naomi shingo 2 nakiraomi 2 abanaomicaba 1 3 943 2 takuotakeshishingo 1 5 135832 2 shingotakeshi 1 5 0 2 shingotakeshi | Output: -1 0 943 135832 943 | Expert | 4 | 1,195 | 837 | 248 | 14 | |
538 | G | 538G | G. Berserk Robot | 3,100 | constructive algorithms; math; sortings | Help! A robot escaped our lab and we need help finding it. The lab is at the point (0, 0) of the coordinate plane, at time 0 the robot was there. The robot's movements are defined by a program β a string of length l, consisting of characters U, L, D, R. Each second the robot executes the next command in his program: if... | The first line of the input contains two space-separated integers n and l (1 β€ n β€ 2Β·105, 1 β€ l β€ 2Β·106).Next n lines contain three space-separated integers β ti, xi, yi (1 β€ ti β€ 1018, - 1018 β€ xi, yi β€ 1018). The radar data is given chronologically, i.e. ti < ti + 1 for all i from 1 to n - 1. | Print any of the possible programs that meet the data. If no program meets the data, print a single word 'NO' (without the quotes). | Input: 3 31 1 02 1 -13 0 -1 | Output: RDL | Master | 3 | 1,045 | 295 | 131 | 5 | |
1,747 | D | 1747D | D. Yet Another Problem | 1,900 | binary search; bitmasks; constructive algorithms; data structures | You are given an array \(a\) of \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\).You have to answer \(q\) independent queries, each consisting of two integers \(l\) and \(r\). Consider the subarray \(a[l:r]\) \(=\) \([a_l, a_{l+1}, \ldots, a_r]\). You can apply the following operation to the subarray any number of times (... | The first line contains two integers \(n\) and \(q\) \((1 \le n, q \le 2 \cdot 10^5)\) β the length of the array \(a\) and the number of queries.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((0 \le a_i \lt 2^{30})\) β the elements of the array \(a\).The \(i\)-th of the next \(q\) lines contains two ... | For each query, output a single integer β the answer to that query. | In the first query, \(l = 3, r = 4\), subarray = \([3, 3]\). We can apply operation only to the subarrays of length \(1\), which won't change the array; hence it is impossible to make all elements equal to \(0\).In the second query, \(l = 4, r = 6\), subarray = \([3, 1, 2]\). We can choose the whole subarray \((L = 4, ... | Input: 7 6 3 0 3 3 1 2 3 3 4 4 6 3 7 5 6 1 6 2 2 | Output: -1 1 1 -1 2 0 | Hard | 4 | 792 | 419 | 67 | 17 |
1,990 | A | 1990A | A. Submission Bait | 900 | brute force; games; greedy; sortings | Alice and Bob are playing a game in an array \(a\) of size \(n\).They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable \(mx\) is set to \(0\).In one operation, a player can do: Choose an index \(i\) (\(1 \le i \le n\)) such that \(a_{i} \geq mx\) and... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.For each test case: The first line contains an integer \(n\) (\(2 \leq n \leq 50\)) β the size of the array. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) β the elements of the array. | For each test case, if Alice has a winning strategy, output ""YES"". Otherwise, output ""NO"".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, Alice can choose \(i=1\) since \(a_1=2 \ge mx=0\).After Alice's operation, \(a=[0,1]\) and \(mx=2\). Bob can not do any operation. Alice wins.In the second test case, Alice doesn't have a winning strategy.For example, if Alice chooses \(i=1\), after Alice's operation: \(a=[0,1]\) and \(mx=1\). T... | Input: 522 121 133 3 343 3 4 441 2 2 2 | Output: YES NO YES NO YES | Beginner | 4 | 422 | 320 | 255 | 19 |
1,114 | B | 1114B | B. Yet Another Array Partitioning Task | 1,500 | constructive algorithms; greedy; sortings | An array \(b\) is called to be a subarray of \(a\) if it forms a continuous subsequence of \(a\), that is, if it is equal to \(a_l\), \(a_{l + 1}\), \(\ldots\), \(a_r\) for some \(l, r\).Suppose \(m\) is some known constant. For any array, having \(m\) or more elements, let's define it's beauty as the sum of \(m\) larg... | The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m\), \(2 \le k\), \(m \cdot k \le n\)) β the number of elements in \(a\), the constant \(m\) in the definition of beauty and the number of subarrays to split to.The second line contains \(n\) integers \(a_1, a_2, \ldots... | In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.In the second line, print \(k-1\) integers \(p_1, p_2, \ldots, p_{k-1}\) (\(1 \le p_1 < p_2 < \ldots < p_{k-1} < n\)) representing the partition of the array, in which: All elements with indices from \(1\) to \(p... | In the first example, one of the optimal partitions is \([5, 2, 5]\), \([2, 4]\), \([1, 1, 3, 2]\). The beauty of the subarray \([5, 2, 5]\) is \(5 + 5 = 10\). The beauty of the subarray \([2, 4]\) is \(2 + 4 = 6\). The beauty of the subarray \([1, 1, 3, 2]\) is \(3 + 2 = 5\). The sum of their beauties is \(10 + 6 + 5 ... | Input: 9 2 3 5 2 5 2 4 1 1 3 2 | Output: 21 3 5 | Medium | 3 | 953 | 357 | 605 | 11 |
142 | E | 142E | E. Help Greg the Dwarf 2 | 3,000 | geometry | Greg the Dwarf has been really busy recently with excavations by the Neverland Mountain. However for the well-known reasons (as you probably remember he is a very unusual dwarf and he cannot stand sunlight) Greg can only excavate at night. And in the morning he should be in his crypt before the first sun ray strikes. T... | The first input line contains space-separated integers r and h (1 β€ r, h β€ 1000) β the base radius and the cone height correspondingly. The second and third lines contain coordinates of two points on the cone surface, groups of three space-separated real numbers. The coordinates of the points are given in the systems o... | Print the length of the shortest path between the points given in the input, with absolute or relative error not exceeding 10 - 6. | Input: 2 21.0 0.0 0.0-1.0 0.0 0.0 | Output: 2.000000000 | Master | 1 | 1,764 | 876 | 130 | 1 | |
1,552 | F | 1552F | F. Telepanting | 2,200 | binary search; data structures; dp; sortings | An ant moves on the real line with constant speed of \(1\) unit per second. It starts at \(0\) and always moves to the right (so its position increases by \(1\) each second).There are \(n\) portals, the \(i\)-th of which is located at position \(x_i\) and teleports to position \(y_i < x_i\). Each portal can be either a... | The first line contains the integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the number of portals.The \(i\)-th of the next \(n\) lines contains three integers \(x_i\), \(y_i\) and \(s_i\) (\(1\le y_i < x_i\le 10^9\), \(s_i\in\{0,1\}\)) β the position of the \(i\)-th portal, the position where the ant is teleported when it ... | Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position \(x_n+1\). Since the answer may be very large, output it modulo \(998\,244\,353\). | Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed \(1\) and the time spent during the movement is written above the arrow). $$$\( 0 \stackrel{6}{\longrightarrow} 6 \leadsto 5 \stackrel{3}{\longrightarrow} 8 \leadsto 1 \sta... | Input: 4 3 2 0 6 5 1 7 4 0 8 1 1 | Output: 23 | Hard | 4 | 1,109 | 633 | 207 | 15 |
1,671 | B | 1671B | B. Consecutive Points Segment | 1,000 | brute force; math; sortings | You are given \(n\) points with integer coordinates on a coordinate axis \(OX\). The coordinate of the \(i\)-th point is \(x_i\). All points' coordinates are distinct and given in strictly increasing order.For each point \(i\), you can do the following operation no more than once: take this point and move it by \(1\) t... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of points in the set \(x\).The second line of the test case contains \(n\)... | For each test case, print the answer β if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO. | Input: 521 431 2 341 2 3 71100000032 5 6 | Output: YES YES NO YES YES | Beginner | 3 | 954 | 671 | 165 | 16 | |
643 | F | 643F | F. Bears and Juice | 2,900 | dp; math; meet-in-the-middle | There are n bears in the inn and p places to sleep. Bears will party together for some number of nights (and days).Bears love drinking juice. They don't like wine but they can't distinguish it from juice by taste or smell.A bear doesn't sleep unless he drinks wine. A bear must go to sleep a few hours after drinking a w... | The only line of the input contains three integers n, p and q (1 β€ n β€ 109, 1 β€ p β€ 130, 1 β€ q β€ 2 000 000) β the number of bears, the number of places to sleep and the number of scenarios, respectively. | Print one integer, equal to . | In the first sample, there are 5 bears and only 1 place to sleep. We have R1 = 6, R2 = 11, R3 = 16 so the answer is . Let's analyze the optimal strategy for scenario with 2 days. There are R2 = 11 barrels and 10 of them contain juice. In the first night, the i-th bear chooses a barrel i only. If one of the first 5 barr... | Input: 5 1 3 | Output: 32 | Master | 3 | 1,583 | 203 | 29 | 6 |
316 | C2 | 316C2 | C2. Tidying Up | 2,300 | flows; graph matchings | Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dre... | The first line contains two space-separated integers n and m. They correspond to the dressing room size. Next n lines contain m space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. It is guaranteed that: nΒ·m is even. All numbers, corresponding to the numbers of ... | Print exactly one integer β the minimum number of the sneakers that need to change their location. | The second sample. | Input: 2 31 1 22 3 3 | Output: 2 | Expert | 2 | 1,621 | 583 | 98 | 3 |
2,025 | E | 2025E | E. Card Game | 2,200 | combinatorics; dp; fft; greedy; math | In the most popular card game in Berland, a deck of \(n \times m\) cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from \(1\) to \(n\), and ranks are numbered from \(1\) to \(m\). There is exactly one card in the deck for each combination of suit and rank.A card with suit \(a\... | The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 500\)).Additional constraint on the input: \(m\) is even. | Print a single integer β the number of ways to distribute the cards so that the first player wins, taken modulo \(998244353\). | Input: 1 4 | Output: 2 | Hard | 5 | 1,356 | 127 | 126 | 20 | |
370 | E | 370E | E. Summer Reading | 2,500 | dp; greedy | At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer.As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the li... | The first line contains integer n β the number of summer days (2 β€ n β€ 2Β·105). The second line contains n integers a1, a2, ... an β the records in the diary in the order they were written (0 β€ ai β€ 105). If Vasya forgot to write the number of the book on the i-th day, then ai equals 0. | If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print ""-1"". Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line print n integers β the diary with correctly inserted rec... | Input: 70 1 0 0 0 3 0 | Output: 31 1 2 2 3 3 3 | Expert | 2 | 1,170 | 286 | 393 | 3 | |
40 | C | 40C | C. Berland Square | 2,300 | implementation; math | Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1, 2, ..., K and the center i... | The first line contains integers N, x, M, y. (1 β€ N, M β€ 100000, - 100000 β€ x, y β€ 100000, x β y). | Print the sought number of parts. | Picture for the third sample: | Input: 1 0 1 1 | Output: 4 | Expert | 2 | 488 | 98 | 33 | 0 |
1,919 | B | 1919B | B. Plus-Minus Split | 800 | greedy | You are given a string \(s\) of length \(n\) consisting of characters ""+"" and ""-"". \(s\) represents an array \(a\) of length \(n\) defined by \(a_i=1\) if \(s_i=\) ""+"" and \(a_i=-1\) if \(s_i=\) ""-"".You will do the following process to calculate your penalty: Split \(a\) into non-empty arrays \(b_1,b_2,\ldots,b... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)) β the length of string \(s\).The second line of ea... | For each test case, output a single integer representing the minimum possible penalty you will receive. | In the first test case, we have \(a=[1]\). We can split array \(a\) into \(([1])\). Then, the sum of penalties of the subarrays is \(p([1]) = 1\).In the second test case, we have \(a=[-1,-1,-1,-1,-1]\). We can split array \(a\) into \(([-1],[-1],[-1],[-1],[-1])\). Then, the sum of penalties of the subarrays is \(p([-1]... | Input: 51+5-----6+-+-+-10--+++++++-20+---++++-+++++---++- | Output: 1 5 0 4 4 | Beginner | 1 | 1,064 | 486 | 103 | 19 |
1,743 | E | 1743E | E. FTL | 2,400 | binary search; dp | Monocarp is playing a video game. In the game, he controls a spaceship and has to destroy an enemy spaceship.Monocarp has two lasers installed on his spaceship. Both lasers \(1\) and \(2\) have two values: \(p_i\) β the power of the laser; \(t_i\) β the reload time of the laser. When a laser is fully charged, Monocarp ... | The first line contains two integers \(p_1\) and \(t_1\) (\(2 \le p_1 \le 5000\); \(1 \le t_1 \le 10^{12}\)) β the power and the reload time of the first laser.The second line contains two integers \(p_2\) and \(t_2\) (\(2 \le p_2 \le 5000\); \(1 \le t_2 \le 10^{12}\)) β the power and the reload time of the second lase... | Print a single integer β the lowest amount of time it can take Monocarp to destroy an enemy spaceship. | In the first example, Monocarp waits for both lasers to charge, then shoots both lasers at \(10\), they deal \((5 + 4 - 1) = 8\) damage. Then he waits again and shoots lasers at \(20\), dealing \(8\) more damage.In the second example, Monocarp doesn't wait for the second laser to charge. He just shoots the first laser ... | Input: 5 10 4 9 16 1 | Output: 20 | Expert | 2 | 997 | 594 | 102 | 17 |
1,690 | B | 1690B | B. Array Decrements | 800 | greedy; implementation | Kristina has two arrays \(a\) and \(b\), each containing \(n\) non-negative integers. She can perform the following operation on array \(a\) any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element \(a_i\) such that \(a_i > 0\) with the value \(a_i - 1\) (... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^4\)).The second line of each test case contains exactly \(n\) non-negati... | For each test case, output on a separate line: YES, if by doing some number of operations it is possible to get an array \(b\) from an array \(a\); NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). | The first test case is analyzed in the statement.In the second test case, it is enough to apply the operation to array \(a\) once.In the third test case, it is impossible to get array \(b\) from array \(a\). | Input: 643 5 4 11 3 2 031 2 10 1 045 3 7 21 1 1 151 2 3 4 51 2 3 4 6180146 | Output: YES YES NO NO YES NO | Beginner | 2 | 948 | 621 | 287 | 16 |
171 | A | 171A | A. Mysterious numbers - 1 | 1,200 | *special; constructive algorithms | The input contains two integers a1, a2 (0 β€ ai β€ 109), separated by a single space. | Output a single integer. | Input: 3 14 | Output: 44 | Easy | 2 | 0 | 83 | 24 | 1 | ||
1,462 | B | 1462B | B. Last Year's Substring | 800 | dp; implementation; strings | Polycarp has a string \(s[1 \dots n]\) of length \(n\) consisting of decimal digits. Polycarp performs the following operation with the string \(s\) no more than once (i.e. he can perform operation \(0\) or \(1\) time): Polycarp selects two numbers \(i\) and \(j\) (\(1 \leq i \leq j \leq n\)) and removes characters fro... | The first line contains a positive integer \(t\) (\(1 \leq t \leq 1000 \)) β number of test cases in the test. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(4 \leq n \leq 200\)) β length of the string \(s\). The next line contains a string \(s\) of length \(n\) consisting of... | For each test case, output on a separate line: ""YES"" if Polycarp can turn the string \(s\) into a string ""2020"" in no more than one operation (i.e. he can perform \(0\) or \(1\) operation); ""NO"" otherwise. You may print every letter of ""YES"" and ""NO"" in any case you want (so, for example, the strings yEs, yes... | In the first test case, Polycarp could choose \(i=3\) and \(j=6\).In the second test case, Polycarp could choose \(i=2\) and \(j=5\).In the third test case, Polycarp did not perform any operations with the string. | Input: 6 8 20192020 8 22019020 4 2020 5 20002 6 729040 6 200200 | Output: YES YES YES NO NO NO | Beginner | 3 | 1,052 | 393 | 377 | 14 |
1,819 | B | 1819B | B. The Butcher | 1,900 | geometry; greedy; implementation; sortings; two pointers | Anton plays his favorite game ""Defense of The Ancients 2"" for his favorite hero β The Butcher. Now he wants to make his own dinner. To do this he will take a rectangle of height \(h\) and width \(w\), then make a vertical or horizontal cut so that both resulting parts have integer sides. After that, he will put one o... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of rectangles obtained.The \(i\)... | For each test case, on the first line output a single integer \(m\) β the number of pairs \((h, w)\) denoting the sizes of rectangles from which the given rectangles can be obtained. Two rectangles are considered different if they have different heights or widths.On each of the following \(m\) lines print output intege... | In the first test case, Butcher could only have a rectangle of size \(4 \times 5\). Then the cuts could look like this (first the green cut was made, then the red one): In the second test case, Butcher could have either a rectangle of \(1 \times 3\) or \(3 \times 1\). The cuts would have looked like this (first the gre... | Input: 431 23 51 331 11 11 1110 1043 25 52 28 7 | Output: 1 4 5 2 1 3 3 1 1 10 10 1 13 7 | Hard | 5 | 1,423 | 561 | 475 | 18 |
1,968 | C | 1968C | C. Assembly via Remainders | 1,000 | constructive algorithms; number theory | You are given an array \(x_2,x_3,\dots,x_n\). Your task is to find any array \(a_1,\dots,a_n\), where: \(1\le a_i\le 10^9\) for all \(1\le i\le n\). \(x_i=a_i \bmod a_{i-1}\) for all \(2\le i\le n\). Here \(c\bmod d\) denotes the remainder of the division of the integer \(c\) by the integer \(d\). For example \(5 \bmod... | 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\) \((2\le n\le 500)\) β the number of elements in \(a\).The second line of each test case contains \(n-1\) integers \(x_2,\dots,x_n\) \((1\le x_i\le 500)\) β the ... | For each test case output any \(a_1,\dots,a_n\) (\(1 \le a_i \le 10^9\)) which satisfies the statement. | In the first test case \(a=[3,5,4,9]\) satisfies the conditions, because: \(a_2\bmod a_1=5\bmod 3=2=x_2\); \(a_3\bmod a_2=4\bmod 5=4=x_3\); \(a_4\bmod a_3=9\bmod 4=1=x_4\); | Input: 542 4 131 164 2 5 1 2250031 5 | Output: 3 5 4 9 2 5 11 5 14 16 5 11 24 501 500 2 7 5 | Beginner | 2 | 472 | 437 | 103 | 19 |
1,236 | E | 1236E | E. Alice and the Unfair Game | 2,500 | binary search; data structures; dp; dsu | Alice is playing a game with her good friend, Marisa.There are \(n\) boxes arranged in a line, numbered with integers from \(1\) to \(n\) from left to right. Marisa will hide a doll in one of the boxes. Then Alice will have \(m\) chances to guess where the doll is. If Alice will correctly guess the number of box, where... | The first line contains two integers \(n\) and \(m\), separated by space (\(1 \leq n, m \leq 10^5\)) β the number of boxes and the number of guesses, which Alice will make.The next line contains \(m\) integers \(a_1, a_2, \ldots, a_m\), separated by spaces (\(1 \leq a_i \leq n\)), the number \(a_i\) means the number of... | Print the number of scenarios in a single line, or the number of pairs of boxes \((x, y)\) (\(1 \leq x, y \leq n\)), such that if Marisa will put the doll into the box with number \(x\), she can make tricks in such way, that at the end of the game the doll will be in the box with number \(y\) and she will win the game. | In the first example, the possible scenarios are \((1, 1)\), \((1, 2)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((3, 2)\), \((3, 3)\).Let's take \((2, 2)\) as an example. The boxes, in which the doll will be during the game can be \(2 \to 3 \to 3 \to 3 \to 2\) | Input: 3 3 2 2 2 | Output: 7 | Expert | 4 | 1,371 | 374 | 320 | 12 |
193 | A | 193A | A. Cutting Figure | 1,700 | constructive algorithms; graphs; trees | You've gotten an n Γ m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected.A set of painted squares is called connected, if for every two squ... | The first input line contains two space-separated integers n and m (1 β€ n, m β€ 50) β the sizes of the sheet of paper. Each of the next n lines contains m characters β the description of the sheet of paper: the j-th character of the i-th line equals either ""#"", if the corresponding square is painted (belongs to set A)... | On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1. | In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The dele... | Input: 5 4#####..##..##..##### | Output: 2 | Medium | 3 | 644 | 493 | 137 | 1 |
683 | H | 683H | H. Exchange of Books | 1,900 | *special | n pupils, who love to read books, study at school. It is known that each student has exactly one best friend, and each pupil is the best friend of exactly one other pupil. Each of the pupils has exactly one interesting book.The pupils decided to share books with each other. Every day, all pupils give their own books to... | The first line contains two integers n and k (2 β€ n β€ 100000, 1 β€ k β€ 1016) β the number of pupils and days during which they will exchange books.The second line contains n different integers ai (1 β€ ai β€ n), where ai is equal to the number of the pupil who has the best friend with the number i.It is guaranteed that no... | In a single line print n different integers, where i-th integer should be equal to the number of the pupil who will have the book, which the pupil with the number i had in the beginning, after k days. | The explanation to the first test.There are 4 pupils and 1 day. The list of the best friends equals to {2, 4, 1, 3}. It means that: the pupil with the number 3 β is the best friend of pupil with the number 1, the pupil with the number 1 β is the best friend of pupil with the number 2, the pupil with the number 4 β is t... | Input: 4 12 4 1 3 | Output: 3 1 4 2 | Hard | 1 | 576 | 357 | 200 | 6 |
1,176 | F | 1176F | F. Destroy it! | 2,100 | dp; implementation; sortings | You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.The boss battle consists of \(n\) turns. During each turn, you will get several cards. Each card has two parameters: its cost \(c_i\) and damage \(d_i\). You may play some of your cards during e... | The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of turns.Then \(n\) blocks of input follow, the \(i\)-th block representing the cards you get during the \(i\)-th turn.Each block begins with a line containing one integer \(k_i\) (\(1 \le k_i \le 2 \cdot 10^5\)) β the number of cards... | Print one integer β the maximum damage you may deal. | In the example test the best course of action is as follows:During the first turn, play all three cards in any order and deal \(18\) damage.During the second turn, play both cards and deal \(7\) damage.During the third turn, play the first and the third card and deal \(13\) damage.During the fourth turn, play the first... | Input: 5 3 1 6 1 7 1 5 2 1 4 1 3 3 1 10 3 5 2 3 3 1 15 2 4 1 10 1 1 100 | Output: 263 | Hard | 3 | 852 | 588 | 52 | 11 |
1,678 | A | 1678A | A. Tokitsukaze and All Zero Sequence | 800 | implementation | Tokitsukaze has a sequence \(a\) of length \(n\). For each operation, she selects two numbers \(a_i\) and \(a_j\) (\(i \ne j\); \(1 \leq i,j \leq n\)). If \(a_i = a_j\), change one of them to \(0\). Otherwise change both of them to \(\min(a_i, a_j)\). Tokitsukaze wants to know the minimum number of operations to change... | The first line contains a single positive integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 100\)) β the length of the sequence \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 1... | For each test case, print a single integer β the minimum number of operations to change all numbers in the sequence to \(0\). | In the first test case, one of the possible ways to change all numbers in the sequence to \(0\):In the \(1\)-st operation, \(a_1 < a_2\), after the operation, \(a_2 = a_1 = 1\). Now the sequence \(a\) is \([1,1,3]\).In the \(2\)-nd operation, \(a_1 = a_2 = 1\), after the operation, \(a_1 = 0\). Now the sequence \(a\) i... | Input: 331 2 331 2 231 2 0 | Output: 4 3 2 | Beginner | 1 | 406 | 347 | 125 | 16 |
2,030 | G2 | 2030G2 | G2. The Destruction of the Universe (Hard Version) | 3,100 | combinatorics; math | This is the hard version of the problem. In this version, \(n \leq 10^6\). You can only make hacks if both versions of the problem are solved.Orangutans are powerful beingsβso powerful that they only need \(1\) unit of time to destroy every vulnerable planet in the universe!There are \(n\) planets in the universe. Each... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^6\)) β the number of planets in the universe.The following \(n\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β t... | For each test case, output an integer β the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo \(998\,244\,353\). | In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets \(\{[1,1]\}, \{[2,3]\}, \{[3,3]\}\), the score is \(0\). For the subset \(\{[2,3], [3,3]\}\), the score is \(0\), because the point \(3\) is already contained in both planets' interval of vulnerability. For the... | Input: 331 12 33 341 42 32 41 151 22 33 44 51 5 | Output: 5 6 24 | Master | 2 | 1,147 | 464 | 148 | 20 |
305 | B | 305B | B. Continued Fractions | 1,700 | brute force; implementation; math | A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as and the other one is represented as a finite fraction of height n. Check if they are equal. | The first line contains two space-separated integers p, q (1 β€ q β€ p β€ 1018) β the numerator and the denominator of the first fraction.The second line contains integer n (1 β€ n β€ 90) β the height of the second fraction. The third line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 1018) β the continued f... | Print ""YES"" if these fractions are equal and ""NO"" otherwise. | In the first sample .In the second sample .In the third sample . | Input: 9 422 4 | Output: YES | Medium | 3 | 206 | 475 | 64 | 3 |
1,061 | E | 1061E | E. Politics | 2,600 | flows; graphs | There are \(n\) cities in the country. Two candidates are fighting for the post of the President. The elections are set in the future, and both candidates have already planned how they are going to connect the cities with roads. Both plans will connect all cities using \(n - 1\) roads only. That is, each plan can be vi... | The first line contains integers \(n\), \(x\) and \(y\) (\(1 \le n \le 500\), \(1 \le x, y \le n\)) β the number of cities, the capital of the first candidate and the capital of the second candidate respectively.Next line contains integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\,000\)) β the revenue gained if t... | Print exactly one integer β the maximum possible revenue that can be gained, while satisfying demands of both candidates, or -1 if it is not possible to satisfy all of the demands. | In the first example, it is optimal to build ports in cities \(2\), \(3\) and \(4\), which fulfills all demands of both candidates and gives revenue equal to \(2 + 3 + 4 = 9\).In the second example, it is optimal to build ports in cities \(2\) and \(3\), which fulfills all demands of both candidates and gives revenue e... | Input: 4 1 21 2 3 41 21 33 41 22 31 421 34 112 3 | Output: 9 | Expert | 2 | 1,176 | 1,651 | 180 | 10 |
1,931 | A | 1931A | A. Recovering a Small String | 800 | brute force; strings | Nikita had a word consisting of exactly \(3\) lowercase Latin letters. The letters in the Latin alphabet are numbered from \(1\) to \(26\), where the letter ""a"" has the index \(1\), and the letter ""z"" has the index \(26\).He encoded this word as the sum of the positions of all the characters in the alphabet. For ex... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the test.This is followed by the descriptions of the test cases.The first and only line of each test case contains an integer \(n\) (\(3 \le n \le 78\)) β the encoded word. | For each test case, output the lexicographically smallest three-letter word that could have been encoded on a separate line. | Input: 5247035548 | Output: aav rzz aaa czz auz | Beginner | 2 | 1,085 | 284 | 124 | 19 | |
2,020 | E | 2020E | E. Expected Power | 2,000 | bitmasks; dp; math; probabilities | You are given an array of \(n\) integers \(a_1,a_2,\ldots,a_n\). You are also given an array \(p_1, p_2, \ldots, p_n\).Let \(S\) denote the random multiset (i. e., it may contain equal elements) constructed as follows: Initially, \(S\) is empty. For each \(i\) from \(1\) to \(n\), insert \(a_i\) into \(S\) with probabi... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1... | For each test case, output the expected value of \((f(S))^2\), modulo \(10^9 + 7\). | In the first test case, \(a = [1, 2]\) and each element is inserted into \(S\) with probability \(\frac{1}{2}\), since \(p_1 = p_2 = 5000\) and \(\frac{p_i}{10^4} = \frac{1}{2}\). Thus, there are \(4\) outcomes for \(S\), each happening with the same probability of \(\frac{1}{4}\): \(S = \varnothing\). In this case, \(... | Input: 421 25000 500021 11000 20006343 624 675 451 902 8206536 5326 7648 2165 9430 54281110000 | Output: 500000007 820000006 280120536 1 | Hard | 4 | 901 | 560 | 83 | 20 |
1,789 | B | 1789B | B. Serval and Inversion Magic | 800 | brute force; implementation; strings; two pointers | Serval has a string \(s\) that only consists of 0 and 1 of length \(n\). The \(i\)-th character of \(s\) is denoted as \(s_i\), where \(1\leq i\leq n\).Serval can perform the following operation called Inversion Magic on the string \(s\): Choose an segment \([l, r]\) (\(1\leq l\leq r\leq n\)). For \(l\leq i\leq r\), ch... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 10^5\)) β the length of string \(s\).The second line of each test case contains... | For each test case, print Yes if \(s\) can be a palindrome after performing Inversion Magic exactly once, and print No if not.You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). | In the first test case, Serval can perform Inversion Magic on the segment \([1,4]\). The string \(s\) will be 0110 after the magic.In the second test case, Serval can perform Inversion Magic on the segment \([1,3]\). The string \(s\) will be 01110 after the magic.In the third test case, Serval can't make \(s\) a palind... | Input: 34100151001070111011 | Output: Yes Yes No | Beginner | 4 | 794 | 494 | 251 | 17 |
796 | E | 796E | E. Exam Cheating | 2,400 | binary search; dp | Zane and Zane's crush have just decided to date! However, the girl is having a problem with her Physics final exam, and needs your help.There are n questions, numbered from 1 to n. Question i comes before question i + 1 (1 β€ i < n). Each of the questions cannot be guessed on, due to the huge penalty for wrong answers. ... | The first line contains three integers n, p, and k (1 β€ n, p β€ 1, 000, 1 β€ k β€ min(n, 50)) β the number of questions, the maximum number of times the girl can glance, and the maximum number of consecutive questions that can be looked at in one time glancing, respectively.The second line starts with one integer r (0 β€ r... | Print one integer β the maximum number of questions the girl can answer correctly. | Let (x, l, r) denote the action of looking at all questions i such that l β€ i β€ r on the answer sheet of the x-th genius.In the first sample, the girl could get 4 questions correct by performing sequence of actions (1, 1, 3) and (2, 5, 6).In the second sample, the girl could perform sequence of actions (1, 3, 5), (2, 2... | Input: 6 2 33 1 3 64 1 2 5 6 | Output: 4 | Expert | 2 | 1,010 | 826 | 82 | 7 |
895 | A | 895A | A. Pizza Separation | 1,200 | brute force; implementation | Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to ai. Vasya and Petya want to divide all piece... | The first line contains one integer n (1 β€ n β€ 360) β the number of pieces into which the delivered pizza was cut.The second line contains n integers ai (1 β€ ai β€ 360) β the angles of the sectors into which the pizza was cut. The sum of all ai is 360. | Print one integer β the minimal difference between angles of sectors that will go to Vasya and Petya. | In first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.In third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.In fourth sample Vasya can take 1 and 4 pieces, then Petya wi... | Input: 490 90 90 90 | Output: 0 | Easy | 2 | 534 | 251 | 101 | 8 |
222 | B | 222B | B. Cosmic Tables | 1,300 | data structures; implementation | The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement. UCDHP stores some secret information about meteors as an n Γ m table with integers in its cells. The order of meteors in t... | The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 1000, 1 β€ k β€ 500000) β the number of table columns and rows and the number of queries, correspondingly.Next n lines contain m space-separated numbers each β the initial state of the table. Each number p in the table is an integer and satisfi... | For each query to obtain a number (si = ""g"") print the required number. Print the answers to the queries in the order of the queries in the input. | Let's see how the table changes in the second test case.After the first operation is fulfilled, the table looks like that:2 1 41 3 5After the second operation is fulfilled, the table looks like that:1 3 52 1 4So the answer to the third query (the number located in the first row and in the third column) will be 5. | Input: 3 3 51 2 34 5 67 8 9g 3 2r 3 2c 2 3g 2 2g 3 2 | Output: 896 | Easy | 2 | 661 | 997 | 148 | 2 |
315 | A | 315A | A. Sereja and Bottles | 1,400 | brute force | Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles.Sereja knows that the i-th bottle is from brand ai, besides, you can use it to open other bottles of brand bi. You can use one bottle to... | The first line contains integer n (1 β€ n β€ 100) β the number of bottles. The next n lines contain the bottles' description. The i-th line contains two integers ai, bi (1 β€ ai, bi β€ 1000) β the description of the i-th bottle. | In a single line print a single integer β the answer to the problem. | Input: 41 12 23 34 4 | Output: 4 | Easy | 1 | 556 | 224 | 68 | 3 | |
1,792 | E | 1792E | E. Divisors and Table | 2,400 | brute force; dfs and similar; dp; number theory | You are given an \(n \times n\) multiplication table and a positive integer \(m = m_1 \cdot m_2\). A \(n \times n\) multiplication table is a table with \(n\) rows and \(n\) columns numbered from \(1\) to \(n\), where \(a_{i, j} = i \cdot j\).For each divisor \(d\) of \(m\), check: does \(d\) occur in the table at leas... | The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases.The first and only line of each test case contains three integers \(n\), \(m_1\) and \(m_2\) (\(1 \le n \le 10^9\); \(1 \le m_1, m_2 \le 10^9\)) β the size of the multiplication table and the integer \(m\) represented as \(m_... | For each test case, let \(d_1, d_2, \dots, d_k\) be all divisors of \(m\) sorted in the increasing order. And let \(a_1, a_2, \dots, a_k\) be an array of answers, where \(a_i\) is equal to the minimum row index where divisor \(d_i\) occurs, or \(0\), if there is no such row.Since array \(a\) may be large, first, print ... | In the first test case, \(m = 72 \cdot 1 = 72\) and has \(12\) divisors \([1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72]\). The \(3 \times 3\) multiplication table looks like that: 123112322463369 For each divisor of \(m\) that is present in the table, the position with minimum row index is marked. So the array of answers \... | Input: 33 72 110 10 156 1 210 | Output: 6 2 10 0 8 5 | Expert | 4 | 388 | 334 | 543 | 17 |
1,090 | B | 1090B | 1,900 | Hard | 0 | 0 | 0 | 0 | 10 | |||||||
321 | A | 321A | A. Ciel and Robot | 1,700 | binary search; implementation; math | Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all: 'U': go up, (x, y) β (x, y+1); 'D': go down, (x, y) β (x, y-1); 'L': go left, (x, y) β (x-1, y)... | The first line contains two integers a and b, ( - 109 β€ a, b β€ 109). The second line contains a string s (1 β€ |s| β€ 100, s only contains characters 'U', 'D', 'L', 'R') β the command. | Print ""Yes"" if the robot will be located at (a, b), and ""No"" otherwise. | In the first and second test case, command string is ""RU"", so the robot will go right, then go up, then right, and then up and so on.The locations of its moves are (0, 0) β (1, 0) β (1, 1) β (2, 1) β (2, 2) β ...So it can reach (2, 2) but not (1, 2). | Input: 2 2RU | Output: Yes | Medium | 3 | 524 | 182 | 75 | 3 |
1,450 | F | 1450F | F. The Struggling Contestant | 2,400 | constructive algorithms; greedy | To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.The contest consists of \(n\) problems, where the tag of the \(i\)-th problem is deno... | The first line contains a single integer \(t\) (\(1\leq t\leq 10^4\)) β the number of test cases.The first line of the description of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of problems in the contest.The next line contains \(n\) integers \(a_1,a_2,\ldots a_n\) (\(1 \le a_i \l... | For each test case, if there are no permutations that satisfy the required condition, print \(-1\). Otherwise, print the minimum possible cost of a permutation that satisfies the required condition. | In the first test case, let \(p=[5, 4, 3, 2, 1, 6]\). The cost is \(1\) because we jump from \(p_5=1\) to \(p_6=6\), and \(|6-1|>1\). This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than \(1\).In the second test case, let \(p=[... | Input: 4 6 2 1 2 3 1 1 5 1 1 1 2 2 8 7 7 2 7 7 1 8 7 10 1 2 3 4 1 1 2 3 4 1 | Output: 1 3 -1 2 | Expert | 2 | 1,231 | 438 | 198 | 14 |
1,416 | D | 1416D | D. Graph and Queries | 2,600 | data structures; dsu; graphs; implementation; trees | You are given an undirected graph consisting of \(n\) vertices and \(m\) edges. Initially there is a single integer written on every vertex: the vertex \(i\) has \(p_i\) written on it. All \(p_i\) are distinct integers from \(1\) to \(n\).You have to process \(q\) queries of two types: \(1\) \(v\) β among all vertices ... | The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 3 \cdot 10^5\); \(1 \le q \le 5 \cdot 10^5\)).The second line contains \(n\) distinct integers \(p_1\), \(p_2\), ..., \(p_n\), where \(p_i\) is the number initially written on vertex \(i\) (\(1 \le p_i \le n\)).Th... | For every query of the first type, print the value of \(p_u\) written on the chosen vertex \(u\). | Input: 5 4 6 1 2 5 4 3 1 2 2 3 1 3 4 5 1 1 2 1 2 3 1 1 1 2 1 2 | Output: 5 1 2 0 | Expert | 5 | 899 | 980 | 97 | 14 | |
1,137 | B | 1137B | B. Camp Schedule | 1,600 | greedy; hashing; strings | The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule \(s\), which can be represented as a binary string, in which the \(i\)... | The first line contains string \(s\) (\(1 \leqslant |s| \leqslant 500\,000\)), denoting the current project of the camp's schedule.The second line contains string \(t\) (\(1 \leqslant |t| \leqslant 500\,000\)), denoting the optimal schedule according to Gleb.Strings \(s\) and \(t\) contain characters '0' and '1' only. | In the only line print the schedule having the largest number of substrings equal to \(t\). Printed schedule should consist of characters '0' and '1' only and the number of zeros should be equal to the number of zeros in \(s\) and the number of ones should be equal to the number of ones in \(s\).In case there multiple ... | In the first example there are two occurrences, one starting from first position and one starting from fourth position.In the second example there is only one occurrence, which starts from third position. Note, that the answer is not unique. For example, if we move the first day (which is a day off) to the last positio... | Input: 101101 110 | Output: 110110 | Medium | 3 | 996 | 319 | 357 | 11 |
628 | C | 628C | C. Bear and String Distance | 1,300 | greedy; strings | Limak is a little polar bear. He likes nice strings β strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distance... | The first line contains two integers n and k (1 β€ n β€ 105, 0 β€ k β€ 106).The second line contains a string s of length n, consisting of lowercase English letters. | If there is no string satisfying the given conditions then print ""-1"" (without the quotes).Otherwise, print any nice string s' that . | Input: 4 26bear | Output: roar | Easy | 2 | 807 | 161 | 135 | 6 | |
1,399 | E1 | 1399E1 | E1. Weights Division (easy version) | 2,000 | data structures; dfs and similar; greedy; trees | Easy and hard versions are actually different problems, so we advise you to read both statements carefully.You are given a weighted rooted tree, vertex \(1\) is the root of this tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex \(v\) is the last di... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(S\) (\(2 \le n \le 10^5; 1 \le S \le 10^{16}\)) β the number of vertices in the tree and the maximum possible s... | For each test case, print the answer: the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most \(S\). | Input: 3 3 20 2 1 8 3 1 7 5 50 1 3 100 1 5 10 2 3 123 5 4 55 2 100 1 2 409 | Output: 0 8 3 | Hard | 4 | 1,388 | 716 | 152 | 13 | |
1,063 | E | 1063E | E. Lasers and Mirrors | 3,000 | constructive algorithms; math | Oleg came to see the maze of mirrors. The maze is a \(n\) by \(n\) room in which each cell is either empty or contains a mirror connecting opposite corners of this cell. Mirrors in this maze reflect light in a perfect way, which causes the interesting visual effects and contributes to the loss of orientation in the maz... | The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the size of the maze.The second line contains a permutation of \(n\) integers \(a_i\) (\(1 \le a_i \le n\)), where \(a_i\) defines the number of the receiver, to which the beam from \(i\)-th laser should come. | In the first line print the maximum possible number of laser beams, which can come to the receivers they should.In the next \(n\) lines of length \(n\) print the arrangement of mirrors, causing such number of laser beams to come where they should. If the corresponding cell is empty, print ""."", otherwise print ""/"" o... | The picture illustrates the arrangements of the mirrors in the first example. | Input: 44 1 3 2 | Output: 3.\..\\../../...\ | Master | 2 | 1,213 | 281 | 713 | 10 |
845 | E | 845E | E. Fire in the City | 2,400 | binary search; data structures | The capital of Berland looks like a rectangle of size n Γ m of the square blocks of same size.Fire!It is known that k + 1 blocks got caught on fire (k + 1 β€ nΒ·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct.The... | The first line contains three integers n, m and k (1 β€ n, m β€ 109, 1 β€ k β€ 500).Each of the next k lines contain two integers xi and yi (1 β€ xi β€ n, 1 β€ yi β€ m) β coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. | Print the minimal time it takes the fire to lighten up the whole city (in minutes). | In the first example the last block can have coordinates (4, 4).In the second example the last block can have coordinates (8, 3). | Input: 7 7 31 22 15 5 | Output: 3 | Expert | 2 | 1,040 | 283 | 83 | 8 |
1,503 | C | 1503C | C. Travelling Salesman Problem | 2,200 | binary search; data structures; dp; greedy; shortest paths; sortings; two pointers | There are \(n\) cities numbered from \(1\) to \(n\), and city \(i\) has beauty \(a_i\).A salesman wants to start at city \(1\), visit every city exactly once, and return to city \(1\).For all \(i\ne j\), a flight from city \(i\) to city \(j\) costs \(\max(c_i,a_j-a_i)\) dollars, where \(c_i\) is the price floor enforce... | The first line contains a single integer \(n\) (\(2\le n\le 10^5\)) β the number of cities.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\), \(c_i\) (\(0\le a_i,c_i\le 10^9\)) β the beauty and price floor of the \(i\)-th city. | Output a single integer β the minimum total cost. | In the first test case, we can travel in order \(1\to 3\to 2\to 1\). The flight \(1\to 3\) costs \(\max(c_1,a_3-a_1)=\max(9,4-1)=9\). The flight \(3\to 2\) costs \(\max(c_3, a_2-a_3)=\max(1,2-4)=1\). The flight \(2\to 1\) costs \(\max(c_2,a_1-a_2)=\max(1,1-2)=1\). The total cost is \(11\), and we cannot do better. | Input: 3 1 9 2 1 4 1 | Output: 11 | Hard | 7 | 441 | 244 | 49 | 15 |
1,744 | C | 1744C | C. Traffic Light | 1,000 | binary search; implementation; two pointers | You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every \(n\) seconds and at the \(i\)-th second the color \(s_i\) is on.That way, the order of the colors is described ... | The first line contains a single integer \(t\) \((1 \leq t \leq 10^4\)) β the number of test cases.Then the description of the test cases follows.The first line of each test case contains an integer \(n\) and a symbol \(c\) (\(1 \leq n \leq 2 \cdot 10^5\), \(c\) is one of allowed traffic light colors r, y or g)β the le... | For each test case output the minimal number of second in which you are guaranteed to cross the road. | The first test case is explained in the statement.In the second test case the green color is on so you can cross the road immediately. In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we wou... | Input: 65 rrggry1 gg3 rrrg5 yyrrgy7 rrgrgyrg9 yrrrgyyygy | Output: 3 0 2 4 1 4 | Beginner | 3 | 1,367 | 699 | 101 | 17 |
583 | B | 583B | B. Robot's Task | 1,200 | greedy; implementation | Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at ... | The first line contains number n (1 β€ n β€ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 β€ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information. | Print a single number β the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information. | In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.In the second sample to c... | Input: 30 2 0 | Output: 1 | Easy | 2 | 1,000 | 242 | 147 | 5 |
1,973 | C | 1973C | C. Cat, Fox and Double Maximum | 1,700 | constructive algorithms; greedy; implementation; math; sortings | Fox loves permutations! She came up with the following problem and asked Cat to solve it:You are given an even positive integer \(n\) and a permutation\(^\dagger\) \(p\) of length \(n\). The score of another permutation \(q\) of length \(n\) is the number of local maximums in the array \(a\) of length \(n\), where \(a_... | The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases in the input you will have to solve.The first line of each test case contains one even integer \(n\) (\(4 \leq n \leq 10^5\), \(n\) is even) β the length of the permutation \(p\).The second line of each test case conta... | For each test case, output one line containing any permutation of length \(n\) (the array \(q\)), such that \(q\) maximizes the score under the given constraints. | In the first example, \(a = [3, 6, 4, 7]\). The array has just one local maximum (on the second position), so the score of the chosen permutation \(q\) is \(1\). It can be proven that this score is optimal under the constraints.In the last example, the resulting array \(a = [6, 6, 12, 7, 14, 7, 14, 6]\) has \(3\) local... | Input: 441 2 3 444 3 1 266 5 1 4 2 381 2 4 5 7 6 8 3 | Output: 2 4 1 3 3 1 4 2 2 5 1 4 3 6 5 4 8 2 7 1 6 3 | Medium | 5 | 1,061 | 540 | 162 | 19 |
85 | E | 85E | E. Guard Towers | 2,600 | binary search; dsu; geometry; graphs; sortings | In a far away kingdom lives a very greedy king. To defend his land, he built n guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower.Durin... | The first line contains an integer n (2 β€ n β€ 5000), n is the number of guard towers. Then follow n lines, each of which contains two integers x, y β the coordinates of the i-th tower (0 β€ x, y β€ 5000). No two towers are present at one point.Pretest 6 is one of the maximal tests for this problem. | Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals. Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109 + 7). | In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in... | Input: 20 01 1 | Output: 02 | Expert | 5 | 1,769 | 297 | 281 | 0 |
3 | B | 3B | B. Lorry | 1,900 | greedy; sortings | A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times b... | The first line contains a pair of integer numbers n and v (1 β€ n β€ 105; 1 β€ v β€ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi... | In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them. | Input: 3 21 22 71 3 | Output: 72 | Hard | 2 | 1,005 | 529 | 225 | 0 | |
1,839 | E | 1839E | E. Decreasing Game | 2,400 | constructive algorithms; dfs and similar; dp; greedy; interactive | This is an interactive problem.Consider the following game for two players: Initially, an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) is written on blackboard. Game consists of rounds. On each round, the following happens: The first player selects any \(i\) such that \(a_i \gt 0\). If there is no such \... | The first line contains a single integer \(n\) (\(1 \le n \le 300\)) β the length of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 300\)) β array \(a\). | In the first example \(n = 4\) and array \(a\) is \([\, 10, 4, 6, 3 \,]\). The game goes as follows: After reading array \(a\) contestant's program chooses to play as the first player and prints ""First"". First round: the first player chooses \(i = 1\), the second player chooses \(j = 3\). \(d = \min(a_1, a_3) = \min(... | Input: 4 10 4 6 3 3 1 0 | Output: First 1 2 4 | Expert | 5 | 849 | 201 | 0 | 18 | |
1,914 | F | 1914F | F. Programming Competition | 1,900 | dfs and similar; dp; graph matchings; greedy; trees | BerSoft is the biggest IT corporation in Berland. There are \(n\) employees at BerSoft company, numbered from \(1\) to \(n\). The first employee is the head of the company, and he does not have any superiors. Every other employee \(i\) has exactly one direct superior \(p_i\).Employee \(x\) is considered to be a superio... | 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\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of employees.The second line contains \(n-1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i \le n\)), where \(p_i... | For each test case, print a single integer β the maximum possible number of teams according to the aforementioned rules. | In the first test case, team \((3, 4)\) can be created.In the second test case, no team can be created, because there are only \(2\) employees and one is the superior of another.In the third test case, team \((2, 3)\) can be created.In the fourth test case, teams \((2, 4)\), \((3, 5)\) and \((6, 7)\) can be created.In ... | Input: 641 2 12155 5 5 171 2 1 1 3 371 1 3 2 2 471 2 1 1 1 3 | Output: 1 0 1 3 3 3 | Hard | 5 | 1,091 | 453 | 120 | 19 |
407 | E | 407E | E. k-d-sequence | 3,100 | data structures | We'll call a sequence of integers a good k-d sequence if we can add to it at most k numbers in such a way that after the sorting the sequence will be an arithmetic progression with difference d.You got hold of some sequence a, consisting of n integers. Your task is to find its longest contiguous subsegment, such that i... | The first line contains three space-separated integers n, k, d (1 β€ n β€ 2Β·105; 0 β€ k β€ 2Β·105; 0 β€ d β€ 109). The second line contains n space-separated integers: a1, a2, ..., an ( - 109 β€ ai β€ 109) β the actual sequence. | Print two space-separated integers l, r (1 β€ l β€ r β€ n) show that sequence al, al + 1, ..., ar is the longest subsegment that is a good k-d sequence.If there are multiple optimal answers, print the one with the minimum value of l. | In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6 β after adding number 4 and sorting it becomes sequence 2, 4, 6, 8 β the arithmetic progression with difference 2. | Input: 6 1 24 3 2 8 6 2 | Output: 3 5 | Master | 1 | 345 | 219 | 230 | 4 |
1,538 | A | 1538A | A. Stone Game | 800 | brute force; dp; greedy | Polycarp is playing a new computer game. This game has \(n\) stones in a row. The stone on the position \(i\) has integer power \(a_i\). The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the right... | The first line contains an integer \(t\) (\(1 \le t \le 100\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(2 \le n \le 100\)) β the number of stones.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the power of the stones... | For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. | Input: 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 | Output: 2 4 5 3 2 | Beginner | 3 | 1,321 | 321 | 125 | 15 | |
1,343 | C | 1343C | C. Alternating Subsequence | 1,200 | dp; greedy; two pointers | Recall that the sequence \(b\) is a a subsequence of the sequence \(a\) if \(b\) can be derived from \(a\) by removing zero or more elements without changing the order of the remaining elements. For example, if \(a=[1, 2, 1, 3, 1, 2, 1]\), then possible subsequences are: \([1, 1, 1, 1]\), \([3]\) and \([1, 2, 1, 3, 1, ... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\). The second line of the test case contains \(n\) integers \(a... | For each test case, print the answer β the maximum sum of the maximum by size (length) alternating subsequence of \(a\). | In the first test case of the example, one of the possible answers is \([1, 2, \underline{3}, \underline{-1}, -2]\).In the second test case of the example, one of the possible answers is \([-1, -2, \underline{-1}, -3]\).In the third test case of the example, one of the possible answers is \([\underline{-2}, 8, 3, \unde... | Input: 4 5 1 2 3 -1 -2 4 -1 -2 -1 -3 10 -2 8 3 8 -4 -15 5 -2 -3 1 6 1 -1000000000 1 -1000000000 1 -1000000000 | Output: 2 -1 6 -2999999997 | Easy | 3 | 1,075 | 552 | 120 | 13 |
337 | A | 337A | A. Puzzles | 900 | greedy | The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddl... | The first line contains space-separated integers n and m (2 β€ n β€ m β€ 50). The second line contains m space-separated integers f1, f2, ..., fm (4 β€ fi β€ 1000) β the quantities of pieces in the puzzles sold in the shop. | Print a single integer β the least possible difference the teacher can obtain. | Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the t... | Input: 4 610 12 10 7 5 22 | Output: 5 | Beginner | 1 | 1,014 | 218 | 78 | 3 |
710 | B | 710B | B. Optimal Point on a Line | 1,400 | brute force; sortings | You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal. | The first line contains integer n (1 β€ n β€ 3Β·105) β the number of points on the line.The second line contains n integers xi ( - 109 β€ xi β€ 109) β the coordinates of the given n points. | Print the only integer x β the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer. | Input: 41 2 3 4 | Output: 2 | Easy | 2 | 132 | 184 | 205 | 7 | |
2,084 | E | 2084E | E. Blossom | 2,400 | binary search; combinatorics; dp; implementation; math; two pointers | You are given a permutation \(a\) of length \(n\)\(^{\text{β}}\) where some elements are missing and represented by \(-1\).Define the value of a permutation as the sum of the MEX\(^{\text{β }}\) of all its non-empty subsegments\(^{\text{β‘}}\).Find the sum of the value of all possible valid permutations that can be forme... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-... | For each test case, output a single integer β the sum of the value of all possible valid permutations modulo \(10^9 + 7\). | In the first test case, the only valid permutation is \([0, 1]\), and the value of \([0, 1]\) is \(3\) since:$$$\(\operatorname{mex}([0]) + \operatorname{mex}([1]) + \operatorname{mex}([0, 1]) = 1 + 0 + 2 = 3\)\(So the answer is \)3\(.In the second test case, there are two valid permutations: \)[0, 1]\( and \)[1, 0]\(.... | Input: 520 -12-1 -132 0 13-1 2 -15-1 0 -1 2 -1 | Output: 3 6 7 10 104 | Expert | 6 | 1,178 | 498 | 122 | 20 |
1,606 | C | 1606C | C. Banknotes | 1,400 | greedy; number theory | In Berland, \(n\) different types of banknotes are used. Banknotes of the \(i\)-th type have denomination \(10^{a_i}\) burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly \(1\).Let's denote \(f(s)\) as the minimum number of banknotes required to represent exactly... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10; 1 \le k \le 10^9\)).The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 = a_1 < a_2 < \dots < a_n \le 9\)). | For each test case, print one integer β the minimum positive number of burles \(s\) that cannot be represented with \(k\) or fewer banknotes. | Input: 4 3 13 0 1 2 2 777 0 4 3 255 0 1 3 10 1000000000 0 1 2 3 4 5 6 7 8 9 | Output: 59 778 148999 999999920999999999 | Easy | 2 | 833 | 306 | 141 | 16 | |
952 | F | 952F | F. 2 + 2 != 4 | 2,400 | *special | One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution... | The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. | Reproduce the output of the reference solution, including the bug. | Input: 8-7+6-5+4-3+2-1-0 | Output: 4 | Expert | 1 | 265 | 231 | 66 | 9 | |
1,104 | B | 1104B | B. Game with string | 1,200 | data structures; implementation; math | Two people are playing a game with a string \(s\), consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to ""xaax"" than there is only one possible turn: delete ""aa"", so the string will become ""xx"... | The only line contains the string \(s\), consisting of lowercase latin letters (\(1 \leq |s| \leq 100\,000\)), where \(|s|\) means the length of a string \(s\). | If the first player wins, print ""Yes"". If the second player wins, print ""No"". | In the first example the first player is unable to make a turn, so he loses.In the second example first player turns the string into ""q"", then second player is unable to move, so he loses. | Input: abacaba | Output: No | Easy | 3 | 433 | 160 | 81 | 11 |
639 | F | 639F | F. Bear and Chemistry | 3,300 | data structures; dfs and similar; graphs; trees | Limak is a smart brown bear who loves chemistry, reactions and transforming elements.In Bearland (Limak's home) there are n elements, numbered 1 through n. There are also special machines, that can transform elements. Each machine is described by two integers ai, bi representing two elements, not necessarily distinct. ... | The first line contains three integers n, m and q (1 β€ n, q β€ 300 000, 0 β€ m β€ 300 000) β the number of elements, the number of Limak's machines and the number of gossips, respectively.Each of the next m lines contains two integers ai and bi (1 β€ ai, bi β€ n) describing one of Limak's machines.Then, the description of q... | You should print q lines. The i-th of them should contain ""YES"" (without quotes) if for the i-th gossip for each pair of elements x and y (in the set xi, 1, xi, 2, ..., xi, ni) Limak is able to succeed. Otherwise you should print ""NO"" (without quotes). | Lets look at first sample:In first gossip Radewoosh's favorite set is {4, 2} and he has no machines. Limak can tranform element 4 into 2 (so half of a task is complete) and then 2 into 3, and 3 into 4. Answer is ""YES"", so R is increased by 1.In second gossip set in the input is denoted by {6, 2} and machine by (3, 4)... | Input: 6 5 41 22 33 42 45 62 04 22 16 23 43 26 3 42 54 62 11 21 2 | Output: YESNOYESYES | Master | 4 | 1,940 | 1,516 | 256 | 6 |
1,999 | C | 1999C | C. Showering | 800 | greedy; implementation | As a computer science student, Alex faces a hard challenge β showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes \(s\) minutes to shower and a day only has \(m\) minutes! He already has \(n\) tasks planned for the day. Task \(i\) is represented as an interval \((l_i\)... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(s\), and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq s, m \leq 10^9\)) β the number of time intervals Alex already has planned, the amount of time Alex... | For each test case output ""YES"" (without quotes) if Alex can take a shower for that given test case, and ""NO"" (also 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). | Input: 43 3 103 56 89 103 3 101 23 56 73 3 101 23 56 83 4 101 26 78 9 | Output: YES YES NO YES | Beginner | 2 | 749 | 707 | 287 | 19 | |
863 | F | 863F | F. Almost Permutation | 2,200 | flows | Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.Ivan clearly remembers that there were n elements in the array, and each element was not less t... | The first line contains two integer numbers n and q (1 β€ n β€ 50, 0 β€ q β€ 100).Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 β€ ti β€ 2, 1 β€ li β€ ri β€ n, 1 β€ vi β€ n, ti denotes the type of the fact). | If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array. | Input: 3 0 | Output: 3 | Hard | 1 | 959 | 279 | 141 | 8 | |
1,398 | F | 1398F | F. Controversial Rounds | 2,500 | binary search; data structures; dp; greedy; two pointers | Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won \(x\) rounds in a row. For example, if Bob won five rounds in a row and \(x = 2\), then two sets ends.You know that Alice a... | The first line contains one integer \(n\) (\(1 \le n \le 10^6\)) β the number of rounds.The second line contains one string \(s\) of length \(n\) β the descriptions of rounds. If the \(i\)-th element of the string is 0, then Alice won the \(i\)-th round; if it is 1, then Bob won the \(i\)-th round, and if it is ?, then... | In the only line print \(n\) integers. The \(i\)-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins \(i\) rounds in a row. | Let's consider the first test case: if \(x = 1\) and \(s = 110000\) or \(s = 111000\) then there are six finished sets; if \(x = 2\) and \(s = 110000\) then there are three finished sets; if \(x = 3\) and \(s = 111000\) then there are two finished sets; if \(x = 4\) and \(s = 110000\) then there is one finished set; if... | Input: 6 11?000 | Output: 6 3 2 1 0 0 | Expert | 5 | 697 | 363 | 218 | 13 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.