contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,133 | A | 1133A | A. Middle of the Contest | 1,000 | implementation | Polycarp is going to participate in the contest. It starts at \(h_1:m_1\) and ends at \(h_2:m_2\). It is guaranteed that the contest lasts an even number of minutes (i.e. \(m_1 \% 2 = m_2 \% 2\), where \(x \% y\) is \(x\) modulo \(y\)). It is also guaranteed that the entire contest is held during a single day. And fina... | The first line of the input contains two integers \(h_1\) and \(m_1\) in the format hh:mm.The second line of the input contains two integers \(h_2\) and \(m_2\) in the same format (hh:mm).It is guaranteed that \(0 \le h_1, h_2 \le 23\) and \(0 \le m_1, m_2 \le 59\).It is guaranteed that the contest lasts an even number... | Print two integers \(h_3\) and \(m_3\) (\(0 \le h_3 \le 23, 0 \le m_3 \le 59\)) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ':'. | Input: 10:00 11:00 | Output: 10:30 | Beginner | 1 | 619 | 552 | 257 | 11 | |
1,850 | C | 1850C | C. Word on the Paper | 800 | implementation; strings | On an \(8 \times 8\) grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it? | The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.Each test case consists of \(8\) lines, each containing \(8\) characters. Each character in the grid is either \(\texttt{.}\) (representing a dot) or a lowercase Lat... | For each test case, output a single line containing the word made up of lowercase Latin letters (\(\texttt{a}\)β\(\texttt{z}\)) that is written vertically in one column from top to bottom. | Input: 5...................................i.....................................l.......o.......s.......t....................................................................t.......h.......e................................................g.......a.......m.......ea.......a.......a.......a.......a.......a.......a.......... | Beginner | 2 | 148 | 518 | 188 | 18 | |
1,553 | I | 1553I | I. Stairs | 3,400 | combinatorics; divide and conquer; dp; fft; math | For a permutation \(p\) of numbers \(1\) through \(n\), we define a stair array \(a\) as follows: \(a_i\) is length of the longest segment of permutation which contains position \(i\) and is made of consecutive values in sorted order: \([x, x+1, \ldots, y-1, y]\) or \([y, y-1, \ldots, x+1, x]\) for some \(x \leq y\). F... | The first line of input contains integer \(n\) (\(1 \le n \le 10^5\)) β the length of a stair array \(a\).The second line of input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)). | Print the number of permutations which have stair array equal to \(a\). Since the number can be big, compute it modulo \(998\,244\,353\). | Input: 6 3 3 3 1 1 1 | Output: 6 | Master | 5 | 655 | 203 | 137 | 15 | |
600 | F | 600F | F. Edge coloring of bipartite graph | 2,800 | graphs | You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour. | The first line contains three integers a, b, m (1 β€ a, b β€ 1000, 0 β€ m β€ 105), a is the size of the first part, b is the size of the second part, m is the number of edges in the graph.Each of the next m lines contains two integers x, y (1 β€ x β€ a, 1 β€ y β€ b), where x is the number of the vertex in the first part and y ... | In the first line print integer c β the minimal number of colours. The second line should contain m integers from 1 to c β the colours of the edges (in the order they appear in the input).If there are several solutions, you can print any one of them. | Input: 4 3 51 22 23 24 14 3 | Output: 31 2 3 1 2 | Master | 1 | 185 | 418 | 250 | 6 | |
1,979 | A | 1979A | A. Guess the Maximum | 800 | brute force; greedy; implementation | Alice and Bob came up with a rather strange game. They have an array of integers \(a_1, a_2,\ldots, a_n\). Alice chooses a certain integer \(k\) and tells it to Bob, then the following happens: Bob chooses two integers \(i\) and \(j\) (\(1 \le i < j \le n\)), and then finds the maximum among the integers \(a_i, a_{i + ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^4\)) β the number of elements in the array.The... | For each test case, output one integer β the maximum integer \(k\) at which Alice is guaranteed to win. | In the first test case, all possible subsegments that Bob can choose look as follows: \([2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]\). The maximums on the subsegments are respectively equal to \(4, 4, 7, 4, 7, 7\). It can be shown that \(3\) is the largest integer such that any of the maximums will be st... | Input: 642 4 1 751 2 3 4 521 1337 8 16510 10 10 10 9103 12 9 5 2 3 2 9 8 2 | Output: 3 1 0 15 9 2 | Beginner | 3 | 494 | 545 | 103 | 19 |
9 | A | 9A | A. Die Roll | 800 | math; probabilities | Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpr... | The only line of the input file contains two natural numbers Y and W β the results of Yakko's and Wakko's die rolls. | Output the required probability in the form of irreducible fraction in format Β«A/BΒ», where A β the numerator, and B β the denominator. If the required probability equals to zero, output Β«0/1Β». If the required probability equals to 1, output Β«1/1Β». | Dot will go to Transylvania, if she is lucky to roll 4, 5 or 6 points. | Input: 4 2 | Output: 1/2 | Beginner | 2 | 1,017 | 116 | 247 | 0 |
1,662 | J | 1662J | J. Training Camp | 0 | flows; graphs | You are organizing a training camp to teach algorithms to young kids. There are \(n^2\) kids, organized in an \(n\) by \(n\) grid. Each kid is between \(1\) and \(n\) years old (inclusive) and any two kids who are in the same row or in the same column have different ages.You want to select exactly \(n\) kids for a prog... | The first line contains \(n\) (\(1 \leq n \leq 128\)) β the size of the grid.The following \(n\) lines describe the ages of the kids. Specifically, the \(i\)-th line contains \(n\) integers \(a_{i,1}, \, a_{i,2}, \, \dots, \, a_{i, n}\) (\(1 \le a_{i,j} \le n\)) β where \(a_{i,j}\) is the age of the kid in the \(i\)-th... | Print the maximum number of kids good at programming that you can select while satisfying all the requirements. | In the first sample, it is not possible to select the two kids good at programming (in row \(1\) and column \(1\), and in row \(2\) and column \(3\)), because then you would have to select the kid in row \(3\) and column \(2\), and in that case two kids would complain (the one in row \(1\) and column \(2\), and the one... | Input: 3 1 2 3 3 1 2 2 3 1 1 0 0 0 0 1 0 0 0 | Output: 1 | Beginner | 2 | 964 | 905 | 111 | 16 |
1,340 | C | 1340C | C. Nastya and Unexpected Guest | 2,400 | dfs and similar; dp; graphs; shortest paths | If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of \(n\) lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place... | The first line contains two integers \(n\) and \(m\) \((1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4))\) β road width and the number of safety islands.The second line contains \(m\) distinct integers \(d_1, d_2, \ldots, d_m\) \((0 \leq d_i \leq n)\) β the points where the safety islands are located. It is guarante... | Output a single integer β the minimum time for which Denis can cross the road with obeying all the rules.If it is impossible to cross the road output \(-1\). | In the first test, the optimal route is: for the first green light, go to \(7\) and return to \(3\). In this case, we will change the direction of movement at the point \(7\), which is allowed, since there is a safety island at this point. In the end, we will be at the point of \(3\), where there is also a safety islan... | Input: 15 5 0 3 7 14 15 11 11 | Output: 45 | Expert | 4 | 2,543 | 521 | 157 | 13 |
2,104 | B | 2104B | B. Move to the End | 1,000 | brute force; data structures; dp; greedy; implementation | You are given an array \(a\) consisting of \(n\) integers.For every integer \(k\) from \(1\) to \(n\), you have to do the following: choose an arbitrary element of \(a\) and move it to the right end of the array (you can choose the last element, then the array won't change); print the sum of \(k\) last elements of \(a\... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)); the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)). Additional constraint on ... | For each test case, print \(n\) integers. The \(i\)-th of these integers should be equal to the maximum value you can print if \(k=i\). | Let's consider the first test case from the statement: when \(k = 1\), you can move the \(6\)-th element to the end, the array becomes \([13, 5, 10, 14, 8, 13, 15]\), and the value you print is \(15\); when \(k = 2\), you can move the \(6\)-th element to the end, the array becomes \([13, 5, 10, 14, 8, 13, 15]\), and th... | Input: 4713 5 10 14 8 15 1361000000000 1000000000 1000000000 1000000000 1000000000 100000000014227 5 | Output: 15 28 42 50 63 73 78 1000000000 2000000000 3000000000 4000000000 5000000000 6000000000 42 7 12 | Beginner | 5 | 586 | 401 | 135 | 21 |
336 | D | 336D | D. Vasily the Bear and Beautiful Strings | 2,100 | combinatorics; math; number theory | Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria: String s only consists of characters 0 and 1, at that character 0 must occur in string s exactly n times, and character 1 must occur exactly m times. We can obtain character g from string s with some (possibly, zero) numb... | The first line of the input contains three space-separated integers n, m, g (0 β€ n, m β€ 105, n + m β₯ 1, 0 β€ g β€ 1). | Print a single integer β the answer to the problem modulo 1000000007 (109 + 7). | In the first sample the beautiful strings are: ""01"", ""10"".In the second sample the beautiful strings are: ""0011"", ""1001"", ""1010"", ""1100"".In the third sample there are no beautiful strings. | Input: 1 1 0 | Output: 2 | Hard | 3 | 983 | 115 | 79 | 3 |
314 | A | 314A | A. Sereja and Contest | 1,600 | implementation | During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants. Let's assume that n people took part in the contest. Let's assume that the participant who got the first place has rating a1, the second place participant has rating a2, ..., the n... | The first line contains two integers n, k (1 β€ n β€ 2Β·105, - 109 β€ k β€ 0). The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β ratings of the participants in the initial table. | Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table. | Consider the first test sample. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be consider... | Input: 5 05 3 4 1 2 | Output: 234 | Medium | 1 | 1,596 | 208 | 205 | 3 |
188 | H | 188H | H. Stack | 1,800 | *special; expression parsing; implementation | In this problem we'll use a stack which supports two types of operations: Push a given number on the stack. Pop two numbers from the stack, perform a given operation (addition or multiplication) on them and push the result on the stack. You are given a string which describes the sequence of operations to be performed o... | The only line of input contains a string of operations, consisting of characters Β«+Β», Β«*Β» and digits (0..9). The length of the string will be between 1 and 20 characters, inclusive.The given sequence of operations is guaranteed to be correct, i.e. the stack will have at least two elements before every math operation. T... | Output a single number β the topmost element of the stack after performing all given operations. | In the first case the stack will end up containing a single number β the result of calculating (1+2)*3+6*6.In the second case there are no math operations, so the answer will be the last number pushed on the stack. | Input: 12+3*66*+ | Output: 45 | Medium | 3 | 629 | 366 | 96 | 1 |
1,883 | D | 1883D | D. In Love | 1,500 | data structures; greedy | Initially, you have an empty multiset of segments. You need to process \(q\) operations of two types: \(+\) \(l\) \(r\) β Add the segment \((l, r)\) to the multiset, \(-\) \(l\) \(r\) β Remove exactly one segment \((l, r)\) from the multiset. It is guaranteed that this segment exists in the multiset.After each operatio... | The first line of each test case contains an integer \(q\) (\(1 \leq q \leq 10^5\)) β the number of operations.The next \(q\) lines describe two types of operations. If it is an addition operation, it is given in the format \(+\) \(l\) \(r\). If it is a deletion operation, it is given in the format \(-\) \(l\) \(r\) (\... | After each operation, print ""YES"" if there exists a pair of segments in the multiset that do not intersect, and ""NO"" otherwise.You can print the answer in any case (uppercase or lowercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive answers. | In the example, after the second, third, fourth, and fifth operations, there exists a pair of segments \((1, 2)\) and \((3, 4)\) that do not intersect.Then we remove exactly one segment \((3, 4)\), and by that time we had two segments. Therefore, the answer after this operation also exists. | Input: 12+ 1 2+ 3 4+ 2 3+ 2 2+ 3 4- 3 4- 3 4- 1 2+ 3 4- 2 2- 2 3- 3 4 | Output: NO YES YES YES YES YES NO NO YES NO NO NO | Medium | 2 | 573 | 350 | 297 | 18 |
2,048 | C | 2048C | C. Kevin and Binary Strings | 1,200 | bitmasks; brute force; greedy; implementation; strings | Kevin discovered a binary string \(s\) that starts with 1 in the river at Moonlit River Park and handed it over to you. Your task is to select two non-empty substrings\(^{\text{β}}\) of \(s\) (which can be overlapped) to maximize the XOR value of these two substrings.The XOR of two binary strings \(a\) and \(b\) is def... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)).The only line of each test case contains a binary string \(s\) that starts with 1 (\(1\le\lvert s\rvert\le 5000\)).It is guaranteed that the sum of \(\lvert s\rvert\) over all test cases doesn't exceed ... | For each test case, output four integers \(l_1, r_1, l_2, r_2\) (\(1 \le l_1 \le r_1 \le |s|\), \(1 \le l_2 \le r_2 \le |s|\)) β in the case the two substrings you selected are \(s_{l_1} s_{l_1 + 1} \ldots s_{r_1}\) and \(s_{l_2} s_{l_2 + 1} \ldots s_{r_2}\).If there are multiple solutions, print any of them. | In the first test case, we can choose \( s_2=\texttt{1} \) and \( s_1 s_2 s_3=\texttt{111} \), and \( \texttt{1}\oplus\texttt{111}=\texttt{110} \). It can be proven that it is impossible to obtain a larger result. Additionally, \( l_1=3\), \(r_1=3\), \(l_2=1\), \(r_2=3 \) is also a valid solution.In the second test cas... | Input: 5111100010111111011100010001101 | Output: 2 2 1 3 1 3 1 4 1 5 1 4 3 4 1 5 1 13 1 11 | Easy | 5 | 845 | 329 | 310 | 20 |
1,759 | A | 1759A | A. Yes-Yes? | 800 | implementation; strings | You talked to Polycarp and asked him a question. You know that when he wants to answer ""yes"", he repeats Yes many times in a row.Because of the noise, you only heard part of the answer β some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.De... | The first line of input data contains the singular \(t\) (\(1 \le t \le 1000\)) β the number of test cases in the test.Each test case is described by a single string of Latin letters \(s\) (\(1 \le |s| \le 50\)) β the part of Polycarp's answer that you heard, where \(|s|\) β is the length of the string \(s\). | Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if the specified string \(s\) is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""... | Input: 12YESesYescodeforcesesseYesYesYesYesYesYesYesYeseYYesssYoYes | Output: NO YES NO YES NO YES YES NO NO YES NO YES | Beginner | 2 | 436 | 310 | 388 | 17 | |
935 | B | 935B | B. Fafa and the Gates | 900 | implementation | Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.... | The first line of the input contains single integer n (1 β€ n β€ 105) β the number of moves in the walking sequence.The second line contains a string S of length n consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence S in order from left to right. | On a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence S. | The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins. | Input: 1U | Output: 0 | Beginner | 1 | 1,302 | 294 | 132 | 9 |
2,000 | G | 2000G | G. Call During the Journey | 2,100 | binary search; brute force; graphs; greedy; shortest paths | You live in a city consisting of \(n\) intersections and \(m\) streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possib... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The following are the descriptions of the test cases.The first line of each test case contains two integers \(n\), \(m\) (\(2 \le n \le 10^5, 1 \le m \le 10^5\)) β the number of intersections and streets in the city.The second l... | For each test case, output a single integer β the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1. | Input: 75 5100 20 801 5 30 1001 2 20 502 3 20 503 4 20 504 5 20 502 1100 50 601 2 55 1104 4100 40 601 2 30 1002 4 30 1001 3 20 503 4 20 503 3100 80 901 2 1 102 3 10 501 3 20 213 258 55 572 1 1 32 3 3 42 112 9 102 1 6 105 58 5 62 1 1 82 3 4 84 2 2 45 3 3 44 5 2 6 | Output: 0 -1 60 80 53 3 2 | Hard | 5 | 1,468 | 1,243 | 195 | 20 | |
140 | B | 140B | B. New Year Cards | 1,800 | brute force; greedy; implementation | As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's ... | The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format. | Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them. | In the sample, the algorithm of actions Alexander and his friends perform is as follows: Alexander receives card 1 from the first friend. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. Alexander receives... | Input: 41 2 3 44 1 3 24 3 1 23 4 2 13 1 2 4 | Output: 2 1 1 4 | Medium | 3 | 1,753 | 296 | 220 | 1 |
460 | C | 460C | C. Present | 1,700 | binary search; data structures; greedy | Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noti... | The first line contains space-separated integers n, m and w (1 β€ w β€ n β€ 105; 1 β€ m β€ 105). The second line contains space-separated integers a1, a2, ..., an (1 β€ ai β€ 109). | Print a single integer β the maximum final height of the smallest flower. | In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test. | Input: 6 2 32 2 2 2 1 1 | Output: 2 | Medium | 3 | 993 | 173 | 73 | 4 |
1,054 | F | 1054F | F. Electric Scheme | 2,700 | flows; graph matchings | Pasha is a young technician, nevertheless, he has already got a huge goal: to assemble a PC. The first task he has to become familiar with is to assemble an electric scheme.The scheme Pasha assembled yesterday consists of several wires. Each wire is a segment that connects two points on a plane with integer coordinates... | The first line contains a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of points where Pasha saw sparks.Each of the next \(n\) lines contain two integers \(x\) and \(y\) (\(1 \leq x, y \leq 10^9\)) β the coordinates of a point with sparks. It is guaranteed that all points are distinct. | Print the description of the scheme in the following format.In the first line print \(h\) β the number of horizontal red wires (\(0 \leq h\)). In each of the next \(h\) lines print \(4\) integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) β the coordinates of two points \((x_1, y_1)\) and \((x_2, y_2)\) that are connected with... | In the first example Pasha could have assembled the following scheme: In this scheme there are \(2\) wires of each color: red ones connecting \((5, 2)\) with \((1, 2)\) and \((1, 4)\) with \((5, 4)\), blue ones connecting \((2, 1)\) with \((2, 5)\) and \((4, 5)\) with \((4, 1)\). Note that the sparks appear in the poin... | Input: 42 22 44 24 4 | Output: 25 2 1 21 4 5 422 1 2 54 5 4 1 | Master | 2 | 1,572 | 300 | 1,178 | 10 |
292 | C | 292C | C. Beautiful IP Addresses | 2,000 | brute force | The problem uses a simplified TCP/IP address model, please read the statement carefully.An IP address is a 32-bit integer, represented as a group of four decimal 8-bit integers (without leading zeroes), separated by commas. For example, record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.0... | The first line contains a single integer n (1 β€ n β€ 10) β the number of digits in the set. The second line contains the set of integers a1, a2, ..., an (0 β€ ai β€ 9). It is guaranteed that all digits in the set are distinct. | In the first line print a single integer k β the number of beautiful IP addresses that contain the given set of digits. In the following k lines print the IP addresses, one per line in the arbitrary order. | Input: 60 1 2 9 8 7 | Output: 678.190.209.18779.180.208.19787.190.209.17889.170.207.19897.180.208.17998.170.207.189 | Hard | 1 | 1,262 | 223 | 205 | 2 | |
690 | A1 | 690A1 | A1. Collective Mindsets (easy) | 1,100 | Tonight is brain dinner night and all zombies will gather together to scarf down some delicious brains. The artful Heidi plans to crash the party, incognito, disguised as one of them. Her objective is to get away with at least one brain, so she can analyze the zombies' mindset back home and gain a strategic advantage.T... | The only line of input contains one integer: N, the number of attendees (1 β€ N β€ 109). | Output one integer: the smallest number of brains in the chest which allows Heidi to take one brain home. | Input: 1 | Output: 1 | Easy | 0 | 2,259 | 86 | 105 | 6 | ||
932 | F | 932F | F. Escape Through Leaf | 2,700 | data structures; dp; geometry | You are given a tree with n nodes (numbered from 1 to n) rooted at node 1. Also, each node has two values associated with it. The values for i-th node are ai and bi.You can jump from a node to any node in its subtree. The cost of one jump from node x to node y is the product of ax and by. The total cost of a path forme... | The first line of input contains an integer n (2 β€ n β€ 105) β the number of nodes in the tree.The second line contains n space-separated integers a1, a2, ..., an( - 105 β€ ai β€ 105).The third line contains n space-separated integers b1, b2, ..., bn( - 105 β€ bi β€ 105).Next n - 1 lines contains two space-separated integer... | Output n space-separated integers, i-th of which denotes the minimum cost of a path from node i to reach any leaf. | In the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost a2 Γ b3 = 50. For node 1, jump directly to node 3 with cost a1 Γ b3 = 10.In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost a2 Γ b4 = 100. For node 1, jump to n... | Input: 32 10 -17 -7 52 32 1 | Output: 10 50 0 | Master | 3 | 579 | 401 | 114 | 9 |
898 | C | 898C | C. Phone Numbers | 1,400 | implementation; strings | Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a frien... | First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books. The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English lette... | Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.The following m lines must contain entries in the following format ""name number_of_phone_numbers phone_numbers"". Phone numbers should be separated by a space. Each rec... | Input: 2ivan 1 00123masha 1 00123 | Output: 2masha 1 00123 ivan 1 00123 | Easy | 2 | 1,287 | 579 | 490 | 8 | |
1,556 | D | 1556D | D. Take a Guess | 1,800 | bitmasks; constructive algorithms; interactive; math | This is an interactive taskWilliam has a certain sequence of integers \(a_1, a_2, \dots, a_n\) in his mind, but due to security concerns, he does not want to reveal it to you completely. William is ready to respond to no more than \(2 \cdot n\) of the following questions: What is the result of a bitwise AND of two item... | It is guaranteed that for each element in a sequence the condition \(0 \le a_i \le 10^9\) is satisfied. | In the example, the hidden sequence is \([1, 6, 4, 2, 3, 5, 4]\).Below is the interaction in the example.Query (contestant's program)Response (interactor)Notesand 2 52\(a_2=6\), \(a_5=3\). Interactor returns bitwise AND of the given numbers.or 5 67\(a_5=3\), \(a_6=5\). Interactor returns bitwise OR of the given numbers... | Input: 7 6 2 7 | Output: and 2 5 or 5 6 finish 5 | Medium | 4 | 815 | 103 | 0 | 15 | |
822 | E | 822E | E. Liar | 2,400 | binary search; dp; hashing; string suffix structures | The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to ViΔkopolis. As a modest souvenir for Leha, she brought a sausage of length m from Pavlopolis. Everyone knows that any sausage can be represented as a string of lowercase English letters, th... | The first line contains single integer n (1 β€ n β€ 105) β length of sausage bought by Leha, i.e. the length of the string s.The second line contains string s of the length n consisting of lowercase English letters.The third line contains single integer m (1 β€ m β€ n) β length of sausage bought by Noora, i.e. the length o... | In the only line print ""YES"" (without quotes), if Leha is able to succeed in creating new sausage so that Noora doesn't notice anything. Otherwise print ""NO"" (without quotes). | Let's consider the first sample.In the optimal answer, Leha should cut the sausage he bought in the following way: hloyaygrt = h + loy + a + y + g + rt. Then he numbers received parts from 1 to 6: h β number 1 loy β number 2 a β number 3 y β number 4 g β number 5 rt β number 6 Hereupon the hacker should glue the parts ... | Input: 9hloyaygrt6loyyrt3 | Output: YES | Expert | 4 | 1,979 | 578 | 179 | 8 |
452 | F | 452F | F. Permutation | 2,700 | data structures; divide and conquer; hashing | You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 β€ a, b β€ n; a β b) such that the element (note, that it is usual division, not integer one) is between a and b in this permutation. | First line consists of a single integer n (1 β€ n β€ 300000) β the size of permutation.Second line contains n integers β the permutation itself. | Print ""YES"", if such a pair exists, ""NO"" otherwise (in both cases without quotes, the answer is case insensitive). | In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5. | Input: 41 3 4 2 | Output: NO | Master | 3 | 236 | 142 | 118 | 4 |
99 | A | 99A | A. Help Far Away Kingdom | 800 | strings | In a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there.Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the King... | The first line contains a single number to round up β the integer part (a non-empty set of decimal digits that do not start with 0 β with the exception of a case when the set consists of a single digit β in this case 0 can go first), then follows character Β«.Β» (a dot), and then follows the fractional part (any non-empt... | If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message ""GOTO Vasilisa."" (without the quotes). | Input: 0.0 | Output: 0 | Beginner | 1 | 1,399 | 466 | 179 | 0 | |
1,780 | D | 1780D | D. Bit Guessing Game | 1,800 | binary search; bitmasks; constructive algorithms; interactive | This is an interactive problem.Kira has a hidden positive integer \(n\), and Hayato needs to guess it.Initially, Kira gives Hayato the value \(\mathrm{cnt}\) β the number of unit bits in the binary notation of \(n\). To guess \(n\), Hayato can only do operations of one kind: choose an integer \(x\) and subtract it from... | The input data contains several test cases. The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains the number \(\mathrm{cnt}\) β the initial number of unit bits in the binary notation \(n\).The hidde... | For example, the number of unit bits in number \(6\) is \(2\), because binary notation of \(6\) is \(110\). For \(13\) the number of unit bits is \(3\), because \(13_{10} = 1101_2\).In the first test case, \(n = 1\), so the input is the number \(1\). After subtracting one from \(n\), it becomes zero, so the number of u... | Input: 3 1 0 1 1 0 2 1 0 | Output: - 1 ! 1 - 1 - 1 ! 2 - 2 - 1 ! 3 | Medium | 4 | 1,007 | 393 | 0 | 17 | |
32 | C | 32C | C. Flea | 1,700 | math | It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary n... | The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump. | Output the only integer β the number of the required starting positions of the flea. | Input: 2 3 1000000 | Output: 6 | Medium | 1 | 682 | 139 | 84 | 0 | |
808 | D | 808D | D. Array Division | 1,900 | binary search; data structures; implementation | Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element be... | The first line contains single integer n (1 β€ n β€ 100000) β the size of the array.The second line contains n integers a1, a2... an (1 β€ ai β€ 109) β the elements of the array. | Print YES if Vasya can divide the array after moving one element. Otherwise print NO. | In the first example Vasya can move the second element to the end of the array.In the second example no move can make the division possible.In the third example Vasya can move the fourth element by one position to the left. | Input: 31 3 2 | Output: YES | Hard | 3 | 593 | 174 | 85 | 8 |
18 | D | 18D | D. Seller Bob | 2,000 | brute force; dp; greedy | Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. Bob won some programming competition and got a 2x MB memory stick as a ... | The first input line contains number n (1 β€ n β€ 5000) β amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 β€ x β€ 2000). It's guaranteed that for each x there is not more than one line sell x. Li... | Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time. | Input: 7win 10win 5win 3sell 5sell 3win 10sell 10 | Output: 1056 | Hard | 3 | 859 | 395 | 203 | 0 | |
2,111 | C | 2111C | C. Equal Values | 1,100 | brute force; greedy; two pointers | You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) integers.In one operation, you are allowed to perform one of the following actions: Choose a position \(i\) (\(1 < i \le n\)) and make all elements to the left of \(i\) equal to \(a_i\). That is, assign the value \(a_i\) to all \(a_j\) (\(1 \le j < i\... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).The sum of \(n\) over all test cases does not... | For each test case, print a single integer β the minimum total cost of operations to make all elements of the array equal. | In the first test case, you can perform the operation twice: choose \(i = 3\) and make all elements to the left of it equal to it, the cost will be \(2 \cdot 1 = 2\); choose \(i = 3\) and make all elements to the right of it equal to it, the cost will be \(1 \cdot 1 = 1\). The total cost is \(2 + 1 = 3\).In the second ... | Input: 342 4 1 331 1 1107 5 5 5 10 9 9 4 6 10 | Output: 3 0 35 | Easy | 3 | 863 | 345 | 122 | 21 |
11 | D | 11D | D. A Simple Task | 2,200 | bitmasks; dp; graphs | Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges. | The first line of input contains two integers n and m (1 β€ n β€ 19, 0 β€ m) β respectively the number of vertices and edges of the graph. Each of the subsequent m lines contains two integers a and b, (1 β€ a, b β€ n, a β b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge... | Output the number of cycles in the given graph. | The example graph is a clique and contains four cycles of length 3 and three cycles of length 4. | Input: 4 61 21 31 42 32 43 4 | Output: 7 | Hard | 3 | 125 | 353 | 47 | 0 |
14 | C | 14C | C. Four Segments | 1,700 | brute force; constructive algorithms; geometry; implementation; math | Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the progr... | The input data contain four lines. Each of these lines contains four integers x1, y1, x2, y2 ( - 109 β€ x1, y1, x2, y2 β€ 109) β coordinates of segment's beginning and end positions. The given segments can degenerate into points. | Output the word Β«YESΒ», if the given four segments form the required rectangle, otherwise output Β«NOΒ». | Input: 1 1 6 11 0 6 06 0 6 11 1 1 0 | Output: YES | Medium | 5 | 532 | 227 | 101 | 0 | |
893 | E | 893E | E. Counting Arrays | 2,000 | combinatorics; dp; math; number theory | You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: There are y elements in F, and all of them are integer numbers; . You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are conside... | The first line contains one integer q (1 β€ q β€ 105) β the number of testcases to solve.Then q lines follow, each containing two integers xi and yi (1 β€ xi, yi β€ 106). Each of these lines represents a testcase. | Print q integers. i-th integer has to be equal to the number of yi-factorizations of xi modulo 109 + 7. | In the second testcase of the example there are six y-factorizations: { - 4, - 1}; { - 2, - 2}; { - 1, - 4}; {1, 4}; {2, 2}; {4, 1}. | Input: 26 34 2 | Output: 366 | Hard | 4 | 463 | 209 | 103 | 8 |
1,811 | D | 1811D | D. Umka and a Long Flight | 1,600 | constructive algorithms; implementation; math | The girl Umka loves to travel and participate in math olympiads. One day she was flying by plane to the next olympiad and out of boredom explored a huge checkered sheet of paper.Denote the \(n\)-th Fibonacci number as \(F_n = \begin{cases} 1, & n = 0; \\ 1, & n = 1; \\ F_{n-2} + F_{n-1}, & n \ge 2. \end{cases}\)A check... | The first line contains an integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β number of test cases.For each test case the integers \(n\), \(x\), \(y\) are given (\(1 \le n \le 44\), \(1 \le x \le F_n\), \(1 \le y \le F_{n+1}\)) β the order of the Fibonacci rectangle and the coordinates of the colored cell. | For each test case, print ""YES"" if the answer is positive, and ""NO"" otherwise.You can print ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive answer). | The first, third and fourth test cases. | Input: 121 1 12 1 23 1 43 3 24 4 64 3 35 6 55 4 125 2 124 2 11 1 244 758465880 1277583853 | Output: YES NO YES YES YES NO YES NO NO YES YES NO | Medium | 3 | 872 | 303 | 223 | 18 |
1,076 | A | 1076A | A. Minimizing the String | 1,200 | greedy; strings | You are given a string \(s\) consisting of \(n\) lowercase Latin letters.You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.String \(s = s_1 s_2 \dots s_n\) is ... | The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of \(s\).The second line of the input contains exactly \(n\) lowercase Latin letters β the string \(s\). | Print one string β the smallest possible lexicographically string that can be obtained by removing at most one character from the string \(s\). | In the first example you can remove any character of \(s\) to obtain the string ""aa"".In the second example ""abca"" < ""abcd"" < ""abcda"" < ""abda"" < ""acda"" < ""bcda"". | Input: 3 aaa | Output: aa | Easy | 2 | 688 | 203 | 143 | 10 |
1,183 | A | 1183A | A. Nearest Interesting Number | 800 | implementation | Polycarp knows that if the sum of the digits of a number is divisible by \(3\), then the number itself is divisible by \(3\). He assumes that the numbers, the sum of the digits of which is divisible by \(4\), are also somewhat interesting. Thus, he considers a positive integer \(n\) interesting if its sum of digits is ... | The only line in the input contains an integer \(a\) (\(1 \le a \le 1000\)). | Print the nearest greater or equal interesting number for the given number \(a\). In other words, print the interesting number \(n\) such that \(n \ge a\) and \(n\) is minimal. | Input: 432 | Output: 435 | Beginner | 1 | 519 | 76 | 176 | 11 | |
1,216 | E2 | 1216E2 | E2. Numerical Sequence (hard version) | 2,200 | binary search; math | The only difference between the easy and the hard versions is the maximum value of \(k\).You are given an infinite sequence of form ""112123123412345\(\dots\)"" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from \(1\) to \(1\), the second... | The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β the number of queries.The \(i\)-th of the following \(q\) lines contains one integer \(k_i\) \((1 \le k_i \le 10^{18})\) β the description of the corresponding query. | Print \(q\) lines. In the \(i\)-th line print one digit \(x_i\) \((0 \le x_i \le 9)\) β the answer to the query \(i\), i.e. \(x_i\) should be equal to the element at the position \(k_i\) of the sequence. | Answers on queries from the first example are described in the problem statement. | Input: 5 1 3 20 38 56 | Output: 1 2 5 2 0 | Hard | 2 | 1,020 | 245 | 203 | 12 |
525 | A | 525A | A. Vitaliy and Pie | 1,100 | greedy; hashing; strings | After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third ro... | The first line of the input contains a positive integer n (2 β€ n β€ 105) β the number of rooms in the house.The second line of the input contains string s of length 2Β·n - 2. Let's number the elements of the string from left to right, starting from one. The odd positions in the given string s contain lowercase Latin lett... | Print the only integer β the minimum number of keys that Vitaly needs to buy to surely get from room one to room n. | Input: 3aAbB | Output: 0 | Easy | 3 | 1,732 | 783 | 115 | 5 | |
1,428 | G1 | 1428G1 | G1. Lucky Numbers (Easy Version) | 2,900 | dp; greedy | This is the easy version of the problem. The only difference is that in this version \(q=1\). You can make hacks only if all versions of the problem are solved.Zookeeper has been teaching his \(q\) sheep how to write and how to add. The \(i\)-th sheep has to write exactly \(k\) non-negative integers with the sum \(n_i\... | The first line contains a single integer \(k\) (\(1 \leq k \leq 999999\)): the number of numbers each sheep has to write. The next line contains six integers \(F_0\), \(F_1\), \(F_2\), \(F_3\), \(F_4\), \(F_5\) (\(1 \leq F_i \leq 10^9\)): the fortune assigned to each digit. The next line contains a single integer \(q\)... | Print \(q\) lines, where the \(i\)-th line contains the maximum sum of fortune of all numbers of the \(i\)-th sheep. In this version, you should print only one line. | In the first test case, \(57 = 9 + 9 + 39\). The three \(9\)'s contribute \(1 \cdot 3\) and \(3\) at the tens position contributes \(2 \cdot 1\). Hence the sum of fortune is \(11\).In the second test case, \(63 = 35 + 19 + 9\). The sum of fortune is \(8\). | Input: 3 1 2 3 4 5 6 1 57 | Output: 11 | Master | 2 | 865 | 540 | 165 | 14 |
2,101 | E | 2101E | E. Kia Bakes a Cake | 3,100 | data structures; dp; greedy; trees | You are given a binary string \(s\) of length \(n\) and a tree \(T\) with \(n\) vertices. Let \(k\) be the number of 1s in \(s\). We will construct a complete undirected weighted graph with \(k\) vertices as follows: For each \(1\le i\le n\) with \(s_i = \mathtt{1}\), create a vertex labeled \(i\). For any two vertices... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\le n\le 7\cdot10^4\)) β the length of the binary string \(s\) and the number of vertices... | For each test case, output \(n\) integers, the \(i\)-th integer representing the maximum number of vertices in any nice simple path starting from the vertex labeled \(i\). If there is no vertex labeled \(i\), i.e., \(s_i = \mathtt{0}\), output \(-1\) instead. | In the first test case, the tree \(T\) and the constructed graph are as follows: Left side is the tree \(T\) with selected nodes colored yellow. The right side is the constructed complete graph. The nice path shown in the diagram is \(3\rightarrow 4\rightarrow 2\). The path is nice as \(w(4, 2) = 2\) is at least twice ... | Input: 35011111 22 33 44 517011010111101011011 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 1616 172011 2 | Output: -1 3 3 3 3 -1 5 4 -1 4 -1 5 5 5 5 -1 4 -1 5 5 -1 3 -1 1 | Master | 4 | 1,644 | 859 | 259 | 21 |
1,929 | B | 1929B | B. Sasha and the Drawing | 800 | constructive algorithms; greedy; math | Even in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention.As a drawing, he decided to draw a square grid of size \(n \times n\), in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time,... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 10^8\), \(1 \leq k \leq 4n - 2\)) β the size of th... | For each test case, output a single integer β the minimum number of cells that need to be colored. | In the pictures below, the colored cells are marked in black, and all diagonals are marked in purple.In the first test case, you can color \(2\) cells so that \(4\) diagonals contain at least one colored cell: In the third test case, you can color \(6\) cells so that all \(10\) diagonals contain at least one colored ce... | Input: 73 43 33 103 94 77 112 3 | Output: 2 2 6 5 4 6 2 | Beginner | 3 | 591 | 421 | 98 | 19 |
1,292 | F | 1292F | F. Nora's Toy Boxes | 3,500 | bitmasks; combinatorics; dp | SIHanatsuka - EMber SIHanatsuka - ATONEMENTBack in time, the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.One day, Nora's adoptive father, Phoenix Wyle, brought Nora \(n\) boxes of toys. Before unpacking, Nora decided to make a fun game for RO... | The first line contains an integer \(n\) (\(3 \le n \le 60\)), denoting the number of boxes.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 60\)), where \(a_i\) is the label of the \(i\)-th box. | Print the number of distinct piles having the maximum number of boxes that ROBO_Head can have, modulo \(10^9 + 7\). | Let's illustrate the box pile as a sequence \(b\), with the pile's bottommost box being at the leftmost position.In the first example, there are \(2\) distinct piles possible: \(b = [6]\) (\([2, \mathbf{6}, 8] \xrightarrow{(1, 3, 2)} [2, 8]\)) \(b = [8]\) (\([2, 6, \mathbf{8}] \xrightarrow{(1, 2, 3)} [2, 6]\)) In the s... | Input: 3 2 6 8 | Output: 2 | Master | 3 | 1,449 | 238 | 115 | 12 |
651 | A | 651A | A. Joysticks | 1,100 | dp; greedy; implementation; math | Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not c... | The first line of the input contains two positive integers a1 and a2 (1 β€ a1, a2 β€ 100), the initial charge level of first and second joystick respectively. | Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged. | In the first sample game lasts for 6 minute by using the following algorithm: at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%; continue the game without changing charger, by the end of the second minute the first joystick is ... | Input: 3 5 | Output: 6 | Easy | 4 | 895 | 156 | 128 | 6 |
1,766 | F | 1766F | F. MCF | 2,800 | flows | You are given a graph consisting of \(n\) vertices and \(m\) directed arcs. The \(i\)-th arc goes from the vertex \(x_i\) to the vertex \(y_i\), has capacity \(c_i\) and weight \(w_i\). No arc goes into the vertex \(1\), and no arc goes from the vertex \(n\). There are no cycles of negative weight in the graph (it is i... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 100\); \(1 \le m \le 200\)).Then \(m\) lines follow. The \(i\)-th of them contains four integers \(x_i\), \(y_i\), \(c_i\), and \(w_i\) (\(1 \le x_i \le n - 1\); \(2 \le y_i \le n\); \(x_i \ne y_i\); \(1 \le c_i \le 100\); \(-100 \le w_i \le 100\)). Th... | If a flow satisfying all of the constraints does not exist, print Impossible.Otherwise, print two lines: the first line should contain one word Possible; the second line should contain \(m\) integers \(f_1, f_2, \dots, f_m\). If there are multiple answers, print any of them. Note that the cost of the flow should be min... | Input: 3 3 1 2 3 -10 1 2 3 -15 2 3 2 0 | Output: Possible 1 1 2 | Master | 1 | 1,063 | 438 | 327 | 17 | |
677 | E | 677E | E. Vanya and Balloons | 2,300 | binary search; brute force; dp; implementation | Vanya plays a game of balloons on the field of size n Γ n, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example:**o****o**o... | The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of rows and columns in the table with balloons.The each of the following n lines contains n characters '0', '1', '2' or '3' β the description of the values in balloons. | Print the maximum possible product modulo 109 + 7. Note, that you are not asked to maximize the remainder modulo 109 + 7, but to find the maximum value and print it this modulo. | In the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3, 3) and radius 1: 2Β·2Β·3Β·3Β·3 = 108. | Input: 41233021320200303 | Output: 108 | Expert | 4 | 896 | 252 | 177 | 6 |
1,327 | B | 1327B | B. Princesses and Princes | 1,200 | brute force; graphs; greedy | The King of Berland Polycarp LXXXIV has \(n\) daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are \(n\) other kingdoms as well.So Polycarp LXXXIV has enumerated his daughters from \(1\) to \(n\) and the kingdom... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of daughters and the number of kingdoms.Each of the next \(n\) lines contains the description... | For each test case print the answer to it.Print ""IMPROVE"" in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter's list so that the total number of married couples increases. The second line then should contain two integers β the index of the daughter and the index of the kingdom Polycarp L... | The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.In the second test case any new entry will increase the number of marriages from \(0\) to \(1\).In the third and the fourth test cases there is no way to add an... | Input: 5 4 2 2 3 2 1 2 2 3 4 1 3 2 0 0 3 3 1 2 3 3 1 2 3 3 1 2 3 1 1 1 4 1 1 1 2 1 3 1 4 | Output: IMPROVE 4 4 IMPROVE 1 1 OPTIMAL OPTIMAL OPTIMAL | Easy | 3 | 2,199 | 859 | 541 | 13 |
1,770 | B | 1770B | B. Koxia and Permutation | 1,000 | constructive algorithms | Reve has two integers \(n\) and \(k\).Let \(p\) be a permutation\(^\dagger\) of length \(n\). Let \(c\) be an array of length \(n - k + 1\) such that $$$\(c_i = \max(p_i, \dots, p_{i+k-1}) + \min(p_i, \dots, p_{i+k-1}).\)\( Let the cost of the permutation \)p\( be the maximum element of \)c\(.Koxia wants you to constru... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)).It is guaranteed that the su... | For each test case, output \(n\) integers \(p_1,p_2,\dots,p_n\), which is a permutation with minimal cost. If there is more than one permutation with minimal cost, you may output any of them. | In the first test case, \(c_1 = \max(p_1,p_2,p_3) + \min(p_1,p_2,p_3) = 5 + 1 = 6\). \(c_2 = \max(p_2,p_3,p_4) + \min(p_2,p_3,p_4) = 3 + 1 = 4\). \(c_3 = \max(p_3,p_4,p_5) + \min(p_3,p_4,p_5) = 4 + 2 = 6\). Therefore, the cost is \(\max(6,4,6)=6\). It can be proven that this is the minimal cost. | Input: 35 35 16 6 | Output: 5 1 2 3 4 1 2 3 4 5 3 2 4 1 6 5 | Beginner | 1 | 706 | 384 | 191 | 17 |
1,144 | B | 1144B | B. Parity Alternated Deletions | 900 | greedy; implementation; sortings | Polycarp has an array \(a\) consisting of \(n\) integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains \(n-1\) elements). For each of the next moves he chooses any element with the only restri... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2000\)) β the number of elements of \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^6\)), where \(a_i\) is the \(i\)-th element of \(a\). | Print one integer β the minimum possible sum of non-deleted elements of the array after end of the game. | Input: 5 1 5 7 8 2 | Output: 0 | Beginner | 3 | 1,128 | 263 | 104 | 11 | |
2,030 | C | 2030C | C. A TRUE Battle | 1,100 | brute force; games; greedy | Alice and Bob are playing a game. There is a list of \(n\) booleans, each of which is either true or false, given as a binary string \(^{\text{β}}\) of length \(n\) (where \(\texttt{1}\) represents true, and \(\texttt{0}\) represents false). Initially, there are no operators between the booleans.Alice and Bob will take... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of the string.The second line contains a binary string of length \(n\), consisting of characters \(\texttt{0}\) and \(\texttt{1}\) β ... | For each testcase, output ""YES"" (without quotes) if Alice wins, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response). | In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true.In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right ... | Input: 5211301012101111111100100111111011801000010 | Output: YES NO YES YES NO | Easy | 3 | 1,367 | 439 | 244 | 20 |
478 | E | 478E | E. Wavy numbers | 2,900 | brute force; dfs and similar; meet-in-the-middle; sortings | A wavy number is such positive integer that for any digit of its decimal representation except for the first one and the last one following condition holds: the digit is either strictly larger than both its adjacent digits or strictly less than both its adjacent digits. For example, numbers 35270, 102, 747, 20 and 3 ar... | The only line of input contains two integers n and k, separated by a single space (1 β€ n, k β€ 1014). | Your task is to output the only integer r β the answer to the given problem. If such number does not exist or it is larger than 1014, then print ""-1"" (minus one without the quotes) instead. | The values of the first four wavy numbers that are divisible by n for the first sample are: 492, 615, 738 and 1845. | Input: 123 4 | Output: 1845 | Master | 4 | 561 | 100 | 191 | 4 |
1,234 | B2 | 1234B2 | B2. Social Network (hard version) | 1,300 | data structures; implementation | The only difference between easy and hard versions are constraints on \(n\) and \(k\).You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most \(k\) most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversation... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n, k \le 2 \cdot 10^5)\) β the number of messages and the number of conversations your smartphone can show.The second line of the input contains \(n\) integers \(id_1, id_2, \dots, id_n\) (\(1 \le id_i \le 10^9\)), where \(id_i\) is the ID of th... | In the first line of the output print one integer \(m\) (\(1 \le m \le min(n, k)\)) β the number of conversations shown after receiving all \(n\) messages.In the second line print \(m\) integers \(ids_1, ids_2, \dots, ids_m\), where \(ids_i\) should be equal to the ID of the friend corresponding to the conversation dis... | In the first example the list of conversations will change in the following way (in order from the first to last message): \([]\); \([1]\); \([2, 1]\); \([3, 2]\); \([3, 2]\); \([1, 3]\); \([1, 3]\); \([2, 1]\). In the second example the list of conversations will change in the following way: \([]\); \([2]\); \([3, 2]\... | Input: 7 2 1 2 3 2 1 3 2 | Output: 2 2 1 | Easy | 2 | 1,679 | 366 | 384 | 12 |
1,693 | B | 1693B | B. Fake Plastic Trees | 1,700 | dfs and similar; dp; greedy; trees | We are given a rooted tree consisting of \(n\) vertices numbered from \(1\) to \(n\). The root of the tree is the vertex \(1\) and the parent of the vertex \(v\) is \(p_v\).There is a number written on each vertex, initially all numbers are equal to \(0\). Let's denote the number written on the vertex \(v\) as \(a_v\).... | The first line contains an 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 2 \cdot 10^5)\) β the number of the vertices in the tree.The second line of each test case contains \(n - 1\) ... | For each test case output the minimum number of operations needed. | In the first test case, we can achieve the goal with a single operation: choose \(v = 2\) and \(c = [1, 2]\), resulting in \(a_1 = 1, a_2 = 2\).In the second test case, we can achieve the goal with two operations: first, choose \(v = 2\) and \(c = [3, 3]\), resulting in \(a_1 = 3, a_2 = 3, a_3 = 0\). Then, choose \(v =... | Input: 4211 52 931 14 52 46 1041 2 16 95 64 52 451 2 3 45 54 43 32 21 1 | Output: 1 2 2 5 | Medium | 4 | 900 | 639 | 66 | 16 |
1,081 | A | 1081A | A. Definite Game | 800 | constructive algorithms; math | Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games.He came up with the following game. The player has a positive integer \(n\). Initially the value of \(n\) equals to \(v\) and the player is able to do the followin... | The input contains only one integer in the first line: \(v\) (\(1 \le v \le 10^9\)), the initial value of \(n\). | Output a single integer, the minimum value of \(n\) the player can get. | In the first example, the player can choose \(x=3\) in the first turn, then \(n\) becomes \(5\). He can then choose \(x=4\) in the second turn to get \(n=1\) as the result. There are other ways to get this minimum. However, for example, he cannot choose \(x=2\) in the first turn because \(2\) is a divisor of \(8\).In t... | Input: 8 | Output: 1 | Beginner | 2 | 628 | 112 | 71 | 10 |
1,956 | D | 1956D | D. Nene and the Mex Operator | 2,000 | bitmasks; brute force; constructive algorithms; divide and conquer; dp; greedy; implementation; math | Nene gave you an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\).You can perform the following operation no more than \(5\cdot 10^5\) times (possibly zero): Choose two integers \(l\) and \(r\) such that \(1 \le l \le r \le n\), compute \(x\) as \(\operatorname{MEX}(\{a_l, a_{l+1}, \ldots, a_r\})\), and simu... | The first line contains an integer \(n\) (\(1 \le n \le 18\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0\leq a_i \leq 10^7\)) β the array \(a\). | In the first line, output two integers \(s\) and \(m\) (\(0\le m\le 5\cdot 10^5\)) β the maximum sum of elements of the array \(a\) and the number of operations in your solution.In the \(i\)-th of the following \(m\) lines, output two integers \(l\) and \(r\) (\(1 \le l \le r \le n\)), representing the parameters of th... | In the first example, after the operation with \(l=1\) and \(r=2\) the array \(a\) becomes equal to \([2,2]\). It can be shown that it is impossible to achieve a larger sum of the elements of \(a\), so the answer is \(4\).In the second example, the initial sum of elements is \(13\) which can be shown to be the largest.... | Input: 20 1 | Output: 4 1 1 2 | Hard | 8 | 857 | 201 | 476 | 19 |
952 | E | 952E | E. Cheese Board | 2,000 | Not to be confused with chessboard. | The first line of input contains a single integer N (1 β€ N β€ 100) β the number of cheeses you have.The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The ... | Output a single number. | Input: 9brie softcamembert softfeta softgoat softmuenster softasiago hardcheddar hardgouda hardswiss hard | Output: 3 | Hard | 0 | 35 | 385 | 23 | 9 | ||
1,478 | C | 1478C | C. Nezzar and Symmetric Array | 1,700 | implementation; math; sortings | Long time ago there was a symmetric array \(a_1,a_2,\ldots,a_{2n}\) consisting of \(2n\) distinct integers. Array \(a_1,a_2,\ldots,a_{2n}\) is called symmetric if for each integer \(1 \le i \le 2n\), there exists an integer \(1 \le j \le 2n\) such that \(a_i = -a_j\).For each integer \(1 \le i \le 2n\), Nezzar wrote do... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(2n\) integers \(d_1, d_2, \ldots, d_{2n}\) (\(0 \le d_i \le 10^{12}\)).It is guaranteed t... | For each test case, print ""YES"" in a single line if there exists a possible array \(a\). Otherwise, print ""NO"".You can print letters in any case (upper or lower). | In the first test case, \(a=[1,-3,-1,3]\) is one possible symmetric array that generates the array \(d=[8,12,8,12]\).In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array \(d\). | Input: 6 2 8 12 8 12 2 7 7 9 11 2 7 11 7 11 1 1 1 4 40 56 48 40 80 56 80 48 6 240 154 210 162 174 154 186 240 174 186 162 210 | Output: YES NO NO NO NO YES | Medium | 3 | 701 | 386 | 166 | 14 |
32 | A | 32A | A. Reconnaissance | 800 | brute force | According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights are a1, a2, ..., an centimeters. Some soldiers are ... | The first line contains two integers n and d (1 β€ n β€ 1000, 1 β€ d β€ 109) β amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers β heights of all the soldiers in Bob's detachment. These numbers don't exceed 109. | Output one number β amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d. | Input: 5 1010 20 50 60 65 | Output: 6 | Beginner | 1 | 500 | 304 | 123 | 0 | |
1,832 | D2 | 1832D2 | D2. Red-Blue Operations (Hard Version) | 2,400 | binary search; constructive algorithms; greedy; implementation; math | The only difference between easy and hard versions is the maximum values of \(n\) and \(q\).You are given an array, consisting of \(n\) integers. Initially, all elements are red.You can apply the following operation to the array multiple times. During the \(i\)-th operation, you select an element of the array; then: if... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the number of elements in the array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).The third line contains \(q\) integers \(k_1, k_2, \dots, k_q\) (\(1 \le k_j \l... | For each query, print a single integer β the largest minimum that the array can have after you apply exactly \(k\) operations to it. | Input: 4 10 5 2 8 4 1 2 3 4 5 6 7 8 9 10 | Output: 3 4 5 6 7 8 8 10 8 12 | Expert | 5 | 839 | 330 | 132 | 18 | |
1,633 | B | 1633B | B. Minority | 800 | greedy | You are given a string \(s\), consisting only of characters '0' and '1'.You have to choose a contiguous substring of \(s\) and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The only line of each testcase contains a non-empty string \(s\), consisting only of characters '0' and '1'. The length of \(s\) doesn't exceed \(2 \cdot 10^5\).The total length of strings \(s\) over all testcases doesn't ex... | For each testcase, print a single integer β the maximum amount of characters that can be removed after applying the operation exactly once. | In the first testcase, you can choose substrings ""0"", ""1"" or ""01"". In ""0"" the amount of '0' is \(1\), the amount of '1' is \(0\). '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were \(0\) of them, nothing changes. Same for ""1"". And in ""01"" neither o... | Input: 4011010101010111001100010001 | Output: 0 5 3 0 | Beginner | 1 | 609 | 342 | 139 | 16 |
1,856 | C | 1856C | C. To Become Max | 1,600 | binary search; brute force; data structures; dp | You are given an array of integers \(a\) of length \(n\).In one operation you: Choose an index \(i\) such that \(1 \le i \le n - 1\) and \(a_i \le a_{i + 1}\). Increase \(a_i\) by \(1\). Find the maximum possible value of \(\max(a_1, a_2, \ldots a_n)\) that you can get after performing this operation at most \(k\) time... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 1000\), \(1 \le k \le 10^{8}\)) β the length o... | For each test case output a single integer β the maximum possible maximum of the array after performing at most \(k\) operations. | In the first test case, one possible optimal sequence of operations is: \([\color{red}{1}, 3, 3] \rightarrow [2, \color{red}{3}, 3] \rightarrow [\color{red}{2}, 4, 3] \rightarrow [\color{red}{3}, 4, 3] \rightarrow [4, 4, 3]\).In the second test case, one possible optimal sequence of operations is: \([1, \color{red}{3},... | Input: 63 41 3 35 61 3 4 5 14 131 1 3 1795 34 3 2 2 25 66 5 4 1 52 173 5 | Output: 4 7 179 5 7 6 | Medium | 4 | 322 | 623 | 129 | 18 |
625 | B | 625B | B. War of the Corporations | 1,200 | constructive algorithms; greedy; strings | A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineap... | The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters. | Print the minimum number of characters that must be replaced with ""#"" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring. | In the first sample AI's name may be replaced with ""int#llect"".In the second sample Gogol can just keep things as they are.In the third sample one of the new possible names of AI may be ""s#ris#ri"". | Input: intellecttell | Output: 1 | Easy | 3 | 1,137 | 295 | 165 | 6 |
329 | D | 329D | D. The Evil Temple and the Moving Rocks | 2,500 | constructive algorithms | Important: All possible tests are in the pretest, so you shouldn't hack on this problem. So, if you passed pretests, you will also pass the system test.You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak monsters, you arrived at a square room consisting of tiles forming an... | The first line will consists of two integers n and x, denoting the size of the room and the number of sounds required to open the door. There will be exactly three test cases for this problem: n = 5, x = 5; n = 3, x = 2; n = 100, x = 105. All of these testcases are in pretest. | Output n lines. Each line consists of n characters β the j-th character of the i-th line represents the content of the tile at the i-th row and the j-th column, and should be one of these: '^', '<', '>', or 'v': a rock as described in the problem statement. '.': an empty tile. Then, output two integers r and c (1 β€ r, ... | Here's a simulation of the first example, accompanied with the number of sounds produced so far. 0 sound 1 sound 2 sounds 3 sounds 4 sounds still 4 sounds In the picture above, the activated rock switches between the '^' rock and the '<' rock. However, no sound is produced since the '^' rock didn't move even a single t... | Input: 5 5 | Output: >...vv.<....^..>......^.<1 1 | Expert | 1 | 2,322 | 277 | 553 | 3 |
579 | A | 579A | A. Raising Bacteria | 1,000 | bitmasks | You are a lover of bacteria. You want to raise some bacteria in a box. Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment. What is the minimum nu... | The only line containing one integer x (1 β€ x β€ 109). | The only line containing one integer: the answer. | For the first sample, we can add one bacterium in the box in the first day morning and at the third morning there will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so the answer is 2.For the second sample, we can put one in the first morning and in the 4-th mo... | Input: 5 | Output: 2 | Beginner | 1 | 384 | 53 | 49 | 5 |
2,034 | F1 | 2034F1 | F1. Khayyam's Royal Decree (Easy Version) | 2,500 | combinatorics; dp; math; sortings | This is the easy version of the problem. The only differences between the two versions are the constraints on \(k\) and the sum of \(k\).In ancient Persia, Khayyam, a clever merchant and mathematician, is playing a game with his prized treasure chest containing \(n\) red rubies worth \(2\) dinars each and \(m\) blue sa... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m \leq 2 \cdot 10^5\), \(0 \leq k \leq 500\)) β the number of red... | For each test case, print a single integer: the expected value of Khayyam's satchel at the end of the process, modulo \(998,244,353\). | In the first test case, at the end of the process, there will always be \(3\) red rubies and \(4\) blue sapphires. None of the special conditions described in the scrolls are met, so the value of Khayyam's satchel remains unchanged. The total value of the satchel at the end is always \(2 \cdot 3 + 1 \cdot 4 = 10\).In t... | Input: 53 4 01 1 11 03 3 21 12 23 3 22 11 210 4 51 08 06 40 27 4 | Output: 10 499122180 798595498 149736666 414854846 | Expert | 4 | 1,538 | 815 | 134 | 20 |
1,583 | G | 1583G | G. Omkar and Time Travel | 3,000 | data structures; math | El Psy Kongroo.Omkar is watching Steins;Gate.In Steins;Gate, Okabe Rintarou needs to complete \(n\) tasks (\(1 \leq n \leq 2 \cdot 10^5\)). Unfortunately, he doesn't know when he needs to complete the tasks.Initially, the time is \(0\). Time travel will now happen according to the following rules:For each \(k = 1, 2, \... | The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of tasks that Okabe needs to complete.\(n\) lines follow. The \(k\)-th of these lines contain two integers \(a_k\) and \(b_k\) (\(1 \leq a_k < b_k \leq 2n\)) β the time at which Okabe needs to complete the \(k\)-th task and the time ... | Output a single integer β the number of times that Okabe time travels until all tasks in the set \(s\) are simultaneously completed, modulo \(10^9 + 7\). | For the first sample, all tasks need to be completed in order for the funny scene to occur.Initially, the time is \(0\). Nothing happens until time \(3\), when Okabe realizes that he should have done the \(2\)-nd task at time \(2\). He then time travels to time \(2\) and completes the task.As the task is done now, he d... | Input: 2 1 4 2 3 2 1 2 | Output: 3 | Master | 2 | 1,772 | 777 | 153 | 15 |
1,528 | D | 1528D | D. It's a bird! No, it's a plane! No, it's AaParsa! | 2,500 | constructive algorithms; graphs; shortest paths | There are \(n\) cities in Shaazzzland, numbered from \(0\) to \(n-1\). Ghaazzzland, the immortal enemy of Shaazzzland, is ruled by AaParsa.As the head of the Ghaazzzland's intelligence agency, AaParsa is carrying out the most important spying mission in Ghaazzzland's history on Shaazzzland.AaParsa has planted \(m\) tra... | The first line contains two integers \(n\) and \(m\) \((2 \le n \le 600 , n \le m \le n^2)\) β the number of cities and cannons correspondingly.The \(i\)-th line of the following \(m\) lines contains three integers \(a_i\), \(b_i\) and \(c_i\) \(( 0 \le a_i , b_i \le n-1 , 1 \le c_i \le 10^9)\), denoting the cannon in ... | Print \(n\) lines, each line should contain \(n\) integers.The \(j\)-th integer in the \(i\)-th line should be equal to the minimum time required to reach city \(j\) from city \(i\). | In the first example one possible path for going from \(0\) to \(2\) would be: Stay inside \(0\) and do nothing for \(1\) second. Use the first cannon and land at \(2\) after \(1\) second. Note that: we could have used the second cannon in \(0\)-th second but it would have taken us \(3\) seconds to reach city \(2\) in ... | Input: 3 4 0 1 1 0 2 3 1 0 1 2 0 1 | Output: 0 1 2 1 0 2 1 2 0 | Expert | 3 | 2,033 | 655 | 182 | 15 |
510 | C | 510C | C. Fox And Names | 1,600 | dfs and similar; graphs; sortings | Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: ""Fox""). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in l... | The first line contains an integer n (1 β€ n β€ 100): number of names.Each of the following n lines contain one string namei (1 β€ |namei| β€ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different. | If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'β'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).Otherwise output a single word ""Impossible"" (without quotes). | Input: 3rivestshamiradleman | Output: bcdefghijklmnopqrsatuvwxyz | Medium | 3 | 1,029 | 232 | 304 | 5 | |
383 | A | 383A | A. Milking cows | 1,600 | data structures; greedy | Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of mi... | The first line contains an integer n (1 β€ n β€ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right. | Print a single integer, the minimum amount of lost milk.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 first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost. | Input: 40 0 1 0 | Output: 1 | Medium | 2 | 943 | 186 | 205 | 3 |
1,139 | D | 1139D | D. Steps to One | 2,300 | dp; math; number theory; probabilities | Vivek initially has an empty array \(a\) and some integer constant \(m\).He performs the following algorithm: Select a random integer \(x\) uniformly in range from \(1\) to \(m\) and append it to the end of \(a\). Compute the greatest common divisor of integers in \(a\). In case it equals to \(1\), break Otherwise, ret... | The first and only line contains a single integer \(m\) (\(1 \leq m \leq 100000\)). | Print a single integer β the expected length of the array \(a\) written as \(P \cdot Q^{-1} \pmod{10^9+7}\). | In the first example, since Vivek can choose only integers from \(1\) to \(1\), he will have \(a=[1]\) after the first append operation, and after that quit the algorithm. Hence the length of \(a\) is always \(1\), so its expected value is \(1\) as well.In the second example, Vivek each time will append either \(1\) or... | Input: 1 | Output: 1 | Expert | 4 | 562 | 83 | 108 | 11 |
58 | C | 58C | C. Trees | 1,800 | brute force | On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 β€ i β€ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should... | The first line contains integer n (1 β€ n β€ 105) which is the number of trees. The second line contains integers ai (1 β€ ai β€ 105) which are the heights of the trees. | Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful. | Input: 32 2 2 | Output: 1 | Medium | 1 | 1,365 | 165 | 134 | 0 | |
1,450 | C1 | 1450C1 | C1. Errich-Tac-Toe (Easy Version) | 2,100 | constructive algorithms; math | The only difference between the easy and hard versions is that tokens of type O do not appear in the input of the easy version.Errichto gave Monogon the following challenge in order to intimidate him from taking his top contributor spot on Codeforces.In a Tic-Tac-Toe grid, there are \(n\) rows and \(n\) columns. Each c... | The first line contains a single integer \(t\) (\(1\le t\le 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 300\)) β the size of the grid.The following \(n\) lines each contain a string of \(n\) characters, denoting the initial grid. The character in the \... | For each test case, print the state of the grid after applying the operations.We have proof that a solution always exists. If there are multiple solutions, print any. | In the first test case, there are initially three 'X' consecutive in the second row and the second column. By changing the middle token to 'O' we make the grid a draw, and we only changed \(1\le \lfloor 5/3\rfloor\) token.In the second test case, we change only \(9\le \lfloor 32/3\rfloor\) tokens, and there does not ex... | Input: 3 3 .X. XXX .X. 6 XX.XXX XXXXXX XXX.XX XXXXXX XX.X.X XXXXXX 5 XXX.X .X..X XXX.X ..X.. ..X.. | Output: .X. XOX .X. XX.XXO XOXXOX OXX.XX XOOXXO XX.X.X OXXOXX XOX.X .X..X XXO.O ..X.. ..X.. | Hard | 2 | 958 | 609 | 166 | 14 |
920 | G | 920G | G. List Of Integers | 2,200 | binary search; bitmasks; brute force; combinatorics; math; number theory | Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respec... | The first line contains one integer t (1 β€ t β€ 30000) β the number of queries to process.Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 β€ x, p, k β€ 106). | Print t integers, where i-th integer is the answer to i-th query. | Input: 37 22 17 22 27 22 3 | Output: 91315 | Hard | 6 | 466 | 190 | 65 | 9 | |
2,011 | I | 2011I | I. Stack and Queue | 0 | *special; data structures; divide and conquer | There are \(2\) queues of patients at the doors of two doctors. The first doctor sees patients in the usual order of the queue β whoever arrived first will be seen first. The second doctor does the opposite β he sees those who arrived last first. Thus, there is a queue for the first doctor and a stack for the second do... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of requests.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β for each patient, the time their visit to the doctor will take.Each of the following \(n\) lines contains two integers \(t\) and \(x\... | After each query, print ""YES"", if the current configuration is good, and ""NO"" otherwise. | In the first test, there are the following configurations: queue: \([1]\); stack: \([]\); patient \(1\) is seen by the first doctor for \(10\) minutes, starting at minute \(0\). queue: \([1]\); stack: \([1]\); patient \(1\) is seen by the first doctor during \([0; 10)\) and by the second during \([0; 10)\). Since patie... | Input: 310 15 41 12 12 2 | Output: YES NO YES | Beginner | 3 | 1,702 | 678 | 92 | 20 |
604 | A | 604A | A. Uncowed Forces | 1,000 | implementation | Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd a... | The first line of the input contains five space-separated integers m1, m2, m3, m4, m5, where mi (0 β€ mi β€ 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted.The second line contains five space-separated integers w1, w2, w3, w4, w5, where wi (0 β€ wi β€ 10) i... | Print a single integer, the value of Kevin's final score. | In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets of the points on each problem. So his score from solving problems is . Adding in 10Β·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930. | Input: 20 40 60 80 1000 1 2 3 41 0 | Output: 4900 | Beginner | 1 | 1,196 | 532 | 57 | 6 |
567 | D | 567D | D. One-Dimensional Battle Ships | 1,700 | binary search; data structures; greedy; sortings | Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ n table).At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ a rectangle (that is, it occupies a... | The first line of the input contains three integers: n, k and a (1 β€ n, k, a β€ 2Β·105) β the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.The second l... | Print a single integer β the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print ""-1"". | Input: 11 3 354 8 6 1 11 | Output: 3 | Medium | 4 | 824 | 563 | 224 | 5 | |
86 | A | 86A | A. Reflection | 1,600 | math | For each positive integer n consider the integer Ο(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9 - a). We say that Ο(n) is the reflection of n. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals... | Input contains two space-separated integers l and r (1 β€ l β€ r β€ 109) β bounds of the range. | Output should contain single integer number: maximum value of the product nΒ·Ο(n), where l β€ n β€ r.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). | In the third sample weight of 8 equals 8Β·1 = 8, weight of 9 equals 9Β·0 = 0, weight of 10 equals 890.Thus, maximum value of the product is equal to 890. | Input: 3 7 | Output: 20 | Medium | 1 | 598 | 92 | 229 | 0 |
891 | B | 891B | B. Gluttony | 2,000 | constructive algorithms; greedy | You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. | The first line contains one integer n (1 β€ n β€ 22) β the size of the array.The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array. | If there is no such array b, print -1.Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.If there are multiple answers, print any of them. | An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.Note that the empty subset and the subset containing all indices are not counted. | Input: 21 2 | Output: 2 1 | Hard | 2 | 251 | 195 | 203 | 8 |
11 | A | 11A | A. Increasing Sequence | 900 | constructive algorithms; implementation; math | A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? | The first line of the input contains two integer numbers n and d (2 β€ n β€ 2000, 1 β€ d β€ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 β€ bi β€ 106). | Output the minimal number of moves needed to make the sequence increasing. | Input: 4 21 3 3 2 | Output: 3 | Beginner | 3 | 320 | 179 | 74 | 0 | |
1,065 | G | 1065G | G. Fibonacci Suffix | 2,700 | strings | Let's denote (yet again) the sequence of Fibonacci strings:\(F(0) = \) 0, \(F(1) = \) 1, \(F(i) = F(i - 2) + F(i - 1)\), where the plus sign denotes the concatenation of two strings.Let's denote the lexicographically sorted sequence of suffixes of string \(F(i)\) as \(A(F(i))\). For example, \(F(4)\) is 01101, and \(A(... | The only line of the input contains three numbers \(n\), \(k\) and \(m\) (\(1 \le n, m \le 200\), \(1 \le k \le 10^{18}\)) denoting the index of the Fibonacci string you have to consider, the index of the element of \(A(F(n))\) and the number of characters you have to output, respectively.It is guaranteed that \(k\) do... | Output \(m\) first characters of \(k\)-th element of \(A(F(n))\), or the whole element if its length is less than \(m\). | Input: 4 5 3 | Output: 110 | Master | 1 | 596 | 357 | 120 | 10 | |
394 | D | 394D | D. Physical Education and Buns | 0 | brute force; implementation; math | The Physical education teacher at SESC is a sort of mathematician too. His most favorite topic in mathematics is progressions. That is why the teacher wants the students lined up in non-decreasing height form an arithmetic progression.To achieve the goal, the gym teacher ordered a lot of magical buns from the dining ro... | The single line contains integer n (2 β€ n β€ 103) β the number of students. The second line contains n space-separated integers β the heights of all students. The height of one student is an integer which absolute value doesn't exceed 104. | In the first line print the maximum number of buns eaten by some student to achieve the teacher's aim. In the second line, print two space-separated integers β the height of the lowest student in the end and the step of the progression. Please, pay attention that the step should be non-negative.If there are multiple po... | Lets look at the first sample. We can proceed in the following manner: don't feed the 1-st student, his height will stay equal to -3; give two buns of the first type to the 2-nd student, his height become equal to -2; give two buns of the first type to the 3-rd student, his height become equal to 0; give two buns of th... | Input: 5-3 -4 -2 -3 3 | Output: 2-3 1 | Beginner | 3 | 1,024 | 238 | 362 | 3 |
335 | C | 335C | C. More Reclamation | 2,100 | games | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns β each cell represents a rectangular area. The rows are numbered 1 ... | The first line consists of two integers r and n (1 β€ r β€ 100, 0 β€ n β€ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 β€ ri β€ r, 1 β€ ci β€ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be... | Output ""WIN"" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print ""LOSE"". | In the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In th... | Input: 3 11 1 | Output: WIN | Hard | 1 | 1,273 | 394 | 145 | 3 |
135 | C | 135C | C. Zero-One | 1,900 | constructive algorithms; games; greedy | Little Petya very much likes playing with little Masha. Recently he has received a game called ""Zero-One"" as a gift from his mother. Petya immediately offered Masha to play the game with him.Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card c... | The first line contains a sequence of characters each of which can either be a ""0"", a ""1"" or a ""?"". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters ""?"" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 1... | Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the f... | In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will con... | Input: ???? | Output: 00011011 | Hard | 3 | 1,589 | 334 | 333 | 1 |
852 | E | 852E | E. Casinos and travel | 2,100 | dp | John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.John first has to select from which city he will start his journey.... | In the first line, a positive integer N (1 β€ N β€ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 β€ a, b β€ N) separated by a single space meaning that cities a and b are connected by a bidirectional road. | Output one number, the answer to the problem modulo 109 + 7. | Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go ... | Input: 21 2 | Output: 4 | Hard | 1 | 1,329 | 231 | 60 | 8 |
622 | F | 622F | F. The Sum of the k-th Powers | 2,600 | math | There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7). | The only line contains two integers n, k (1 β€ n β€ 109, 0 β€ k β€ 106). | Print the only integer a β the remainder after dividing the value of the sum by the value 109 + 7. | Input: 4 1 | Output: 10 | Expert | 1 | 222 | 68 | 98 | 6 | |
478 | C | 478C | C. Table Decorations | 1,800 | greedy | You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?Your task is to write a program that ... | The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space. | Print a single integer t β the maximum number of tables that can be decorated in the required manner. | In the first sample you can decorate the tables with the following balloon sets: ""rgg"", ""gbb"", ""brr"", ""rrg"", where ""r"", ""g"" and ""b"" represent the red, green and blue balls, respectively. | Input: 5 4 3 | Output: 4 | Medium | 1 | 435 | 178 | 101 | 4 |
1,761 | D | 1761D | D. Carry Bit | 2,100 | combinatorics; math | Let \(f(x,y)\) be the number of carries of \(x+y\) in binary (i. e. \(f(x,y)=g(x)+g(y)-g(x+y)\), where \(g(x)\) is the number of ones in the binary representation of \(x\)).Given two integers \(n\) and \(k\), find the number of ordered pairs \((a,b)\) such that \(0 \leq a,b < 2^n\), and \(f(a,b)\) equals \(k\). Note th... | The only line of each test contains two integers \(n\) and \(k\) (\(0\leq k<n\leq 10^6\)). | Output a single integer β the answer modulo \(10^9+7\). | Here are some examples for understanding carries:$$$\( \begin{aligned} &\begin{array}{r} 1_{\ \ }1_{\ \ }1\\ +\ _{1}1_{\ \ }0_{\ \ }0\\ \hline \ 1_{\ \ }0_{\ \ }1_{\ \ }1 \end{array} &\begin{array}{r} \ 1_{\ \ }0_{\ \ }1\\ +\ _{\ \ }0_{\ \ }0_{1}1\\ \hline \ 0_{\ \ }1_{\ \ }1_{\ \ }0 \end{array} & &\begin{array}{r} \ 1... | Input: 3 1 | Output: 15 | Hard | 2 | 459 | 90 | 55 | 17 |
174 | C | 174C | C. Range Increments | 1,800 | data structures; greedy | Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following: function rangeIncrement(l, r) for i := l .. r do... | The first input line contains a single integer n (1 β€ n β€ 105) β the length of the array a[1... n]. The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 β€ a[i] β€ 105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at least one element of the array is p... | Print on the first line t β the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.Then print t lines β the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 β€... | The first sample requires a call for the entire array, and four additional calls: one for the segment [2,2] (i.e. the second element of the array), three for the segment [5,5] (i.e. the fifth element of the array). | Input: 61 2 1 1 4 1 | Output: 52 25 55 55 51 6 | Medium | 2 | 718 | 427 | 493 | 1 |
1,288 | C | 1288C | C. Two Arrays | 1,600 | combinatorics; dp | You are given two integers \(n\) and \(m\). Calculate the number of pairs of arrays \((a, b)\) such that: the length of both arrays is equal to \(m\); each element of each array is an integer between \(1\) and \(n\) (inclusive); \(a_i \le b_i\) for any index \(i\) from \(1\) to \(m\); array \(a\) is sorted in non-desce... | The only line contains two integers \(n\) and \(m\) (\(1 \le n \le 1000\), \(1 \le m \le 10\)). | Print one integer β the number of arrays \(a\) and \(b\) satisfying the conditions described above modulo \(10^9+7\). | In the first test there are \(5\) suitable arrays: \(a = [1, 1], b = [2, 2]\); \(a = [1, 2], b = [2, 2]\); \(a = [2, 2], b = [2, 2]\); \(a = [1, 1], b = [2, 1]\); \(a = [1, 1], b = [1, 1]\). | Input: 2 2 | Output: 5 | Medium | 2 | 450 | 95 | 117 | 12 |
1,918 | B | 1918B | B. Minimize Inversions | 900 | constructive algorithms; data structures; greedy; implementation; sortings | You are given two permutations \(a\) and \(b\) of length \(n\). A permutation is an array of \(n\) elements from \(1\) to \(n\) where all elements are distinct. For example, an array [\(2,1,3\)] is a permutation, but [\(0,1\)] and [\(1,3,1\)] aren't.You can (as many times as you want) choose two indices \(i\) and \(j\)... | The first line contains an integer \(t\) (\(1 \leq t \leq 20\,000\)) β the number of test cases.Each test case consists of three lines. The first line contains an integer \(n\) (\(1 \leq n \leq 2\cdot10^5\)) β the length of the permutations \(a\) and \(b\). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_... | For each test case, output two permutations \(a'\) and \(b'\) (in the same format as in the input) β the permutations after all operations. The total number of inversions in \(a'\) and \(b'\) should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.If there... | In the first test case, the minimum possible number of inversions is \(10\).In the second test case, we can sort both permutations at the same time. For this, the following operations can be done: Swap the elements in the positions \(1\) and \(3\) in both permutations. After the operation, \(a =\) [\(2,1,3\)], \(b =\) ... | Input: 351 2 3 4 55 4 3 2 133 1 23 1 262 5 6 1 3 41 5 3 6 2 4 | Output: 3 2 5 1 4 3 4 1 5 2 1 2 3 1 2 3 2 3 4 6 5 1 1 2 4 3 5 6 | Beginner | 5 | 732 | 510 | 363 | 19 |
1,863 | G | 1863G | G. Swaps | 2,800 | combinatorics; dp; graphs; math | You are given an array of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)). You can perform the following operation several (possibly, zero) times: pick an arbitrary \(i\) and perform swap\((a_i, a_{a_i})\). How many distinct arrays is it possible to attain? Output the answer modulo \((10^9 + 7)\). | The first line contains an integer \(n\) (\(1 \le n \le 10^6\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1\le a_i\le n\)). | Output the number of attainable arrays modulo \((10^9 + 7)\). | In the first example, the initial array is \([1, 1, 2]\). If we perform the operation with \(i = 3\), we swap \(a_3\) and \(a_2\), obtaining \([1, 2, 1]\). One can show that there are no other attainable arrays.In the second example, the four attainable arrays are \([2, 1, 4, 3]\), \([1, 2, 4, 3]\), \([1, 2, 3, 4]\), \... | Input: 31 1 2 | Output: 2 | Master | 4 | 306 | 149 | 61 | 18 |
1,497 | E1 | 1497E1 | E1. Square-Free Division (easy version) | 1,700 | data structures; dp; greedy; math; number theory; two pointers | This is the easy version of the problem. The only difference is that in this version \(k = 0\).There is an array \(a_1, a_2, \ldots, a_n\) of \(n\) positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose pro... | The first line contains a single integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The first line of each test case contains two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(k = 0\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^7\)).It's... | For each test case print a single integer β the answer to the problem. | In the first test case the division may be as follows: \([18, 6]\) \([2, 4]\) \([1]\) | Input: 35 018 6 2 4 15 06 8 1 24 81 01 | Output: 3 2 1 | Medium | 6 | 655 | 407 | 70 | 14 |
377 | B | 377B | B. Preparing for the Contest | 1,900 | binary search; data structures; greedy; sortings | Soon there will be held the world's largest programming contest, but the testing system still has m bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has n students able to perform such work. The students realize that they are the ... | The first line contains three space-separated integers: n, m and s (1 β€ n, m β€ 105, 0 β€ s β€ 109) β the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.The next line contains m space-separated integers a1, a2, ..., am (1 β€ ai β€ 109) β the... | If the university can't correct all bugs print ""NO"".Otherwise, on the first line print ""YES"", and on the next line print m space-separated integers: the i-th of these numbers should equal the number of the student who corrects the i-th bug in the optimal answer. The bugs should be corrected as quickly as possible (... | Consider the first sample.The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the st... | Input: 3 4 91 3 1 22 1 34 3 6 | Output: YES2 3 2 3 | Hard | 4 | 1,254 | 610 | 475 | 3 |
2,064 | C | 2064C | C. Remove the Ends | 1,300 | brute force; constructive algorithms; dp; greedy | You have an array \(a\) of length \(n\) consisting of non-zero integers. Initially, you have \(0\) coins, and you will do the following until \(a\) is empty: Let \(m\) be the current size of \(a\). Select an integer \(i\) where \(1 \le i \le m\), gain \(|a_i|\)\(^{\text{β}}\) coins, and then: if \(a_i < 0\), then repla... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of \(a\).The second line of each testcase contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(-10^9 \le a_i \le 10^9\), \(a_i \ne ... | For each test case, output the maximum number of coins you can have at the end of the process. | An example of how to get \(23\) coins in the first testcase is as follows: \(a = [3, 1, 4, -1, -5, \color{red}{-9}] \xrightarrow{i = 6} a = [3, 1, 4, -1, -5] \), and get \(9\) coins. \(a = [\color{red}{3}, 1, 4, -1, -5] \xrightarrow{i = 1} a = [1, 4, -1, -5] \), and get \(3\) coins. \(a = [\color{red}{1}, 4, -1, -5] \x... | Input: 363 1 4 -1 -5 -96-10 -3 -17 1 19 2011 | Output: 23 40 1 | Easy | 4 | 744 | 396 | 94 | 20 |
1,106 | A | 1106A | A. Lunar New Year and Cross Counting | 800 | implementation | Lunar New Year is approaching, and you bought a matrix with lots of ""crosses"".This matrix \(M\) of size \(n \times n\) contains only 'X' and '.' (without quotes). The element in the \(i\)-th row and the \(j\)-th column \((i, j)\) is defined as \(M(i, j)\), where \(1 \leq i, j \leq n\). We define a cross appearing in ... | The first line contains only one positive integer \(n\) (\(1 \leq n \leq 500\)), denoting the size of the matrix \(M\).The following \(n\) lines illustrate the matrix \(M\). Each line contains exactly \(n\) characters, each of them is 'X' or '.'. The \(j\)-th element in the \(i\)-th line represents \(M(i, j)\), where \... | Output a single line containing only one integer number \(k\) β the number of crosses in the given matrix \(M\). | In the first sample, a cross appears at \((3, 3)\), so the answer is \(1\).In the second sample, no crosses appear since \(n < 3\), so the answer is \(0\).In the third sample, crosses appear at \((3, 2)\), \((3, 4)\), \((4, 3)\), \((4, 5)\), so the answer is \(4\). | Input: 5 ..... .XXX. .XXX. .XXX. ..... | Output: 1 | Beginner | 1 | 754 | 342 | 112 | 11 |
1,910 | B | 1910B | B. Security Guard | 1,600 | *special; greedy | Monocarp is a security guard in Berland State University. Every day, he tracks how many people and at what time enter and leave the university. He writes this information down as follows: when someone enters the university, Monocarp writes a plus sign '+'; when someone leaves the university, Monocarp writes a minus sig... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains a single string \(s\) (\(1 \le |s| \le 3 \cdot 10^5\)), consisting only of characters '+' and/or '-'. A plus sign '+' represents a person entering the university. A minus sign '-' re... | For each test case, print an answer.If it's impossible to swap at most one pair of characters so that the resulting sequence is plausible, print -1.Otherwise, print two integers. If you swap one pair of characters, print two distinct integers from \(1\) to \(n\) β the indices of characters to swap. If you don't swap, p... | Input: 6-++-+++----+++- | Output: 1 2 1 1 1 1 -1 1 1 -1 | Medium | 2 | 1,106 | 436 | 443 | 19 | |
1,936 | E | 1936E | E. Yet Yet Another Permutation Problem | 3,400 | divide and conquer; fft; math | You are given a permutation \(p\) of length \(n\).Please count the number of permutations \(q\) of length \(n\) which satisfy the following: for each \(1 \le i < n\), \(\max(q_1,\ldots,q_i) \neq \max(p_1,\ldots,p_i)\).Since the answer may be large, output the answer modulo \(998\,244\,353\). | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(p_1... | For each test case, print a single integer β the answer modulo \(998\,244\,353\). | In the first test case, \(p = [2, 1]\). The only suitable \(q\) is \([1, 2]\). Indeed, we need to satisfy the inequality \(q_1 \neq p_1\). It only holds for \(q = [1, 2]\).In the second test case, \(p = [1, 2, 3]\). So \(q\) has to satisfy two inequalities: \(q_1 \neq p_1\) and \(\max(q_1, q_2) \neq \max(1, 2) = 2\). O... | Input: 622 131 2 333 1 242 4 1 353 5 1 4 2156 13 2 8 7 11 1 3 9 15 4 5 12 10 14 | Output: 1 3 2 4 18 424488915 | Master | 3 | 292 | 501 | 81 | 19 |
1,926 | G | 1926G | G. Vlad and Trouble at MIT | 1,900 | dfs and similar; dp; flows; graphs; greedy; implementation; trees | Vladislav has a son who really wanted to go to MIT. The college dormitory at MIT (Moldova Institute of Technology) can be represented as a tree with \(n\) vertices, each vertex being a room with exactly one student. A tree is a connected undirected graph with \(n\) vertices and \(n-1\) edges.Tonight, there are three ty... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 10^5\)) β the number of vertices in the tree.The second line of each test case contains \(n-1\) integers \(a_2, \dots , a_n\) (\(1 \leq a_i < i\)... | For each test case, output a single integer β the minimum number of thick walls needed. | In the first case, we can install one thick wall between rooms \(1\) and \(2\), as shown below. We cannot install \(0\) walls, since then the music from room 3 will reach room 2 where a student wants to sleep, so the answer is \(1\). There are other valid solutions. | Input: 331 1CSP41 2 2PCSS41 2 2PPSS | Output: 1 1 2 | Hard | 7 | 1,074 | 647 | 87 | 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.