contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,732 | C1 | 1732C1 | C1. Sheikh (Easy version) | 1,800 | binary search; bitmasks; greedy; two pointers | This is the easy version of the problem. The only difference is that in this version \(q = 1\).You are given an array of integers \(a_1, a_2, \ldots, a_n\).The cost of a subsegment of the array \([l, r]\), \(1 \leq l \leq r \leq n\), is the value \(f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)\), where ... | Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 10^5\), \(q = 1\)) β the length of the array and the numb... | For each test case print \(q\) pairs of numbers \(L_i \leq l \leq r \leq R_i\) such that the value \(f(l, r)\) is maximum and among such the length \(r - l + 1\) is minimum. If there are several correct answers, print any of them. | In the first test case, \(f(1, 1) = 0 - 0 = 0\).In the second test case, \(f(1, 1) = 5 - 5 = 0\), \(f(2, 2) = 10 - 10 = 0\). Note that \(f(1, 2) = (10 + 5) - (10 \oplus 5) = 0\), but we need to find a subsegment with the minimum length among the maximum values of \(f(l, r)\). So, only segments \([1, 1]\) and \([2, 2]\)... | Input: 61 101 12 15 101 23 10 2 41 34 10 12 8 31 45 121 32 32 32 101 57 10 1 0 1 0 1 01 7 | Output: 1 1 1 1 1 1 2 3 2 3 2 4 | Medium | 4 | 877 | 784 | 230 | 17 |
241 | B | 241B | B. Friends | 2,700 | binary search; bitmasks; data structures; math | You have n friends and you want to take m pictures of them. Exactly two of your friends should appear in each picture and no two pictures should contain the same pair of your friends. So if you have n = 3 friends you can take 3 different pictures, each containing a pair of your friends.Each of your friends has an attra... | The first line of input contains two integers n and m β the number of friends and the number of pictures that you want to take. Next line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β the values of attractiveness of the friends. | The only line of output should contain an integer β the optimal total sum of attractiveness of your pictures. | Input: 3 11 2 3 | Output: 3 | Master | 4 | 786 | 251 | 109 | 2 | |
802 | K | 802K | K. Send the Fool Further! (medium) | 2,100 | dp; trees | Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end...In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would no... | The first line contains two space-separated integers β the number of friends n () and the parameter k (1 β€ k β€ 105). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104) meaning that u and v are friends and the cost for traveling between u and v is c.It is again gu... | Again, output a single integer β the maximum sum of costs of tickets. | In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: 0, 1, 5, 1, 3, 1, 0, 2, 6, 2, 7, 2, 8. Observe that no friend is visited more than 3 times. | Input: 9 30 1 10 2 11 3 21 4 21 5 22 6 32 7 32 8 3 | Output: 15 | Hard | 2 | 945 | 379 | 69 | 8 |
1,244 | G | 1244G | G. Running in Pairs | 2,400 | constructive algorithms; greedy; math | Demonstrative competitions will be held in the run-up to the \(20NN\) Berlatov Olympic Games. Today is the day for the running competition!Berlatov team consists of \(2n\) runners which are placed on two running tracks; \(n\) runners are placed on each track. The runners are numbered from \(1\) to \(n\) on each track. ... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6, 1 \le k \le n^2\)) β the number of runners on each track and the maximum possible duration of the competition, respectively. | If it is impossible to reorder the runners so that the duration of the competition does not exceed \(k\) seconds, print \(-1\). Otherwise, print three lines. The first line should contain one integer \(sum\) β the maximum possible duration of the competition not exceeding \(k\). The second line should contain a permuta... | In the first example the order of runners on the first track should be \([5, 3, 2, 1, 4]\), and the order of runners on the second track should be \([1, 4, 2, 5, 3]\). Then the duration of the competition is \(max(5, 1) + max(3, 4) + max(2, 2) + max(1, 5) + max(4, 3) = 5 + 4 + 2 + 5 + 4 = 20\), so it is equal to the ma... | Input: 5 20 | Output: 20 1 2 3 4 5 5 2 4 3 1 | Expert | 3 | 1,431 | 198 | 935 | 12 |
28 | E | 28E | E. DravDe saves the world | 2,800 | geometry; math | How horrible! The empire of galactic chickens tries to conquer a beautiful city ""Z"", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane Oxy with n vertices. Naturally, DravDe can't keep still, he wants to destro... | The first line contains the number n (3 β€ n β€ 104) β the amount of vertices of the fence. Then there follow n lines containing the coordinates of these vertices (two integer numbers xi, yi) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections.The following four li... | In the first line output two numbers t1, t2 such, that if DravDe air drops at time t1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the duratio... | Input: 40 01 01 10 10 -11 0 1-10 1 -1 | Output: 1.00000000 0.00000000 | Master | 2 | 1,462 | 614 | 640 | 0 | |
991 | C | 991C | C. Candies | 1,500 | binary search; implementation | After passing a test, Vasya got himself a box of \(n\) candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.This means the process of eating candies is the following: in the beginning Vasya cho... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^{18}\)) β the initial amount of candies in the box. | Output a single integer β the minimal amount of \(k\) that would allow Vasya to eat at least half of candies he got. | In the sample, the amount of candies, with \(k=3\), would change in the following way (Vasya eats first):\(68 \to 65 \to 59 \to 56 \to 51 \to 48 \to 44 \to 41 \\ \to 37 \to 34 \to 31 \to 28 \to 26 \to 23 \to 21 \to 18 \to 17 \to 14 \\ \to 13 \to 10 \to 9 \to 6 \to 6 \to 3 \to 3 \to 0\).In total, Vasya would eat \(39\) ... | Input: 68 | Output: 3 | Medium | 2 | 1,238 | 118 | 116 | 9 |
178 | D3 | 178D3 | D3. Magic Squares | 2,100 | The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n Γ n. The elements of this matrix are integers. T... | The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 β€ ai β€ 108), separated by single spaces.The input limitations for getting 20 points are: 1 β€ n β€ 3 The input limitations for getting 50 points are: 1 β€ n β€ 4 It is guaranteed that there are no more than 9 distinct numbers am... | The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed... | Input: 31 2 3 4 5 6 7 8 9 | Output: 152 7 69 5 14 3 8 | Hard | 0 | 970 | 387 | 342 | 1 | ||
703 | D | 703D | D. Mishka and Interesting sum | 2,100 | data structures | Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because o... | The first line of the input contains single integer n (1 β€ n β€ 1 000 000) β the number of elements in the array.The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β array elements.The third line of the input contains single integer m (1 β€ m β€ 1 000 000) β the number of queries.Each of the n... | Print m non-negative integers β the answers for the queries in the order they appear in the input. | In the second sample:There is no integers in the segment of the first query, presented even number of times in the segment β the answer is 0.In the second query there is only integer 3 is presented even number of times β the answer is 3.In the third query only integer 1 is written down β the answer is 1.In the fourth q... | Input: 33 7 811 3 | Output: 0 | Hard | 1 | 1,004 | 438 | 98 | 7 |
1,167 | B | 1167B | B. Lost Numbers | 1,400 | brute force; divide and conquer; interactive; math | This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You m... | If you want to submit a hack for this problem, your test should contain exactly six space-separated integers \(a_1\), \(a_2\), ..., \(a_6\). Each of \(6\) special numbers should occur exactly once in the test. The test should be ended with a line break character. | Input: 16 64 345 672 | Output: ? 1 1 ? 2 2 ? 3 5 ? 4 6 ! 4 8 15 16 23 42 | Easy | 4 | 1,079 | 0 | 0 | 11 | ||
616 | B | 616B | B. Dinner with Emma | 1,000 | games; greedy | Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are... | The first line contains two integers n, m (1 β€ n, m β€ 100) β the number of streets and avenues in Munhattan.Each of the next n lines contains m integers cij (1 β€ cij β€ 109) β the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue. | Print the only integer a β the cost of the dinner for Jack and Emma. | In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinne... | Input: 3 44 1 3 52 2 2 25 4 5 1 | Output: 2 | Beginner | 2 | 904 | 275 | 68 | 6 |
1,902 | B | 1902B | B. Getting Points | 1,100 | binary search; brute force; greedy | Monocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject β programming.The academic term consists of \(n\) days, and in order not to get expelled, Monocarp has to earn at least \(P\) points during those \(n\) days. There are two way... | The first line contains a single integer \(tc\) (\(1 \le tc \le 10^4\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains four integers \(n\), \(P\), \(l\) and \(t\) (\(1 \le n, l, t \le 10^9\); \(1 \le P \le 10^{18}\)) β the number of days, the minimum total... | For each test, print one integer β the maximum number of days Monocarp can rest without being expelled from University. | In the first test case, the term lasts for \(1\) day, so Monocarp should attend at day \(1\). Since attending one lesson already gives \(5\) points (\(5 \ge P\)), so it doesn't matter, will Monocarp complete the task or not.In the second test case, Monocarp can, for example, study at days \(8\) and \(9\): at day \(8\) ... | Input: 51 5 5 214 3000000000 1000000000 500000000100 20 1 108 120 10 2042 280 13 37 | Output: 0 12 99 0 37 | Easy | 3 | 1,356 | 558 | 119 | 19 |
1,916 | H1 | 1916H1 | H1. Matrix Rank (Easy Version) | 2,700 | brute force; combinatorics; dp; math; matrices | This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on \(k\). You can make hacks only if all versions of the problem are solved.You are given integers \(n\), \(p\) and \(k\). \(p\) is guaranteed to be a prime number. For each \(r\) from \(0\) to \(k... | The first line of input contains three integers \(n\), \(p\) and \(k\) (\(1 \leq n \leq 10^{18}\), \(2 \leq p < 998\,244\,353\), \(0 \leq k \leq 5000\)).It is guaranteed that \(p\) is a prime number. | Output \(k+1\) integers, the answers for each \(r\) from \(0\) to \(k\). | Input: 3 2 3 | Output: 1 49 294 168 | Master | 5 | 699 | 199 | 72 | 19 | |
805 | A | 805A | A. Fake NP | 1,000 | greedy; math | Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.Solve the... | The first line contains two integers l and r (2 β€ l β€ r β€ 109). | Print single integer, the integer that appears maximum number of times in the divisors. If there are multiple answers, print any of them. | Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.htmlThe first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}. | Input: 19 29 | Output: 2 | Beginner | 2 | 364 | 63 | 137 | 8 |
1,316 | A | 1316A | A. Grade Allocation | 800 | implementation | \(n\) students are taking an exam. The highest possible score at this exam is \(m\). Let \(a_{i}\) be the score of the \(i\)-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All scores are i... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). 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^{3}\), \(1 \leq m \leq 10^{5}\)) β the number of students and the hig... | For each testcase, output one integer β the highest possible score you can assign to yourself such that both conditions are satisfied._ | In the first case, \(a = [1,2,3,4] \), with average of \(2.5\). You can change array \(a\) to \([10,0,0,0]\). Average remains \(2.5\), and all conditions are satisfied.In the second case, \(0 \leq a_{i} \leq 5\). You can change \(a\) to \([5,1,1,3]\). You cannot increase \(a_{1}\) further as it will violate condition \... | Input: 2 4 10 1 2 3 4 4 5 1 2 3 4 | Output: 10 5 | Beginner | 1 | 565 | 487 | 135 | 13 |
1,516 | D | 1516D | D. Cut | 2,100 | binary search; data structures; dp; graphs; number theory; two pointers | This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array \(a\) of length \(n\) written on it, and then he does the following: he picks a range \((l, r)\) and cuts the subsegment \(a_l, a_{l + 1}, \ldots, a_r\) out, removing the rest of the array. he then cuts this range into multip... | The first line contains \(2\) integers \(n\) and \(q\) (\(1 \le n,q \le 10^5\)) β the length of the array \(a\) and the number of queries.The next line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(1 \le a_i \le 10^5\)) β the elements of the array \(a\).Each of the next \(q\) lines contains \(2\) int... | For each query, print its answer on a new line. | The first query asks about the whole array. You can partition it into \([2]\), \([3,10,7]\), and \([5,14]\). The first subrange has product and LCM equal to \(2\). The second has product and LCM equal to \(210\). And the third has product and LCM equal to \(70\). Another possible partitioning is \([2,3]\), \([10,7]\), ... | Input: 6 3 2 3 10 7 5 14 1 6 2 4 3 5 | Output: 3 1 2 | Hard | 6 | 747 | 409 | 47 | 15 |
1,353 | D | 1353D | D. Constructing the Array | 1,600 | constructive algorithms; data structures; sortings | You are given an array \(a\) of length \(n\) consisting of zeros. You perform \(n\) actions with this array: during the \(i\)-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; ... | 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 only line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed... | For each test case, print the answer β the array \(a\) of length \(n\) after performing \(n\) actions described in the problem statement. Note that the answer exists and unique. | Input: 6 1 2 3 4 5 6 | Output: 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6 | Medium | 3 | 1,368 | 368 | 177 | 13 | |
1,906 | A | 1906A | A. Easy As ABC | 1,000 | brute force | You are playing a word puzzle. The puzzle starts with a \(3\) by \(3\) grid, where each cell contains either the letter A, B, or C.The goal of this puzzle is to find the lexicographically smallest possible word of length \(3\). The word can be formed by choosing three different cells where the cell containing the first... | Input consists of three lines, each containing three letters, representing the puzzle grid. Each letter in the grid can only be either A, B, or C. | Output the lexicographically smallest possible word of length \(3\) that you can find within the grid. | Input: BCB CAC BCB | Output: ABC | Beginner | 1 | 1,367 | 146 | 102 | 19 | |
2,048 | G | 2048G | G. Kevin and Matrices | 2,800 | brute force; combinatorics; dp; math | Kevin has been transported to Sacred Heart Hospital, which contains all the \( n \times m \) matrices with integer values in the range \( [1,v] \).Now, Kevin wants to befriend some matrices, but he is willing to befriend a matrix \( a \) if and only if the following condition is satisfied:$$$\( \min_{1\le i\le n}\left(... | Each test contains multiple test cases. The first line contains the number of test cases \( t \) (\( 1 \le t \le 8\cdot 10^3 \)).The only line of each test case contains three integers \(n\), \(m\), \(v\) (\( 1 \le n, v, n \cdot v \leq 10^6\), \(1 \le m \le 10^9 \)).It is guaranteed that the sum of \( n \cdot v \) over... | For each test case, output one integer β the number of matrices that can be friends with Kevin modulo \(998\,244\,353\). | In the first test case, besides the matrices \( a=\begin{bmatrix}1&2\\2&1\end{bmatrix} \) and \( a=\begin{bmatrix}2&1\\1&2\end{bmatrix} \), which do not satisfy the condition, the remaining \( 2^{2 \cdot 2} - 2 = 14 \) matrices can all be friends with Kevin. | Input: 32 2 22 3 411 45 14 | Output: 14 2824 883799966 | Master | 4 | 656 | 362 | 120 | 20 |
2,057 | C | 2057C | C. Trip to the Olympiad | 1,500 | bitmasks; constructive algorithms; greedy; math | In the upcoming year, there will be many team olympiads, so the teachers of ""T-generation"" need to assemble a team of three pupils to participate in them. Any three pupils will show a worthy result in any team olympiad. But winning the olympiad is only half the battle; first, you need to get there...Each pupil has an... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case set contains two integers \(l\) and \(r\) (\(0 \le l, r < 2^{30}\), \(r - l > 1\)) β the minimum and maximu... | For each test case set, output three pairwise distinct integers \(a, b\), and \(c\), such that \(l \le a, b, c \le r\) and the value of the expression \((a \oplus b) + (b \oplus c) + (a \oplus c)\) is maximized. If there are multiple triples with the maximum value, any of them can be output. | In the first test case, the only suitable triplet of numbers (\(a, b, c\)) (up to permutation) is (\(0, 1, 2\)).In the second test case, one of the suitable triplets is (\(8, 7, 1\)), where \((8 \oplus 7) + (7 \oplus 1) + (8 \oplus 1) = 15 + 6 + 9 = 30\). It can be shown that \(30\) is the maximum possible value of \((... | Input: 80 20 81 36 22128 13769 98115 1270 1073741823 | Output: 1 2 0 8 7 1 2 1 3 7 16 11 134 132 137 98 85 76 123 121 118 965321865 375544086 12551794 | Medium | 4 | 792 | 358 | 292 | 20 |
1,002 | E2 | 1002E2 | E2. Another array reconstruction algorithm | 1,900 | *special | You are given a quantum oracle - an operation on N + 1 qubits which implements a function . You are guaranteed that the function f implemented by the oracle can be represented in the following form (oracle from problem D2):Here (a vector of N integers, each of which can be 0 or 1), and is a vector of N 1s.Your task is ... | Hard | 1 | 1,401 | 0 | 0 | 10 | ||||
134 | A | 134A | A. Average Numbers | 1,200 | brute force; implementation | You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one). | The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers. | Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an e... | Input: 51 2 3 4 5 | Output: 13 | Easy | 2 | 202 | 177 | 330 | 1 | |
609 | D | 609D | D. Gadgets for dollars and pounds | 2,000 | binary search; greedy; two pointers | Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.Nura can buy gadgets for n days. For each day you know the exchange rates of dolla... | First line contains four integers n, m, k, s (1 β€ n β€ 2Β·105, 1 β€ k β€ m β€ 2Β·105, 1 β€ s β€ 109) β number of days, total number and required number of gadgets, number of burles Nura has.Second line contains n integers ai (1 β€ ai β€ 106) β the cost of one dollar in burles on i-th day.Third line contains n integers bi (1 β€ bi... | If Nura can't buy k gadgets print the only line with the number -1.Otherwise the first line should contain integer d β the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β the number of gadget and the day gadget should be bought. All values qi should be different... | Input: 5 4 2 21 2 3 2 13 2 1 2 31 12 11 22 2 | Output: 31 12 3 | Hard | 3 | 901 | 618 | 498 | 6 | |
1,539 | D | 1539D | D. PriceFixed | 1,600 | binary search; greedy; implementation; sortings; two pointers | Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β ""PriceFixed"". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: \(2\) rubles per item. For every... | The first line contains a single integer \(n\) (\(1 \leq n \leq 100\,000\)) β the number of products.Each of next \(n\) lines contains a product description. Each description consists of two integers \(a_i\) and \(b_i\) (\(1 \leq a_i \leq 10^{14}\), \(1 \leq b_i \leq 10^{14}\)) β the required number of the \(i\)-th pro... | Output the minimum sum that Lena needs to make all purchases. | In the first example, Lena can purchase the products in the following way: one item of product \(3\) for \(2\) rubles, one item of product \(1\) for \(2\) rubles, one item of product \(1\) for \(2\) rubles, one item of product \(2\) for \(1\) ruble (she can use the discount because \(3\) items are already purchased), o... | Input: 3 3 4 1 3 1 5 | Output: 8 | Medium | 5 | 904 | 459 | 61 | 15 |
2,103 | D | 2103D | D. Local Construction | 2,000 | constructive algorithms; dfs and similar; implementation; two pointers | An element \(b_i\) (\(1\le i\le m\)) in an array \(b_1, b_2, \ldots, b_m\) is a local minimum if at least one of the following holds: \(2\le i\le m - 1\) and \(b_i < b_{i - 1}\) and \(b_i < b_{i + 1}\), or \(i = 1\) and \(b_1 < b_2\), or \(i = m\) and \(b_m < b_{m - 1}\). Similarly, an element \(b_i\) (\(1\le i\le m\))... | 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 number of elements in permutation \(p\).The second line ... | For each test case, output \(n\) integers representing the elements of the permutation satisfying array \(a\).If there are multiple solutions, you may output any of them. | In the first test case, operations will be applied to permutation \([3, 2, 1]\) as follows: The only local minimum in \([3, 2, 1]\) is \(1\). Hence, elements \(3\) and \(2\) are removed. There is only one remaining element; hence the process terminates. This satisfies array \(a = [1, 1, -1]\) as both \(p_1\) and \(p_2\... | Input: 731 1 -151 -1 1 2 183 1 2 1 -1 1 1 271 1 1 -1 1 1 151 1 1 1 -15-1 1 1 1 15-1 1 2 1 2 | Output: 3 2 1 4 3 5 1 2 6 7 2 4 3 8 5 1 6 5 2 1 3 4 7 5 4 3 2 1 1 2 3 4 5 4 5 2 3 1 | Hard | 4 | 1,967 | 686 | 170 | 21 |
777 | D | 777D | D. Cloud of Hashtags | 1,800 | binary search; greedy; implementation; strings | Vasya is an administrator of a public page of organization ""Mouse and keyboard"" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hash... | The first line of the input contains a single integer n (1 β€ n β€ 500 000) β the number of hashtags being edited now.Each of the next n lines contains exactly one hashtag of positive length.It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed... | Print the resulting hashtags in any of the optimal solutions. | Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold: at first position i, such that ai β bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character than b in the first position where they differ; if... | Input: 3#book#bigtown#big | Output: #b#big#big | Medium | 4 | 1,182 | 329 | 61 | 7 |
115 | D | 115D | D. Unambiguous Arithmetic Expression | 2,600 | dp; expression parsing | Let's define an unambiguous arithmetic expression (UAE) as follows. All non-negative integers are UAE's. Integers may have leading zeroes (for example, 0000 and 0010 are considered valid integers). If X and Y are two UAE's, then ""(X) + (Y)"", ""(X) - (Y)"", ""(X) * (Y)"", and ""(X) / (Y)"" (all without the double quot... | The first line is a non-empty string consisting of digits ('0'-'9') and characters '-', '+', '*', and/or '/'. Its length will not exceed 2000. The line doesn't contain any spaces. | Print a single integer representing the number of different unambiguous arithmetic expressions modulo 1000003 (106 + 3) such that if all its brackets are removed, it becomes equal to the input string (character-by-character). | For the first example, the two possible unambiguous arithmetic expressions are:((1) + (2)) * (3)(1) + ((2) * (3))For the second example, the three possible unambiguous arithmetic expressions are:(03) + (( - (30)) + (40))(03) + ( - ((30) + (40)))((03) + ( - (30))) + (40) | Input: 1+2*3 | Output: 2 | Expert | 2 | 838 | 179 | 225 | 1 |
130 | B | 130B | B. Gnikool Ssalg | 1,400 | *special; implementation; strings | You are given a string. Reverse its characters. | The only line of input contains a string between 1 and 100 characters long. Each character of the string has ASCII-code between 33 (exclamation mark) and 126 (tilde), inclusive. | Output the characters of this string in reverse order. | Input: secrofedoc | Output: codeforces | Easy | 3 | 47 | 177 | 54 | 1 | |
1,873 | F | 1873F | F. Money Trees | 1,300 | binary search; greedy; math; two pointers | Luca is in front of a row of \(n\) trees. The \(i\)-th tree has \(a_i\) fruit and height \(h_i\).He wants to choose a contiguous subarray of the array \([h_l, h_{l+1}, \dots, h_r]\) such that for each \(i\) (\(l \leq i < r\)), \(h_i\) is divisible\(^{\dagger}\) by \(h_{i+1}\). He will collect all the fruit from each of... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first of each test case line contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq k \leq 10^9\)) β the number of trees and the maximum amount of fruits Luca can collect witho... | For each test case output a single integer, the length of the maximum length contiguous subarray satisfying the conditions, or \(0\) if there is no such subarray. | In the first test case, Luca can select the subarray with \(l=1\) and \(r=3\).In the second test case, Luca can select the subarray with \(l=3\) and \(r=4\).In the third test case, Luca can select the subarray with \(l=2\) and \(r=2\). | Input: 55 123 2 4 1 84 4 2 4 14 85 4 1 26 2 3 13 127 9 102 2 41 101117 102 6 3 1 5 10 672 24 24 12 4 4 2 | Output: 3 2 1 0 3 | Easy | 4 | 659 | 703 | 162 | 18 |
719 | B | 719B | B. Anatoly and Cockroaches | 1,400 | greedy | Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.Anatoly just made all his cockroaches to form a single line. As he is a perfectionist... | The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of cockroaches.The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively. | Print one integer β the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate. | In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0... | Input: 5rbbrr | Output: 1 | Easy | 1 | 658 | 245 | 139 | 7 |
1,548 | C | 1548C | C. The Three Little Pigs | 2,500 | combinatorics; dp; fft; math | Three little pigs from all over the world are meeting for a convention! Every minute, a triple of 3 new pigs arrives on the convention floor. After the \(n\)-th minute, the convention ends.The big bad wolf has learned about this convention, and he has an attack plan. At some minute in the convention, he will arrive and... | The first line of input contains two integers \(n\) and \(q\) (\(1 \le n \le 10^6\), \(1 \le q \le 2\cdot 10^5\)), the number of minutes the convention lasts and the number of queries the wolf asks.Each of the next \(q\) lines contains a single integer \(x_i\) (\(1 \le x_i \le 3n\)), the number of pigs the wolf will ea... | You should print \(q\) lines, with line \(i\) representing the number of attack plans if the wolf wants to eat \(x_i\) pigs. Since each query answer can be large, output each answer modulo \(10^9+7\). | In the example test, \(n=2\). Thus, there are \(3\) pigs at minute \(1\), and \(6\) pigs at minute \(2\). There are three queries: \(x=1\), \(x=5\), and \(x=6\).If the wolf wants to eat \(1\) pig, he can do so in \(3+6=9\) possible attack plans, depending on whether he arrives at minute \(1\) or \(2\).If the wolf wants... | Input: 2 3 1 5 6 | Output: 9 6 1 | Expert | 4 | 771 | 344 | 200 | 15 |
1,276 | B | 1276B | B. Two Fairs | 1,900 | combinatorics; dfs and similar; dsu; graphs | There are \(n\) cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from \(1\) to \(n\).Two fairs are currently taking place in Berland β they are held in two different cities \(a\) and \(b\)... | The first line of the input contains an integer \(t\) (\(1 \le t \le 4\cdot10^4\)) β the number of test cases in the input. Next, \(t\) test cases are specified.The first line of each test case contains four integers \(n\), \(m\), \(a\) and \(b\) (\(4 \le n \le 2\cdot10^5\), \(n - 1 \le m \le 5\cdot10^5\), \(1 \le a,b ... | Print \(t\) integers β the answers to the given test cases in the order they are written in the input. | Input: 3 7 7 3 5 1 2 2 3 3 4 4 5 5 6 6 7 7 5 4 5 2 3 1 2 2 3 3 4 4 1 4 2 4 3 2 1 1 2 2 3 4 1 | Output: 4 0 1 | Hard | 4 | 881 | 1,063 | 102 | 12 | |
489 | B | 489B | B. BerSU Ball | 1,200 | dfs and similar; dp; graph matchings; greedy; sortings; two pointers | The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair mus... | The first line contains an integer n (1 β€ n β€ 100) β the number of boys. The second line contains sequence a1, a2, ..., an (1 β€ ai β€ 100), where ai is the i-th boy's dancing skill.Similarly, the third line contains an integer m (1 β€ m β€ 100) β the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 β€ ... | Print a single number β the required maximum possible number of pairs. | Input: 41 4 6 255 1 5 7 9 | Output: 3 | Easy | 6 | 551 | 373 | 70 | 4 | |
486 | A | 486A | A. Calculating Function | 800 | implementation; math | For a positive integer n let's define a function f:f(n) = - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. | The single line contains the positive integer n (1 β€ n β€ 1015). | Print f(n) in a single line. | f(4) = - 1 + 2 - 3 + 4 = 2f(5) = - 1 + 2 - 3 + 4 - 5 = - 3 | Input: 4 | Output: 2 | Beginner | 2 | 139 | 63 | 28 | 4 |
1,611 | E2 | 1611E2 | E2. Escape The Maze (hard version) | 1,900 | dfs and similar; dp; greedy; shortest paths; trees | The only difference with E1 is the question of the problem.Vlad built a maze out of \(n\) rooms and \(n-1\) bidirectional corridors. From any room \(u\) any other room \(v\) can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited \(k\) friends to play a game with them... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. The input contains an empty string before each test case.The first line of the test case contains two numbers \(n\) and \(k\) (\(1 \le k < n \le 2\cdot 10^5\)) β the number of rooms and friends, respect... | Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be \(-1\) if Vlad wins anyway and a minimal number of friends otherwise. | In the first set of inputs, even if all the friends stay in the maze, Vlad can still win. Therefore, the answer is ""-1"".In the second set of inputs it is enough to leave friends from rooms \(6\) and \(7\). Then Vlad will not be able to win. The answer is ""2"".In the third and fourth sets of inputs Vlad cannot win on... | Input: 4 8 2 5 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 8 4 6 5 7 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 3 1 2 1 2 2 3 3 2 2 3 3 1 1 2 | Output: -1 2 1 2 | Hard | 5 | 1,308 | 888 | 188 | 16 |
920 | E | 920E | E. Connected Components? | 2,100 | data structures; dfs and similar; dsu; graphs | You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.You have t... | The first line contains two integers n and m (1 β€ n β€ 200000, ).Then m lines follow, each containing a pair of integers x and y (1 β€ x, y β€ n, x β y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). I... | Firstly print k β the number of connected components in this graph.Then print k integers β the sizes of components. You should output these integers in non-descending order. | Input: 5 51 23 43 24 22 5 | Output: 21 4 | Hard | 4 | 622 | 421 | 173 | 9 | |
553 | B | 553B | B. Kyoya and Permutation | 1,900 | binary search; combinatorics; constructive algorithms; greedy; implementation; math | Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a s... | The first line will contain two integers n, k (1 β€ n β€ 50, 1 β€ k β€ min{1018, l} where l is the length of the Kyoya's list). | Print n space-separated integers, representing the permutation that is the answer for the question. | The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | Input: 4 3 | Output: 1 3 2 4 | Hard | 6 | 1,778 | 123 | 99 | 5 |
1,548 | B | 1548B | B. Integers Have Friends | 1,800 | binary search; data structures; divide and conquer; math; number theory; two pointers | British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that ""every positive integer was one of his personal friends.""It turns out that positive integers can also be friends with each other! You are given an array \(a\) of distinct positive integers. Define a subarray \(a_i, a_{... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). Each test case begins with a line containing the integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), the size of the array \(a\).The next line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\... | Your output should consist of \(t\) lines. Each line should consist of a single integer, the size of the largest friend group in \(a\). | In the first test case, the array is \([1,5,2,4,6]\). The largest friend group is \([2,4,6]\), since all those numbers are congruent to \(0\) modulo \(2\), so \(m=2\).In the second test case, the array is \([8,2,5,10]\). The largest friend group is \([8,2,5]\), since all those numbers are congruent to \(2\) modulo \(3\... | Input: 4 5 1 5 2 4 6 4 8 2 5 10 2 1000 2000 8 465 55 3 54 234 12 45 78 | Output: 3 3 2 6 | Medium | 6 | 632 | 547 | 135 | 15 |
863 | C | 863C | C. 1-2-3 | 1,800 | graphs; implementation | Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is ""Bob"", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, ""Alice"... | The first line contains three numbers k, a, b (1 β€ k β€ 1018, 1 β€ a, b β€ 3). Then 3 lines follow, i-th of them containing 3 numbers Ai, 1, Ai, 2, Ai, 3, where Ai, j represents Alice's choice in the game if Alice chose i in previous game and Bob chose j (1 β€ Ai, j β€ 3). Then 3 lines follow, i-th of them containing 3 numb... | Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after k games. | In the second example game goes like this:The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice. | Input: 10 2 11 1 11 1 11 1 12 2 22 2 22 2 2 | Output: 1 9 | Medium | 2 | 1,309 | 459 | 143 | 8 |
1,905 | D | 1905D | D. Cyclic MEX | 2,000 | data structures; implementation; math; two pointers | For an array \(a\), define its cost as \(\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i])\).You are given a permutation\(^\ddagger\) \(p\) of the set \(\{0,1,2,\ldots,n-1\}\). Find the maximum cost across all cyclic shifts of \(p\).\(^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m])\) is the smallest no... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the length of the permutation \(p\).The second ... | For each test case, output a single integer β the maximum cost across all cyclic shifts of \(p\). | In the first test case, the cyclic shift that yields the maximum cost is \([2,1,0,5,4,3]\) with cost \(0+0+3+3+3+6=15\).In the second test case, the cyclic shift that yields the maximum cost is \([0,2,1]\) with cost \(1+1+3=5\). | Input: 465 4 3 2 1 032 1 082 3 6 7 0 1 4 510 | Output: 15 5 31 1 | Hard | 4 | 761 | 541 | 97 | 19 |
1,445 | B | 1445B | B. Elimination | 900 | greedy; math | There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.A result of the elimination stage is the total ... | You need to process \(t\) test cases.The first line contains an integer \(t\) (\(1 \leq t \leq 3025\)) β the number of test cases. Then descriptions of \(t\) test cases follow.The first line of each test case contains four integers \(a\), \(b\), \(c\), \(d\) (\(0 \le a,\,b,\,c,\,d \le 9\); \(d \leq a\); \(b \leq c\)). ... | For each test case print a single integer β the smallest possible cutoff score in some olympiad scenario satisfying the given information. | For the first test case, consider the following olympiad scenario: there are \(101\) participants in the elimination stage, each having \(1\) point for the first contest and \(2\) points for the second contest. Hence the total score of the participant on the 100-th place is \(3\).For the second test case, consider the ... | Input: 2 1 2 2 1 4 8 9 2 | Output: 3 12 | Beginner | 2 | 1,660 | 439 | 138 | 14 |
570 | A | 570A | A. Elections | 1,100 | implementation | The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each c... | The first line of the input contains two integers n, m (1 β€ n, m β€ 100) β the number of candidates and of cities, respectively.Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 β€ j β€ n, 1 β€ i β€ m, 0 β€ aij β€ 109) denotes the number of votes for candidate j in city i.It is... | Print a single number β the index of the candidate who won the elections. The candidates are indexed starting from one. | Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smal... | Input: 3 31 2 32 3 11 2 1 | Output: 2 | Easy | 1 | 815 | 402 | 119 | 5 |
462 | A | 462A | A. Appleman and Easy Task | 1,000 | brute force; implementation | Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n Γ n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the ... | The first line contains an integer n (1 β€ n β€ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces. | Print ""YES"" or ""NO"" (without the quotes) depending on the answer to the problem. | Input: 3xxoxoxoxx | Output: YES | Beginner | 2 | 360 | 190 | 84 | 4 | |
1,718 | F | 1718F | F. Burenka, an Array and Queries | 3,300 | data structures; math; number theory | Eugene got Burenka an array \(a\) of length \(n\) of integers from \(1\) to \(m\) for her birthday. Burenka knows that Eugene really likes coprime integers (integers \(x\) and \(y\) such that they have only one common factor (equal to \(1\))) so she wants to to ask Eugene \(q\) questions about the present. Each time Bu... | In the first line of input there are four integers \(n\), \(m\), \(C\), \(q\) (\(1 \leq n, q \leq 10^5\), \(1 \leq m \leq 2\cdot 10^4\), \(1 \leq C \leq 10^5\)) β the length of the array \(a\), the maximum possible value of \(a_{i}\), the value \(C\), and the number of queries.The second line contains \(n\) integers \(... | Print \(q\) integers β the answers to Burenka's queries. | Here's an explanation for the example: in the first query, the product is equal to \(1\), which is coprime with \(1,2,3,4,5\). in the second query, the product is equal to \(12\), which is coprime with \(1\) and \(5\). in the third query, the product is equal to \(10\), which is coprime with \(1\) and \(3\). | Input: 5 5 5 3 1 2 3 2 5 1 1 2 4 4 5 | Output: 5 2 2 | Master | 3 | 650 | 517 | 56 | 17 |
986 | D | 986D | D. Perfect Encoding | 3,100 | fft; math | You are working as an analyst in a company working on a new system for big data storage. This system will store \(n\) different objects. Each object should have a unique ID.To create the system, you choose the parameters of the system β integers \(m \ge 1\) and \(b_{1}, b_{2}, \ldots, b_{m}\). With these parameters an ... | In the only line of input there is one positive integer \(n\). The length of the decimal representation of \(n\) is no greater than \(1.5 \cdot 10^{6}\). The integer does not contain leading zeros. | Print one number β minimal value of \(\sum_{i=1}^{m} b_{i}\). | Input: 36 | Output: 10 | Master | 2 | 780 | 197 | 61 | 9 | |
1,846 | F | 1846F | F. Rudolph and Mimic | 1,800 | constructive algorithms; implementation; interactive | This is an interactive task.Rudolph is a scientist who studies alien life forms. There is a room in front of Rudolph with \(n\) different objects scattered around. Among the objects there is exactly one amazing creature β a mimic that can turn into any object. He has already disguised himself in this room and Rudolph n... | The first line contains one integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The first line of each test case contains one integer \(n\) \((2 \le n \le 200)\) β the number of objects in the room.The second line of each test case contains \(n\) integers \(a_1\),\(a_2\),...,\(a_n\) \((1 \le a_i \le 9)\) β ... | Explanation for the first test: initial array is \(x_1\), \(x_2\), \(x_3\), \(x_4\), \(x_5\). Mimic is in first position. Delete the fifth object. After that, the positions are shuffled, and the mimic chose not to change his appearance. Object positions become \(x_4\), \(x_1\), \(x_2\), \(x_3\). Delete the third object... | Input: 3 5 1 1 2 2 3 2 1 1 2 2 2 2 2 8 1 2 3 4 3 4 2 1 4 3 4 3 2 2 1 3 2 3 3 2 5 3 2 2 5 15 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 9 5 4 3 2 1 | Output: - 1 5 - 1 3 - 2 1 2 ! 1 - 0 - 4 1 3 7 8 - 1 4 - 1 2 ! 2 - 0 ! 10 | Medium | 3 | 1,332 | 333 | 0 | 18 | |
1,638 | B | 1638B | B. Odd Swap Sort | 1,100 | data structures; math; sortings | You are given an array \(a_1, a_2, \dots, a_n\). You can perform operations on the array. In each operation you can choose an integer \(i\) (\(1 \le i < n\)), and swap elements \(a_i\) and \(a_{i+1}\) of the array, if \(a_i + a_{i+1}\) is odd.Determine whether it can be sorted in non-decreasing order using this operati... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array.The second line of each test c... | For each test case, print ""Yes"" or ""No"" depending on whether you can or can not sort the given array.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer). | In the first test case, we can simply swap \(31\) and \(14\) (\(31 + 14 = 45\) which is odd) and obtain the non-decreasing array \([1,6,14,31]\).In the second test case, the only way we could sort the array is by swapping \(4\) and \(2\), but this is impossible, since their sum \(4 + 2 = 6\) is even.In the third test c... | Input: 441 6 31 1424 252 9 6 7 1036 6 6 | Output: Yes No No Yes | Easy | 3 | 343 | 517 | 235 | 16 |
1,607 | C | 1607C | C. Minimum Extraction | 1,000 | brute force; sortings | Yelisey has an array \(a\) of \(n\) integers.If \(a\) has length strictly greater than \(1\), then Yelisei can apply an operation called minimum extraction to it: First, Yelisei finds the minimal number \(m\) in the array. If there are several identical minima, Yelisey can choose any of them. Then the selected minimal ... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases.In the description of each test case, the first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the original length of the array \(a\). The second l... | Print \(t\) lines, each of them containing the answer to the corresponding test case. The answer to the test case is a single integer β the maximal possible minimum in \(a\), which can be obtained by several applications of the described operation to it. | In the first example test case, the original length of the array \(n = 1\). Therefore minimum extraction cannot be applied to it. Thus, the array remains unchanged and the answer is \(a_1 = 10\).In the second set of input data, the array will always consist only of zeros.In the third set, the array will be changing as ... | Input: 8 1 10 2 0 0 3 -1 2 0 4 2 10 1 7 2 2 3 5 3 2 -4 -2 0 2 -1 1 1 -2 | Output: 10 0 2 5 2 2 2 -2 | Beginner | 2 | 1,303 | 541 | 254 | 16 |
747 | C | 747C | C. Servers | 1,300 | implementation | There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id β integer from 1 to n.It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti β the moment in seconds in which the task will come, ki β the number of servers needed to pe... | The first line contains two positive integers n and q (1 β€ n β€ 100, 1 β€ q β€ 105) β the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 β€ ti β€ 106, 1 β€ ki β€ n, 1 β€ di β€ 1000) β the moment in seconds in which the i-th task will come, t... | Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. | In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third t... | Input: 4 31 3 22 2 13 4 3 | Output: 6-110 | Easy | 1 | 931 | 500 | 171 | 7 |
1,267 | B | 1267B | B. Balls of Buma | 900 | Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls).When the ball is inserted the following happens repeatedly: if some... | The only line contains a non-empty string of uppercase English letters of length at most \(3 \cdot 10^5\). Each letter represents a ball with the corresponding color. | Output the number of ways to choose a color and a position of a new ball in order to eliminate all the balls. | Input: BBWWBB | Output: 3 | Beginner | 0 | 1,220 | 166 | 109 | 12 | ||
1,038 | A | 1038A | A. Equality | 800 | implementation; strings | You are given a string \(s\) of length \(n\), which consists only of the first \(k\) letters of the Latin alphabet. All letters in string \(s\) are uppercase.A subsequence of string \(s\) is a string that can be derived from \(s\) by deleting some of its symbols without changing the order of the remaining symbols. For ... | The first line of the input contains integers \(n\) (\(1\le n \le 10^5\)) and \(k\) (\(1 \le k \le 26\)).The second line of the input contains the string \(s\) of length \(n\). String \(s\) only contains uppercase letters from 'A' to the \(k\)-th letter of Latin alphabet. | Print the only integer β the length of the longest good subsequence of string \(s\). | In the first example, ""ACBCAB"" (""ACAABCCAB"") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence ""CAB"" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is \(... | Input: 9 3ACAABCCAB | Output: 6 | Beginner | 2 | 581 | 272 | 84 | 10 |
1,338 | C | 1338C | C. Perfect Triples | 2,200 | bitmasks; brute force; constructive algorithms; divide and conquer; math | Consider the infinite sequence \(s\) of positive integers, created by repeating the following steps: Find the lexicographically smallest triple of positive integers \((a, b, c)\) such that \(a \oplus b \oplus c = 0\), where \(\oplus\) denotes the bitwise XOR operation. \(a\), \(b\), \(c\) are not in \(s\). Here triple ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(1\le n \le 10^{16}\)) β the position of the element you want to know. | In each of the \(t\) lines, output the answer to the corresponding test case. | The first elements of \(s\) are \(1, 2, 3, 4, 8, 12, 5, 10, 15, \dots \) | Input: 9 1 2 3 4 5 6 7 8 9 | Output: 1 2 3 4 8 12 5 10 15 | Hard | 5 | 915 | 230 | 77 | 13 |
1,439 | C | 1439C | C. Greedy Shopping | 2,600 | binary search; data structures; divide and conquer; greedy; implementation | You are given an array \(a_1, a_2, \ldots, a_n\) of integers. This array is non-increasing.Let's consider a line with \(n\) shops. The shops are numbered with integers from \(1\) to \(n\) from left to right. The cost of a meal in the \(i\)-th shop is equal to \(a_i\).You should process \(q\) queries of two types: 1 x y... | The first line contains two integers \(n\), \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_{1},a_{2}, \ldots, a_{n}\) \((1 \leq a_{i} \leq 10^9)\) β the costs of the meals. It is guaranteed, that \(a_1 \geq a_2 \geq \ldots \geq a_n\).Each of the next \(q\) lines contains three int... | For each query of type \(2\) output the answer on the new line. | In the first query a hungry man will buy meals in all shops from \(3\) to \(10\).In the second query a hungry man will buy meals in shops \(4\), \(9\), and \(10\).After the third query the array \(a_1, a_2, \ldots, a_n\) of costs won't change and will be \(\{10, 10, 10, 6, 6, 5, 5, 5, 3, 1\}\).In the fourth query a hun... | Input: 10 6 10 10 10 6 6 5 5 5 3 1 2 3 50 2 4 10 1 3 10 2 2 36 1 4 7 2 2 17 | Output: 8 3 6 2 | Expert | 5 | 717 | 512 | 63 | 14 |
2,068 | E | 2068E | E. Porto Vs. Benfica | 2,800 | data structures; dfs and similar; dsu; graphs; shortest paths | FC Porto and SL Benfica are the two largest football teams in Portugal. Naturally, when the two play each other, a lot of people travel from all over the country to watch the game. This includes the Benfica supporters' club, which is going to travel from Lisbon to Porto to watch the upcoming game. To avoid tensions bet... | The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 200\,000\), \(n - 1 \leq m \leq \min\{n(n - 1)/2, 200\,000\}\)) β the number of towns and the number of roads in the road network of Portugal.Each of the next \(m\) lines contains two integers \(s_i\) and \(t_i\) (\(1 \leq s_i, t_i \leq n\)) β the tw... | Print the minimum number of roads the supporters' club needs to traverse to travel from Lisbon to Porto. | In the first sample, the road network is represented by the following picture: Note that the optimal strategy for the police is to wait until the supporters' club is on a vertex adjacent to the destination (i.e., vertex \(5\)) and then block the edge that connects that vertex to the destination. The optimal strategy fo... | Input: 5 51 21 32 53 44 5 | Output: 5 | Master | 5 | 1,772 | 488 | 104 | 20 |
1,978 | F | 1978F | F. Large Graph | 2,400 | data structures; dfs and similar; dsu; graphs; number theory; two pointers | Given an array \(a\) of length \(n\). Let's construct a square matrix \(b\) of size \(n \times n\), in which the \(i\)-th row contains the array \(a\) cyclically shifted to the right by \((i - 1)\). For example, for the array \(a = [3, 4, 5]\), the obtained matrix is$$$\(b = \begin{bmatrix} 3 & 4 & 5 \\ 5 & 3 & 4 \\ 4 ... | Each test 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 first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 10^6\), \(2 \le k \le 2 \cdot 10^6\)) β the lengt... | For each test case, output a single integer β the number of connected components in the obtained graph. | In the first test case, the matrix \(b\) is given in the statement. The first connected component contains the vertices \((1, 1)\), \((2, 2)\), and \((3, 3)\). The second connected component contains the vertices \((1, 2)\), \((2, 3)\), and \((3, 1)\). The third connected component contains the vertices \((1, 3)\), \((... | Input: 63 33 4 53 33 4 93 23 4 92 22 85 38 27 5 4 34 102 2 2 2 | Output: 3 2 3 1 4 1 | Expert | 6 | 1,090 | 587 | 103 | 19 |
753 | C | 753C | C. Interactive Bulls and Cows (Hard) | 2,500 | brute force; constructive algorithms; interactive | The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get resp... | To read answers to the queries, the program must use the standard input.The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string s and the string xi printed by your program. If the system... | The program must use the standard output to print queries.Your program must output requests β 4-digit strings x1, x2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.Your program is allowed to do at most 7... | The secret string s in the example is ""0123"". | Input: 0 12 01 10 42 14 0 | Output: 800001793159321001120123 | Expert | 3 | 2,751 | 610 | 329 | 7 |
1,413 | B | 1413B | B. A New Technique | 1,100 | implementation | All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of \(n\cdot m\) different seals, denoted by distinct numbers. All of them were written in an \(n\times m\) table.The table is lost now. Naruto managed to remember elements of each row from left to r... | The first line of the input contains the only integer \(t\) (\(1\leq t\leq 100\,000\)) denoting the number of test cases. Their descriptions follow.The first line of each test case description consists of two space-separated integers \(n\) and \(m\) (\(1 \leq n, m \leq 500\)) standing for the number of rows and columns... | For each test case, output \(n\) lines with \(m\) space-separated integers each, denoting the restored table. One can show that the answer is always unique. | Consider the first test case. The matrix is \(2 \times 3\). You are given the rows and columns in arbitrary order.One of the rows is \([6, 5, 4]\). One of the rows is \([1, 2, 3]\).One of the columns is \([1, 6]\). One of the columns is \([2, 5]\). One of the columns is \([3, 4]\).You are to reconstruct the matrix. The... | Input: 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 | Output: 1 2 3 6 5 4 3 1 2 | Easy | 1 | 535 | 1,048 | 156 | 14 |
1,994 | H | 1994H | H. Fortnite | 3,500 | combinatorics; constructive algorithms; games; greedy; hashing; interactive; math; number theory; strings | This is an interactive problem!Timofey is writing a competition called Capture the Flag (or CTF for short). He has one task left, which involves hacking a security system. The entire system is based on polynomial hashes\(^{\text{β}}\).Timofey can input a string consisting of lowercase Latin letters into the system, and... | Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.It is guaranteed that the \(p\) and \(m\) used by the system satisfy the conditions: \(26 < p \leq 50\) and \(p + 1 < m \leq 2 \cdot 10^9\). | Answer for the first query is \((ord(a) \cdot 31^0 + ord(a) \cdot 31^1) \mod 59 = (1 + 1 \cdot 31) \mod 59 = 32\).Answer for the second query is \((ord(y) \cdot 31^0 + ord(b) \cdot 31^1) \mod 59 = (25 + 2 \cdot 31) \mod 59 = 28\). | Input: 1 32 28 | Output: ? aa ? yb ! 31 59 | Master | 9 | 1,135 | 276 | 0 | 19 | |
490 | D | 490D | D. Chocolate | 1,900 | brute force; dfs and similar; math; meet-in-the-middle; number theory | Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a1 Γ b1 segments large and the second one is a2 Γ b2 segments large.Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. ... | The first line of the input contains integers a1, b1 (1 β€ a1, b1 β€ 109) β the initial sizes of the first chocolate bar. The second line of the input contains integers a2, b2 (1 β€ a2, b2 β€ 109) β the initial sizes of the second bar.You can use the data of type int64 (in Pascal), long long (in Π‘++), long (in Java) to pro... | In the first line print m β the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must c... | Input: 2 62 3 | Output: 11 62 3 | Hard | 5 | 1,497 | 360 | 513 | 4 | |
1,681 | B | 1681B | B. Card Trick | 800 | implementation; math | Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of \(n\) cards. You see that the values of cards from the topmost to the bottommost are integers \(a_1, a_2, \dots, a_n\), and all values are different.Then he asks you to shuffle the deck \(m\) times. With the... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of cards in the deck.The second line contains \(n\) pairwise distinct integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i... | For each testcase, print a single integer β the value of the card on the top of the deck after the deck is shuffled \(m\) times. | In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be \([2, 1]\).In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck r... | Input: 321 231 1 143 1 4 223 152 1 5 4 353 2 1 2 1 | Output: 2 3 3 | Beginner | 2 | 711 | 748 | 128 | 16 |
1,083 | E | 1083E | E. The Fair Nut and Rectangles | 2,400 | data structures; dp; geometry | The Fair Nut got stacked in planar world. He should solve this task to get out.You are given \(n\) rectangles with vertexes in \((0, 0)\), \((x_i, 0)\), \((x_i, y_i)\), \((0, y_i)\). For each rectangle, you are also given a number \(a_i\). Choose some of them that the area of union minus sum of \(a_i\) of the chosen on... | The first line contains one integer \(n\) (\(1 \leq n \leq 10^6\)) β the number of rectangles.Each of the next \(n\) lines contains three integers \(x_i\), \(y_i\) and \(a_i\) (\(1 \leq x_i, y_i \leq 10^9\), \(0 \leq a_i \leq x_i \cdot y_i\)).It is guaranteed that there are no nested rectangles. | In a single line print the answer to the problem β the maximum value which you can achieve. | In the first example, the right answer can be achieved by choosing the first and the second rectangles.In the second example, the right answer can also be achieved by choosing the first and the second rectangles. | Input: 3 4 4 8 1 5 0 5 2 10 | Output: 9 | Expert | 3 | 453 | 296 | 91 | 10 |
1,627 | C | 1627C | C. Not Assigning | 1,400 | constructive algorithms; dfs and similar; number theory; trees | You are given a tree of \(n\) vertices numbered from \(1\) to \(n\), with edges numbered from \(1\) to \(n-1\). A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of eve... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^5\)) β the number of vertices in the tree.Then, \(n-1\) lines... | For each test case, if a valid assignment exists, then print a single line containing \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\) (\(1 \leq a_i \le 10^5\)), where \(a_i\) denotes the weight assigned to the edge numbered \(i\). Otherwise, print \(-1\).If there are multiple solutions, you may print any. | For the first test case, there are only two paths having one edge each: \(1 \to 2\) and \(2 \to 1\), both having a weight of \(17\), which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case. | Input: 321 241 34 32 171 21 33 43 56 27 2 | Output: 17 2 5 11 -1 | Easy | 4 | 1,024 | 607 | 304 | 16 |
515 | D | 515D | D. Drazil and Tiles | 2,000 | constructive algorithms; greedy | Drazil created a following problem about putting 1 Γ 2 tiles into an n Γ m grid:""There is a grid with some cells that are empty and some cells that are occupied. You should use 1 Γ 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it.""But Drazil... | The first line contains two integers n and m (1 β€ n, m β€ 2000).The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied. | If there is no solution or the solution is not unique, you should print the string ""Not unique"".Otherwise you should print how to cover all empty cells with 1 Γ 2 tiles. Use characters ""<>"" to denote horizontal tiles and characters ""^v"" to denote vertical tiles. Refer to the sample test for the output format exam... | In the first case, there are indeed two solutions:<>^^*vv<>and^<>v*^<>vso the answer is ""Not unique"". | Input: 3 3....*.... | Output: Not unique | Hard | 2 | 824 | 200 | 324 | 5 |
744 | C | 744C | C. Hongcow Buys a Deck of Cards | 2,400 | bitmasks; brute force; dp | One day, Hongcow goes to the store and sees a brand new deck of n special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.This game takes some number of turns to complete. On a turn, Hongcow may do one of two... | The first line of input will contain a single integer n (1 β€ n β€ 16).The next n lines of input will contain three tokens ci, ri and bi. ci will be 'R' or 'B', denoting the color of the card as red or blue. ri will be an integer denoting the amount of red resources required to obtain the card, and bi will be an integer ... | Output a single integer, denoting the minimum number of turns needed to acquire all the cards. | For the first sample, Hongcow's four moves are as follows: Collect tokens Buy card 1 Buy card 2 Buy card 3 Note, at the fourth step, Hongcow is able to buy card 3 because Hongcow already has one red and one blue card, so we don't need to collect tokens.For the second sample, one optimal strategy is as follows: Collect ... | Input: 3R 0 1B 1 0R 1 1 | Output: 4 | Expert | 3 | 1,010 | 405 | 94 | 7 |
2,124 | G | 2124G | G. Maximise Sum | 3,200 | binary search; data structures | This problem differs from problem B. In this problem, you must output the maximum sum of prefix minimums over all operations that cost at least \(x\) for each integer \(x\) from \(0\) to \(n-1\).You are given an array \(a\) of length \(n\), with elements satisfying \(\boldsymbol{0 \le a_i \le n}\). You can perform the ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 10^6\)) β the length of \(a\).The following line contains \(n\) space-separated in... | For each test case, output \(n\) integers on a new line: the \(i\)-th integer denoting the maximum answer over all operations that have a cost of at least \(i-1\). | Let's analyze the fifth test case: \(x=0,1,2\): the optimal operation is \(i=2\) and \(j=4\), which has a cost of \(2\). The array \(a\) becomes \([4,4,3,0,1]\), which has a score of \(11\). \(x=3\): the optimal operation is \(i=2\) and \(j=5\), which has a cost of \(3\). The array \(a\) becomes \([4,2,3,3,0]\), which ... | Input: 621 242 3 1 421 055 5 0 5 554 1 3 3 1107 4 7 0 8 7 5 0 2 1 | Output: 3 3 10 10 10 10 1 1 20 20 20 15 15 11 11 11 10 8 27 27 27 27 23 23 20 19 17 16 | Master | 2 | 789 | 486 | 163 | 21 |
1,452 | D | 1452D | D. Radio Towers | 1,600 | combinatorics; dp; math | There are \(n + 2\) towns located on a coordinate line, numbered from \(0\) to \(n + 1\). The \(i\)-th town is located at the point \(i\).You build a radio tower in each of the towns \(1, 2, \dots, n\) with probability \(\frac{1}{2}\) (these events are independent). After that, you want to set the signal power on each ... | The first (and only) line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)). | Print one integer β the probability that there will be a way to set signal powers so that all constraints are met, taken modulo \(998244353\).Formally, the probability can be expressed as an irreducible fraction \(\frac{x}{y}\). You have to print the value of \(x \cdot y^{-1} \bmod 998244353\), where \(y^{-1}\) is an i... | The real answer for the first example is \(\frac{1}{4}\): with probability \(\frac{1}{4}\), the towers are built in both towns \(1\) and \(2\), so we can set their signal powers to \(1\). The real answer for the second example is \(\frac{1}{4}\): with probability \(\frac{1}{8}\), the towers are built in towns \(1\), \(... | Input: 2 | Output: 748683265 | Medium | 3 | 1,402 | 97 | 376 | 14 |
429 | A | 429A | A. Xor-tree | 1,300 | dfs and similar; trees | Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.The game is played on a tree having n nodes, numbered from 1 to n. Each node i has an initial value initi, which is... | The first line contains an integer n (1 β€ n β€ 105). Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ n; ui β vi) meaning there is an edge between nodes ui and vi. The next line contains n integer numbers, the i-th of them corresponds to initi (initi is either 0 or 1). The following line also c... | In the first line output an integer number cnt, representing the minimal number of operations you perform. Each of the next cnt lines should contain an integer xi, representing that you pick a node xi. | Input: 102 13 14 25 16 27 58 69 810 51 0 1 1 0 1 0 1 0 11 0 1 0 0 1 1 1 0 1 | Output: 247 | Easy | 2 | 890 | 409 | 201 | 4 | |
1,951 | F | 1951F | F. Inversion Composition | 2,500 | constructive algorithms; data structures; greedy | My Chemical Romance - DisenchantedΰΆYou are given a permutation \(p\) of size \(n\), as well as a non-negative integer \(k\). You need to construct a permutation \(q\) of size \(n\) such that \(\operatorname{inv}(q) + \operatorname{inv}(q \cdot p) = k {}^\dagger {}^\ddagger\), or determine if it is impossible to do so.\... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5, 0 \le k \le n(n - 1)\)) β the size of \(p\) an... | For each test case, print on one line ""YES"" if there exists a permutation \(q\) that satisfies the given condition, or ""NO"" if there is no such permutation.If the answer is ""YES"", on the second line, print \(n\) integers \(q_1, q_2, \ldots, q_n\) that represent such a satisfactory permutation \(q\). If there are ... | In the first test case, we have \(q \cdot p = [2, 1, 3]\), \(\operatorname{inv}(q) = 3\), and \(\operatorname{inv}(q \cdot p) = 1\).In the fourth test case, we have \(q \cdot p = [9, 1, 8, 5, 7, 6, 4, 3, 2]\), \(\operatorname{inv}(q) = 24\), and \(\operatorname{inv}(q \cdot p) = 27\). | Input: 53 42 3 15 52 3 5 1 46 115 1 2 3 4 69 513 1 4 2 5 6 7 8 91 01 | Output: YES 3 2 1 NO NO YES 1 5 9 8 7 6 4 3 2 YES 1 | Expert | 3 | 705 | 615 | 361 | 19 |
919 | A | 919A | A. Supermarket | 800 | brute force; greedy; implementation | We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that \(a\) yuan for \(b\) kilos (You don't need to care about what ""yuan"" is), the same as \(a/b\) yuan for a kilo.Now imagine... | The first line contains two positive integers \(n\) and \(m\) (\(1 \leq n \leq 5\,000\), \(1 \leq m \leq 100\)), denoting that there are \(n\) supermarkets and you want to buy \(m\) kilos of apples.The following \(n\) lines describe the information of the supermarkets. Each line contains two positive integers \(a, b\) ... | The only line, denoting the minimum cost for \(m\) kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed \(10^{-6}\).Formally, let your answer be \(x\), and the jury's answer be \(y\). Your answer is considered correct if \(\frac{|x - y|}{\max{(1, ... | In the first sample, you are supposed to buy \(5\) kilos of apples in supermarket \(3\). The cost is \(5/3\) yuan.In the second sample, you are supposed to buy \(1\) kilo of apples in supermarket \(2\). The cost is \(98/99\) yuan. | Input: 3 51 23 41 3 | Output: 1.66666667 | Beginner | 3 | 517 | 444 | 341 | 9 |
1,504 | B | 1504B | B. Flip the Bits | 1,200 | constructive algorithms; greedy; implementation; math | There is a binary string \(a\) of length \(n\). In one operation, you can select any prefix of \(a\) with an equal number of \(0\) and \(1\) symbols. Then all symbols in the prefix are inverted: each \(0\) becomes \(1\) and each \(1\) becomes \(0\).For example, suppose \(a=0111010000\). In the first operation, we can s... | The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 3\cdot 10^5\)) β the length of the strings \(a\) and \(b\).The following two lines contain strings \(a\) and \(b\) of length \(n\), consisting of s... | For each test case, output ""YES"" if it is possible to transform \(a\) into \(b\), or ""NO"" if it is impossible. You can print each letter in any case (upper or lower). | The first test case is shown in the statement.In the second test case, we transform \(a\) into \(b\) by using zero operations.In the third test case, there is no legal operation, so it is impossible to transform \(a\) into \(b\).In the fourth test case, here is one such transformation: Select the length \(2\) prefix to... | Input: 5 10 0111010000 0100101100 4 0000 0000 3 001 000 12 010101010101 100110011010 6 000111 110100 | Output: YES YES NO YES NO | Easy | 4 | 802 | 414 | 170 | 15 |
1,635 | F | 1635F | F. Closest Pair | 2,800 | data structures; greedy | There are \(n\) weighted points on the \(OX\)-axis. The coordinate and the weight of the \(i\)-th point is \(x_i\) and \(w_i\), respectively. All points have distinct coordinates and positive weights. Also, \(x_i < x_{i + 1}\) holds for any \(1 \leq i < n\). The weighted distance between \(i\)-th point and \(j\)-th poi... | The first line contains 2 integers \(n\) and \(q\) \((2 \leq n \leq 3 \cdot 10^5; 1 \leq q \leq 3 \cdot 10^5)\) β the number of points and the number of queries.Then, \(n\) lines follows, the \(i\)-th of them contains two integers \(x_i\) and \(w_i\) \((-10^9 \leq x_i \leq 10^9; 1 \leq w_i \leq 10^9)\) β the coordinate... | For each query output one integer, the minimum weighted distance among all pair of distinct points in the given subarray. | For the first query, the minimum weighted distance is between points \(1\) and \(3\), which is equal to \(|x_1 - x_3| \cdot (w_1 + w_3) = |-2 - 1| \cdot (2 + 1) = 9\).For the second query, the minimum weighted distance is between points \(2\) and \(3\), which is equal to \(|x_2 - x_3| \cdot (w_2 + w_3) = |0 - 1| \cdot ... | Input: 5 5 -2 2 0 10 1 1 9 2 12 7 1 3 2 3 1 5 3 5 2 4 | Output: 9 11 9 24 11 | Master | 2 | 617 | 597 | 121 | 16 |
1,218 | E | 1218E | E. Product Tuples | 2,500 | divide and conquer; fft | While roaming the mystic areas of Stonefalls, in order to drop legendary loot, an adventurer was given a quest as follows. He was given an array \(A = {a_1,a_2,...,a_N }\) of length \(N\), and a number \(K\).Define array \(B\) as \(B(q, A) = \) { \(q-a_1, q-a_2, ..., q-a_N\) }. Define function \(F\) as \(F(B,K)\) being... | In the first two lines, numbers \(N\) (\(1 \leq N \leq 2*10^4\)) and \(K\) (\(1 \leq K \leq N\)), the length of initial array \(A\), and tuple size, followed by \(a_1,a_2,a_3,β¦,a_N\) (\(0 \leq a_i \leq 10^9\)) , elements of array \(A\), in the next line. Then follows number \(Q\) (\(Q \leq 10\)), number of queries. In ... | Print \(Q\) lines, the answers to queries, modulo \(998244353\). | In the first query array A = [1, 2, 3, 4, 5], B = [5, 4, 3, 2, 1], sum of products of 2-tuples = 85.In second query array A = [1, 2, 3, 4, 2], B = [5, 4, 3, 2, 4], sum of products of 2-tuples = 127In third query array A = [1, 3, 4, 4, 5], B = [5, 3, 2, 2, 1], sum of products of 2-tuples = 63 | Input: 5 2 1 2 3 4 5 3 1 6 1 1 1 6 5 2 2 6 2 3 1 | Output: 85 127 63 | Expert | 2 | 1,065 | 478 | 64 | 12 |
1,063 | F | 1063F | F. String Journey | 3,300 | data structures; dp; string suffix structures | We call a sequence of strings t1, ..., tk a journey of length k, if for each i > 1 ti is a substring of ti - 1 and length of ti is strictly less than length of ti - 1. For example, {ab, b} is a journey, but {ab, c} and {a, a} are not.Define a journey on string s as journey t1, ..., tk, such that all its parts can be ne... | The first line contains a single integer n (1 β€ n β€ 500 000) β the length of string s.The second line contains the string s itself, consisting of n lowercase Latin letters. | Print one number β the maximum possible length of string journey on s. | In the first sample, the string journey of maximum length is {abcd, bc, c}.In the second sample, one of the suitable journeys is {bb, b}. | Input: 7abcdbcc | Output: 3 | Master | 3 | 757 | 172 | 70 | 10 |
1,991 | G | 1991G | G. Grid Reset | 2,700 | constructive algorithms; greedy; implementation | You are given a grid consisting of \(n\) rows and \(m\) columns, where each cell is initially white. Additionally, you are given an integer \(k\), where \(1 \le k \le \min(n, m)\).You will process \(q\) operations of two types: \(\mathtt{H}\) (horizontal operation) β You choose a \(1 \times k\) rectangle completely wit... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains four integers \(n\), \(m\), \(k\), and \(q\) (\(1 \le n, m \le 100\), \(1 \le k \le \min(n, m)\), \(1 ... | For each test case, output a single integer \(-1\) if it is impossible to perform all the operations.Otherwise, output \(q\) lines. Each line contains two integers \(i\), \(j\) (\(1 \le i \le n\), \(1 \le j \le m\)) β the coordinates of the top-left cell of the operation rectangle.If there are multiple solutions, outpu... | Illustration of example. The first operation is horizontal. The operation rectangle starts at \((1,1)\) and is a \(1 \times 3\) rectangle. After the operation, cells \((1,1)\), \((1,2)\), and \((1,3)\) become black.The second operation is vertical. The operation rectangle starts at \((2,1)\) and is a \(3 \times 1\) rec... | Input: 14 5 3 6HVVHHV | Output: 1 1 2 1 1 1 2 3 3 3 2 2 | Master | 3 | 1,036 | 721 | 334 | 19 |
1,181 | A | 1181A | A. Chunga-Changa | 1,000 | greedy; math | Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called ""chizhik"". One has to pay in chizhiks to buy a coconut now.Sasha and Masha are about to buy some coconuts which are sold at price \(z\) chizhiks per c... | The first line contains three integers \(x\), \(y\) and \(z\) (\(0 \le x, y \le 10^{18}\), \(1 \le z \le 10^{18}\)) β the number of chizhics Sasha has, the number of chizhics Masha has and the price of a coconut. | Print two integers: the maximum possible number of coconuts the girls can buy and the minimum number of chizhiks one girl has to give to the other. | The first example is described in the statement. In the second example the optimal solution is to dot exchange any chizhiks. The girls will buy \(3 + 4 = 7\) coconuts. | Input: 5 4 3 | Output: 3 1 | Beginner | 2 | 1,605 | 212 | 147 | 11 |
1,179 | C | 1179C | C. Serge and Dining Room | 2,200 | binary search; data structures; graph matchings; greedy; implementation; math; trees | Serge came to the school dining room and discovered that there is a big queue here. There are \(m\) pupils in the queue. He's not sure now if he wants to wait until the queue will clear, so he wants to know which dish he will receive if he does. As Serge is very tired, he asks you to compute it instead of him.Initially... | The first line contains integers \(n\) and \(m\) (\(1 \leq n, m \leq 300\ 000\)) β number of dishes and pupils respectively. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^{6}\)) β elements of array \(a\). The third line contains \(m\) integers \(b_1, b_2, \ldots, b_{m}\) (\(1 \... | For each of \(q\) queries prints the answer as the statement describes, the answer of the \(i\)-th query in the \(i\)-th line (the price of the dish which Serge will buy or \(-1\) if nothing remains) | In the first sample after the first query, there is one dish with price \(100\) togrogs and one pupil with one togrog, so Serge will buy the dish with price \(100\) togrogs.In the second sample after the first query, there is one dish with price one togrog and one pupil with \(100\) togrogs, so Serge will get nothing.I... | Input: 1 1 1 1 1 1 1 100 | Output: 100 | Hard | 7 | 1,717 | 873 | 199 | 11 |
50 | A | 50A | A. Domino piling | 800 | greedy; math | You are given a rectangular board of M Γ N squares. Also you are given an unlimited number of standard domino pieces of 2 Γ 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:1. Each domino completely covers two square... | In a single line you are given two integers M and N β board sizes in squares (1 β€ M β€ N β€ 16). | Output one number β the maximal number of dominoes, which can be placed. | Input: 2 4 | Output: 4 | Beginner | 2 | 524 | 94 | 72 | 0 | |
40 | A | 40A | A. Find Color | 1,300 | constructive algorithms; geometry; implementation; math | Not so long ago as a result of combat operations the main Berland place of interest β the magic clock β was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin correspond... | The first and single line contains two integers x and y β the coordinates of the hole made in the clock by the ball. Each of the numbers x and y has an absolute value that does not exceed 1000. | Find the required color.All the points between which and the origin of coordinates the distance is integral-value are painted black. | Input: -2 1 | Output: white | Easy | 4 | 715 | 193 | 132 | 0 | |
746 | B | 746B | B. Decoding | 900 | implementation; strings | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, inf... | The first line contains a positive integer n (1 β€ n β€ 2000) β the length of the encoded word.The second line contains the string s of length n consisting of lowercase English letters β the encoding. | Print the word that Polycarp encoded. | In the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word bec... | Input: 5logva | Output: volga | Beginner | 2 | 715 | 198 | 37 | 7 |
809 | E | 809E | E. Surprise me! | 3,100 | divide and conquer; math; number theory; trees | Tired of boring dates, Leha and Noora decided to play a game.Leha found a tree with n vertices numbered from 1 to n. We remind you that tree is an undirected graph without cycles. Each vertex v of a tree has a number av written on it. Quite by accident it turned out that all values written on vertices are distinct and ... | The first line of input contains one integer number n (2 β€ n β€ 2Β·105) β number of vertices in a tree.The second line contains n different numbers a1, a2, ..., an (1 β€ ai β€ n) separated by spaces, denoting the values written on a tree vertices.Each of the next n - 1 lines contains two integer numbers x and y (1 β€ x, y β€... | In a single line print a number equal to PΒ·Q - 1 modulo 109 + 7. | Euler's totient function Ο(n) is the number of such i that 1 β€ i β€ n,and gcd(i, n) = 1, where gcd(x, y) is the greatest common divisor of numbers x and y.There are 6 variants of choosing vertices by Leha and Noora in the first testcase: u = 1, v = 2, f(1, 2) = Ο(a1Β·a2)Β·d(1, 2) = Ο(1Β·2)Β·1 = Ο(2) = 1 u = 2, v = 1, f(2, 1... | Input: 31 2 31 22 3 | Output: 333333338 | Master | 4 | 1,302 | 418 | 64 | 8 |
501 | E | 501E | E. Misha and Palindrome Degree | 2,500 | binary search; combinatorics; implementation | Misha has an array of n integers indexed by integers from 1 to n. Let's define palindrome degree of array a as the number of such index pairs (l, r)(1 β€ l β€ r β€ n), that the elements from the l-th to the r-th one inclusive can be rearranged in such a way that the whole array will be a palindrome. In other words, pair (... | The first line contains integer n (1 β€ n β€ 105).The second line contains n positive integers a[i] (1 β€ a[i] β€ n), separated by spaces β the elements of Misha's array. | In a single line print the answer to the problem. | In the first sample test any possible pair (l, r) meets the condition.In the third sample test following pairs (1, 3), (1, 4), (1, 5), (2, 5) meet the condition. | Input: 32 2 2 | Output: 6 | Expert | 3 | 606 | 166 | 49 | 5 |
1,178 | B | 1178B | B. WOW Factor | 1,300 | dp; strings | Recall that string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly zero or all) characters. For example, for the string \(a\)=""wowwo"", the following strings are subsequences: ""wowwo"", ""wowo"", ""oo"", ""wow"", """", and others, but the following are not... | The input contains a single non-empty string \(s\), consisting only of characters ""v"" and ""o"". The length of \(s\) is at most \(10^6\). | Output a single integer, the wow factor of \(s\). | The first example is explained in the legend. | Input: vvvovvv | Output: 4 | Easy | 2 | 1,306 | 139 | 49 | 11 |
724 | E | 724E | E. Goods transportation | 2,900 | dp; flows; greedy | There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road.The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city.For each pair of cities i and j such that 1 β€ i < j β€ n you can no more than once transport no more th... | The first line of the input contains two integers n and c (1 β€ n β€ 10 000, 0 β€ c β€ 109) β the number of cities and the maximum amount of goods for a single transportation.The second line contains n integers pi (0 β€ pi β€ 109) β the number of units of goods that were produced in each city.The third line of input contains... | Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations. | Input: 3 01 2 33 2 1 | Output: 4 | Master | 3 | 655 | 411 | 116 | 7 | |
1,973 | F | 1973F | F. Maximum GCD Sum Queries | 3,100 | bitmasks; brute force; dp; implementation; number theory | For \(k\) positive integers \(x_1, x_2, \ldots, x_k\), the value \(\gcd(x_1, x_2, \ldots, x_k)\) is the greatest common divisor of the integers \(x_1, x_2, \ldots, x_k\) β the largest integer \(z\) such that all the integers \(x_1, x_2, \ldots, x_k\) are divisible by \(z\).You are given three arrays \(a_1, a_2, \ldots,... | There are two integers on the first line β the numbers \(n\) and \(q\) (\(1 \leq n \leq 5 \cdot 10^5\), \(1 \leq q \leq 5 \cdot 10^5\)).On the second line, there are \(n\) integers β the numbers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^8\)).On the third line, there are \(n\) integers β the numbers \(b_1, b_2, \l... | Print \(q\) integers β the maximum value you can get for each of the \(q\) possible values \(d\). | In the first query of the first example, we are not allowed to do any swaps at all, so the answer is \(\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2\). In the second query, one of the ways to achieve the optimal value is to swap \(a_2\) and \(b_2\), then the answer is \(\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3\).In the second query of th... | Input: 3 41 2 34 5 61 1 10 1 2 3 | Output: 2 3 3 3 | Master | 5 | 899 | 585 | 97 | 19 |
2,128 | C | 2128C | C. Leftmost Below | 1,200 | greedy; math | Consider an array \(a_1, \ldots, a_n\). Initially, \(a_i = 0\) for every \(i\).You can do operations of the following form. You choose an integer \(x\) greater than \(\min(a)\). Then, \(i\) is defined as the minimum index such that \(a_i < x\). In other words, \(i\) is the unique integer between \(1\) and \(n\) inclusi... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 200\,000\)).The second line of each test case contains \(n\) integers \(b... | For each test case, print YES if you can reach the target array and NO otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, we can do the following sequence of operations: we choose \(x=2\), \(a\) becomes \([2, 0, 0, 0]\) we choose \(x=2\), \(a\) becomes \([2, 2, 0, 0]\) we choose \(x=3\), \(a\) becomes \([5, 2, 0, 0]\) we choose \(x=4\), \(a\) becomes \([5, 6, 0, 0]\) we choose \(x=1\), \(a\) becomes \([5, 6, 1, 0]\... | Input: 445 6 1 133 1 2340 60 9021 1 | Output: YES NO NO YES | Easy | 2 | 724 | 434 | 242 | 21 |
1,599 | G | 1599G | G. Shortest path | 2,700 | brute force; geometry; math; shortest paths | You are given \(N\) points on an infinite plane with the Cartesian coordinate system on it. \(N-1\) points lay on one line, and one point isn't on that line. You are on point \(K\) at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. Wha... | The first line contains two integers: \(N\) (\(3 \leq N \leq 2*10^5\)) - the number of points, and \(K\) (\(1 \leq K \leq N\)) - the index of the starting point.Each of the next \(N\) lines contain two integers, \(A_i\), \(B_i\) (\(-10^6 \leq A_i, B_i \leq 10^6\)) - coordinates of the \(i-th\) point. | The output contains one number - the shortest path to visit all given points starting from point \(K\). The absolute difference between your solution and the main solution shouldn't exceed \(10^-6\); | The shortest path consists of these moves: 2 -> 5 5 -> 4 4 -> 1 1 -> 3 There isn't any shorter path possible. | Input: 5 2 0 0 -1 1 2 -2 0 1 -2 2 | Output: 7.478709 | Master | 4 | 356 | 301 | 199 | 15 |
1,532 | F | 1532F | F. Prefixes and Suffixes | 0 | *special; strings | Ivan wants to play a game with you. He picked some string \(s\) of length \(n\) consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from \(1\) to \(n-1\)), but he didn't tell you which strings a... | The first line of the input contains one integer number \(n\) (\(2 \le n \le 100\)) β the length of the guessed string \(s\).The next \(2n-2\) lines are contain prefixes and suffixes, one per line. Each of them is the string of length from \(1\) to \(n-1\) consisting only of lowercase Latin letters. They can be given i... | Print one string of length \(2n-2\) β the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The \(i\)-th character of this string should be 'P' if the \(i\)-th of the input strings is the prefix and 'S' otherwise.If there are several possible... | The only string which Ivan can guess in the first example is ""ababa"".The only string which Ivan can guess in the second example is ""aaa"". Answers ""SPSP"", ""SSPP"" and ""PSPS"" are also acceptable.In the third example Ivan can guess the string ""ac"" or the string ""ca"". The answer ""SP"" is also acceptable. | Input: 5 ba a abab a aba baba ab aba | Output: SPPSPSPS | Beginner | 2 | 710 | 537 | 348 | 15 |
235 | B | 235B | B. Let's Play Osu! | 2,000 | dp; math; probabilities | You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as ""O"", bad as ""X"", then the whole play can be encoded as a sequence of n characters ""O"" and ""X"".Using the play sequence you can calcula... | The first line contains an integer n (1 β€ n β€ 105) β the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 β€ pi β€ 1).There will be at most six digits after the decimal point in the given pi. | Print a single real number β the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. | For the first example. There are 8 possible outcomes. Each has a probability of 0.125. ""OOO"" β 32 = 9; ""OOX"" β 22 = 4; ""OXO"" β 12 + 12 = 2; ""OXX"" β 12 = 1; ""XOO"" β 22 = 4; ""XOX"" β 12 = 1; ""XXO"" β 12 = 1; ""XXX"" β 0. So the expected score is | Input: 30.5 0.5 0.5 | Output: 2.750000000000000 | Hard | 3 | 986 | 233 | 159 | 2 |
1,632 | E1 | 1632E1 | E1. Distance Tree (easy version) | 2,400 | binary search; data structures; dfs and similar; graphs; shortest paths; trees | This version of the problem differs from the next one only in the constraint on \(n\).A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.You are given a weighted tree with ... | 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 3000\)).Each of the next \(nβ1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\)) indicating that there is an edge between ver... | For each test case, print \(n\) integers in a single line, \(x\)-th of which is equal to \(f(x)\) for all \(x\) from \(1\) to \(n\). | In the first testcase: For \(x = 1\), we can an edge between vertices \(1\) and \(3\), then \(d(1) = 0\) and \(d(2) = d(3) = d(4) = 1\), so \(f(1) = 1\). For \(x \ge 2\), no matter which edge we add, \(d(1) = 0\), \(d(2) = d(4) = 1\) and \(d(3) = 2\), so \(f(x) = 2\). | Input: 341 22 31 421 271 21 33 43 53 65 7 | Output: 1 2 2 2 1 1 2 2 3 3 3 3 3 | Expert | 6 | 766 | 476 | 132 | 16 |
1,637 | C | 1637C | C. Andrew and Stones | 1,200 | greedy; implementation | Andrew has \(n\) piles with stones. The \(i\)-th pile contains \(a_i\) stones. He wants to make his table clean so he decided to put every stone either to the \(1\)-st or the \(n\)-th pile.Andrew can perform the following operation any number of times: choose \(3\) indices \(1 \le i < j < k \le n\), such that the \(j\)... | The input contains several test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases.The first line for each test case contains one integer \(n\) (\(3 \leq n \leq 10^5\)) β the length of the array.The second line contains a sequence of integers \(a_1, a_2, \ldots, a_n\... | For each test case print the minimum number of operations needed to move stones to piles \(1\) and \(n\), or print \(-1\) if it's impossible. | In the first test case, it is optimal to do the following: Select \((i, j, k) = (1, 2, 5)\). The array becomes equal to \([2, 0, 2, 3, 7]\). Select \((i, j, k) = (1, 3, 4)\). The array becomes equal to \([3, 0, 0, 4, 7]\). Twice select \((i, j, k) = (1, 4, 5)\). The array becomes equal to \([5, 0, 0, 0, 9]\). This arra... | Input: 451 2 2 3 631 3 131 2 143 1 1 2 | Output: 4 -1 1 -1 | Easy | 2 | 615 | 465 | 141 | 16 |
1,485 | F | 1485F | F. Copy or Prefix Sum | 2,400 | combinatorics; data structures; dp; sortings | You are given an array of integers \(b_1, b_2, \ldots, b_n\).An array \(a_1, a_2, \ldots, a_n\) of integers is hybrid if for each \(i\) (\(1 \leq i \leq n\)) at least one of these conditions is true: \(b_i = a_i\), or \(b_i = \sum_{j=1}^{i} a_j\). Find the number of hybrid arrays \(a_1, a_2, \ldots, a_n\). As the resul... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-10^9 \le b_i \le 10^9\)).It is guarante... | For each test case, print a single integer: the number of hybrid arrays \(a_1, a_2, \ldots, a_n\) modulo \(10^9 + 7\). | In the first test case, the hybrid arrays are \([1, -2, 1]\), \([1, -2, 2]\), \([1, -1, 1]\).In the second test case, the hybrid arrays are \([1, 1, 1, 1]\), \([1, 1, 1, 4]\), \([1, 1, 3, -1]\), \([1, 1, 3, 4]\), \([1, 2, 0, 1]\), \([1, 2, 0, 4]\), \([1, 2, 3, -2]\), \([1, 2, 3, 4]\).In the fourth test case, the only h... | Input: 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 | Output: 3 8 223 1 | Expert | 4 | 389 | 397 | 118 | 14 |
1,442 | A | 1442A | A. Extreme Subtraction | 1,800 | constructive algorithms; dp; greedy | You are given an array \(a\) of \(n\) positive integers.You can use the following operation as many times as you like: select any integer \(1 \le k \le n\) and do one of two things: decrement by one \(k\) of the first elements of the array. decrement by one \(k\) of the last elements of the array. For example, if \(n=5... | The first line contains one positive integer \(t\) (\(1 \le t \le 30000\)) β the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing one integer \(n\) (\(1 \le n \le 30000\)) β the number of elements in the array.The second line of each test case contains \(n\) integers \(a_1... | For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise. The letters in the words YES and NO can be outputed in any case. | Input: 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 | Output: YES YES NO YES | Medium | 3 | 861 | 422 | 241 | 14 | |
2,126 | C | 2126C | C. I Will Definitely Make It | 1,100 | greedy; sortings | You are given \(n\) towers, numbered from \(1\) to \(n\). Tower \(i\) has a height of \(h_i\). At time \(0\), you are on the tower with index \(k\), and the current water level is \(1\).Every second, the water level rises by \(1\) unit. At any moment, if the water level becomes strictly greater than the height of the t... | 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. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) β the number of towers and the index o... | For each test case, output one line: ""YES"", if you can reach the tower with the maximum height before the water covers you, or ""NO"" otherwise.You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first test case, the only possible path is: \(3 \rightarrow 2 \rightarrow 1 \rightarrow 4 \rightarrow 5\).In the second test case, regardless of the order, it will not be possible to reach the tallest tower.In the third test case, one of the possible paths is: \(4 \rightarrow 1\). | Input: 55 33 2 1 4 53 11 3 44 44 4 4 26 22 3 6 9 1 24 21 2 5 6 | Output: YES NO YES YES NO | Easy | 2 | 1,171 | 561 | 313 | 21 |
277 | D | 277D | D. Google Code Jam | 2,800 | dp; probabilities | Many of you must be familiar with the Google Code Jam round rules. Let us remind you of some key moments that are crucial to solving this problem. During the round, the participants are suggested to solve several problems, each divided into two subproblems: an easy one with small limits (Small input), and a hard one wi... | The first line contains two integers n and t (1 β€ n β€ 1000, 1 β€ t β€ 1560). Then follow n lines, each containing 5 numbers: scoreSmalli, scoreLargei, timeSmalli, timeLargei, probFaili (1 β€ scoreSmalli, scoreLargei β€ 109, 1 β€ timeSmalli, timeLargei β€ 1560, 0 β€ probFaili β€ 1).probFaili are real numbers, given with at most... | Print two real numbers β the maximum expectation of the total points and the corresponding minimum possible time penalty expectation. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 9. | In the first sample one of the optimal orders of solving problems is: The Small input of the third problem. The Small input of the first problem. The Large input of the third problem. The Large input of the first problem.Note that if you solve the Small input of the second problem instead of two inputs of the third one... | Input: 3 4010 20 15 4 0.54 100 21 1 0.991 4 1 1 0.25 | Output: 24.0 18.875 | Master | 2 | 2,822 | 399 | 228 | 2 |
2,111 | F | 2111F | F. Puzzle | 2,400 | brute force; constructive algorithms; greedy; math | You have been gifted a puzzle, where each piece of this puzzle is a square with a side length of one. You can glue any picture onto this puzzle, cut it, and obtain an almost ordinary jigsaw puzzle.Your friend is an avid mathematician, so he suggested you consider the following problem. Is it possible to arrange the puz... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(p\) and \(s\) (\(1 \le p, s \le 50\)). | For each test case: if it is impossible to arrange the pieces as described above, output a single integer \(-1\); otherwise, in the first line output a single integer \(k\) (\(1 \le k \le 50\,000\)), and then in \(k\) lines output the coordinates of the pieces: each line should contain two integers \(x_{i}\) and \(y_{i... | In the first test case of the first test, the figure may look like this: In the second test, the figures look like this: Note that the internal perimeter is also taken into account! | Input: 21 131 4 | Output: 20 3 7 3 8 6 4 6 5 3 5 4 4 4 5 4 3 3 4 5 3 5 4 5 7 3 6 4 6 5 5 5 6 4 7 4 8 6 6 6 7 -1 | Expert | 4 | 902 | 274 | 445 | 21 |
1,725 | K | 1725K | K. Kingdom of Criticism | 2,500 | data structures; dsu | Pak Chanek is visiting a kingdom that earned a nickname ""Kingdom of Criticism"" because of how often its residents criticise each aspect of the kingdom. One aspect that is often criticised is the heights of the buildings. The kingdom has \(N\) buildings. Initially, building \(i\) has a height of \(A_i\) units.At any p... | The first line contains a single integer \(N\) (\(1 \leq N \leq 4 \cdot 10^5\)) β the number buildings in the kingdom.The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) (\(1 \leq A_i \leq 10^9\)) β the initial heights of the buildings.The next line contains a single integer \(Q\) (\(1 \leq Q \leq 4 \cdot... | For each query of type \(2\), output a line containing an integer representing the height of the building asked. | After the \(1\)-st query, the height of each building is \(2, 6, 5, 6, 10\).After the \(3\)-rd query, the height of each building is \(3, 6, 5, 6, 10\).After the \(4\)-th query, the height of each building is \(2, 7, 7, 7, 10\).After the \(5\)-th query, the height of each building is \(2, 7, 7, 7, 10\).After the \(6\)-... | Input: 5 2 6 5 6 2 9 1 5 10 2 5 1 1 3 3 3 6 3 8 9 1 2 9 2 3 2 2 2 4 | Output: 10 7 9 7 | Expert | 2 | 1,703 | 474 | 112 | 17 |
549 | D | 549D | D. Haar Features | 1,900 | greedy; implementation | The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.Let's consider a rectangular image that is represented... | The first line contains two space-separated integers n and m (1 β€ n, m β€ 100) β the number of rows and columns in the feature.Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to ""W"", if this element of the feature is white and ""B"" if... | Print a single number β the minimum number of operations that you need to make to calculate the value of the feature. | The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 Γ 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: add the sum of pixels in the... | Input: 6 8BBBBBBBBBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWWWWWWWWWW | Output: 2 | Hard | 2 | 1,812 | 333 | 117 | 5 |
303 | C | 303C | C. Minimum Modular | 2,400 | brute force; graphs; math; number theory | You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the minimum modular m (m > 0), so that for every pair of the remaining integers (ai, aj), the following unequality holds: . | The first line contains two integers n and k (1 β€ n β€ 5000, 0 β€ k β€ 4), which we have mentioned above. The second line contains n distinct integers a1, a2, ..., an (0 β€ ai β€ 106). | Print a single positive integer β the minimum m. | Input: 7 00 2 3 6 7 12 18 | Output: 13 | Expert | 4 | 219 | 179 | 48 | 3 | |
449 | E | 449E | E. Jzzhu and Squares | 2,900 | dp; math; number theory | Jzzhu has two integers, n and m. He calls an integer point (x, y) of a plane special if 0 β€ x β€ n and 0 β€ y β€ m. Jzzhu defines a unit square as a square with corners at points (x, y), (x + 1, y), (x + 1, y + 1), (x, y + 1), where x and y are some integers.Let's look at all the squares (their sides not necessarily paral... | The first line contains a single integer t (1 β€ t β€ 105) β the number of tests.Each of the next t lines contains the description of the test: two integers n and m (1 β€ n, m β€ 106) β the value of variables for the current test. | For each test output the total number of dots modulo 1000000007 (109 + 7). | Input: 41 32 22 53 4 | Output: 382658 | Master | 3 | 632 | 226 | 74 | 4 | |
1,499 | E | 1499E | E. Chaotic Merge | 2,400 | combinatorics; dp; math; strings | You are given two strings \(x\) and \(y\), both consist only of lowercase Latin letters. Let \(|s|\) be the length of string \(s\).Let's call a sequence \(a\) a merging sequence if it consists of exactly \(|x|\) zeros and exactly \(|y|\) ones in some order.A merge \(z\) is produced from a sequence \(a\) by the followin... | The first line contains a string \(x\) (\(1 \le |x| \le 1000\)).The second line contains a string \(y\) (\(1 \le |y| \le 1000\)).Both strings consist only of lowercase Latin letters. | Print a single integer β the sum of \(f(l_1, r_1, l_2, r_2)\) over \(1 \le l_1 \le r_1 \le |x|\) and \(1 \le l_2 \le r_2 \le |y|\) modulo \(998\,244\,353\). | In the first example there are: \(6\) pairs of substrings ""a"" and ""b"", each with valid merging sequences ""01"" and ""10""; \(3\) pairs of substrings ""a"" and ""bb"", each with a valid merging sequence ""101""; \(4\) pairs of substrings ""aa"" and ""b"", each with a valid merging sequence ""010""; \(2\) pairs of s... | Input: aaa bb | Output: 24 | Expert | 4 | 1,257 | 182 | 156 | 14 |
921 | 03 | 92103 | 03. Labyrinth-3 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.