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,773 | J | 1773J | J. Jumbled Trees | 2,900 | constructive algorithms; math | You are given an undirected connected graph with \(n\) vertices and \(m\) edges. Each edge has an associated counter, initially equal to \(0\). In one operation, you can choose an arbitrary spanning tree and add any value \(v\) to all edges of this spanning tree. Determine if it's possible to make every counter equal t... | The first line contains three integers \(n\), \(m\), and \(p\) β the number of vertices, the number of edges, and the prime modulus (\(1 \le n \le 500\); \(1 \le m \le 1000\); \(2 \le p \le 10^9\), \(p\) is prime).Next \(m\) lines contain three integers \(u_i\), \(v_i\), \(x_i\) each β the two endpoints of the \(i\)-th... | If the target values on counters cannot be achieved, print -1. Otherwise, print \(t\) β the number of operations, followed by \(t\) lines, describing the sequence of operations. Each line starts with integer \(v\) (\(0 \le v < p\)) β the counter increment for this operation. Then, in the same line, followed by \(n - 1\... | Input: 3 3 101 1 2 30 2 3 40 3 1 50 | Output: 3 10 1 2 20 1 3 30 2 3 | Master | 2 | 421 | 541 | 610 | 17 | |
2,048 | D | 2048D | D. Kevin and Competition Memories | 1,600 | binary search; brute force; data structures; greedy; sortings; two pointers | Kevin used to get into Rio's Memories, and in Rio's Memories, a series of contests was once held. Kevin remembers all the participants and all the contest problems from that time, but he has forgotten the specific rounds, the distribution of problems, and the exact rankings.There are \( m \) problems in total, with the... | Each test contains multiple test cases. The first line contains the number of test cases \( t \) (\( 1 \le t \le 5\cdot 10^4 \)).The first line of each test case contains two integers \( n \) and \( m \) (\( 1 \le n, m \leq 3\cdot 10^5 \)) β the number of participants and the number of problems.The second line of each ... | For each test case, output \(m\) integers β the minimum sum of Kevin's ranks for each \( k = 1, 2, \ldots, m\). | For the first test case:When \(k=1\), since each contest only contains one problem, the distribution is in fact unique. For example, in the contest which only includes the third problem (which has a difficulty of \(4\)), all participants except the \(2\)-nd can solve it. Since no one solves strictly more problems than ... | Input: 44 44 3 7 52 5 4 65 55 0 4 8 61 3 9 2 76 71 1 4 5 1 41 9 1 9 8 1 07 61 9 1 9 8 1 01 1 4 5 1 4 | Output: 7 4 2 3 6 2 1 1 2 7 3 2 1 1 1 1 15 9 5 4 4 4 | Medium | 6 | 1,919 | 713 | 111 | 20 |
47 | C | 47C | C. Crossword | 2,000 | implementation | Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplΠ΅ type so far. All of them consist of exactly six words; the words can be read only from top to bottom vertically and from the left to the right horizontally. The words are arranged in the form of a rectangular ""eight"" or infinit... | Six lines contain the given words. Every word consists of no more than 30 and no less than 3 uppercase Latin letters. | If it is impossible to solve the problem, print Impossible. Otherwise, print the sought crossword. All the empty squares should be marked as dots.If there can be several solutions to that problem, print the lexicographically minimum one. I.e. the solution where the first line is less than the first line of other soluti... | Input: NODBAAYARDAIRWAYNEWTONBURN | Output: BAA...U.I...R.R...NEWTON..A..O..YARD | Hard | 1 | 797 | 117 | 513 | 0 | |
1,684 | C | 1684C | C. Column Swapping | 1,400 | brute force; constructive algorithms; greedy; implementation; sortings | You are given a grid with \(n\) rows and \(m\) columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each \(1 \le i \le n\) and \(2 \le j \le m\) the following holds: \(a_{i,j} \ge a_{i, j-1}\... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of rows and columns respectively.Each of the n... | If after the swap it is impossible to get a good grid, output \(-1\).In the other case output \(2\) integers β the indices of the columns that should be swapped to get a good grid.If there are multiple solutions, print any. | In the first test case the grid is initially good, so we can, for example, swap the first column with itself.In the second test case it is impossible to make the grid good.In the third test case it is needed to swap the first and the second column, then the grid becomes good. | Input: 52 31 2 31 1 12 24 12 32 22 11 12 36 2 15 4 32 112 | Output: 1 1 -1 1 2 1 3 1 1 | Easy | 5 | 634 | 598 | 223 | 16 |
1,970 | C3 | 1970C3 | C3. Game on Tree (Hard) | 1,900 | dfs and similar; dp; games; trees | This is the hard version of the problem. The only difference in this version is the constraint on \(t\).Ron and Hermione are playing a game on a tree of \(n\) nodes that are initially inactive. The game consists of \(t\) rounds, each of which starts with a stone on exactly one node, which is considered as activated. A ... | The first line contains integers \(n\) (\(2 \leq n \leq 2\times 10^5\)), \(t\) (\(1 \leq t \leq n\)), the number of nodes in the tree and the number of rounds, respectively.The next \(n-1\) lines contain two integers \(1 \leq u, v \leq n\) each, corresponding to an edge of the tree.The next line contains \(t\) integers... | The output consists of \(t\) lines, each line being either ""Ron"" or ""Hermione"". | Input: 5 21 21 33 43 51 2 | Output: Ron Ron | Hard | 4 | 838 | 417 | 83 | 19 | |
1,779 | F | 1779F | F. Xorcerer's Stones | 2,500 | bitmasks; constructive algorithms; dp; trees | Misha had been banned from playing chess for good since he was accused of cheating with an engine. Therefore, he retired and decided to become a xorcerer.One day, while taking a walk in a park, Misha came across a rooted tree with nodes numbered from \(1\) to \(n\). The root of the tree is node \(1\). For each \(1\le i... | The first line contains a single integer \(n\) (\(2 \leq n \leq 2\cdot 10^5\)) β the size of the treeThe second line contains an array of integers \(a_1,a_2,\ldots, a_n\) (\(0 \leq a_i \leq 31\)), describing the number of stones in each node initially.The third line contains an array of integers \(p_2,p_3,\ldots, p_n\)... | If there is not a valid sequence of spells, output \(-1\).Otherwise, output a single integer \(q\) (\(0 \leq q \leq 2n\)) in the first line β the number of performed spells.In the second line output a sequence of integers \(v_1,v_2,\ldots,v_q\) (\(1 \leq v_i \leq n\)) β the \(i\)-th spell will be performed on the subtr... | Please refer to the following pictures for an explanation of the third test. Only the first \(4\) spells are shown since the last \(2\) do nothing. The first picture represents the tree initially with the number of stones for each node written above it in green. Changes applied by the current spell are highlighted in r... | Input: 2 13 13 1 | Output: 1 1 | Expert | 4 | 1,187 | 419 | 464 | 17 |
1,137 | E | 1137E | E. Train Car Selection | 2,700 | data structures; greedy | Vasya likes to travel by train, but doesn't like when the car he travels in is located in the tail of the train.Vasya gets on the train at the station. The train consists of \(n\) cars indexed from \(1\) to \(n\) counting from the locomotive (head of the train). Three types of events occur while the train is moving: So... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^9\), \(1 \leq m \leq 300\,000\)), the number of cars in the train at the time of departure from the station and the number of stations, respectively.Next \(m\) lines contain the descriptions of events. Each event is one of the following three type... | After each of the \(m\) queries print two integers: \(j\) and \(A_j\) β the number of the car closest to the head of the train, such that its value \(A_j\) is minimal, and the value \(A_j\) itself. | Initially the train consists of one car with \(A_1 = 0\), let's denote train as \([0]\) for simplicity. After adding one car to the head, train is \([0, 0]\). After recalculation of values with parameters \(b=1, s=1\), train is \([1, 2]\). After another recalculation of values with the parameters \(b=1, s=1\), train is... | Input: 1 8 1 1 3 1 1 3 1 1 2 1 2 1 3 1 1 2 1 3 1 5 | Output: 1 0 1 1 1 2 3 0 3 0 1 3 5 0 1 4 | Master | 2 | 1,397 | 962 | 197 | 11 |
761 | B | 761B | B. Dasha and friends | 1,300 | brute force; implementation; math | Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation:The track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you l... | The first line contains two integers n and L (1 β€ n β€ 50, n β€ L β€ 100) β the number of barriers on a track and its length. The second line contains n distinct integers in the ascending order β the distance from Kefa's start to each barrier in the order of its appearance. All integers are in the range from 0 to L - 1 in... | Print ""YES"" (without quotes), if Kefa and Sasha ran the coinciding tracks (it means that the position of all barriers coincides, if they start running from the same points on the track). Otherwise print ""NO"" (without quotes). | The first test is analyzed in the statement. | Input: 3 82 4 61 5 7 | Output: YES | Easy | 3 | 1,554 | 538 | 229 | 7 |
1,748 | D | 1748D | D. ConstructOR | 2,100 | bitmasks; chinese remainder theorem; combinatorics; constructive algorithms; math; number theory | You are given three integers \(a\), \(b\), and \(d\). Your task is to find any integer \(x\) which satisfies all of the following conditions, or determine that no such integers exist: \(0 \le x \lt 2^{60}\); \(a|x\) is divisible by \(d\); \(b|x\) is divisible by \(d\). Here, \(|\) denotes the bitwise OR operation. | Each test contains multiple test cases. The first line of input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Each test case consists of one line, containing three integers \(a\), \(b\), and \(d\) (\(1 \le a,b,d \lt 2^{30}\)). | For each test case print one integer. If there exists an integer \(x\) which satisfies all of the conditions from the statement, print \(x\). Otherwise, print \(-1\).If there are multiple solutions, you may print any of them. | In the first test case, \(x=18\) is one of the possible solutions, since \(39|18=55\) and \(12|18=30\), both of which are multiples of \(d=5\).In the second test case, \(x=14\) is one of the possible solutions, since \(8|14=6|14=14\), which is a multiple of \(d=14\).In the third and fourth test cases, we can show that ... | Input: 812 39 56 8 14100 200 2003 4 62 2 218 27 3420 666 69987654321 123456789 999999999 | Output: 18 14 -1 -1 0 11 25599 184470016815529983 | Hard | 6 | 315 | 258 | 225 | 17 |
1,936 | A | 1936A | A. Bitwise Operation Wizard | 1,700 | bitmasks; constructive algorithms; greedy; interactive; math | This is an interactive problem.There is a secret sequence \(p_0, p_1, \ldots, p_{n-1}\), which is a permutation of \(\{0,1,\ldots,n-1\}\).You need to find any two indices \(i\) and \(j\) such that \(p_i \oplus p_j\) is maximized, where \(\oplus\) denotes the bitwise XOR operation.To do this, you can ask queries. Each q... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. | In the first test case, the hidden permutation is \(p=[0,3,1,2]\).For the query ""? 0 2 3 1"", the jury return ""<"" because \((p_0 \mid p_2) = (0 \mid 1) =1 < (p_3 \mid p_1) = (2 \mid 3) = 3\).For the query ""? 1 1 2 3"", the jury return ""="" because \((p_1 \mid p_1) = (3\mid 3)= 3 = (p_2 \mid p_3) = (1 \mid 2)=3\).F... | Input: 2 4 < = > 2 | Output: ? 0 2 3 1 ? 1 1 2 3 ? 1 2 0 3 ! 3 2 ! 0 1 | Medium | 5 | 945 | 161 | 0 | 19 | |
137 | B | 137B | B. Permutation | 1,000 | greedy | ""Hey, it's homework time"" β thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him.The sequence of n integers is calle... | The first line of the input data contains an integer n (1 β€ n β€ 5000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1 β€ ai β€ 5000, 1 β€ i β€ n). | Print the only number β the minimum number of changes needed to get the permutation. | The first sample contains the permutation, which is why no replacements are required.In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation.In the third sample we can replace the second element with number 4 and the fourth element with num... | Input: 33 1 2 | Output: 0 | Beginner | 1 | 753 | 203 | 84 | 1 |
1,245 | C | 1245C | C. Constanze's Machine | 1,400 | dp | Constanze is the smartest girl in her village but she has bad eyesight.One day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe ""code"" onto... | Input consists of a single line containing a string \(s\) (\(1 \leq |s| \leq 10^5\)) β the received message. \(s\) contains only lowercase Latin letters. | Print a single integer β the number of strings that Constanze's machine would've turned into the message \(s\), modulo \(10^9+7\). | For the first example, the candidate strings are the following: ""ouuokarinn"", ""ouuokarim"", ""owokarim"", and ""owokarinn"".For the second example, there is only one: ""banana"".For the third example, the candidate strings are the following: ""nm"", ""mn"" and ""nnn"".For the last example, there are no candidate str... | Input: ouuokarinn | Output: 4 | Easy | 1 | 1,601 | 153 | 130 | 12 |
1,707 | D | 1707D | D. Partial Virtual Trees | 3,000 | combinatorics; dfs and similar; dp; math; trees | Kawashiro Nitori is a girl who loves competitive programming. One day she found a rooted tree consisting of \(n\) vertices. The root is vertex \(1\). As an advanced problem setter, she quickly thought of a problem.Kawashiro Nitori has a vertex set \(U=\{1,2,\ldots,n\}\). She's going to play a game with the tree and the... | The first line contains two integers \(n\) and \(p\) (\(2 \le n \le 2000\), \(10^8 + 7 \le p \le 10^9+9\)). It's guaranteed that \(p\) is a prime number.Each of the next \(n-1\) lines contains two integers \(u_i\), \(v_i\) (\(1 \leq u_i, v_i \leq n\)), representing an edge between \(u_i\) and \(v_i\).It is guaranteed t... | The only line contains \(n-1\) integers β the answer modulo \(p\) for \(k=1,2,\ldots,n-1\). | In the first test case, when \(k=1\), the only possible way is: \(\{1,2,3,4\} \to \{1\}\). When \(k=2\), there are \(6\) possible ways: \(\{1,2,3,4\} \to \{1,2\} \to \{1\}\); \(\{1,2,3,4\} \to \{1,2,3\} \to \{1\}\); \(\{1,2,3,4\} \to \{1,2,4\} \to \{1\}\); \(\{1,2,3,4\} \to \{1,3\} \to \{1\}\); \(\{1,2,3,4\} \to \{1,3,... | Input: 4 998244353 1 2 2 3 1 4 | Output: 1 6 6 | Master | 5 | 1,289 | 352 | 91 | 17 |
1,660 | A | 1660A | A. Vasya and Coins | 800 | greedy; math | Vasya decided to go to the grocery store. He found in his wallet \(a\) coins of \(1\) burle and \(b\) coins of \(2\) burles. He does not yet know the total cost of all goods, so help him find out \(s\) (\(s > 0\)): the minimum positive integer amount of money he cannot pay without change or pay at all using only his co... | 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 description of each test case consists of one line containing two integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^8\)) β the number of \(1\)-burle coins and \(2\)-burles coins Vasya has respecti... | For each test case, on a separate line print one integer \(s\) (\(s > 0\)): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all. | The first test case of the example is clarified into the main part of the statement. In the second test case, Vasya has only \(1\) burle coins, and he can collect either any amount from \(1\) to \(4\), but \(5\) can't. In the second test case, Vasya has only \(2\) burle coins, and he cannot pay \(1\) burle without chan... | Input: 51 14 00 20 02314 2374 | Output: 4 5 1 1 7063 | Beginner | 2 | 804 | 325 | 172 | 16 |
1,375 | F | 1375F | F. Integer Game | 2,600 | constructive algorithms; games; interactive; math | This is an interactive problem.Anton and Harris are playing a game to decide which of them is the king of problemsetting.There are three piles of stones, initially containing \(a\), \(b\), and \(c\) stones, where \(a\), \(b\), and \(c\) are distinct positive integers. On each turn of the game, the following sequence of... | The first line of input contains three distinct positive integers \(a\), \(b\), and \(c\) (\(1 \le a, b, c \le 10^9\)) β the initial number of stones in piles \(1\), \(2\), and \(3\) respectively. | In the sample input, the piles initially have \(5\), \(2\), and \(6\) stones. Harris decides to go first and provides the number \(2\) to Anton. Anton adds \(2\) stones to the third pile, which results in \(5\), \(2\), and \(8\).In the next turn, Harris chooses \(3\). Note that Anton cannot add the stones to the third ... | Input: 5 2 6 3 0 | Output: First 2 3 | Expert | 4 | 918 | 196 | 0 | 13 | |
1,893 | D | 1893D | D. Colorful Constructive | 2,600 | constructive algorithms; data structures; greedy | You have \(n\) colored cubes, the \(i\)-th cube has color \(a_i\).You need to distribute all the cubes on shelves. There are a total of \(m\) shelves, the \(i\)-th shelf can hold \(s_i\) cubes. Also, \(s_1 + s_2 + \ldots + s_m = n\).Suppose on a shelf of size \(k\) there are cubes of colors \(c_1, c_2, \ldots, c_k\), i... | Each test contains multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 10^4)\) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n, m\) \((1 \leq m \leq n \leq 2 \cdot 10^5)\) β the number of cubes and the numb... | For each test case, if it is impossible to distribute the cubes on the shelves satisfying all the requirements, output a single number \(-1\). Otherwise, output \(m\) lines, where the \(i\)-th line should contain \(s_i\) numbers representing the colors of the cubes on the \(i\)-th shelf, in the appropriate order. | Input: 610 31 1 1 1 2 2 2 3 3 46 2 24 1 18 27 7 7 7 8 8 8 84 42 25 15 4 3 2 1537 31 2 2 2 2 3 41 2 41 2 412 76 6 6 6 6 6 6 6 6 7 8 92 2 2 2 2 1 11 2 2 2 1 1 120 211 20 15 6 8 18 12 16 8 20 10 12 3 12 20 11 15 8 17 178 123 5 | Output: 1 3 4 2 1 3 1 1 2 2 8 7 8 7 8 7 8 7 2 4 5 3 1 -1 6 6 7 6 8 6 9 6 6 6 6 6 12 17 20 15 8... | Expert | 3 | 1,296 | 927 | 314 | 18 | |
1,716 | A | 1716A | A. 2-3 Moves | 800 | greedy; math | You are standing at the point \(0\) on a coordinate line. Your goal is to reach the point \(n\). In one minute, you can move by \(2\) or by \(3\) to the left or to the right (i. e., if your current coordinate is \(x\), it can become \(x-3\), \(x-2\), \(x+2\) or \(x+3\)). Note that the new coordinate can become negative... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) lines describing the test cases follow.The \(i\)-th of these lines contains one integer \(n\) (\(1 \le n \le 10^9\)) β the goal of the \(i\)-th test case. | For each test case, print one integer β the minimum number of minutes required to get from the point \(0\) to the point \(n\) for the corresponding test case. | Input: 413412 | Output: 2 1 2 4 | Beginner | 2 | 476 | 271 | 158 | 17 | |
748 | C | 748C | C. Santa Claus and Robot | 1,400 | constructive algorithms; math | Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please not... | The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled t... | The only line of input should contain the minimum possible length of the sequence. | The illustrations to the first three tests are given below. The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | Input: 4RURD | Output: 2 | Easy | 2 | 916 | 492 | 82 | 7 |
1,131 | A | 1131A | A. Sea Battle | 800 | math | In order to make the ""Sea Battle"" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of \(w_1\) and a height of \(h_1\), while the second rectangle has a width of \(w_2\) and a height of \(h_2\), where \(w_1 \ge w_2\). In this game, ... | Four lines contain integers \(w_1, h_1, w_2\) and \(h_2\) (\(1 \leq w_1, h_1, w_2, h_2 \leq 10^8\), \(w_1 \ge w_2\)) β the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles. | Print exactly one integer β the number of cells, which should be marked after the ship is destroyed. | In the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares): In the second example the field looks as: | Input: 2 1 2 1 | Output: 12 | Beginner | 1 | 1,485 | 294 | 100 | 11 |
277 | A | 277A | A. Learning Languages | 1,400 | dfs and similar; dsu | The ""BerCorp"" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official langua... | The first line contains two integers n and m (2 β€ n, m β€ 100) β the number of employees and the number of languages.Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0 β€ ki β€ m) β the number of languages the i-th employee knows. Next, the i-th line contains ki integer... | Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating). | In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2. | Input: 5 51 22 2 32 3 42 4 51 5 | Output: 0 | Easy | 2 | 704 | 568 | 177 | 2 |
1,765 | H | 1765H | H. Hospital Queue | 2,200 | binary search; graphs; greedy; implementation | There are \(n\) people (numbered from \(1\) to \(n\)) signed up for a doctor's appointment. The doctor has to choose in which order he will appoint these people. The \(i\)-th patient should be appointed among the first \(p_i\) people. There are also \(m\) restrictions of the following format: the \(i\)-th restriction i... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2000\); \(0 \le m \le 2000\)).The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\)).Then \(m\) lines follow. The \(i\)-th of them contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\); \(a_i \ne b_i\)). Al... | Print \(n\) integers, where \(i\)-th integer is equal to the minimum position of \(i\)-th patient in the order, among all valid orders. Positions in the order are numbered from \(1\) to \(n\). | In the first example, \([3, 1, 2, 4]\) the only one valid order, so the minimum position of each patient is equal to their position in this order.In the second example, any order is valid, so any patient can be appointed first.In the third example, there are three valid orders: \([4, 2, 3, 1, 5]\), \([3, 4, 2, 1, 5]\) ... | Input: 4 1 2 3 2 4 3 1 | Output: 2 3 1 4 | Hard | 4 | 926 | 519 | 192 | 17 |
1,033 | A | 1033A | A. King Escape | 1,000 | dfs and similar; graphs; implementation | Alice and Bob are playing chess on a huge chessboard with dimensions \(n \times n\). Alice has a single piece left β a queen, located at \((a_x, a_y)\), while Bob has only the king standing at \((b_x, b_y)\). Alice thinks that as her queen is dominating the chessboard, victory is hers. But Bob has made a devious plan t... | The first line contains a single integer \(n\) (\(3 \leq n \leq 1000\)) β the dimensions of the chessboard.The second line contains two integers \(a_x\) and \(a_y\) (\(1 \leq a_x, a_y \leq n\)) β the coordinates of Alice's queen.The third line contains two integers \(b_x\) and \(b_y\) (\(1 \leq b_x, b_y \leq n\)) β the... | Print ""YES"" (without quotes) if Bob can get from \((b_x, b_y)\) to \((c_x, c_y)\) without ever getting in check, otherwise print ""NO"".You can print each letter in any case (upper or lower). | In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.In the first case, the king can move, for instance, via the squares \((2, 3)\) and \((3, 2)\). Note that the direct route through \((2, 2)\) goes through check. In the second case, the queen watches the... | Input: 84 41 33 1 | Output: YES | Beginner | 3 | 887 | 893 | 193 | 10 |
2,120 | D | 2120D | D. Matrix game | 1,800 | combinatorics; math | Aryan and Harshith play a game. They both start with three integers \(a\), \(b\), and \(k\). Aryan then gives Harshith two integers \(n\) and \(m\). Harshith then gives Aryan a matrix \(X\) with \(n\) rows and \(m\) columns, such that each of the elements of \(X\) is between \(1\) and \(k\)(inclusive). After that, Arya... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. Each test case contains a single line with three space-separated integers \(a, b\) and \(k\) (\(1\leq a,b,k\leq 10^5\)).It is guaranteed that the sum of \(\ma... | For each test case, output a single line containing two space-separated integers \(n\) and \(m\), denoting the answer to the problem. The values of \(n\) and \(m\) can be large, so output them modulo \(10^9+7\). | For the first test case, every \(n\times m\) matrix contains a \(1\times 1\) submatrix with all elements equal. \((1,1)\) is the lexicographically minimum tuple among all of them.For the second test case, it can be verified that whatever \(3\times 7\) matrix Harshith gives to Aryan, Aryan can always win by finding a \(... | Input: 31 1 52 2 290000 80000 70000 | Output: 1 1 3 7 299929959 603196135 | Medium | 2 | 1,280 | 378 | 211 | 21 |
1,045 | I | 1045I | I. Palindrome Pairs | 1,600 | hashing; strings | After learning a lot about space exploration, a little girl named Ana wants to change the subject.Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whether it's a palindrome or not, but soon she grew tired of this problem, so she... | The first line contains a positive integer \(N\) (\(1 \le N \le 100\,000\)), representing the length of the input array.Eacg of the next \(N\) lines contains a string (consisting of lowercase English letters from 'a' to 'z') β an element of the input array. The total number of characters in the input array will be less... | Output one number, representing how many palindrome pairs there are in the array. | The first example: aa \(+\) bb \(\to\) abba. The second example: aab \(+\) abcac \(=\) aababcac \(\to\) aabccbaa aab \(+\) aa \(=\) aabaa abcac \(+\) aa \(=\) abcacaa \(\to\) aacbcaa dffe \(+\) ed \(=\) dffeed \(\to\) fdeedf dffe \(+\) aade \(=\) dffeaade \(\to\) adfaafde ed \(+\) aade \(=\) edaade \(\to\) aeddea | Input: 3aabbcd | Output: 1 | Medium | 2 | 1,156 | 342 | 81 | 10 |
257 | B | 257B | B. Playing Cubes | 1,300 | games; greedy; implementation | Petya and Vasya decided to play a little. They found n red cubes and m blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have n + m cubes). Petya moves first. Petya's task is to get as many pairs of nei... | The only line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of red and blue cubes, correspondingly. | On a single line print two space-separated integers β the number of Petya's and Vasya's points correspondingly provided that both players play optimally well. | In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point. If Petya would choose the red cube... | Input: 3 1 | Output: 2 1 | Easy | 3 | 982 | 129 | 158 | 2 |
1,025 | A | 1025A | A. Doggo Recoloring | 900 | implementation; sortings | Panic is rising in the committee for doggo standardization β the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.The committee rules strictly prohibit even the smallest diversity between doggo... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of puppies.The second line contains a string \(s\) of length \(n\) consisting of lowercase Latin letters, where the \(i\)-th symbol denotes the \(i\)-th puppy's color. | If it's possible to recolor all puppies into one color, print ""Yes"".Otherwise print ""No"".Output the answer without quotation signs. | In the first example Slava can perform the following steps: take all puppies of color 'a' (a total of two) and recolor them into 'b'; take all puppies of color 'd' (a total of two) and recolor them into 'c'; take all puppies of color 'b' (three puppies for now) and recolor them into 'c'. In the second example it's impo... | Input: 6aabddc | Output: Yes | Beginner | 2 | 1,452 | 250 | 135 | 10 |
1,957 | E | 1957E | E. Carousel of Combinations | 2,400 | brute force; combinatorics; dp; math; number theory | You are given an integer \(n\). The function \(C(i,k)\) represents the number of distinct ways you can select \(k\) distinct numbers from the set {\(1, 2, \ldots, i\)} and arrange them in a circle\(^\dagger\).Find the value of $$$\( \sum\limits_{i=1}^n \sum\limits_{j=1}^i \left( C(i,j) \bmod j \right). \)\( Here, the o... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)). | For each test case, output a single integer on a new line β the value of the expression to be calculated modulo \(10^9+7\). | In the first test case, \(C(1,1) \bmod 1 = 0\).In the second test case: \(C(1,1)=1\) (the arrangements are: \([1]\)); \(C(2,1)=2\) (the arrangements are: \([1]\), \([2]\)); \(C(2,2)=1\) (the arrangements are: \([1, 2]\)); \(C(3,1)=3\) (the arrangements are: \([1]\), \([2]\), \([3]\)); \(C(3,2)=3\) (the arrangements are... | Input: 4136314159 | Output: 0 4 24 78926217 | Expert | 5 | 647 | 186 | 123 | 19 |
868 | A | 868A | A. Bark to Unlock | 900 | brute force; implementation; strings | As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.Mu-mu's enemy Kashtanka wants to unlock Mu-mu's pho... | The first line contains two lowercase English letters β the password on the phone.The second line contains single integer n (1 β€ n β€ 100) β the number of words Kashtanka knows.The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct. | Print ""YES"" if Kashtanka can bark several words in a line forming a string containing the password, and ""NO"" otherwise.You can print each letter in arbitrary case (upper or lower). | In the first example the password is ""ya"", and Kashtanka can bark ""oy"" and then ""ah"", and then ""ha"" to form the string ""oyahha"" which contains the password. So, the answer is ""YES"".In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark ""ht"" and the... | Input: ya4ahoytoha | Output: YES | Beginner | 3 | 684 | 317 | 184 | 8 |
42 | D | 42D | D. Strange town | 2,300 | constructive algorithms; math | Volodya has recently visited a very odd town. There are N tourist attractions in the town and every two of them are connected by a bidirectional road. Each road has some travel price (natural number) assigned to it and all prices are distinct. But the most striking thing about this town is that each city sightseeing to... | Input contains just one natural number (3 β€ N β€ 20) β the number of town attractions. | Output should contain N rows containing N positive integer numbers each β the adjacency matrix of the prices graph (thus, j-th number in i-th row should be equal to the price of the road between the j-th and the i-th attraction). Diagonal numbers should be equal to zero. All numbers should not be greater than 1000. All... | Input: 3 | Output: 0 3 4 3 0 5 4 5 0 | Expert | 2 | 615 | 85 | 421 | 0 | |
1,116 | A2 | 1116A2 | A2. Generate equal superposition of four basis states | 0 | *special | You are given \(N\) qubits in the zero state \(|0...0 \rangle\). You are also given four distinct bit vectors which describe four different basis states on \(N\) qubits \(|\psi_i \rangle\).Your task is to generate a state which is an equal superposition of the given basis states:$$$\(|S \rangle = \frac{1}{2} \big( |\ps... | Beginner | 1 | 1,449 | 0 | 0 | 11 | ||||
1,290 | E | 1290E | E. Cartesian Tree | 3,300 | data structures | Ildar is the algorithm teacher of William and Harris. Today, Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be constructed from a sequence of distinct integers. We build the cartesian tree as follows: If the sequence is empty, ret... | The first line contains a single integer \(n\) (\(1 \le n \le 150000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)). It is guarenteed that each integer from \(1\) to \(n\) appears in the sequence exactly once. | Print \(n\) lines, \(i\)-th line should contain a single integer β the answer to the \(i\)-th question. | After the first round, the sequence is \(1\). The tree is The answer is \(1\).After the second round, the sequence is \(2, 1\). The tree is The answer is \(2+1=3\).After the third round, the sequence is \(2, 1, 3\). The tree is The answer is \(2+1+3=6\).After the fourth round, the sequence is \(2, 4, 1, 3\). The tree i... | Input: 5 2 4 1 5 3 | Output: 1 3 6 8 11 | Master | 1 | 1,848 | 253 | 103 | 12 |
1,866 | A | 1866A | A. Ambitious Kid | 800 | math | Chaneka, Pak Chanek's child, is an ambitious kid, so Pak Chanek gives her the following problem to test her ambition.Given an array of integers \([A_1, A_2, A_3, \ldots, A_N]\). In one operation, Chaneka can choose one element, then increase or decrease the element's value by \(1\). Chaneka can do that operation multip... | The first line contains a single integer \(N\) (\(1 \leq N \leq 10^5\)).The second line contains \(N\) integers \(A_1, A_2, A_3, \ldots, A_N\) (\(-10^5 \leq A_i \leq 10^5\)). | An integer representing the minimum number of operations that must be done to make it such that \(A_1 \times A_2 \times A_3 \times \ldots \times A_N = 0\). | In the first example, initially, \(A_1\times A_2\times A_3=2\times(-6)\times5=-60\). Chaneka can do the following sequence of operations: Decrease the value of \(A_1\) by \(1\). Then, \(A_1\times A_2\times A_3=1\times(-6)\times5=-30\) Decrease the value of \(A_1\) by \(1\). Then, \(A_1\times A_2\times A_3=0\times(-6)\t... | Input: 3 2 -6 5 | Output: 2 | Beginner | 1 | 497 | 174 | 155 | 18 |
1,712 | A | 1712A | A. Wonderful Permutation | 800 | greedy; implementation | God's Blessing on This PermutationForces!A Random PebbleYou are given a permutation \(p_1,p_2,\ldots,p_n\) of length \(n\) and a positive integer \(k \le n\). In one operation you can choose two indices \(i\) and \(j\) (\(1 \le i < j \le n\)) and swap \(p_i\) with \(p_j\).Find the minimum number of operations needed to... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)).The second line of each test case contains \(n\) integers \(p_1,p... | For each test case print one integer β the minimum number of operations needed to make the sum \(p_1 + p_2 + \ldots + p_k\) as small as possible. | In the first test case, the value of \(p_1 + p_2 + \ldots + p_k\) is initially equal to \(2\), but the smallest possible value is \(1\). You can achieve it by swapping \(p_1\) with \(p_3\), resulting in the permutation \([1, 3, 2]\).In the second test case, the sum is already as small as possible, so the answer is \(0\... | Input: 43 12 3 13 31 2 34 23 4 1 21 11 | Output: 1 0 2 0 | Beginner | 2 | 691 | 434 | 145 | 17 |
2,129 | D | 2129D | D. Permutation Blackhole | 2,600 | brute force; combinatorics; dp; implementation; math | For a permutation \(p_1, p_2, \ldots, p_n\) of length \(n\), the corresponding coloring sequence \(s\) can be obtained by the following coloring process: Initially, there are \(n\) white cells indexed from \(1\) to \(n\) from left to right. At second \(0\), the score of each cell is \(0\). At second \(i\) (\(1 \le i \l... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. For each test case, the first line contains an integer \(n\) (\(2 \leq n \leq 100\)).The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(-1 \... | For each test case, output the total of different permutations \(p_1, p_2, \ldots, p_n\) that can produce the coloring sequence, modulo \(998\,244\,353\). | In the first test case, \(p=[3,1,2]\) and \(p=[3,2,1]\) can produce the coloring sequence \(s=[-1,-1,1]\).For \(p=[3,1,2]\), the coloring process is shown as the following picture. The grid at seconds \(0\), \(1\), \(2\), and \(3\) respectively when \(p=[3,1,2]\). For \(p=[3,2,1]\), the coloring process is shown as the... | Input: 93-1 -1 13-1 -1 -14-1 2 -1 04-1 0 1 -15-1 3 -1 0 -154 4 4 4 451 0 1 2 06-1 1 -1 -1 3 013-1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 | Output: 2 6 4 3 8 0 4 10 867303072 | Expert | 5 | 1,182 | 540 | 154 | 21 |
1,055 | C | 1055C | C. Lucky Days | 1,900 | math; number theory | Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days \([l_a; r_a]\) are lucky, then there are some unlucky days: \([r... | The first line contains three integers \(l_a\), \(r_a\), \(t_a\) (\(0 \le l_a \le r_a \le t_a - 1, 2 \le t_a \le 10^9\)) and describes Alice's lucky days.The second line contains three integers \(l_b\), \(r_b\), \(t_b\) (\(0 \le l_b \le r_b \le t_b - 1, 2 \le t_b \le 10^9\)) and describes Bob's lucky days.It is guarant... | Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. | The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. | Input: 0 2 51 3 5 | Output: 2 | Hard | 2 | 991 | 371 | 95 | 10 |
1,038 | E | 1038E | E. Maximum Matching | 2,400 | bitmasks; brute force; dfs and similar; dp; graphs | You are given \(n\) blocks, each of them is of the form [color\(_1\)|value|color\(_2\)], where the block can also be flipped to get [color\(_2\)|value|color\(_1\)]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B an... | The first line of input contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of given blocks.Each of the following \(n\) lines describes corresponding block and consists of \(\mathrm{color}_{1,i}\), \(\mathrm{value}_i\) and \(\mathrm{color}_{2,i}\) (\(1 \le \mathrm{color}_{1,i}, \mathrm{color}_{2,i} \le 4... | Print exactly one integer β the maximum total value of the subset of blocks, which makes a valid sequence. | In the first example, it is possible to form a valid sequence from all blocks.One of the valid sequences is the following:[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]The first block from the input ([2|1|4] \(\to\) [4|1|2]) and second ([1|2|4] \(\to\) [4|2|1]) are flipped.In the second example, the optimal answers ... | Input: 62 1 41 2 43 4 42 8 33 16 31 32 2 | Output: 63 | Expert | 5 | 794 | 365 | 106 | 10 |
1,089 | A | 1089A | A. Alice the Fan | 2,200 | dp | Alice is a big fan of volleyball and especially of the very strong ""Team A"".Volleyball match consists of up to five sets. During each set teams score one point for winning a ball. The first four sets are played until one of the teams scores at least 25 points and the fifth set is played until one of the teams scores ... | The first line contains a single integer \(m\) (\(1 \le m \le 50\,000\)) β the number of matches found by Alice in the book.Each of the next \(m\) lines contains two integers \(a\) and \(b\) (\(0 \le a, b \le 200\)) β the number of points scored by ""Team A"" and the number of points scored by their opponents respectiv... | Output the solution for every match in the same order as they are given in the input. If the teams could not score \(a\) and \(b\) points respectively, output ""Impossible"".Otherwise, output the match score formatted as ""\(x\):\(y\)"", where \(x\) is the number of sets won by ""Team A"" and \(y\) is the number of set... | Input: 6 75 0 90 90 20 0 0 75 78 50 80 100 | Output: 3:0 25:0 25:0 25:0 3:1 25:22 25:22 15:25 25:21 Impossible 0:3 0:25 0:25 0:25 3:0 25:11 28:26 25:13 3:2 25:17 0:25 25:22 15:25 15:11 | Hard | 1 | 1,551 | 324 | 626 | 10 | |
1,252 | H | 1252H | H. Twin Buildings | 1,800 | greedy; implementation | As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find land to build the buildings.There are \(N\) lands available for sale. The... | Input begins with a line containing an integer: \(N\) (\(1 \le N \le 100\,000\)) representing the number of available lands. The next \(N\) lines each contains two integers: \(L_i\) \(W_i\) (\(1 \le L_i, W_i \le 10^9\)) representing the size of the land. | Output in a line a number representing the largest building that ICPC Jakarta can build with exactly one decimal point (see sample input/output for clarity). | Explanation for the sample input/output #1Two buildings of \(2.5 \times 5\) can be built both on the first land.Explanation for the sample input/output #2Two buildings of \(2 \times 4\) can be built each on the first and second lands.Explanation for the sample input/output #3Two buildings of \(7 \times 6\) can be built... | Input: 2 5 5 3 4 | Output: 12.5 | Medium | 2 | 1,498 | 254 | 157 | 12 |
1,184 | C2 | 1184C2 | C2. Heidi and the Turing Test (Medium) | 2,200 | data structures | The Cybermen solved that first test much quicker than the Daleks. Luckily for us, the Daleks were angry (shocking!) and they destroyed some of the Cybermen.After the fighting stopped, Heidi gave them another task to waste their time on.There are \(n\) points on a plane. Given a radius \(r\), find the maximum number of ... | The first line contains two integers \(n, r\) (\(1 \le n \le 300\,000, 1 \le r \le 10^6\)), the number of points and the radius of the ball, respectively. Each of the next \(n\) lines contains integers \(x_i, y_i\) (\(-10^6 \leq x_i, y_i \leq 10^6\)), describing the coordinates of the \(i\)-th point.It is guaranteed, t... | Print one integer β the maximum number points that an \(L^1\)-ball with radius \(r\) can cover. | In the first example, a ball centered at \((1, 0)\) covers the points \((1, 1)\), \((1, -1)\), \((2, 0)\).In the second example, a ball centered at \((0, 0)\) covers all the points.Note that \(x_0\) and \(y_0\) need not be integer. | Input: 5 1 1 1 1 -1 -1 1 -1 -1 2 0 | Output: 3 | Hard | 1 | 686 | 348 | 95 | 11 |
1,778 | A | 1778A | A. Flip Flop Sum | 800 | greedy; implementation | You are given an array of \(n\) integers \(a_1, a_2, \ldots, a_n\). The integers are either \(1\) or \(-1\). You have to perform the following operation exactly once on the array \(a\): Choose an index \(i\) (\(1 \leq i < n\)) and flip the signs of \(a_i\) and \(a_{i+1}\). Here, flipping the sign means \(-1\) will be \... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)), the length of the array \(a\).The next line contains \(n\) integers \(a_1, ... | For each test case, print the maximum possible sum of the array \(a\) you can get in a separate line. | In the first case, we can choose index \(4\) and flip the signs of \(a_4\) and \(a_5\). After this operation, the sum will be \(-1+1+1+1+1 = 3\). We can't make the sum larger than this.In the third case, the only option is to choose the index \(1\). | Input: 45-1 1 1 -1 -151 1 -1 -1 -121 141 -1 -1 1 | Output: 3 3 -2 4 | Beginner | 2 | 453 | 425 | 101 | 17 |
1,254 | A | 1254A | A. Feeding Chicken | 1,700 | constructive algorithms; greedy; implementation | Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.His farm is presented by a rectangle grid with \(r\) rows and \(c\) columns. Some of these cells contain rice, others are empty. \(k\) chickens are living on his farm. The number ... | Each test contains multiple test cases. The first line contains the number of test cases \(T\) (\(1 \le T \le 2 \cdot 10^4\)). Description of the test cases follows.The first line of each test case contains three integers \(r\), \(c\) and \(k\) (\(1 \leq r, c \leq 100, 1 \leq k \leq 62\)), representing the size of Long... | For each test case, print \(r\) lines with \(c\) characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase charact... | These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is \(0\).In the s... | Input: 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR | Output: 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ01... | Medium | 3 | 1,160 | 707 | 451 | 12 |
1,234 | C | 1234C | C. Pipes | 1,500 | dp; implementation | You are given a system of pipes. It consists of two rows, each row consists of \(n\) pipes. The top left pipe has the coordinates \((1, 1)\) and the bottom right β \((2, n)\).There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types: Types of pipes ... | The first line of the input contains one integer \(q\) (\(1 \le q \le 10^4\)) β the number of queries. Then \(q\) queries follow.Each query consists of exactly three lines. The first line of the query contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of pipes in each row. The next two lines contain... | For the \(i\)-th query print the answer for it β ""YES"" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach \((2, n + 1)\) from \((1, 0)\), and ""NO"" otherwise. | The first query from the example is described in the problem statement. | Input: 6 7 2323216 1615124 1 3 4 2 13 24 2 12 34 3 536 345 2 46 54 | Output: YES YES YES NO YES NO | Medium | 2 | 1,478 | 736 | 203 | 12 |
1,776 | D | 1776D | D. Teamwork | 2,800 | constructive algorithms; greedy; math | As soon as SWERC starts, your experienced \(3\)-person team immediately realizes that the contest features \(a\) easy problems, \(b\) medium problems, and \(c\) hard problems. Solving a problem will take any of you \(2\), \(3\), or \(4\) time units, depending on whether the problem is easy, medium, or hard. Regardless ... | The input has a single line. It contains four integers \(a\), \(b\), \(c\), \(l\) (\(0 \leq a, b, c, \leq 10^4\), \(0 \le l \le 10^5\)) β the number of easy, medium, and hard problems, and the duration of the contest. | On the first line, print a single integer \(n\) β the maximum number of problems that your team can solve.Then, on the \(j\)-th of the following \(n\) lines, print three integers \(x_j\), \(p_j\), \(q_j\) (\(1 \leq x \leq 3\), \(0 \leq p_j < q_j \leq l\)) β the contestant that solves the \(j\)-th problem, and the start... | In the first sample, the first contestant solves an easy problem between time \(0\) and time \(2\) while the second contestant solves a medium problem between time \(0\) and time \(3\).In the second sample, the first contestant solves an easy problem between time \(0\) and time \(2\), and then also solves a medium prob... | Input: 2 1 1 3 | Output: 2 1 0 2 2 0 3 | Master | 3 | 1,221 | 217 | 708 | 17 |
1,603 | A | 1603A | A. Di-visible Confusion | 1,300 | constructive algorithms; math; number theory | YouKn0wWho has an integer sequence \(a_1, a_2, \ldots, a_n\). He will perform the following operation until the sequence becomes empty: select an index \(i\) such that \(1 \le i \le |a|\) and \(a_i\) is not divisible by \((i + 1)\), and erase this element from the sequence. Here \(|a|\) is the length of sequence \(a\) ... | The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)).It is guaranteed that t... | For each test case, print ""YES"" (without quotes) if it is possible to erase the whole sequence using the aforementioned operation, print ""NO"" (without quotes) otherwise. You can print each letter in any register (upper or lower). | In the first test case, YouKn0wWho can perform the following operations (the erased elements are underlined): \([1, \underline{2}, 3] \rightarrow [\underline{1}, 3] \rightarrow [\underline{3}] \rightarrow [\,].\)In the second test case, it is impossible to erase the sequence as \(i\) can only be \(1\), and when \(i=1\)... | Input: 5 3 1 2 3 1 2 2 7 7 10 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 8 6 69 696 69696 696969 6969696 69696969 696969696 | Output: YES NO YES YES NO | Easy | 3 | 718 | 388 | 233 | 16 |
1,097 | H | 1097H | H. Mateusz and an Infinite Sequence | 3,400 | bitmasks; brute force; dp; strings | A Thue-Morse-Radecki-Mateusz sequence (Thorse-Radewoosh sequence in short) is an infinite sequence constructed from a finite sequence \(\mathrm{gen}\) of length \(d\) and an integer \(m\), obtained in the following sequence of steps: In the beginning, we define the one-element sequence \(M_0=(0)\). In the \(k\)-th step... | The first line contains two integers \(d\) and \(m\) (\(2 \leq d \leq 20\), \(2 \leq m \leq 60\)) β the length of the sequence \(\mathrm{gen}\) and an integer used to perform the modular operations. The second line contains \(d\) integers \(\mathrm{gen}_i\) (\(0 \leq \mathrm{gen}_i < m\)). It's guaranteed that the firs... | Print a single integer β the answer to the problem. | Thorse-Radewoosh sequence in the first example is the standard Thue-Morse sequence, so the sequence \(A\) is as follows: \(11010011001011010010\). Here are the places where the sequence \(B\) majorizes \(A\): | Input: 2 2 0 1 4 0 1 1 0 2 21 | Output: 6 | Master | 4 | 2,327 | 660 | 51 | 10 |
305 | E | 305E | E. Playing with String | 2,300 | games | Two people play the following string game. Initially the players have got some string s. The players move in turns, the player who cannot make a move loses. Before the game began, the string is written on a piece of paper, one letter per cell. An example of the initial situation at s = ""abacaba"" A player's move is th... | The first line contains string s (1 β€ |s| β€ 5000). It is guaranteed that string s only contains lowercase English letters. | If the second player wins, print in the single line ""Second"" (without the quotes). Otherwise, print in the first line ""First"" (without the quotes), and in the second line print the minimal possible winning move β integer i (1 β€ i β€ |s|). | In the first sample the first player has multiple winning moves. But the minimum one is to cut the character in position 2. In the second sample the first player has no available moves. | Input: abacaba | Output: First2 | Expert | 1 | 1,348 | 122 | 241 | 3 |
1,386 | A | 1386A | A. Colors | 2,700 | *special; binary search; constructive algorithms; interactive | Linda likes to change her hair color from time to time, and would be pleased if her boyfriend Archie would notice the difference between the previous and the new color. Archie always comments on Linda's hair color if and only if he notices a difference β so Linda always knows whether Archie has spotted the difference o... | Comments to the example input line by line: \(N = 7\). Answer to the first query is meaningless (can also be \(0\)). \(C \leq 5\). \(3 < C \leq 5\). It would be wise to check difference \(4\). However, this can not be done in the next query since \(4 + 4 = 8\) and \(4 - 4 = 0\) both are outside the allowed interval \(1... | Input: 1 7 1 1 0 0 1 | Output: ? 2 ? 7 ? 4 ? 1 ? 5 = 4 | Master | 4 | 1,816 | 0 | 0 | 13 | ||
1,137 | C | 1137C | C. Museums Tour | 2,500 | dp; graphs; implementation | In the country \(N\), there are \(n\) cities connected by \(m\) one-way roads. Although this country seems unremarkable, there are two interesting facts about it. At first, a week lasts \(d\) days here. At second, there is exactly one museum in each city of the country \(N\).Travel agency ""Open museums"" is developing... | The first line contains three integers \(n\), \(m\) and \(d\) (\(1 \leq n \leq 100\,000\), \(0 \leq m \leq 100\,000\), \(1 \leq d \leq 50\)), the number of cities, the number of roads and the number of days in a week.Each of next \(m\) lines contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne... | Print a single integer β the maximum number of distinct museums, that it's possible to visit, starting a trip in the first city on the first day of the week. | Explanation of the first example The maximum number of distinct museums to visit is \(3\). It's possible to visit \(3\) museums, for example, in the way described below. Day 1. Now it's the 1st day of a week, and the tourist is in the city \(1\). The museum there is closed. At night the tourist goes to the city number ... | Input: 4 5 3 3 1 1 2 2 4 4 1 2 3 011 110 111 001 | Output: 3 | Expert | 3 | 1,117 | 857 | 157 | 11 |
2,053 | D | 2053D | D. Refined Product Optimality | 1,700 | binary search; data structures; greedy; math; schedules; sortings | As a tester, when my solution has a different output from the example during testing, I suspect the author first.β Chris, a commentAlthough Iris occasionally sets a problem where the solution is possibly wrong, she still insists on creating problems with her imagination; after all, everyone has always been on the road ... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 2\cdot 10^5\), \(1 \leq q \leq 2\cdot 10^5\))... | For each test case, output \(q + 1\) integers in a line, representing the answers that Chris will calculate, modulo \(998\,244\,353\). | In the first test case: Before the modifications, Chris can rearrange \(b\) to \([1, 2, 3]\) so that \(P = \prod\limits_{i=1}^n \min(a_i, b_i) = 1 \cdot 1 \cdot 2 = 2\). We can prove that this is the maximum possible value. For example, if Chris rearranges \(b = [2, 3, 1]\), \(P\) will be equal \(1 \cdot 1 \cdot 1 = 1 ... | Input: 43 41 1 23 2 11 32 31 12 16 81 4 2 7 3 57 6 5 6 3 32 51 61 51 51 52 32 31 613 87 7 6 6 5 5 5 2 2 3 4 5 11 4 1 9 6 6 9 1 5 1 3 8 42 22 112 42 41 71 12 121 55 310000000 20000000 30000000 40000000 5000000010000000 20000000 30000000 40000000 500000001 12 22 1 | Output: 2 3 3 6 6 840 840 1008 1344 1680 2016 2016 2016... | Medium | 6 | 1,788 | 914 | 134 | 20 |
513 | F1 | 513F1 | F1. Scaygerboss | 2,700 | flows | Cthulhu decided to catch Scaygerboss. Scaygerboss found it out and is trying to hide in a pack of his scaygers. Each scayger except Scaygerboss is either a male or a female. Scaygerboss's gender is ""other"".Scaygers are scattered on a two-dimensional map divided into cells. A scayger looks nerdy and loveable if it is ... | The first line contains 4 integers: n, m, males, females (0 β€ males, females β€ nΒ·m). n and m are dimensions of the map; males and females are numbers of male scaygers and female scaygers.Next n lines describe the map. Each of these lines contains m characters. Character '.' stands for a free cell; character '#' stands ... | Output the minimum possible time it takes to make all scaygers look nerdy and loveable or -1 if it is impossible. | Consider the first sample test. The scaygers are hiding on a 4 by 4 map. Scaygerboss initially resides in the cell (2, 1) and can move between cells in 1 unit of time. There are also 2 male and 3 female scaygers on the map. One of the females initially is in the cell (1, 1), and all the other scaygers are in the cell (... | Input: 4 4 2 3.....###########2 1 12 1 22 1 22 1 22 1 21 1 2 | Output: 2 | Master | 1 | 1,003 | 1,224 | 113 | 5 |
1,688 | A | 1688A | A. Cirno's Perfect Bitmasks Classroom | 800 | bitmasks; brute force | Even if it's a really easy question, she won't be able to answer itβ Perfect Memento in Strict SenseCirno's perfect bitmasks classroom has just started!Cirno gave her students a positive integer \(x\). As an assignment, her students need to find the minimum positive integer \(y\), which satisfies the following two cond... | The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of input test cases.For each test case, the only line of input contains one integer \(x\) (\(1 \leq x \leq 2^{30}\)). | For each test case, print a single integer β the minimum number of \(y\). | Test case 1: \(1\; \texttt{and}\; 3=1>0\), \(1\; \texttt{xor}\; 3=2>0\).Test case 2: \(2\; \texttt{and}\; 3=2>0\), \(2\; \texttt{xor}\; 3=1>0\). | Input: 71259161145141000000 | Output: 3 3 1 1 17 2 64 | Beginner | 2 | 590 | 211 | 73 | 16 |
1,034 | D | 1034D | D. Intervals of Intervals | 3,500 | binary search; data structures; two pointers | Little D is a friend of Little C who loves intervals very much instead of number ""\(3\)"".Now he has \(n\) intervals on the number axis, the \(i\)-th of which is \([a_i,b_i]\).Only the \(n\) intervals can not satisfy him. He defines the value of an interval of intervals \([l,r]\) (\(1 \leq l \leq r \leq n\), \(l\) and... | First line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 3 \cdot 10^5\), \(1 \leq k \leq \min\{\frac{n(n+1)}{2},10^9\}\)) β the number of intervals Little D has and the number of intervals of intervals he will select.Each of the next \(n\) lines contains two integers \(a_i\) and \(b_i\), the \(i\)-th line of t... | Print one integer β the maximal sum of values Little D can get. | For the first example, Little D will select \([1,2]\), the union of the first interval and the second interval is \([1,4]\), whose length is \(3\).For the second example, Little D will select \([1,2]\), \([2,3]\) and \([1,3]\), the answer is \(5+6+4=15\). | Input: 2 11 32 4 | Output: 3 | Master | 3 | 580 | 401 | 63 | 10 |
1,858 | E2 | 1858E2 | E2. Rollbacks (Hard Version) | 2,600 | data structures; interactive; trees | This is a hard version of this problem. The only difference between the versions is that you have to solve the hard version in online mode. You can make hacks only if both versions of the problem are solved.You have an array \(a\), which is initially empty. You need to process queries of the following types: + \(x\) β ... | The first line contains an integer \(q\) (\(1 \leq q \leq 10^6\)) β the number of queries.The next \(q\) lines contain the queries as described above.It is guaranteed that in the queries of the first type, \(1 \le x \le 10^6\); in the queries of the second type, \(k \ge 1\) and \(k\) does not exceed the current length ... | For each query of the fourth type output one integer β the number of distinct elements in array \(a\) at the moment of query. | In the first example array \(a\) changes as follows: After the first query, \(a=[1]\). After the second query, \(a=[1,2]\). After the third query, \(a=[1,2,2]\). At the moment of the fourth query, there are \(2\) distinct intergers in the array \(a\): \(1\) and \(2\). After the fifth query, \(a=[1,2]\) (rolled back the... | Input: 10 + 1 + 2 + 2 ? ! + 3 - 2 ? + 1 ? | Output: 2 1 1 | Expert | 3 | 684 | 940 | 125 | 18 |
2,055 | A | 2055A | A. Two Frogs | 800 | constructive algorithms; games; greedy; math | Roaming through the alligator-infested Everglades, Florida Man encounters a most peculiar showdown.There are \(n\) lilypads arranged in a row, numbered from \(1\) to \(n\) from left to right. Alice and Bob are frogs initially positioned on distinct lilypads, \(a\) and \(b\), respectively. They take turns jumping, start... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first and only line of each test case contains three integers \(n\), \(a\), and \(b\) (\(2 \leq n \leq 100\), \(1 \leq a, b \leq n\), \(a\neq b\)) β the nu... | For each test case, print a single line containing either ""YES"" or ""NO"", representing whether or not Alice has a winning strategy. 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 has no legal moves. Therefore, Alice loses on the first turn.In the second test case, Alice can only move to lilypad \(2\). Then, Bob has no legal moves. Therefore, Alice has a winning strategy in this case.In the third test case, Alice can only move to lilypad \(1\). Then, Bob can move to... | Input: 52 1 23 3 14 2 35 2 47 6 2 | Output: NO YES NO YES YES | Beginner | 4 | 1,173 | 471 | 296 | 20 |
1,284 | F | 1284F | F. New Year and Social Network | 3,200 | data structures; graph matchings; graphs; math; trees | Donghyun's new social network service (SNS) contains \(n\) users numbered \(1, 2, \ldots, n\). Internally, their network is a tree graph, so there are \(n-1\) direct connections between each user. Each user can reach every other users by using some sequence of direct connections. From now on, we will denote this primar... | The first line contains an integer \(n\) (\(2 \le n \le 250\,000\)), the number of users. In the next \(n-1\) lines, two integers \(a_i\), \(b_i\) (\(1 \le a_i, b_i \le n\)) are given. Those two numbers denote the indices of the vertices connected by the corresponding edge in \(T_1\).In the next \(n-1\) lines, two inte... | In the first line, print the number \(m\) (\(0 \leq m < n\)), the maximum number of edges that can be protected.In the next \(m\) lines, print four integers \(a_i, b_i, c_i, d_i\). Those four numbers denote that the edge \((a_i, b_i)\) in \(T_1\) is will be replaced with an edge \((c_i, d_i)\) in \(T_2\).All printed ed... | Input: 4 1 2 2 3 4 3 1 3 2 4 1 4 | Output: 3 3 2 4 2 2 1 1 3 4 3 1 4 | Master | 5 | 1,392 | 544 | 702 | 12 | |
1,634 | A | 1634A | A. Reverse and Concatenate | 800 | greedy; strings | Real stupidity beats artificial intelligence every time.β Terry Pratchett, Hogfather, DiscworldYou are given a string \(s\) of length \(n\) and a number \(k\). Let's denote by \(rev(s)\) the reversed string \(s\) (i.e. \(rev(s) = s_n s_{n-1} ... s_1\)). You can apply one of the two kinds of operations to the string: re... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β number of test cases. Next \(2 \cdot t\) lines contain \(t\) test cases:The first line of a test case contains two integers \(n\) and \(k\) (\(1 \le n \le 100\), \(0 \le k \le 1000\)) β the length of the string and the number of operations respectively.T... | For each test case, print the answer (that is, the number of different strings that you can get after exactly \(k\) operations) on a separate line.It can be shown that the answer does not exceed \(10^9\) under the given constraints. | In the first test case of the example:After the first operation the string \(s\) can become either aabbaa or baaaab. After the second operation there are 2 possibilities for \(s\): aabbaaaabbaa and baaaabbaaaab. | Input: 43 2aab3 3aab7 1abacaba2 0ab | Output: 2 2 1 1 | Beginner | 2 | 783 | 432 | 232 | 16 |
1,874 | E | 1874E | E. Jellyfish and Hack | 3,000 | dp; math | It is well known that quick sort works by randomly selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. But Jellyfish thinks that choosing a random element is just a waste of time, so she always chooses the... | The only line of the input contains two integers \(n\) and \(lim\) (\(1 \leq n \leq 200\), \(1 \leq lim \leq 10^9\)). | Output the number of different permutations that satisfy the condition modulo \(10^9+7\). | In the first example, \(P = [1, 4, 2, 3]\) satisfies the condition, because: \(\mathrm{fun([1, 4, 2, 3]) = 4 + fun([4, 2, 3]) = 7 + fun([2, 3]) = 9 + fun([3]) = 10}\)Do remember to output the answer modulo \(10^9+7\). | Input: 4 10 | Output: 8 | Master | 2 | 1,098 | 117 | 89 | 18 |
314 | B | 314B | B. Sereja and Periods | 2,000 | binary search; dfs and similar; strings | Let's introduce the designation , where x is a string, n is a positive integer and operation "" + "" is the string concatenation operation. For example, [abc, 2] = abcabc.We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab an... | The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100. | In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0. | Input: 10 3ababbab | Output: 3 | Hard | 3 | 555 | 235 | 108 | 3 | |
1,621 | A | 1621A | A. Stable Arrangement of Rooks | 800 | constructive algorithms | You have an \(n \times n\) chessboard and \(k\) rooks. Rows of this chessboard are numbered by integers from \(1\) to \(n\) from top to bottom and columns of this chessboard are numbered by integers from \(1\) to \(n\) from left to right. The cell \((x, y)\) is the cell on the intersection of row \(x\) and collumn \(y\... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two integers \(n\), \(k\) (\(1 \leq k \leq n \leq 40\)) β the size of the chessboard and the number of rooks. | If there is a stable arrangement of \(k\) rooks on the \(n \times n\) chessboard, output \(n\) lines of symbols . and R. The \(j\)-th symbol of the \(i\)-th line should be equals R if and only if there is a rook on the cell \((i, j)\) in your arrangement.If there are multiple solutions, you may output any of them.If th... | In the first test case, you should find stable arrangement of \(2\) rooks on the \(3 \times 3\) chessboard. Placing them in cells \((3, 1)\) and \((1, 3)\) gives stable arrangement.In the second test case it can be shown that it is impossbile to place \(3\) rooks on the \(3 \times 3\) chessboard to get stable arrangeme... | Input: 53 23 31 15 240 33 | Output: ..R ... R.. -1 R ..... R.... ..... ....R ..... -1 | Beginner | 1 | 1,146 | 249 | 364 | 16 |
165 | B | 165B | B. Burning Midnight Oil | 1,500 | binary search; implementation | One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as lines, drinks another cup of tea, then he writes lines and... | The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10. | Print the only integer β the minimum value of v that lets Vasya write the program in one night. | In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.In the second sample the answer is v = 54. Vasya writes the code in the following portions:... | Input: 7 2 | Output: 4 | Medium | 2 | 701 | 167 | 95 | 1 |
51 | F | 51F | F. Caterpillar | 2,800 | dfs and similar; dp; graphs; trees | An undirected graph is called a caterpillar if it is a connected graph without cycles and it has such a path p that any vertex is located at a distance of at most 1 from the path p. The caterpillar can contain loops (edges from a vertex to itself) but cannot contain multiple (parallel) edges.The picture contains an exa... | The first line contains a pair of integers n, m (1 β€ n β€ 2000;0 β€ m β€ 105), where n represents the number of vertices in the graph and m is the number of edges in it. Then the following m lines contain edge descriptions, one edge description per line. Every line contains a pair of integers ai, bi (1 β€ ai, bi β€ n;ai β b... | Print the minimal required number of operations. | Input: 4 41 22 33 44 2 | Output: 2 | Master | 4 | 1,419 | 564 | 48 | 0 | |
1,830 | F | 1830F | F. The Third Grace | 3,200 | data structures; dp | You are given \(n\) intervals and \(m\) points on the number line. The \(i\)-th intervals covers coordinates \([l_i,r_i]\) and the \(i\)-th point is on coordinate \(i\) and has coefficient \(p_i\).Initially, all points are not activated. You should choose a subset of the \(m\) points to activate. For each of \(n\) inte... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^6, 1 \le m \le 10^6\)) β the number of interva... | Output the maximum possible sum of costs of all intervals. | In the first sample, we can activate points \(1\) and \(8\). The sum of costs of all intervals will be \(78+30=108\).In the second sample, we will activate no points. The sum of costs of all intervals will be \(0\). | Input: 22 81 53 878 0 50 0 0 0 0 301 61 50 0 0 0 0 100 | Output: 108 0 | Master | 2 | 595 | 757 | 58 | 18 |
1,553 | E | 1553E | E. Permutation Shift | 2,100 | brute force; combinatorics; constructive algorithms; dfs and similar; dsu; graphs; math | An identity permutation of length \(n\) is an array \([1, 2, 3, \dots, n]\).We performed the following operations to an identity permutation of length \(n\): firstly, we cyclically shifted it to the right by \(k\) positions, where \(k\) is unknown to you (the only thing you know is that \(0 \le k \le n - 1\)). When an ... | The first line contains one integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.Each test case consists of two lines. The first line contains two integers \(n\) and \(m\) (\(3 \le n \le 3 \cdot 10^5\); \(0 \le m \le \frac{n}{3}\)).The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i... | For each test case, print the answer in the following way: firstly, print one integer \(r\) (\(0 \le r \le n\)) β the number of possible values of \(k\) for the cyclic shift operation; secondly, print \(r\) integers \(k_1, k_2, \dots, k_r\) (\(0 \le k_i \le n - 1\)) β all possible values of \(k\) in increasing order. | Consider the example: in the first test case, the only possible value for the cyclic shift is \(3\). If we shift \([1, 2, 3, 4]\) by \(3\) positions, we get \([2, 3, 4, 1]\). Then we can swap the \(3\)-rd and the \(4\)-th elements to get the array \([2, 3, 1, 4]\); in the second test case, the only possible value for t... | Input: 4 4 1 2 3 1 4 3 1 1 2 3 3 1 3 2 1 6 0 1 2 3 4 6 5 | Output: 1 3 1 0 3 0 1 2 0 | Hard | 7 | 1,050 | 494 | 318 | 15 |
1,889 | D | 1889D | D. Game of Stacks | 3,000 | brute force; dfs and similar; graphs; implementation; trees | You have \(n\) stacks \(r_1,r_2,\ldots,r_n\). Each stack contains some positive integers ranging from \(1\) to \(n\).Define the following functions:function init(pos): stacks := an array that contains n stacks r[1], r[2], ..., r[n] return get(stacks, pos)function get(stacks, pos): if stacks[pos] is empty: return pos el... | The first line of the input contains one integer \(n\) (\(1\le n\le 10^5\)) β the length of the array \(r\).Each of the following \(n\) lines contains several integers. The first integer \(k_i\) (\(0\le k_i\le 10^5\)) represents the number of elements in the \(i\)-th stack, and the following \(k_i\) positive integers \... | You need to output \(n\) values, the \(i\)-th of which is the value returned by \(\texttt{init(i)}\). | In the first example: When you call \(\texttt{init(1)}\), set \(\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}\), and then call \(\texttt{get(stacks, 1)}\). \(\texttt{stacks[1]}\) is not empty, set \(\texttt{new_pos := 2}\), and pop the top element of \(\texttt{stacks[1]}\), which makes \(\texttt{stacks}\) become \([[1,2... | Input: 3 3 1 2 2 3 3 1 2 3 1 2 1 | Output: 1 2 2 | Master | 5 | 706 | 495 | 101 | 18 |
1,110 | C | 1110C | C. Meaningless Operations | 1,500 | constructive algorithms; math; number theory | Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.Suppose you are given a positive integer \(a\). You want to choose some integer \(b\) from \(1\) to \(a - 1\) inclusive in such a way that the greatest common divisor (GCD) of integers \(a \oplus b\) and \... | The first line contains an integer \(q\) (\(1 \le q \le 10^3\)) β the number of integers you need to compute the answer for.After that \(q\) integers are given, one per line: \(a_1, a_2, \ldots, a_q\) (\(2 \le a_i \le 2^{25} - 1\)) β the integers you need to compute the answer for. | For each integer, print the answer in the same order as the integers are given in input. | For the first integer the optimal choice is \(b = 1\), then \(a \oplus b = 3\), \(a \> \& \> b = 0\), and the greatest common divisor of \(3\) and \(0\) is \(3\).For the second integer one optimal choice is \(b = 2\), then \(a \oplus b = 1\), \(a \> \& \> b = 2\), and the greatest common divisor of \(1\) and \(2\) is \... | Input: 3 2 3 5 | Output: 3 1 7 | Medium | 3 | 924 | 282 | 88 | 11 |
909 | D | 909D | D. Colorful Points | 2,100 | data structures; greedy; implementation | You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right.You perform a sequence of operations on this set of poi... | Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.The number of the points is between 1 and... | Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. | In the first test case, the first operation will delete two middle points and leave points ""ab"", which will be deleted with the second operation. There will be no points left to apply the third operation to.In the second test case, the first operation will delete the four points in the middle, leaving points ""aa"". ... | Input: aabb | Output: 2 | Hard | 3 | 770 | 325 | 156 | 9 |
1,920 | F1 | 1920F1 | F1. Smooth Sailing (Easy Version) | 2,500 | binary search; brute force; data structures; dfs and similar; dsu; graphs; shortest paths | The only difference between the two versions of this problem is the constraint on \(q\). You can make hacks only if both versions of the problem are solved.Thomas is sailing around an island surrounded by the ocean. The ocean and island can be represented by a grid with \(n\) rows and \(m\) columns. The rows are number... | The first line contains three integers \(n\), \(m\), and \(q\) (\(3 \leq n, m \leq 10^5\), \(9 \leq n \cdot m \leq 3 \cdot 10^5\), \(1 \leq q \leq 5\)) β the number of rows and columns of the grid and the number of queries. Each of the following \(n\) lines contains \(m\) characters describing the cells of the grid. Th... | For each query, output a single integer β the maximum safety of a round trip starting from the specified position. | For the first example, the image below shows an optimal round trip starting from \((1, 1)\). The round trip has a safety of \(3\) as the minimum Manhattan distance from a cell on the round trip to an underwater volcano is \(3\). Example of an optimal round trip For the fourth example, remember that it is allowed for Th... | Input: 9 9 3 ......... ......... ....###.. ...v#.... ..###.... ...##...v ...##.... ......... v........ 1 1 9 1 5 7 | Output: 3 0 3 | Expert | 7 | 3,081 | 1,155 | 114 | 19 |
1,930 | F | 1930F | F. Maximize the Difference | 2,700 | bitmasks; brute force; dfs and similar | For an array \(b\) of \(m\) non-negative integers, define \(f(b)\) as the maximum value of \(\max\limits_{i = 1}^{m} (b_i | x) - \min\limits_{i = 1}^{m} (b_i | x)\) over all possible non-negative integers \(x\), where \(|\) is bitwise OR operation.You are given integers \(n\) and \(q\). You start with an empty array \(... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 2^{22}\), \(1 \leq q \leq 10^6\)) β the nu... | For each test case, print \(q\) integers. The \(i\)-th integer is the output of the \(i\)-th query. | In the first test case, the final \(a=[1,2]\). For \(i=1\), the answer is always \(0\), irrespective of \(x\). For \(i=2\), we can select \(x=5\).In the second test case, the final \(a=[3,1,0,5]\). | Input: 25 21 27 43 1 5 2 | Output: 0 2 0 2 3 5 | Master | 3 | 509 | 852 | 99 | 19 |
2,101 | F | 2101F | F. Shoo Shatters the Sunshine | 3,300 | combinatorics; dp; trees | You are given a tree with \(n\) vertices, where each vertex can be colored red, blue, or white. The coolness of a coloring is defined as the maximum distance\(^{\text{β}}\) between a red and a blue vertex.Formally, if we denote the color of the \(i\)-th vertex as \(c_i\), the coolness of a coloring is \(\max d(u, v)\) ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2\le n\le 3000\)) β the number of vertices in the tree.Each of the next \(n - 1\) lines cont... | For each test case, output the sum of coolness over all \(3^n\) possible colorings of the tree, modulo \(998\,244\,353\). | In the first test case, there are \(12\) colorings that have at least one blue and one red node. The following pictures show their coloring and their coolness: All these colorings have coolness \(2\) All these colorings have coolness \(1\) Therefore, the sum of coolness over all possible colorings is \(6\cdot 2 + 6\cdo... | Input: 331 22 361 21 31 43 55 6171 21 31 41 52 62 72 83 93 107 117 1211 1313 1414 1510 1616 17 | Output: 18 1920 78555509 | Master | 3 | 761 | 550 | 121 | 21 |
780 | G | 780G | G. Andryusha and Nervous Barriers | 2,700 | data structures; dp | Andryusha has found a perplexing arcade machine. The machine is a vertically adjusted board divided into square cells. The board has w columns numbered from 1 to w from left to right, and h rows numbered from 1 to h from the bottom to the top.Further, there are barriers in some of board rows. There are n barriers in to... | The first line contains three integers h, w, and n (1 β€ h β€ 109, 2 β€ w β€ 105, 0 β€ n β€ 105) β the number of rows, columns, and barriers in the machine respectively.Next n lines describe barriers. i-th of these lines containts four integers ui, li, ri, and si (1 β€ ui β€ h, 1 β€ li β€ ri β€ w, 1 β€ si β€ 109) β row index, leftm... | Print one integer β the answer to the problem modulo 109 + 7. | In the first sample case, there is a single barrier: if one throws a marble in the second or the third column, two marbles come out, otherwise there is only one. The total answer is 7.In the second sample case, the numbers of resulting marbles are 2, 2, 4, 4, 4 in order of indexing columns with the initial marble.In th... | Input: 10 5 13 2 3 10 | Output: 7 | Master | 2 | 1,938 | 544 | 61 | 7 |
176 | B | 176B | B. Word Cut | 1,700 | dp | Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split operation is transforming word w = xy into word u = yx. For example, a split operation c... | The first line contains a non-empty word start, the second line contains a non-empty word end. The words consist of lowercase Latin letters. The number of letters in word start equals the number of letters in word end and is at least 2 and doesn't exceed 1000 letters.The third line contains integer k (0 β€ k β€ 105) β th... | Print a single number β the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109 + 7). | The sought way in the first sample is:ab β a|b β ba β b|a β abIn the second sample the two sought ways are: ababab β abab|ab β ababab ababab β ab|abab β ababab | Input: abab2 | Output: 1 | Medium | 1 | 896 | 352 | 124 | 1 |
1,288 | E | 1288E | E. Messenger Simulator | 2,000 | data structures | Polycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has \(n\) friends, numbered from \(1\) to \(n\).Recall that a permutation of size \(n\) is an array of size \(n\) such that each integer from \(1\) to \(n\) occurs exactly once in this array.So his recent chat lis... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of Polycarp's friends and the number of received messages, respectively.The second line contains \(m\) integers \(a_1, a_2, \dots, a_m\) (\(1 \le a_i \le n\)) β the descriptions of the received messages. | Print \(n\) pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message. | In the first example, Polycarp's recent chat list looks like this: \([1, 2, 3, 4, 5]\) \([3, 1, 2, 4, 5]\) \([5, 3, 1, 2, 4]\) \([1, 5, 3, 2, 4]\) \([4, 1, 5, 3, 2]\) So, for example, the positions of the friend \(2\) are \(2, 3, 4, 4, 5\), respectively. Out of these \(2\) is the minimum one and \(5\) is the maximum on... | Input: 5 4 3 5 1 4 | Output: 1 3 2 5 1 4 1 5 1 5 | Hard | 1 | 1,428 | 303 | 154 | 12 |
1,530 | A | 1530A | A. Binary Decimal | 800 | greedy; math | Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either \(0\) or \(1\). For example, \(1\,010\,111\) is a binary decimal, while \(10\,201\) and \(787\,788\) are not.Given a number \(n\), you are asked to represent \(n\) as a sum of some (not necessarily distinct... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)), denoting the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^9\)), denoting the number to be represented. | For each test case, output the smallest number of binary decimals required to represent \(n\) as a sum. | In the first test case, \(121\) can be represented as \(121 = 110 + 11\) or \(121 = 111 + 10\).In the second test case, \(5\) can be represented as \(5 = 1 + 1 + 1 + 1 + 1\).In the third test case, \(1\,000\,000\,000\) is a binary decimal itself, thus the answer is \(1\). | Input: 3 121 5 1000000000 | Output: 2 5 1 | Beginner | 2 | 404 | 231 | 103 | 15 |
1,510 | C | 1510C | C. Cactus Not Enough | 2,900 | dfs and similar; graph matchings; graphs | There was no problem about a cactus at the NERC 2020 online round. That's a bad mistake, so judges decided to fix it. You shall not pass to the World Finals 2021 without solving a problem about a cactus!A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively, a cactus ... | The input consists of one or more independent test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\), \(0 \le m \le 10^5\)), where \(n\) is the number of vertices in the graph. Vertices are numbered from \(1\) to \(n\). Edges of the graph are represented by a set of edge... | For each test case, first output the line with the minimal possible number of additional edges \(A\). Then output \(A\) lines, each describing one edge as \(u_i\) \(v_i\), where \(u_i\) and \(v_i\) are the numbers of vertices to connect. After adding these edges, the resulting graph must be a strong cactus. | Input: 6 1 7 1 2 5 6 2 3 4 3 1 4 1 2 3 1 5 2 3 1 3 5 3 1 2 4 7 2 6 1 2 3 4 5 3 3 6 5 7 0 0 | Output: 1 1 4 0 1 5 4 2 1 3 6 7 | Master | 3 | 1,029 | 1,204 | 308 | 15 | |
2,000 | B | 2000B | B. Seating in a Bus | 800 | two pointers | In Berland, a bus consists of a row of \(n\) seats numbered from \(1\) to \(n\). Passengers are advised to always board the bus following these rules: If there are no occupied seats in the bus, a passenger can sit in any free seat; Otherwise, a passenger should sit in any free seat that has at least one occupied neighb... | The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The following describes the input test cases.The first line of each test case contains exactly one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of seats in the bus and the number of passengers who boar... | For each test case, output on a separate line: ""YES"", if all passengers followed the recommendations; ""NO"" otherwise. You may output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer). | The first test case is explained in the problem statement. | Input: 455 4 2 1 332 3 142 3 1 451 2 3 5 4 | Output: NO YES YES NO | Beginner | 1 | 1,075 | 650 | 266 | 20 |
1,693 | A | 1693A | A. Directional Increase | 1,300 | greedy | We have an array of length \(n\). Initially, each element is equal to \(0\) and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by... | The first line contains a single integer \(t\) \((1\le t\le 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 2 \cdot 10^5)\) β the size of array \(a\).The second line of each test case contains \(n\) integers \(a_... | For each test case, print ""Yes"" (without quotes) if it's possible to obtain \(a\) after some operations, and ""No"" (without quotes) otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response). | In the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.\(\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \lan... | Input: 7 2 1 0 4 2 -1 -1 0 4 1 -4 3 0 4 1 -1 1 -1 5 1 2 3 4 -10 7 2 -1 1 -2 0 0 0 1 0 | Output: No Yes No No Yes Yes Yes | Easy | 1 | 705 | 486 | 285 | 16 |
2,113 | E | 2113E | E. From Kazan with Love | 2,800 | dfs and similar; graphs; implementation; trees | Marat is a native of Kazan. Kazan can be represented as an undirected tree consisting of \(n\) vertices. In his youth, Marat often got into street fights, and now he has \(m\) enemies, numbered from \(1\) to \(m\), living in Kazan along with him.Every day, all the people living in the city go to work. Marat knows that ... | 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 four integers \(n\), \(m\), \(x\), and \(y\) (\(2 \le n \le 10^5\), \(1 \le m \le 200\), \(1 \le x, y \le n\), \(x \... | For each test case, output a single integer β the minimum moment in time when Marat can reach work, or \(-1\) if it is impossible. | In the first test case, it is possible to reach vertex number \(4\) from vertex number \(1\) via the shortest path. Note that Marat will meet a single enemy on an edge, not at a vertex.In the second test case, the optimal strategy is to wait for one moment in time at the starting vertex and then go along the shortest p... | Input: 54 1 1 41 22 33 44 15 1 1 51 22 33 44 55 19 2 1 91 22 33 43 55 66 76 88 99 17 19 2 7 21 42 53 64 55 64 75 86 92 83 73 2 1 31 22 32 13 1 | Output: 4 6 10 5 -1 | Master | 4 | 1,571 | 953 | 130 | 21 |
65 | D | 65D | D. Harry Potter and the Sorting Hat | 2,200 | brute force; dfs and similar; hashing | As you know, Hogwarts has four houses: Gryffindor, Hufflepuff, Ravenclaw and Slytherin. The sorting of the first-years into houses is done by the Sorting Hat. The pupils are called one by one in the alphabetical order, each of them should put a hat on his head and, after some thought, the hat solemnly announces the nam... | The first input line contains an integer n (1 β€ n β€ 10000). It is the number of students who are in the list before Hermione. The next line contains n symbols. If all the relatives of a student used to study in the same house, then the i-th character in the string coincides with the first letter of the name of this hou... | Print all the possible houses where Hermione can be sent. The names of the houses should be printed in the alphabetical order, one per line. | Consider the second example. There are only two students before Hermione. The first student is sent to Hufflepuff. The second disciple is given the choice between the houses where the least number of students has been sent, i.e. Gryffindor, Slytherin and Ravenclaw. If he chooses Gryffindor, Hermione is forced to choose... | Input: 11G????SS???H | Output: GryffindorRavenclaw | Hard | 3 | 1,635 | 369 | 140 | 0 |
830 | C | 830C | C. Bamboo Partition | 2,300 | brute force; data structures; implementation; math; number theory; sortings; two pointers | Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is ai meters high. Vladimir has just planted n bamboos in a row, each of w... | The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 1011) β the number of bamboos and the maximum total length of cut parts, in meters.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the required heights of bamboos, in meters. | Print a single integer β the maximum value of d such that Vladimir can reach his goal. | In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters. | Input: 3 41 3 5 | Output: 3 | Expert | 7 | 941 | 262 | 86 | 8 |
316 | E3 | 316E3 | E3. Summer Homework | 2,300 | data structures; math | By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:You are given a sequence of integers a1, a2, ..., an. Your task is to perform on it m consecutive operations of the following type: For given numbers xi and vi assign value vi to element axi. For giv... | The first line contains two integers n and m (1 β€ n, m β€ 2Β·105) β the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 105). Then follow m lines, each describes an operation. Each line starts with an integer ti (1 β€ ti β€ 3) β... | For each query print the calculated sum modulo 1000000000 (109). | Input: 5 51 3 1 2 42 1 42 1 52 2 41 3 102 1 5 | Output: 1232850 | Expert | 2 | 632 | 972 | 64 | 3 | |
506 | E | 506E | E. Mr. Kitayuta's Gift | 3,000 | combinatorics; dp; matrices; strings | Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly n lowercase English letters into s to make it a palindrome. (A palindrome is a string that reads the same forward and backward. For example, ""noon"", ""testset"" and ""a"" are all palindromes, while ""... | The first line contains a string s (1 β€ |s| β€ 200). Each character in s is a lowercase English letter.The second line contains an integer n (1 β€ n β€ 109). | Print the number of the palindromes that can be obtained, modulo 10007. | For the first sample, you can obtain the palindrome ""reviver"" by inserting 'r' to the end of ""revive"".For the second sample, the following 28 palindromes can be obtained: ""adada"", ""adbda"", ..., ""adzda"", ""dadad"" and ""ddadd"". | Input: revive1 | Output: 1 | Master | 4 | 694 | 154 | 71 | 5 |
2,115 | A | 2115A | A. Gellyfish and Flaming Peony | 1,500 | constructive algorithms; dp; math; number theory | Gellyfish hates math problems, but she has to finish her math homework:Gellyfish is given an array of \(n\) positive integers \(a_1, a_2, \ldots, a_n\).She needs to do the following two-step operation until all elements of \(a\) are equal: Select two indexes \(i\), \(j\) satisfying \(1 \leq i, j \leq n\) and \(i \neq j... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β the length of the array.The second line of each test case contains... | For each test case, output a single integer β the minimum number of operations to achieve her goal. | In the first test case, the following is a way that minimizes the number of operations: Choose \(i = 3\) and \(j=2\) and replace \(a_3\) with \(\gcd(a_3,a_2) = \gcd(30, 20) = 10\), then \(a\) becomes \([12, 20, 10]\). Choose \(i=1\) and \(j=3\) and replace \(a_1\) with \(\gcd(a_1,a_3) = \gcd(12, 10) = 2\), then \(a\) b... | Input: 3312 20 3061 9 1 9 8 136 14 15 | Output: 4 3 3 | Medium | 4 | 506 | 498 | 99 | 21 |
1,344 | F | 1344F | F. Piet's Palette | 3,200 | matrices | Piet Mondrian is an artist most famous for his minimalist works, consisting only of the four colors red, yellow, blue, and white. Most people attribute this to his style, but the truth is that his paint behaves in a very strange way where mixing two primary colors only produces another primary color! A lesser known pie... | The first line contains two integers \(n\) and \(k\) (\(1\le n,k\le 1000\)) β the number of cells in the palette and the number of operations, respectively.The next \(k\) lines describe the operations. The \(i\)-th line begins with the name of the \(i\)-th operation and an integer \(m\) (\(1\le m\le n\)) β the number o... | Output ""YES"" if a solution exists, or ""NO"" otherwise. You can print each character in any case (upper or lower).If the answer is ""YES"", on the next line output a string of length \(n\), consisting of characters ""R"", ""Y"", ""B"", and ""."", representing the paint colors in the \(n\) cells of the initial palette... | For the first test case, the answer ""YB."" is consistent with both mixings. The first mixing ""BY"" results in red, while the second mixing ""Y"" results in yellow (the empty cell is ignored). Other valid solutions include ""BYR"" and "".RY"".For the second test case, we can show that no solution exists.For the third ... | Input: 3 2 mix 2 2 1 R mix 2 1 3 Y | Output: YES YB. | Master | 1 | 2,396 | 733 | 469 | 13 |
1,578 | G | 1578G | G. Game of Chance | 3,500 | math; probabilities | The King wants to marry off his daughter, and he wants her husband to have the greatest innate luckiness possible. To find such a person he decided to hold a heads-or-tails tournament. If person \(A\) with luckiness \(x\) and person \(B\) with luckiness \(y\) play heads-or-tails against each other, person \(A\) wins wi... | The first line of the input contains the number of participants, \(n\) (\(2 \le n \le 3 \cdot 10^5\)). The second line of the input contains \(n\) integer numbers, \(a_1, \ldots, a_{n}\) (\(1 \le a_i \le 10^9\)). The luckiness of the \(i\)-th participant equals to \(a_i\). | Print \(n\) numbers \(p_i\). The \(i\)-th number should be the probability of the \(i\)-th participant winning the tournament. The absolute error of your answer must not exceed \(10^{-9}\). | Here is an example of a tournament bracket, showing the winning probability in each pair. | Input: 5 1 4 1 1 4 | Output: 0.026 0.3584 0.0676 0.0616 0.4864 | Master | 2 | 1,632 | 273 | 189 | 15 |
409 | B | 409B | B. Mysterious Language | 1,700 | *special | You are given a mysterious language (codenamed ""Secret"") available in ""Custom Invocation"" tab. Figure out what this language is and write a program which prints its name. Note that the program must be written in this language. | This program has only one test (your program doesn't have to read anything). | Output the name of the mysterious language. Note that the name is case-sensitive and might contain digits and special characters. | Some scientists disagree on what should be considered as a language and what should be considered as a dialect. | Medium | 1 | 230 | 76 | 129 | 4 | |
1,833 | E | 1833E | E. Round Dance | 1,600 | dfs and similar; dsu; graphs; shortest paths | \(n\) people came to the festival and decided to dance a few round dances. There are at least \(2\) people in the round dance and each person has exactly two neighbors. If there are \(2\) people in the round dance then they have the same neighbor on each side.You decided to find out exactly how many dances there were. ... | The first line contains a positive number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The following is a description of the test cases.The first line of the description of each test case contains a positive number \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of people at the holiday.The second line of... | For each test case, output two integers β the minimum and maximum number of round dances that could be. | Input: 1062 1 4 3 6 562 3 1 5 6 492 3 2 5 6 5 8 9 822 144 3 2 152 3 4 5 165 3 4 1 1 253 5 4 1 266 3 2 5 4 365 1 4 3 4 2 | Output: 1 3 2 2 1 3 1 1 1 2 1 1 1 1 2 2 1 2 1 1 | Medium | 4 | 761 | 694 | 103 | 18 | |
1,832 | B | 1832B | B. Maximum Sum | 1,100 | brute force; sortings; two pointers | You are given an array \(a_1, a_2, \dots, a_n\), where all elements are different.You have to perform exactly \(k\) operations with it. During each operation, you do exactly one of the following two actions (you choose which to do yourself): find two minimum elements in the array, and delete them; find the maximum elem... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: the first line contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le k \le 99999\); \(2k < n\)) β the number of elements and operations, respectively. the second line ... | For each test case, print one integer β the maximum possible sum of elements in the resulting array. | In the first testcase, applying the first operation produces the following outcome: two minimums are \(1\) and \(2\); removing them leaves the array as \([5, 10, 6]\), with sum \(21\); a maximum is \(10\); removing it leaves the array as \([2, 5, 1, 6]\), with sum \(14\). \(21\) is the best answer.In the second testcas... | Input: 65 12 5 1 10 65 22 5 1 10 63 11 2 36 115 22 12 10 13 116 215 22 12 10 13 115 1999999996 999999999 999999997 999999998 999999995 | Output: 21 11 3 62 46 3999999986 | Easy | 3 | 435 | 536 | 100 | 18 |
1,729 | A | 1729A | A. Two Elevators | 800 | math | Vlad went into his appartment house entrance, now he is on the \(1\)-th floor. He was going to call the elevator to go up to his apartment.There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor \(a\) (it is currently motionless), the second elevator is located ... | The first line of the input contains the only \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.This is followed by \(t\) lines, three integers each \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10^8\), \(b \ne c\)) β floor numbers described in the statement. | Output \(t\) numbers, each of which is the answer to the corresponding test case. As an answer, output: \(1\), if it is better to call the first elevator; \(2\), if it is better to call the second one; \(3\), if it doesn't matter which elevator to call (both elevators will arrive in the same time). | In the first test case of the example, the first elevator is already on the floor of \(1\).In the second test case of the example, when called, the elevators would move as follows: At the time of the call, the first elevator is on the floor of \(3\), and the second one is on the floor of \(1\), but is already going to ... | Input: 31 2 33 1 23 2 1 | Output: 1 3 2 | Beginner | 1 | 872 | 263 | 299 | 17 |
1,430 | A | 1430A | A. Number of Apartments | 900 | brute force; constructive algorithms; math | Recently a new building with a new layout was constructed in Monocarp's hometown. According to this new layout, the building consists of three types of apartments: three-room, five-room, and seven-room apartments. It's also known that each room of each apartment has exactly one window. In other words, a three-room apar... | Th first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of windows in the building. | For each test case, if a building with the new layout and the given number of windows just can't exist, print \(-1\).Otherwise, print three non-negative integers β the possible number of three-room, five-room, and seven-room apartments. If there are multiple answers, print any of them. | Input: 4 30 67 4 14 | Output: 2 2 2 7 5 3 -1 0 0 2 | Beginner | 3 | 1,382 | 213 | 286 | 14 | |
575 | I | 575I | I. Robots protection | 2,800 | data structures | Company ""Robots industries"" produces robots for territory protection. Robots protect triangle territories β right isosceles triangles with catheti parallel to North-South and East-West directions.Owner of some land buys and sets robots on his territory to protect it. From time to time, businessmen want to build offic... | The first line contains integer N β width and height of the land, and integer Q β number of queries to handle.Next Q lines contain queries you need to process.Two types of queries: 1 dir x y len β add a robot to protect a triangle. Depending on the value of dir, the values of x, y and len represent a different triangle... | For each second type query output how many robots guard this point. Each answer should be in a separate line. | Input: 17 101 1 3 2 41 3 10 3 71 2 6 8 21 3 9 4 22 4 41 4 15 10 62 7 72 9 42 12 22 13 8 | Output: 22201 | Master | 1 | 416 | 879 | 109 | 5 | |
241 | C | 241C | C. Mirror Box | 2,000 | geometry; implementation | Mirror Box is a name of a popular game in the Iranian National Amusement Park (INAP). There is a wooden box, 105 cm long and 100 cm high in this game. Some parts of the box's ceiling and floor are covered by mirrors. There are two negligibly small holes in the opposite sides of the box at heights hl and hr centimeters ... | The first line of the input contains three space-separated integers hl, hr, n (0 < hl, hr < 100, 0 β€ n β€ 100) β the heights of the holes and the number of the mirrors.Next n lines contain the descriptions of the mirrors. The i-th line contains space-separated vi, ci, ai, bi; the integer vi (1 β€ vi β€ 1000) is the score ... | The only line of output should contain a single integer β the maximum possible score a player could gain. | The second sample is depicted above. The red beam gets 10 + 50 + 5 + 35 + 8 + 2 = 110 points and the blue one gets 120.The red beam on the picture given in the statement shows how the laser beam can go approximately, this is just illustration how the laser beam can gain score. So for the second sample there is no such ... | Input: 50 50 710 F 1 8000020 T 1 8000030 T 81000 8200040 T 83000 8400050 T 85000 8600060 T 87000 8800070 F 81000 89000 | Output: 100 | Hard | 2 | 917 | 855 | 105 | 2 |
2,068 | J | 2068J | J. The Ultimate Wine Tasting Event | 2,000 | combinatorics; greedy | Rumors of the excellence of Gabriella's wine tasting events have toured the world and made it to the headlines of prestigious wine magazines. Now, she has been asked to organize an event at the EUC 2025!This time she selected \(2n\) bottles of wine, of which exactly \(n\) are of white wine, and exactly \(n\) of red win... | The first line contains an integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β where \(2n\) is the total number of bottles.The second line of each test case contains a string \(... | For each test case, print \(\texttt{YES}\) if it is possible to divide the bottles as explained in the statement. Otherwise, print \(\texttt{NO}\). | In the first test case, we can make one subset out of the bottles at positions \(1\), \(2\), \(3\) and \(7\) (which are, respectively: white, red, red, red) and the other subset out of the bottles at positions \(4\), \(5\), \(6\), \(8\) (which are, respectively: white, white, white, red). We will then swap pairs \((1, ... | Input: 34WRRWWWRR1WR20WWWWRRWRRRRRWRRWRWRRWRRWWWWWWWRWWRWWRRRR | Output: YES NO YES | Hard | 2 | 1,097 | 595 | 147 | 20 |
81 | A | 81A | A. Plug-in | 1,400 | implementation | Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase ""how are you"" he can type ""hhoow aaaare... | The input data consists of a single line to be processed. The length of the line is from 1 to 2Β·105 characters inclusive. The string contains only lowercase Latin letters. | Print the given string after it is processed. It is guaranteed that the result will contain at least one character. | Input: hhoowaaaareyyoouu | Output: wre | Easy | 1 | 1,132 | 171 | 115 | 0 | |
113 | A | 113A | A. Grammar Lessons | 1,600 | implementation; strings | Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: There are three parts of speech:... | The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105.It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that ... | If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print ""NO"" (without the quotes). Otherwise, print ""YES"" (without the quotes). | Input: petr | Output: YES | Medium | 2 | 1,942 | 370 | 200 | 1 | |
60 | B | 60B | B. Serial Time! | 1,400 | dfs and similar; dsu | The Cereal Guy's friend Serial Guy likes to watch soap operas. An episode is about to start, and he hasn't washed his plate yet. But he decided to at least put in under the tap to be filled with water. The plate can be represented by a parallelepiped k Γ n Γ m, that is, it has k layers (the first layer is the upper one... | The first line contains three numbers k, n, m (1 β€ k, n, m β€ 10) which are the sizes of the plate. Then follow k rectangles consisting of n lines each containing m characters '.' or '#', which represents the ""layers"" of the plate in the order from the top to the bottom. The rectangles are separated by empty lines (se... | The answer should contain a single number, showing in how many minutes the plate will be filled. | Input: 1 1 1.1 1 | Output: 1 | Easy | 2 | 1,033 | 649 | 96 | 0 | |
1,996 | C | 1996C | C. Sort | 1,200 | dp; greedy; sortings; strings | You are given two strings \(a\) and \(b\) of length \(n\). Then, you are (forced against your will) to answer \(q\) queries.For each query, you are given a range bounded by \(l\) and \(r\). In one operation, you can choose an integer \(i\) (\(l \leq i \leq r\)) and set \(a_i = x\) where \(x\) is any character you desir... | The first line contains \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)) β the length of both strings and the number of queries.The following line contains \(a\) of length \(n\). It is guaranteed \(a\) on... | For each query, output an integer, the minimum number of operations you need to perform in a new line. | For the first query, \(\texttt{sorted(a[1..5])} =\) abcde and \(\texttt{sorted(b[1..5])} =\) abcde, so no operations are necessary.For the second query, you need to set \(a_1 = \) e. Then, \(\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = \) bcde. | Input: 35 3abcdeedcba1 51 43 34 2zzdeazbe1 31 46 3uwuwuwwuwuwu2 41 31 6 | Output: 0 1 0 2 2 1 1 0 | Easy | 4 | 684 | 682 | 102 | 19 |
1,413 | E | 1413E | E. Solo mid Oracle | 2,100 | greedy; math; ternary search | Meka-Naruto plays a computer game. His character has the following ability: given an enemy hero, deal \(a\) instant damage to him, and then heal that enemy \(b\) health points at the end of every second, for exactly \(c\) seconds, starting one second after the ability is used. That means that if the ability is used at ... | The first line contains an integer \(t\) (\(1\leq t\leq 10^5\)) standing for the number of testcases.Each test case is described with one line containing four numbers \(a\), \(b\), \(c\) and \(d\) (\(1\leq a, b, c, d\leq 10^6\)) denoting the amount of instant damage, the amount of heal per second, the number of heals a... | For each testcase in a separate line print \(-1\) if the skill can kill an enemy hero with an arbitrary number of health points, otherwise print the maximal number of health points of the enemy that can be killed. | In the first test case of the example each unit of damage is cancelled in a second, so Meka-Naruto cannot deal more than 1 damage.In the fourth test case of the example the enemy gets: \(4\) damage (\(1\)-st spell cast) at time \(0\); \(4\) damage (\(2\)-nd spell cast) and \(3\) heal (\(1\)-st spell cast) at time \(1\)... | Input: 7 1 1 1 1 2 2 2 2 1 2 3 4 4 3 2 1 228 21 11 3 239 21 11 3 1000000 1 1000000 1 | Output: 1 2 1 5 534 -1 500000500000 | Hard | 3 | 1,430 | 358 | 213 | 14 |
1,621 | H | 1621H | H. Trains and Airplanes | 3,500 | dfs and similar; graphs; shortest paths; trees | Railway network of one city consists of \(n\) stations connected by \(n-1\) roads. These stations and roads forms a tree. Station \(1\) is a city center. For each road you know the time trains spend to pass this road. You can assume that trains don't spend time on stops. Let's define \(dist(v)\) as the time that trains... | The first line contains the single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of stations.Each of the next \(n - 1\) lines contains three integers \(v_i\), \(u_i\), \(t_i\) (\(1 \leq v_i, u_i \leq n, 1 \leq t_i \leq 10^9\)) β the ends of the \(i\)-th road and the time it takes a train to pass this road... | For each query of the third type print the answer to it. | Note, that the fine can be cheaper than the pass. The railway network from the example. Green color is used for stations and roads of zone A, blue color is used for zone B and red color is used for zone D. The integer near each road is time that trains spend to pass it. In the first query, the airport can be located ne... | Input: 8 1 2 7 2 3 4 2 4 3 4 5 1 5 6 6 4 7 10 6 8 6 4 AABABBDB 11 12 10 42 16 15 15 30 4 6 3 2 1 A 10 3 3 2 A 3 3 7 3 6 | Output: 0 10 6 6 | Master | 4 | 2,854 | 1,457 | 56 | 16 |
475 | B | 475B | B. Strongly Connected City | 1,400 | brute force; dfs and similar; graphs; implementation | Imagine a city with n horizontal streets crossing m vertical streets, forming an (n - 1) Γ (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traff... | The first line of input contains two integers n and m, (2 β€ n, m β€ 20), denoting the number of horizontal streets and the number of vertical streets.The second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If the i-th character is equal to '<', the str... | If the given pattern meets the mayor's criteria, print a single line containing ""YES"", otherwise print a single line containing ""NO"". | The figure above shows street directions in the second sample test case. | Input: 3 3><>v^v | Output: NO | Easy | 4 | 700 | 760 | 137 | 4 |
600 | B | 600B | B. Queries about less or equal elements | 1,300 | binary search; data structures; sortings; two pointers | You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj. | The first line contains two integers n, m (1 β€ n, m β€ 2Β·105) β the sizes of arrays a and b.The second line contains n integers β the elements of array a ( - 109 β€ ai β€ 109).The third line contains m integers β the elements of array b ( - 109 β€ bj β€ 109). | Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj. | Input: 5 41 3 5 7 96 4 2 8 | Output: 3 2 1 4 | Easy | 4 | 180 | 254 | 152 | 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.