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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
814 | D | 814D | D. An overnight dance in discotheque | 2,000 | dfs and similar; dp; geometry; greedy; trees | The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is ... | The first line of input contains a positive integer n (1 β€ n β€ 1 000) β the number of dancers.The following n lines each describes a dancer: the i-th line among them contains three space-separated integers xi, yi and ri ( - 106 β€ xi, yi β€ 106, 1 β€ ri β€ 106), describing a circular movement range centered at (xi, yi) wit... | Output one decimal number β the largest achievable sum of spaciousness over two halves of the night.The output is considered correct if it has a relative or absolute error of at most 10 - 9. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if . | The first sample corresponds to the illustrations in the legend. | Input: 52 1 60 4 12 -1 31 -2 14 -1 1 | Output: 138.23007676 | Hard | 5 | 1,557 | 332 | 289 | 8 |
1,503 | A | 1503A | A. Balance the Bits | 1,600 | constructive algorithms; greedy | A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.You are given a binary string \(s\) of length \(n\). Construct two balanced bracket sequ... | The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2\le n\le 2\cdot 10^5\), \(n\) is even).The next line contains a string \(s\) of length \(n\), consisting of characters 0 and 1.The sum of \(n\) across all t... | If such two balanced bracked sequences exist, output ""YES"" on the first line, otherwise output ""NO"". You can print each letter in any case (upper or lower).If the answer is ""YES"", output the balanced bracket sequences \(a\) and \(b\) satisfying the conditions on the next two lines.If there are multiple solutions,... | In the first test case, \(a=\)""()()()"" and \(b=\)""((()))"". The characters are equal in positions \(1\), \(3\), \(4\), and \(6\), which are the exact same positions where \(s_i=1\).In the second test case, \(a=\)""()()((()))"" and \(b=\)""(())()()()"". The characters are equal in positions \(1\), \(4\), \(5\), \(7\)... | Input: 3 6 101101 10 1001101101 4 1100 | Output: YES ()()() ((())) YES ()()((())) (())()()() NO | Medium | 2 | 506 | 362 | 339 | 15 |
1,840 | D | 1840D | D. Wooden Toy Festival | 1,400 | binary search; greedy; sortings | In a small town, there is a workshop specializing in woodwork. Since the town is small, only three carvers work there.Soon, a wooden toy festival is planned in the town. The workshop employees want to prepare for it.They know that \(n\) people will come to the workshop with a request to make a wooden toy. People are di... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follow the descriptions of the test cases.The first line of a test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of people who will come to the workshop.The second line of a te... | Output \(t\) numbers, each of which is the answer to the corresponding test case β the best maximum waiting time that the carvers can achieve. | In the first example, the carvers can choose patterns \(1\), \(7\), \(9\) for preparation.In the second example, the carvers can choose patterns \(3\), \(30\), \(60\) for preparation.In the third example, the carvers can choose patterns \(14\), \(50\), \(85\) for preparation. | Input: 561 7 7 9 9 965 4 2 1 30 60914 19 37 59 1 4 4 98 731263 10 1 17 15 11 | Output: 0 2 13 0 1 | Easy | 3 | 1,547 | 511 | 142 | 18 |
1,677 | A | 1677A | A. Tokitsukaze and Strange Inequality | 1,600 | brute force; data structures; dp | Tokitsukaze has a permutation \(p\) of length \(n\). Recall that a permutation \(p\) of length \(n\) is a sequence \(p_1, p_2, \ldots, p_n\) consisting of \(n\) distinct integers, each of which from \(1\) to \(n\) (\(1 \leq p_i \leq n\)).She wants to know how many different indices tuples \([a,b,c,d]\) (\(1 \leq a < b ... | The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Each test case consists of two lines.The first line contains a single integer \(n\) (\(4 \leq n \leq 5000\)) β the length of permutation \(p\).The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \l... | For each test case, print a single integer β the number of different \([a,b,c,d]\) tuples. | In the first test case, there are \(3\) different \([a,b,c,d]\) tuples.\(p_1 = 5\), \(p_2 = 3\), \(p_3 = 6\), \(p_4 = 1\), where \(p_1 < p_3\) and \(p_2 > p_4\) satisfies the inequality, so one of \([a,b,c,d]\) tuples is \([1,2,3,4]\).Similarly, other two tuples are \([1,2,3,6]\), \([2,3,5,6]\). | Input: 365 3 6 1 4 241 2 3 4105 1 6 2 8 3 4 10 9 7 | Output: 3 0 28 | Medium | 3 | 607 | 436 | 90 | 16 |
2,030 | F | 2030F | F. Orangutan Approved Subarrays | 2,400 | binary search; data structures; dp; greedy; implementation; two pointers | Suppose you have an array \(b\). Initially, you also have a set \(S\) that contains all distinct elements of \(b\). The array \(b\) is called orangutan-approved if it can be emptied by repeatedly performing the following operation: In one operation, select indices \(l\) and \(r\) (\(1 \leq l \leq r \leq |b|\)) such tha... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains integers \(n\) and \(q\) (\(1 \leq n,q \leq 2 \cdot 10^5\)) β the size of \(a\) and the number of queries, respectively.The following line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \l... | For each query, output ""YES"" (without quotes) if the subarray from \(l\) to \(r\) is orangutan-approved, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response). | In the first query of the first testcase, the answer is YES. Initially, \(S=\{1,2\}\) and \(b=[1,2,2,1]\) Select \(l=2\) and \(r=3\). Since \(b_2=b_3=2\) is in \(S\), we may erase \(b_2\) and \(b_3\) from the array, as well as erasing \(2\) from \(S\). The set \(S\) becomes \(\{1\}\) and the array becomes \([1,1]\). Se... | Input: 34 21 2 2 11 41 35 31 2 1 2 12 53 51 38 41 2 3 2 1 3 2 31 52 83 56 8 | Output: YES YES NO YES YES YES NO YES YES | Expert | 6 | 831 | 612 | 285 | 20 |
1,311 | C | 1311C | C. Perform the Combo | 1,300 | brute force | You want to perform the combo on your opponent in one popular fighting game. The combo is the string \(s\) consisting of \(n\) lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in \(s\). I.e. if \(s=\)""abca"" then you have to press 'a', then 'b', 'c' and 'a' again.Yo... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the length of \(s\) and the number of tries correspo... | For each test case, print the answer β \(26\) integers: the number of times you press the button 'a', the number of times you press the button 'b', \(\dots\), the number of times you press the button 'z'. | The first test case is described in the problem statement. Wrong tries are ""a"", ""abc"" and the final try is ""abca"". The number of times you press 'a' is \(4\), 'b' is \(2\) and 'c' is \(2\).In the second test case, there are five wrong tries: ""co"", ""codeforc"", ""cod"", ""co"", ""codeforce"" and the final try i... | Input: 3 4 2 abca 1 3 10 5 codeforces 2 8 3 2 9 26 10 qwertyuioplkjhgfdsazxcvbnm 20 10 1 2 3 5 10 5 9 4 | Output: 4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 | Easy | 1 | 1,208 | 838 | 204 | 13 |
685 | A | 685A | A. Robbers' watch | 1,700 | brute force; combinatorics; dp; math | Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours,... | The first line of the input contains two integers, given in the decimal notation, n and m (1 β€ n, m β€ 109) β the number of hours in one day and the number of minutes in one hour, respectively. | Print one integer in decimal notation β the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. | In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1). | Input: 2 3 | Output: 4 | Medium | 4 | 1,113 | 192 | 149 | 6 |
993 | B | 993B | B. Open Communication | 1,900 | bitmasks; brute force | Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.Both participants communicated to each other a set of... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 12\)) β the number of pairs the first participant communicated to the second and vice versa.The second line contains \(n\) pairs of integers, each between \(1\) and \(9\), β pairs of numbers communicated from first participant to the second.The thir... | If you can deduce the shared number with certainty, print that number.If you can with certainty deduce that both participants know the shared number, but you do not know it, print \(0\).Otherwise print \(-1\). | In the first example the first participant communicated pairs \((1,2)\) and \((3,4)\), and the second communicated \((1,5)\), \((3,4)\). Since we know that the actual pairs they received share exactly one number, it can't be that they both have \((3,4)\). Thus, the first participant has \((1,2)\) and the second has \((... | Input: 2 21 2 3 41 5 3 4 | Output: 1 | Hard | 2 | 599 | 825 | 209 | 9 |
621 | C | 621C | C. Wet Shark and Flowers | 1,700 | combinatorics; math; number theory; probabilities | There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from l... | The first line of the input contains two space-separated integers n and p (3 β€ n β€ 100 000, 2 β€ p β€ 109) β the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime.The i-th of the following n lines contains information about i-th shark β two space-separated integers li and ri (1 β€ l... | Print a single real number β the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct... | A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of fl... | Input: 3 21 2420 421420420 420421 | Output: 4500.0 | Medium | 4 | 667 | 461 | 326 | 6 |
1,356 | B2 | 1356B2 | B2. Decrement | 0 | *special | Implement a unitary operation on a register of \(N\) qubits that decrements the number written in the register modulo \(2^N\).Your operation should take a register of type LittleEndian - an array of qubits that encodes an unsigned integer in little-endian format, with the least significant bit written first (correspond... | Beginner | 1 | 1,447 | 0 | 0 | 13 | ||||
1,379 | A | 1379A | A. Acacius and String | 1,500 | brute force; implementation; strings | Acacius is studying strings theory. Today he came with the following problem.You are given a string \(s\) of length \(n\) consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string ""abacaba"" occurs as a substring in a... | First line of input contains an integer \(T\) (\(1 \leq T \leq 5000\)), number of test cases. \(T\) pairs of lines with test case descriptions follow.The first line of a test case description contains a single integer \(n\) (\(7 \leq n \leq 50\)), length of a string \(s\).The second line of a test case description cont... | For each test case output an answer for it.In case if there is no way to replace question marks in string \(s\) with a lowercase English letters in such a way that there is exactly one occurrence of a string ""abacaba"" in the resulting string as a substring output ""No"".Otherwise output ""Yes"" and in the next line o... | In first example there is exactly one occurrence of a string ""abacaba"" in the string ""abacaba"" as a substring.In second example seven question marks can be replaced with any seven lowercase English letters and with ""abacaba"" in particular.In sixth example there are two occurrences of a string ""abacaba"" as a sub... | Input: 6 7 abacaba 7 ??????? 11 aba?abacaba 11 abacaba?aba 15 asdf???f???qwer 11 abacabacaba | Output: Yes abacaba Yes abacaba Yes abadabacaba Yes abacabadaba No No | Medium | 3 | 1,269 | 413 | 609 | 13 |
1,459 | B | 1459B | B. Move and Turn | 1,300 | dp; math | A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly \(1\) meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, but then at the end of every second it has to turn 90 degrees... | The only line contains a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of steps the robot makes. | Print a single integer β the number of different possible locations after exactly \(n\) steps. | In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction.In the second sample case, the robot will always end up \(\sqrt{2}\) meters north-west, north-east, south-west, or south-east. | Input: 1 | Output: 4 | Easy | 2 | 714 | 109 | 94 | 14 |
1,002 | A4 | 1002A4 | A4. Generate W state | 1,900 | *special | You are given N = 2k qubits (0 β€ k β€ 4) in zero state . Your task is to create a generalized W state on them. Generalized W state is an equal superposition of all basis states on N qubits that have Hamming weight equal to 1:For example, for N = 1, .You have to implement an operation which takes an array of N qubits as ... | Hard | 1 | 621 | 0 | 0 | 10 | ||||
1,904 | C | 1904C | C. Array Game | 1,400 | binary search; brute force; data structures; sortings; two pointers | You are given an array \(a\) of \(n\) positive integers. In one operation, you must pick some \((i, j)\) such that \(1\leq i < j\leq |a|\) and append \(|a_i - a_j|\) to the end of the \(a\) (i.e. increase \(n\) by \(1\) and set \(a_n\) to \(|a_i - a_j|\)). Your task is to minimize and print the minimum value of \(a\) a... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2\le n\le 2\cdot 10^3\), \(1\le k\le 10^9\)) β the length of the array... | For each test case, print a single integer β the smallest possible value of the minimum of array \(a\) after performing \(k\) operations. | In the first test case, after any \(k=2\) operations, the minimum value of \(a\) will be \(1\).In the second test case, an optimal strategy is to first pick \(i=1, j=2\) and append \(|a_1 - a_2| = 3\) to the end of \(a\), creating \(a=[7, 4, 15, 12, 3]\). Then, pick \(i=3, j=4\) and append \(|a_3 - a_4| = 3\) to the en... | Input: 45 23 9 7 15 14 37 4 15 126 242 47 50 54 62 792 1500000000000000000 1000000000000000000 | Output: 1 0 3 500000000000000000 | Easy | 5 | 353 | 606 | 137 | 19 |
1,711 | B | 1711B | B. Party | 1,300 | brute force; graphs | A club plans to hold a party and will invite some of its \(n\) members. The \(n\) members are identified by the numbers \(1, 2, \dots, n\). If member \(i\) is not invited, the party will gain an unhappiness value of \(a_i\).There are \(m\) pairs of friends among the \(n\) members. As per tradition, if both people from ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq \min(10^5,\frac{n(n-1)}{2})\)) β the number o... | For each test case, print a line containing a single integer β the minimum possible unhappiness value of a valid party. | In the first test case, all members can be invited. So the unhappiness value is \(0\).In the second test case, the following options are possible: invite \(1\) and \(2\) (\(0\) cakes eaten, unhappiness value equal to \(3\)); invite \(2\) and \(3\) (\(0\) cakes eaten, unhappiness value equal to \(2\)); invite only \(1\)... | Input: 41 013 12 1 31 35 51 2 3 4 51 21 31 41 52 35 51 1 1 1 11 22 33 44 55 1 | Output: 0 2 3 2 | Easy | 2 | 779 | 821 | 119 | 17 |
1,807 | E | 1807E | E. Interview | 1,300 | binary search; implementation; interactive | This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants.Before the last stage of the exam, the director conducted an interview. He gave Gon \(n\) piles of stones, the \(i\)-th pile having \(a_i\) stones.Each stone is identical and weighs... | The input data contains several test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of piles.The second line of ... | In the first test case, the stone with weight two is located in pile \(2\), as shown in the picture. We perform the following interaction: \(\texttt{? 4 1 2 3 4}\) β ask the total weight of piles \(1\), \(2\), \(3\), and \(4\). The total weight we receive back is \(1+3+3+4=11\). \(\texttt{? 2 2 3}\) β ask the total wei... | Input: 2 5 1 2 3 4 5 11 6 3 7 1 2 3 5 3 4 2 12 6 | Output: ? 4 1 2 3 4 ? 2 2 3 ? 1 2 ! 2 ? 4 2 3 5 6 ? 2 1 4 ! 7 | Easy | 3 | 1,127 | 606 | 0 | 18 | |
1,946 | B | 1946B | B. Maximum Sum | 1,100 | dp; greedy; math | You have an array \(a\) of \(n\) integers.You perform exactly \(k\) operations on it. In one operation, you select any contiguous subarray of the array \(a\) (possibly empty) and insert the sum of this subarray anywhere in the array.Your task is to find the maximum possible sum of the array after \(k\) such operations.... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k \le 2 \cdot 10^5\)) β the length of the array \(... | For each test, output a single integer β the maximum sum of the array that can be obtained after \(k\) operations modulo \(10^9 + 7\). | In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be \((-4) + (-7) + 0 + 0 = -11\), modulo \(10^9 + 7\) this is \(999\,999\,996\).In the second test case, it is advantageous to take t... | Input: 122 2-4 -73 32 2 81 775 14 -2 8 -12 97 48 14 -9 6 0 -1 37 1005 3 -8 12 -5 -9 36 1000-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -10000000002 11000000000 85 40 0 0 0 06 1048973 757292 58277 -38574 27475 9999847 1-1000 1000 -1000 1000 -1000 1000 -100010 10050408293874 -3498597 7374783 295774930 -48... | Easy | 3 | 548 | 620 | 134 | 19 |
1,490 | F | 1490F | F. Equalize the Array | 1,500 | binary search; data structures; greedy; math; sortings | Polycarp was gifted an array \(a\) of length \(n\). Polycarp considers an array beautiful if there exists a number \(C\), such that each number in the array occurs either zero or \(C\) times. Polycarp wants to remove some elements from the array \(a\) to make it beautiful.For example, if \(n=6\) and \(a = [1, 3, 2, 1, ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \l... | For each test case, output one integer β the minimum number of elements that Polycarp has to remove from the array \(a\) to make it beautiful. | Input: 3 6 1 3 2 1 4 2 4 100 100 4 100 8 1 2 3 3 3 2 6 6 | Output: 2 1 2 | Medium | 5 | 826 | 463 | 142 | 14 | |
1,970 | A2 | 1970A2 | A2. Balanced Unshuffle (Medium) | 2,400 | brute force; constructive algorithms; trees | The differences with the easy version of this problem are highlighted in bold.A parentheses sequence is a string consisting of characters ""("" and "")"", for example ""(()(("".A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operatio... | The only line of input contains a string \(s\) consisting only of characters ""("" and "")"". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding \(1\,000\). | Print the balanced parentheses sequence \(t\) such that the balanced shuffle of \(t\) is equal to \(s\). It is guaranteed that the answer always exists and is unique. | Input: ()(()()) | Output: (()(())) | Expert | 3 | 2,184 | 209 | 166 | 19 | |
1,801 | B | 1801B | B. Buying gifts | 1,800 | data structures; greedy; sortings | Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city.There are \(n\) departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from \(1\) to \(n\). It is known... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1\,000\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 500\,000\)) β the number of departments in the mall.Each of the following \(n... | Print one integer β the minimum price difference of the most expensive gifts bought to friends. | In the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend β in the second department, or vice versa. In the first case, \(m_1 = m_2 = 1\), and in the second case β \(m_1 = m_2 = 2\). In both cases, the answer is \(0\). In the second test case,... | Input: 221 22 151 52 73 34 102 5 | Output: 0 1 | Medium | 3 | 1,303 | 597 | 95 | 18 |
605 | E | 605E | E. Intergalaxy Trips | 2,700 | probabilities; shortest paths | The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems. The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you ne... | The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is ... | Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. 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 p... | In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is . | Input: 3100 50 500 100 800 0 100 | Output: 1.750000000000000 | Master | 2 | 1,174 | 391 | 366 | 6 |
1,101 | F | 1101F | F. Trucks and Cities | 2,400 | binary search; dp | There are \(n\) cities along the road, which can be represented as a straight line. The \(i\)-th city is situated at the distance of \(a_i\) kilometers from the origin. All cities are situated in the same direction from the origin. There are \(m\) trucks travelling from one city to another. Each truck can be described ... | First line contains two integers \(n\) and \(m\) (\(2 \le n \le 400\), \(1 \le m \le 250000\)) β the number of cities and trucks.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\), \(a_i < a_{i+1}\)) β positions of cities in the ascending order.Next \(m\) lines contains \(4\) integ... | Print the only integer β minimum possible size of gas-tanks \(V\) such that all trucks can reach their destinations. | Let's look at queries in details: the \(1\)-st truck must arrive at position \(7\) from \(2\) without refuelling, so it needs gas-tank of volume at least \(50\). the \(2\)-nd truck must arrive at position \(17\) from \(2\) and can be refueled at any city (if it is on the path between starting point and ending point), s... | Input: 7 6 2 5 7 10 14 15 17 1 3 10 0 1 7 12 7 4 5 13 3 4 7 10 1 4 7 10 1 1 5 11 2 | Output: 55 | Expert | 2 | 967 | 514 | 116 | 11 |
691 | B | 691B | B. s-palindrome | 1,600 | implementation; strings | Let's call a string ""s-palindrome"" if it is symmetric about the middle of the string. For example, the string ""oHo"" is ""s-palindrome"", but the string ""aa"" is not. The string ""aa"" is not ""s-palindrome"", because the second half of it is not a mirror reflection of the first half. English alphabet You are given... | The only line contains the string s (1 β€ |s| β€ 1000) which consists of only English letters. | Print ""TAK"" if the string s is ""s-palindrome"" and ""NIE"" otherwise. | Input: oXoxoXo | Output: TAK | Medium | 2 | 373 | 92 | 72 | 6 | |
711 | D | 711D | D. Directed Roads | 1,900 | combinatorics; dfs and similar; graphs; math | ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai β i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it go... | The first line of the input contains single integer n (2 β€ n β€ 2Β·105) β the number of towns in Udayland.The next line contains n integers a1, a2, ..., an (1 β€ ai β€ n, ai β i), ai denotes a road going from town i to town ai. | Print a single integer β the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7. | Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are , , initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invali... | Input: 32 3 1 | Output: 6 | Hard | 4 | 1,160 | 223 | 205 | 7 |
1,998 | D | 1998D | D. Determine Winning Islands in Race | 2,100 | data structures; dp; graphs; greedy; shortest paths | MOOOOOOOOOOOOOOOOOβ Bessie the Cow, The Art of Racing on IslandsTwo of Farmer John's cows, Bessie and Elsie, are planning to race on \(n\) islands. There are \(n - 1\) main bridges, connecting island \(i\) to island \(i + 1\) for all \(1 \leq i \leq n - 1\). Additionally, there are \(m\) alternative bridges. Elsie can ... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq m \leq 2 \cdot 10^5\)) β the number of islands and the number of alternative bridges.The next \(m\) lines of each test case contain \(u\) ... | For each test case, output a binary string of length \(n - 1\) on a new line. The \(i\)'th character is \(1\) if it is possible for Bessie to win if she starts on island \(i\). Otherwise, it is \(0\). | In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island \(n\) first, so Bessie will win on all islands because she always moves first.In the second case, Bessie will lose if she starts on island \(3\) because: Bessie's Turn: Take a main bridge from island \(3\) to island \... | Input: 56 06 12 66 11 510 41 31 62 73 815 32 84 98 15 | Output: 11111 11011 10011 100001111 11000111000111 | Hard | 5 | 1,753 | 623 | 200 | 19 |
1,916 | A | 1916A | A. 2023 | 800 | constructive algorithms; implementation; math; number theory | In a sequence \(a\), whose product was equal to \(2023\), \(k\) numbers were removed, leaving a sequence \(b\) of length \(n\). Given the resulting sequence \(b\), find any suitable sequence \(a\) and output which \(k\) elements were removed from it, or state that such a sequence could not have existed.Notice that you ... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. This is followed by a description of the test cases.The first line of each test case contains two integers \(n\) (\(1 \le n, k \le 5\)) β the size of sequence \(b\) and the number o... | For each test case, output ""YES"" if the sequence \(a\) exists, and in the following line output \(k\) non-negative integers that were removed from the sequence \(a\). If the sequence \(a\) does not exist, output ""NO"" in a single line.You can output the answer in any case (uppercase or lowercase). For example, the s... | In third test case product is equal to \(289 \cdot 7 = 2023\).In fourth test case product is already equal to \(2023\).In seventh test case product is equal to \(7 \cdot 17 \cdot 17 = 2023\). | Input: 72 25 23 17 17 74 21 289 1 13 17 17 171 12891 120231 31 | Output: NO NO YES 7 1 YES 1 YES 7 YES 1 YES 7 17 17 | Beginner | 4 | 362 | 531 | 405 | 19 |
509 | A | 509A | A. Maximum in Table | 800 | brute force; implementation | An n Γ n table a is defined as follows: The first row and the first column contain ones, that is: ai, 1 = a1, i = 1 for all i = 1, 2, ..., n. Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the ... | The only line of input contains a positive integer n (1 β€ n β€ 10) β the number of rows and columns of the table. | Print a single line containing a positive integer m β the maximum value in the table. | In the second test the rows of the table look as follows: {1, 1, 1, 1, 1}, {1, 2, 3, 4, 5}, {1, 3, 6, 10, 15}, {1, 4, 10, 20, 35}, {1, 5, 15, 35, 70}. | Input: 1 | Output: 1 | Beginner | 2 | 523 | 112 | 85 | 5 |
1,029 | C | 1029C | C. Maximal Intersection | 1,600 | greedy; math; sortings | You are given \(n\) segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.The intersection of a sequence of segments is such a maximal set of points (not necesserily havi... | The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of segments in the sequence.Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) (\(0 \le l_i \le r_i \le 10^9\)) β the description of the \(i\)-th segment. | Print a single integer β the maximal possible length of the intersection of \((n - 1)\) remaining segments after you remove exactly one segment from the sequence. | In the first example you should remove the segment \([3;3]\), the intersection will become \([2;3]\) (length \(1\)). Removing any other segment will result in the intersection \([3;3]\) (length \(0\)).In the second example you should remove the segment \([1;3]\) or segment \([2;6]\), the intersection will become \([2;4... | Input: 41 32 60 43 3 | Output: 1 | Medium | 3 | 1,043 | 265 | 162 | 10 |
666 | E | 666E | E. Forensic Examination | 3,100 | data structures; string suffix structures | The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.Ber... | The first line contains the string s (1 β€ |s| β€ 5Β·105) β the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters.The second line contains the only integer m (1 β€ m β€ 5Β·104) β the number of texts of leaflets for expertise.Each of the next m lines contains the only string ti β the t... | Print q lines. The i-th of them should contain two integers β the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one. | Input: suffixtree3suffixtreesareawesomecartesiantreeisworsethansegmenttreenyeeheeheee21 2 1 101 3 9 10 | Output: 1 13 4 | Master | 2 | 864 | 733 | 239 | 6 | |
1,468 | K | 1468K | K. The Robot | 1,600 | brute force; implementation | There is a robot on a checkered field that is endless in all directions. Initially, the robot is located in the cell with coordinates \((0, 0)\). He will execute commands which are described by a string of capital Latin letters 'L', 'R', 'D', 'U'. When a command is executed, the robot simply moves in the corresponding ... | The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.Each test case consists of a single line containing \(s\) β the sequence of commands, which are uppercase Latin letters 'L', 'R', 'D', 'U' only. The length of \(s\) is between \(1\) and \(5000\), inclusive. Additional constraint ... | For each test case print a single line: if there is a solution, print two integers \(x\) and \(y\) (\(-10^9 \le x,y \le 10^9\)) such that an obstacle in \((x, y)\) will force the robot to return back to the cell \((0, 0)\); otherwise, print two zeroes (i. e. 0 0). If there are multiple answers, you can print any of the... | Input: 4LRUUDLLLUUDDDUUUUU | Output: -1 0 1 2 0 0 0 1 | Medium | 2 | 1,459 | 510 | 322 | 14 | |
450 | B | 450B | B. Jzzhu and Sequences | 1,300 | implementation; math | Jzzhu has invented a kind of sequences, they meet the following property:You are given x and y, please calculate fn modulo 1000000007 (109 + 7). | The first line contains two integers x and y (|x|, |y| β€ 109). The second line contains a single integer n (1 β€ n β€ 2Β·109). | Output a single integer representing fn modulo 1000000007 (109 + 7). | In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6). | Input: 2 33 | Output: 1 | Easy | 2 | 144 | 123 | 68 | 4 |
1,984 | A | 1984A | A. Strange Splitting | 800 | constructive algorithms | Define the range of a non-empty array to be the maximum value minus the minimum value. For example, the range of \([1,4,2]\) is \(4-1=3\).You are given an array \(a_1, a_2, \ldots, a_n\) of length \(n \geq 3\). It is guaranteed \(a\) is sorted.You have to color each element of \(a\) red or blue so that: the range of th... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 50\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)). It... | For each test case, if it is impossible to color \(a\) to satisfy all the constraints, output \(\texttt{NO}\).Otherwise, first output \(\texttt{YES}\).Then, output a string \(s\) of length \(n\). For \(1 \leq i \leq n\), if you color \(a_i\) red, \(s_i\) should be \(\texttt{R}\). For \(1 \leq i \leq n\), if you color \... | In the first test case, given the array \([1, 1, 2, 2]\), we can color the second element blue and the remaining elements red; then the range of the red elements \([1, 2, 2]\) is \(2-1=1\), and the range of the blue elements \([1]\) is \(1-1=0\).In the second test case, we can color the first, second, fourth and fifth ... | Input: 741 1 2 251 2 3 4 533 3 341 2 2 231 2 231 1 231 9 84 | Output: YES RBRR YES BBRBB NO YES RBBR YES RRB YES BRR YES BRB | Beginner | 1 | 561 | 390 | 366 | 19 |
986 | A | 986A | A. Fair | 1,600 | graphs; greedy; number theory; shortest paths | Some company is going to hold a fair in Byteland. There are \(n\) towns in Byteland and \(m\) two-way roads between towns. Of course, you can reach any town from any other town using roads.There are \(k\) types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at leas... | There are \(4\) integers \(n\), \(m\), \(k\), \(s\) in the first line of input (\(1 \le n \le 10^{5}\), \(0 \le m \le 10^{5}\), \(1 \le s \le k \le min(n, 100)\)) β the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.In the ne... | Print \(n\) numbers, the \(i\)-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town \(i\). Separate numbers with spaces. | Let's look at the first sample.To hold a fair in town \(1\) you can bring goods from towns \(1\) (\(0\) coins), \(2\) (\(1\) coin) and \(4\) (\(1\) coin). Total numbers of coins is \(2\).Town \(2\): Goods from towns \(2\) (\(0\)), \(1\) (\(1\)), \(3\) (\(1\)). Sum equals \(2\).Town \(3\): Goods from towns \(3\) (\(0\))... | Input: 5 5 4 31 2 4 3 21 22 33 44 14 5 | Output: 2 2 2 2 3 | Medium | 4 | 730 | 895 | 169 | 9 |
2,003 | C | 2003C | C. Turtle and Good Pairs | 1,200 | constructive algorithms; greedy; sortings; strings | Turtle gives you a string \(s\), consisting of lowercase Latin letters.Turtle considers a pair of integers \((i, j)\) (\(1 \le i < j \le n\)) to be a pleasant pair if and only if there exists an integer \(k\) such that \(i \le k < j\) and both of the following two conditions hold: \(s_k \ne s_{k + 1}\); \(s_k \ne s_i\)... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the string.The second line of each test case co... | For each test case, output the string \(s\) after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them. | In the first test case, \((1, 3)\) is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than \(1\). bac and cab can also be the answer.In the second test case, \((1, 2)\), \((1, 4)\), \((1, 5)\), \((2, 4)\), \((2, 5)\), \((3, 5)\) are good p... | Input: 53abc5edddf6turtle8pppppppp10codeforces | Output: acb ddedf urtlet pppppppp codeforces | Easy | 4 | 611 | 489 | 157 | 20 |
2,060 | C | 2060C | C. Game of Mathletes | 900 | games; greedy; sortings; two pointers | Alice and Bob are playing a game. There are \(n\) (\(n\) is even) integers written on a blackboard, represented by \(x_1, x_2, \ldots, x_n\). There is also a given integer \(k\) and an integer score that is initially \(0\). The game lasts for \(\frac{n}{2}\) turns, in which the following events happen sequentially: Ali... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2\cdot 10^5, 1 \leq k \leq 2\cdot n\), \(n\) is even).The second line of each test case contains \(n\) integers \(x_1,x_2,\ldots,x_n\) (\(... | For each test case, output the score if both players play optimally. | In the first test case, one way the game may go is as follows: Alice selects \(1\) and Bob selects \(3\). The score increases as \(1+3=4\). Now the two integers remaining on the blackboard are \(2\) and \(2\). Alice and Bob both select \(2\). The score increases as \(2+2=4\). The game ends as the blackboard now has no ... | Input: 44 41 2 3 28 151 2 3 4 5 6 7 86 11 1 1 1 1 116 93 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 | Output: 2 1 0 4 | Beginner | 4 | 716 | 465 | 68 | 20 |
848 | B | 848B | B. Rooter's Song | 1,900 | constructive algorithms; data structures; geometry; implementation; sortings; two pointers | Wherever the destination is, whoever we meet, let's render this song together.On a Cartesian coordinate plane lies a rectangular stage of size w Γ h, represented by a rectangle with corners (0, 0), (w, 0), (w, h) and (0, h). It can be seen that no collisions will happen before one enters the stage.On the sides of the s... | The first line of input contains three space-separated positive integers n, w and h (1 β€ n β€ 100 000, 2 β€ w, h β€ 100 000) β the number of dancers and the width and height of the stage, respectively.The following n lines each describes a dancer: the i-th among them contains three space-separated integers gi, pi, and ti ... | Output n lines, the i-th of which contains two space-separated integers (xi, yi) β the stopping position of the i-th dancer in the input. | The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure. In the second example, no dancers collide. | Input: 8 10 81 1 101 4 131 7 11 8 22 2 02 5 142 6 02 6 1 | Output: 4 810 58 810 610 21 87 810 6 | Hard | 6 | 1,099 | 670 | 137 | 8 |
27 | C | 27C | C. Unordered Subsequence | 1,900 | constructive algorithms; greedy | The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.A subsequence is a sequence that can be d... | The first line of the input contains one integer n (1 β€ n β€ 105). The second line contains n space-separated integers β the given sequence. All numbers in this sequence do not exceed 106 by absolute value. | If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] β indexes of the elements of this subsequence. If there are several solutions, output any of them. | Input: 567 499 600 42 23 | Output: 31 3 5 | Hard | 2 | 438 | 205 | 285 | 0 | |
1,513 | B | 1513B | B. AND Sequences | 1,400 | bitmasks; combinatorics; constructive algorithms; math | A sequence of \(n\) non-negative integers (\(n \ge 2\)) \(a_1, a_2, \dots, a_n\) is called good if for all \(i\) from \(1\) to \(n-1\) the following condition holds true: $$$\(a_1 \: \& \: a_2 \: \& \: \dots \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: \dots \: \& \: a_n,\)\( where \)\&\( denotes the bitwise AND op... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)), denoting the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le ... | Output \(t\) lines, where the \(i\)-th line contains the number of good permutations in the \(i\)-th test case modulo \(10^9 + 7\). | In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of \(6\) permutations possible with numbers from \(1\) to \(3\): \([1,2,3]\), \([1,3,2]\), \([2,1,3]\), \([2,3,1]\), \([3,1,2]\), \([3,2,1]\).In the second test case, it can be proved that no p... | Input: 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 | Output: 6 0 36 4 | Easy | 4 | 601 | 455 | 131 | 15 |
852 | I | 852I | I. Dating | 2,300 | brute force; dfs and similar; graphs; trees | This story is happening in a town named BubbleLand. There are n houses in BubbleLand. In each of these n houses lives a boy or a girl. People there really love numbers and everyone has their favorite number f. That means that the boy or girl that lives in the i-th house has favorite number equal to fi.The houses are nu... | The first line contains an integer n (1 β€ n β€ 105), the number of houses in the town.The second line contains n integers, where the i-th number is 1 if a boy lives in the i-th house or 0 if a girl lives in i-th house.The third line contains n integers, where the i-th number represents the favorite number fi (1 β€ fi β€ 1... | For each of the q questions output a single number, the answer to the citizens question. | In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3).In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5). | Input: 71 0 0 1 0 1 09 2 9 2 2 9 92 61 24 26 53 67 421 37 5 | Output: 23 | Expert | 4 | 976 | 809 | 88 | 8 |
768 | F | 768F | F. Barrels and boxes | 2,300 | brute force; combinatorics; math; number theory; probabilities | Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two s... | The first line of input contains three integers f, w, h (0 β€ f, w, h β€ 105) β number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel. | Output the probability that Jon Snow will like the arrangement. The probability is of the form , then you need to output a single integer pΒ·q - 1 mod (109 + 7). | In the first example f = 1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them.In the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is . | Input: 1 1 1 | Output: 0 | Expert | 5 | 916 | 231 | 160 | 7 |
1,446 | A | 1446A | A. Knapsack | 1,300 | constructive algorithms; greedy; sortings | You have a knapsack with the capacity of \(W\). There are also \(n\) items, the \(i\)-th one has weight \(w_i\). You want to put some of these items into the knapsack in such a way that their total weight \(C\) is at least half of its size, but (obviously) does not exceed it. Formally, \(C\) should satisfy: \(\lceil \f... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains integers \(n\) and \(W\) (\(1 \le n \le 200\,000\), \(1\le W \le 10^{18}\)). The second line of each test case contains \... | For each test case, if there is no solution, print a single integer \(-1\). If there exists a solution consisting of \(m\) items, print \(m\) in the first line of the output and \(m\) integers \(j_1\), \(j_2\), ..., \(j_m\) (\(1 \le j_i \le n\), all \(j_i\) are distinct) in the second line of the output β indices of th... | In the first test case, you can take the item of weight \(3\) and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is \(-1\).In the third test case, you fill the knapsack exactly in half. | Input: 3 1 3 3 6 2 19 8 19 69 9 4 7 12 1 1 1 17 1 1 1 | Output: 1 1 -1 6 1 2 3 5 6 7 | Easy | 3 | 639 | 473 | 539 | 14 |
1,891 | E | 1891E | E. Brukhovich and Exams | 2,500 | brute force; greedy; implementation; math; sortings | The boy Smilo is learning algorithms with a teacher named Brukhovich.Over the course of the year, Brukhovich will administer \(n\) exams. For each exam, its difficulty \(a_i\) is known, which is a non-negative integer.Smilo doesn't like when the greatest common divisor of the difficulties of two consecutive exams is eq... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)) β the total number of exams and the maximum number of exams that can be sim... | For each test case, output the minimum possible sadness that can be achieved by performing no more than \(k\) operations. | In the first test case, a sadness of \(1\) can be achieved. To this, you can simplify the second and fourth exams. After this, there will be only one pair of adjacent exams with a greatest common divisor (GCD) equal to one, which is the first and second exams.In the second test case, a sadness of \(0\) can be achieved ... | Input: 95 21 3 5 7 95 23 5 7 9 118 217 15 10 1 1 5 14 85 31 1 1 1 15 51 1 1 1 119 71 1 2 3 4 5 5 6 6 7 8 9 10 1 1 1 2 3 115 62 1 1 1 1 2 1 1 2 1 1 1 2 1 25 21 1 1 1 25 21 0 1 0 1 | Output: 1 0 2 2 0 5 5 2 1 | Expert | 5 | 1,276 | 616 | 121 | 18 |
1,562 | B | 1562B | B. Scenes From a Memory | 1,000 | brute force; constructive algorithms; implementation; math; number theory | During the hypnosis session, Nicholas suddenly remembered a positive integer \(n\), which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or eq... | Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer \(k\) (\(1 \le k \le 50\)) β the number of digits in the number.The se... | For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions. If there are multiple solutions, print any. | In the first test case, you can't delete \(2\) digits from the number \(237\), as all the numbers \(2\), \(3\), and \(7\) are prime. However, you can delete \(1\) digit, obtaining a number \(27 = 3^3\).In the second test case, you can delete all digits except one, as \(4 = 2^2\) is a composite number. | Input: 7 3 237 5 44444 3 221 2 35 3 773 1 4 30 626221626221626221626221626221 | Output: 2 27 1 4 1 1 2 35 2 77 1 4 1 6 | Beginner | 5 | 867 | 652 | 238 | 15 |
448 | E | 448E | E. Divisors | 2,200 | brute force; dfs and similar; implementation; number theory | Bizon the Champion isn't just friendly, he also is a rigorous coder.Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of seq... | A single line contains two space-separated integers β X (1 β€ X β€ 1012) and k (0 β€ k β€ 1018). | Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements. | Input: 6 1 | Output: 1 2 3 6 | Hard | 4 | 743 | 92 | 156 | 4 | |
1,958 | B | 1958B | B. Clock in the Pool | 1,400 | *special; math | You are swimming in the pool, and you need to control the time of swimming.The pool has a clock that cycles between three different modes: showing water temperature, showing air temperature, and showing time. At the start of the \(0\)-th second it starts showing water temperature, at the start of the \(k\)-th second it... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains two integers \(k\) and \(m\) (\(1 \le k \le 10^8; 1 \le m \le 10^9\)) β the length of the period of the clock and the moment of time you check the ... | For each test case, print a single integer β the time you have to wait from the moment \(m\) until the moment the clock starts showing the time.You can assume that you are able to read the time instantly the moment it displays on the clock. | In the first test case, the clock will start showing time during the \(2\)-nd second, so you have to wait \(2 - 1 = 1\) second.In the second test case, the clock shows time from the \(10\)-th until the \(15\)-th second, so during the \(14\)-th second it shows time, and you don't have to wait.In the third test case, dur... | Input: 51 15 145 1510 11099999999 1000000000 | Output: 1 0 10 0 99999989 | Easy | 2 | 798 | 326 | 240 | 19 |
1,080 | A | 1080A | A. Petya and Origami | 800 | math | Petya is having a party soon, and he has decided to invite his \(n\) friends.He wants to make invitations in the form of origami. For each invitation, he needs two red sheets, five green sheets, and eight blue sheets. The store sells an infinite number of notebooks of each color, but each notebook consists of only one ... | The first line contains two integers \(n\) and \(k\) (\(1\leq n, k\leq 10^8\)) β the number of Petya's friends and the number of sheets in each notebook respectively. | Print one number β the minimum number of notebooks that Petya needs to buy. | In the first example, we need \(2\) red notebooks, \(3\) green notebooks, and \(5\) blue notebooks.In the second example, we need \(5\) red notebooks, \(13\) green notebooks, and \(20\) blue notebooks. | Input: 3 5 | Output: 10 | Beginner | 1 | 516 | 166 | 75 | 10 |
1,776 | N | 1776N | N. Count Permutations | 3,500 | math | You are given a string \(s\) with length \(n-1\) whose characters are either \(\texttt{<}\) or \(\texttt{>}\).Count the permutations \(p_1, \, p_2, \, \dots, \, p_n\) of \(1, \, 2, \, \dots, \, n\) such that, for all \(i = 1, \, 2, \, \dots, \, n - 1\), if \(s_i\) is \(\texttt{<}\) then \(p_i < p_{i+1}\) and if \(s_i\)... | The first line contains a single integer \(n\) (\(2 \le n \le 100\,000\)).The second line contains a string \(s\) of length \(n-1\); each character of \(s\) is either \(\texttt{<}\) or \(\texttt{>}\). | Print the logarithm in base \(2\) of the number of permutations satisfying the constraints described in the statement.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\). Formally, let your answer be \(x\) and let the correct answer be \(y\). Your answer is accepted if and on... | In the first sample, there is only one valid permutation, that is \([2, 1]\). Since \(\log_2(1)=0\), the correct output is \(0\).In the second sample, there are \(2\) valid permutations, that are \([3, 1, 2]\) and \([2, 1, 3]\). Since \(\log_2(2)=1\), the correct output is \(1\).In the third sample, there are \(4\) val... | Input: 2 < | Output: 0.0000000000 | Master | 1 | 435 | 200 | 373 | 17 |
1,089 | I | 1089I | I. Interval-Free Permutations | 2,600 | combinatorics | Consider a permutation \(p_1, p_2, \dots p_n\) of integers from 1 to \(n\). We call a sub-segment \(p_l, p_{l+1}, \dots, p_{r-1}, p_{r}\) of the permutation an interval if it is a reordering of some set of consecutive integers. For example, the permutation \((6,7,1,8,5,3,2,4)\) has the intervals \((6,7)\), \((5,3,2,4)\... | In the first line of the input there are two integers \(t\) (\(1 \le t \le 400\)) and \(p\) (\(10^8 \le p \le 10^9\)) β the number of test cases to solve and the prime modulo. In each of the next \(t\) lines there is one integer \(n\) (\(1 \le n \le 400\)) β the length of the permutation. | For each of \(t\) test cases print a single integer β the number of interval-free permutations modulo \(p\). | For \(n = 1\) the only permutation is interval-free. For \(n = 4\) two interval-free permutations are \((2,4,1,3)\) and \((3,1,4,2)\). For \(n = 5\) β \((2,4,1,5,3)\), \((2,5,3,1,4)\), \((3,1,5,2,4)\), \((3,5,1,4,2)\), \((4,1,3,5,2)\), and \((4,2,5,1,3)\). We will not list all 28146 for \(n = 9\), but for example \((4,... | Input: 4 998244353 1 4 5 9 | Output: 1 2 6 28146 | Expert | 1 | 741 | 289 | 108 | 10 |
559 | B | 559B | B. Equivalent Strings | 1,700 | divide and conquer; hashing; sortings; strings | Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of... | The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length. | Print ""YES"" (without the quotes), if these two strings are equivalent, and ""NO"" (without the quotes) otherwise. | In the first sample you should split the first string into strings ""aa"" and ""ba"", the second one β into strings ""ab"" and ""aa"". ""aa"" is equivalent to ""aa""; ""ab"" is equivalent to ""ba"" as ""ab"" = ""a"" + ""b"", ""ba"" = ""b"" + ""a"".In the second sample the first string can be splitted into strings ""aa"... | Input: aabaabaa | Output: YES | Medium | 4 | 620 | 197 | 115 | 5 |
1,777 | E | 1777E | E. Edge Reverse | 2,200 | binary search; dfs and similar; graphs; trees | You will be given a weighted directed graph of \(n\) nodes and \(m\) directed edges, where the \(i\)-th edge has a weight of \(w_i\) (\(1 \le i \le m\)).You need to reverse some edges of this graph so that there is at least one node in the graph from which every other node is reachable. The cost of these reversals is e... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.Each test case begins with a line containing two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the number of nodes in... | For each test case, output the minimum cost. If there is no solution, print \(-1\). | In the first test case, an edge exists from \(1\) to \(2\), so all nodes are reachable (from \(1\)).In the second test case, no nodes are reachable from any node no matter what edges we reverse, so the answer is \(-1\).In the third test case, reversing the \(4\)-th or \(5\)-th edge allows all nodes to be reachable from... | Input: 42 11 2 35 41 2 102 3 103 1 104 5 104 51 2 100002 3 200001 3 300004 2 5004 3 204 51 2 100002 3 200001 3 300004 2 54 3 20 | Output: 0 -1 20 5 | Hard | 4 | 602 | 810 | 83 | 17 |
580 | D | 580D | D. Kefa and Dishes | 1,800 | bitmasks; dp | When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the i-th dish gives him ai units of satisfactio... | The first line of the input contains three space-separated numbers, n, m and k (1 β€ m β€ n β€ 18, 0 β€ k β€ n * (n - 1)) β the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.The second line contains n space-separated numbers ai, (0 β€ ai β€ 109) β the satisfa... | In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant. | In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get ... | Input: 2 2 11 12 1 1 | Output: 3 | Medium | 2 | 728 | 689 | 111 | 5 |
364 | E | 364E | E. Empty Rectangles | 3,000 | divide and conquer; two pointers | You've got an n Γ m table (n rows and m columns), each cell of the table contains a ""0"" or a ""1"".Your task is to calculate the number of rectangles with the sides that are parallel to the sides of the table and go along the cell borders, such that the number one occurs exactly k times in the rectangle. | The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 2500, 0 β€ k β€ 6) β the sizes of the table and the required number of numbers one.Next n lines each contains m characters ""0"" or ""1"". The i-th character of the j-th line corresponds to the character that is in the j-th row and the i-th col... | Print a single number β the number of rectangles that contain exactly k numbers one.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Input: 3 3 2101000101 | Output: 8 | Master | 2 | 307 | 337 | 233 | 3 | |
545 | B | 545B | B. Equidistant String | 1,100 | greedy | Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:We will define the distance between two strings s and t of the same length consisting of digits zero and one as the numbe... | The first line contains string s of length n. The second line contains string t of length n.The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one. | Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line ""impossible"" (without the quotes).If there are multiple possible answers, print any of them. | In the first sample different answers are possible, namely β 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101. | Input: 00011011 | Output: 0011 | Easy | 1 | 661 | 214 | 239 | 5 |
1,157 | G | 1157G | G. Inverse of Rows and Columns | 2,200 | brute force; constructive algorithms | You are given a binary matrix \(a\) of size \(n \times m\). A binary matrix is a matrix where each element is either \(0\) or \(1\).You may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is chan... | The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 200\)) β the number of rows and the number of columns in the matrix.The next \(n\) lines contain \(m\) integers each. The \(j\)-th element in the \(i\)-th line is \(a_{i, j}\) (\(0 \le a_{i, j} \le 1\)) β the element of \(a\) at positio... | If it is impossible to obtain a sorted matrix, print ""NO"" in the first line.Otherwise print ""YES"" in the first line. In the second line print a string \(r\) of length \(n\). The \(i\)-th character \(r_i\) of this string should be '1' if the \(i\)-th row of the matrix is inverted and '0' otherwise. In the third line... | Input: 2 2 1 1 0 1 | Output: YES 00 10 | Hard | 2 | 737 | 333 | 536 | 11 | |
575 | D | 575D | D. Tablecity | 1,700 | constructive algorithms; implementation | There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert β Tablecityβs Chief of Police. Albert does not know where the thief is located, but he does know how he moves.Tablecity can be represented as 1000 Γ 2 grid, where every cell represents one district. Each ... | There is no input for this problem. | The first line of output contains integer N β duration of police search in hours. Each of the following N lines contains exactly 4 integers Xi1, Yi1, Xi2, Yi2 separated by spaces, that represent 2 districts (Xi1, Yi1), (Xi2, Yi2) which got investigated during i-th hour. Output is given in chronological order (i-th line... | Let's consider the following output:25 1 50 28 1 80 2This output is not guaranteed to catch the thief and is not correct. It is given to you only to show the expected output format. There exists a combination of an initial position and a movement strategy such that the police will not catch the thief.Consider the follo... | Input: Π ΡΡΠΎΠΉ Π·Π°Π΄Π°ΡΠ΅ Π½Π΅Ρ ΠΏΡΠΈΠΌΠ΅ΡΠΎΠ² Π²Π²ΠΎΠ΄Π°-Π²ΡΠ²ΠΎΠ΄Π°.This problem doesn't have sample input and output. | Output: Π‘ΠΌΠΎΡΡΠΈΡΠ΅ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π½ΠΈΠΆΠ΅.See the note below. | Medium | 2 | 1,097 | 35 | 529 | 5 |
291 | B | 291B | B. Command Line Arguments | 1,300 | *special; implementation; strings | The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings ar... | The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the "".,?!"""" signs, or a space.It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guar... | In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the ""<"" (less) character to the left of your lexemes and the "">"" (more) character to the right. Print the lexemes in the order in which they occur in the command.Please, follow the given ou... | Input: ""RUn.exe O"" """" "" 2ne, "" two! . "" "" | Output: <RUn.exe O><>< 2ne, ><two!><.>< > | Easy | 3 | 2,122 | 391 | 408 | 2 | |
1,955 | E | 1955E | E. Long Inversions | 1,700 | brute force; greedy; implementation; sortings | A binary string \(s\) of length \(n\) is given. A binary string is a string consisting only of the characters '1' and '0'.You can choose an integer \(k\) (\(1 \le k \le n\)) and then apply the following operation any number of times: choose \(k\) consecutive characters of the string and invert them, i.e., replace all '... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 5000\)) β the length of the string \(s\).The second line of each test case contains a string \(s\) of length \(n\), consisting of the characters '1' and '0... | For each test case, output the maximum integer \(k\) (\(1 \le k \le n\)) for which it is possible to obtain a string \(s\) consisting only of the characters '1' using the described operations. | Input: 5500100501000710111013000210 | Output: 3 2 4 3 1 | Medium | 4 | 945 | 440 | 192 | 19 | |
1,550 | E | 1550E | E. Stringforces | 2,500 | binary search; bitmasks; brute force; dp; strings; two pointers | You are given a string \(s\) of length \(n\). Each character is either one of the first \(k\) lowercase Latin letters or a question mark.You are asked to replace every question mark with one of the first \(k\) lowercase Latin letters in such a way that the following value is maximized.Let \(f_i\) be the maximum length ... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 17\)) β the length of the string and the number of first Latin letters used.The second line contains a string \(s\), consisting of \(n\) characters. Each character is either one of the first \(k\) lowercase Latin letters o... | Print a single integer β the maximum value of the string after every question mark is replaced with one of the first \(k\) lowercase Latin letters. | In the first example the question marks can be replaced in the following way: ""aaaababbbb"". \(f_1 = 4\), \(f_2 = 4\), thus the answer is \(4\). Replacing it like this is also possible: ""aaaabbbbbb"". That way \(f_1 = 4\), \(f_2 = 6\), however, the minimum of them is still \(4\).In the second example one of the possi... | Input: 10 2 a??ab????b | Output: 4 | Expert | 6 | 694 | 338 | 147 | 15 |
703 | C | 703C | C. Chris and Road | 2,100 | geometry; implementation | And while Mishka is enjoying her trip...Chris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend.Once walking with his friend, John gave Chris the following problem:At... | The first line of the input contains four integers n, w, v, u (3 β€ n β€ 10 000, 1 β€ w β€ 109, 1 β€ v, u β€ 1000) β the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively.The next n lines describes polygon vertices in counter-clockwise order. i-th of them contains pair of integers xi... | Print the single real t β the time the pedestrian needs to croos the road and not to be hit by the bus. The answer is considered correct if its relative or absolute error doesn't exceed 10 - 6. | Following image describes initial position in the first sample case: | Input: 5 5 1 21 23 14 33 41 4 | Output: 5.0000000000 | Hard | 2 | 1,634 | 449 | 193 | 7 |
767 | A | 767A | A. Snacktower | 1,100 | data structures; implementation | According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be ... | The first line contains single integer n (1 β€ n β€ 100 000) β the total number of snacks.The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n. | Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty. | In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right aft... | Input: 33 1 2 | Output: 3 2 1 | Easy | 2 | 976 | 239 | 239 | 7 |
1,045 | C | 1045C | C. Hyperspace Highways | 2,300 | dfs and similar; graphs; trees | In an unspecified solar system, there are \(N\) planets. A space government company has recently hired space contractors to build \(M\) bidirectional Hyperspaceβ’ highways, each connecting two different planets. The primary objective, which was to make sure that every planet can be reached from any other planet taking o... | The first line contains three positive integers \(N\) (\(1\leq N\leq 100\,000\)), \(M\) (\(1\leq M\leq 500\,000\)) and \(Q\) (\(1\leq Q\leq 200\,000\)), denoting the number of planets, the number of Hyperspaceβ’ highways, and the number of queries, respectively.Each of the following M lines contains a highway: highway \... | Output \(Q\) lines: the \(j\)-th line of output should contain the minimal number of Hyperspaceβ’ highways one needs to take to travel from planet \(a_j\) to planet \(b_j\). | The graph from the second sample: | Input: 5 7 21 21 31 42 32 43 41 51 42 5 | Output: 12 | Expert | 3 | 1,382 | 861 | 172 | 10 |
1,216 | C | 1216C | C. White Sheet | 1,700 | geometry; math | There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates \((0, 0)\), and coordinate axes are left and bottom sides of the table, then the ... | The first line of the input contains four integers \(x_1, y_1, x_2, y_2\) \((0 \le x_1 < x_2 \le 10^{6}, 0 \le y_1 < y_2 \le 10^{6})\) β coordinates of the bottom left and the top right corners of the white sheet.The second line of the input contains four integers \(x_3, y_3, x_4, y_4\) \((0 \le x_3 < x_4 \le 10^{6}, 0... | If some part of the white sheet can be seen from the above after the two black sheets are placed, print ""YES"" (without quotes). Otherwise print ""NO"". | In the first example the white sheet is fully covered by black sheets.In the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point \((6.5, 4.5)\) lies not strictly inside the white sheet and lies strictly outside of both black sheets. | Input: 2 2 4 4 1 1 3 5 3 1 5 5 | Output: NO | Medium | 2 | 1,090 | 738 | 153 | 12 |
1,609 | G | 1609G | G. A Stroll Around the Matrix | 3,000 | data structures; greedy; math | William has two arrays of numbers \(a_1, a_2, \dots, a_n\) and \(b_1, b_2, \dots, b_m\). The arrays satisfy the conditions of being convex. Formally an array \(c\) of length \(k\) is considered convex if \(c_i - c_{i - 1} < c_{i + 1} - c_i\) for all \(i\) from \(2\) to \(k - 1\) and \(c_1 < c_2\).Throughout William's l... | The first line contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 100, 2 \le m \le 10^5\), \(1 \le q \le 10^5\)), the sizes of the arrays and the number of changes.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)), the contents of array \(a\).The third line contains... | After each change, output one integer, the minimum length of the path in the constructed matrix. | Input: 5 3 4 1 2 4 7 11 5 7 10 1 3 2 2 2 5 1 5 4 2 1 7 | Output: 98 128 219 229 | Master | 3 | 1,213 | 611 | 96 | 16 | |
1,030 | E | 1030E | E. Vasya and Good Sequences | 2,000 | bitmasks; dp | Vasya has a sequence \(a\) consisting of \(n\) integers \(a_1, a_2, \dots, a_n\). Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number \(6\) \((\dots 00000000110_2)\) into \(3\) \((\dots 00000000011... | The first line contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β length of the sequence.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{18}\)) β the sequence \(a\). | Print one integer β the number of pairs \((l, r)\) such that \(1 \le l \le r \le n\) and the sequence \(a_l, a_{l + 1}, \dots, a_r\) is good. | In the first example pairs \((2, 3)\) and \((1, 3)\) are valid. Pair \((2, 3)\) is valid since \(a_2 = 7 \rightarrow 11\), \(a_3 = 14 \rightarrow 11\) and \(11 \oplus 11 = 0\), where \(\oplus\) β bitwise exclusive or. Pair \((1, 3)\) is valid since \(a_1 = 6 \rightarrow 3\), \(a_2 = 7 \rightarrow 13\), \(a_3 = 14 \righ... | Input: 36 7 14 | Output: 2 | Hard | 2 | 864 | 217 | 141 | 10 |
255 | C | 255C | C. Almost Arithmetical Progression | 1,500 | brute force; dp | Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: a1 = p, where p is some integer; ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer.... | The first line contains integer n (1 β€ n β€ 4000). The next line contains n integers b1, b2, ..., bn (1 β€ bi β€ 106). | Print a single integer β the length of the required longest subsequence. | In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10. | Input: 23 5 | Output: 2 | Medium | 2 | 761 | 115 | 72 | 2 |
451 | E | 451E | E. Devu and Flowers | 2,300 | bitmasks; combinatorics; number theory | Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.Now Devu wants to select exactly s flowers from the boxes to decorate ... | The first line of input contains two space-separated integers n and s (1 β€ n β€ 20, 0 β€ s β€ 1014).The second line contains n space-separated integers f1, f2, ... fn (0 β€ fi β€ 1012). | Output a single integer β the number of ways in which Devu can select the flowers modulo (109 + 7). | Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.Sample 2. There is only one way of selecting 4 flowers: {2, 2}.Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}. | Input: 2 31 3 | Output: 2 | Expert | 3 | 644 | 180 | 99 | 4 |
1,073 | A | 1073A | A. Diverse Substring | 1,000 | implementation; strings | You are given a string \(s\), consisting of \(n\) lowercase Latin letters.A substring of string \(s\) is a continuous segment of letters from \(s\). For example, ""defor"" is a substring of ""codeforces"" and ""fors"" is not. The length of the substring is the number of letters in it.Let's call some string of length \(... | The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the length of string \(s\).The second line is the string \(s\), consisting of exactly \(n\) lowercase Latin letters. | Print ""NO"" if there is no diverse substring in the string \(s\).Otherwise the first line should contain ""YES"". The second line should contain any diverse substring of string \(s\). | The first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to ""No comments"" answer. | Input: 10codeforces | Output: YEScode | Beginner | 2 | 691 | 188 | 184 | 10 |
553 | E | 553E | E. Kyoya and Train | 3,200 | dp; fft; graphs; math; probabilities | Kyoya Ootori wants to take the train to get to school. There are n train stations and m one-way train lines going between various stations. Kyoya is currently at train station 1, and the school is at station n. To take a train, he must pay for a ticket, and the train also takes a certain amount of time. However, the tr... | The first line of input contains four integers n, m, t, x (2 β€ n β€ 50, 1 β€ m β€ 100, 1 β€ t β€ 20 000, 0 β€ x β€ 106).The next 2m lines contain the description of the trains. The 2i-th line will have 3 integers ai, bi, ci, representing a one way train from station ai to bi with ticket cost ci (1 β€ ai, bi β€ n, ai β bi, 0 β€ c... | Print a single real number that is equal to an optimal expected cost of getting to school. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6. | The optimal strategy in the first case is as follows:First, travel along first train line. With probability 1 / 2 Kyoya will take 1 time unit. Otherwise, Kyoya will take 3 time units.If the train takes 1 time unit, travel along the 4th train line. Kyoya will make it to school in time with probability 1 / 2. Otherwise, ... | Input: 4 4 5 11 2 050000 0 50000 0 02 3 010000 0 0 0 900003 4 0100000 0 0 0 02 4 00 0 0 50000 50000 | Output: 0.7000000000 | Master | 5 | 1,463 | 716 | 185 | 5 |
1,082 | G | 1082G | G. Petya and Graph | 2,400 | flows; graphs | Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of \(n\) vertices and \(m\) edges.The weight of the \(i\)-th vertex is \(a_i\).The weight of the \(i\)-th edge is \(w_i\).A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must ... | The first line contains two numbers \(n\) and \(m\) (\(1 \le n \le 10^3, 0 \le m \le 10^3\)) - the number of vertices and edges in the graph, respectively.The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) - the weights of the vertices of the graph.The following \(m\) lines contain ... | Print one integer β the maximum weight of the subgraph of the given graph. | In the first test example, the optimal subgraph consists of the vertices \({1, 3, 4}\) and has weight \(4 + 4 + 5 - (1 + 2 + 2) = 8\). In the second test case, the optimal subgraph is empty. | Input: 4 5 1 5 2 2 1 3 4 1 4 4 3 4 5 3 2 2 4 2 2 | Output: 8 | Expert | 2 | 652 | 633 | 74 | 10 |
1,742 | C | 1742C | C. Stripes | 900 | implementation | On an \(8 \times 8\) grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes.Determine which color was used last. The red stripe... | The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 4000\)) β the number of test cases. The description of test cases follows. There is an empty line before each test case.Each test case consists of \(8\) lines, each containing \(8\) characters. Each of these characters is either 'R', 'B', or '... | For each test case, output 'R' if a red stripe was painted last, and 'B' if a blue stripe was painted last (without quotes). | The first test case is pictured in the statement.In the second test case, the first blue column is painted first, then the first and last red rows, and finally the last blue column. Since a blue stripe is painted last, the answer is B. | Input: 4....B.......B.......B...RRRRRRRR....B.......B.......B.......B...RRRRRRRBB......BB......BB......BB......BB......BB......BRRRRRRRBRRRRRRBB.B.B..BBRRRRRRBB.B.B..BB.B.B..BBRRRRRRBB.B.B..BB.B.B..BB........................RRRRRRRR................................ | Output: R B B R | Beginner | 1 | 372 | 559 | 124 | 17 |
1,299 | D | 1299D | D. Around the World | 3,000 | bitmasks; combinatorics; dfs and similar; dp; graphs; graphs; math; trees | Guy-Manuel and Thomas are planning \(144\) trips around the world.You are given a simple weighted undirected connected graph with \(n\) vertexes and \(m\) edges with the following restriction: there isn't any simple cycle (i. e. a cycle which doesn't pass through any vertex more than once) of length greater than \(3\) ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^5\)) β the number of vertexes and edges in the graph. The \(i\)-th of the next \(m\) lines contains three integers \(a_i\), \(b_i\) and \(w_i\) (\(1 \le a_i, b_i \le n, a_i \neq b_i, 0 \le w_i < 32\)) β the endpoints of the \(i\)-th edge and its w... | Output the answer modulo \(10^9+7\). | The pictures below represent the graphs from examples. In the first example, there aren't any nontrivial cycles with cost \(0\), so we can either remove or keep the only edge incident to the vertex \(1\). In the second example, if we don't remove the edge \(1-2\), then there is a cycle \(1-2-4-5-2-1\) with cost \(0\); ... | Input: 6 8 1 2 0 2 3 1 2 4 3 2 6 2 3 4 8 3 5 4 5 4 5 5 6 6 | Output: 2 | Master | 8 | 995 | 499 | 36 | 12 |
1,914 | B | 1914B | B. Preparing for the Contest | 800 | constructive algorithms; math | Monocarp is practicing for a big contest. He plans to solve \(n\) problems to make sure he's prepared. Each of these problems has a difficulty level: the first problem has a difficulty level of \(1\), the second problem has a difficulty level of \(2\), and so on, until the last (\(n\)-th) problem, which has a difficult... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 50\); \(0 \le k \le n - 1\)). | For each test case, print \(n\) distinct integers from \(1\) to \(n\), denoting the order in which Monocarp should solve the problems. If there are multiple answers, print any of them.It can be shown that under the constraints of the problem, the answer always exists. | Input: 36 25 45 0 | Output: 3 5 4 1 6 2 1 2 3 4 5 5 4 3 2 1 | Beginner | 2 | 957 | 212 | 268 | 19 | |
1,891 | F | 1891F | F. A Growing Tree | 2,000 | data structures; dfs and similar; trees | You are given a rooted tree with the root at vertex \(1\), initially consisting of a single vertex. Each vertex has a numerical value, initially set to \(0\). There are also \(q\) queries of two types: The first type: add a child vertex with the number \(sz + 1\) to vertex \(v\), where \(sz\) is the current size of the... | The first line contains a single integer \(T\) (\(1 \leq T \leq 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(q\) (\(1 \leq q \leq 5 \cdot 10^5\)) β the number of queries.The following \(q\) lines can fall into two cases: The f... | For each test case, output the numerical value of each vertex of the final tree after all queries have been performed. | In the first case, the final tree with the assigned numerical values will look like this: The final tree with the assigned numerical values | Input: 392 1 31 12 2 11 12 3 21 32 1 41 32 3 252 1 11 12 1 -11 12 1 151 11 12 1 12 1 32 2 10 | Output: 7 5 8 6 2 1 0 1 4 14 4 | Hard | 3 | 566 | 987 | 118 | 18 |
1,468 | G | 1468G | G. Hobbits | 2,500 | binary search; geometry | The hobbits Frodo and Sam are carrying the One Ring to Mordor. In order not to be spotted by orcs, they decided to go through the mountains.The mountain relief can be represented as a polyline with \(n\) points \((x_i, y_i)\), numbered from \(1\) to \(n\) (\(x_i < x_{i + 1}\) for \(1 \le i \le n - 1\)). Hobbits start t... | The first line of the input contains two integers \(n\) and \(H\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le H \le 10^4\)) β the number of vertices in polyline and the tower height.The next \(n\) lines contain two integers \(x_i, y_i\) each (\(0 \le x_i \le 4 \cdot 10^5\); \(0 \le y_i \le 10^4\)) β the coordinates of the p... | Print one real number β the total distance the hobbits have to walk while wearing cloaks. Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-6}\) β formally, if your answer is \(a\), and the jury's answer is \(b\), your answer will be accepted if \(\dfrac{|a - b|}{\max(1, b)... | Input: 6 10 10 40 20 10 25 30 30 15 50 15 65 30 | Output: 70.4034587602 | Expert | 2 | 1,378 | 406 | 336 | 14 | |
976 | E | 976E | E. Well played! | 2,100 | greedy; sortings | Recently Max has got himself into popular CCG ""BrainStone"". As ""BrainStone"" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two type... | The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature. | Print single integer β maximum total damage creatures can deal. | In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on t... | Input: 2 1 110 156 1 | Output: 27 | Hard | 2 | 890 | 304 | 63 | 9 |
2,001 | E2 | 2001E2 | E2. Deterministic Heap (Hard Version) | 2,900 | combinatorics; dp; trees | This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on \(n\) and \(t\). You can make hacks only if both versions of the problem are solved.Consider a perfect binary tree with size \(2^n - 1\), with nodes numbered from ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows.The first line of each test case contains three integers \(n, k, p\) (\(2 \le n \le 100\), \(1 \le k \le 500\), \(10^8 \le p \le 10^9\), \(p\) is a prime).It is ... | For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation \(\mathrm{add}\) exactly \(k\) times, modulo \(p\). | For the first testcase, if we choose \(v = 1\) and do the operation, we would have \(a = [1, 0, 0]\), and since \(a_2 = a_3\), we can choose either of them when doing the first \(\mathrm{pop}\) operation, so such heap is not a deterministic max-heap. And if we choose \(v = 2\), we would have \(a = [1, 1, 0]\), during t... | Input: 62 1 9982443533 2 9982448533 3 9982443533 4 1000000374 2 1000000394 3 100000037 | Output: 2 12 40 100 32 224 | Master | 3 | 2,068 | 442 | 213 | 20 |
946 | B | 946B | B. Weird Subtraction Process | 1,100 | math; number theory | You have two variables a and b. Consider the following sequence of actions performed with these variables: If a = 0 or b = 0, end the process. Otherwise, go to step 2; If a β₯ 2Β·b, then set the value of a to a - 2Β·b, and repeat step 1. Otherwise, go to step 3; If b β₯ 2Β·a, then set the value of b to b - 2Β·a, and repeat s... | The only line of the input contains two integers n and m (1 β€ n, m β€ 1018). n is the initial value of variable a, and m is the initial value of variable b. | Print two integers β the values of a and b after the end of the process. | Explanations to the samples: a = 12, b = 5 a = 2, b = 5 a = 2, b = 1 a = 0, b = 1; a = 31, b = 12 a = 7, b = 12. | Input: 12 5 | Output: 0 1 | Easy | 2 | 510 | 155 | 72 | 9 |
98 | E | 98E | E. Help Shrek and Donkey | 2,700 | dp; games; math; probabilities | Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds m cards and the Donkey holds n cards (the players do not see each other's cards), and one more card lies on the table face down so that both players... | The first line contains space-separated integers m and n (0 β€ m, n β€ 1000). | Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10 - 9. | Input: 0 3 | Output: 0.25 0.75 | Master | 4 | 1,325 | 75 | 129 | 0 | |
201 | B | 201B | B. Guess That Car! | 1,800 | math; ternary search | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called ""Guess That Car!"".The game show takes place on a giant parking lot, which is 4n meters long from north to south and 4m meters wide from west ... | The first line contains two integers n and m (1 β€ n, m β€ 1000) β the sizes of the parking lot. Each of the next n lines contains m integers: the j-th number in the i-th line describes the ""rarity"" cij (0 β€ cij β€ 100000) of the car that is located in the square with coordinates (i, j). | In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers li and lj (0 β€ li β€ n, 0 β€ lj β€ m) β the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting ... | In the first test case the total time of guessing all cars is equal to 3Β·8 + 3Β·8 + 4Β·8 + 9Β·8 + 5Β·40 + 1Β·40 = 392.The coordinate system of the field: | Input: 2 33 4 53 9 1 | Output: 3921 1 | Medium | 2 | 1,907 | 287 | 580 | 2 |
846 | B | 846B | B. Math Show | 1,800 | brute force; greedy | Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.By solving sub... | The first line contains three integer numbers n, k and M (1 β€ n β€ 45, 1 β€ k β€ 45, 0 β€ M β€ 2Β·109).The second line contains k integer numbers, values tj (1 β€ tj β€ 1000000), where tj is the time in minutes required to solve j-th subtask of any task. | Print the maximum amount of points Polycarp can earn in M minutes. | In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.In the second example Polycarp can solve the first subtask of all five tasks and spend 5Β·1 = 5 minutes. Also he can solve the second subtasks of two ... | Input: 3 4 111 2 3 4 | Output: 6 | Medium | 2 | 730 | 246 | 66 | 8 |
45 | C | 45C | C. Dancing Lessons | 1,900 | data structures | There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, havi... | The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in ... | Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the in... | Input: 4BGBG4 2 4 3 | Output: 23 41 2 | Hard | 1 | 735 | 340 | 396 | 0 | |
721 | A | 721A | A. One-dimensional Japanese Crossword | 800 | implementation | Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a Γ b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents... | The first line of the input contains a single integer n (1 β€ n β€ 100) β the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' β to white square in the row that Adaltik drew). | The first line should contain a single integer k β the number of integers encrypting the row, e.g. the number of groups of black squares in the row.The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right. | The last sample case correspond to the picture in the statement. | Input: 3BBW | Output: 12 | Beginner | 1 | 948 | 274 | 310 | 7 |
1,054 | D | 1054D | D. Changing Array | 1,900 | greedy; implementation | At a break Vanya came to the class and saw an array of \(n\) \(k\)-bit integers \(a_1, a_2, \ldots, a_n\) on the board. An integer \(x\) is called a \(k\)-bit integer if \(0 \leq x \leq 2^k - 1\). Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will n... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \leq n \leq 200\,000\), \(1 \leq k \leq 30\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 2^k - 1\)), separated by spaces β the array of \(k\)-bit integers. | Print one integer β the maximum possible number of segments with XOR not equal to \(0\) that can be obtained by making several (possibly \(0\)) operations described in the statement. | In the first example if Vasya does not perform any operations, he gets an array that has \(5\) segments that Vanya likes. If he performs the operation with \(i = 2\), he gets an array \([1, 0, 0]\), because \(\overline{3} = 0\) when \(k = 2\). This array has \(3\) segments that Vanya likes. Also, to get an array with \... | Input: 3 21 3 0 | Output: 5 | Hard | 2 | 1,013 | 266 | 182 | 10 |
42 | B | 42B | B. Game of chess unfinished | 1,700 | implementation | Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. ""Aha, blacks certainly didn't win!"", β Volodya said and was right for sure. And your task is to say whether whites had won or not.Pieces on the ch... | The input contains 4 space-separated piece positions: positions of the two rooks, the white king and the black king. Each position on 8 Γ 8 chessboard is denoted by two symbols β ('a' - 'h') and ('1' - '8') β which stand for horizontal and vertical coordinates of the cell occupied by the piece. It is guaranteed, that n... | Output should contain one word: ""CHECKMATE"" if whites mate blacks, and ""OTHER"" otherwise. | Input: a6 b4 c8 a8 | Output: CHECKMATE | Medium | 1 | 1,005 | 388 | 93 | 0 | |
127 | A | 127A | A. Wasted Time | 900 | geometry | Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.Mr. Scrooge's signature can be represented as a polyline A1... | The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai β integers xi and yi, separated by a space.All points Ai are different. The absolute value of all coordinates do... | Print one real number β the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6. | Input: 2 10 010 0 | Output: 0.200000000 | Beginner | 1 | 1,001 | 382 | 145 | 1 | |
1,578 | C | 1578C | C. Cactus Lady and her Cing | 3,500 | Cactus lady loves her cactuses very much. Especially she likes a small cactus named Cing. Cing can be seen as a connected undirected graph in which every vertex lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair ... | The first line contains an integer \(t\) β the number of test cases.Each test case begins with a line containing two integers \(n\) and \(m\) β the number of vertices and the number of edges in a given cactus, respectively (\(1 \le n \le 200\,000\); \(0 \le m \le 250\,000\)).Each of the following \(m\) lines contains t... | Print an answer for each test case in the same order the cases appear in the input.For each test case print ""No"" in the first line, if no layout exists.Otherwise print ""Yes"" in the first line, and the following \(n\) lines describing the layout. The \(i\)-th of these \(n\) lines should contain two integers \(x_i\) ... | Empty lines between test cases are for clarity. In real test cases there are no empty lines.In these notes, we consider the embeddings for tests 2 and 4.We start with the embedding for test 2. Here goes the embedding for test 4. | Input: 5 4 3 1 2 2 3 3 4 8 7 1 2 3 2 2 4 4 5 4 6 6 7 6 8 5 4 1 2 1 3 1 4 1 5 8 9 1 2 2 3 3 4 1 4 4 5 5 6 6 7 7 8 5 8 10 10 1 2 2 3 3 4 4 5 5 6 6 1 3 7 4 8 1 9 6 10 | Output: Yes 0 0 0 1 1 1 1 2 Yes 0 3 1 3 1 4 1 2 0 2 1 1 0 1 1 0 No Yes 0 0 1 0 1 1 0 1 0 2 0 3 1 3 1 2 Yes 1 1 1 2 1 3 0 3 0 2 0 1 1 4 0 4 1 0 0 0 | Master | 0 | 1,046 | 484 | 427 | 15 | |
1,695 | E | 1695E | E. Ambiguous Dominoes | 2,700 | constructive algorithms; dfs and similar; graphs | Polycarp and Monocarp are both solving the same puzzle with dominoes. They are given the same set of \(n\) dominoes, the \(i\)-th of which contains two numbers \(x_i\) and \(y_i\). They are also both given the same \(m\) by \(k\) grid of values \(a_{ij}\) such that \(m\cdot k = 2n\).The puzzle asks them to place the \(... | The first line contains a single integer \(n\) (\(1 \le n \le 3\cdot 10^5\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le 2n\)). | If there is no solution, print a single integer \(-1\).Otherwise, print \(m\) and \(k\), the height and width of the puzzle grid, on the first line of output. These should satisfy \(m\cdot k = 2n\).The \(i\)-th of the next \(m\) lines should contain \(k\) integers, the \(j\)-th of which is \(a_{ij}\).The next \(m\) lin... | Extra blank lines are added to the output for clarity, but are not required.The third sample case corresponds to the image from the statement. | Input: 1 1 2 | Output: -1 | Master | 3 | 1,260 | 184 | 784 | 16 |
1,141 | E | 1141E | E. Superhero Battle | 1,700 | math | A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly \(n\) minutes. After a round ends, the next round starts immediately. This is repeated over and over again.Each round has the same scenario. It is described by a sequence of \(n\) numbers: \(d_1, d_2, \dots, d_n\) (\(-10^6 \le... | The first line contains two integers \(H\) and \(n\) (\(1 \le H \le 10^{12}\), \(1 \le n \le 2\cdot10^5\)). The second line contains the sequence of integers \(d_1, d_2, \dots, d_n\) (\(-10^6 \le d_i \le 10^6\)), where \(d_i\) is the value to change monster's hp in the \(i\)-th minute of a round. | Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer \(k\) such that \(k\) is the first minute after which the monster is dead. | Input: 1000 6 -100 -200 -300 125 77 -4 | Output: 9 | Medium | 1 | 864 | 297 | 198 | 11 | |
1,301 | C | 1301C | C. Ayoub's function | 1,700 | binary search; combinatorics; greedy; math; strings | Ayoub thinks that he is a very smart person, so he created a function \(f(s)\), where \(s\) is a binary string (a string which contains only symbols ""0"" and ""1""). The function \(f(s)\) is equal to the number of substrings in the string \(s\) that contains at least one symbol, that is equal to ""1"".More formally, \... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The only line for each test case contains two integers \(n\), \(m\) (\(1 \leq n \leq 10^{9}\), \(0 \leq m \leq n\)) β the length of the... | For every test case print one integer number β the maximum value of \(f(s)\) over all strings \(s\) of length \(n\), which has exactly \(m\) symbols, equal to ""1"". | In the first test case, there exists only \(3\) strings of length \(3\), which has exactly \(1\) symbol, equal to ""1"". These strings are: \(s_1 = \)""100"", \(s_2 = \)""010"", \(s_3 = \)""001"". The values of \(f\) for them are: \(f(s_1) = 3, f(s_2) = 4, f(s_3) = 3\), so the maximum value is \(4\) and the answer is \... | Input: 5 3 1 3 2 3 3 4 0 5 2 | Output: 4 5 6 0 12 | Medium | 5 | 1,100 | 375 | 165 | 13 |
1,405 | A | 1405A | A. Permutation Forgery | 800 | constructive algorithms | A permutation of length \(n\) 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 arra... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 668\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 100\)) β the length of the permutation.The second line of each test case contains \(n\... | For each test case, output \(n\) integers \(p'_1,\ldots, p'_n\) β a permutation such that \(p'\ne p\) and \(F(p')=F(p)\).We can prove that for every permutation satisfying the input constraints, a solution exists.If there are multiple solutions, you may output any. | In the first test case, \(F(p)=\mathrm{sort}([1+2])=[3]\).And \(F(p')=\mathrm{sort}([2+1])=[3]\).In the second test case, \(F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]\).And \(F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]\).In the third test c... | Input: 3 2 1 2 6 2 1 6 5 4 3 5 2 4 3 1 5 | Output: 2 1 1 2 5 6 3 4 3 1 5 2 4 | Beginner | 1 | 945 | 416 | 265 | 14 |
906 | D | 906D | D. Power Tower | 2,700 | chinese remainder theorem; math; number theory | Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to... | First line of input contains two integers n (1 β€ n β€ 105) and m (1 β€ m β€ 109).Second line of input contains n integers wk (1 β€ wk β€ 109) which is the power of rocks that priests have.Third line of input contains single integer q (1 β€ q β€ 105) which is amount of queries from priests to you.kth of next q lines contains t... | Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk. | 327 = 7625597484987 | Input: 6 10000000001 2 2 3 3 381 11 62 22 32 44 44 54 6 | Output: 1124256327597484987 | Master | 3 | 908 | 360 | 134 | 9 |
1,681 | F | 1681F | F. Unique Occurrences | 2,300 | data structures; dfs and similar; divide and conquer; dp; dsu; trees | You are given a tree, consisting of \(n\) vertices. Each edge has an integer value written on it.Let \(f(v, u)\) be the number of values that appear exactly once on the edges of a simple path between vertices \(v\) and \(u\).Calculate the sum of \(f(v, u)\) over all pairs of vertices \(v\) and \(u\) such that \(1 \le v... | The first line contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β the number of vertices in the tree.Each of the next \(n-1\) lines contains three integers \(v, u\) and \(x\) (\(1 \le v, u, x \le n\)) β the description of an edge: the vertices it connects and the value written on it.The given edges form a... | Print a single integer β the sum of \(f(v, u)\) over all pairs of vertices \(v\) and \(u\) such that \(v < u\). | Input: 3 1 2 1 1 3 2 | Output: 4 | Expert | 6 | 333 | 326 | 111 | 16 | |
295 | E | 295E | E. Yaroslav and Points | 2,500 | data structures | Yaroslav has n points that lie on the Ox axis. The coordinate of the first point is x1, the coordinate of the second point is x2, ..., the coordinate of the n-th point is β xn. Now Yaroslav wants to execute m queries, each of them is of one of the two following types: Move the pj-th point from position xpj to position ... | The first line contains integer n β the number of points (1 β€ n β€ 105). The second line contains distinct integers x1, x2, ..., xn β the coordinates of points (|xi| β€ 109).The third line contains integer m β the number of queries (1 β€ m β€ 105). The next m lines contain the queries. The j-th line first contains integer ... | For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. | Input: 836 50 28 -75 40 -60 -95 -48202 -61 291 5 -531 1 4291 5 1302 -101 -712 -69 531 1 4041 5 5182 -101 532 50 8721 1 -2072 -99 -401 7 -3891 6 -1711 2 4641 7 -7071 1 -7301 1 5602 635 6441 7 -677 | Output: 17620406104616381560 | Expert | 1 | 596 | 597 | 277 | 2 | |
1,540 | E | 1540E | E. Tasty Dishes | 3,500 | math; matrices | Note that the memory limit is unusual.There are \(n\) chefs numbered \(1, 2, \ldots, n\) that must prepare dishes for a king. Chef \(i\) has skill \(i\) and initially has a dish of tastiness \(a_i\) where \(|a_i| \leq i\). Each chef has a list of other chefs that he is allowed to copy from. To stop chefs from learning ... | The first line contains a single integer \(n\) (\(1 \le n \le 300\)) β the number of chefs.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-i \le a_i \le i\)).The next \(n\) lines each begin with a integer \(c_i\) (\(0 \le c_i < n\)), denoting the number of chefs the \(i\)-th chef can copy from. Th... | For each query of the first type, print a single integer β the answer to the query. | Below is the set of chefs that each chef is allowed to copy from: \(1\): \(\{2, 3, 4, 5\}\) \(2\): \(\{3\}\) \(3\): \(\{4\}\) \(4\): \(\{5\}\) \(5\): \(\emptyset\) (no other chefs)Following is a description of the sample.For the first query of type \(1\), the initial tastiness values are \([1, 0, -2, -2, 4]\).The final... | Input: 5 1 0 -2 -2 4 4 2 3 4 5 1 3 1 4 1 5 0 7 1 1 1 5 2 4 3 1 1 1 5 2 3 2 1 2 2 4 2 5 1 1 981 4 5 | Output: 57 71 316 278497818 | Master | 2 | 2,207 | 859 | 83 | 15 |
1,436 | A | 1436A | A. Reorder | 800 | math | For a given array \(a\) consisting of \(n\) integers and a given integer \(m\) find if it is possible to reorder elements of the array \(a\) in such a way that \(\sum_{i=1}^{n}{\sum_{j=i}^{n}{\frac{a_j}{j}}}\) equals \(m\)? It is forbidden to delete elements as well as insert new elements. Please note that no rounding ... | The first line contains a single integer \(t\) β the number of test cases (\(1 \le t \le 100\)). The test cases follow, each in two lines.The first line of a test case contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(0 \le m \le 10^6\)). The second line contains integers \(a_1, a_2, \ldots, a_n\) (\(0 \le ... | For each test case print ""YES"", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and ""NO"" otherwise. | In the first test case one of the reorders could be \([1, 2, 5]\). The sum is equal to \((\frac{1}{1} + \frac{2}{2} + \frac{5}{3}) + (\frac{2}{2} + \frac{5}{3}) + (\frac{5}{3}) = 8\). The brackets denote the inner sum \(\sum_{j=i}^{n}{\frac{a_j}{j}}\), while the summation of brackets corresponds to the sum over \(i\). | Input: 2 3 8 2 5 1 4 4 0 1 2 3 | Output: YES NO | Beginner | 1 | 377 | 364 | 170 | 14 |
1,886 | B | 1886B | B. Fear of the Dark | 1,200 | binary search; geometry; math | Monocarp tries to get home from work. He is currently at the point \(O = (0, 0)\) of a two-dimensional plane; his house is at the point \(P = (P_x, P_y)\).Unfortunately, it is late in the evening, so it is very dark. Monocarp is afraid of the darkness. He would like to go home along a path illuminated by something.Than... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of three lines: the first line contains two integers \(P_x\) and \(P_y\) (\(-10^3 \le P_x, P_y \le 10^3\)) β the location of Monocarp's house; the second line contains two integers \(A_x\) an... | For each test case, print the answer on a separate line β one real number equal to the minimum value of \(w\) such that there is a completely illuminated path from the point \(O\) to the point \(P\).Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-6}\) β formally, if your ... | Input: 23 31 0-1 63 3-1 -14 3 | Output: 3.6055512755 3.2015621187 | Easy | 3 | 965 | 656 | 445 | 18 | |
372 | C | 372C | C. Watching Fireworks is Fun | 2,100 | data structures; dp; math | A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.In the festival m fireworks will be launched. The i-th (1 β€ i β€ m) launching is on time ti at section ai. If you are at sec... | The first line contains three integers n, m, d (1 β€ n β€ 150000; 1 β€ m β€ 300; 1 β€ d β€ n).Each of the next m lines contains integers ai, bi, ti (1 β€ ai β€ n; 1 β€ bi β€ 109; 1 β€ ti β€ 109). The i-th line contains description of the i-th launching.It is guaranteed that the condition ti β€ ti + 1 (1 β€ i < m) will be satisfied. | Print a single integer β the maximum sum of happiness that you can gain from watching all the fireworks.Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | Input: 50 3 149 1 126 1 46 1 10 | Output: -31 | Hard | 3 | 846 | 319 | 253 | 3 | |
301 | A | 301A | A. Yaroslav and Sequence | 1,800 | constructive algorithms | Yaroslav has an array, consisting of (2Β·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.Yaroslav is now wondering: what maximum sum of array elements can... | The first line contains an integer n (2 β€ n β€ 100). The second line contains (2Β·n - 1) integers β the array elements. The array elements do not exceed 1000 in their absolute value. | In a single line print the answer to the problem β the maximum sum that Yaroslav can get. | In the first sample you do not need to change anything. The sum of elements equals 150.In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100. | Input: 250 50 50 | Output: 150 | Medium | 1 | 410 | 180 | 89 | 3 |
883 | K | 883K | K. Road Widening | 1,800 | constructive algorithms; greedy; implementation | Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on ... | The first line contains integer n (1 β€ n β€ 2Β·105) β number of parts of the street.Each of the following n lines contains two integers si, gi (1 β€ si β€ 106, 0 β€ gi β€ 106) β current width of road and width of the lawn on the i-th part of the street. | In the first line print the total width of lawns which will be removed.In the second line print n integers s'1, s'2, ..., s'n (si β€ s'i β€ si + gi) β new widths of the road starting from the first part and to the last.If there is no solution, print the only integer -1 in the first line. | Input: 34 54 54 10 | Output: 169 9 10 | Medium | 3 | 1,290 | 247 | 286 | 8 | |
1,860 | A | 1860A | A. Not a Substring | 900 | constructive algorithms; strings | A bracket sequence is a string consisting of characters '(' and/or ')'. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" ... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains a string \(s\) (\(2 \le |s| \le 50\)), consisting of characters ""("" and/or "")"". | For each test case, print the answer to it. If there is no required regular bracket sequence, print NO in a separate line. Otherwise, print YES in the first line, and the required regular bracket sequence \(t\) itself in the second line. If there are multiple answers β you may print any of them. | Input: 4)((()()))() | Output: YES (()) YES ()()() NO YES ()(()()) | Beginner | 2 | 720 | 221 | 296 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.