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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
547 | C | 547C | C. Mike and Foam | 2,300 | bitmasks; combinatorics; dp; math; number theory | Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1 to n. i-th kind of beer has ai milliliters of foam on it. Maxim is Mike's boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives ... | The first line of input contains numbers n and q (1 β€ n, q β€ 2 Γ 105), the number of different kinds of beer and number of queries.The next line contains n space separated integers, a1, a2, ... , an (1 β€ ai β€ 5 Γ 105), the height of foam in top of each kind of beer.The next q lines contain the queries. Each query consi... | For each query, print the answer for that query in one line. | Input: 5 61 2 3 4 6123451 | Output: 013562 | Expert | 5 | 783 | 434 | 60 | 5 | |
1,424 | M | 1424M | M. Ancient Language | 2,200 | graphs; sortings | While exploring the old caves, researchers found a book, or more precisely, a stash of mixed pages from a book. Luckily, all of the original pages are present and each page contains its number. Therefore, the researchers can reconstruct the book.After taking a deeper look into the contents of these pages, linguists thi... | First-line contains two integers: \(n\) and \(k\) (\(1 \le n, k \le 10^3\)) β the number of pages that scientists have found and the number of words present at each page. Following \(n\) groups contain a line with a single integer \(p_i\) (\(0 \le n \lt 10^3\)) β the number of \(i\)-th page, as well as \(k\) lines, eac... | Output a string representing the reconstructed alphabet of this ancient civilization. If the book found is not a dictionary, output ""IMPOSSIBLE"" without quotes. In case there are multiple solutions, output any of them. | Input: 3 3 2 b b bbac 0 a aca acba 1 ab c ccb | Output: acb | Hard | 2 | 746 | 421 | 220 | 14 | |
1,841 | A | 1841A | A. Game with Board | 800 | constructive algorithms; games | Alice and Bob play a game. They have a blackboard; initially, there are \(n\) integers written on it, and each integer is equal to \(1\).Alice and Bob take turns; Alice goes first. On their turn, the player has to choose several (at least two) equal integers on the board, wipe them and write a new integer which is equa... | The first line contains one integer \(t\) (\(1 \le t \le 99\)) β the number of test cases.Each test case consists of one line containing one integer \(n\) (\(2 \le n \le 100\)) β the number of integers equal to \(1\) on the board. | For each test case, print Alice if Alice wins when both players play optimally. Otherwise, print Bob. | In the first test case, \(n = 3\), so the board initially contains integers \(\{1, 1, 1\}\). We can show that Bob can always win as follows: there are two possible first moves for Alice. if Alice chooses two integers equal to \(1\), wipes them and writes \(2\), the board becomes \(\{1, 2\}\). Bob cannot make a move, so... | Input: 236 | Output: Bob Alice | Beginner | 2 | 963 | 230 | 101 | 18 |
633 | H | 633H | H. Fibonacci-ish II | 3,100 | data structures; implementation | Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials. Fibonacci-ish potential of an array ai is computed as follows: Remove all elements j if there exists i < j such that ai = aj. Sort the remaining elements in... | The first line of the input contains integers of n and m (1 β€ n, m β€ 30 000) β the length of the initial array and the modulo, respectively.The next line contains n integers ai (0 β€ ai β€ 109) β elements of the array.Then follow the number of ranges q (1 β€ q β€ 30 000).Last q lines contain pairs of indices li and ri (1 β€... | Print q lines, i-th of them must contain the Fibonacci-ish potential of the i-th range modulo m. | For the purpose of this problem define Fibonacci numbers as follows: F1 = F2 = 1. Fn = Fn - 1 + Fn - 2 for each n > 2. In the first query, the subarray [1,2,1] can be formed using the minimal set {1,2}. Thus, the potential of this subarray is 1*1+2*1=3. | Input: 5 102 1 2 1 222 44 5 | Output: 33 | Master | 2 | 731 | 379 | 96 | 6 |
16 | A | 16A | A. Flag | 800 | implementation | According to a new ISO standard, a flag of every country should have a chequered field n Γ m, each square should be of one of 10 colours, and the flag should be Β«stripedΒ»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland'... | The first line of the input contains numbers n and m (1 β€ n, m β€ 100), n β the amount of rows, m β the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the... | Output YES, if the flag meets the new ISO standard, and NO otherwise. | Input: 3 3000111222 | Output: YES | Beginner | 1 | 401 | 342 | 69 | 0 | |
1,023 | B | 1023B | B. Pair of Toys | 1,000 | math | Tanechka is shopping in the toy shop. There are exactly \(n\) toys in the shop for sale, the cost of the \(i\)-th toy is \(i\) burles. She wants to choose two toys in such a way that their total cost is \(k\) burles. How many ways to do that does she have?Each toy appears in the shop exactly once. Pairs \((a, b)\) and ... | The first line of the input contains two integers \(n\), \(k\) (\(1 \le n, k \le 10^{14}\)) β the number of toys and the expected total cost of the pair of toys. | Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is \(k\) burles. | In the first example Tanechka can choose the pair of toys (\(1, 4\)) or the pair of toys (\(2, 3\)).In the second example Tanechka can choose only the pair of toys (\(7, 8\)).In the third example choosing any pair of toys will lead to the total cost less than \(20\). So the answer is 0.In the fourth example she can cho... | Input: 8 5 | Output: 2 | Beginner | 1 | 402 | 161 | 184 | 10 |
938 | G | 938G | G. Shortest Path Queries | 2,900 | bitmasks; data structures; dsu; graphs | You are given an undirected connected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). There are three types of queries you ha... | The first line contains two numbers n and m (1 β€ n, m β€ 200000) β the number of vertices and the number of edges in the graph, respectively.Then m lines follow denoting the edges of the graph. Each line contains three integers x, y and d (1 β€ x < y β€ n, 0 β€ d β€ 230 - 1). Each pair (x, y) is listed at most once. The ini... | Print the answers for all queries of type 3 in the order they appear in input. | Input: 5 51 2 32 3 43 4 54 5 61 5 153 1 51 1 3 13 1 52 1 53 1 5 | Output: 112 | Master | 4 | 783 | 1,034 | 78 | 9 | |
1,698 | B | 1698B | B. Rising Sand | 800 | constructive algorithms; greedy; implementation | There are \(n\) piles of sand where the \(i\)-th pile has \(a_i\) blocks of sand. The \(i\)-th pile is called too tall if \(1 < i < n\) and \(a_i > a_{i-1} + a_{i+1}\). That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \leq n \leq 2 \cdot 10^5\); \(1 \leq k \leq n\)) β the number of ... | For each test case, output a single integer β the maximum number of piles that are simultaneously too tall after some (possibly zero) operations. | In the first test case, we can perform the following three operations: Add one unit of sand to piles \(1\) and \(2\): \([\color{red}{3}, \color{red}{10}, 2, 4, 1]\). Add one unit of sand to piles \(4\) and \(5\): \([3, 10, 2, \color{red}{5}, \color{red}{2}]\). Add one unit of sand to piles \(3\) and \(4\): \([3, 10, \c... | Input: 35 22 9 2 4 14 41 3 2 13 11 3 1 | Output: 2 0 1 | Beginner | 3 | 682 | 604 | 145 | 16 |
1,858 | B | 1858B | B. The Walkway | 1,500 | brute force; dp; greedy; math; number theory | There are \(n\) benches near the Main Walkway in Summer Infomatics School. These benches are numbered by integers from \(1\) to \(n\) in order they follow. Also there are \(m\) cookie sellers near the Walkway. The \(i\)-th (\(1 \le i \le m\)) cookie sellers is located near the \(s_i\)-th bench.Petya is standing in the ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\) and \(d\) (\(2 \le d \le n \le 10^9\), \(2 \le m \le \min(10^{5}, n)\)) β the number of benches, the number of cookie sellers and the value of parameter ... | For each test case print two integers β the minimum number of cookies that Petya can eat if exactly one cookie seller is removed, and the number of cookie sellers such that if one of them is removed, Petya will eat the minimum possible number of cookies. | In the first test case \(n=6\), \(m=2\), \(d=2\) and \(s=[2, 5]\). If no cookie seller is removed, then Petya will eat \(4\) cookies during his walk (note that you have to remove exactly one cookie seller; this case is explained only to show how Petya decides whether to eat a cookie): Petya will eat a cookie near the \... | Input: 86 2 22 58 3 23 5 810 4 92 8 9 1030 5 86 8 15 24 2930 5 86 8 12 20 278 8 31 2 3 4 5 6 7 82 2 21 21000000000 3 2000000057008429 66778899 837653445 | Output: 3 1 4 1 4 4 6 4 5 2 7 7 1 1 51 1 | Medium | 5 | 1,942 | 662 | 254 | 18 |
788 | B | 788B | B. Weird journey | 2,100 | combinatorics; constructive algorithms; dfs and similar; dsu; graphs | Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland β Uzhlyandia.It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending i... | The first line contains two integers n, m (1 β€ n, m β€ 106) β the number of cities and roads in Uzhlyandia, respectively.Each of the next m lines contains two integers u and v (1 β€ u, v β€ n) that mean that there is road between cities u and v.It is guaranteed that no road will be given in the input twice. That also mean... | Print out the only integer β the number of good paths in Uzhlyandia. | In first sample test case the good paths are: 2 β 1 β 3 β 1 β 4 β 1 β 5, 2 β 1 β 3 β 1 β 5 β 1 β 4, 2 β 1 β 4 β 1 β 5 β 1 β 3, 3 β 1 β 2 β 1 β 4 β 1 β 5, 3 β 1 β 2 β 1 β 5 β 1 β 4, 4 β 1 β 2 β 1 β 3 β 1 β 5. There are good paths that are same with displayed above, because the sets of roads they pass over once are same:... | Input: 5 41 21 31 41 5 | Output: 6 | Hard | 5 | 767 | 406 | 68 | 7 |
1,346 | C | 1346C | C. Spring Cleaning | 1,600 | *special; greedy; sortings | Tanya wants to organize her bookcase. There are \(n\) bookshelves in the bookcase, the \(i\)-th bookshelf contains \(a_i\) books on it. Tanya will be satisfied if each bookshelf contains no more than \(k\) books.Tanya can do one of the two following operations to achieve her goal: Choose exactly one bookshelf and put a... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains four integers \(n, k, x\) and \(y\) (\(1 \le k \le n \le 2 \cdot 10^5; 1 \le x, y \le 10^4\)) β the number of bookshelves, the maximum required ... | For each test case, print the answer β the minimum number of seconds Tanya has to spend to obtain the bookcase with no more than \(k\) books on each bookshelf. | In the first test case of the example, it's optimal to use the first operation on the fifth bookshelf. So the array \(a\) becomes \([1, 2, 2, 3, 5] \rightarrow [1, 2, 2, 3, 0]\).In the second test case of the example, it's optimal to use the first operation on the second bookshelf and then use the second operation. So ... | Input: 6 5 4 3 5 1 2 2 3 5 5 3 4 5 1 5 1 5 5 5 4 5 6 1 2 5 3 5 4 3 2 10 4 4 1 1 4 3 10 2 4 4 1 1 4 1 5 4 1 2 1 3 | Output: 3 9 6 4 2 9 | Medium | 3 | 1,209 | 738 | 159 | 13 |
1,405 | B | 1405B | B. Array Cancellation | 1,000 | constructive algorithms; implementation | You're given an array \(a\) of \(n\) integers, such that \(a_1 + a_2 + \cdots + a_n = 0\).In one operation, you can choose two different indices \(i\) and \(j\) (\(1 \le i, j \le n\)), decrement \(a_i\) by one and increment \(a_j\) by one. If \(i < j\) this operation is free, otherwise it costs one coin.How many coins ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^5\)) β the number of elements.The next line contains \(n\) integers \(a_1, \ldots, a_n... | For each test case, print the minimum number of coins we have to spend in order to make all elements equal to \(0\). | Possible strategy for the first test case: Do \((i=2, j=3)\) three times (free), \(a = [-3, 2, 0, 1]\). Do \((i=2, j=1)\) two times (pay two coins), \(a = [-1, 0, 0, 1]\). Do \((i=4, j=1)\) one time (pay one coin), \(a = [0, 0, 0, 0]\). | Input: 7 4 -3 5 -3 1 2 1 -1 4 -3 2 -3 4 4 -1 1 1 -1 7 -5 7 -6 -4 17 -13 4 6 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1 0 | Output: 3 0 4 1 8 3000000000 0 | Beginner | 2 | 386 | 479 | 116 | 14 |
329 | E | 329E | E. Evil | 3,100 | math | There are n cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all n cities. The length of this Hamiltonian cycle is defined as the sum of the distan... | The first line contains an integer n (3 β€ n β€ 105). Then n lines follow, each consisting of two integers xi and yi (0 β€ xi, yi β€ 109), denoting the coordinates of a city. All given points will be distinct. | A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6.The Manhattan distance between two cities (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|. | Input: 41 11 22 12 2 | Output: 6 | Master | 1 | 524 | 205 | 293 | 3 |
2,038 | A | 2038A | A. Bonus Project | 1,400 | games; greedy | There is a team of \(n\) software engineers numbered from \(1\) to \(n\). Their boss promises to give them a bonus if they complete an additional project. The project requires \(k\) units of work in total. The bonus promised to the \(i\)-th engineer is \(a_i\) burles. The boss doesn't assign specific tasks to engineers... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 1000\); \(1 \le k \le 10^6\)) β the number of engineers in the company and the number of work units the project requires, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the bonus... | Print \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le k\)) β the amount of work completed by each engineer given that every engineer behaves optimally. Note that the answer is unique. | In the first example, engineers distributed the work across them and got the bonus, even though the benefit for the third engineer is zero. In the second example, the bonus project requires too many work units to complete, so it's more beneficial for engineers not to work at all. | Input: 3 64 7 61 2 3 | Output: 1 3 2 | Easy | 2 | 2,290 | 545 | 192 | 20 |
936 | A | 936A | A. Save Energy! | 1,700 | binary search; implementation; math | Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The sto... | The single line contains three integers k, d and t (1 β€ k, d, t β€ 1018). | Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9.Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if . | In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the... | Input: 3 2 6 | Output: 6.5 | Medium | 3 | 704 | 72 | 249 | 9 |
571 | B | 571B | B. Minimization | 2,000 | dp; greedy; sortings | You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.You need to permute the array elements so that value became minimal possible. In particular, it is allowed not to change order of elements at all. | The first line contains two integers n, k (2 β€ n β€ 3Β·105, 1 β€ k β€ min(5000, n - 1)). The second line contains n integers A[1], A[2], ..., A[n] ( - 109 β€ A[i] β€ 109), separate by spaces β elements of the array A. | Print the minimum possible value of the sum described in the statement. | In the first test one of the optimal permutations is 1 4 2. In the second test the initial order is optimal. In the third test one of the optimal permutations is 2 3 4 4 3 5. | Input: 3 21 2 4 | Output: 1 | Hard | 3 | 260 | 211 | 71 | 5 |
1,494 | E | 1494E | E. A-Z Graph | 2,400 | constructive algorithms; data structures; graphs; hashing | You are given a directed graph consisting of \(n\) vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty.You should process \(m\) queries with it. Each query is one of three types: ""\(+\) \(u\) \(v\) \(c\)"" β add arc from \(u\) to \(v\) with label \(c\). It's guaranteed ... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le m \le 2 \cdot 10^5\)) β the number of vertices in the graph and the number of queries.The next \(m\) lines contain queries β one per line. Each query is one of three types: ""\(+\) \(u\) \(v\) \(c\)"" (\(1 \le u, v \le n\); \(u ... | For each query of the third type, print YES if there exist the sequence \(v_1, v_2, \dots, v_k\) described above, or NO otherwise. | In the first query of the third type \(k = 3\), we can, for example, choose a sequence \([1, 2, 3]\), since \(1 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3\) and \(3 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 1\).In the second query of the third type \(k = 2\), and we can't find sequence \(p_1, p_2\) such th... | Input: 3 11 + 1 2 a + 2 3 b + 3 2 a + 2 1 b ? 3 ? 2 - 2 1 - 3 2 + 2 1 c + 3 2 d ? 5 | Output: YES NO YES | Expert | 4 | 829 | 599 | 130 | 14 |
1,239 | B | 1239B | B. The World Is Just a Programming Task (Hard Version) | 2,500 | implementation | This is a harder version of the problem. In this version, \(n \le 300\,000\).Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya... | The first line contains an integer \(n\) (\(1 \le n \le 300\,000\)), the length of the string.The second line contains a string, consisting of exactly \(n\) characters, where each of the characters is either ""("" or "")"". | The first line should contain a single integer β the largest beauty of the string, which can be achieved by swapping some two characters.The second line should contain integers \(l\) and \(r\) (\(1 \leq l, r \leq n\)) β the indices of two characters, which should be swapped in order to maximize the string's beauty.In c... | In the first example, we can swap \(7\)-th and \(8\)-th character, obtaining a string ""()()()()()"". The cyclical shifts by \(0, 2, 4, 6, 8\) of this string form a correct bracket sequence.In the second example, after swapping \(5\)-th and \(10\)-th character, we obtain a string "")(())()()(()"". The cyclical shifts b... | Input: 10 ()()())(() | Output: 5 8 7 | Expert | 1 | 1,479 | 223 | 376 | 12 |
1,980 | F2 | 1980F2 | F2. Field Division (hard version) | 2,400 | math; sortings | This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.Alice and Bob are dividing the field. The field is a rectangle of size \(n \times m\) (\(2 ... | 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 three integers \(n\), \(m\), and \(k\) (\(2 \le n, m \le 10^9\), \(2 \le k \le 2 \cdot 10^5\)) β the field sizes and the number of fountains, respectively.Then follow \(k\) lines, e... | For each test case, first output \(\alpha\) β the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output \(k\) non-negative integers \(a_1, a_2, \dots, a_k\), where \(a_i\) is a value such that after Bob gives Alice the \(i\)-th fountain, the maximum size of her plo... | Below are the images for the second example: The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain \(1\) or fountain \(3\), then that fountain cannot be on Alice's plot. | Input: 52 2 31 11 22 25 5 41 22 23 44 32 5 91 21 51 12 22 42 51 42 31 36 4 46 21 31 41 23 4 52 13 21 41 32 4 | Output: 1 1 0 1 11 0 1 0 4 1 0 0 1 1 0 0 0 0 0 6 15 0 0 0 1 2 3 0 0 0 | Expert | 2 | 1,808 | 636 | 347 | 19 |
870 | F | 870F | F. Paths | 2,700 | data structures; number theory | You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such a way that there is an edge between vertices u and v if and only if . Let d(u, v) be the shortest distance between u and v, or 0 if there is no path between them. Compute the sum of values d(u, v) over all 1 β€ u < v β€ n.The gcd (gr... | Single integer n (1 β€ n β€ 107). | Print the sum of d(u, v) over all 1 β€ u < v β€ n. | All shortest paths in the first example: There are no paths between other pairs of vertices.The total distance is 2 + 1 + 1 + 2 + 1 + 1 = 8. | Input: 6 | Output: 8 | Master | 2 | 434 | 31 | 48 | 8 |
2,014 | G | 2014G | G. Milky Days | 2,200 | brute force; data structures; greedy; implementation | What is done is done, and the spoilt milk cannot be helped.Little John is as little as night is day β he was known to be a giant, at possibly \(2.1\) metres tall. It has everything to do with his love for milk.His dairy diary has \(n\) entries, showing that he acquired \(a_i\) pints of fresh milk on day \(d_i\). Milk d... | The first line of the input contains a single integer \(t\) (\(1\leq t \leq 10^4\)), the number of test cases.The first line of each test case consists of three integers \(n\), \(m\), \(k\) (\(1\le n\), \(m\), \(k \le 10^5\)), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the du... | For each test case, output a single integer, the number of milk satisfaction days. | In the first test case, \(5\) pints of milk are good for \(3\) days before spoiling.In the second test case, the following will happen: On day \(1\), he will receive \(5\) pints of milk and drink \(3\) of them (leaving \(2\) pints from day \(1\)); On day \(2\), he will receive \(7\) pints of milk and drink \(3\) of the... | Input: 61 1 31 52 3 31 52 74 5 21 92 64 95 65 2 44 75 37 111 212 14 1 35 109 414 815 35 5 58 910 716 1021 528 9 | Output: 3 3 4 5 10 6 | Hard | 4 | 950 | 720 | 82 | 20 |
1,335 | D | 1335D | D. Anti-Sudoku | 1,300 | constructive algorithms; implementation | You are given a correct solution of the sudoku puzzle. If you don't know what is the sudoku, you can read about it here.The picture showing the correct sudoku solution:Blocks are bordered with bold black color.Your task is to change at most \(9\) elements of this field (i.e. choose some \(1 \le i, j \le 9\) and change ... | 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.Each test case consists of \(9\) lines, each line consists of \(9\) characters from \(1\) to \(9\) without any whitespaces β the correct solution of the sudoku puzzle. | For each test case, print the answer β the initial field with at most \(9\) changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists. | Input: 1 154873296 386592714 729641835 863725149 975314628 412968357 631457982 598236471 247189563 | Output: 154873396 336592714 729645835 863725145 979314628 412958357 631457992 998236471 247789563 | Easy | 2 | 827 | 302 | 227 | 13 | |
286 | A | 286A | A. Lucky Permutation | 1,400 | constructive algorithms; math | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | The first line contains integer n (1 β€ n β€ 105) β the required permutation size. | Print ""-1"" (without the quotes) if the lucky permutation p of size n doesn't exist.Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.If there are multiple answers, you can print any of them. | Input: 1 | Output: 1 | Easy | 2 | 306 | 80 | 249 | 2 | |
141 | A | 141A | A. Amusing Joke | 800 | implementation; sortings; strings | So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two ""New Year and Christmas Men"" meet, thear assistants cut out of cardboard the letters from the guest's name and the host's name in honor of this event. Then the hung the letters above the main entrance.... | The input file consists of three lines: the first line contains the guest's name, the second line contains the name of the residence host and the third line contains letters in a pile that were found at the door in the morning. All lines are not empty and contain only uppercase Latin letters. The length of each line do... | Print ""YES"" without the quotes, if the letters in the pile could be permuted to make the names of the ""New Year and Christmas Men"". Otherwise, print ""NO"" without the quotes. | In the first sample the letters written in the last line can be used to write the names and there won't be any extra letters left.In the second sample letter ""P"" is missing from the pile and there's an extra letter ""L"".In the third sample there's an extra letter ""L"". | Input: SANTACLAUSDEDMOROZSANTAMOROZDEDCLAUS | Output: YES | Beginner | 3 | 1,052 | 338 | 179 | 1 |
351 | B | 351B | B. Jeff and Furik | 1,900 | combinatorics; dp; probabilities | Jeff has become friends with Furik. Now these two are going to play one quite amusing game.At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent... | The first line contains integer n (1 β€ n β€ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the permutation p. The numbers are separated by spaces. | In a single line print a single real value β the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. | In the first test the sequence is already sorted, so the answer is 0. | Input: 21 2 | Output: 0.000000 | Hard | 3 | 1,345 | 179 | 166 | 3 |
1,654 | E | 1654E | E. Arithmetic Operations | 2,300 | brute force; data structures; graphs; math | You are given an array of integers \(a_1, a_2, \ldots, a_n\).You can do the following operation any number of times (possibly zero): Choose any index \(i\) and set \(a_i\) to any integer (positive, negative or \(0\)). What is the minimum number of operations needed to turn \(a\) into an arithmetic progression? The arra... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^5\)). | Print a single integer: the minimum number of operations needed to turn \(a\) into an arithmetic progression. | In the first test, you can get the array \(a = [11, 10, 9, 8, 7, 6, 5, 4, 3]\) by performing \(6\) operations: Set \(a_3\) to \(9\): the array becomes \([3, 2, 9, 8, 6, 9, 5, 4, 1]\); Set \(a_2\) to \(10\): the array becomes \([3, 10, 9, 8, 6, 9, 5, 4, 1]\); Set \(a_6\) to \(6\): the array becomes \([3, 10, 9, 8, 6, 6,... | Input: 9 3 2 7 8 6 9 5 4 1 | Output: 6 | Expert | 4 | 418 | 163 | 109 | 16 |
1,418 | G | 1418G | G. Three Occurrences | 2,500 | data structures; divide and conquer; hashing; two pointers | You are given an array \(a\) consisting of \(n\) integers. We denote the subarray \(a[l..r]\) as the array \([a_l, a_{l + 1}, \dots, a_r]\) (\(1 \le l \le r \le n\)).A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array \([1, 2, 2, 2, 1, 1, 2, 2,... | The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le n\)). | Print one integer β the number of good subarrays of the array \(a\). | Input: 9 1 2 2 2 1 1 2 2 2 | Output: 3 | Expert | 4 | 500 | 166 | 68 | 14 | |
1,498 | E | 1498E | E. Two Houses | 2,200 | brute force; graphs; greedy; interactive; sortings | This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You m... | The first line contains a single integer \(n\) (\(3 \le n \le 500\)) denoting the number of houses in the city. The next line contains \(n\) space-separated integers \(k_1, k_2, \dots, k_n\) (\(0 \le k_i \le n - 1\)), the \(i\)-th of them represents the number of incoming roads to the \(i\)-th house. | In the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: ""? 1 2"": asking whether we can reach from house \(1\) to house \(2\). The judge responds with ""Yes"". The user program now concludes that this is sufficient information to determine the correc... | Input: 3 1 1 1 Yes | Output: ? 1 2 ! 1 2 | Hard | 5 | 2,158 | 301 | 0 | 14 | |
850 | F | 850F | F. Rainbow Balls | 2,800 | math | You have a bag of balls of n different colors. You have ai balls of the i-th color.While there are at least two different colored balls in the bag, perform the following steps: Take out two random balls without replacement one by one. These balls might be the same color. Color the second ball to the color of the first ... | The first line of input will contain a single integer n (1 β€ n β€ 2 500) β the number of colors.The next line of input will contain n space separated integers a1, a2, ..., an (1 β€ ai β€ 105) β the number of balls of each color. | Print a single integer, the answer to the problem. | In the first sample, no matter what happens, the balls will become the same color after one step.For the second sample, we have 6 balls. Letβs label the balls from 1 to 6, and without loss of generality, letβs say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3.Here is an example of how... | Input: 21 1 | Output: 1 | Master | 1 | 690 | 225 | 50 | 8 |
1,433 | F | 1433F | F. Zero Remainder Sum | 2,100 | dp | You are given a matrix \(a\) of size \(n \times m\) consisting of integers.You can choose no more than \(\left\lfloor\frac{m}{2}\right\rfloor\) elements in each row. Your task is to choose these elements in such a way that their sum is divisible by \(k\) and this sum is the maximum.In other words, you can choose no mor... | The first line of the input contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m, k \le 70\)) β the number of rows in the matrix, the number of columns in the matrix and the value of \(k\). The next \(n\) lines contain \(m\) elements each, where the \(j\)-th element of the \(i\)-th row is \(a_{i, j}\) (\(1 \le ... | Print one integer β the maximum sum divisible by \(k\) you can obtain. | In the first example, the optimal answer is \(2\) and \(4\) in the first row, \(5\) and \(2\) in the second row and \(7\) and \(4\) in the third row. The total sum is \(2 + 4 + 5 + 2 + 7 + 4 = 24\). | Input: 3 4 3 1 2 3 4 5 2 2 2 7 1 1 4 | Output: 24 | Hard | 1 | 518 | 339 | 70 | 14 |
1,076 | B | 1076B | B. Divisor Subtraction | 1,200 | implementation; math; number theory | You are given an integer number \(n\). The following algorithm is applied to it: if \(n = 0\), then end algorithm; find the smallest prime divisor \(d\) of \(n\); subtract \(d\) from \(n\) and go to step \(1\). Determine the number of subtrations the algorithm will make. | The only line contains a single integer \(n\) (\(2 \le n \le 10^{10}\)). | Print a single integer β the number of subtractions the algorithm will make. | In the first example \(5\) is the smallest prime divisor, thus it gets subtracted right away to make a \(0\).In the second example \(2\) is the smallest prime divisor at both steps. | Input: 5 | Output: 1 | Easy | 3 | 271 | 72 | 76 | 10 |
1,618 | A | 1618A | A. Polycarp and Sums of Subsequences | 800 | math; sortings | Polycarp had an array \(a\) of \(3\) positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array \(b\) of \(7\) integers.For example, if \(a = \{1, 4, 3\}\), then Polycarp wrote out \(1\), \(4\), \(3\), \(1 + 4 = 5\), \(1 + 3 = 4\), \(4 + 3... | The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.Each test case consists of one line which contains \(7\) integers \(b_1, b_2, \dots, b_7\) (\(1 \le b_i \le 10^9\); \(b_i \le b_{i+1}\)). Additional constraint on the input: there exists at least one array \(a\) which yields thi... | For each test case, print \(3\) integers β \(a_1\), \(a_2\) and \(a_3\). If there can be several answers, print any of them. | The subsequence of the array \(a\) is a sequence that can be obtained from \(a\) by removing zero or more of its elements.Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular,... | Input: 5 1 3 4 4 5 7 8 1 2 3 4 5 6 7 300000000 300000000 300000000 600000000 600000000 600000000 900000000 1 1 2 999999998 999999999 999999999 1000000000 1 2 2 3 3 4 5 | Output: 1 4 3 4 1 2 300000000 300000000 300000000 999999998 1 1 1 2 2 | Beginner | 2 | 526 | 364 | 124 | 16 |
129 | B | 129B | B. Students and Shoelaces | 1,200 | brute force; dfs and similar; graphs; implementation | Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the f... | The first line contains two integers n and m β the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β the numbers of students tied by the i-th lace (1 β€ a, b β€ n, a β b). It is guaranteed that no two st... | Print the single number β the number of groups of students that will be kicked out from the club. | In the first sample Anna and Maria won't kick out any group of students β in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two st... | Input: 3 31 22 33 1 | Output: 0 | Easy | 4 | 1,058 | 395 | 97 | 1 |
1,739 | F | 1739F | F. Keyboard Design | 2,600 | bitmasks; data structures; dp; string suffix structures; strings | Monocarp has a dictionary of \(n\) words, consisting of \(12\) first letters of the Latin alphabet. The words are numbered from \(1\) to \(n\). In every pair of adjacent characters in each word, the characters are different. For every word \(i\), Monocarp also has an integer \(c_i\) denoting how often he uses this word... | The first line contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of words.Then, \(n\) lines follow. The \(i\)-th of them contains an integer \(c_i\) (\(1 \le c_i \le 10^5\)) and a string \(s_i\) (\(2 \le |s_i| \le 2000\)) denoting the \(i\)-th word. Each character of \(s_i\) is one of \(12\) first letters ... | Print a sequence of \(12\) first letters of the Latin alphabet, where each letter from a to l appears exactly once, denoting the optimal keyboard. If there are multiple answers, you may print any of them. | Input: 3 7 abacaba 10 cba 4 db | Output: hjkildbacefg | Expert | 5 | 884 | 540 | 204 | 17 | |
1,427 | G | 1427G | G. One Billion Shades of Grey | 3,300 | flows; graphs | You have to paint with shades of grey the tiles of an \(n\times n\) wall. The wall has \(n\) rows of tiles, each with \(n\) tiles.The tiles on the boundary of the wall (i.e., on the first row, last row, first column and last column) are already painted and you shall not change their color. All the other tiles are not p... | The first line contains \(n\) (\(3\le n\le 200\)) β the number of rows and columns.Then \(n\) lines, each containing \(n\) integers, follow. The \(i\)-th of these lines describe the \(i\)-th row of tiles. It contains the \(n\) integers \(a_{ij}\) (\(-1\le a_{ij} \le 10^9)\). The value of \(a_{ij}\) described the tile o... | Print a single integer β the minimum possible total contrast of the wall. | Explanation of the first testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?): 1 7 64 ? 61 1 1 A possible way to paint the tile achieving the minimum possible contrast of \(26\) is: 1 7 64 5 61 1 1 Explanation of the second testcase: Since all tiles are either painted or broken, there i... | Input: 3 1 7 6 4 0 6 1 1 1 | Output: 26 | Master | 2 | 1,143 | 753 | 73 | 14 |
1,786 | B | 1786B | B. Cake Assembly Line | 1,300 | brute force; sortings | A cake assembly line in a bakery was once again optimized, and now \(n\) cakes are made at a time! In the last step, each of the \(n\) cakes should be covered with chocolate.Consider a side view on the conveyor belt, let it be a number line. The \(i\)-th cake occupies the segment \([a_i - w, a_i + w]\) on this line, ea... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(w\), and \(h\) (\(1 \le n \le 10^5\); \(1 \le w, h \le 10^5\); \(h \le w\)) β the number of c... | For each test case output ""YES"", if it's possible to shift the conveyor in such a way that each cake ends up with some chocolate, and no chocolate is outside the cakes, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be ... | The first example is shown in the figures in the statement.In the second example, we can move the conveyor, for example, so that the centers of the cakes are at \(4, 9, 14, 19, 24\).In the third example, we can't move the conveyor accordingly. | Input: 43 10 565 95 16540 65 1455 2 11 6 11 16 214 9 14 19 243 3 213 22 295 16 254 4 127 36 127 13635 50 141 144 | Output: YES YES NO YES | Easy | 2 | 1,100 | 901 | 353 | 17 |
2,025 | B | 2025B | B. Binomial Coefficients, Kind Of | 1,100 | combinatorics; dp; math | Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this: for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive) C[n][0] = 1; C[n][n] = 1; for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive) C[n][k] = C[n][k - 1] + C[n... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of pairs. Next, \(t\) pairs are written in two lines.The second line contains \(t\) integers \(n_1, n_2, \dots, n_t\) (\(2 \le n_i \le 10^5\)).The third line contains \(t\) integers \(k_1, k_2, \dots, k_t\) (\(1 \le k_i < n_i\)). | Print \(t\) integers \(C[n_i][k_i]\) modulo \(10^9 + 7\). | Input: 72 5 5 100000 100000 100000 1000001 2 3 1 33333 66666 99999 | Output: 2 4 8 2 326186014 984426998 303861760 | Easy | 3 | 786 | 312 | 57 | 20 | |
223 | C | 223C | C. Partial Sums | 1,900 | combinatorics; math; number theory | You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 β€ i β€ n) of array s equals . The operation x mod y means that we take the r... | The first line contains two space-separated integers n and k (1 β€ n β€ 2000, 0 β€ k β€ 109). The next line contains n space-separated integers a1, a2, ..., an β elements of the array a (0 β€ ai β€ 109). | Print n integers β elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces. | Input: 3 11 2 3 | Output: 1 3 6 | Hard | 3 | 600 | 197 | 201 | 2 | |
1,931 | E | 1931E | E. Anna and the Valentine's Day Gift | 1,400 | games; greedy; math; sortings | Sasha gave Anna a list \(a\) of \(n\) integers for Valentine's Day. Anna doesn't need this list, so she suggests destroying it by playing a game.Players take turns. Sasha is a gentleman, so he gives Anna the right to make the first move. On her turn, Anna must choose an element \(a_i\) from the list and reverse the seq... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follows the description of the test cases.The first line of each test case contains integers \(n\), \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^6\)) β the number of integers in the list and the parameter de... | For each test case, output: ""Sasha"", if Sasha wins with optimal play; ""Anna"", if Anna wins with optimal play. | Consider the first test case.Anna can reverse the integer \(2\), then Sasha can concatenate the integers \(2\) and \(14\), obtaining the integer \(214\), which is greater than \(10^2 = 100\). If Anna had reversed the integer \(14\), Sasha would have concatenated the integers \(41\) and \(2\), obtaining the integer \(41... | Input: 92 214 23 59 56 14 101 2007 800 15804 55000 123 30 410 106 4 6 2 3 1 10 9 10 71 161 1108 91 2 9 10 10 2 10 24 510 10 10 10 | Output: Sasha Anna Anna Sasha Sasha Anna Anna Anna Sasha | Easy | 4 | 1,355 | 581 | 113 | 19 |
721 | B | 721B | B. Passwords | 1,100 | implementation; math; sortings; strings | Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary ... | The first line of the input contains two integers n and k (1 β€ n, k β€ 100) β the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.The next n lines contains passwords, one per line β pairwise distinct non-empty strings consisting of latin letters and... | Print two integers β time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively. | Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he ente... | Input: 5 2cbaabcbb1abCABCabc | Output: 1 15 | Easy | 4 | 950 | 540 | 143 | 7 |
1,551 | B2 | 1551B2 | B2. Wonderful Coloring - 2 | 1,400 | binary search; constructive algorithms; data structures; greedy | This problem is an extension of the problem ""Wonderful Coloring - 1"". It has quite many differences, so you should read this statement completely.Recently, Paul and Mary have found a new favorite sequence of integers \(a_1, a_2, \dots, a_n\). They want to paint it using pieces of chalk of \(k\) colors. The coloring o... | The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of two lines. The first one contains two integers \(n\) and \(k\) (\(1 \le n \le 2\cdot10^5\), \(1 \le k \le n\)) β the length of a given sequence and the number of colors, ... | Output \(t\) lines, each of them must contain a description of a wonderful coloring for the corresponding test case.Each wonderful coloring must be printed as a sequence of \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le k\)) separated by spaces where \(c_i=0\), if \(i\)-th element isn't painted; \(c_i>0\), if... | In the first test case, the answer is shown in the figure in the statement. The red color has number \(1\), the blue color β \(2\), the green β \(3\). | Input: 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 | Output: 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 | Easy | 4 | 1,251 | 511 | 520 | 15 |
1,070 | J | 1070J | J. Streets and Avenues in Berhattan | 2,300 | dp | Berhattan is the capital of Berland. There are \(n\) streets running parallel in the east-west direction (horizontally), and there are \(m\) avenues running parallel in the south-north direction (vertically). Each street intersects with each avenue, forming a crossroad. So in total there are \(n \cdot m\) crossroads in... | Input contains one or several test cases to process. The first line contains \(t\) (\(1 \le t \le 30000\)) β the number of test cases. Solve test cases separately, test cases are completely independent and do not affect each other.The description of \(t\) test cases follows. Each test case starts with line with space-s... | For each test case print single number \(C\) in the separate line β minimal possible number of inconvenient crossroads in Berhattan after the naming process. | Input: 22 3 9EEZZEEZZZ2 7 9EEZZEEZZZ | Output: 04 | Expert | 1 | 1,042 | 960 | 157 | 10 | |
725 | G | 725G | G. Messages on a Tree | 3,300 | Alice and Bob are well-known for sending messages to each other. This time you have a rooted tree with Bob standing in the root node and copies of Alice standing in each of the other vertices. The root node has number 0, the rest are numbered 1 through n.At some moments of time some copies of Alice want to send a messa... | The first line of input contains two integers n and m (1 β€ n, m β€ 200 000) β the number of nodes with Alices and the number of messages.Second line contains n integers p1, p2, ..., pn (0 β€ pi < i). The integer pi is the number of the parent node of node i.The next m lines describe the messages. The i-th of them contain... | Print m integers β the i-th of them is the moment of time when the answer for the i-th message will be received by the initiator. | In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she re... | Input: 6 30 1 2 3 2 54 66 95 11 | Output: 14 13 11 | Master | 0 | 2,540 | 615 | 129 | 7 | |
1,831 | B | 1831B | B. Array merging | 1,000 | constructive algorithms; greedy | You are given two arrays \(a\) and \(b\) both of length \(n\).You will merge\(^\dagger\) these arrays forming another array \(c\) of length \(2 \cdot n\). You have to find the maximum length of a subarray consisting of equal values across all arrays \(c\) that could be obtained.\(^\dagger\) A merge of two arrays result... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\) and \(b... | For each test case, output the maximum length of a subarray consisting of equal values across all merges. | In the first test case, we can only make \(c=[2,2]\), thus the answer is \(2\).In the second test case, since all values are distinct, the answer must be \(1\).In the third test case, the arrays \(c\) we can make are \([1,2,1,2]\), \([1,2,2,1]\), \([2,1,1,2]\), \([2,1,2,1]\). We can see that the answer is \(2\) when we... | Input: 412231 2 34 5 621 22 151 2 2 2 22 1 1 1 1 | Output: 2 1 2 5 | Beginner | 2 | 603 | 698 | 105 | 18 |
831 | B | 831B | B. Keyboard Layouts | 800 | implementation; strings | There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in ... | The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout.The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout.The third line contains a non-empty string s consisting of lowercase and upp... | Print the text if the same keys were pressed in the second layout. | Input: qwertyuiopasdfghjklzxcvbnmveamhjsgqocnrbfxdtwkylupziTwccpQZAvb2017 | Output: HelloVKCup2017 | Beginner | 2 | 746 | 436 | 66 | 8 | |
325 | C | 325C | C. Monsters and Diamonds | 2,600 | dfs and similar; graphs; shortest paths | Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are n types of monsters, each with an ID between 1 and n. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monster... | The first line contains two integers: m and n (1 β€ m, n β€ 105), the number of possible splits and the number of different monster types. Each of the following m lines contains a split rule. Each split rule starts with an integer (a monster ID) mi (1 β€ mi β€ n), and a positive integer li indicating the number of monsters... | For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an ar... | Input: 6 41 3 -1 1 -11 2 -1 -12 3 -1 3 -12 3 -1 -1 -13 2 -1 -14 2 4 -1 | Output: 2 -23 42 2-1 -1 | Expert | 3 | 1,033 | 635 | 498 | 3 | |
748 | A | 748A | A. Santa Claus and a Place in a Class | 800 | implementation; math | Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numb... | The only line contains three integers n, m and k (1 β€ n, m β€ 10 000, 1 β€ k β€ 2nm) β the number of lanes, the number of desks in each lane and the number of Santa Claus' place. | Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be ""L"", if Santa Clause should sit on the left, and ""R"" if his place is on the right. | The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example.In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place... | Input: 4 3 9 | Output: 2 2 L | Beginner | 2 | 1,071 | 175 | 246 | 7 |
891 | D | 891D | D. Sloth | 3,100 | dfs and similar; dp; graph matchings; trees | Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys.You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed e... | The first line contains n (2 β€ n β€ 5Β·105) β the number of vertices.Each of the next n - 1 lines contains two integers a and b (1 β€ a, b β€ n) β the endpoints of one edge. It's guaranteed that the graph is a tree. | Output a single integer β the answer to the problem. | In first sample, there are 8 ways: edge between 2 and 3 turns to edge between 1 and 3, edge between 2 and 3 turns to edge between 1 and 4, edge between 2 and 3 turns to edge between 2 and 3, edge between 2 and 3 turns to edge between 2 and 4, edge between 1 and 2 turns to edge between 1 and 2, edge between 1 and 2 turn... | Input: 41 22 33 4 | Output: 8 | Master | 4 | 519 | 211 | 52 | 8 |
1,208 | F | 1208F | F. Bits And Pieces | 2,600 | bitmasks; dfs and similar; dp; greedy | You are given an array \(a\) of \(n\) integers.You need to find the maximum value of \(a_{i} | ( a_{j} \& a_{k} )\) over all triplets \((i,j,k)\) such that \(i < j < k\).Here \(\&\) denotes the bitwise AND operation, and \(|\) denotes the bitwise OR operation. | The first line of input contains the integer \(n\) (\(3 \le n \le 10^{6}\)), the size of the array \(a\).Next line contains \(n\) space separated integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_{i} \le 2 \cdot 10^{6}\)), representing the elements of the array \(a\). | Output a single integer, the maximum value of the expression given in the statement. | In the first example, the only possible triplet is \((1, 2, 3)\). Hence, the answer is \(2 | (4 \& 6) = 6\).In the second example, there are \(4\) possible triplets: \((1, 2, 3)\), value of which is \(2|(8\&4) = 2\). \((1, 2, 4)\), value of which is \(2|(8\&7) = 2\). \((1, 3, 4)\), value of which is \(2|(4\&7) = 6\). \... | Input: 3 2 4 6 | Output: 6 | Expert | 4 | 260 | 269 | 84 | 12 |
921 | 04 | 92104 | 04. Labyrinth-4 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 | |||||
343 | D | 343D | D. Water Tree | 2,100 | data structures; dfs and similar; graphs; trees | Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this v... | The first line of the input contains an integer n (1 β€ n β€ 500000) β the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 β€ ai, bi β€ n, ai β bi) β the edges of the tree.The next line contains a number q (1 β€ q β€ 500000) β the number of operations to perfor... | For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input. | Input: 51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5 | Output: 00010101 | Hard | 4 | 984 | 613 | 192 | 3 | |
1,354 | A | 1354A | A. Alarm Clock | 900 | math | Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least \(a\) minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in \(b\) minutes.Every time Polycarp wakes up, he decides if he wants to s... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.The only line of each testcase contains four integers \(a, b, c, d\) (\(1 \le a, b, c, d \le 10^9\)) β the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeedi... | For each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed. | In the first testcase Polycarp wakes up after \(3\) minutes. He only rested for \(3\) minutes out of \(10\) minutes he needed. So after that he sets his alarm to go off in \(6\) minutes and spends \(4\) minutes falling asleep. Thus, he rests for \(2\) more minutes, totaling in \(3+2=5\) minutes of sleep. Then he repeat... | Input: 7 10 3 6 4 11 3 6 4 5 9 4 10 6 5 2 3 1 1 1 1 3947465 47342 338129 123123 234123843 13 361451236 361451000 | Output: 27 27 9 -1 1 6471793 358578060125049 | Beginner | 1 | 908 | 382 | 161 | 13 |
2,073 | E | 2073E | 3,300 | interactive | Master | 1 | 0 | 0 | 0 | 20 | ||||||
126 | B | 126B | B. Password | 1,700 | binary search; dp; hashing; string suffix structures; strings | Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the tem... | You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. | Print the string t. If a suitable t string does not exist, then print ""Just a legend"" without the quotes. | Input: fixprefixsuffix | Output: fix | Medium | 5 | 1,101 | 110 | 107 | 1 | |
1,045 | B | 1045B | B. Space Isaac | 2,400 | hashing; number theory | Everybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets of numbers in the bags is \(\{0,1,β¦,M-1\}\), for some positive integer \(M\). Ajs draws a number from the first bag a... | The first line contains two positive integer \(N\) (\(1 \leq N \leq 200\,000\)) and \(M\) (\(N+1 \leq M \leq 10^{9}\)), denoting the number of the elements in the first bag and the modulus, respectively.The second line contains \(N\) nonnegative integers \(a_1,a_2,\ldots,a_N\) (\(0 \leq a_1<a_2< \ldots< a_N<M\)), the c... | In the first line, output the cardinality \(K\) of the set of residues modulo \(M\) which Ajs cannot obtain.In the second line of the output, print \(K\) space-separated integers greater or equal than zero and less than \(M\), which represent the residues Ajs cannot obtain. The outputs should be sorted in increasing or... | In the first sample, the first bag and the second bag contain \(\{3,4\}\) and \(\{0,1,2\}\), respectively. Ajs can obtain every residue modulo \(5\) except the residue \(2\): \( 4+1 \equiv 0, \, 4+2 \equiv 1, \, 3+0 \equiv 3, \, 3+1 \equiv 4 \) modulo \(5\). One can check that there is no choice of elements from the fi... | Input: 2 53 4 | Output: 12 | Expert | 2 | 460 | 345 | 380 | 10 |
1,305 | A | 1305A | A. Kuroni and the Gifts | 800 | brute force; constructive algorithms; greedy; sortings | Kuroni has \(n\) daughters. As gifts for them, he bought \(n\) necklaces and \(n\) bracelets: the \(i\)-th necklace has a brightness \(a_i\), where all the \(a_i\) are pairwise distinct (i.e. all \(a_i\) are different), the \(i\)-th bracelet has a brightness \(b_i\), where all the \(b_i\) are pairwise distinct (i.e. al... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of daughters, necklaces and bracelets.The se... | For each test case, print a line containing \(n\) integers \(x_1, x_2, \dots, x_n\), representing that the \(i\)-th daughter receives a necklace with brightness \(x_i\). In the next line print \(n\) integers \(y_1, y_2, \dots, y_n\), representing that the \(i\)-th daughter receives a bracelet with brightness \(y_i\).Th... | In the first test case, it is enough to give the \(i\)-th necklace and the \(i\)-th bracelet to the \(i\)-th daughter. The corresponding sums are \(1 + 8 = 9\), \(8 + 4 = 12\), and \(5 + 5 = 10\).The second test case is described in the statement. | Input: 2 3 1 8 5 8 4 5 3 1 7 5 6 1 2 | Output: 1 8 5 8 4 5 5 1 7 6 2 1 | Beginner | 4 | 1,804 | 619 | 706 | 13 |
1,687 | A | 1687A | A. The Enchanted Forest | 1,600 | brute force; greedy | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.βPerfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by \(n\) points on the \(X\)-axis numbered \(1\) thro... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10 ^ 5\), \(1\le k \le 10^9\)) β the number of ... | For each test case, print the maximum number of mushrooms Marisa can pick after \(k\) minutes. | Test case 1:Marisa can start at \(x=2\). In the first minute, she moves to \(x=1\) and collect \(5\) mushrooms. The number of mushrooms will be \([1,7,2,3,4]\). In the second minute, she moves to \(x=2\) and collects \(7\) mushrooms. The numbers of mushrooms will be \([2,1,3,4,5]\). After \(2\) minutes, Marisa collects... | Input: 45 25 6 1 2 35 75 6 1 2 31 29999995 700001000000000 1000000000 1000000000 1000000000 1000000000 | Output: 12 37 1000000 5000349985 | Medium | 2 | 905 | 643 | 94 | 16 |
1,008 | B | 1008B | B. Turn the Rectangles | 1,000 | greedy; sortings | There are \(n\) rectangles in a row. You can either turn each rectangle by \(90\) degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the r... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of rectangles.Each of the next \(n\) lines contains two integers \(w_i\) and \(h_i\) (\(1 \leq w_i, h_i \leq 10^9\)) β the width and the height of the \(i\)-th rectangle. | Print ""YES"" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print ""NO"".You can print each letter in any case (upper or lower). | In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one. | Input: 33 44 63 5 | Output: YES | Beginner | 2 | 562 | 255 | 187 | 10 |
292 | E | 292E | E. Copying Data | 1,900 | data structures | We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of l... | The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).Next m li... | For each second type query print the result on a single line. | Input: 5 101 2 0 -1 33 1 5 -2 02 51 3 3 32 52 42 11 2 1 42 12 41 4 2 12 2 | Output: 03-1323-1 | Hard | 1 | 801 | 980 | 61 | 2 | |
1,743 | C | 1743C | C. Save the Magazines | 1,100 | constructive algorithms; dp; greedy | Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between \(n\) boxes, arranged in a row. The \(i\)-th box contains \(a_i\) magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of the testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of boxes.The second line contains a string of \(n\) characters 0 and/or 1. If the \(i\)-th character is 1, the \... | For each testcase, print one integer β the maximum number of magazines Monocarp can save from the rain. | In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes \(1\), \(3\) and \(4\) are covered, and \(10 + 8 + 9 = 27\) magazines are saved.In the second testcase, Monocarp can move the lid from the second box to the first box, then from the third box to the second... | Input: 450111010 5 8 9 6601101120 10 9 30 20 1940000100 100 100 100401115 4 5 1 | Output: 27 80 0 14 | Easy | 3 | 953 | 666 | 103 | 17 |
899 | A | 899A | A. Splitting in Teams | 800 | constructive algorithms; greedy; math | There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams o... | The first line contains single integer n (2 β€ n β€ 2Β·105) β the number of groups.The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 2), where ai is the number of people in group i. | Print the maximum number of teams of three people the coach can form. | In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.In the second example he can't make a single team.In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the se... | Input: 41 1 2 1 | Output: 1 | Beginner | 3 | 585 | 202 | 69 | 8 |
2,062 | E2 | 2062E2 | E2. The Game (Hard Version) | 3,000 | data structures; dfs and similar; games; graphs; implementation; trees | This is the hard version of the problem. The difference between the versions is that in this version, you need to find all possible nodes Cirno may choose. You can hack only if you solved all versions of this problem. Cirno and Daiyousei are playing a game with a tree\(^{\text{β}}\) of \(n\) nodes, rooted at node \(1\)... | The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of input test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 4\cdot 10^5\)) β the number of nodes in the tree.The second line contains \(n\) integers \(w_1,w_2,\ldots,w_n\) (\(1 \le w_i \le n\... | For each test case, print one line.If Cirno wins the game, print several integers. The first integer \(k\) represents the number of possible nodes she may choose in the first turn. The other \(k\) integers are all possible nodes in increasing order.Otherwise, print ""0"" (without quotes). | In the first test case: If Cirno chooses \(1\) or \(3\) in the first turn, Daiyousei cannot make a move, so Daiyousei wins. If Cirno chooses \(2\) or \(4\) in the first turn, Daiyousei can only choose \(3\), but after it Cirno cannot make a move, so Cirno wins.Therefore, all possible nodes Cirno may choose are \(2\) an... | Input: 542 2 4 31 21 32 451 2 3 4 51 22 33 44 531 2 31 21 353 1 3 4 51 22 33 44 5101 2 3 2 4 3 3 4 4 31 44 67 46 96 57 81 22 32 10 | Output: 2 2 4 0 1 2 1 2 5 3 4 6 7 10 | Master | 6 | 1,047 | 680 | 289 | 20 |
1,837 | B | 1837B | B. Comparison String | 900 | greedy | You are given a string \(s\) of length \(n\), where each character is either < or >.An array \(a\) consisting of \(n+1\) elements is compatible with the string \(s\) if, for every \(i\) from \(1\) to \(n\), the character \(s_i\) represents the result of comparing \(a_i\) and \(a_{i+1}\), i. e.: \(s_i\) is < if and only... | The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 100\)); the second line contains the string \(s\), consisting of \(n\) characters. Each character of \(s\) is either < or >. | For each test case, print one integer β the minimum cost among all arrays which are compatible with the given string \(s\). | In the first test case of the example, the array can be \([13, 37, 42, 37, 13]\).In the second test case of the example, the array can be \([42, 37, 13, 37, 42]\). | Input: 44<<>>4>><<5>>>>>7<><><>< | Output: 3 3 6 2 | Beginner | 1 | 839 | 310 | 123 | 18 |
337 | B | 337B | B. Routine Problem | 1,400 | greedy; math; number theory | Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the scre... | A single line contains four space-separated integers a, b, c, d (1 β€ a, b, c, d β€ 1000). | Print the answer to the problem as ""p/q"", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1. | Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: Sample 2. T... | Input: 1 1 3 2 | Output: 1/3 | Easy | 3 | 649 | 88 | 165 | 3 |
1,271 | C | 1271C | C. Shawarma Tent | 1,300 | brute force; geometry; greedy; implementation | The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.The main school of the capital is located in \((s_x, ... | The first line contains three integers \(n\), \(s_x\), \(s_y\) (\(1 \le n \le 200\,000\), \(0 \le s_x, s_y \le 10^{9}\)) β the number of students and the coordinates of the school, respectively.Then \(n\) lines follow. The \(i\)-th of them contains two integers \(x_i\), \(y_i\) (\(0 \le x_i, y_i \le 10^{9}\)) β the loc... | The output should consist of two lines. The first of them should contain one integer \(c\) β the maximum number of students that will buy shawarmas at the tent. The second line should contain two integers \(p_x\) and \(p_y\) β the coordinates where the tent should be located. If there are multiple answers, print any of... | In the first example, If we build the shawarma tent in \((4, 2)\), then the students living in \((4, 2)\), \((4, 1)\) and \((5, 1)\) will visit it.In the second example, it is possible to build the shawarma tent in \((1, 1)\), then both students living in \((0, 0)\) will visit it. | Input: 4 3 2 1 3 4 2 5 1 4 1 | Output: 3 4 2 | Easy | 4 | 1,426 | 483 | 427 | 12 |
1,812 | B | 1812B | B. Was it Rated? | 0 | *special; *special; *special; brute force; implementation | The only line of the input contains a single integer \(n\) (\(1 \leq n \leq 25\)). | Input: 1 | Output: YES | Beginner | 5 | 0 | 82 | 0 | 18 | |||
1,531 | B2 | 1531B2 | B2. ΠΠΎΠ½ΠΈΡΠΎΡΠΈΠ½Π³ | 0 | *special | ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅ ΠΎΡΠΊΡΡΠ»Π° Π²ΡΠΎΡΠΎΠΉ ΡΡΠ°Π± Π² Π‘Π°Π½ΠΊΡ-ΠΠ΅ΡΠ΅ΡΠ±ΡΡΠ³Π΅! ΠΡ Π½Π΅ ΠΏΡΠ΅ΠΌΠΈΠ½ΡΠ»ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΡΡ ΡΠΌΠ΅Π½ΠΈΡΡ ΠΎΠ±ΡΡΠ°Π½ΠΎΠ²ΠΊΡ ΠΈ ΡΠ΅ΡΠΈΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΅Ρ
Π°ΡΡ ΠΈΠ· ΠΎΡΠΈΡΠ° Π² ΠΠΎΠΌΠ΅ ΠΠΈΠ½Π³Π΅ΡΠ° Π² ΠΎΡΠΈΡ Π½Π° ΠΡΠ°ΡΠ½ΠΎΠΌ ΠΌΠΎΡΡΡ.ΠΠ»Ρ ΠΊΠΎΠΌΡΠΎΡΡΠ½ΠΎΠΉ ΡΠ°Π±ΠΎΡΡ Π²Π°ΠΌ ΠΏΠΎΡΡΠ΅Π±ΡΡΡΡΡ Π΄Π²Π° ΠΌΠΎΠ½ΠΈΡΠΎΡΠ° Ρ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ, ΡΡΠΎΠ±Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π½ΠΈΡ
Π²ΡΠ³Π»ΡΠ΄Π΅Π»ΠΎ Π΅Π΄ΠΈΠ½ΡΠΌ ΡΠ΅Π»ΡΠΌ. ΠΠ° ΡΠΊΠ»Π°Π΄Π΅ ΠΎΡΠΈΡΠ° Π½Π° ΠΡΠ°ΡΠ½ΠΎΠΌ ΠΌΠΎΡΡΡ ... | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π·Π°Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(n\) β ΡΠΈΡΠ»ΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² Π½Π° ΡΠΊΠ»Π°Π΄Π΅.Π ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
\(n\) ΡΡΡΠΎΠΊ Π·Π°Π΄Π°Π½Ρ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° \(w_i\) ΠΈ \(h_i\) (\(1 \le w_i, h_i \le 10^9\)) β ΡΠΈΡΠΈΠ½Π° ΠΈ Π²ΡΡΠΎΡΠ° \(i\)-Π³ΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΠ°. ΠΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΡΡΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΡ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΠΊΠ²Π°Π΄ΡΠ°ΡΠ½ΡΠΌΠΈ (\(w_i = h_i\)), Π° ΡΠ°Π·ΠΌΠ΅ΡΡ ΡΠ°Π·Π½ΡΡ
ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² ΠΌΠΎΠ³ΡΡ... | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠΈΡΠ»ΠΎ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΡ
ΠΏΠ°Ρ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ². | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠΌΠΈ ΡΠ²Π»ΡΡΡΡΡ ΠΏΠ°ΡΡ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² Ρ Π½ΠΎΠΌΠ΅ΡΠ°ΠΌΠΈ \((1, 2)\), \((1, 4)\), \((1, 5)\), \((3, 4)\), \((4, 5)\).ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π²ΡΠ΅ ΠΏΠ°ΡΡ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² β ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠ΅. | Input: 5 3 2 2 2 5 5 3 5 4 3 | Output: 5 | Beginner | 1 | 770 | 373 | 40 | 15 |
1,004 | D | 1004D | D. Sonya and Matrix | 2,300 | brute force; constructive algorithms; implementation | Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers ha... | The first line contains a single integer \(t\) (\(1\leq t\leq 10^6\)) β the number of cells in the matrix.The second line contains \(t\) integers \(a_1, a_2, \ldots, a_t\) (\(0\leq a_i< t\)) β the values in the cells in arbitrary order. | In the first line, print two positive integers \(n\) and \(m\) (\(n \times m = t\)) β the size of the matrix.In the second line, print two integers \(x\) and \(y\) (\(1\leq x\leq n\), \(1\leq y\leq m\)) β the row number and the column number where the cell with \(0\) is located.If there are multiple possible answers, p... | You can see the solution to the first example in the legend. You also can choose the cell \((2, 2)\) for the cell where \(0\) is located. You also can choose a \(5\times 4\) matrix with zero at \((4, 2)\).In the second example, there is a \(3\times 6\) matrix, where the zero is located at \((2, 3)\) there.In the third ... | Input: 201 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4 | Output: 4 52 2 | Expert | 3 | 1,239 | 236 | 395 | 10 |
1,583 | D | 1583D | D. Omkar and the Meaning of Life | 1,800 | constructive algorithms; greedy; interactive | It turns out that the meaning of life is a permutation \(p_1, p_2, \ldots, p_n\) of the integers \(1, 2, \ldots, n\) (\(2 \leq n \leq 100\)). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries.A query consists of an array \(a_1, a_2, \ldots, a_n\) of integers ... | In the sample, the hidden permutation \(p\) is \([3, 2, 1, 5, 4]\). Three queries were made.The first query is \(a = [4, 4, 2, 3, 2]\). This yields \(s = [3 + 4, 2 + 4, 1 + 2, 5 + 3, 4 + 2] = [7, 6, 3, 8, 6]\). \(6\) is the only number that appears more than once, and it appears first at index \(2\), making the answer ... | Input: 5 2 0 1 | Output: ? 4 4 2 3 2 ? 3 5 1 5 5 ? 5 2 4 3 1 ! 3 2 1 5 4 | Medium | 3 | 811 | 0 | 0 | 15 | ||
1,848 | A | 1848A | A. Vika and Her Friends | 900 | games; math | Vika and her friends went shopping in a mall, which can be represented as a rectangular grid of rooms with sides of length \(n\) and \(m\). Each room has coordinates \((a, b)\), where \(1 \le a \le n, 1 \le b \le m\). Thus we call a hall with coordinates \((c, d)\) a neighbouring for it if \(|a - c| + |b - d| = 1\).Tir... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(k\) (\(1 \le n, m, k \le 100\)) β the sizes of the mall and the n... | For each test case, output ""YES"" if Vika can run away from her friends forever, otherwise output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first test case, the friend will never catch up with Vika, because Vika can always move to the room diagonally opposite to the one where the friend is.In the second test case, no matter where Vika goes, each of her friends can catch her after the first move.In the third test case, Vika and her friend will always... | Input: 62 2 11 11 22 2 21 12 22 21 2 11 11 25 5 43 31 11 55 15 52 2 21 12 11 23 4 11 23 3 | Output: YES NO YES NO YES YES | Beginner | 2 | 1,358 | 702 | 273 | 18 |
1,455 | B | 1455B | B. Jumps | 1,200 | constructive algorithms; math | You are standing on the \(\mathit{OX}\)-axis at point \(0\) and you want to move to an integer point \(x > 0\).You can make several jumps. Suppose you're currently at point \(y\) (\(y\) may be negative) and jump for the \(k\)-th time. You can: either jump to the point \(y + k\) or jump to the point \(y - 1\). What is t... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first and only line of each test case contains the single integer \(x\) (\(1 \le x \le 10^6\)) β the destination point. | For each test case, print the single integer β the minimum number of jumps to reach \(x\). It can be proved that we can reach any integer point \(x\). | In the first test case \(x = 1\), so you need only one jump: the \(1\)-st jump from \(0\) to \(0 + 1 = 1\).In the second test case \(x = 2\). You need at least three jumps: the \(1\)-st jump from \(0\) to \(0 + 1 = 1\); the \(2\)-nd jump from \(1\) to \(1 + 2 = 3\); the \(3\)-rd jump from \(3\) to \(3 - 1 = 2\); Two ju... | Input: 5 1 2 3 4 5 | Output: 1 3 2 3 4 | Easy | 2 | 381 | 220 | 150 | 14 |
1,494 | F | 1494F | F. Delete The Edges | 2,900 | brute force; constructive algorithms; dfs and similar; graphs; implementation | You are given an undirected connected graph consisting of \(n\) vertices and \(m\) edges. Your goal is to destroy all edges of the given graph.You may choose any vertex as the starting one and begin walking from it along the edges. When you walk along an edge, you destroy it. Obviously, you cannot walk along an edge if... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 3000\); \(n - 1 \le m \le \min(\frac{n(n-1)}{2}, 3000\))) β the numbef of vertices and the number of edges in the graph.Then \(m\) lines follow, each containing two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)) β the endpoint... | If it's impossible to destroy all of the edges, print 0.Otherwise, print the sequence of your actions as follows. First, print \(k\) β the number of actions (\(k \le 2m + 2\)). Then, print the sequence itself, consisting of \(k\) integers. The first integer should be the index of the starting vertex. Then, each of the ... | Input: 3 3 1 2 2 3 3 1 | Output: 4 1 2 3 1 | Master | 5 | 924 | 413 | 528 | 14 | |
771 | E | 771E | E. Bear and Rectangle Strips | 3,000 | dp; greedy | Limak has a grid that consists of 2 rows and n columns. The j-th cell in the i-th row contains an integer ti, j which can be positive, negative or zero.A non-empty rectangle of cells is called nice if and only if the sum of numbers in its cells is equal to 0.Limak wants to choose some nice rectangles and give them to h... | The first line of the input contains an integer n (1 β€ n β€ 300 000) β the number of columns in the grid.The next two lines contain numbers in the grid. The i-th of those two lines contains n integers ti, 1, ti, 2, ..., ti, n ( - 109 β€ ti, j β€ 109). | Print one integer, denoting the maximum possible number of cell-disjoint nice rectangles. | In the first sample, there are four nice rectangles: Limak can't choose all of them because they are not disjoint. He should take three nice rectangles: those denoted as blue frames on the drawings.In the second sample, it's optimal to choose six nice rectangles, each consisting of one cell with a number 0.In the third... | Input: 670 70 70 70 70 -1590 -60 -30 30 -30 15 | Output: 3 | Master | 2 | 460 | 248 | 89 | 7 |
1,272 | C | 1272C | C. Yet Another Broken Keyboard | 1,200 | combinatorics; dp; implementation | Recently, Norge found a string \(s = s_1 s_2 \ldots s_n\) consisting of \(n\) lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string \(s\). Yes, all \(\frac{n (n + 1)}{2}\) of them!A substring of \(s\) is a non-empty string \(x = s[a \ldots b] = s_{a} s_{a +... | The first line contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq k \leq 26\)) β the length of the string \(s\) and the number of Latin letters still available on the keyboard.The second line contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.The t... | Print a single number β the number of substrings of \(s\) that can be typed using only available letters \(c_1, c_2, \ldots, c_k\). | In the first example Norge can print substrings \(s[1\ldots2]\), \(s[2\ldots3]\), \(s[1\ldots3]\), \(s[1\ldots1]\), \(s[2\ldots2]\), \(s[3\ldots3]\), \(s[5\ldots6]\), \(s[6\ldots7]\), \(s[5\ldots7]\), \(s[5\ldots5]\), \(s[6\ldots6]\), \(s[7\ldots7]\). | Input: 7 2 abacaba a b | Output: 12 | Easy | 3 | 762 | 466 | 131 | 12 |
1,525 | C | 1525C | C. Robot Collisions | 2,000 | data structures; greedy; implementation; sortings | There are \(n\) robots driving along an OX axis. There are also two walls: one is at coordinate \(0\) and one is at coordinate \(m\).The \(i\)-th robot starts at an integer coordinate \(x_i~(0 < x_i < m)\) and moves either left (towards the \(0\)) or right with the speed of \(1\) unit per second. No two robots start at... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5\); \(2 \le m \le 10^8\)) β the number of robots and the coordinate of the righ... | For each testcase print \(n\) integers β for the \(i\)-th robot output the time it explodes at if it does and \(-1\) otherwise. | Here is the picture for the seconds \(0, 1, 2\) and \(3\) of the first testcase: Notice that robots \(2\) and \(3\) don't collide because they meet at the same point \(2.5\), which is not integer.After second \(3\) robot \(6\) just drive infinitely because there's no robot to collide with. | Input: 5 7 12 1 2 3 4 9 10 11 R R L L R R R 2 10 1 6 R R 2 10 1 3 L L 1 10 5 R 7 8 6 1 7 2 3 5 4 R L R L L L L | Output: 1 1 1 1 2 -1 2 -1 -1 2 2 -1 -1 2 7 3 2 7 3 | Hard | 4 | 823 | 761 | 127 | 15 |
322 | A | 322A | A. Ciel and Dancing | 1,000 | greedy | Fox Ciel and her friends are in a dancing room. There are n boys and m girls here, and they never danced before. There will be some songs, during each song, there must be exactly one boy and one girl are dancing. Besides, there is a special rule: either the boy in the dancing pair must dance for the first time (so, he ... | The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of boys and girls in the dancing room. | In the first line print k β the number of songs during which they can dance. Then in the following k lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to n, and the girls are indexed from 1 to m. | In test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song).And in test case 2, we have 2 boys with 2 girls, the answer is 3. | Input: 2 1 | Output: 21 12 1 | Beginner | 1 | 496 | 113 | 274 | 3 |
1,346 | D | 1346D | D. Constructing the Dungeon | 1,600 | *special; graphs; greedy | Polycarp is developing an RPG game where the main character fights monsters and searches for treasure in dungeons. Now Polycarp is making one of the dungeons the character can explore.The dungeon consists of \(n\) rooms connected by \(m\) two-way tunnels, and it is possible to reach every room from every other room usi... | The first line contains one integer \(t\) (\(1 \le t \le 100000\)) β the number of test cases. Then the test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 200000\); \(n - 1 \le m \le \min(200000, \frac{n(n-1)}{2})\)) β the number of rooms and tunnels in the dungeon, ... | For each test case, print the answer as follows:If it is impossible to find the values of \(a_1\), \(a_2\), ..., \(a_n\) satisfying all the constraints, print one single string NO on a separate line. Otherwise, print YES in the first line, and \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) in the second line. If there a... | Input: 3 3 2 1 2 1 2 3 1 5 7 3 2 7 3 4 9 1 5 5 1 2 5 4 1 5 4 2 7 3 1 5 4 4 1 2 5 3 2 2 4 1 3 3 4 4 | Output: YES 1 1 1 YES 5 7 9 9 5 NO | Medium | 3 | 1,377 | 969 | 365 | 13 | |
955 | B | 955B | B. Not simply beatiful strings | 1,400 | implementation | Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others β a gr... | The only line contains s (1 β€ |s| β€ 105) consisting of lowercase latin letters. | Print Β«YesΒ» if the string can be split according to the criteria above or Β«NoΒ» otherwise.Each letter can be printed in arbitrary case. | In sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable.There's no suitable partition in sample case three. | Input: ababa | Output: Yes | Easy | 1 | 646 | 79 | 134 | 9 |
1,924 | D | 1924D | D. Balanced Subsequences | 2,700 | combinatorics; dp; math | 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.A subsequence is a sequence that can be derived from the given sequence by deleting zero... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 3 \cdot 10^3\)). Description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m, k \le 2 \cdot 10^3\)) | For each test case, print one integer β the answer to the problem. | For the first test case ""()()"", ""(())"" are the \(2\) sequencesFor the second test case no sequence is possible.For the third test case "")((()"", "")(()("", "")()(("", ""())(("" are the \(4\) sequences. | Input: 32 2 23 2 33 2 1 | Output: 2 0 4 | Master | 3 | 669 | 281 | 66 | 19 |
1,806 | E | 1806E | E. Tree Master | 2,200 | brute force; data structures; dfs and similar; trees | You are given a tree with \(n\) weighted vertices labeled from \(1\) to \(n\) rooted at vertex \(1\). The parent of vertex \(i\) is \(p_i\) and the weight of vertex \(i\) is \(a_i\). For convenience, define \(p_1=0\).For two vertices \(x\) and \(y\) of the same depth\(^\dagger\), define \(f(x,y)\) as follows: Initializ... | The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 10^5\); \(1 \le q \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)).The third line contains \(n-1\) integers \(p_2, \ldots, p_n\) (\(1 \le p_i < i\)).Each of the next \(q\) lines contains two integer... | Output \(q\) lines, the \(i\)-th line contains a single integer, the value of \(f(x_i,y_i)\). | Consider the first example:In the first query, the answer is \(a_4\cdot a_5+a_3\cdot a_3+a_2\cdot a_2+a_1\cdot a_1=3+4+25+1=33\).In the second query, the answer is \(a_6\cdot a_6+a_2\cdot a_2+a_1\cdot a_1=1+25+1=27\). | Input: 6 2 1 5 2 3 1 1 1 2 3 3 2 4 5 6 6 | Output: 33 27 | Hard | 4 | 798 | 431 | 93 | 18 |
1,718 | D | 1718D | D. Permutation for Burenka | 3,300 | data structures; graph matchings; greedy; math; trees | We call an array \(a\) pure if all elements in it are pairwise distinct. For example, an array \([1, 7, 9]\) is pure, \([1, 3, 3, 7]\) isn't, because \(3\) occurs twice in it.A pure array \(b\) is similar to a pure array \(c\) if their lengths \(n\) are the same and for all pairs of indices \(l\), \(r\), such that \(1 ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) is the number of test cases. The description of the test cases follows.The first line of each test case contains a couple of integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)).The second line of each input test case contains \(n\) integers \(... | Output \(q\) lines. For each value \(d\), print ""YES"" if there is a way to fill the array \(a\) to make it similar to \(p\), and ""NO"" otherwise. | In the first test case for \(d = 9\), you can get \(a = [5, 9, 7, 6]\), it can be proved that \(a\) is similar to \(p\), for \(d=1\) and \(d=4\) it can be proved that there is no answer.In the second test case for \(d = 1\), you can get \(a = [1, 5, 10, 9, 3]\), for \(d = 8\), you can get \(a = [3, 5, 10, 9, 8]\), it c... | Input: 44 31 4 3 25 0 7 069145 31 2 5 4 30 5 10 0 03 918115 21 4 3 2 50 0 0 0 07 9 1 561004 24 1 3 20 5 3 0246 | Output: YES NO NO YES YES NO YES YES NO NO | Master | 5 | 1,481 | 1,259 | 148 | 17 |
946 | C | 946C | C. String Transformation | 1,300 | greedy; strings | You are given a string s consisting of |s| small english letters.In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot replace letter z with any other letter.Your target is to make some number of move... | The only one line of the input consisting of the string s consisting of |s| (1 β€ |s| β€ 105) small english letters. | If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print Β«-1Β» (without quotes). | Input: aacceeggiikkmmooqqssuuwwyy | Output: abcdefghijklmnopqrstuvwxyz | Easy | 2 | 679 | 114 | 167 | 9 | |
978 | A | 978A | A. Remove Duplicates | 800 | implementation | Petya has an array \(a\) consisting of \(n\) integers. He wants to remove duplicate (equal) elements.Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed. | The first line contains a single integer \(n\) (\(1 \le n \le 50\)) β the number of elements in Petya's array.The following line contains a sequence \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 1\,000\)) β the Petya's array. | In the first line print integer \(x\) β the number of elements which will be left in Petya's array after he removed the duplicates.In the second line print \(x\) integers separated with a space β Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left. | In the first example you should remove two integers \(1\), which are in the positions \(1\) and \(4\). Also you should remove the integer \(5\), which is in the position \(2\).In the second example you should remove integer \(2\), which is in the position \(1\), and two integers \(4\), which are in the positions \(2\) ... | Input: 61 5 5 1 6 1 | Output: 35 6 1 | Beginner | 1 | 265 | 221 | 307 | 9 |
1,987 | E | 1987E | E. Wonderful Tree! | 2,000 | brute force; data structures; dfs and similar; dsu; greedy; trees | God's Blessing on This ArrayForces!A Random PebbleYou are given a tree with \(n\) vertices, rooted at vertex \(1\). The \(i\)-th vertex has an integer \(a_i\) written on it.Let \(L\) be the set of all direct children\(^{\text{β}}\) of \(v\). A tree is called wonderful, if for all vertices \(v\) where \(L\) is not empty... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5000\)) β the number of vertices in the tree.The se... | For each test case, output a single integer β the minimum number of operations needed to make the tree wonderful. | The tree in the first test case: You can apply the operation once on vertex \(5\) and twice on vertex \(2\) to get a wonderful tree.In the second test case, you can apply the operation twice on vertex \(2\) to get a wonderful tree.In the third and fourth test cases, the tree is already wonderful, so you don't need to a... | Input: 459 3 4 1 21 1 3 325 31236 54130 0 01 2 | Output: 3 2 0 0 | Hard | 6 | 700 | 783 | 113 | 19 |
1,197 | E | 1197E | E. Culture Code | 2,300 | binary search; combinatorics; data structures; dp; shortest paths; sortings | There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has \(n\) different matryoshkas. Any matryoshka is a figure of volume \(out_i\) with an empty space inside of volume \(in_i\) (of course, \(out_i > in_i\)).You don't have ... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of matryoshkas.The next \(n\) lines contain a description of each doll: two integers \(out_i\) and \(in_i\) (\(1 \le in_i < out_i \le 10^9\)) β the outer and inners volumes of the \(i\)-th matryoshka. | Print one integer β the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo \(10^9 + 7\). | There are \(6\) big enough nested subsets with minimum possible extra space in the example: \(\{1, 5\}\): we can't add any other matryoshka and keep it nested; it's extra space is \(1\); \(\{1, 6\}\); \(\{2, 4, 5\}\); \(\{2, 4, 6\}\); \(\{3, 4, 5\}\); \(\{3, 4, 6\}\). There are no more ""good"" subsets because, for exa... | Input: 7 4 1 4 2 4 2 2 1 5 4 6 4 3 2 | Output: 6 | Expert | 6 | 2,008 | 291 | 188 | 11 |
2,004 | C | 2004C | C. Splitting Items | 1,100 | games; greedy; sortings | Alice and Bob have \(n\) items they'd like to split between them, so they decided to play a game. All items have a cost, and the \(i\)-th item costs \(a_i\). Players move in turns starting from Alice.In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left.Let's... | The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(0 \le k \le 10^9\)) β the number of items and the maximum total increase Bob can make.The seco... | For each test case, print a single integer β the minimum possible score \(A - B\) after Bob increases the costs of several (possibly none or all) items. | In the first test case, Bob can increase \(a_1\) by \(5\), making costs equal to \([6, 10]\). Tomorrow, Alice will take \(10\) and Bob will take \(6\). The total score will be equal to \(10 - 6 = 4\), and it's the minimum possible.In the second test case, Bob can't change costs. So the score will be equal to \((15 + 10... | Input: 42 51 103 010 15 124 63 1 2 42 46 9 | Output: 4 13 0 0 | Easy | 3 | 1,043 | 543 | 152 | 20 |
248 | B | 248B | B. Chilly Willy | 1,400 | math; number theory | Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.Chilly Willy wants to find the minimum number of length n, such that it is simultaneously d... | A single input line contains a single integer n (1 β€ n β€ 105). | Print a single integer β the answer to the problem without leading zeroes, or ""-1"" (without the quotes), if the number that meet the problem condition does not exist. | Input: 1 | Output: -1 | Easy | 2 | 494 | 62 | 168 | 2 | |
1,895 | G | 1895G | G. Two Characters, Two Colors | 3,100 | binary search; data structures; dp; flows; greedy | You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue.If you paint the \(i\)-th character red, you get \(r_i\) coins. If you paint it blue, you get \(b_i\) coins.After coloring the string, you remove every blue character from it,... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of four lines: the first line contains one integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β the length of the string; the second line contains \(s\) β a string of \(n\) characters, where each c... | For each test case, print one integer β the maximum number of coins you can earn. | Explanations for the test cases for the example (blue characters are underlined, red ones are not): \(0100\underline{0}1\underline{0}\); \(10\underline{11}1\); \(\underline{0}1\underline{00000000}\); \(0\underline{1}010000\). | Input: 4701000106 6 6 7 7 6 63 3 5 4 7 6 75101119 8 5 7 54 4 7 8 41001000000007 7 6 5 2 2 5 3 8 38 6 9 6 6 8 9 7 7 98010100008 7 7 7 8 7 7 84 4 4 2 1 4 4 4 | Output: 43 36 76 52 | Master | 5 | 607 | 650 | 81 | 18 |
1,017 | H | 1017H | H. The Films | 3,300 | brute force | In ""The Man in the High Castle"" world, there are \(m\) different film endings. Abendsen owns a storage and a shelf. At first, he has \(n\) ordered films on the shelf. In the \(i\)-th month he will do: Empty the storage. Put \(k_i \cdot m\) films into the storage, \(k_i\) films for each ending. He will think about a q... | The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \le n, m, q \le 10^5\), \(n+q\leq 10^5\)) β the number of films on the shelf initially, the number of endings, and the number of months.The second line contains \(n\) integers \(e_1, e_2, \ldots, e_n\) (\(1\leq e_i\leq m\)) β the ending of the \(i\)-th... | Print the answer for each question in a separate line. | In the first sample in the second query, after adding \(2 \cdot m\) films into the storage, the storage will look like this: \(\{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4\}\).There are \(26730\) total ways of choosing the films so that \(e_l, e_{l+1}, \ldots, e_r\) will not be changed, for example, \([1, 2, 3, 2, 2]\) a... | Input: 6 4 41 2 3 4 4 41 4 01 3 21 4 21 5 2 | Output: 626730121504860 | Master | 1 | 1,118 | 578 | 54 | 10 |
807 | B | 807B | B. T-Shirt Hunt | 1,300 | brute force; implementation | Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participants out of those who got into top 500, will receive a Codeforces T-shirt.Unfortunately, you didn't manage to get into top 25, but you got into top 500, taking place p.Now the elimination round o... | The only line contains three integers p, x and y (26 β€ p β€ 500; 1 β€ y β€ x β€ 20000) β your place in Codecraft-17, your current score in the elimination round of 8VC Venture Cup 2017, and the smallest number of points you consider sufficient for winning the current round. | Output a single integer β the smallest number of successful hacks you have to do in order to both win the elimination round of 8VC Venture Cup 2017 and ensure getting a Codecraft-17 T-shirt.It's guaranteed that your goal is achievable for any valid input data. | In the first example, there is no need to do any hacks since 10880 points already bring the T-shirt to the 239-th place of Codecraft-17 (that is, you). In this case, according to the pseudocode, the T-shirts will be given to the participants at the following places: 475 422 84 411 453 210 157 294 146 188 420 367 29 356... | Input: 239 10880 9889 | Output: 0 | Easy | 2 | 1,629 | 270 | 260 | 8 |
1,617 | D1 | 1617D1 | D1. Too Many Impostors (easy version) | 1,800 | constructive algorithms; implementation; interactive | This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.There are \(n\) players labelled from \(1\) to \(n\). It is guaranteed that \(n\) is a multiple of \(3\).Among them, there are \(k\) impostors and \(n-k\) crewmates. The number of impostors, \(k\), ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first and only line of each test case contains a single integer \(n\) (\(6 \le n < 10^4\), \(n\) is a multiple of \(3\)) β the number of players.It is guarantee... | Explanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):For the first test case:Question ""? 1 2 3"" returns \(0\), so there are more impostors than crewmates among players \(1\), \(2\) and \(3\).Question ""? 3 4 5"... | Input: 2 6 0 1 9 1 | Output: ? 1 2 3 ? 3 4 5 ! 3 4 1 2 ? 7 1 9 ! 4 2 3 6 8 | Medium | 3 | 1,087 | 397 | 0 | 16 | |
753 | A | 753A | A. Santa Claus and Candies | 1,000 | dp; greedy; math | Santa Claus has n candies, he dreams to give them as gifts to children.What is the maximal number of children for whose he can give candies if Santa Claus want each kid should get distinct positive integer number of candies. Santa Class wants to give all n candies he has. | The only line contains positive integer number n (1 β€ n β€ 1000) β number of candies Santa Claus has. | Print to the first line integer number k β maximal number of kids which can get candies.Print to the second line k distinct integer numbers: number of candies for each of k kid. The sum of k printed numbers should be exactly n.If there are many solutions, print any of them. | Input: 5 | Output: 22 3 | Beginner | 3 | 272 | 100 | 274 | 7 | |
351 | C | 351C | C. Jeff and Brackets | 2,500 | dp; matrices | Jeff loves regular bracket sequences.Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of nm brackets. Let's number all brackets of this sequence from 0 to nm - 1 from left to right. Jeff knows that he is going to spend ai mod n liters of ink on the i-th bracket of the ... | The first line contains two integers n and m (1 β€ n β€ 20; 1 β€ m β€ 107; m is even). The next line contains n integers: a0, a1, ..., an - 1 (1 β€ ai β€ 10). The next line contains n integers: b0, b1, ..., bn - 1 (1 β€ bi β€ 10). The numbers are separated by spaces. | In a single line print the answer to the problem β the minimum required amount of ink in liters. | In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12. | Input: 2 61 22 1 | Output: 12 | Expert | 2 | 611 | 259 | 96 | 3 |
138 | E | 138E | E. Hellish Constraints | 2,900 | brute force; dp; two pointers | Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those ""N does not exceed a thousand"" and ""the sum of ai does not exceed a million"" and she decided to come up with something a little more complicated.Th... | The first line contains a non-empty string s, consisting of small Latin letters. The length of the string s does not exceed 105.The second line contains three space-separated integers k, L and R (0 β€ L β€ R β€ k β€ 500).Next k lines contain Katya's constrictions in the following form ""ci li ri"". All letters ci are small... | Print a single number β the number of substrings that meet the constrictions.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cout stream or the %I64d specificator. | In the first test we should count the number of strings that do not contain characters ""e"" and ""o"". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): ""c"", ""d""', ""f"", ""r"", ""rc"", ""c"", ""s"".In the second test we cannot achieve fulfilling exactly... | Input: codeforces2 0 0o 1 2e 1 2 | Output: 7 | Master | 3 | 1,466 | 458 | 223 | 1 |
1,823 | D | 1823D | D. Unique Palindromes | 1,900 | constructive algorithms; math; strings | A palindrome is a string that reads the same backwards as forwards. For example, the string abcba is palindrome, while the string abca is not.Let \(p(t)\) be the number of unique palindromic substrings of string \(t\), i. e. the number of substrings \(t[l \dots r]\) that are palindromes themselves. Even if some substri... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le k \le 20\)) β length of good string \(s\) and num... | For each test case, if there is no good string \(s\) of length \(n\) that satisfies all conditions, print NO.Otherwise, print YES and a string \(s\) of length \(n\), consisting of lowercase Latin letters, that satisfies all conditions. If there are multiple answers, print any of them. | In the first test case, string \(s\) \(=\) abcbbcabcb satisfies \(k = 2\) conditions: \(p(s, x_1) = p(s, 5) =\) \(p(\)abcbb\() = 5 = s_1\). Palindromic substrings are a, b, c, bb and bcb. \(p(s, x_2) = p(s, 10) =\) \(p(\)abcbbcabcb\() = 6 = s_2\). Palindromic substrings are the same as above, and one extra substring cb... | Input: 710 25 105 63 1334 23 43 34 23 43 44 14510 34 6 104 5 810 44 6 7 104 5 7 8 | Output: YES abcbbcabcb YES foo YES ayda YES wada NO YES abcbcacbab NO | Hard | 3 | 1,313 | 877 | 285 | 18 |
845 | C | 845C | C. Two TVs | 1,500 | data structures; greedy; sortings | Polycarp is a great fan of television.He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri.Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment ... | The first line contains one integer n (1 β€ n β€ 2Β·105) β the number of shows.Each of the next n lines contains two integers li and ri (0 β€ li < ri β€ 109) β starting and ending time of i-th show. | If Polycarp is able to check out all the shows using only two TVs then print ""YES"" (without quotes). Otherwise, print ""NO"" (without quotes). | Input: 31 22 34 5 | Output: YES | Medium | 3 | 505 | 193 | 144 | 8 | |
1,361 | A | 1361A | A. Johnny and Contribution | 1,700 | constructive algorithms; graphs; greedy; sortings | Today Johnny wants to increase his contribution. His plan assumes writing \(n\) blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the read... | The first line contains two integers \(n\) \((1 \leq n \leq 5 \cdot 10^5)\) and \(m\) \((0 \leq m \leq 5 \cdot 10^5)\) β the number of blogs and references, respectively.Each of the following \(m\) lines contains two integers \(a\) and \(b\) (\(a \neq b\); \(1 \leq a, b \leq n\)), which mean that there is a reference b... | If the solution does not exist, then write \(-1\). Otherwise, output \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) \((1 \leq p_i \leq n)\), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any. | In the first example, Johnny starts with writing blog number \(2\), there are no already written neighbors yet, so it receives the first topic. Later he writes blog number \(1\), it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number \(3\), it has referen... | Input: 3 3 1 2 2 3 3 1 2 1 3 | Output: 2 1 3 | Medium | 4 | 1,524 | 566 | 262 | 13 |
628 | A | 628A | A. Tennis Tournament | 1,000 | implementation; math | A tennis tournament with n participants is running. The participants are playing by an olympic system, so the winners move on and the losers drop out.The tournament takes place in the following way (below, m is the number of the participants of the current round): let k be the maximal power of the number 2 such that k ... | The only line contains three integers n, b, p (1 β€ n, b, p β€ 500) β the number of participants and the parameters described in the problem statement. | Print two integers x and y β the number of bottles and towels need for the tournament. | In the first example will be three rounds: in the first round will be two matches and for each match 5 bottles of water are needed (two for each of the participants and one for the judge), in the second round will be only one match, so we need another 5 bottles of water, in the third round will also be only one match, ... | Input: 5 2 3 | Output: 20 15 | Beginner | 2 | 886 | 149 | 86 | 6 |
847 | D | 847D | D. Dog Show | 2,200 | constructive algorithms; data structures; greedy | A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win!On the show a dog needs to eat as many bowls of dog food as possib... | Two integer numbers are given in the first line - n and T (1 β€ n β€ 200 000, 1 β€ T β€ 2Β·109) β the number of bowls of food and the time when the dog is stopped.On the next line numbers t1, t2, ..., tn (1 β€ ti β€ 109) are given, where ti is the moment of time when the i-th bowl of food is ready for eating. | Output a single integer β the maximum number of bowls of food the dog will be able to eat in T seconds. | In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third). | Input: 3 51 5 3 | Output: 2 | Hard | 3 | 1,982 | 303 | 103 | 8 |
1,679 | F | 1679F | F. Formalism for Formalism | 2,600 | bitmasks; dp; math | Yura is a mathematician, and his cognition of the world is so absolute as if he have been solving formal problems a hundred of trillions of billions of years. This problem is just that!Consider all non-negative integers from the interval \([0, 10^{n})\). For convenience we complement all numbers with leading zeros in s... | The first line contains an integer \(n\) (\(1 \le n \le 50\,000\)) β the number of digits in considered numbers.The second line contains an integer \(m\) (\(0 \le m \le 45\)) β the number of pairs of digits in the set.Each of the following \(m\) lines contains two digits \(u_i\) and \(v_i\), separated with a space (\(0... | Print one integer β the maximum value \(k\) such that there exists a set of integers \(x_1, x_2, \ldots, x_k\) (\(0 \le x_i < 10^{n}\)) such that for each \(1 \le i < j \le k\) the number \(x_i\) is not equivalent to the number \(x_j\).As the answer can be big enough, print the number \(k\) modulo \(998\,244\,353\). | In the first example we can construct a set that contains all integers from \(0\) to \(9\). It's easy to see that there are no two equivalent numbers in the set.In the second example there exists a unique pair of equivalent numbers: \(01\) and \(10\). We can construct a set that contains all integers from \(0\) to \(99... | Input: 1 0 | Output: 10 | Expert | 3 | 1,509 | 407 | 317 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.