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,575 | G | 1575G | G. GCD Festival | 2,200 | math; number theory | Mr. Chanek has an array \(a\) of \(n\) integers. The prettiness value of \(a\) is denoted as:$$$\(\sum_{i=1}^{n} {\sum_{j=1}^{n} {\gcd(a_i, a_j) \cdot \gcd(i, j)}}\)\(where \)\gcd(x, y)\( denotes the greatest common divisor (GCD) of integers \)x\( and \)y\(.In other words, the prettiness value of an array \)a\( is the ... | The first line contains an integer \(n\) (\(2 \leq n \leq 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^5\)). | Output an integer denoting the prettiness value of \(a\) modulo \(10^9 + 7\). | Input: 5 3 6 2 1 4 | Output: 77 | Hard | 2 | 489 | 159 | 77 | 15 | |
1,910 | F | 1910F | F. Build Railway Stations | 2,000 | *special; greedy; trees | Monocarp is playing a computer game where he's controlling an empire. An empire consists of \(n\) cities, connected by \(n - 1\) roads. The cities are numbered from \(1\) to \(n\). It's possible to reach every city from every other one using the roads.Traversing every road takes \(2\) hours. However, that can be improv... | 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 two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\)) β the number of cities and the maximum number of railway stations Monocarp can build.Each of the following \(n-1\) li... | For each testcase, print a single integer β the smallest total time it takes to travel from every city to every other one that Monocarp can achieve after building at most \(k\) railway stations. | Input: 35 21 22 33 44 54 41 21 31 45 31 21 32 42 5 | Output: 34 9 26 | Hard | 3 | 934 | 585 | 194 | 19 | |
431 | B | 431B | B. Shower Line | 1,200 | brute force; implementation | Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory s... | The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 β€ gij β€ 105). It is guaranteed that gii = 0 for all i.Assume that the students are numbered from 1 to 5. | Print a single integer β the maximum possible total happiness of the students. | In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. | Input: 0 0 0 0 90 0 0 0 00 0 0 0 00 0 0 0 07 0 0 0 0 | Output: 32 | Easy | 2 | 1,793 | 234 | 78 | 4 |
1,100 | F | 1100F | F. Ivan and Burgers | 2,500 | data structures; divide and conquer; greedy; math | Ivan loves burgers and spending money. There are \(n\) burger joints on the street where Ivan lives. Ivan has \(q\) friends, and the \(i\)-th friend suggested to meet at the joint \(l_i\) and walk to the joint \(r_i\) \((l_i \leq r_i)\). While strolling with the \(i\)-th friend Ivan can visit all joints \(x\) which sat... | The first line contains one integer \(n\) (\(1 \leq n \leq 500\,000\)) β the number of burger shops.The next line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \leq c_i \leq 10^6\)), where \(c_i\) β the cost of the most expensive burger in the burger joint \(i\).The third line contains one integer \(q\) (\(1 \... | Output \(q\) lines, \(i\)-th of which containing the maximum amount of money Ivan can spend with the friend \(i\). | In the first test, in order to spend the maximum amount of money with the first and third friends, Ivan just needs to go into the first burger. With a second friend, Ivan just go to the third burger.In the second test for a third friend (who is going to walk from the first to the third burger), there are only 8 options... | Input: 47 2 3 431 42 31 3 | Output: 737 | Expert | 4 | 1,221 | 541 | 114 | 11 |
1,817 | A | 1817A | A. Almost Increasing Subsequence | 1,500 | binary search; data structures; greedy | A sequence is almost-increasing if it does not contain three consecutive elements \(x, y, z\) such that \(x\ge y\ge z\).You are given an array \(a_1, a_2, \dots, a_n\) and \(q\) queries.Each query consists of two integers \(1\le l\le r\le n\). For each query, find the length of the longest almost-increasing subsequence... | The first line of input contains two integers, \(n\) and \(q\) (\(1 \leq n, q \leq 200\,000\)) β the length of the array \(a\) and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the values of the array \(a\).Each of the next \(q\) lines contains the d... | For each of the \(q\) queries, print a line containing the length of the longest almost-increasing subsequence of the subarray \(a_l, a_{l+1}, \dots, a_r\). | In the first query, the subarray is \(a_1, a_2, a_3 = [1,2,4]\). The whole subarray is almost-increasing, so the answer is \(3\).In the second query, the subarray is \(a_1, a_2, a_3,a_4 = [1,2,4,3]\). The whole subarray is a almost-increasing, because there are no three consecutive elements such that \(x \geq y \geq z\... | Input: 9 8 1 2 4 3 3 5 6 2 1 1 3 1 4 2 5 6 6 3 7 7 8 1 8 8 8 | Output: 3 4 3 1 4 2 7 1 | Medium | 3 | 526 | 483 | 156 | 18 |
311 | E | 311E | E. Biologist | 2,300 | flows | SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa.She is going to demonstrate this technique. Now SmallR has n dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to n. The cost of... | The first line contains three integers n, m, g (1 β€ n β€ 104, 0 β€ m β€ 2000, 0 β€ g β€ 104). The second line contains n integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains n integers v1, v2, ..., vn (0 β€ vi β€ 104).Each of the next m lines describes a rich ... | Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money. | Input: 5 5 90 1 1 1 01 8 6 2 30 7 3 3 2 1 11 8 1 5 11 0 3 2 1 4 10 8 3 4 2 1 01 7 2 4 1 1 | Output: 2 | Expert | 1 | 1,549 | 664 | 119 | 3 | |
416 | E | 416E | E. President's Path | 2,500 | dp; graphs; shortest paths | Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose o... | The first line of the input contains integers n, m (2 β€ n β€ 500, 0 β€ m β€ nΒ·(n - 1) / 2) β the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 β€ xi, yi β€ n, xi β yi, 1 β€ li β€ 106), where x... | Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0. | Input: 5 61 2 12 3 13 4 14 1 12 4 24 5 4 | Output: 1 4 1 2 1 5 6 1 2 1 | Expert | 3 | 798 | 404 | 299 | 4 | |
149 | C | 149C | C. Division into Teams | 1,500 | greedy; math; sortings | Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).The key in football is to divide into teams fairly before the ga... | The first line contains the only integer n (2 β€ n β€ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 β€ ai β€ 104), the i-th number represents the i-th boy's playing skills. | On the first line print an integer x β the number of boys playing for the first team. On the second line print x integers β the individual numbers of boys playing for the first team. On the third line print an integer y β the number of boys playing for the second team, on the fourth line print y integers β the individu... | Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 β€ 1) is fulfilled, th... | Input: 31 2 1 | Output: 21 2 13 | Medium | 3 | 1,228 | 242 | 761 | 1 |
1,095 | F | 1095F | F. Make It Connected | 1,900 | dsu; graphs; greedy | You are given an undirected graph consisting of \(n\) vertices. A number is written on each vertex; the number on vertex \(i\) is \(a_i\). Initially there are no edges in the graph.You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices \(x\) and \(y\) is \(a_x + ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\)) β the number of vertices in the graph and the number of special offers, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)) β the numbers written on the... | Print one integer β the minimum number of coins you have to pay to make the graph connected. | In the first example it is possible to connect \(1\) to \(2\) using special offer \(2\), and then \(1\) to \(3\) without using any offers.In next two examples the optimal answer may be achieved without using special offers. | Input: 3 2 1 3 3 2 3 5 2 1 1 | Output: 5 | Hard | 3 | 960 | 594 | 92 | 10 |
2,122 | G | 2122G | G. Tree Parking | 3,300 | combinatorics; fft; math; trees | Consider the following problem statement: You are given a tree with \(n\) vertices rooted at \(1\). For each \(1 \leq i \leq n\), a car will enter the root at time \(l_i\). It will then instantaneously travel from the root to vertex \(i\) through the unique simple path and park there. It will leave through the same pat... | 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 only line of each test case contains two integers \(n\), \(k\) (\(1 \leq k < n \leq 2 \cdot 10^5\)) β the number of vertices and leaves of the tree, respe... | For each test case, output one integer β the answer modulo \(998\,244\,353\). | In the first case, there is only one tree that satisfies the constraints. The correct pairs of sequences are: $$$\( (l, r) = \bigl([1, 3], [2, 4]\bigr),\:\bigl([3, 1], [4, 2]\bigr),\:\bigl([2, 1], [3, 4]\bigr). \)$$$ | Input: 32 18 365 43 | Output: 3 899171636 38330886 | Master | 4 | 1,065 | 420 | 77 | 21 |
2,094 | G | 2094G | G. Chimpanzini Bananini | 1,700 | data structures; implementation; math | Chimpanzini Bananini stands on the brink of a momentous battleβone destined to bring finality.For an arbitrary array \(b\) of length \(m\), let's denote the rizziness of the array to be \(\sum_{i=1}^mb_i\cdot i=b_1\cdot 1+b_2\cdot 2+b_3\cdot 3+\ldots + b_m\cdot m\).Chimpanzini Bananini gifts you an empty array. There a... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of the input contains an integer \(q\) (\(1 \leq q \leq 2\cdot 10^5\)) β the number of operations you perform on your array.The following \(q\) lines first contain a single integer \(s\) (\(1 \leq s \leq 3\)) β t... | For each test case, output \(q\) lines, outputting the rizziness of your array after each operation. | The first six states of the array: \([1]\) \([1, 2]\) \([1, 2, 3]\) \([3, 1, 2]\) \([3, 1, 2, 4]\) \([4, 2, 1, 3]\) | Input: 1133 13 23 313 423 513 623 721 | Output: 1 5 14 11 27 23 48 38 74 73 122 102 88 | Medium | 3 | 1,068 | 805 | 100 | 20 |
886 | F | 886F | F. Symmetric Projections | 2,900 | geometry | You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.Multiset is a set where equal elements are allowed.Multiset is called symmetric, if there is a point P on the p... | The first line contains a single integer n (1 β€ n β€ 2000) β the number of points in the set.Each of the next n lines contains two integers xi and yi ( - 106 β€ xi, yi β€ 106) β the coordinates of the points. It is guaranteed that no two points coincide. | If there are infinitely many good lines, print -1.Otherwise, print single integer β the number of good lines. | Picture to the first sample test: In the second sample, any line containing the origin is good. | Input: 31 22 13 3 | Output: 3 | Master | 1 | 393 | 251 | 109 | 8 |
992 | B | 992B | B. Nastya Studies Informatics | 1,600 | math; number theory | Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.We define a pair of integers (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common d... | The only line contains four integers l, r, x, y (1 β€ l β€ r β€ 109, 1 β€ x β€ y β€ 109). | In the only line print the only integer β the answer for the problem. | In the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l... | Input: 1 2 1 2 | Output: 2 | Medium | 2 | 586 | 83 | 69 | 9 |
1,336 | F | 1336F | F. Journey | 3,500 | data structures; divide and conquer; graphs; trees | In the wilds far beyond lies the Land of Sacredness, which can be viewed as a tree β connected undirected graph consisting of \(n\) nodes and \(n-1\) edges. The nodes are numbered from \(1\) to \(n\). There are \(m\) travelers attracted by its prosperity and beauty. Thereupon, they set off their journey on this land. T... | The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n, m \le 1.5 \cdot 10^5\), \(1\le k\le n\)). Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\)), denoting there is an edge between \(u\) and \(v\). The \(i\)-th line of the next \(m\) lines contains two intege... | The only line contains a single integer β the number of pairs of travelers satisfying the given conditions. | In the first example there are \(4\) pairs satisfying the given requirements: \((1,2)\), \((1,3)\), \((1,4)\), \((3,4)\). The \(1\)-st traveler and the \(2\)-nd traveler both go through the edge \(6-8\). The \(1\)-st traveler and the \(3\)-rd traveler both go through the edge \(2-6\). The \(1\)-st traveler and the \(4\... | Input: 8 4 1 1 7 1 2 2 5 4 6 6 3 6 2 6 8 7 8 3 8 2 6 4 1 | Output: 4 | Master | 4 | 1,032 | 506 | 107 | 13 |
121 | C | 121C | C. Lucky Permutation | 1,900 | brute force; combinatorics; number theory | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how ma... | The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation. | If the k-th permutation of numbers from 1 to n does not exist, print the single number ""-1"" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers. | A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj ... | Input: 7 4 | Output: 1 | Hard | 3 | 422 | 156 | 227 | 1 |
257 | E | 257E | E. Greedy Elevator | 2,200 | data structures; implementation | The m-floor (m > 1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows.All office floors are sequentially numbered with integers from 1 to m. At time t = 0, the elevator is on the first floor, the elevator is empty and nobody is waiting for the eleva... | The first line contains two space-separated integers: n, m (1 β€ n β€ 105, 2 β€ m β€ 105) β the number of people and floors in the building, correspondingly.Next n lines each contain three space-separated integers: ti, si, fi (1 β€ ti β€ 109, 1 β€ si, fi β€ m, si β fi) β the time when the i-th person begins waiting for the ele... | Print n lines. In the i-th line print a single number β the moment of time, when the i-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. Please don't use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout strea... | In the first sample the elevator worked as follows: t = 1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. pup = 1 + 0 = 1, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 2. t = 2. The elevator is on the floor number 2. One person enters... | Input: 3 101 2 73 6 53 4 8 | Output: 7118 | Hard | 2 | 2,513 | 440 | 346 | 2 |
1,556 | H | 1556H | H. DIY Tree | 3,300 | graphs; greedy; math; probabilities | William really likes puzzle kits. For one of his birthdays, his friends gifted him a complete undirected edge-weighted graph consisting of \(n\) vertices.He wants to build a spanning tree of this graph, such that for the first \(k\) vertices the following condition is satisfied: the degree of a vertex with index \(i\) ... | The first line of input contains two integers \(n\), \(k\) (\(2 \leq n \leq 50\), \(1 \leq k \leq min(n - 1, 5)\)).The second line contains \(k\) integers \(d_1, d_2, \ldots, d_k\) (\(1 \leq d_i \leq n\)). The \(i\)-th of the next \(n - 1\) lines contains \(n - i\) integers \(w_{i,i+1}, w_{i,i+2}, \ldots, w_{i,n}\) (\(... | Print one integer: the minimum weight of a spanning tree under given degree constraints for the first \(k\) vertices. | Input: 10 5 5 3 4 2 1 29 49 33 12 55 15 32 62 37 61 26 15 58 15 22 8 58 37 16 9 39 20 14 58 10 15 40 3 19 55 53 13 37 44 52 23 59 58 4 69 80 29 89 28 48 | Output: 95 | Master | 4 | 706 | 398 | 117 | 15 | |
68 | D | 68D | D. Half-decay tree | 2,500 | data structures; divide and conquer; dp; math; probabilities | Recently Petya has become keen on physics. Anna V., his teacher noticed Petya's interest and gave him a fascinating physical puzzle β a half-decay tree. A half-decay tree is a complete binary tree with the height h. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While stu... | First line will contain two integers h and q (1 β€ h β€ 30, 1 β€ q β€ 105). Next q lines will contain a query of one of two types: add v ePetya adds e electrons to vertex number v (1 β€ v β€ 2h + 1 - 1, 0 β€ e β€ 104). v and e are integers.The vertices of the tree are numbered in the following way: the root is numbered with 1,... | For each query decay solution you should output the mathematical expectation of potential of the tree after being desintegrated. The absolute or relative error in the answer should not exceed 10 - 4. | Input: 1 4add 1 3add 2 10add 3 11decay | Output: 13.50000000 | Expert | 5 | 1,096 | 425 | 199 | 0 | |
1,916 | D | 1916D | D. Mathematical Problem | 1,700 | brute force; constructive algorithms; geometry; math | The mathematicians of the 31st lyceum were given the following task:You are given an odd number \(n\), and you need to find \(n\) different numbers that are squares of integers. But it's not that simple. Each number should have a length of \(n\) (and should not have leading zeros), and the multiset of digits of all the... | The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The following \(t\) lines contain one odd integer \(n\) (\(1 \leq n \leq 99\)) β the number of numbers to be found and their length.It is guaranteed that the solution exists within the given constraints.It is guaranteed that the... | For each test case, you need to output \(n\) numbers of length \(n\) β the answer to the problem.If there are several answers, print any of them. | Below are the squares of the numbers that are the answers for the second test case:\(\mathtt{169}\) = \(\mathtt{13}^2\)\(\mathtt{196}\) = \(\mathtt{14}^2\)\(\mathtt{961}\) = \(\mathtt{31}^2\)Below are the squares of the numbers that are the answers for the third test case:\(\mathtt{16384}\) = \(\mathtt{128}^2\)\(\matht... | Input: 3135 | Output: 1 169 196 961 16384 31684 36481 38416 43681 | Medium | 4 | 609 | 400 | 145 | 19 |
1,733 | B | 1733B | B. Rule of League | 900 | constructive algorithms; math | There is a badminton championship in which \(n\) players take part. The players are numbered from \(1\) to \(n\). The championship proceeds as follows: player \(1\) and player \(2\) play a game, then the winner and player \(3\) play a game, and then the winner and player \(4\) play a game, and so on. So, \(n-1\) games ... | The first line contains one integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The only line of each test case contains three integers \(n\), \(x\), \(y\) (\(2 \le n \le 10^5\), \(0 \le x, y < n\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\). | Print the answer for each test case, one per line. If there is no result that matches the given information about \(n\), \(x\), \(y\), print \(-1\). Otherwise, print \(n-1\) space separated integers, where the \(i\)-th integer is the player number of the winner of the \(i\)-th game. If there are multiple valid results,... | In the first test case, player \(1\) and player \(4\) won \(x\) times, player \(2\) and player \(3\) won \(y\) times.In the second, third, and fifth test cases, no valid result exists. | Input: 55 2 08 1 23 0 02 0 16 3 0 | Output: 1 1 4 4 -1 -1 2 -1 | Beginner | 2 | 685 | 302 | 331 | 17 |
601 | D | 601D | D. Acyclic Organic Compounds | 2,400 | data structures; dfs and similar; dsu; hashing; strings; trees | You are given a tree T with n vertices (numbered 1 through n) and a letter in each vertex. The tree is rooted at vertex 1.Let's look at the subtree Tv of some vertex v. It is possible to read a string along each simple path starting at v and ending at some vertex in Tv (possibly v itself). Let's denote the number of di... | The first line of the input contains one integer n (1 β€ n β€ 300 000) β the number of vertices of the tree.The second line contains n space-separated integers ci (0 β€ ci β€ 109).The third line contains a string s consisting of n lowercase English letters β the i-th character of this string is the letter in vertex i.The f... | Print two lines. On the first line, print over all 1 β€ i β€ n. On the second line, print the number of vertices v for which . | In the first sample, the tree looks like this: The sets of strings that can be read from individual vertices are: Finally, the values of are: In the second sample, the values of are (5, 4, 2, 1, 1, 1). The distinct strings read in T2 are ; note that can be read down to vertices 3 or 4. | Input: 101 2 7 20 20 30 40 50 50 50cacabbcddd1 26 87 26 25 45 93 102 52 3 | Output: 513 | Expert | 6 | 580 | 532 | 124 | 6 |
130 | H | 130H | H. Balanced brackets | 1,600 | *special | A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters Β«+Β» and Β«1Β». For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are balanced, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not.You are given a string which consists of opening and closing round brackets. Check whether... | The only line of input contains a string between 1 and 100 characters long, inclusive. Each character in the string will be Β«(Β» or Β«)Β». | Output Β«YESΒ» if the bracket sequence is balanced, and Β«NOΒ» otherwise (quotes for clarity only). | Input: (()(()))() | Output: YES | Medium | 1 | 355 | 135 | 95 | 1 | |
757 | E | 757E | E. Bash Plays with Functions | 2,500 | brute force; combinatorics; dp; number theory | Bash got tired on his journey to become the greatest Pokemon master. So he decides to take a break and play with functions.Bash defines a function f0(n), which denotes the number of ways of factoring n into two factors p and q such that gcd(p, q) = 1. In other words, f0(n) is the number of ordered pairs of positive int... | The first line contains an integer q (1 β€ q β€ 106) β the number of values Bash wants to know.Each of the next q lines contain two integers r and n (0 β€ r β€ 106, 1 β€ n β€ 106), which denote Bash wants to know the value fr(n). | Print q integers. For each pair of r and n given, print fr(n) modulo 109 + 7 on a separate line. | Input: 50 301 253 652 54 48 | Output: 85254630 | Expert | 4 | 733 | 223 | 96 | 7 | |
1,833 | F | 1833F | F. Ira and Flamenco | 1,700 | combinatorics; constructive algorithms; data structures; implementation; math; sortings; two pointers | Ira loves Spanish flamenco dance very much. She decided to start her own dance studio and found \(n\) students, \(i\)th of whom has level \(a_i\).Ira can choose several of her students and set a dance with them. So she can set a huge number of dances, but she is only interested in magnificent dances. The dance is calle... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of testcases.The first line of each testcase contains integers \(n\) and \(m\) (\(1 \le m \le n \le 2 \cdot 10^5\)) β the number of Ira students and the number of dancers in the magnificent dance.The second line of each testcase contains \(n... | For each testcase, print a single integer β the number of magnificent dances. Since this number can be very large, print it modulo \(10^9 + 7\). | In the first testcase, Ira can set such magnificent dances: \([\color{red}{8}, 10, 10, \color{red}{9}, \color{red}{6}, 11, \color{red}{7}]\), \([\color{red}{8}, \color{red}{10}, 10, \color{red}{9}, 6, 11, \color{red}{7}]\), \([\color{red}{8}, 10, \color{red}{10}, \color{red}{9}, 6, 11, \color{red}{7}]\), \([\color{red}... | Input: 97 48 10 10 9 6 11 75 34 2 2 3 68 21 5 2 2 3 1 3 33 33 3 35 13 4 3 10 712 35 2 1 1 4 3 5 5 5 2 7 51 113 21 2 32 21 2 | Output: 5 2 10 0 5 11 1 2 1 | Medium | 7 | 1,695 | 495 | 144 | 18 |
43 | E | 43E | E. Race | 2,300 | brute force; implementation; two pointers | Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior β the system of segments on each of which the s... | The first line contains two integers n and s (2 β€ n β€ 100, 1 β€ s β€ 106) β the number of cars and the length of the track in kilometers. Then follow n lines β the description of the system of segments for each car. Every description starts with integer k (1 β€ k β€ 100) β the number of segments in the system. Then k space... | Print the single number β the number of times some car managed to take the lead over another car during the race. | Input: 2 332 5 1 2 141 3 11 | Output: 1 | Expert | 3 | 1,190 | 596 | 113 | 0 | |
2,069 | B | 2069B | B. Set of Strangers | 1,200 | greedy; matrices | You are given a table of \(n\) rows and \(m\) columns. Initially, the cell at the \(i\)-th row and the \(j\)-th column has color \(a_{i, j}\).Let's say that two cells are strangers if they don't share a side. Strangers are allowed to touch with corners.Let's say that the set of cells is a set of strangers if all pairs ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Next, \(t\) cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 700\)) β the number of rows and columns in the table.The next \(n\) lines contain the colors of cells in ... | For each test case, print one integer β the minimum number of steps to paint all cells of the table the same color. | In the first test case, the table is painted in one color from the start.In the second test case, you can, for example, choose all cells with color \(1\) and paint them in \(3\). Then choose all cells with color \(2\) and also paint them in \(3\).In the third test case, you can choose all cells with color \(5\) and pai... | Input: 41 113 31 2 12 3 21 3 11 65 4 5 4 4 53 41 4 2 21 4 3 56 6 3 5 | Output: 0 2 1 10 | Easy | 2 | 680 | 497 | 115 | 20 |
538 | E | 538E | E. Demiurges Play Again | 2,200 | dfs and similar; dp; math; trees | Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of th... | The first line contains a single integer n β the number of nodes in the tree (1 β€ n β€ 2Β·105).Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ n) β the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root ... | Print two space-separated integers β the maximum possible and the minimum possible result of the game. | Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.In the second sample no mat... | Input: 51 21 32 42 5 | Output: 3 2 | Hard | 4 | 1,477 | 334 | 102 | 5 |
546 | A | 546A | A. Soldier and Bananas | 800 | brute force; implementation; math | A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay iΒ·k dollars for the i-th banana). He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas? | The first line contains three positive integers k, n, w (1 β€ k, w β€ 1000, 0 β€ n β€ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants. | Output one integer β the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0. | Input: 3 17 4 | Output: 13 | Beginner | 3 | 296 | 191 | 134 | 5 | |
1,237 | G | 1237G | G. Balanced Distribution | 3,500 | data structures; dp; greedy | There are \(n\) friends living on a circular street. The friends and their houses are numbered clockwise from \(0\) to \(n-1\).Initially person \(i\) has \(a_i\) stones. The friends want to make the distribution of stones among them perfectly balanced: everyone should possess the same number of stones.The only way to c... | The first line contains two integers \(n\) and \(k\) (\(2 \le k < n \le 10^5\)), denoting the number of friends and the size of each meeting.The second line contains \(n\) integers \(a_0, a_1, \ldots, a_{n-1}\) (\(0 \le a_i \le 10^4\)), denoting the number of stones people initially have.The sum of all \(a_i\) is divis... | Output the minimum number of meetings \(m\) (\(m \ge 0\)), followed by \(m\) descriptions of meetings in chronological order.The \(i\)-th description must consist of an integer \(s_i\) (\(0 \le s_i < n\)), followed by \(k\) non-negative integers \(b_{i, 0}, b_{i, 1}, \ldots, b_{i, k-1}\) (\(b_{i, j} \ge 0\)). Such a de... | In the first example, the distribution of stones changes as follows: after the first meeting: \(2\) \(6\) \(\mathbf{7}\) \(\mathbf{3}\) \(\mathbf{4}\) \(2\); after the second meeting: \(\mathbf{4}\) \(\mathbf{2}\) \(7\) \(3\) \(4\) \(\mathbf{4}\); after the third meeting: \(4\) \(\mathbf{4}\) \(\mathbf{4}\) \(\mathbf{4... | Input: 6 3 2 6 1 10 3 2 | Output: 3 2 7 3 4 5 4 4 2 1 4 4 4 | Master | 3 | 888 | 334 | 772 | 12 |
1,454 | E | 1454E | E. Number of Simple Paths | 2,000 | combinatorics; dfs and similar; graphs; trees | You are given an undirected graph consisting of \(n\) vertices and \(n\) edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph.Your task is to calculate the number of simple paths of length a... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of vertices (and the number of edges) in the graph.The next \(n\) lines of... | For each test case, print one integer: the number of simple paths of length at least \(1\) in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). | Consider the second test case of the example. It looks like that:There are \(11\) different simple paths: \([1, 2]\); \([2, 3]\); \([3, 4]\); \([2, 4]\); \([1, 2, 4]\); \([1, 2, 3]\); \([2, 3, 4]\); \([2, 4, 3]\); \([3, 2, 4]\); \([1, 2, 3, 4]\); \([1, 2, 4, 3]\). | Input: 3 3 1 2 2 3 1 3 4 1 2 2 3 3 4 4 2 5 1 2 2 3 1 3 2 5 4 3 | Output: 6 11 18 | Hard | 4 | 915 | 1,010 | 248 | 14 |
1,555 | C | 1555C | C. Coin Rows | 1,300 | brute force; constructive algorithms; dp; implementation | Alice and Bob are playing a game on a matrix, consisting of \(2\) rows and \(m\) columns. The cell in the \(i\)-th row in the \(j\)-th column contains \(a_{i, j}\) coins in it.Initially, both Alice and Bob are standing in a cell \((1, 1)\). They are going to perform a sequence of moves to reach a cell \((2, m)\).The po... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of the testcase contains a single integer \(m\) (\(1 \le m \le 10^5\)) β the number of columns of the matrix.The \(i\)-th of the next \(2\) lines contain \(m\) i... | For each testcase print a single integer β the score of the game if both players play optimally. | The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue. | Input: 3 3 1 3 7 3 5 1 3 1 3 9 3 5 1 1 4 7 | Output: 7 8 0 | Easy | 4 | 952 | 545 | 96 | 15 |
317 | E | 317E | E. Princess and Her Shadow | 3,100 | constructive algorithms; shortest paths | Princess Vlada enjoys springing in the meadows and walking in the forest. One day β wonderful, sunny day β during her walk Princess found out with astonishment that her shadow was missing! ""Blimey!"", β she thought and started her search of the shadow in the forest.Normally the Shadow is too lazy and simply sleeps und... | First line of the input contains the coordinates of the characters vx, vy, sx, sy and the number of trees m (0 β€ m β€ 400). The following m lines contain the coordinates of the trees.All the coordinates are integers between -100 and 100, inclusive. The Princess, The Shadow and the trees are located in the different cell... | If it is impossible for the Princess to catch the Shadow, print ""-1"" (without quotes).Otherwise print a sequence of characters ""L"", ""R"", ""D"", ""U"", corresponding to the Princess's moves, following which she will be able to catch the Shadow at some turn (L β move to the left, R β to the right, U β up, D β down;... | Below the pictures for the samples are given (Princess, Shadow and the trees are colored in pink, gray and black correspondingly; the blue dot marks the lattice center).In the first case the Princess may make two left steps, one step upwards and one right step: In the following case the Princess cannot catch the Shadow... | Input: 0 0 1 0 10 1 | Output: LLUR | Master | 2 | 1,577 | 322 | 628 | 3 |
1,716 | B | 1716B | B. Permutation Chain | 800 | constructive algorithms; math | A permutation of length \(n\) is a sequence of integers from \(1\) to \(n\) such that each integer appears in it exactly once.Let the fixedness of a permutation \(p\) be the number of fixed points in it β the number of positions \(j\) such that \(p_j = j\), where \(p_j\) is the \(j\)-th element of the permutation \(p\)... | The first line contains a single integer \(t\) (\(1 \le t \le 99\)) β the number of testcases.The only line of each testcase contains a single integer \(n\) (\(2 \le n \le 100\)) β the required length of permutations in the chain. | For each testcase, first, print the length of a permutation chain \(k\).Then print \(k\) permutations \(a_1, a_2, \dots, a_k\). \(a_1\) should be an identity permutation of length \(n\) (\([1, 2, \dots, n]\)). For each \(i\) from \(2\) to \(k\), \(a_i\) should be obtained by swapping two elements in \(a_{i-1}\). It sho... | Input: 223 | Output: 2 1 2 2 1 3 1 2 3 3 2 1 3 1 2 | Beginner | 2 | 1,902 | 230 | 378 | 17 | |
1,297 | D | 1297D | D. Bonus Distribution | 0 | *special; binary search; greedy; sortings | For the first time, Polycarp's startup ended the year with a profit! Now he is about to distribute \(k\) burles as a bonus among \(n\) employees.It is known that the current salary of the \(i\)-th employee is \(a_i\) and all the values of \(a_i\) in the company are different.Polycarp wants to distribute the \(k\) burle... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le 10^9\)) β the number of employees and the total bonus.The second line of each ... | Print the answers to \(t\) test cases in the order they appear in the input. Print each answer as a sequence of non-negative integers \(d_1, d_2, \dots, d_n\). If there are several answers, print any of them. | Input: 5 4 1 3 1 4 2 2 3 10 2 4 1000000000 987654321 1000000000 999999999 500000000 8 9 5 6 1 8 3 4 2 7 6 1 6 3 1 8 5 9 | Output: 0 0 1 0 0 3 134259259 121913582 121913582 621913577 2 2 0 2 0 1 0 2 1 0 0 0 0 0 | Beginner | 4 | 1,624 | 564 | 208 | 12 | |
1,881 | F | 1881F | F. Minimum Maximum Distance | 1,700 | dfs and similar; dp; graphs; shortest paths; trees | You have a tree with \(n\) vertices, some of which are marked. A tree is a connected undirected graph without cycles.Let \(f_i\) denote the maximum distance from vertex \(i\) to any of the marked vertices.Your task is to find the minimum value of \(f_i\) among all vertices. For example, in the tree shown in the example... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree and the number of marked vertices, respectively.The second line of each test case co... | For each test case, output a single integer β the minimum value of \(f_i\) among all vertices. | Input: 67 32 6 71 21 32 42 53 63 74 41 2 3 41 22 33 45 111 21 31 41 55 24 51 22 31 44 510 81 2 3 4 5 8 9 102 1010 55 33 11 77 44 98 96 110 91 2 4 5 6 7 8 9 101 33 99 44 1010 66 77 22 55 8 | Output: 2 2 0 1 4 5 | Medium | 5 | 468 | 639 | 94 | 18 | |
946 | E | 946E | E. Largest Beautiful Number | 2,200 | greedy; implementation | Yes, that's another problem with definition of ""beautiful"" numbers.Let's call a positive integer x beautiful if its decimal representation without leading zeroes contains even number of digits, and there exists a permutation of this representation which is palindromic. For example, 4242 is a beautiful number, since i... | The first line contains one integer t (1 β€ t β€ 105) β the number of testcases you have to solve.Then t lines follow, each representing one testcase and containing one string which is the decimal representation of number s. It is guaranteed that this string has even length, contains no leading zeroes, and there exists a... | For each testcase print one line containing the largest beautiful number which is less than s (it is guaranteed that the answer exists). | Input: 48988100028923845 | Output: 88779928923839 | Hard | 2 | 472 | 425 | 136 | 9 | |
1,423 | B | 1423B | B. Valuable Paper | 1,900 | binary search; flows; graph matchings; graphs | The pandemic is upon us, and the world is in shortage of the most important resource: toilet paper. As one of the best prepared nations for this crisis, BubbleLand promised to help all other world nations with this valuable resource. To do that, the country will send airplanes to other countries carrying toilet paper.I... | The first line contains two integers \(N\) \((1 \leq N \leq 10^4)\) - number of airports/factories, and \(M\) \((1 \leq M \leq 10^5)\) - number of available pairs to build a road between.On next \(M\) lines, there are three integers \(u\), \(v\) \((1 \leq u,v \leq N)\), \(d\) \((1 \leq d \leq 10^9)\) - meaning that you... | If there are no solutions, output -1. If there exists a solution, output the minimal number of days to complete all roads, equal to maximal \(d\) among all chosen roads. | Input: 3 5 1 2 1 2 3 2 3 3 3 2 1 4 2 2 5 | Output: 4 | Hard | 4 | 933 | 393 | 169 | 14 | |
466 | A | 466A | A. Cheap Travel | 1,200 | implementation | Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of... | The single line contains four space-separated integers n, m, a, b (1 β€ n, m, a, b β€ 1000) β the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride ticket. | Print a single integer β the minimum sum in rubles that Ann will need to spend. | In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy three m ride tickets. | Input: 6 2 1 2 | Output: 6 | Easy | 1 | 366 | 244 | 79 | 4 |
467 | A | 467A | A. George and Accommodation | 800 | implementation | George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living i... | The first line contains a single integer n (1 β€ n β€ 100) β the number of rooms.The i-th of the next n lines contains two integers pi and qi (0 β€ pi β€ qi β€ 100) β the number of people who already live in the i-th room and the room's capacity. | Print a single integer β the number of rooms where George and Alex can move in. | Input: 31 12 23 3 | Output: 0 | Beginner | 1 | 461 | 241 | 79 | 4 | |
367 | D | 367D | D. Sereja and Sets | 2,400 | bitmasks; dfs and similar | Sereja has m non-empty sets of integers A1, A2, ..., Am. What a lucky coincidence! The given sets are a partition of the set of all integers from 1 to n. In other words, for any integer v (1 β€ v β€ n) there is exactly one set At such that . Also Sereja has integer d.Sereja decided to choose some sets from the sets he ha... | The first line contains integers n, m, d (1 β€ d β€ n β€ 105, 1 β€ m β€ 20). The next m lines contain sets. The first number in the i-th line is si (1 β€ si β€ n). This number denotes the size of the i-th set. Then the line contains si distinct integers from 1 to n β set Ai.It is guaranteed that the sets form partition of all... | In a single line print the answer to the problem β the minimum value k at the right choice. | Input: 3 2 21 22 1 3 | Output: 1 | Expert | 2 | 885 | 342 | 91 | 3 | |
1,431 | B | 1431B | B. Polycarp and the Language of Gods | 1,400 | *special; implementation; two pointers | Polycarp has just finished writing down the lecture on elvish languages. The language this week was ""VwV"" (pronounced as ""uwu""). The writing system of this language consists of only two lowercase Latin letters: 'v' and 'w'.Unfortunately, Polycarp has written all the lecture in cursive and without any spaces, so the... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.Each of the next \(t\) lines contains a non-empty string in VwV language, which consists only of lowercase Latin letters 'v' and 'w'. The length of the string does not exceed \(100\). | For each testcase print a single integer: the minimum number of letters Polycarp should underline so that there is no ambiguity in his notes. | In the first testcase it's enough to underline any of the two letters 'v'.In the second testcase the letter 'v' is not ambiguous by itself already, so you don't have to underline anything.In the third testcase you have to underline 'w', so that you don't mix it up with two letters 'v'.In the fourth testcase you can und... | Input: 5 vv v w vwv vwvvwv | Output: 1 0 1 1 3 | Easy | 3 | 1,026 | 278 | 141 | 14 |
1,336 | D | 1336D | D. Yui and Mahjong Set | 3,200 | constructive algorithms; interactive | This is an interactive problem.Yui is a girl who enjoys playing Mahjong. She has a mysterious set which consists of tiles (this set can be empty). Each tile has an integer value between \(1\) and \(n\), and at most \(n\) tiles in the set have the same value. So the set can contain at most \(n^2\) tiles.You want to figu... | The first line contains a single integer \(n\) (\(4 \le n \le 100\)).The second line contains two integers which represent the number of triplet subsets and straight subsets of the initial set respectively. | When you are ready to answer, print a single line of form ""! \(a_1\) \(a_2\) \(\ldots\) \(a_n\)"" (\(0 \le a_i \le n\)), where \(a_i\) is equal to the number of tiles in the initial set with value \(i\). | In the first test, the initial set of tiles is \(\{1, 1, 2, 3, 3, 3, 5, 5\}\). It has only one triplet subset \(\{3, 3, 3\}\) and six straight subsets, all equal to \(\{1, 2, 3\}\). After inserting a tile with value \(1\) the set of tiles will be \(\{1, 1, 1, 2, 3, 3, 3, 5, 5\}\) and will have two triplet subsets \(\{1... | Input: 5 1 6 2 9 5 12 5 24 6 24 | Output: + 1 + 1 + 2 + 5 ! 2 1 3 0 2 | Master | 2 | 1,359 | 206 | 204 | 13 |
2,031 | B | 2031B | B. Penchick and Satay Sticks | 900 | brute force; greedy; sortings | Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya!In the bustling food stalls of Surabaya, Kohane bought \(n\) satay sticks and arranged them in a line, with the \(i\)-th satay stick having length \(p_i\). It is given that \(p\) is a permutation\(^{\text{β}}\) of length \(n\).Penc... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β the number of satay sticks.The second line of each test... | For each test case, output ""YES"" if it is possible to sort permutation \(p\) by performing the operation. Otherwise, output ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, we can sort permutation \(p = [2, 1, 3, 4]\) by performing an operation on index \(1\) (\(|p_2 - p_1| = |1 - 2| = 1\)), resulting in \(p = [1, 2, 3, 4]\).In the second test case, it can be proven that it is impossible to sort permutation \(p = [4, 2, 3, 1]\) by performing the operation. Here is ... | Input: 242 1 3 444 2 3 1 | Output: YES NO | Beginner | 3 | 1,196 | 557 | 294 | 20 |
1,106 | E | 1106E | E. Lunar New Year and Red Envelopes | 2,100 | data structures; dp | Lunar New Year is approaching, and Bob is going to receive some red envelopes with countless money! But collecting money from red envelopes is a time-consuming process itself.Let's describe this problem in a mathematical way. Consider a timeline from time \(1\) to \(n\). The \(i\)-th red envelope will be available from... | The first line contains three non-negative integers \(n\), \(m\) and \(k\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq 200\), \(1 \leq k \leq 10^5\)), denoting the length of the timeline, the number of times Alice can disturb Bob and the total number of red envelopes, respectively.The following \(k\) lines describe those ... | Output one integer β the minimum number of coins Bob would get if Alice disturbs him optimally. | In the first sample, Alice has no chance to disturb Bob. Therefore Bob will collect the coins in the red envelopes at time \(1\) and \(5\), collecting \(13\) coins in total.In the second sample, Alice should disturb Bob at time \(1\). Therefore Bob skips the first envelope, collects the second one and can not do anythi... | Input: 5 0 2 1 3 4 5 2 5 5 8 | Output: 13 | Hard | 2 | 1,473 | 696 | 95 | 11 |
1,506 | D | 1506D | D. Epic Transformation | 1,400 | constructive algorithms; data structures; greedy | You are given an array \(a\) of length \(n\) consisting of integers. You can apply the following operation, consisting of several steps, on the array \(a\) zero or more times: you select two different numbers in the array \(a_i\) and \(a_j\); you remove \(i\)-th and \(j\)-th elements from the array. For example, if \(n... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) is length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_... | For each test case, output the minimum possible size of the array after applying some sequence of operations to it. | Input: 5 6 1 6 1 1 4 4 2 1 2 2 1 1 5 4 5 4 5 4 6 2 3 2 1 3 1 | Output: 0 0 2 1 0 | Easy | 3 | 643 | 426 | 115 | 15 | |
768 | B | 768B | B. Code For 1 | 1,600 | constructive algorithms; dfs and similar; divide and conquer | Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam s... | The first line contains three integers n, l, r (0 β€ n < 250, 0 β€ r - l β€ 105, r β₯ 1, l β₯ 1) β initial element and the range l to r.It is guaranteed that r is not greater than the length of the final list. | Output the total number of 1s in the range l to r in the final sequence. | Consider first example:Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.For the second example:Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5. | Input: 7 2 5 | Output: 4 | Medium | 3 | 1,027 | 204 | 72 | 7 |
1,478 | A | 1478A | A. Nezzar and Colorful Balls | 800 | brute force; greedy | Nezzar has \(n\) balls, numbered with integers \(1, 2, \ldots, n\). Numbers \(a_1, a_2, \ldots, a_n\) are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that \(a_i \leq a_{i+1}\) for all \(1 \leq i < n\).Nezzar wants to color the balls using the minimum number of color... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le n\)). It is guaranteed that \(a_1 \leq ... | For each test case, output the minimum number of colors Nezzar can use. | Let's match each color with some numbers. Then:In the first test case, one optimal color assignment is \([1,2,3,3,2,1]\).In the second test case, one optimal color assignment is \([1,2,1,2,1]\). | Input: 5 6 1 1 1 2 3 4 5 1 1 2 2 3 4 2 2 2 2 3 1 2 3 1 1 | Output: 3 2 4 1 1 | Beginner | 2 | 655 | 347 | 71 | 14 |
934 | B | 934B | B. A Prosperous Lot | 1,200 | constructive algorithms; implementation | Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.Big Banban is hesitating over the amount of... | The first and only line contains an integer k (1 β€ k β€ 106) β the desired number of loops. | Output an integer β if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 1018. | Input: 2 | Output: 462 | Easy | 2 | 818 | 90 | 169 | 9 | |
552 | C | 552C | C. Vanya and Scales | 1,900 | brute force; dp; greedy; math; meet-in-the-middle; number theory | Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally spea... | The first line contains two integers w, m (2 β€ w β€ 109, 1 β€ m β€ 109) β the number defining the masses of the weights and the mass of the item. | Print word 'YES' if the item can be weighted and 'NO' if it cannot. | Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have... | Input: 3 7 | Output: YES | Hard | 6 | 545 | 142 | 67 | 5 |
730 | L | 730L | L. Expression Queries | 3,200 | data structures | A simplified arithmetic expression (SAE) is an arithmetic expression defined by the following grammar: <SAE> ::= <Number> | <SAE>+<SAE> | <SAE>*<SAE> | (<SAE>) <Number> ::= <Digit> | <Digit><Number> <Digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 In other words it's a correct arithmetic expression that is allowed to ... | The first line of the input contains non-empty string s (1 β€ |s| β€ 4Β·105) which represents a correct SAE. Each character of the string can be one of the following characters: '*', '+', '(', ')' or a digit ('0'-'9'). The expression might contain extra-huge numbers.The second line contains an integer m (1 β€ m β€ 4Β·105) wh... | The i-th number of output should be the answer for the i-th query. If the i-th query corresponds to a valid sub-expression output the value of the sub-expression modulo 1000000007 (109 + 7). Otherwise output -1 as an answer for the query. Print numbers on separate lines. | Input: ((1+2)*3+101*2)68 141 62 1011 145 54 5 | Output: 205-11022-1 | Master | 1 | 1,230 | 457 | 271 | 7 | |
1,905 | F | 1905F | F. Field Should Not Be Empty | 2,600 | brute force; data structures; divide and conquer | You are given a permutation\(^{\dagger}\) \(p\) of length \(n\).We call index \(x\) good if for all \(y < x\) it holds that \(p_y < p_x\) and for all \(y > x\) it holds that \(p_y > p_x\). We call \(f(p)\) the number of good indices in \(p\). You can perform the following operation: pick \(2\) distinct indices \(i\) an... | Each test consists of multiple test cases. The first line of contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the permutatio... | For each test case, output a single integer β the maximum value of \(f(p)\) after performing the operation exactly once. | In the first test case, \(p = [1,2,3,4,5]\) and \(f(p)=5\) which is already maximum possible. But must perform the operation anyway. We can get \(f(p)=3\) by choosing \(i=1\) and \(j=2\) which makes \(p = [2,1,3,4,5]\).In the second test case, we can transform \(p\) into \([1,2,3,4,5]\) by choosing \(i=1\) and \(j=2\).... | Input: 551 2 3 4 552 1 3 4 572 1 5 3 7 6 462 3 5 4 1 677 6 5 4 3 2 1 | Output: 3 5 2 3 2 | Expert | 3 | 797 | 570 | 120 | 19 |
1,175 | F | 1175F | F. The Number of Subpermutations | 2,500 | brute force; data structures; divide and conquer; hashing; math | You have an array \(a_1, a_2, \dots, a_n\). Let's call some subarray \(a_l, a_{l + 1}, \dots , a_r\) of this array a subpermutation if it contains all integers from \(1\) to \(r-l+1\) exactly once. For example, array \(a = [2, 2, 1, 3, 2, 3, 1]\) contains \(6\) subarrays which are subpermutations: \([a_2 \dots a_3]\), ... | The first line contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le n\)). This array can contain the same integers. | Print the number of subpermutations of the array \(a\). | There are \(7\) subpermutations in the first test case. Their segments of indices are \([1, 4]\), \([3, 3]\), \([3, 6]\), \([4, 7]\), \([6, 7]\), \([7, 7]\) and \([7, 8]\).In the second test case \(6\) subpermutations exist: \([1, 1]\), \([2, 2]\), \([2, 3]\), \([3, 4]\), \([4, 4]\) and \([4, 5]\). | Input: 8 2 4 1 3 4 2 1 2 | Output: 7 | Expert | 5 | 481 | 203 | 55 | 11 |
525 | C | 525C | C. Ilya and Sticks | 1,600 | greedy; math; sortings | In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximi... | The first line of the input contains a positive integer n (1 β€ n β€ 105) β the number of the available sticks.The second line of the input contains n positive integers li (2 β€ li β€ 106) β the lengths of the sticks. | The first line of the output must contain a single non-negative integer β the maximum total area of the rectangles that Ilya can make from the available sticks. | Input: 42 4 4 2 | Output: 8 | Medium | 3 | 1,181 | 213 | 160 | 5 | |
802 | I | 802I | I. Fake News (hard) | 2,300 | string suffix structures | Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message... | The input starts with a line indicating the number of test cases T (1 β€ T β€ 10). After that, T test cases follow, each of which consists of one line containing a string s (1 β€ |s| β€ 100 000) composed of lowercase letters (a-z). | Output T lines, every line containing one number β the answer to the corresponding test case. | A string s contains another string p as a substring if p is a contiguous subsequence of s. For example, ab is a substring of cab but not of acb. | Input: 4aaabcdcccabcc | Output: 5101412 | Expert | 1 | 778 | 227 | 93 | 8 |
762 | F | 762F | F. Tree nesting | 2,800 | combinatorics; graphs; trees | You are given two trees (connected undirected acyclic graphs) S and T.Count the number of subtrees (connected subgraphs) of S that are isomorphic to tree T. Since this number can get quite large, output it modulo 109 + 7.Two subtrees of tree S are considered different, if there exists a vertex in S that belongs to exac... | The first line contains a single integer |S| (1 β€ |S| β€ 1000) β the number of vertices of tree S.Next |S| - 1 lines contain two integers ui and vi (1 β€ ui, vi β€ |S|) and describe edges of tree S.The next line contains a single integer |T| (1 β€ |T| β€ 12) β the number of vertices of tree T.Next |T| - 1 lines contain two ... | On the first line output a single integer β the answer to the given task modulo 109 + 7. | Input: 51 22 33 44 531 22 3 | Output: 3 | Master | 3 | 755 | 387 | 88 | 7 | |
1,443 | C | 1443C | C. The Delivery Dilemma | 1,400 | binary search; greedy; sortings | Petya is preparing for his birthday. He decided that there would be \(n\) different dishes on the dinner table, numbered from \(1\) to \(n\). Since Petya doesn't like to cook, he wants to order these dishes in restaurants.Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick ... | The first line contains one positive integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β 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 2 \cdot 10^5\)) β the number of dishes that Petya wants to order.The second line of each test case cont... | For each test case output one integer β the minimum time after which all dishes can be at Petya's home. | Input: 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 | Output: 5 3 2 3 | Easy | 3 | 1,583 | 694 | 103 | 14 | |
1,791 | C | 1791C | C. Prepend and Append | 800 | implementation; two pointers | Timur initially had a binary string\(^{\dagger}\) \(s\) (possibly of length \(0\)). He performed the following operation several (possibly zero) times: Add \(\texttt{0}\) to one end of the string and \(\texttt{1}\) to the other end of the string. For example, starting from the string \(\texttt{1011}\), you can obtain e... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of testcases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2000\)) β the length of Timur's final string.The second line of each test case contains a string \(s\) of length \(n\) consisting of charact... | For each test case, output a single nonnegative integer β the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output \(0\). | In the first test case, the shortest possible string Timur started with is \(\texttt{0}\), and he performed the following operation: \(\texttt{0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}}\).In the second test case, the shortest possible string Timur started with is \(\texttt{11}\), and he performed t... | Input: 931004011151010161010107101011011210211101011011010 | Output: 1 2 5 0 3 1 0 2 4 | Beginner | 2 | 704 | 384 | 210 | 17 |
1,231 | C | 1231C | C. Increasing Matrix | 1,100 | greedy | In this problem, a \(n \times m\) rectangular matrix \(a\) is called increasing if, for each row of \(i\), when go from left to right, the values strictly increase (that is, \(a_{i,1}<a_{i,2}<\dots<a_{i,m}\)) and for each column \(j\), when go from top to bottom, the values strictly increase (that is, \(a_{1,j}<a_{2,j}... | The first line contains integers \(n\) and \(m\) (\(3 \le n, m \le 500\)) β the number of rows and columns in the given matrix \(a\).The following lines contain \(m\) each of non-negative integers β the values in the corresponding row of the given matrix: \(a_{i,1}, a_{i,2}, \dots, a_{i,m}\) (\(0 \le a_{i,j} \le 8000\)... | If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. | In the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value \(3\) must be put in the middle cell.In the third example, the desired resultant matrix does not exist. | Input: 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 | Output: 144 | Easy | 1 | 748 | 408 | 166 | 12 |
435 | E | 435E | E. Special Graph | 2,500 | In this problem you will need to deal with an n Γ m grid graph. The graph's vertices are the nodes of the n Γ m grid. The graph's edges are all the sides and diagonals of the grid's unit squares.The figure below shows a 3 Γ 5 graph. The black lines are the graph's edges, the colored circles are the graph's vertices. Th... | The first line contains two integers n and m (2 β€ n, m β€ 1000). Each of the next n lines consists of m characters β the given graph. Each character is either Β«0Β», Β«1Β», Β«2Β», Β«3Β», Β«4Β». Character Β«0Β» means that the corresponding vertex is unpainted, otherwise the character means the color of the vertex.Assume that all the... | If there is no way to get correct vertex coloring of the graph, print 0 in a single line. Otherwise print the colored n Γ m graph. Print the graph in the same format as in the input.If multiple answers exist, print any of them. | The answer to the first sample is shown on the picture (1 β green color, 2 β blue, 3 β dark blue, 4 β pink).In the second sample there exists 4! answers, each of them is considered correct.In the third sample two vertices with equal colors are connected. So the correct vertex coloring couldn't be obtained. | Input: 3 5101010002001000 | Output: 131314242431313 | Expert | 0 | 899 | 363 | 227 | 4 | |
1,849 | C | 1849C | C. Binary String Copying | 1,600 | binary search; brute force; data structures; hashing; strings | You are given a string \(s\) consisting of \(n\) characters 0 and/or 1.You make \(m\) copies of this string, let the \(i\)-th copy be the string \(t_i\). Then you perform exactly one operation on each of the copies: for the \(i\)-th copy, you sort its substring \([l_i; r_i]\) (the substring from the \(l_i\)-th characte... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of \(s\) and the number of copies, respectively.The second line contains \(n\) characters 0 and/or 1 β th... | Print one integer β the number of different strings among \(t_1, t_2, \ldots, t_m\). | Consider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted: 101100 \(\rightarrow\) 011100; 101100 \(\rightarrow\) 011100; 101100 \(\rightarrow\) 101100; 101100 \(\rightarrow\) 101100; 101100 \(\rightarrow\) 000111. There are three different ... | Input: 36 51011001 21 32 45 51 66 41001112 21 41 31 21 101 1 | Output: 3 3 1 | Medium | 5 | 688 | 656 | 84 | 18 |
1,848 | E | 1848E | E. Vika and Stone Skipping | 2,600 | brute force; implementation; math; number theory | In Vika's hometown, Vladivostok, there is a beautiful sea.Often you can see kids skimming stones. This is the process of throwing a stone into the sea at a small angle, causing it to fly far and bounce several times off the water surface.Vika has skimmed stones many times and knows that if you throw a stone from the sh... | The first line of the input contains three integers \(x\) (\(1 \le x \le 10^9\)), \(q\) (\(1 \le q \le 10^5\)) and \(M\) (\(100 \le M \le 2 \cdot 10^9\)) β the initial coordinate for which Vika answered the question on her own, the number of integers \(x_i\) by which Vika will multiply the initial coordinate and prime ... | Output \(q\) integers, where the \(i\)-th number corresponds to the answer to Vika's question for the coordinate \(X_i\). Output all the answers modulo \(M\). | In the first sample, to make the stone touch the water at a point with coordinate \(2\), it needs to be thrown with a force of \(2\). To make the stone touch the water at a point with coordinate \(2 \cdot 3 = 6\), it needs to be thrown with a force of \(3\) or \(6\).In the second sample, you can skim a stone with a for... | Input: 1 2 179 2 3 | Output: 1 2 | Expert | 4 | 2,070 | 484 | 158 | 18 |
1,339 | A | 1339A | A. Filling Diamonds | 900 | brute force; dp; implementation; math | You have integer \(n\). Calculate how many ways are there to fully cover belt-like area of \(4n-2\) triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. \(2\) coverings are different if some \(2\) triangles are covered by the same diamo... | The first line contains a single integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(1 \le n \le 10^{9}\)). | For each test case, print the number of ways to fully cover belt-like area of \(4n-2\) triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed \(10^{18}\). | In the first test case, there are the following \(2\) ways to fill the area: In the second test case, there is a unique way to fill the area: | Input: 2 2 1 | Output: 2 1 | Beginner | 4 | 684 | 185 | 210 | 13 |
1,728 | B | 1728B | B. Best Permutation | 800 | constructive algorithms; greedy | Let's define the value of the permutation \(p\) of \(n\) integers \(1\), \(2\), ..., \(n\) (a permutation is an array where each element from \(1\) to \(n\) occurs exactly once) as follows: initially, an integer variable \(x\) is equal to \(0\); if \(x < p_1\), then add \(p_1\) to \(x\) (set \(x = x + p_1\)), otherwise... | The first line contains one integer \(t\) (\(1 \le t \le 97\)) β the number of test cases.The only line of each test case contains one integer \(n\) (\(4 \le n \le 100\)). | For each test case, print \(n\) integers β the permutation \(p\) of size \(n\) with the maximum possible value among all permutations of size \(n\). | Input: 3456 | Output: 2 1 3 4 1 2 3 4 5 4 5 1 2 3 6 | Beginner | 2 | 969 | 171 | 148 | 17 | |
1,973 | D | 1973D | D. Cat, Fox and Maximum Array Split | 2,400 | brute force; interactive; math | This is an interactive problem.Fox gave Cat two positive integers \(n\) and \(k\). She has a hidden array \(a_1, \ldots , a_n\) of length \(n\), such that \(1 \leq a_i \leq n\) for every \(i\). Now they are going to play the following game:For any two integers \(l, r\) such that \(1 \leq l \leq r \leq n\), define \(f(l... | The hidden arrays in the three testcases are \([1]\), \([1, 2]\) and \([1, 3, 6, 1, 2, 1]\). In the second testcase, no split satisfies the constraints, so the answer is \(-1\). The answer for the first query of the third testcase is \(7\) since no valid \(r\) exists. For the second query of the third testcase, since \... | Input: 3 1 1 1 2 2 1 3 1 6 3 7 2 3 6 1 | Output: ! 1 ? 1 1 ? 2 1 ! -1 ? 1 9 ? 1 6 ? 3 6 ? 4 6 ! 6 | Expert | 3 | 1,610 | 0 | 0 | 19 | ||
1,612 | B | 1612B | B. Special Permutation | 900 | constructive algorithms; greedy | A permutation of length \(n\) is an array \(p=[p_1,p_2,\dots, p_n]\) which contains every integer from \(1\) to \(n\) (inclusive) exactly once. For example, \(p=[4, 2, 6, 5, 3, 1]\) is a permutation of length \(6\).You are given three integers \(n\), \(a\) and \(b\), where \(n\) is an even number. Print any permutation... | The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)), the number of test cases in the test. The following \(t\) lines contain test case descriptions.Each test case description contains three integers \(n\), \(a\), \(b\) (\(2 \le n \le 100\); \(1 \le a,b \le n\); \(a \ne b\)), where \(n\) is an ... | For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them. | Input: 7 6 2 5 6 1 3 6 4 3 4 2 4 10 5 3 2 1 2 2 2 1 | Output: 4 2 6 5 3 1 -1 6 4 5 1 3 2 3 2 4 1 -1 1 2 2 1 | Beginner | 2 | 517 | 357 | 163 | 16 | |
1,520 | D | 1520D | D. Same Differences | 1,200 | data structures; hashing; math | You are given an array \(a\) of \(n\) integers. Count the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_j - a_i = j - i\). | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β array \(a\).It is guarante... | For each test case output the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_j - a_i = j - i\). | Input: 4 6 3 5 1 4 6 6 3 1 2 3 4 1 3 3 4 6 1 6 3 4 5 6 | Output: 1 3 3 10 | Easy | 3 | 142 | 398 | 114 | 15 | |
1,788 | F | 1788F | F. XOR, Tree, and Queries | 2,500 | bitmasks; constructive algorithms; dfs and similar; dsu; graphs; greedy; implementation; trees | You are given a tree of \(n\) vertices. The vertices are numbered from \(1\) to \(n\).You will need to assign a weight to each edge. Let the weight of the \(i\)-th edge be \(a_i\) (\(1 \leq i \leq n-1\)). The weight of each edge should be an integer between \(0\) and \(2^{30}-1\), inclusive.You are given \(q\) conditio... | The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 2.5 \cdot 10^5\), \(0 \le q \le 2.5 \cdot 10^5\)).The \(i\)-th of the following \(n-1\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \neq y_i\)), meaning that the \(i\)-th edge connects vertices \(x_i\) and \(y_i\) i... | If there do not exist \(a_1\), \(a_2\), ..., \(a_{n-1}\) that satisfy the given conditions, print ""No"".Otherwise, print ""Yes"" in the first line.Then print \(n-1\) integers on the next line, where the \(i\)-th integer is the weight of the \(i\)-th edge. If there are multiple solutions that satisfy the given conditio... | For the first example, there does not exist a set of edge weights that satisfies the given conditions.For the second example, the two given conditions are \(a_1 \oplus a_2 \oplus a_3=2\) and \(a_4 \oplus a_5=7\). There can be multiple solutions, for example, \((a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3)\).For the third e... | Input: 4 4 1 2 2 3 3 4 1 4 3 2 4 2 1 3 1 2 3 1 | Output: No | Expert | 8 | 850 | 661 | 733 | 17 |
1,559 | D1 | 1559D1 | D1. Mocha and Diana (Easy Version) | 1,400 | brute force; constructive algorithms; dsu; graphs; greedy; trees | This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if all versions of the problem are solved.A forest is an undirected graph without cycles (not necessarily connected).Mocha and Diana are friends in Zhijiang, both of them have a fore... | The first line contains three integers \(n\), \(m_1\) and \(m_2\) (\(1 \le n \le 1000\), \(0 \le m_1, m_2 < n\)) β the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.Each of the next \(m_1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the edge... | The first line contains only one integer \(h\), the maximum number of edges Mocha and Diana can add (in each forest).Each of the next \(h\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the edge you add each time.If there are multiple correct answers, you can print any one of them. | In the first example, we cannot add any edge.In the second example, the initial forests are as follows.We can add an edge \((2, 4)\). | Input: 3 2 2 1 2 2 3 1 2 1 3 | Output: 0 | Easy | 6 | 731 | 476 | 319 | 15 |
1,826 | D | 1826D | D. Running Miles | 1,700 | brute force; dp; greedy | There is a street with \(n\) sights, with sight number \(i\) being \(i\) miles from the beginning of the street. Sight number \(i\) has beauty \(b_i\). You want to start your morning jog \(l\) miles and end it \(r\) miles from the beginning of the street. By the time you run, you will see sights you run by (including s... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^5\)).The second line of each test case contains \(n\) integers \(b_i\) (\(1 \leq b_i \leq 10^8\)) β beauties of sights \(i\) miles from ... | For each test case output a single integer equal to the maximum value \(b_{i_1} + b_{i_2} + b_{i_3} - (r - l)\) for some running range \([l, r]\). | In the first example, we can choose \(l\) and \(r\) to be \(1\) and \(5\). So we visit all the sights and the three sights with the maximum beauty are the sights with indices \(1\), \(3\), and \(5\) with beauties \(5\), \(4\), and \(3\), respectively. So the total value is \(5 + 4 + 3 - (5 - 1) = 8\).In the second exam... | Input: 455 1 4 2 341 1 1 169 8 7 6 5 47100000000 1 100000000 1 100000000 1 100000000 | Output: 8 1 22 299999996 | Medium | 3 | 890 | 415 | 146 | 18 |
1,044 | B | 1044B | B. Intersecting Subtrees | 1,900 | dfs and similar; interactive; trees | You are playing a strange game with Li Chen. You have a tree with \(n\) nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from \(1\) to \(n\). Neither of you know the other's labelling of the tree.You and Li Chen each chose a subtree (i.e., a co... | For the first sample, Li Chen's hidden permutation is \([2, 3, 1]\), and for the second, his hidden permutation is \([5, 3, 2, 4, 1, 6]\) for both cases.In the first sample, there is a tree with three nodes in a line. On the top, is how you labeled the tree and the subtree you chose, and the bottom is how Li Chen label... | Input: 131 22 3111221 | Output: A 1B 2C 1 | Hard | 3 | 1,475 | 0 | 0 | 10 | ||
1,338 | B | 1338B | B. Edge Weight Assignment | 1,800 | bitmasks; constructive algorithms; dfs and similar; greedy; math; trees | You have unweighted tree of \(n\) vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves \(v_{1}\) and \(v_{2}\) of this tree, bitwise XOR of weights of all edges on the simple path between \(v_{1}\) and \(v_{2}\) has to be equal to \(0\). ... | The first line contains integer \(n\) (\(3 \le n \le 10^{5}\)) β the number of vertices in given tree.The \(i\)-th of the next \(n-1\) lines contains two integers \(a_{i}\) and \(b_{i}\) (\(1 \le a_{i} \lt b_{i} \le n\)) β it means there is an edge between \(a_{i}\) and \(b_{i}\). It is guaranteed that given graph form... | Print two integers β the minimum and maximum possible value of \(f\) can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. | In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. In the second example, possible assignments for each minimum and maximum are described in picture below. The \(f\) value of valid assig... | Input: 6 1 3 2 3 3 4 4 5 5 6 | Output: 1 4 | Medium | 6 | 1,004 | 345 | 195 | 13 |
426 | B | 426B | B. Sereja and Mirroring | 1,300 | implementation | Let's assume that we are given a matrix b of size x Γ y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x Γ y matrix c which has the following properties: the upper half of matrix c (rows with numbers from 1 to x) exactly matches b; the lower half of matrix c (rows with numbers fro... | The first line contains two integers, n and m (1 β€ n, m β€ 100). Each of the next n lines contains m integers β the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 β€ aij β€ 1) β the i-th row of the matrix a. | In the single line, print the answer to the problem β the minimum number of rows of matrix b. | In the first test sample the answer is a 2 Γ 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001 | Input: 4 30 0 11 1 01 1 00 0 1 | Output: 2 | Easy | 1 | 699 | 233 | 93 | 4 |
1,794 | E | 1794E | E. Labeling the Tree with Distances | 2,400 | data structures; dp; greedy; hashing; implementation; trees | You are given an unweighted tree of \(n\) vertices numbered from \(1\) to \(n\) and a list of \(n-1\) integers \(a_1, a_2, \ldots, a_{n-1}\). A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remain... | The first line contains one integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.The second line contains \(n - 1\) integers \(a_1,a_2,\ldots,a_{n-1}\) (\(0\le a_i < n\)) β the given list.Then, \(nβ1\) lines follow. Each of them contains two integers \(u\) and \(v\) (\(1\le u,v\le n\)) denotin... | In the first line print the number of good vertices.In the second line, print the indices of all good vertices in ascending order. | This is the tree for the first example: And these are two possible labelings with the elements on the list so that \(2\) is a good vertex (left) and \(4\) is a good vertex (right). The square below each vertex represents its label. The black squares contain the numbers which were on the given list and the only white sq... | Input: 6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6 | Output: 2 2 4 | Expert | 6 | 687 | 408 | 130 | 17 |
963 | D | 963D | D. Frequency of String | 2,500 | hashing; string suffix structures; strings | You are given a string \(s\). You should answer \(n\) queries. The \(i\)-th query consists of integer \(k_i\) and string \(m_i\). The answer for this query is the minimum length of such a string \(t\) that \(t\) is a substring of \(s\) and \(m_i\) has at least \(k_i\) occurrences as a substring in \(t\).A substring of ... | The first line contains string \(s\) \((1 \leq \left | s \right | \leq 10^{5})\).The second line contains an integer \(n\) (\(1 \leq n \leq 10^5\)).Each of next \(n\) lines contains an integer \(k_i\) \((1 \leq k_i \leq |s|)\) and a non-empty string \(m_i\) β parameters of the query with number \(i\), in this order.All... | For each query output the answer for it in a separate line.If a string \(m_{i}\) occurs in \(s\) less that \(k_{i}\) times, output -1. | Input: aaaaa53 a3 aa2 aaa3 aaaa1 aaaaa | Output: 344-15 | Expert | 3 | 476 | 465 | 134 | 9 | |
1,098 | F | 1098F | F. Π-function | 3,500 | string suffix structures; strings | The length of the longest common prefix of two strings \(s=s_1 s_2 \ldots s_n\) and \(t = t_1 t_2 \ldots t_m\) is defined as the maximum \(k \le \min(n, m)\) such that \(s_1 s_2 \ldots s_k\) equals \(t_1 t_2 \ldots t_k\). Let's denote the longest common prefix of two strings \(s\) and \(t\) as \(lcp(s,t)\).Z-function o... | The first line contains the string \(s\), consisting of lowercase English letters (\(1 \leq |s| \leq 200\,000\)). The second line contains one integer \(q\) β the number of queries (\(1 \leq q \leq 200\,000\)). Each of the following \(q\) lines contains two integers \(l_i\) and \(r_i\), describing the query (\(1 \leq l... | For every query output one integer: the value of Π-function of the corresponding substring. | In the first sample case there are four queries: the first query corresponds to the substring bb, and its Π-function equals \(2 + 1 = 3\); the second query corresponds to the substring abb, and its Π-function equals \(3 + 0 + 0 = 3\); the third query corresponds to the substring b, and its Π-function equals \(1\). the ... | Input: abbd 4 2 3 1 3 3 3 1 4 | Output: 3 3 1 4 | Master | 2 | 809 | 344 | 91 | 10 |
1,773 | K | 1773K | 1,900 | constructive algorithms | Hard | 1 | 0 | 0 | 0 | 17 | ||||||
1,726 | G | 1726G | G. A Certain Magical Party | 3,300 | combinatorics; data structures; greedy; sortings | There are \(n\) people at a party. The \(i\)-th person has an amount of happiness \(a_i\).Every person has a certain kind of personality which can be represented as a binary integer \(b\). If \(b = 0\), it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If... | The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of people.The second line contains a sequence of \(n\) integers \(a_1,a_2,...,a_n\) (\(1 \leq a_i \leq 2n\)) β the happiness values.The third line contains a sequence of \(n\) binary numbers \(b_1,b_2,...,b_n\) (\(b_i \in \{0,1... | Output the number of different valid speaking orders. Since this number can be large, output it modulo \(998244353\). | Here is the explanation for the first example. One valid speaking order is \([2,1,4,3]\) (here, we have written the indices of each person). Each step shows the current happiness values and results.Step \(1\): \([1,2,4,4]\) \(\rightarrow\) Person \(2\) tells the story to others. Since his kind of personality is \(1\), ... | Input: 4 1 2 4 4 1 1 0 0 | Output: 2 | Master | 4 | 1,315 | 353 | 117 | 17 |
2,070 | A | 2070A | A. FizzBuzz Remixed | 800 | brute force; math | FizzBuzz is one of the most well-known problems from coding interviews. In this problem, we will consider a remixed version of FizzBuzz:Given an integer \(n\), process all integers from \(0\) to \(n\). For every integer such that its remainders modulo \(3\) and modulo \(5\) are the same (so, for every integer \(i\) suc... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case contains one line consisting of one integer \(n\) (\(0 \le n \le 10^9\)). | For each test case, print one integer β the number of times the correct solution will print FizzBuzz with the given value of \(n\). | In the first test case, the solution will print FizzBuzz for the integer \(0\).In the second test case, the solution will print FizzBuzz for the integers \(0, 1, 2\).In the third test case, the solution will print FizzBuzz for the integers \(0, 1, 2, 15\). | Input: 7051542133717101997998244353 | Output: 1 3 4 9 270 3420402 199648872 | Beginner | 2 | 532 | 180 | 131 | 20 |
1,388 | A | 1388A | A. Captain Flint and Crew Recruitment | 800 | brute force; greedy; math; number theory | Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Next \(t\) lines contain test cases β one per line. The first and only line of each test case contains the single integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) β the number Flint guessed. | For each test case print: YES and \(4\) different positive integers such that at least \(3\) of them are nearly prime and their sum is equal to \(n\) (if there are multiple answers print any of them); NO if there is no way to represent \(n\) as the sum of \(4\) different positive integers where at least \(3\) of them a... | In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.In the third test case, \(n=31=2 \cdot 7 + 2 \cdot 5 + 2 \cdot 3 + 1\): integers \(14\), \(10\), \(6\) are nearly prime.In the fourth test case, \(n=36=5 + 2 \cdot 3 ... | Input: 7 7 23 31 36 44 100 258 | Output: NO NO YES 14 10 6 1 YES 5 6 10 15 YES 6 7 10 21 YES 2 10 33 55 YES 10 21 221 6 | Beginner | 4 | 945 | 283 | 391 | 13 |
131 | A | 131A | A. cAPS lOCK | 1,000 | implementation; strings | wHAT DO WE NEED cAPS LOCK FOR?Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by default. If it is pressed by accident, it leads to accidents like the one we had in the first passage. Let's consider that a word has been typed with the Caps lock key accidentally sw... | The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word's length is from 1 to 100 characters, inclusive. | Print the result of the given word's processing. | Input: cAPS | Output: Caps | Beginner | 2 | 730 | 159 | 48 | 1 | |
1,374 | D | 1374D | D. Zero Remainder Array | 1,400 | math; sortings; two pointers | You are given an array \(a\) consisting of \(n\) positive integers.Initially, you have an integer \(x = 0\). During one move, you can do one of the following two operations: Choose exactly one \(i\) from \(1\) to \(n\) and increase \(a_i\) by \(x\) (\(a_i := a_i + x\)), then increase \(x\) by \(1\) (\(x := x + 1\)). Ju... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9\)) β the length of \(a\) and the required divisior. The secon... | For each test case, print the answer β the minimum number of moves required to obtain such an array that each its element is divisible by \(k\). | Consider the first test case of the example: \(x=0\), \(a = [1, 2, 1, 3]\). Just increase \(x\); \(x=1\), \(a = [1, 2, 1, 3]\). Add \(x\) to the second element and increase \(x\); \(x=2\), \(a = [1, 3, 1, 3]\). Add \(x\) to the third element and increase \(x\); \(x=3\), \(a = [1, 3, 3, 3]\). Add \(x\) to the fourth ele... | Input: 5 4 3 1 2 1 3 10 6 8 7 1 8 3 7 5 10 8 9 5 10 20 100 50 20 100500 10 25 24 24 24 24 24 24 24 24 24 24 8 8 1 2 3 4 5 6 7 8 | Output: 6 18 0 227 8 | Easy | 3 | 653 | 568 | 144 | 13 |
920 | C | 920C | C. Swap Adjacent Elements | 1,400 | dfs and similar; greedy; math; sortings; two pointers | You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.For some indices i (1 β€ i β€ n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the nu... | The first line contains one integer n (2 β€ n β€ 200000) β the number of elements in the array.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 200000) β the elements of the array. Each integer from 1 to n appears exactly once.The third line contains a string of n - 1 characters, each character is either 0 o... | If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO. | In the first example you may swap a3 and a4, and then swap a4 and a5. | Input: 61 2 5 3 4 601110 | Output: YES | Easy | 5 | 505 | 480 | 139 | 9 |
1,856 | E1 | 1856E1 | E1. PermuTree (easy version) | 1,800 | dfs and similar; dp; trees | This is the easy version of the problem. The differences between the two versions are the constraint on \(n\) and the time limit. You can make hacks only if both versions of the problem are solved.You are given a tree with \(n\) vertices rooted at vertex \(1\).For some permutation\(^\dagger\) \(a\) of length \(n\), let... | The first line contains a single integer \(n\) (\(2 \le n \le 5000\)).The second line contains \(n - 1\) integers \(p_2,p_3,\ldots,p_n\) (\(1 \le p_i < i\)) indicating that there is an edge between vertices \(i\) and \(p_i\). | Output the maximum value of \(f(a)\). | The tree in the first test: One possible optimal permutation \(a\) is \([2, 1, 4, 5, 3]\) with \(4\) suitable pairs of vertices: \((2, 3)\), since \(\operatorname{lca}(2, 3) = 1\) and \(1 < 2 < 4\), \((2, 4)\), since \(\operatorname{lca}(2, 4) = 1\) and \(1 < 2 < 5\), \((2, 5)\), since \(\operatorname{lca}(2, 5) = 1\) ... | Input: 5 1 1 3 3 | Output: 4 | Medium | 3 | 954 | 225 | 37 | 18 |
1,479 | C | 1479C | C. Continuous City | 2,500 | bitmasks; constructive algorithms | Some time ago Homer lived in a beautiful city. There were \(n\) blocks numbered from \(1\) to \(n\) and \(m\) directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one... | The single line contains two integers \(L\) and \(R\) (\(1 \leq L \leq R \leq 10^6\)). | If it is impossible to find a \((L, R)\)-continuous city within \(32\) blocks, print ""NO"" in a single line.Otherwise, print ""YES"" in the first line followed by a description of a \((L, R)\)-continuous city. The second line should contain two integers \(n\) (\(2 \leq n \leq 32\)) and \(m\) (\(1 \leq m \leq \frac {n(... | In the first example there is only one path from block \(1\) to block \(n = 2\), and its length is \(1\). In the second example there are three paths from block \(1\) to block \(n = 5\), which are \(1 \to 2 \to 5\) of length \(4\), \(1 \to 3 \to 5\) of length \(5\) and \(1 \to 4 \to 5\) of length \(6\). | Input: 1 1 | Output: YES 2 1 1 2 1 | Expert | 2 | 1,557 | 86 | 863 | 14 |
44 | D | 44D | D. Hyperdrive | 1,800 | math | In a far away galaxy there are n inhabited planets, numbered with numbers from 1 to n. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was invented. As soon as this significant event took place, n - 1 spaceships ... | The first line contains a number n (3 β€ n β€ 5000) β the number of inhabited planets in the galaxy. The next n lines contain integer coordinates of the planets in format ""xi yi zi"" ( - 104 β€ xi, yi, zi β€ 104). | Print the single number β the solution to the task with an absolute or relative error not exceeding 10 - 6. | Input: 40 0 00 0 10 1 01 0 0 | Output: 1.7071067812 | Medium | 1 | 2,174 | 210 | 107 | 0 | |
177 | A1 | 177A1 | A1. Good Matrix Elements | 800 | implementation | The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the ""middle"" row β the row which has exactly rows above... | The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.The input limitations for getting 30 points are: 1 β€ n β€ 5 The input limitations for getting 100 points are: 1 β€ n β€ 101 | Print a single integer β the sum of good matrix elements. | In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | Input: 31 2 34 5 67 8 9 | Output: 45 | Beginner | 1 | 650 | 309 | 57 | 1 |
220 | A | 220A | A. Little Elephant and Problem | 1,300 | implementation; sortings | The Little Elephant has got a problem β somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array.The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could ha... | The first line contains a single integer n (2 β€ n β€ 105) β the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β array a.Note that the elements of the array are not necessarily distinct numbers. | In a single line print ""YES"" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and ""NO"" (without the quotes) otherwise. | In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is ""YES"".In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is ""YES"".In the third sample we ... | Input: 21 2 | Output: YES | Easy | 2 | 662 | 257 | 168 | 2 |
1,627 | B | 1627B | B. Not Sitting | 1,300 | games; greedy; sortings | Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a \(n \times m\) grid. The seat in row \(r\) and column \(c\) is denoted by \((r, c)\), and the distance between two seats \((a,b)\) and \((c,d)\) is \(|a-c| + ... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) (\(2 \leq n \cdot m \leq 10^5\)) β the number of rows and columns... | For each test case, output \(n \cdot m\) ordered integers β the distance between Rahul and Tina if both of them act optimally for every \(k \in [0, n \cdot m - 1]\). | One possible sequence of choices for the first testcase where Tina has \(k=3\) buckets of paints is as follows.Tina paints the seats at positions \((1, 2)\), \((2, 2)\), \((3, 2)\) with pink paint. Rahul chooses the seat at \((3, 1)\) after which Tina chooses to sit at \((1, 3)\). Therefore, the distance between Tina a... | Input: 24 31 2 | Output: 3 3 4 4 4 4 4 4 5 5 5 5 1 1 | Easy | 3 | 1,396 | 419 | 165 | 16 |
1,999 | E | 1999E | E. Triple Operations | 1,300 | dp; implementation; math | On the board Ivy wrote down all integers from \(l\) to \(r\), inclusive.In an operation, she does the following: pick two numbers \(x\) and \(y\) on the board, erase them, and in their place write the numbers \(3x\) and \(\lfloor \frac{y}{3} \rfloor\). (Here \(\lfloor \bullet \rfloor\) denotes rounding down to the near... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The only line of each test case contains two integers \(l\) and \(r\) (\(1 \leq l < r \leq 2 \cdot 10^5\)). | For each test case, output a single integer β the minimum number of operations needed to make all numbers on the board equal \(0\). | In the first test case, we can perform \(5\) operations as follows: $$$\( 1,2,3 \xrightarrow[x=1,\,y=2]{} 3,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,0 .\)$$$ | Input: 41 32 4199999 20000019 84 | Output: 5 6 36 263 | Easy | 3 | 476 | 200 | 131 | 19 |
1,992 | B | 1992B | B. Angry Monk | 800 | greedy; math; sortings | To celebrate his recovery, k1o0n has baked an enormous \(n\) metres long potato casserole.Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into \(k\) pieces, of lengths \(a_1, a_2, \dots, a_k\) meters.k1o0n wasn't keen on that. Luckily, everything can be fixed. In ord... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of each test case consists of two lines. The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 10^9\), \(2 \le k \le 10^5\)) β length of casserole and the number of pieces.The... | For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk. | Input: 45 33 1 15 23 211 42 3 1 516 61 6 1 1 1 6 | Output: 2 3 9 15 | Beginner | 3 | 1,252 | 585 | 124 | 19 | |
1,634 | C | 1634C | C. OKEA | 1,000 | constructive algorithms | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.β Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work β that is, robots!The department you wor... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k \le 500\)) β the number of shelves and length of each shelf, respectively.It is guaranteed that the sum \(n\) over all test cases ... | Print the answer for each test case.If such an arrangement exists, print ""YES"" on a single line. After that, print any example on \(n\) lines of \(k\) numbers each, one line per shelf. Each number from \(1\) to \(n \cdot k\) must occur exactly once in the output.If no good arrangement exists, print a single word ""NO... | Input: 41 12 23 33 1 | Output: YES 1 YES 1 3 2 4 NO YES 1 2 3 | Beginner | 1 | 1,557 | 406 | 339 | 16 | |
1,491 | C | 1491C | C. Pekora and Trampoline | 1,700 | brute force; data structures; dp; greedy; implementation | There is a trampoline park with \(n\) trampolines in a line. The \(i\)-th of which has strength \(S_i\).Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice. If at the moment Pekora jumps on trampoline \(i\), the trampoline will launch her to position \(i + S... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β the number of trampolines.The second line of each test case contains \(n\) integers \(S_1, S_2, \dots, S_n\) (\(1 \le S_i \le 10^9\)... | For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all \(S_i\) to \(1\). | For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.) \([1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]\) \([1,\textbf{4},1,2,1,\textbf{2},1]\) \([1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]\) \([1,\textbf{2},1,... | Input: 3 7 1 4 2 2 2 2 2 2 2 3 5 1 1 1 1 1 | Output: 4 3 0 | Medium | 5 | 1,010 | 462 | 125 | 14 |
1,004 | A | 1004A | A. Sonya and Hotels | 900 | implementation | Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has \(n\) hotels, where the \(i\)-th hotel is located in the city with coord... | The first line contains two integers \(n\) and \(d\) (\(1\leq n\leq 100\), \(1\leq d\leq 10^9\)) β the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.The second line contains \(n\) different integers in strictly increasing order \(x_1, x_2, \ldots, x_n\) (\(-10^9\leq x_i\leq 10^... | Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to \(d\). | In the first example, there are \(6\) possible cities where Sonya can build a hotel. These cities have coordinates \(-6\), \(5\), \(6\), \(12\), \(13\), and \(19\).In the second example, there are \(5\) possible cities where Sonya can build a hotel. These cities have coordinates \(2\), \(6\), \(13\), \(16\), and \(21\)... | Input: 4 3-3 2 9 16 | Output: 6 | Beginner | 1 | 1,045 | 357 | 138 | 10 |
1,696 | G | 1696G | G. Fishingprince Plays With Array Again | 3,300 | brute force; data structures; geometry; math | Suppose you are given a 1-indexed sequence \(a\) of non-negative integers, whose length is \(n\), and two integers \(x\), \(y\). In consecutive \(t\) seconds (\(t\) can be any positive real number), you can do one of the following operations: Select \(1\le i<n\), decrease \(a_i\) by \(x\cdot t\), and decrease \(a_{i+1}... | The first line of input contains two integers \(n\) and \(q\) (\(2\le n\le 2\cdot 10^5\), \(1\le q\le 2\cdot 10^5\)).The second line of input contains two integers \(x\) and \(y\) (\(1\le x,y\le 10^6\)).The third line of input contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(1\le b_i\le 10^6\)).This is followed by \(q... | For each query of type \(2\), print one real number β the answer to the query. Your answer is considered correct if its absolute error or relative error does not exceed \(10^{-9}\). | Let's analyse the sample.In the first query, we are asked to compute \(f([3,1,1,4])\). The answer is \(3.5\). One optimal sequence of operations is: In the first \(1.5\) seconds do the second operation with \(i=1\). In the next \(2\) seconds do the first operation with \(i=3\). In the third query, we are asked to compu... | Input: 4 3 1 2 3 1 1 4 2 1 4 1 1 1 2 1 3 | Output: 3.500000000000000 1.000000000000000 | Master | 4 | 1,224 | 724 | 181 | 16 |
2,112 | D | 2112D | D. Reachability and Tree | 1,700 | constructive algorithms; dfs and similar; graphs; trees | Let \(u\) and \(v\) be two distinct vertices in a directed graph. Let's call the ordered pair \((u, v)\) good if there exists a path from vertex \(u\) to vertex \(v\) along the edges of the graph.You are given an undirected tree with \(n\) vertices and \(n - 1\) edges. Determine whether it is possible to assign a direc... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.The next \(n - 1\) lines describe the edges. The \(i\)-th line contains two integers \(u_i\) and \(v... | For each test case, print ""NO"" (case-insensitive) if it is impossible to direct all edges of the tree and obtain exactly \(n\) good pairs of vertices.Otherwise, print ""YES"" (case-insensitive) and then print \(n - 1\) pairs of integers \(u_i\) and \(v_i\) separated by spaces β the edges directed from \(u_i\) to \(v_... | The tree from the first test case and its possible directed version are shown in the legend above. In this version, there are exactly \(5\) good pairs of vertices: \((3, 5)\), \((3, 1)\), \((3, 2)\), \((1, 2)\), and \((4, 2)\).One possible directed version of the tree from the second test case is shown below: In the pr... | Input: 451 22 41 33 551 21 31 44 522 143 11 22 4 | Output: YES 1 2 3 1 3 5 4 2 YES 2 1 3 1 4 1 5 4 NO YES 1 3 2 1 2 4 | Medium | 4 | 566 | 569 | 405 | 21 |
1,387 | B2 | 1387B2 | B2. Village (Maximum) | 2,500 | *special; dfs and similar; trees | This problem is split into two tasks. In this task, you are required to find the maximum possible answer. In the task Village (Minimum) you are required to find the minimum possible answer. Each task is worth \(50\) points.There are \(N\) houses in a certain village. A single villager lives in each of the houses. The h... | The first line contains an integer \(N\) (\(1 < N \le 10^5\)). Houses are numbered by consecutive integers \(1, 2, \ldots, N\).Then \(N-1\) lines follow that describe the roads. Each line contains two integers \(a\) and \(b\) (\(1 \le a, b \le N\), \(a \neq b\)) denoting that there is a road connecting houses \(a\) and... | In the first line output the largest total length of the shortest paths in kilometers.In the second line describe one valid assignment of the new houses with the largest total length: \(N\) space-separated distinct integers \(v_1, v_2, \ldots, v_N\). For each \(i\), \(v_i\) is the house number where villager from the h... | Input: 4 1 2 2 3 3 4 | Output: 8 4 3 2 1 | Expert | 3 | 1,332 | 327 | 421 | 13 | |
105 | A | 105A | A. Transmigration | 1,700 | implementation | In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes... | The first line contains three numbers n, m and k β the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; n and m are integers, and k is a real number with exactly two digits after decimal p... | Print on the first line number z β the number of skills the character will have after the transmigration. Then print z lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order. | Input: 5 4 0.75axe 350impaler 300ionize 80megafire 120magicboost 220healmegafireshieldmagicboost | Output: 6axe 262heal 0impaler 225magicboost 165megafire 0shield 0 | Medium | 1 | 1,584 | 954 | 260 | 1 | |
372 | A | 372A | A. Counting Kangaroos is Fun | 1,600 | binary search; greedy; sortings; two pointers | There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.Each kangaroo can hold at most one kangaroo, and the kangaroo who is he... | The first line contains a single integer β n (1 β€ n β€ 5Β·105). Each of the next n lines contains an integer si β the size of the i-th kangaroo (1 β€ si β€ 105). | Output a single integer β the optimal number of visible kangaroos. | Input: 825769842 | Output: 5 | Medium | 4 | 539 | 157 | 66 | 3 | |
1,335 | A | 1335A | A. Candies and Two Sisters | 800 | math | There are two sisters Alice and Betty. You have \(n\) candies. You want to distribute these \(n\) candies between two sisters in such a way that: Alice will get \(a\) (\(a > 0\)) candies; Betty will get \(b\) (\(b > 0\)) candies; each sister will get some integer number of candies; Alice will get a greater amount of ca... | 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 a test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^9\)) β the number of candies you have. | For each test case, print the answer β the number of ways to distribute exactly \(n\) candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print \(0\). | For the test case of the example, the \(3\) possible ways to distribute candies are: \(a=6\), \(b=1\); \(a=5\), \(b=2\); \(a=4\), \(b=3\). | Input: 6 7 1 2 3 2000000000 763243547 | Output: 3 0 0 1 999999999 381621773 | Beginner | 1 | 760 | 255 | 221 | 13 |
1,463 | A | 1463A | A. Dungeon | 1,100 | binary search; math | You are playing a new computer game in which you have to fight monsters. In a dungeon you are trying to clear, you met three monsters; the first of them has \(a\) health points, the second has \(b\) health points, and the third has \(c\).To kill the monsters, you can use a cannon that, when fired, deals \(1\) damage to... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of a single line that contains three integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10^8\)) β the number of health points each monster has. | For each test case, print YES if you can kill all the monsters with the same enhanced shot. Otherwise, print NO. 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, you can do as follows: \(1\)-th shot to the first monster, \(2\)-th shot to the second monster, \(3\)-th shot to the third monster, \(4\)-th shot to the first monster, \(5\)-th shot to the third monster, \(6\)-th shot to the third monster, and \(7\)-th enhanced shot will kill all the monsters.In... | Input: 3 3 2 4 1 1 1 10 1 7 | Output: YES NO NO | Easy | 2 | 954 | 266 | 227 | 14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.