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,120 | F | 1120F | F. Secret Letters | 3,100 | data structures; dp; greedy | Little W and Little P decided to send letters to each other regarding the most important events during a day. There are \(n\) events during a day: at time moment \(t_i\) something happens to the person \(p_i\) (\(p_i\) is either W or P, denoting Little W and Little P, respectively), so he needs to immediately send a le... | The first line contains three integers \(n, c, d\) (\(1 \leq n \leq 10^5\), \(1 \leq c \leq 10^2\), \(1 \leq d \leq 10^8\)) — the number of letters, the cost of storing a letter for one time unit at Wise R's den and the cost of delivering a letter via Friendly O.The next \(n\) describe the events. The \(i\)-th of them ... | Print a single integer — the minimum possible cost of delivery of all letters. | One of optimal solutions in the first example: At time moment 0 Little P leaves the letter at Wise R's den. At time moment 1 Little W leaves his letter at Wise R's den and takes Little P's letter. This letter is at the den from time moment 0 to time moment 1, it costs \(1\) acorn. At time moment 3 Little P sends his le... | Input: 5 1 4 0 P 1 W 3 P 5 P 8 P 10 | Output: 16 | Master | 3 | 1,072 | 745 | 78 | 11 |
1,764 | F | 1764F | F. Doremy's Experimental Tree | 2,500 | brute force; constructive algorithms; dfs and similar; dsu; sortings; trees | Doremy has an edge-weighted tree with \(n\) vertices whose weights are integers between \(1\) and \(10^9\). She does \(\frac{n(n+1)}{2}\) experiments on it.In each experiment, Doremy chooses vertices \(i\) and \(j\) such that \(j \leq i\) and connects them directly with an edge with weight \(1\). Then, there is exactly... | The first line of input contains a single integer \(n\) (\(2 \le n \le 2000\)) — the number of vertices in the tree.The following \(n\) lines contain a lower-triangular matrix with \(i\) integers on the \(i\)-th line; the \(j\)-th integer on the \(i\)-th line is \(f(i,j)\) (\(0 \le f(i,j) \le 2\cdot 10^{15}\)).It is gu... | Print \(n-1\) lines describing the tree. In the \(i\)-th line of the output, output three integers \(u_i\), \(v_i\), \(w_i\) (\(1 \le u_i,v_i \le n\), \(1 \le w_i \le 10^9\)), representing an edge \((u_i,v_i)\) whose weight is \(w_i\).If there are multiple answers, you may output any.All edges must form a tree and all ... | In the first test case, the picture below, from left to right, from top to bottom, shows the graph when pairs \((1,1)\), \((1,2)\), \((1,3)\), \((2,2)\), \((2,3)\), \((3,3)\) are connected with an edge, respectively. The nodes colored yellow are on the cycle. | Input: 3 7 3 5 0 2 8 | Output: 2 3 3 1 2 2 | Expert | 6 | 1,222 | 486 | 377 | 17 |
1,107 | E | 1107E | E. Vasya and Binary String | 2,400 | dp | Vasya has a string \(s\) of length \(n\) consisting only of digits 0 and 1. Also he has an array \(a\) of length \(n\). Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of the... | The first line contains one integer \(n\) (\(1 \le n \le 100\)) — the length of string \(s\).The second line contains string \(s\), consisting only of digits 0 and 1.The third line contains \(n\) integers \(a_1, a_2, \dots a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the number of points for erasing the substring o... | Print one integer — the maximum total points Vasya can get. | In the first example the optimal sequence of erasings is: 1101001 \(\rightarrow\) 111001 \(\rightarrow\) 11101 \(\rightarrow\) 1111 \(\rightarrow\) \(\varnothing\).In the second example the optimal sequence of erasings is: 10101 \(\rightarrow\) 1001 \(\rightarrow\) 11 \(\rightarrow\) \(\varnothing\). | Input: 7 1101001 3 4 9 100 1 2 3 | Output: 109 | Expert | 1 | 552 | 335 | 59 | 11 |
2,073 | A | 2073A | 1,900 | Hard | 0 | 0 | 0 | 0 | 20 | |||||||
575 | C | 575C | C. Party | 2,700 | bitmasks; brute force; graph matchings | Note the unusual memory limit for the problem.People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday.There are N people working in MDCS and there are N clubs in the city. Unfortunately, if there is more than one Microsoft employee in night club,... | The first line contains integer N — number of employees in MDCS.Then an N × N matrix follows, where element in i-th row and j-th column is an integer number that represents how much i-th person likes j-th club’s Friday party.Then another N × N matrix follows, where element in i-th row and j-th column is an integer numb... | Output should contain a single integer — maximum sum of happiness possible. | Here is how we matched people with clubs:Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness). Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness).4+4+81+78 = 167 | Input: 41 2 3 42 3 4 13 4 1 24 1 2 35 8 7 16 9 81 355 78 1 61 1 1 1 | Output: 167 | Master | 3 | 857 | 466 | 75 | 5 |
1,482 | D | 1482D | D. Playlist | 1,900 | data structures; dsu; implementation; shortest paths | Arkady has a playlist that initially consists of \(n\) songs, numerated from \(1\) to \(n\) in the order they appear in the playlist. Arkady starts listening to the songs in the playlist one by one, starting from song \(1\). The playlist is cycled, i. e. after listening to the last song, Arkady will continue listening ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of songs.The second line contains \(n\) integers \(a_1, a_2... | For each test case, print a single line. First, print a single integer \(k\) — the number of deleted songs. After that print \(k\) distinct integers: deleted songs in the order of their deletion. | Explanation of the first test case is given in the statement.In the second test case, the playlist is converted as follows: [1, 2, 4, 2, 4, 2] \(\to\) [1, 2, 4, 2, 4, 2] \(\to\) [1, 4, 2, 4, 2] (because \(\operatorname{gcd}(1, 2) = 1\)) \(\to\) [1, 4, 2, 4, 2] \(\to\) [1, 4, 2, 4, 2] \(\to\) [1, 4, 2, 4, 2] \(\to\) [1,... | Input: 5 5 5 9 2 10 15 6 1 2 4 2 4 2 2 1 2 1 1 1 2 | Output: 2 2 3 2 2 1 2 2 1 1 1 0 | Hard | 4 | 1,654 | 471 | 195 | 14 |
398 | B | 398B | B. Painting The Wall | 0 | dp; probabilities | User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at leas... | The first line contains two integers n and m (1 ≤ n ≤ 2·103; 0 ≤ m ≤ min(n2, 2·104)) — the size of the wall and the number of painted cells.Next m lines goes, each contains two integers ri and ci (1 ≤ ri, ci ≤ n) — the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows... | In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error. | Input: 5 22 34 1 | Output: 11.7669491886 | Beginner | 2 | 933 | 419 | 162 | 3 | |
1,218 | G | 1218G | G. Alpha planetary system | 3,000 | constructive algorithms; graphs; shortest paths | Three planets \(X\), \(Y\) and \(Z\) within the Alpha planetary system are inhabited with an advanced civilization. The spaceports of these planets are connected by interplanetary space shuttles. The flight scheduler should decide between \(1\), \(2\) and \(3\) return flights for every existing space shuttle connection... | The first row of the input is the integer number \(N\) \((3 \leq N \leq 100 000)\), representing overall number of spaceports. The second row is the integer number \(M\) \((2 \leq M \leq 100 000)\) representing number of shuttle flight connections. Third row contains \(N\) characters from the set \(\{X, Y, Z\}\). Lette... | The same representation of shuttle flights in separate rows as in the input, but also containing a third number from the set \(\{1, 2, 3\}\) standing for the number of shuttle flights between these spaceports. | Input: 10 15 XXXXYYYZZZ 0 4 0 5 0 6 4 1 4 8 1 7 1 9 7 2 7 5 5 3 6 2 6 9 8 2 8 3 9 3 | Output: 0 4 2 0 5 2 0 6 2 4 1 1 4 8 1 1 7 2 1 9 3 7 2 2 7 5 1 5 3 1 6 2 1 6 9 1 8 2 3 8 3 1 9 3 1 | Master | 3 | 1,000 | 911 | 209 | 12 | |
1,528 | E | 1528E | E. Mashtali and Hagh Trees | 2,900 | combinatorics; dp; trees | Today is Mashtali's birthday! He received a Hagh tree from Haj Davood as his birthday present!A directed tree is called a Hagh tree iff: The length of the longest directed path in it is exactly \(n\). Every vertex has at most three edges attached to it independent of their orientation. Let's call vertices \(u\) and \(v... | The first line of input contains a single integer \(n\) \((1 \le n \le 10^6)\). | Print a single integer, the answer to Mashtali's task modulo \(998244353\). | All the five Hagh trees for \(n = 1\): | Input: 1 | Output: 5 | Master | 3 | 1,751 | 79 | 75 | 15 |
409 | F | 409F | F. 000001 | 1,900 | *special | The input contains a single integer a (1 ≤ a ≤ 64). | Output a single integer. | Input: 2 | Output: 1 | Hard | 1 | 0 | 51 | 24 | 4 | ||
1,748 | F | 1748F | F. Circular Xor Reversal | 3,000 | bitmasks; constructive algorithms | You have an array \(a_0, a_1, \ldots, a_{n-1}\) of length \(n\). Initially, \(a_i = 2^i\) for all \(0 \le i \lt n\). Note that array \(a\) is zero-indexed. You want to reverse this array (that is, make \(a_i\) equal to \(2^{n-1-i}\) for all \(0 \le i \lt n\)). To do this, you can perform the following operation no more... | The first line contains a single integer \(n\) (\(2 \le n \le 400\)) — the length of the array \(a\). | On the first line print one integer \(k\) (\(0 \le k \le 250\,000\)) — the number of operations performed.On the second line print \(k\) integers \(i_1,i_2,\ldots,i_k\) (\(0 \le i_j \lt n\)). Here, \(i_j\) should be the integer selected on the \(j\)-th operation.Note that you don't need to minimize the number of operat... | In the notes, the elements on which the operations are performed are colored red.In the first test case, array \(a\) will change in the following way: \([1,\color{red}{2}] \rightarrow [\color{red}{1},3] \rightarrow [2,\color{red}{3}] \rightarrow [2,1]\).In the second test case, array \(a\) will change in the following ... | Input: 2 | Output: 3 1 0 1 | Master | 2 | 672 | 101 | 325 | 17 |
600 | A | 600A | A. Extract Numbers | 1,600 | implementation; strings | You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string ""aba,123;1a;0"": ""aba"", ""123"", ""1a"", ""0"". A word can be empty: for example, the string s="";;"" contains three empty words separated b... | The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters. | Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34).If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line. | In the second example the string s contains five words: ""1"", """", ""01"", ""a0"", """". | Input: aba,123;1a;0 | Output: ""123,0""""aba,1a"" | Medium | 2 | 934 | 189 | 259 | 6 |
472 | B | 472B | B. Design Tutorial: Learn from Life | 1,300 | One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following w... | The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator.The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person. | Output a single integer — the minimal time needed to achieve the goal. | In first sample, an optimal solution is: The elevator takes up person #1 and person #2. It goes to the 2nd floor. Both people go out of the elevator. The elevator goes back to the 1st floor. Then the elevator takes up person #3. And it goes to the 2nd floor. It picks up person #2. Then it goes to the 3rd floor. Person ... | Input: 3 22 3 4 | Output: 8 | Easy | 0 | 907 | 248 | 70 | 4 | |
1,938 | L | 1938L | 2,800 | Master | 0 | 0 | 0 | 0 | 19 | |||||||
1,202 | A | 1202A | A. You Are Given Two Binary Strings... | 1,100 | bitmasks; greedy | You are given two binary strings \(x\) and \(y\), which are binary representations of some two integers (let's denote these integers as \(f(x)\) and \(f(y)\)). You can choose any integer \(k \ge 0\), calculate the expression \(s_k = f(x) + f(y) \cdot 2^k\) and write the binary representation of \(s_k\) in reverse order... | The first line contains a single integer \(T\) (\(1 \le T \le 100\)) — the number of queries.Next \(2T\) lines contain a description of queries: two lines per query. The first line contains one binary string \(x\), consisting of no more than \(10^5\) characters. Each character is either 0 or 1.The second line contains ... | Print \(T\) integers (one per query). For each query print such \(k\) that \(rev_k\) is lexicographically minimal. | The first query was described in the legend.In the second query, it's optimal to choose \(k = 3\). The \(2^3 = 1000_2\) so \(s_3 = 10001_2 + 110_2 \cdot 1000_2 = 10001 + 110000 = 1000001\) and \(rev_3 = 1000001\). For example, if \(k = 0\), then \(s_0 = 10111\) and \(rev_0 = 11101\), but \(rev_3 = 1000001\) is lexicogr... | Input: 4 1010 11 10001 110 1 1 1010101010101 11110000 | Output: 1 3 0 0 | Easy | 2 | 763 | 766 | 114 | 12 |
1,267 | A | 1267A | 2,400 | Expert | 0 | 0 | 0 | 0 | 12 | |||||||
596 | C | 596C | C. Wilbur and Points | 1,700 | combinatorics; greedy; sortings | Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set.Now Wilbur wants to number the points in the set he has, that is as... | The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with.Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are disti... | If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print ""YES"" on the first line of the output. Otherwise, print ""NO"".If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there ... | In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi.In the second sample, the special values of the points in the set are ... | Input: 52 00 01 01 10 10 -1 -2 1 0 | Output: YES0 01 02 00 11 1 | Medium | 3 | 1,164 | 692 | 362 | 5 |
665 | F | 665F | F. Four Divisors | 2,400 | data structures; dp; math; number theory; sortings; two pointers | If an integer a is divisible by another integer b, then b is called the divisor of a.For example: 12 has positive 6 divisors. They are 1, 2, 3, 4, 6 and 12.Let’s define a function D(n) — number of integers between 1 and n (inclusive) which has exactly four positive divisors.Between 1 and 10 only the integers 6, 8 and 1... | The only line contains integer n (1 ≤ n ≤ 1011) — the parameter from the problem statement. | Print the only integer c — the number of integers between 1 and n with exactly four divisors. | Input: 10 | Output: 3 | Expert | 6 | 427 | 91 | 93 | 6 | |
2,069 | C | 2069C | C. Beautiful Sequence | 1,500 | combinatorics; dp; greedy; two pointers | Let's call an integer sequence beautiful if the following conditions hold: its length is at least \(3\); for every element except the first one, there is an element to the left less than it; for every element except the last one, there is an element to the right larger than it; For example, \([1, 4, 2, 4, 7]\) and \([1... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 3\)).Additional constraint on the input: the ... | For each test case, print a single integer — the number of beautiful subsequences of the array \(a\), taken modulo \(998244353\). | In the first test case of the example, the following subsequences are beautiful: \([a_3, a_4, a_7]\); \([a_3, a_5, a_7]\); \([a_3, a_4, a_5, a_7]\). | Input: 473 2 1 2 2 1 343 1 2 231 2 391 2 3 2 1 3 2 2 3 | Output: 3 0 1 22 | Medium | 4 | 812 | 385 | 129 | 20 |
1,844 | A | 1844A | A. Subtraction Game | 800 | constructive algorithms; games | You are given two positive integers, \(a\) and \(b\) (\(a < b\)).For some positive integer \(n\), two players will play a game starting with a pile of \(n\) stones. They take turns removing exactly \(a\) or exactly \(b\) stones from the pile. The player who is unable to make a move loses.Find a positive integer \(n\) s... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The only line of each test case contains two integers, \(a\) and \(b\) (\(1 \le a < b \le 100\)). | For each test case, output any positive integer \(n\) (\(1 \le n \le 10^6\)) such that the second player to move wins.It can be proven that such an \(n\) always exists under the constraints of the problem. | In the first test case, when \(n = 2\), the first player must remove \(a = 1\) stone. Then, the second player can respond by removing \(a = 1\) stone. The first player can no longer make a move, so the second player wins.In the second test case, when \(n = 6\), the first player has two options: If they remove \(b = 5\)... | Input: 31 41 59 26 | Output: 2 6 3 | Beginner | 2 | 572 | 257 | 205 | 18 |
1,277 | D | 1277D | D. Let's Play the Words? | 1,900 | data structures; hashing; implementation; math | Polycarp has \(n\) different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: ""0001"", ""11"", ""0"" and ""0011100"".Polycarp wants to offer his set of \(n\) binary words to play a game ""words"". In this game, players name words and each next word (st... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains one integer \(n\) (\(1 \le n \le 2\cdot10^5\)) — the number of words in the Polycarp's set. Next \(n\) lines contain these words. All... | Print answer for all of \(t\) test cases in the order they appear.If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain \(k\) (\(0 \le k \le n\)) — the minimal number of words in the set which should be reversed. The second line of the output should contain \(k\) dist... | Input: 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 | Output: 1 3 -1 0 2 1 2 | Hard | 4 | 1,234 | 675 | 584 | 12 | |
1,170 | E | 1170E | E. Sliding Doors | 0 | *special; binary search | Imagine that you are the CEO of a big old-fashioned company. Unlike any modern and progressive company (such as JetBrains), your company has a dress code. That's why you have already allocated a spacious room for your employees where they can change their clothes. Moreover, you've already purchased an \(m\)-compartment... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 4 \cdot 10^5\)) — the number of doors and compartments respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le m\), \(\sum{a_i} \le m\)) — the corresponding widths of the doors.The th... | Print \(q\) answers. Each answer is ""YES"" or ""NO"" (case insensitive). Print ""YES"" if it is possible, that all employees on the corresponding day can access their compartments simultaneously. | Input: 3 10 2 3 2 6 1 5 2 1 10 2 2 9 2 5 6 3 1 7 8 4 1 2 3 4 | Output: YES YES NO NO YES NO | Beginner | 2 | 1,374 | 837 | 196 | 11 | |
1,763 | D | 1763D | D. Valid Bitonic Permutations | 2,200 | combinatorics; dp; implementation; math; number theory | You are given five integers \(n\), \(i\), \(j\), \(x\), and \(y\). Find the number of bitonic permutations \(B\), of the numbers \(1\) to \(n\), such that \(B_i=x\), and \(B_j=y\). Since the answer can be large, compute it modulo \(10^9+7\).A bitonic permutation is a permutation of numbers, such that the elements of th... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The description of test cases follows.The only line of each test case contains five integers, \(n\), \(i\), \(j\), \(x\), and \(y\) (\(3 \le n \le 100\) and \(1 \le i,j,x,y \le n\)). ... | For each test case, output a single line containing the number of bitonic permutations satisfying the above conditions modulo \(10^9+7\). | A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).An array of \... | Input: 73 1 3 2 33 2 3 3 24 3 4 3 15 2 5 2 45 3 4 5 49 3 7 8 620 6 15 8 17 | Output: 0 1 1 1 3 0 4788 | Hard | 5 | 471 | 368 | 137 | 17 |
2,108 | E | 2108E | E. Spruce Dispute | 2,600 | constructive algorithms; dfs and similar; graphs; greedy; implementation; shortest paths; trees | It's already a hot April outside, and Polycarp decided that this is the perfect time to finally take down the spruce tree he set up several years ago. As he spent several hours walking around it, gathering his strength, he noticed something curious: the spruce is actually a tree\(^{\text{∗}}\) — and not just any tree, ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains an odd number \(n\) (\(3 \le n < 2 \cdot 10^5\)) — the number of vertices in the tree.The following \(n-1\) lines de... | For each test case, you need to output two lines.In the first line, output the pair of vertices \(u\), \(v\), the edge between which Polycarp is going to remove.In the next line, output the array \(c\) of \(n\) numbers from \(0\) to \(\frac{n-1}{2}\), where \(c[i]\) — the positive color number assigned to vertex \(i\).... | Consider the first test case.Remove the edge connecting vertices \(1\) and \(2\). After this, vertex \(2\) will be removed from the tree, and vertices \(3\) and \(4\) will be connected to vertex \(1\).Color vertices \(3\) and \(4\) with the first color, and vertices \(1\) and \(5\) with the second. The sum of the lengt... | Input: 351 22 32 44 551 21 31 41 571 55 44 33 22 66 7 | Output: 1 2 2 0 1 1 2 1 5 1 1 2 2 0 4 3 1 3 3 0 2 2 1 | Expert | 7 | 1,607 | 511 | 394 | 21 |
2,039 | D | 2039D | D. Shohag Loves GCD | 1,700 | constructive algorithms; greedy; math; number theory | Shohag has an integer \(n\) and a set \(S\) of \(m\) unique integers. Help him find the lexicographically largest\(^{\text{∗}}\) integer array \(a_1, a_2, \ldots, a_n\) such that \(a_i \in S\) for each \(1 \le i \le n\) and \(a_{\operatorname{gcd}(i, j)} \neq \operatorname{gcd}(a_i, a_j)\)\(^{\text{†}}\) is satisfied o... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 10^5\)).The second line contains \(m\) unique integers in increasing order, representing the elements of the set \(S\) (\(1 \le x \... | For each test case, if there is no solution print \(-1\), otherwise print \(n\) integers — the lexicographically largest integer array that satisfies the conditions. | In the first test case, every element in the array belongs to the given set \(S = \{3, 4, 6\}\), and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair \((2, 3)\), \(a_{\operatorname{gcd}(2, 3)} = a_1 = 6\) and \(\operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4\), so ... | Input: 36 33 4 61 112 12 | Output: 6 4 4 3 4 3 1 -1 | Medium | 4 | 740 | 441 | 165 | 20 |
1,097 | C | 1097C | C. Yuhao and a Parenthesis | 1,400 | greedy; implementation | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting cha... | The first line contains one integer \(n\) (\(1 \leq n \leq 10^5\)) — the number of bracket sequences.Each of the following \(n\) lines contains one bracket sequence — a non-empty string which consists only of characters ""("" and "")"".The sum of lengths of all bracket sequences in the input is at most \(5 \cdot 10^5\)... | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | In the first example, it's optimal to construct two pairs: ""(( )())"" and ""( )"". | Input: 7 )()) ) (( (( ( ) ) | Output: 2 | Easy | 2 | 1,015 | 531 | 116 | 10 |
887 | E | 887E | E. Little Brother | 2,800 | binary search; geometry; sortings | Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister. Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.At first, the line going through two points, that brother drew, doesn't int... | First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that ... | Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.The output is considered correct if it has a relative or absolute error of at most 10 - 4. | Input: 2 4 7 1333 0 112 4 2-4 14 2 | Output: 5.1478150705 | Master | 3 | 1,056 | 516 | 247 | 8 | |
1,533 | E | 1533E | E. Chess Team Forming | 0 | *special; binary search; data structures; greedy | Polycarp is coaching a team for an upcoming game at the chess tournament. A complete team for the tournament should consist of \(n+1\) members.There are \(n\) members in his team, the \(i\)-th member's skill value is \(a_i\). Polycarp is yet to choose the final member for the team.The opposing team has \(n+1\) members,... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of members in the Polycarp's team.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the skill value of the \(i\)-th player of the Polycarp's team.The third line contains \... | Print \(m\) integers — the \(k\)-th of them should be equal to the smallest difficulty of the game for the team Polycarp can achieve if he picks the \(k\)-th option player as the final one. | In the first example the optimal pairings for the first three options are the following. Note that there might be multiple valid pairing for the minimum answer.First option:Polycarp's team: 6 1 3 1 10 4Opposing team: 9 4 2 5 9 8The respective difficulties of the game for each player are: 3, 3, -1, 4, -1, 4. The maximum... | Input: 5 10 3 4 6 1 9 4 2 5 9 8 5 1 7 6 4 8 | Output: 4 2 3 4 2 | Beginner | 4 | 1,253 | 790 | 189 | 15 |
762 | C | 762C | C. Two strings | 2,100 | binary search; hashing; strings; two pointers | You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of th... | The first line contains string a, and the second line — string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters. | On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.If the answer consists of zero characters, output «-» (a minus sign). | In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b. | Input: hibob | Output: - | Hard | 4 | 501 | 214 | 193 | 7 |
1,737 | G | 1737G | G. Ela Takes Dancing Class | 3,500 | binary search; data structures | DTL engineers love partying in the weekend. Ela does, too! Unfortunately, she didn't know how to dance yet. Therefore, she decided to take a dancing class.There are \(n\) students in the dancing class, including Ela. In the final project, \(n\) students will participate in a choreography described below.\(n\) students ... | The first line contains three integers \(n\), \(d\) and \(q\) (\(1 \le n \le 10^5\); \(1 \le d \le 10^9\); \(1 \le q \le 10^5\)) — the number of dancers, the positive energy value of the choreography, and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_1 < a_2 < \dots <... | Output \(q\) lines, each contains a single integer — the answer for the corresponding query. | Let's consider the first example test case.In the first minute, \(1\) is the lowest coordinate between movable dancers. The energy level is initiated with \(3\). Then the following happens: The dancer moves from \(1\) to \(2\). The energy level decreased to \(2\). The dancer moves from \(2\) to \(3\). The energy level ... | Input: 4 3 8 1 3 6 7 1011 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 | Output: 3 5 6 7 3 6 7 10 | Master | 2 | 2,143 | 728 | 92 | 17 |
1,129 | C | 1129C | C. Morse Code | 2,400 | binary search; data structures; dp; hashing; sortings; string suffix structures; strings | In Morse code, an letter of English alphabet is represented as a string of some length from \(1\) to \(4\). Moreover, each Morse code representation of an English letter contains only dots and dashes. In this task, we will represent a dot with a ""0"" and a dash with a ""1"".Because there are \(2^1+2^2+2^3+2^4 = 30\) s... | The first line contains an integer \(m\) (\(1 \leq m \leq 3\,000\)) — the number of modifications to \(S\). Each of the next \(m\) lines contains either a ""0"" (representing a dot) or a ""1"" (representing a dash), specifying which character should be appended to \(S\). | Print \(m\) lines, the \(i\)-th of which being the answer after the \(i\)-th modification to \(S\). | Let us consider the first sample after all characters have been appended to \(S\), so S is ""111"".As you can see, ""1"", ""11"", and ""111"" all correspond to some distinct English letter. In fact, they are translated into a 'T', an 'M', and an 'O', respectively. All non-empty sequences of English letters that are rep... | Input: 3111 | Output: 137 | Expert | 7 | 1,119 | 271 | 99 | 11 |
772 | B | 772B | B. Volatile Kite | 1,800 | geometry | You are given a convex polygon P with n distinct vertices p1, p2, ..., pn. Vertex pi has coordinates (xi, yi) in the 2D plane. These vertices are listed in clockwise order.You can choose a real number D and move each vertex of the polygon a distance of at most D from their original positions.Find the maximum value of D... | The first line has one integer n (4 ≤ n ≤ 1 000) — the number of vertices.The next n lines contain the coordinates of the vertices. Line i contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th vertex. These points are guaranteed to be given in clockwise order, and will form a strictly co... | Print one real number D, which is the maximum real number such that no matter how you move the vertices, the polygon stays convex.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely, let's assume that your answer is a and the answer of the jury is b. The checker progra... | Here is a picture of the first sampleHere is an example of making the polygon non-convex.This is not an optimal solution, since the maximum distance we moved one point is ≈ 0.4242640687, whereas we can make it non-convex by only moving each point a distance of at most ≈ 0.3535533906. | Input: 40 00 11 11 0 | Output: 0.3535533906 | Medium | 1 | 423 | 408 | 360 | 7 |
1,812 | G | 1812G | G. Colour Vision | 0 | *special; implementation | Input: 11RR | Output: YES | Beginner | 2 | 0 | 0 | 0 | 18 | ||||
1,275 | E2 | 1275E2 | E2. Контрольная сумма | 0 | *special | Данные пользователей ВКонтакте хранятся на десятках тысяч серверов. Для того, чтобы можно было определять ошибки при записи данных на диск, на диск регулярно записываются текущие контрольные суммы CRC32 (Wiki, IEEE 802-3). Благодаря этому, при чтении данных можно заново вычислить контрольную сумму и проверить, что данн... | В первой строке дано два целых числа \(n\) и \(q\) — количество байт в файле и количество запросов, для которых нужно решить задачу (\(8 \le n \le 2 \cdot 10^5\); \(1 \le q \le 10^5\)).Во второй строке дано \(n\) чисел \(a_0, a_1, \ldots, a_{n-1}\) — содержимое файла в байтах (\(0 \le a_i \le 255\)).В следующих \(q\) с... | Для каждого запроса выведите четыре целых числа \(z_0, z_1, z_2, z_3\), на которые нужно заменить четыре байта с номерами \(j, j+1, j+2, j+3\), чтобы crc32 не изменился. Обратите внимание, что все запросы независимы, и на самом деле последовательность не изменяется.Если существует несколько решений, выведите любое, а е... | CRC32 байтовой последовательности из первого примера (1 2 3 4 5 6 7 8) равен 3fca88c5, CRC32 измененной последовательности (0 0 0 0 212 34 127 159) так же равен 3fca88c5. Стандартная утилита crc32 из большинства дистрибутивов линукса должна посчитать от них данную контрольную сумму.CRC32 последовательности из второго п... | Input: 8 1 1 2 3 4 5 6 7 8 0 4 0 0 0 0 | Output: 212 34 127 159 | Beginner | 1 | 1,646 | 673 | 388 | 12 |
1,734 | A | 1734A | A. Select Three Sticks | 800 | brute force; greedy; sortings | You are given \(n\) sticks with positive integral length \(a_1, a_2, \ldots, a_n\).You can perform the following operation any number of times (possibly zero): choose one stick, then either increase or decrease its length by \(1\). After each operation, all sticks should have positive lengths. What is the minimum numbe... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 300\)) — the number of sticks.The second line of each test case contains \(n\) integers ... | For each test case, print one integer on a single line — the minimum number of operations to be made. | In the first test case, you can increase the length of the first stick by \(1\), then decrease the length of the third stick by \(1\). In total, you perform \(2\) operations, such that the three sticks form an equilateral triangle of side length \(2\).In the fourth test case, you can decrease the length of the seventh ... | Input: 431 2 347 3 7 353 4 2 1 183 1 4 1 5 9 2 6 | Output: 2 4 1 1 | Beginner | 3 | 572 | 482 | 101 | 17 |
921 | 08 | 92108 | 08. Labyrinth-8 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 | |||||
346 | B | 346B | B. Lucky Common Subsequence | 2,000 | dp; strings | In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substri... | The input contains three strings in three separate lines: s1, s2 and virus (1 ≤ |s1|, |s2|, |virus| ≤ 100). Each string consists only of uppercase English letters. | Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted. If there is no valid common subsequence, output 0. | Input: AJKEQSLOBSROFGZOVGURWZLWVLUXTHOZ | Output: ORZ | Hard | 2 | 515 | 163 | 192 | 3 | |
1,220 | E | 1220E | E. Tourism | 2,200 | dfs and similar; dp; dsu; graphs; greedy; trees | Alex decided to go on a touristic trip over the country.For simplicity let's assume that the country has \(n\) cities and \(m\) bidirectional roads connecting them. Alex lives in city \(s\) and initially located in it. To compare different cities Alex assigned each city a score \(w_i\) which is as high as interesting c... | First line of input contains two integers \(n\) and \(m\), (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\)) which are numbers of cities and roads in the country.Second line contains \(n\) integers \(w_1, w_2, \ldots, w_n\) (\(0 \le w_i \le 10^9\)) which are scores of all cities.The following \(m\) lines con... | Output single integer which is the maximum possible sum of scores of visited cities. | Input: 5 7 2 2 8 6 9 1 2 1 3 2 4 3 2 4 5 2 5 1 5 2 | Output: 27 | Hard | 6 | 816 | 788 | 84 | 12 | |
1,100 | B | 1100B | B. Build a Contest | 1,300 | data structures; implementation | Arkady coordinates rounds on some not really famous competitive programming platform. Each round features \(n\) problems of distinct difficulty, the difficulties are numbered from \(1\) to \(n\).To hold a round Arkady needs \(n\) new (not used previously) problems, one for each difficulty. As for now, Arkady creates al... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) — the number of difficulty levels and the number of problems Arkady created.The second line contains \(m\) integers \(a_1, a_2, \ldots, a_m\) (\(1 \le a_i \le n\)) — the problems' difficulties in the order Arkady created them. | Print a line containing \(m\) digits. The \(i\)-th digit should be \(1\) if Arkady held the round after creation of the \(i\)-th problem, and \(0\) otherwise. | In the first example Arkady held the round after the first three problems, because they are of distinct difficulties, and then only after the last problem. | Input: 3 11 2 3 1 2 2 2 3 2 2 3 1 | Output: 00100000001 | Easy | 2 | 1,056 | 305 | 158 | 11 |
1,571 | H | 1571H | H. Laser Beams | 2,900 | *special; geometry; probabilities | Ira is developing a computer game. This game features randomized generation and difficulty of levels. To achieve randomized difficulty, some enemies in each level are randomly replaced with stronger ones.To describe how do the levels in the game look, let's introduce a coordinate system in such a way that \(Ox\) axis g... | The first line contains three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 100\); \(2 \le a, b \le 100\)) — the number of enemies in the level and the dimensions of the level.Then \(n\) lines follow, the \(i\)-th of them contains three integers \(x_i\), \(y_i\) and \(p'_i\) (\(1 \le x_i \le a - 1\); \(1 \le y_i \le b... | Print one integer — the expected number of parts in which the lasers divide the level, taken modulo \(998244353\) (i. e. let the expected number of parts be \(\frac{x}{y}\) as an irreducible fraction; you have to print \(x \cdot y^{-1} \bmod 998244353\), where \(y^{-1}\) is a number such that \(y \cdot y^{-1} \bmod 998... | Explanation to the first example:With probability \(\frac{1}{2}\), the only enemy is not upgraded and the level looks like this (\(4\) parts): With probability \(\frac{1}{2}\), the only enemy is upgraded and the level looks like this (\(8\) parts): So, the expected number of parts is \(4 \cdot \frac{1}{2} + 8 \cdot \fr... | Input: 1 2 2 1 1 500000 | Output: 6 | Master | 3 | 1,940 | 524 | 334 | 15 |
1,987 | B | 1987B | B. K-Sort | 1,000 | greedy | You are given an array of integers \(a\) of length \(n\). You can apply the following operation any number of times (maybe, zero): First, choose an integer \(k\) such that \(1 \le k \le n\) and pay \(k + 1\) coins. Then, choose exactly \(k\) indices such that \(1 \le i_1 < i_2 < \ldots < i_k \le n\). Then, for each \(x... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array \(a\).The second ... | For each test case, output a single integer — the minimum number of coins needed to make \(a\) non-decreasing. | In the first test case, \(a\) is already sorted, so you don't have to spend any coins.In the second test case, the optimal sequence of operations is: Choose \(k = 2\) and the indices \(2\) and \(5\): \([ 2, \color{red}{1}, 4, 7, \color{red}{6} ] \rightarrow [2, 2, 4, 7, 7]\). This costs \(3\) coins. It can be proven th... | Input: 531 7 952 1 4 7 641 3 2 411799344 12 37 60 311 613 365 328 675 | Output: 0 3 2 0 1821 | Beginner | 1 | 489 | 536 | 110 | 19 |
1,912 | E | 1912E | E. Evaluate It and Back Again | 2,200 | constructive algorithms; implementation; math | Aidan and Nadia are long-time friends with a shared passion for mathematics. Each of them has a favorite number: Aidan's favorite number is \(p\), and Nadia's is \(q\). To commemorate their friendship, their friends want to make a present: a plaque with an arithmetic expression whose value is equal to their favorite nu... | The first line of the input contains two integers \(p\) and \(q\) (\(-10^{18} \le p, q \le 10^{18}\)). | Print the expression without spaces or line breaks. It can only contain digits 0 through 9, '+', '-', and '*' characters.The expression must contain at most \(1000\) characters. Leading zeros in numbers are not allowed (the only exception is the notation '0' representing the number \(0\)) in both the expression and its... | Input: 1998 -3192 | Output: 2023-12-13 | Hard | 3 | 927 | 102 | 479 | 19 | |
1,146 | E | 1146E | E. Hot is Cold | 2,400 | bitmasks; data structures; divide and conquer; implementation | You are given an array of \(n\) integers \(a_1, a_2, \ldots, a_n\).You will perform \(q\) operations. In the \(i\)-th operation, you have a symbol \(s_i\) which is either ""<"" or "">"" and a number \(x_i\).You make a new array \(b\) such that \(b_j = -a_j\) if \(a_j s_i x_i\) and \(b_j = a_j\) otherwise (i.e. if \(s_i... | The first line contains two integers \(n,q\) (\(1 \leq n,q \leq 10^5\)) — the number of integers and the number of queries.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^5 \leq a_i \leq 10^5\)) — the numbers.Each of the next \(q\) lines contains a character and an integer \(s_i, x_i\). (\(s_i \i... | Print \(n\) integers \(c_1, c_2, \ldots, c_n\) representing the array after all operations. | In the first example, the array goes through the following changes: Initial: \([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]\) \(> 2\): \([-5, -4, -3, -2, -1, 0, 1, 2, -3, -4, -5]\) \(> -4\): \([-5, -4, 3, 2, 1, 0, -1, -2, 3, -4, -5]\) \(< 5\): \([5, 4, -3, -2, -1, 0, 1, 2, -3, 4, 5]\) | Input: 11 3 -5 -4 -3 -2 -1 0 1 2 3 4 5 > 2 > -4 < 5 | Output: 5 4 -3 -2 -1 0 1 2 -3 4 5 | Expert | 4 | 504 | 374 | 91 | 11 |
1,227 | C | 1227C | C. Messy | 1,700 | constructive algorithms | You are fed up with your messy room, so you decided to clean it up.Your room is a bracket sequence \(s=s_{1}s_{2}\dots s_{n}\) of length \(n\). Each character of this string is either an opening bracket '(' or a closing bracket ')'.In one operation you can choose any consecutive substring of \(s\) and reverse it. In ot... | The first line contains integer number \(t\) (\(1 \le t \le 100\)) — the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains two integers \(n\) and \(k\) (\(1 \le k \le \frac{n}{2}, 2 \le n \le 2000\), \(n\) is even) — length of \(s\) and required number of regular pre... | For each test case print an answer.In the first line print integer \(m\) (\(0 \le m \le n\)) — the number of operations. You do not need to minimize \(m\), any value is suitable.In the following \(m\) lines print description of the operations, each line should contain two integers \(l,r\) (\(1 \le l \le r \le n\)), rep... | In the first example, the final sequence is ""()(()())"", where two prefixes are regular, ""()"" and ""()(()())"". Note, that all the operations except ""5 8"" in the example output are useless (they do not change \(s\)). | Input: 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( | Output: 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 | Medium | 1 | 1,776 | 670 | 686 | 12 |
293 | A | 293A | A. Weird Game | 1,500 | games; greedy | Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game. Roman leaves a word for each of them. Each word consists of 2·n binary characters ""0"" or ""1"". After that the players start moving in turns. Yaroslav moves f... | The first line contains integer n (1 ≤ n ≤ 106). The second line contains string s — Yaroslav's word. The third line contains string t — Andrey's word.It is guaranteed that both words consist of 2·n characters ""0"" and ""1"". | Print ""First"", if both players play optimally well and Yaroslav wins. If Andrey wins, print ""Second"" and if the game ends with a draw, print ""Draw"". Print the words without the quotes. | Input: 201110001 | Output: First | Medium | 2 | 1,384 | 226 | 190 | 2 | |
1,172 | C2 | 1172C2 | C2. Nauuo and Pictures (hard version) | 2,600 | dp; probabilities | The only difference between easy and hard versions is constraints.Nauuo is a girl who loves random picture websites.One day she made a random picture website by herself which includes \(n\) pictures.When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probabi... | The first line contains two integers \(n\) and \(m\) (\(1\le n\le 2\cdot 10^5\), \(1\le m\le 3000\)) — the number of pictures and the number of visits to the website.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(a_i\) is either \(0\) or \(1\)) — if \(a_i=0\) , Nauuo does not like the \(i\)-th pictur... | The output contains \(n\) integers \(r_1,r_2,\ldots,r_n\) — the expected weights modulo \(998244353\). | In the first example, if the only visit shows the first picture with a probability of \(\frac 2 3\), the final weights are \((1,1)\); if the only visit shows the second picture with a probability of \(\frac1 3\), the final weights are \((2,2)\).So, both expected weights are \(\frac2 3\cdot 1+\frac 1 3\cdot 2=\frac4 3\)... | Input: 2 1 0 1 2 1 | Output: 332748119 332748119 | Expert | 2 | 1,281 | 653 | 102 | 11 |
1,936 | F | 1936F | F. Grand Finale: Circles | 3,300 | binary search; geometry | You are given \(n\) circles on the plane. The \(i\)-th of these circles is given by a tuple of integers \((x_i, y_i, r_i)\), where \((x_i, y_i)\) are the coordinates of its center, and \(r_i\) is the radius of the circle.Please find a circle \(C\) which meets the following conditions: \(C\) is contained inside all \(n\... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of circles.The \(i\)-th of the following \(n\) lines contains three integers \(x_i\), \(y_i\), \(r_i\) (\(-10^6 \le x_i,y_i \le 10^6\), \(1 \le r_i \le 2 \cdot 10^6\)).It is guaranteed that there is a circle with a radius of at least \(1... | Output three real values, \(x\), \(y\), and \(r\) — the coordinates of the center and the radius of the circle. | A two-dimensional plot depicting the first test case is given below. The output circle \(C\) is dashed with blue lines. A two-dimensional plot depicting the second test case is given below. The output circle \(C\) is dashed with blue lines. | Input: 4 1 1 3 -1 1 3 1 -1 2 -1 -1 2 | Output: 0.0000000000000000 -0.7637626158259733 0.9724747683480533 | Master | 2 | 853 | 373 | 111 | 19 |
351 | D | 351D | D. Jeff and Removing Periods | 2,700 | data structures | Cosider a sequence, consisting of n integers: a1, a2, ..., an. Jeff can perform the following operation on sequence a: take three integers v, t, k (1 ≤ v, t ≤ n; 0 ≤ k; v + tk ≤ n), such that av = av + t, av + t = av + 2t, ..., av + t(k - 1) = av + tk; remove elements av, av + t, ..., av + t·k from the sequence a, the ... | The first line contains integer m (1 ≤ m ≤ 105). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 105). The third line contains integer q (1 ≤ q ≤ 105) — the number of questions. The next q lines contain pairs of integers, i-th of them contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ m) — the description of... | In q lines print the answers to Jeff's queries. Print the answers according to the order of questions in input. | Input: 52 2 1 1 251 51 12 21 32 3 | Output: 21122 | Master | 1 | 872 | 335 | 111 | 3 | |
1,638 | A | 1638A | A. Reverse | 800 | constructive algorithms; greedy; math | You are given a permutation \(p_1, p_2, \ldots, p_n\) of length \(n\). You have to choose two integers \(l,r\) (\(1 \le l \le r \le n\)) and reverse the subsegment \([l,r]\) of the permutation. The permutation will become \(p_1,p_2, \dots, p_{l-1},p_r,p_{r-1}, \dots, p_l,p_{r+1},p_{r+2}, \dots ,p_n\).Find the lexicogra... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 500\)) — the length of the permutation.The second line of each te... | For each test case print the lexicographically smallest permutation you can obtain. | In the first test case, the permutation has length \(1\), so the only possible segment is \([1,1]\). The resulting permutation is \([1]\).In the second test case, we can obtain the identity permutation by reversing the segment \([1,2]\). The resulting permutation is \([1,2,3]\).In the third test case, the best possible... | Input: 41132 1 341 4 2 351 2 3 4 5 | Output: 1 1 2 3 1 2 4 3 1 2 3 4 5 | Beginner | 3 | 936 | 433 | 83 | 16 |
1,749 | F | 1749F | F. Distance to the Path | 2,800 | data structures; dfs and similar; trees | You are given a tree consisting of \(n\) vertices. Initially, each vertex has a value \(0\).You need to perform \(m\) queries of two types: You are given a vertex index \(v\). Print the value of the vertex \(v\). You are given two vertex indices \(u\) and \(v\) and values \(k\) and \(d\) (\(d \le 20\)). You need to add... | The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of vertices in the tree.Next \(n - 1\) lines contain the edges of the tree — one per line. Each line contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\); \(u \neq v\)) representing one edge of the tree. It's guaranteed th... | For each query of the first type, print the value of the corresponding vertex. | The tree from the first example: Some query explanations: ""\(2\) \(4\) \(5\) \(10\) \(2\)"": affected vertices are \(\{4, 2, 5, 1, 3\}\); ""\(2\) \(1\) \(1\) \(10\) \(20\)"" and ""\(2\) \(6\) \(6\) \(10\) \(20\)"": all vertices are affected, since distance to \(1\) (\(6\)) is less that \(20\) for any vertex; ""\(2\) \... | Input: 6 1 2 1 3 4 2 5 2 3 6 14 2 4 5 10 2 1 3 1 6 2 1 1 10 20 2 6 6 10 20 1 3 2 3 2 10 0 2 5 2 10 1 1 1 1 2 1 3 1 4 1 5 1 6 | Output: 10 0 30 50 50 40 40 40 20 | Master | 3 | 810 | 824 | 78 | 17 |
748 | D | 748D | D. Santa Claus and a Palindrome | 2,100 | constructive algorithms; data structures; greedy | Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this s... | The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k ≤ 100 000).k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The stri... | In the only line print the required maximum possible beauty. | In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order). | Input: 7 3abb 2aaa -3bba -1zyz -4abb 5aaa 7xyx 4 | Output: 12 | Hard | 3 | 870 | 469 | 60 | 7 |
58 | D | 58D | D. Calendar | 2,000 | greedy; strings | BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one sep... | The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-c... | Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the ""<"" operator in the modern progra... | Input: 4baahgc. | Output: aa.bc.hg | Hard | 2 | 1,229 | 544 | 336 | 0 | |
207 | D7 | 207D7 | D7. The Beaver's Problem - 3 | 1,600 | The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj... | The first line contains integer id (0 ≤ id ≤ 106) — the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in... | Print an integer from 1 to 3, inclusive — the number of the subject the given document corresponds to. | Medium | 0 | 1,472 | 653 | 102 | 2 | |||
608 | B | 608B | B. Hamming Distance Sum | 1,500 | combinatorics; strings | Genos needs your help. He was asked to solve the following programming problem by Saitama:The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance ... | The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).Both strings are guaranteed to consist of characters '0' and '1' only. | Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|. | For the first sample case, there are four contiguous substrings of b of length |a|: ""00"", ""01"", ""11"", and ""11"". The distance between ""01"" and ""00"" is |0 - 0| + |1 - 0| = 1. The distance between ""01"" and ""01"" is |0 - 0| + |1 - 1| = 0. The distance between ""01"" and ""11"" is |0 - 1| + |1 - 1| = 1. Last ... | Input: 0100111 | Output: 3 | Medium | 2 | 556 | 219 | 113 | 6 |
1,005 | C | 1005C | C. Summarize to the Power of Two | 1,300 | brute force; greedy; implementation | A sequence \(a_1, a_2, \dots, a_n\) is called good if, for each element \(a_i\), there exists an element \(a_j\) (\(i \ne j\)) such that \(a_i+a_j\) is a power of two (that is, \(2^d\) for some non-negative integer \(d\)).For example, the following sequences are good: \([5, 3, 11]\) (for example, for \(a_1=5\) we can c... | The first line contains the integer \(n\) (\(1 \le n \le 120000\)) — the length of the given sequence.The second line contains the sequence of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)). | Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all \(n\) elements, make it empty, and thus get a good sequence. | In the first example, it is enough to delete one element \(a_4=5\). The remaining elements form the sequence \([4, 7, 1, 4, 9]\), which is good. | Input: 64 7 1 5 4 9 | Output: 1 | Easy | 3 | 1,154 | 202 | 211 | 10 |
331 | E2 | 331E2 | E2. Deja Vu | 3,100 | constructive algorithms; dp | Everybody knows that we have been living in the Matrix for a long time. And in the new seventh Matrix the world is ruled by beavers.So let's take beaver Neo. Neo has so-called ""deja vu"" outbursts when he gets visions of events in some places he's been at or is going to be at. Let's examine the phenomenon in more deta... | The first line contains integers n and m — the number of shops and the number of streets, correspondingly, 1 ≤ n ≤ 50, . Next m lines contain the descriptions of the streets in the following format: xi yi ki v1 v2 ... vk, where xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi) are numbers of shops connected by a street, ki (0 ≤ ki ≤... | Subproblem E1. In the first line print an integer k (1 ≤ k ≤ 2·n) — the numbers of shops on Neo's path. In the next line print k integers — the number of shops in the order Neo passes them. If the graph doesn't have such paths or the length of the shortest path includes more than 2·n shops, print on a single line 0.Sub... | The input in both samples are the same. The first sample contains the answer to the first subproblem, the second sample contains the answer to the second subproblem. | Input: 6 61 2 2 1 22 3 1 33 4 2 4 54 5 05 3 1 36 1 1 6 | Output: 46 1 2 3 | Master | 2 | 1,132 | 886 | 464 | 3 |
1,509 | C | 1509C | C. The Sports Festival | 1,800 | dp; greedy | The student council is preparing for the relay race at the sports festival.The council consists of \(n\) members. They will run one after the other in the race, the speed of member \(i\) is \(s_i\). The discrepancy \(d_i\) of the \(i\)-th stage is the difference between the maximum and the minimum running speed among t... | The first line contains a single integer \(n\) (\(1 \le n \le 2000\)) — the number of members of the student council.The second line contains \(n\) integers \(s_1, s_2, \dots, s_n\) (\(1 \le s_i \le 10^9\)) – the running speeds of the members. | Print a single integer — the minimum possible value of \(d_1 + d_2 + \dots + d_n\) after choosing the order of the members. | In the first test case, we may choose to make the third member run first, followed by the first member, and finally the second. Thus \(a_1 = 2\), \(a_2 = 3\), and \(a_3 = 1\). We have: \(d_1 = \max(2) - \min(2) = 2 - 2 = 0\). \(d_2 = \max(2, 3) - \min(2, 3) = 3 - 2 = 1\). \(d_3 = \max(2, 3, 1) - \min(2, 3, 1) = 3 - 1 =... | Input: 3 3 1 2 | Output: 3 | Medium | 2 | 722 | 243 | 123 | 15 |
690 | C1 | 690C1 | C1. Brain Network (easy) | 1,300 | One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite!... | The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is... | The output consists of one line, containing either yes or no depending on whether the nervous system is valid. | Input: 4 41 22 33 14 1 | Output: no | Easy | 0 | 1,182 | 385 | 110 | 6 | ||
1,234 | A | 1234A | A. Equalize Prices Again | 800 | math | You are both a shop keeper and a shop assistant at a small nearby shop. You have \(n\) goods, the \(i\)-th good costs \(a_i\) coins.You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all \(n\) goo... | The first line of the input contains one integer \(q\) (\(1 \le q \le 100\)) — the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 100)\) — the number of goods. The second line of the query contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \... | For each query, print the answer for it — the minimum possible equal price of all \(n\) goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices. | Input: 3 5 1 2 3 4 5 3 1 2 2 4 1 1 1 1 | Output: 3 2 1 | Beginner | 1 | 1,071 | 380 | 239 | 12 | |
2,008 | D | 2008D | D. Sakurako's Hobby | 1,100 | dp; dsu; graphs; math | For a certain permutation \(p\)\(^{\text{∗}}\) Sakurako calls an integer \(j\) reachable from an integer \(i\) if it is possible to make \(i\) equal to \(j\) by assigning \(i=p_i\) a certain number of times.If \(p=[3,5,6,1,2,4]\), then, for example, \(4\) is reachable from \(1\), because: \(i=1\) \(\rightarrow\) \(i=p_... | The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) — the number of elements in the array.The second line of each test case contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1\le p_i... | For each test case, output \(n\) integers \(F(1), F(2), \dots, F(n)\). | Input: 511051 2 4 5 31010155 4 1 3 21001163 5 6 1 2 401000061 2 3 4 5 6100110 | Output: 1 0 1 1 1 1 2 2 2 2 2 4 1 4 4 1 4 0 1 1 0 0 1 | Easy | 4 | 1,107 | 668 | 70 | 20 | |
1,406 | A | 1406A | A. Subset Mex | 900 | greedy; implementation; math | Given a set of integers (it can contain equal elements).You have to split it into two subsets \(A\) and \(B\) (both of them can contain equal elements or be empty). You have to maximize the value of \(mex(A)+mex(B)\).Here \(mex\) of a set denotes the smallest non-negative integer that doesn't exist in the set. For exam... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\leq t\leq 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\leq n\leq 100\)) — the size of the set.The second line of each testcase contains... | For each test case, print the maximum value of \(mex(A)+mex(B)\). | In the first test case, \(A=\left\{0,1,2\right\},B=\left\{0,1,5\right\}\) is a possible choice.In the second test case, \(A=\left\{0,1,2\right\},B=\varnothing\) is a possible choice.In the third test case, \(A=\left\{0,1,2\right\},B=\left\{0\right\}\) is a possible choice.In the fourth test case, \(A=\left\{1,3,5\right... | Input: 4 6 0 2 1 5 0 1 3 0 1 2 4 0 2 0 1 6 1 2 3 4 5 6 | Output: 5 3 4 0 | Beginner | 3 | 681 | 407 | 65 | 14 |
1,641 | F | 1641F | F. Covering Circle | 3,500 | geometry | Sam started playing with round buckets in the sandbox, while also scattering pebbles. His mom decided to buy him a new bucket, so she needs to solve the following task.You are given \(n\) distinct points with integer coordinates \(A_1, A_2, \ldots, A_n\). All points were generated from the square \([-10^8, 10^8] \times... | 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. Descriptions of test cases follow.The first line of each test case contains three integers \(n\), \(l\), \(k\) (\(2 \leq k \leq l \leq n \leq 50\,000\), \(k \leq 20\)).Each of the ... | For each test case print a single real number — the answer to the problem.Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-9}\). Formally let your answer be \(a\), jury answer be \(b\). Your answer will be considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}\... | In the first test case, we can select subsegment \(A_1, A_2\) and a circle with center \((0, 2)\) and radius \(2\).In the second test case, we can select subsegment \(A_1, A_2, A_3, A_4\) and a circle with center \((1, 2)\) and radius \(1\). | Input: 43 2 20 00 43 05 4 31 10 02 20 22 08 3 20 31 00 21 10 11 20 01 35 4 41 1-3 32 25 35 5 | Output: 2.00000000000000000000 1.00000000000000000000 0.50000000000000000000 4.00000000000000000000 | Master | 1 | 734 | 909 | 322 | 16 |
1,057 | B | 1057B | B. DDoS | 1,400 | *special; brute force | We get more and more news about DDoS-attacks of popular websites.Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds \(100 \cdot t\), where \(t\) — the number of seconds in this time segment. Arseny knows statistics on the number of re... | The first line contains \(n\) (\(1 \le n \le 5000\)) — number of seconds since server has been booted. The second line contains sequence of integers \(r_1, r_2, \dots, r_n\) (\(0 \le r_i \le 5000\)), \(r_i\) — number of requests in the \(i\)-th second. | Print the only integer number — the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0. | Input: 5100 200 1 1 1 | Output: 3 | Easy | 2 | 679 | 252 | 149 | 10 | |
1,586 | I | 1586I | I. Omkar and Mosaic | 3,500 | combinatorics; constructive algorithms; math | Omkar is creating a mosaic using colored square tiles, which he places in an \(n \times n\) grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells. A completed mosaic will be a mastapeece if and only if each tile is... | The first line contains a single integer \(n\) (\(1 \leq n \leq 2000\)).Then follow \(n\) lines with \(n\) characters in each line. The \(i\)-th character in the \(j\)-th line corresponds to the cell in row \(i\) and column \(j\) of the grid, and will be \(S\) if Omkar has placed a sinoper tile in this cell, \(G\) if O... | On the first line, print UNIQUE if there is a unique way to get a mastapeece, NONE if Omkar cannot create any, and MULTIPLE if there is more than one way to do so. All letters must be uppercase.If you print UNIQUE, then print \(n\) additional lines with \(n\) characters in each line, such that the \(i\)-th character in... | For the first test case, Omkar can make the mastapeecesSSSSSGGSSGGSSSSSand SSGGSSGGGGSSGGSS.For the second test case, it can be proven that it is impossible for Omkar to add tiles to create a mastapeece.For the third case, it can be proven that the given mastapeece is the only mastapeece Omkar can create by adding tile... | Input: 4 S... ..G. .... ...S | Output: MULTIPLE | Master | 3 | 579 | 373 | 457 | 15 |
1,582 | G | 1582G | G. Kuzya and Homework | 2,600 | data structures; number theory | Kuzya started going to school. He was given math homework in which he was given an array \(a\) of length \(n\) and an array of symbols \(b\) of length \(n\), consisting of symbols '*' and '/'.Let's denote a path of calculations for a segment \([l; r]\) (\(1 \le l \le r \le n\)) in the following way: Let \(x=1\) initial... | The first line contains a single integer \(n\) (\(2 \le n \le 10^6\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^6\)).The third line contains \(n\) symbols without spaces between them — the array \(b_1, b_2 \ldots b_n\) (\(b_i=\) '/' or \(b_i=\) '*' for every \(1 \le i \le n\)... | Print a single integer — the number of simple segments \([l;r]\). | Input: 3 1 2 3 */* | Output: 2 | Expert | 2 | 1,211 | 322 | 65 | 15 | |
1,913 | A | 1913A | A. Rating Increase | 800 | implementation | Monocarp is a great solver of adhoc problems. Recently, he participated in an Educational Codeforces Round, and gained rating!Monocarp knew that, before the round, his rating was \(a\). After the round, it increased to \(b\) (\(b > a\)). He wrote both values one after another to not forget them.However, he wrote them s... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.The only line of each testcase consists of a single string \(ab\) of length from \(2\) to \(8\) that: consists only of digits; doesn't start with a zero. | For each testcase, determine if such values \(a\) and \(b\) exist. If they don't, print -1. Otherwise, print two integers \(a\) and \(b\).If there are multiple answers, you can print any of them. | In the second testcase, printing \(3\) and \(91125\) is also valid.In the third testcase, \(20\) and \(0200\) is not valid, because \(b\) has a leading zero. \(200\) and \(200\) is not valid, because \(200\) is not strictly greater than \(200\). | Input: 520002001391125200200200100012 | Output: 2000 2001 39 1125 -1 200 1000 1 2 | Beginner | 1 | 700 | 249 | 195 | 19 |
1,526 | B | 1526B | B. I Hate 1111 | 1,400 | dp; math; number theory | You are given an integer \(x\). Can you make \(x\) by summing up some number of \(11, 111, 1111, 11111, \ldots\)? (You can use any number among them any number of times).For instance, \(33=11+11+11\) \(144=111+11+11+11\) | The first line of input contains a single integer \(t\) \((1 \leq t \leq 10000)\) — the number of testcases.The first and only line of each testcase contains a single integer \(x\) \((1 \leq x \leq 10^9)\) — the number you have to make. | For each testcase, you should output a single string. If you can make \(x\), output ""YES"" (without quotes). Otherwise, output ""NO"".You can print each letter of ""YES"" and ""NO"" in any case (upper or lower). | Ways to make \(33\) and \(144\) were presented in the statement. It can be proved that we can't present \(69\) this way. | Input: 3 33 144 69 | Output: YES YES NO | Easy | 3 | 220 | 236 | 212 | 15 |
2,044 | D | 2044D | D. Harder Problem | 1,100 | constructive algorithms; greedy; math | Given a sequence of positive integers, a positive integer is called a mode of the sequence if it occurs the maximum number of times that any positive integer occurs. For example, the mode of \([2,2,3]\) is \(2\). Any of \(9\), \(8\), or \(7\) can be considered to be a mode of the sequence \([9,9,8,8,7,7]\). You gave UF... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of \(a\).The following line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)).It is guarant... | For each test case, output \(n\) numbers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq n\)) on a new line. It can be shown that \(b\) can always be constructed. If there are multiple possible arrays, you may print any. | Let's verify the correctness for our sample output in test case \(2\). At \(i = 1\), \(1\) is the only possible mode of \([1]\). At \(i = 2\), \(1\) is the only possible mode of \([1, 1]\). At \(i = 3\), \(1\) is the only possible mode of \([1, 1, 2]\). At \(i = 4\), \(1\) or \(2\) are both modes of \([1, 1, 2, 2]\). S... | Input: 421 241 1 1 284 5 5 5 1 1 2 1101 1 2 2 1 1 3 3 1 1 | Output: 1 2 1 1 2 2 4 5 5 1 1 2 2 3 1 8 2 2 1 3 3 9 1 1 | Easy | 3 | 693 | 399 | 217 | 20 |
841 | A | 841A | A. Generous Kefa | 900 | brute force; implementation | One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si — lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give out all baloons to his friends. Help Kefa to find out, can he give out all his baloons, ... | The first line contains two integers n and k (1 ≤ n, k ≤ 100) — the number of baloons and friends.Next line contains string s — colors of baloons. | Answer to the task — «YES» or «NO» in a single line.You can choose the case (lower or upper) for each letter arbitrary. | In the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second.In the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is «NO». | Input: 4 2aabb | Output: YES | Beginner | 2 | 486 | 146 | 119 | 8 |
1,881 | C | 1881C | C. Perfect Square | 1,200 | brute force; implementation | Kristina has a matrix of size \(n\) by \(n\), filled with lowercase Latin letters. The value of \(n\) is even.She wants to change some characters so that her matrix becomes a perfect square. A matrix is called a perfect square if it remains unchanged when rotated \(90^\circ\) clockwise once.Here is an example of rotati... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^2\)) — the number of test cases.Then follows the description of each test case.The first line of each test case contains a single even integer \(n\) (\(2 \le n \le 10^3\)) — the number of rows and columns in the matrix.Then follows \(n\) line... | For each test case, output a single number on a separate line: the minimum number of operations required for Kristina to obtain a perfect square. | The first test case is explained in the problem statement. | Input: 54abbabcbbbccbabba2abba6codeforcescodeforcescodeforcescodefo4baaaabbabababaab4bbaaabbaaabaabba | Output: 1 2 181 5 9 | Easy | 2 | 842 | 461 | 145 | 18 |
1,920 | A | 1920A | A. Satisfying Constraints | 800 | brute force; greedy; math | Alex is solving a problem. He has \(n\) constraints on what the integer \(k\) can be. There are three types of constraints: \(k\) must be greater than or equal to some integer \(x\); \(k\) must be less than or equal to some integer \(x\); \(k\) must be not equal to some integer \(x\). Help Alex find the number of integ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 500\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 100\)) — the number of constraints.The following \(n\)... | For each test case, output a single integer — the number of integers \(k\) that satisfy all \(n\) constraints. | In the first test case, \(k \geq 3\) and \(k \leq 10\). Furthermore, \(k \neq 1\) and \(k \neq 5\). The possible integers \(k\) that satisfy the constraints are \(3,4,6,7,8,9,10\). So the answer is \(7\).In the second test case, \(k \ge 5\) and \(k \le 4\), which is impossible. So the answer is \(0\). | Input: 641 32 103 13 521 52 4103 63 71 21 73 1003 442 1002 981 33 9961 52 101 92 23 23 951 12 23 13 23 361 100002 9000000003 5000000001 1000000003 100003 900000001 | Output: 7 0 90 0 0 800000000 | Beginner | 3 | 571 | 948 | 110 | 19 |
1,213 | F | 1213F | F. Unstable String Sort | 2,100 | data structures; dfs and similar; dsu; graphs; greedy; implementation; strings | Authors have come up with the string \(s\) consisting of \(n\) lowercase Latin letters.You are given two permutations of its indices (not necessary equal) \(p\) and \(q\) (both of length \(n\)). Recall that the permutation is the array of length \(n\) which contains each integer from \(1\) to \(n\) exactly once.For all... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5, 1 \le k \le 26\)) — the length of the string and the number of distinct characters required.The second line of the input contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\), all \(p_i\) are distinct integers... | If it is impossible to find the suitable string, print ""NO"" on the first line.Otherwise print ""YES"" on the first line and string \(s\) on the second line. It should consist of \(n\) lowercase Latin letters, contain at least \(k\) distinct characters and suit the given permutations.If there are multiple answers, you... | Input: 3 2 1 2 3 1 3 2 | Output: YES abb | Hard | 7 | 815 | 544 | 343 | 12 | |
610 | D | 610D | D. Vika and Segments | 2,300 | constructive algorithms; data structures; geometry; two pointers | Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbo... | The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are ... | Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer. | In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3). | Input: 30 1 2 11 4 1 20 3 2 3 | Output: 8 | Expert | 4 | 498 | 406 | 132 | 6 |
1,862 | A | 1862A | A. Gift Carpet | 800 | dp; greedy; implementation; strings | Recently, Tema and Vika celebrated Family Day. Their friend Arina gave them a carpet, which can be represented as an \(n \cdot m\) table of lowercase Latin letters.Vika hasn't seen the gift yet, but Tema knows what kind of carpets she likes. Vika will like the carpet if she can read her name on. She reads column by col... | Each test consists of multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\), \(m\) (\(1 \le n, m \le 20\)) — the sizes of the carpet.The ne... | For each set of input data, output ""YES"" if Vika will like the carpet, otherwise output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first sample, Vika can read her name from left to right.In the second sample, Vika cannot read the character ""v"", so she will not like the carpet. | Input: 51 4vika3 3badcarpet4 4vvvviiiikkkkaaaa4 4vkakiiaiavvkviaa4 7vbickdavbickdavbickdavbickda | Output: YES NO YES NO YES | Beginner | 4 | 719 | 407 | 264 | 18 |
1,392 | I | 1392I | I. Kevin and Grid | 3,300 | fft; graphs; math | As Kevin is in BigMan's house, suddenly a trap sends him onto a grid with \(n\) rows and \(m\) columns.BigMan's trap is configured by two arrays: an array \(a_1,a_2,\ldots,a_n\) and an array \(b_1,b_2,\ldots,b_m\).In the \(i\)-th row there is a heater which heats the row by \(a_i\) degrees, and in the \(j\)-th column t... | The first line contains three integers \(n\),\(m\),\(q\) (\(1 \leq n,m,q \leq 10^5\)) – the number of rows, columns, and the number of possible values for \(x\) respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^5\)).The third line contains \(m\) integers \(b_1, b_2, \do... | Output \(q\) lines, in the \(i\)-th line output the answer for the \(i\)-th possible value of \(x\) from the input. | In the first example, the score for components with temperature smaller than \(5\) is \(1+2\), and the score for components with temperature at least \(5\) is \(2\). Thus, the final score is \(2-3=-1\). | Input: 5 5 1 1 3 2 3 1 1 3 2 3 1 5 | Output: -1 | Master | 3 | 2,019 | 446 | 115 | 13 |
1,830 | E | 1830E | E. Bully Sort | 3,500 | data structures; math | On a permutation \(p\) of length \(n\), we define a bully swap as follows: Let \(i\) be the index of the largest element \(p_i\) such that \(p_i \neq i\). Let \(j\) be the index of the smallest element \(p_j\) such that \(i < j\). Swap \(p_i\) and \(p_j\). We define \(f(p)\) as the number of bully swaps we need to perf... | The first line of the input contains two integers \(n\) and \(q\) (\(2 \le n \le 5 \cdot 10^5\), \(1 \le q \le 5 \cdot 10^4\)) — the length of the permutation and the number of updates.The second line of input contains \(n\) integer \(p_1,p_2,\ldots,p_n\) (\(1 \leq p_i \leq n\)) — the permutation \(p\). All elements of... | After each update, output \(f(p)\). | After the first update, we have \(f(p)=5\). The \(5\) bully swaps are illustrated below. \([\mathbf{1}, 2, \mathbf{8}, 5, 3, 4, 7, 6]\), \([1, 2, \mathbf{3}, 5, \mathbf{8}, 4, 7, 6]\), \([1, 2, 3, 5, \mathbf{4}, \mathbf{8}, 7, 6]\), \([1, 2, 3, 5, 4, \mathbf{6}, 7, \mathbf{8}]\), \([1, 2, 3, \mathbf{4}, \mathbf{5}, 6, ... | Input: 8 5 6 2 1 5 3 4 7 8 1 8 2 3 4 7 7 8 3 6 | Output: 5 6 9 8 7 | Master | 2 | 768 | 489 | 35 | 18 |
269 | E | 269E | E. String Theory | 3,100 | geometry; math; strings | Emuskald is an innovative musician and always tries to push the boundaries of music production. Now he has come up with an idea for a revolutionary musical instrument — a rectangular harp.A rectangular harp is a rectangle n × m consisting of n rows and m columns. The rows are numbered 1 to n from top to bottom. Similar... | The first line of input contains two space-separated integers numbers n and m (1 ≤ n, m ≤ 105), the height and width of the harp in units. Each of the following n + m lines contains 4 space-separated tokens, describing a single string: two symbols ai, bi and two integer numbers pi, qi. The pair ai, pi describes the fir... | If it is possible to rearrange the rows and columns to fix the harp, on the first line output n space-separated integers — the old numbers of rows now placed from top to bottom in the fixed harp. On the second line, output m space-separated integers — the old numbers of columns now placed from left to right in the fixe... | Input: 3 4L T 1 3L B 2 2L B 3 3T R 1 2T B 2 1T R 4 1B R 4 3 | Output: 1 2 3 3 2 1 4 | Master | 3 | 1,558 | 898 | 438 | 2 | |
1,705 | F | 1705F | F. Mark and the Online Exam | 2,900 | bitmasks; constructive algorithms; interactive; probabilities | Mark is administering an online exam consisting of \(n\) true-false questions. However, he has lost all answer keys. He needs a way to retrieve the answers before his client gets infuriated.Fortunately, he has access to the grading system. Thus, for each query, you can input the answers to all \(n\) questions, and the ... | The first line of the input consists of an integer \(n\) (\(1\leq n\leq 1000\)) — the number of questions. | The empty lines in the example are just for you to better understand the interaction process. You're not required to print them.In the first example, there are \(3\) questions, and the answer to each question is 'true', 'true', and 'false', respectively. The first query, guessing the answers to be 'false', 'true', and ... | Input: 3 1 3 | Output: FTT TTF | Master | 4 | 584 | 106 | 0 | 17 | |
482 | A | 482A | A. Diverse Permutation | 1,200 | constructive algorithms; greedy | Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k dis... | The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105). | Print n integers forming the permutation. If there are multiple answers, print any of them. | By |x| we denote the absolute value of number x. | Input: 3 2 | Output: 1 3 2 | Easy | 2 | 335 | 99 | 91 | 4 |
997 | C | 997C | C. Sky Full of Stars | 2,500 | combinatorics; math | On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.It is well known that one month on this planet consists of \(n^2\) days, so calendars, represented as square matrix \(n\) by \(n\) are extremely popular.Weather conditions are even more unusual. Due to the unique compo... | The first and only line of input contains a single integer \(n\) (\(1 \le n \le 1000\,000\)) — the number of rows and columns in the calendar. | Print one number — number of lucky colorings of the calendar modulo \(998244353\) | In the first sample any coloring is lucky, since the only column contains cells of only one color.In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky: While these colorings are not lucky: | Input: 1 | Output: 3 | Expert | 2 | 1,018 | 142 | 81 | 9 |
1,998 | C | 1998C | C. Perform Operations to Maximize Score | 1,900 | binary search; brute force; constructive algorithms; greedy; implementation | I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you.— satyam343's biggest fanYou are given an array \(a\) of length \(n\) and an integer \(k\). You are also given a binary array \(b\) of length \(n\). You can perform the following operation at most \(k\)... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case begins with two integers \(n\) and \(k\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq k \leq 10^9\)) — the length of the \(a\) and the number of operations you can perform.The following line contains \(n\) space sep... | For each test case, output the maximum value of score you can get on a new line. | For the first test case, it is optimal to perform \(5\) operations on both elements so \(a = [8,8]\). So, the maximum score we can achieve is \(\max(8 + \operatorname{median}[8], 8 + \operatorname{median}[8]) = 16\), as \(c_1 = [a_2] = [8]\). It can be proven that you cannot get a better score.For the second test case,... | Input: 82 103 31 13 103 3 30 0 04 42 1 5 10 1 0 15 47 5 2 5 40 0 1 0 15 15 15 15 2 111 0 0 1 15 210 11 4 10 151 1 0 1 04 41 1 2 51 1 0 02 10000000001000000000 10000000001 1 | Output: 16 6 8 13 21 26 8 3000000000 | Hard | 5 | 1,142 | 648 | 80 | 19 |
1,812 | F | 1812F | F. Factorization | 0 | *special; number theory | What is the largest prime factor of \(n\)? | The only line contains an integer \(n\) (\(n = 4167792762229302596005813\) or \(n =\) \(5023266485352224530541666346579718188045725802556947855902702063768930526665237598287495728\) \(2186920039740245544313021979167491414627648054421626450903732301970386214502290904360792618\) \(5591029614599889902115472391135622402044... | Beginner | 2 | 42 | 719 | 0 | 18 | |||
4 | B | 4B | B. Before an Exam | 1,200 | constructive algorithms; greedy | Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, th... | The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a ... | In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them. | Input: 1 485 7 | Output: NO | Easy | 2 | 925 | 403 | 308 | 0 | |
586 | A | 586A | A. Alena's Schedule | 900 | implementation | Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).The University works in such a way that every day it hol... | The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university. The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces. | Print a single number — the number of pairs during which Alena stays at the university. | In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair. In the last sample Alena doesn't have a single pair, so she spends all the time at home. | Input: 50 1 0 1 1 | Output: 4 | Beginner | 1 | 1,504 | 301 | 87 | 5 |
288 | B | 288B | B. Polo the Penguin and Houses | 1,500 | combinatorics | Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house... | The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n)) — the number of the houses and the number k from the statement. | In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7). | Input: 5 2 | Output: 54 | Medium | 1 | 1,152 | 159 | 96 | 2 | |
1,943 | B | 1943B | B. Non-Palindromic Substring | 2,000 | hashing; implementation; math; strings | A string \(t\) is said to be \(k\)-good if there exists at least one substring\(^\dagger\) of length \(k\) which is not a palindrome\(^\ddagger\). Let \(f(t)\) denote the sum of all values of \(k\) such that the string \(t\) is \(k\)-good.You are given a string \(s\) of length \(n\). You will have to answer \(q\) of th... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5\)), t... | For each query, output \(f(s_ls_{l + 1}\ldots s_r)\). | In the first query of the first test case, the string is \(\mathtt{aaab}\). \(\mathtt{aaab}\), \(\mathtt{aab}\) and \(\mathtt{ab}\) are all substrings that are not palindromes, and they have lengths \(4\), \(3\) and \(2\) respectively. Thus, the string is \(2\)-good, \(3\)-good and \(4\)-good. Hence, \(f(\mathtt{aaab})... | Input: 54 4aaab1 41 33 42 43 2abc1 31 25 4pqpcc1 54 51 32 42 1aa1 212 1steponnopets1 12 | Output: 9 0 2 5 5 2 14 0 2 5 0 65 | Hard | 4 | 970 | 699 | 53 | 19 |
630 | E | 630E | E. A rectangle | 1,900 | math | Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem.A field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will l... | The only line of input contains four integers x1, y1, x2, y2 ( - 109 ≤ x1 ≤ x2 ≤ 109, - 109 ≤ y1 ≤ y2 ≤ 109) — the coordinates of the centers of two cells. | Output one integer — the number of cells to be filled. | Input: 1 1 5 5 | Output: 13 | Hard | 1 | 1,261 | 155 | 54 | 6 | |
39 | D | 39D | D. Cubical Planet | 1,100 | math | You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0, 0, 0) and (1, 1, 1). Two flies live on the planet. At the moment ... | The first line contains three space-separated integers (0 or 1) — the coordinates of the first fly, the second line analogously contains the coordinates of the second fly. | Output ""YES"" (without quotes) if the flies see each other. Otherwise, output ""NO"". | Input: 0 0 00 1 0 | Output: YES | Easy | 1 | 536 | 171 | 86 | 0 | |
370 | B | 370B | B. Berland Bingo | 1,300 | implementation | Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers.During the game the host takes numbe... | The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer mi (1 ≤ mi ≤ 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers ai, 1, ai, ... | Print n lines, the i-th line must contain word ""YES"" (without the quotes), if the i-th player can win, and ""NO"" (without the quotes) otherwise. | Input: 31 13 2 4 12 10 11 | Output: YESNOYES | Easy | 1 | 948 | 514 | 147 | 3 | |
1,557 | C | 1557C | C. Moamen and XOR | 1,700 | bitmasks; combinatorics; dp; math; matrices | Moamen and Ezzat are playing a game. They create an array \(a\) of \(n\) non-negative integers where every element is less than \(2^k\).Moamen wins if \(a_1 \,\&\, a_2 \,\&\, a_3 \,\&\, \ldots \,\&\, a_n \ge a_1 \oplus a_2 \oplus a_3 \oplus \ldots \oplus a_n\).Here \(\&\) denotes the bitwise AND operation, and \(\oplus... | The first line contains a single integer \(t\) (\(1 \le t \le 5\))— the number of test cases. Each test case consists of one line containing two integers \(n\) and \(k\) (\(1 \le n\le 2\cdot 10^5\), \(0 \le k \le 2\cdot 10^5\)). | For each test case, print a single value — the number of different arrays that Moamen wins with.Print the result modulo \(1\,000\,000\,007\) (\(10^9 + 7\)). | In the first example, \(n = 3\), \(k = 1\). As a result, all the possible arrays are \([0,0,0]\), \([0,0,1]\), \([0,1,0]\), \([1,0,0]\), \([1,1,0]\), \([0,1,1]\), \([1,0,1]\), and \([1,1,1]\).Moamen wins in only \(5\) of them: \([0,0,0]\), \([1,1,0]\), \([0,1,1]\), \([1,0,1]\), and \([1,1,1]\). | Input: 3 3 1 2 1 4 0 | Output: 5 2 1 | Medium | 5 | 512 | 228 | 156 | 15 |
343 | C | 343C | C. Read Time | 1,900 | binary search; greedy; two pointers | Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the i... | The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in ascending order (1 ≤ hi ≤ 1010, hi < hi + 1) — the initial positions of the heads. The third line contains ... | Print a single number — the minimum time required, in seconds, to read all the needed tracks. | The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way: during the first second move the 1-st head to the left and let it stay there; move the second head to the left twice; move the third head to the right twice (note that the 6-th track has already been r... | Input: 3 42 5 61 3 6 8 | Output: 2 | Hard | 3 | 1,164 | 572 | 93 | 3 |
1,393 | C | 1393C | C. Pinkie Pie Eats Patty-cakes | 1,700 | constructive algorithms; greedy; math; sortings | Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the... | The first line contains a single integer \(T\) (\(1 \le T \le 100\)): the number of bags for which you need to solve the problem.The first line of each bag description contains a single integer \(n\) (\(2 \le n \le 10^5\)): the number of patty-cakes in it. The second line of the bag description contains \(n\) integers ... | For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag. | For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): \(1\), \(6\), \(4\), \(7\), \(1\), \(6\), \(4\) (in this way, the minimum distance is equal to \(3\)).For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): \(1\), \(4\), \(6\), \(7\), \(4\), ... | Input: 4 7 1 7 1 6 4 4 6 8 1 1 4 6 4 6 4 7 3 3 3 3 6 2 5 2 3 1 4 | Output: 3 2 0 4 | Medium | 4 | 1,005 | 675 | 188 | 13 |
865 | D | 865D | D. Buy Low Sell High | 2,400 | constructive algorithms; data structures; greedy | You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you... | Input begins with an integer N (2 ≤ N ≤ 3·105), the number of days.Following this is a line with exactly N integers p1, p2, ..., pN (1 ≤ pi ≤ 106). The price of one share of stock on the i-th day is given by pi. | Print the maximum amount of money you can end up with at the end of N days. | In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20. | Input: 910 5 4 7 9 12 6 2 10 | Output: 20 | Expert | 3 | 445 | 211 | 75 | 8 |
909 | C | 909C | C. Python Indentation | 1,800 | dp | In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation.We will consider an extremely simplified subset of Python with only two types of statements.Simple statements are written in a single line, one per line. An exampl... | The first line contains a single integer N (1 ≤ N ≤ 5000) — the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either ""f"" (denoting ""for statement"") or ""s"" (""simple statement""). It is guaranteed that the last line is a simple statement. | Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109 + 7. | In the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one.simple statementfor statement for statement simple statementIn the second test case, there are two ways to indent the program: the second for statement can either be part of the first ... | Input: 4sffs | Output: 1 | Medium | 1 | 874 | 318 | 123 | 9 |
1,714 | C | 1714C | C. Minimum Varied Number | 800 | greedy | Find the minimum number with the given sum of digits \(s\) such that all digits in it are distinct (i.e. all digits are unique).For example, if \(s=20\), then the answer is \(389\). This is the minimum number in which all digits are different and the sum of the digits is \(20\) (\(3+8+9=20\)).For the given \(s\) print ... | The first line contains an integer \(t\) (\(1 \le t \le 45\)) — the number of test cases.Each test case is specified by a line that contains the only integer \(s\) (\(1 \le s \le 45\)). | Print \(t\) integers — the answers to the given test cases. | Input: 42084510 | Output: 389 8 123456789 19 | Beginner | 1 | 340 | 185 | 59 | 17 | |
1,275 | D | 1275D | D. Storage2 | 0 | *special | Для надежного, но компактного хранения картинок во ВКонтакте используются избыточные коды. Все картинки разбиваются на группы по 15, и для каждой группы картинок вычисляются 13 избыточных кодов. Избыточные коды необходимы, чтобы суметь восстановить некоторые картинки, если они будут утеряны вследствие поломки жесткого ... | В единственной строке входных данных записано целое число \(k\) (\(0 \le k \le 28\)) — количество потерянных блоков. | Выведите два числа — вероятность, что хотя бы одну картинку невозможно восстановить из оставшихся блоков, и матожидание количества картинок, которые невозможно восстановить из оставшихся блоков.Ответы будут считаться корректными, если их абсолютная или относительная погрешность не превосходит \(10^{-9}\). | В первом примере утеряны все 28 блоков, очевидно, с 100% вероятностью потеряны все 15 картинок.Во втором примере только в 210 случаев из 376740 будет невозможно восстановить хотя бы одну картинку, поэтому вероятность \(210/376740\). В зависимости от расположения утерянных блоков будет невозможно восстановить от 1 до 6 ... | Input: 28 | Output: 1.00000000000000000000 15.00000000000000000000 | Beginner | 1 | 1,686 | 116 | 306 | 12 |
711 | B | 711B | B. Chris and Magic Square | 1,400 | constructive algorithms; implementation | ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.Chris tried filling in rando... | The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If th... | Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output - 1 instead.If there are multiple solutions, you may print any of them. | In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, The sum of numbers in each row is:4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.The sum of numbers in each column is:4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.The sum of numbers in the two diagonals is:4 + 5 + 6 = 2 ... | Input: 34 0 23 5 78 1 6 | Output: 9 | Easy | 2 | 856 | 506 | 271 | 7 |
49 | A | 49A | A. Sleuth | 800 | implementation | Vasya plays the sleuth with his friends. The rules of the game are as follows: those who play for the first time, that is Vasya is the sleuth, he should investigate a ""crime"" and find out what is happening. He can ask any questions whatsoever that can be answered with ""Yes"" or ""No"". All the rest agree beforehand ... | The single line contains a question represented by a non-empty line consisting of large and small Latin letters, spaces and a question mark. The line length does not exceed 100. It is guaranteed that the question mark occurs exactly once in the line — as the last symbol and that the line contains at least one letter. | Print answer for the question in a single line: YES if the answer is ""Yes"", NO if the answer is ""No"".Remember that in the reply to the question the last letter, not the last character counts. I. e. the spaces and the question mark do not count as letters. | Input: Is it a melon? | Output: NO | Beginner | 1 | 910 | 318 | 259 | 0 | |
1,349 | B | 1349B | B. Orac and Medians | 2,000 | constructive algorithms; greedy; math | Slime has a sequence of positive integers \(a_1, a_2, \ldots, a_n\).In one operation Orac can choose an arbitrary subsegment \([l \ldots r]\) of this sequence and replace all values \(a_l, a_{l + 1}, \ldots, a_r\) to the value of median of \(\{a_l, a_{l + 1}, \ldots, a_r\}\).In this problem, for the integer multiset \(... | The first line of the input is a single integer \(t\): the number of queries.The first line of each query contains two integers \(n\ (1\le n\le 100\,000)\) and \(k\ (1\le k\le 10^9)\), the second line contains \(n\) positive integers \(a_1,a_2,\dots,a_n\ (1\le a_i\le 10^9)\)The total sum of \(n\) is at most \(100\,000\... | The output should contain \(t\) lines. The \(i\)-th line should be equal to 'yes' if it is possible to make all integers \(k\) in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase. | In the first query, Orac can't turn all elements into \(3\).In the second query, \(a_1=6\) is already satisfied.In the third query, Orac can select the complete array and turn all elements into \(2\).In the fourth query, Orac can't turn all elements into \(3\).In the fifth query, Orac can select \([1,6]\) at first and ... | Input: 5 5 3 1 5 2 6 1 1 6 6 3 2 1 2 3 4 3 3 1 2 3 10 3 1 2 3 4 5 6 7 8 9 10 | Output: no yes yes no yes | Hard | 3 | 800 | 322 | 228 | 13 |
705 | A | 705A | A. Hulk | 800 | implementation | Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second... | The only line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of layers of love and hate. | Print Dr.Banner's feeling in one line. | Input: 1 | Output: I hate it | Beginner | 1 | 553 | 109 | 38 | 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.