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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
358 | E | 358E | E. Dima and Kicks | 2,300 | brute force; dsu; graphs; implementation | Dima is a good person. In fact, he's great. But all good things come to an end...Seryozha is going to kick Dima just few times.. For this reason he divides the room into unit squares. Now the room is a rectangle n Γ m consisting of unit squares.For the beginning, Seryozha put Dima in a center of some square. Then he st... | The first line contains n and m (1 β€ n, m β€ 103) β size of the room.Next n lines goes, each contains m numbers aij β Dima's notes: aij = 1, if Dima was staying in the square (i, j) or was flying above it. Otherwise aij = 0.At least one aij equals 1. | In a single line in accending order print all k (k > 1), which matches the Dima's notes. If there are no such k and Dima invented this story with kicks, print -1. | Input: 5 51 1 1 1 11 0 0 0 11 0 0 0 11 0 0 0 11 1 1 1 1 | Output: 2 4 | Expert | 4 | 1,131 | 249 | 162 | 3 | |
1,605 | F | 1605F | F. PalindORme | 2,900 | combinatorics; dp | An integer array \(a\) of length \(n\) is said to be a PalindORme if (\(a_{1}\) \(|\) \(a_{2} \) \(|\) \( \ldots \) \(|\) \( a_{i}) = (a_{{n - i + 1}} \) \(|\) \( \ldots \) \(|\) \( a_{{n - 1}} \) \(|\) \( a_{n}) \) for all \( 1 \leq i \leq n\), where \(|\) denotes the bitwise OR operation.An integer array \(a\) of len... | The first and only line of the input contains three integers \(n\), \(k\) and \(m\) (\(1 \leq n,k \leq 80\), \(10^8 \lt m \lt 10^9\)). It is guaranteed that \(m\) is prime. | Print a single integer β the number of good arrays modulo \(m\). | In the first sample, both the possible arrays \([0]\) and \([1]\) are good.In the second sample, some examples of good arrays are: \([2, 1, 2]\) because it is already PalindORme. \([1, 1, 0]\) because it can rearranged to \([1, 0, 1]\) which is PalindORmeNote that \([1, 1, 0]\), \([1, 0, 1]\) and \([0, 1, 1]\) are all ... | Input: 1 1 998244353 | Output: 2 | Master | 2 | 948 | 172 | 64 | 16 |
2,030 | E | 2030E | E. MEXimize the Score | 2,200 | combinatorics; data structures; dp; greedy; implementation; math | Suppose we partition the elements of an array \(b\) into any number \(k\) of non-empty multisets \(S_1, S_2, \ldots, S_k\), where \(k\) is an arbitrary positive integer. Define the score of \(b\) as the maximum value of \(\operatorname{MEX}(S_1)\)\(^{\text{β}}\)\( + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < n\)) β the el... | For each test case, output the answer, modulo \(998\,244\,353\). | In the first testcase, we must consider seven subsequences: \([0]\): The score is \(1\). \([0]\): The score is \(1\). \([1]\): The score is \(0\). \([0,0]\): The score is \(2\). \([0,1]\): The score is \(2\). \([0,1]\): The score is \(2\). \([0,0,1]\): The score is \(3\). The answer for the first testcase is \(1+1+2+2+... | Input: 430 0 140 0 1 150 0 1 2 241 1 1 1 | Output: 11 26 53 0 | Hard | 6 | 1,135 | 438 | 64 | 20 |
962 | D | 962D | D. Merge Equals | 1,600 | data structures; implementation | You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value \(x\) that occurs in the array \(2\) or more times. Take the first two occurrences of \(x\) in this array (the two leftmost occurrences). Remove the left of the... | The first line contains a single integer \(n\) (\(2 \le n \le 150\,000\)) β the number of elements in the array.The second line contains a sequence from \(n\) elements \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{9}\)) β the elements of the array. | In the first line print an integer \(k\) β the number of elements in the array after all the performed operations. In the second line print \(k\) integers β the elements of the array after all the performed operations. | The first two examples were considered in the statement.In the third example all integers in the given array are distinct, so it will not change. | Input: 73 4 1 2 2 1 1 | Output: 43 8 2 1 | Medium | 2 | 906 | 248 | 218 | 9 |
2,028 | E | 2028E | E. Alice's Adventures in the Rabbit Hole | 2,300 | combinatorics; dfs and similar; dp; games; greedy; math; probabilities; trees | Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree\(^{\text{β}}\) which has an exit at vertex \(1\), and Alice starts at some vertex \(v\). She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution. Each minute, a fair coin is flipped. If it land... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.The \(i\)-th of the next \(n ... | For each test case, output \(n\) integers on one line β the probabilities of Alice escaping starting from vertex \(1, 2, \ldots, n\). Since these probabilities can be very small, output them modulo \(998\,244\,353\). | For the first test case: Alice escapes from the root (vertex \(1\)) by definition with probability \(1\). Alice immediately loses from vertices \(4\) and \(5\) since they are leaves. From the other two vertices, Alice escapes with probability \(\frac 12\) since the Queen will pull her to the leaves. | Input: 251 21 32 43 591 22 34 55 67 88 92 45 7 | Output: 1 499122177 499122177 0 0 1 499122177 0 332748118 166374059 0 443664157 720954255 0 | Expert | 8 | 1,340 | 588 | 216 | 20 |
729 | B | 729B | B. Spotlights | 1,200 | dp; implementation | Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four ... | The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. ... | Print one integer β the number of good positions for placing the spotlight. | In the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and... | Input: 2 40 1 0 01 0 1 0 | Output: 9 | Easy | 2 | 822 | 382 | 75 | 7 |
687 | A | 687A | A. NP-Hard Problem | 1,500 | dfs and similar; graphs | Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).Pari an... | The first line of the input contains two integers n and m (2 β€ n β€ 100 000, 1 β€ m β€ 100 000) β the number of vertices and the number of edges in the prize graph, respectively.Each of the next m lines contains a pair of integers ui and vi (1 β€ ui, vi β€ n), denoting an undirected edge between ui and vi. It's guaranteed t... | If it's impossible to split the graph between Pari and Arya as they expect, print ""-1"" (without quotes).If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of... | In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).In the second sample, there is no way to satisfy both Pari and Arya. | Input: 4 21 22 3 | Output: 12 21 3 | Medium | 2 | 776 | 376 | 476 | 6 |
1,196 | C | 1196C | C. Robot Breakout | 1,500 | implementation | \(n\) robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the loc... | The first line contains one integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.Then \(q\) queries follow. Each query begins with one line containing one integer \(n\) (\(1 \le n \le 10^5\)) β the number of robots in the query. Then \(n\) lines follow, the \(i\)-th of these lines describes the \(i\)-th robot i... | You should answer each query independently, in the order these queries appear in the input.To answer a query, you should do one of the following: if it is impossible to find a point that is reachable by all \(n\) robots, print one number \(0\) on a separate line; if it is possible to find a point that is reachable by a... | Input: 4 2 -1 -2 0 0 0 0 -1 -2 0 0 0 0 3 1 5 1 1 1 1 2 5 0 1 0 1 3 5 1 0 0 0 2 1337 1337 0 1 1 1 1336 1337 1 1 0 1 1 3 5 1 1 1 1 | Output: 1 -1 -2 1 2 5 0 1 -100000 -100000 | Medium | 1 | 1,926 | 893 | 741 | 11 | |
2,005 | A | 2005A | A. Simple Palindrome | 900 | combinatorics; constructive algorithms; greedy; math | Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes.Narek found out that the kids only know the vowels of the English alphabet (the letters \(\mathtt{a}\), \(\mathtt{e}\), \(\mathtt{i}\), \(\mathtt{o}\), an... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Subsequently, the description of each test case follows.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the size of the string. | For each test case, output any string of length \(n\) that satisfies the above conditions. | In the first example, \(\texttt{uo}\) has only three palindrome subsequences: \(\texttt{u}\), \(\texttt{o}\), and the empty string. It can be shown that there is no better answer.In the third example, \(\texttt{oeiiua}\) has only eight palindrome subsequences: \(\texttt{o}\), \(\texttt{e}\), \(\texttt{i}\), \(\texttt{i... | Input: 3236 | Output: uo iae oeiiua | Beginner | 4 | 1,432 | 277 | 90 | 20 |
1,958 | H | 1958H | H. Composite Spells | 2,600 | *special; dp | Monocarp plays a fantasy RPG. His character is a mage, so he casts spells. There are two types of spells he knows β basic spells and composite spells.There are \(n\) basic spells in Monocarp's spell book, numbered from \(1\) to \(n\). Each basic spell simply changes the health of the target: either decreases it or incr... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case is given as follows: the first line contains two integers \(n\) and \(hp\) (\(1 \le n \le 5000\); \(1 \le hp \le 10^{9}\)) β the number of basic spells and the initial health value of the monster; the second line ... | For each test case, print one integer: if the monster dies, print the index of the basic spell that kills the monster; otherwise, print \(-1\). | Input: 44 91 -2 3 -433 1 4 34 1 2 1 26 6 5 6 5 6 54 91 -2 3 -433 1 4 34 1 2 1 27 6 5 6 5 6 6 53 31-10 -20 3016 1 2 3 1 2 36 20-1 -5 -9 -7 -1 -143 6 5 24 3 3 7 66 4 8 4 4 6 73 6 5 7 | Output: 4 4 -1 -1 | Expert | 2 | 1,504 | 1,104 | 143 | 19 | |
1,146 | C | 1146C | C. Tree Diameter | 1,700 | bitmasks; graphs; interactive | There is a weighted tree with \(n\) nodes and \(n-1\) edges. The nodes are conveniently labeled from \(1\) to \(n\). The weights are positive integers at most \(100\). Define the distance between two nodes to be the sum of edges on the unique path between the nodes. You would like to find the diameter of the tree. Diam... | In the first example, the first tree looks as follows: In the first question, we have \(p = {1}\), and \(q = {2, 3, 4, 5}\). The maximum distance between a node in \(p\) and a node in \(q\) is \(9\) (the distance between nodes \(1\) and \(5\)).The second tree is a tree with two nodes with an edge with weight \(99\) bet... | Input: 2 5 9 6 10 9 10 2 99 | Output: 1 4 1 2 3 4 5 1 4 2 3 4 5 1 1 4 3 4 5 1 2 1 4 4 5 1 2 3 1 4 5 1 2 3 4 -1 10 1 1 1 2 -1 99 | Medium | 3 | 834 | 0 | 0 | 11 | ||
1,091 | F | 1091F | F. New Year and the Mallard Expedition | 2,600 | constructive algorithms; greedy | Bob is a duck. He wants to get to Alice's nest, so that those two can duck! Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of \(n\) segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. ... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of segments of terrain. The second line contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \leq l_i \leq 10^{12}\)). The \(l_i\) represents the length of the \(i\)-th terrain segment in meters.The third line contains a string \(s\) ... | Output a single integer \(t\) β the minimum time Bob needs to reach Alice. | In the first sample, Bob first walks \(5\) meters in \(25\) seconds. Then he flies the remaining \(5\) meters in \(5\) seconds.In the second sample, Bob first swims \(10\) meters in \(30\) seconds. Then he flies over the patch of lava for \(10\) seconds.In the third sample, the water pond is much smaller. Bob first swi... | Input: 1 10 G | Output: 30 | Expert | 2 | 1,297 | 474 | 74 | 10 |
178 | E1 | 178E1 | E1. The Beaver's Problem - 2 | 1,900 | Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the ma... | The first input line contains a single integer n (1000 β€ n β€ 2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0 β€ aij β€ 1), separated by spaces. Value of aij = 0 corresponds to a white pixel and... | Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly. | You are given a sample of original data for each difficulty level. The samples are available at http://codeforces.com/static/materials/contests/178/e-samples.zip . | Hard | 0 | 1,269 | 1,111 | 142 | 1 | ||
356 | D | 356D | D. Bags and Coins | 2,700 | bitmasks; constructive algorithms; dp; greedy | When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions: A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible? The answer is quite simp... | The first line contains two integers n and s (1 β€ n, s β€ 70000) β the number of bags and the total number of coins. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 70000), where ai shows the number of coins in the i-th bag. | If the answer doesn't exist, print -1. Otherwise, print n lines, on the i-th line print the contents of the i-th bag. The first number in the line, ci (0 β€ ci β€ ai), must represent the number of coins lying directly in the i-th bag (the coins in the bags that are in the i-th bag are not taken into consideration). The s... | The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one. | Input: 3 31 3 1 | Output: 1 01 2 3 11 0 | Master | 4 | 723 | 235 | 959 | 3 |
1,466 | B | 1466B | B. Last minute enhancements | 800 | dp; greedy | Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of \(n\) notes, which we will treat as positive integers. The diversity of a song is ... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases. Then \(t\) test cases follow, each one is described in two lines.In the first line of each test case there is a single integer \(n\) (\(1 \leq n \leq 10^5\)) denoting the length of... | For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence. | In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence \(1, \underline{3}, 2, 2, \underline{6}, \underline{7}\), which has \(5\) different elements (increased elements are underlined).In the second test case, Euterpe can increase the first element to obtain the sequence ... | Input: 5 6 1 2 2 2 5 6 2 4 4 6 1 1 3 4 4 5 1 1 6 1 1 1 2 2 2 | Output: 5 2 6 1 3 | Beginner | 2 | 832 | 554 | 198 | 14 |
490 | A | 490A | A. Team Olympiad | 800 | greedy; implementation; sortings | The School β0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti: ti = 1, if the i-th child is good at programming, ti = 2, i... | The first line contains integer n (1 β€ n β€ 5000) β the number of children in the school. The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 3), where ti describes the skill of the i-th child. | In the first line output integer w β the largest possible number of teams. Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in ... | Input: 71 3 1 3 2 1 2 | Output: 23 5 26 7 4 | Beginner | 3 | 930 | 202 | 541 | 4 | |
467 | C | 467C | C. George and Job | 1,700 | dp; implementation | The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers: [l1, r1], [l2, r... | The first line contains three integers n, m and k (1 β€ (m Γ k) β€ n β€ 5000). The second line contains n integers p1, p2, ..., pn (0 β€ pi β€ 109). | Print an integer in a single line β the maximum possible value of sum. | Input: 5 2 11 2 3 4 5 | Output: 9 | Medium | 2 | 491 | 143 | 70 | 4 | |
1,581 | B | 1581B | B. Diameter of Graph | 1,200 | constructive algorithms; graphs; greedy; math | CQXYM wants to create a connected undirected graph with \(n\) nodes and \(m\) edges, and the diameter of the graph must be strictly less than \(k-1\). Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at... | The input consists of multiple test cases. The first line contains an integer \(t (1 \leq t \leq 10^5)\) β the number of test cases. The description of the test cases follows.Only one line of each test case contains three integers \(n(1 \leq n \leq 10^9)\), \(m\), \(k\) \((0 \leq m,k \leq 10^9)\). | For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower). | In the first test case, the graph's diameter equal to 0.In the second test case, the graph's diameter can only be 2.In the third test case, the graph's diameter can only be 1. | Input: 5 1 0 3 4 5 3 4 6 3 5 4 1 2 1 1 | Output: YES NO YES NO NO | Easy | 4 | 578 | 298 | 157 | 15 |
1,571 | F | 1571F | F. Kotlinforces | 2,000 | *special; constructive algorithms; dp | Kotlinforces is a web platfrom that hosts programming competitions.The staff of Kotlinforces is asked to schedule \(n\) programming competitions on the next \(m\) days. Each competition is held in multiple stages; the regulations of the \(i\)-th competition state that this competition should consist of exactly \(k_i\) ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 5000\)) β the number of competitions and the number of days, respectively.Then \(n\) lines follow, each describing a competition which should be scheduled. The \(i\)-th line contains two integers \(k_i\) and \(t_i\) (\(2 \le k_i \le 5000\); \(1 \le ... | If it is impossible to schedule all \(n\) competitions on the next \(m\) days so that there is at most one stage during each day, print -1.Otherwise, print \(n\) integers. The \(i\)-th integer should represent the day when the first stage of the \(i\)-th competition is scheduled; days are numbered from \(1\) to \(m\). ... | Input: 3 7 3 2 2 2 2 2 | Output: 2 5 1 | Hard | 3 | 1,043 | 378 | 369 | 15 | |
359 | A | 359A | A. Table | 1,000 | constructive algorithms; greedy; implementation | Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns β from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1... | The first line contains exactly two integers n, m (3 β€ n, m β€ 50).Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cel... | Print a single number β the minimum number of operations Simon needs to carry out his idea. | In the first sample, the sequence of operations can be like this: For the first time you need to choose cell (2, 2) and corner (1, 1). For the second time you need to choose cell (2, 2) and corner (3, 3). For the third time you need to choose cell (2, 2) and corner (3, 1). For the fourth time you need to choose cell (2... | Input: 3 30 0 00 1 00 0 0 | Output: 4 | Beginner | 3 | 930 | 378 | 91 | 3 |
1,153 | E | 1153E | E. Serval and Snake | 2,200 | binary search; brute force; interactive | This is an interactive problem.Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a \(n \times n\) grid. The snake has a head and a tail in different cells, and its body is a... | The first line contains a single integer \(n\) (\(2\leq n \leq 1000\)) β the size of the grid. | When you are ready to answer, you should print ! x1 y1 x2 y2, where \((x_1, y_1)\) represents the position of the head and \((x_2,y_2)\) represents the position of the tail. You can print head and tail in any order. | The pictures above show our queries and the answers in the first example. We first made a query for \((1,1)\) and got an answer \(1\), then found that it must be connected to exactly one other cell. Then we made a query for \((1,2)\) and got an answer of \(0\), then knew that the snake never entered it. So the cell con... | Input: 2 1 0 0 | Output: ? 1 1 1 1 ? 1 2 1 2 ? 2 2 2 2 ! 1 1 2 1 | Hard | 3 | 1,326 | 94 | 215 | 11 |
1,131 | G | 1131G | G. Most Dangerous Shark | 2,700 | data structures; dp; two pointers | Semyon participates in the most prestigious competition of the world ocean for the title of the most dangerous shark. During this competition sharks compete in different subjects: speed swimming, masking, map navigation and many others. Now Semyon is taking part in Β«destructionΒ» contest.During it, \(m\) dominoes are pl... | In order to reduce input size, the heights and costs of the dominoes are described with blocks.The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 250\,000, 1 \leq m \leq 10^7\)) β the number of the blocks and the total number of the dominoes Semyon must drop.Then descriptions of \(n\) blocks follow. ... | Print exactly one integer β the minimum cost to make all the dominoes fall. | In the first example, there are \(7\) dominoes in front of the Semyon. Their heights are equal to \([3, 1, 2, 2, 1, 2, 2]\), and their costs are equal to \([4, 3, 6, 3, 1, 2, 1]\). Semyon should drop the domino with index \(7\) to the left, it will fall and drop the domino \(6\) as well. The domino \(6\) during the fal... | Input: 2 7 3 1 2 2 1 2 1 1 3 2 3 2 2 1 3 1 1 | Output: 5 | Master | 3 | 1,584 | 1,390 | 75 | 11 |
616 | C | 616C | C. The Labyrinth | 1,600 | dfs and similar | You are given a rectangular field of n Γ m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.Let's call a connected component any non-extendible set of cells such that any... | The first line contains two integers n, m (1 β€ n, m β€ 1000) β the number of rows and columns in the field.Each of the next n lines contains m symbols: ""."" for empty cells, ""*"" for impassable cells. | Print the answer as a matrix as described above. See the examples to precise the format of the output. | In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner). | Input: 3 3*.*.*.*.* | Output: 3.3.5.3.3 | Medium | 1 | 1,420 | 201 | 102 | 6 |
37 | A | 37A | A. Towers | 1,000 | sortings | Little Vasya has received a young builderβs kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way po... | The first line contains an integer N (1 β€ N β€ 1000) β the number of bars at Vasyaβs disposal. The second line contains N space-separated integers li β the lengths of the bars. All the lengths are natural numbers not exceeding 1000. | In one line output two numbers β the height of the largest tower and their total number. Remember that Vasya should use all the bars. | Input: 31 2 3 | Output: 1 3 | Beginner | 1 | 327 | 231 | 133 | 0 | |
452 | B | 452B | B. 4-point polyline | 1,800 | brute force; constructive algorithms; geometry; trees | You are given a rectangular grid of lattice points from (0, 0) to (n, m) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible.A polyline defined by points p1, p2, p3, p4 consists of the line segments ... | The only line of the input contains two integers n and m (0 β€ n, m β€ 1000). It is guaranteed that grid contains at least 4 different points. | Print 4 lines with two integers per line separated by space β coordinates of points p1, p2, p3, p4 in order which represent the longest possible polyline.Judge program compares your answer and jury's answer with 10 - 6 precision. | Input: 1 1 | Output: 1 10 01 00 1 | Medium | 4 | 414 | 140 | 229 | 4 | |
48 | G | 48G | G. Galaxy Union | 2,700 | dp; trees; two pointers | In a far away galaxy there are n inhabited planets numbered with numbers from 1 to n. One day the presidents of all the n planets independently from each other came up with an idea of creating the Galaxy Union. Now they need to share this wonderful idea with their galaxymates, thatβs why each president is busy working ... | The first line contains an integer n (3 β€ n β€ 200000) which represents the number of planets in the Galaxy and the number of communication channels equal to it. The next n lines contain three integers each ai, bi and ti (1 β€ ai, bi β€ n, ai β bi, 1 β€ ti β€ 103) that represent the numbers of planet joined by a communicati... | In the first line output n integers β the durations of the supposed negotiations for each president. Separate the numbers by spaces. | Input: 31 2 32 3 21 3 1 | Output: 4 5 3 | Master | 3 | 2,020 | 436 | 132 | 0 | |
11 | B | 11B | B. Jumping Jack | 1,600 | math | Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or righ... | The input data consists of only one integer x ( - 109 β€ x β€ 109). | Output the minimal number of jumps that Jack requires to reach x. | Input: 2 | Output: 3 | Medium | 1 | 384 | 65 | 65 | 0 | |
1,970 | E2 | 1970E2 | E2. Trails (Medium) | 2,000 | dp; matrices | Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long tra... | The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabi... | The number of possible combinations of trails, modulo \(10^9 + 7\). | Input: 3 21 0 10 1 1 | Output: 18 | Hard | 2 | 754 | 448 | 67 | 19 | |
5 | A | 5A | A. Chat Server's Outgoing Traffic | 1,000 | implementation | Polycarp is working on a new project called ""Polychat"". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person t... | Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following: +<name> for 'Add' command. -<name> for 'Remove' command. <sender_name>:<message_text> for 'Send' command. <name> and <sender_name> is a non-empty sequence of Latin... | Print a single number β answer to the problem. | Input: +MikeMike:hello+Kate+Dmitry-DmitryKate:hi-Kate | Output: 9 | Beginner | 1 | 952 | 721 | 46 | 0 | |
1,065 | A | 1065A | A. Vasya and Chocolate | 800 | implementation; math | There is a special offer in Vasya's favourite supermarket: if the customer buys \(a\) chocolate bars, he or she may take \(b\) additional bars for free. This special offer can be used any number of times.Vasya currently has \(s\) roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs \(c... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.Each of the next \(t\) lines contains four integers \(s, a, b, c~(1 \le s, a, b, c \le 10^9)\) β the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get fo... | Print \(t\) lines. \(i\)-th line should contain the maximum possible number of chocolate bars Vasya can get in \(i\)-th test. | In the first test of the example Vasya can buy \(9\) bars, get \(3\) for free, buy another bar, and so he will get \(13\) bars.In the second test Vasya buys \(1000000000\) bars and gets \(1000000000000000000\) for free. So he has \(1000000001000000000\) bars. | Input: 210 3 1 11000000000 1 1000000000 1 | Output: 131000000001000000000 | Beginner | 2 | 413 | 366 | 125 | 10 |
777 | C | 777C | C. Alyona and Spreadsheet | 1,600 | binary search; data structures; dp; greedy; implementation; two pointers | During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in n... | The first line of the input contains two positive integers n and m (1 β€ nΒ·m β€ 100 000) β the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.Each of the following n lines cont... | Print ""Yes"" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print ""No"". | In the sample, the whole table is not sorted in any column. However, rows 1β3 are sorted in column 1, while rows 4β5 are sorted in column 3. | Input: 5 41 2 3 53 1 3 24 5 2 35 5 3 24 4 3 461 12 54 53 51 31 5 | Output: YesNoYesYesYesNo | Medium | 6 | 832 | 609 | 183 | 7 |
1,221 | D | 1221D | D. Make The Fence Great Again | 1,800 | dp | You have a fence consisting of \(n\) vertical boards. The width of each board is \(1\). The height of the \(i\)-th board is \(a_i\). You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from \(2\) to \(n\), the ... | The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The first line of each query contains one integers \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of boards in the fence.The following \(n\) lines of each query contain the descriptions of the boards. The \(i\)-th line c... | For each query print one integer β the minimum number of rubles you have to spend to make the fence great. | In the first query you have to increase the length of second board by \(2\). So your total costs if \(2 \cdot b_2 = 2\).In the second query you have to increase the length of first board by \(1\) and the length of third board by \(1\). So your total costs if \(1 \cdot b_1 + 1 \cdot b_3 = 9\).In the third query the fenc... | Input: 3 3 2 4 2 1 3 5 3 2 3 2 10 2 6 4 1 7 3 3 2 6 1000000000 2 | Output: 2 9 0 | Medium | 1 | 758 | 636 | 106 | 12 |
808 | C | 808C | C. Tea Party | 1,400 | constructive algorithms; greedy; sortings | Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a1, a2, ..., an. His teapot stores w milliliters of tea (w β€ a1 + a2 + ... + an). Polycarp wants to pour tea in cups in such a way that: Every cup will contain tea for at least half of ... | The first line contains two integer numbers n and w (1 β€ n β€ 100, ).The second line contains n numbers a1, a2, ..., an (1 β€ ai β€ 100). | Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.If it's impossible to pour all the tea and satisfy all conditions then output -1. | In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available. | Input: 2 108 7 | Output: 6 4 | Easy | 3 | 745 | 134 | 191 | 8 |
1,787 | I | 1787I | I. Treasure Hunt | 3,400 | data structures; divide and conquer; two pointers | Define the beauty value of a sequence \(b_1,b_2,\ldots,b_c\) as the maximum value of \(\sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i\), where \(q\), \(s\), \(t\) are all integers and \(s > q\) or \(t\leq q\). Note that \(b_i = 0\) when \(i<1\) or \(i>c\), \(\sum\limits_{i=s}^{t}b_i = 0\) when \(s>t\).For example,... | Each test contains multiple test cases. The first line contains an integer \(T\) (\(1 \le T \le 10^4\)) β the number of test cases.The first line contains an integer \(n\) (\(1\le n\le 10^6\)) β the length of \(a\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(-10^6 \leq a_i \leq 10^6\)) β the given... | For each test case, print a line containing a single integer β the answer modulo \(998\,244\,353\). | In the second test case, for the subsequence \([-26,43,-41,34,13]\), when \(q=5\), \(s=2\), \(t=5\), \(\sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i = 23 + 49 = 72\).In the third test case, there is only one non-empty consecutive subsequence \([74]\). When \(q=1\), \(s=1\), \(t=1\), \(\sum\limits_{i=1}^{q}b_i + \... | Input: 4780 59 100 -52 -86 -62 758-48 -14 -26 43 -41 34 13 551742056 -60 62 13 88 -48 64 36 -10 19 94 25 -69 88 87 79 -70 74 -26 59 | Output: 5924 2548 148 98887 | Master | 3 | 751 | 393 | 99 | 17 |
1,005 | E2 | 1005E2 | E2. Median on Segments (General Case Edition) | 2,400 | sortings | You are given an integer sequence \(a_1, a_2, \dots, a_n\).Find the number of pairs of indices \((l, r)\) (\(1 \le l \le r \le n\)) such that the value of median of \(a_l, a_{l+1}, \dots, a_r\) is exactly the given number \(m\).The median of a sequence is the value of an element which is in the middle of the sequence a... | The first line contains integers \(n\) and \(m\) (\(1 \le n,m \le 2\cdot10^5\)) β the length of the given sequence and the required value of the median.The second line contains an integer sequence \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2\cdot10^5\)). | Print the required number. | In the first example, the suitable pairs of indices are: \((1, 3)\), \((1, 4)\), \((1, 5)\), \((2, 2)\), \((2, 3)\), \((2, 5)\), \((4, 5)\) and \((5, 5)\). | Input: 5 41 4 5 60 4 | Output: 8 | Expert | 1 | 934 | 253 | 26 | 10 |
552 | A | 552A | A. Vanya and Table | 1,000 | implementation; math | Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multip... | The first line contains integer n (1 β€ n β€ 100) β the number of rectangles.Each of the following n lines contains four integers x1, y1, x2, y2 (1 β€ x1 β€ x2 β€ 100, 1 β€ y1 β€ y2 β€ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upp... | In a single line print the sum of all values in the cells of the table. | Note to the first sample test:Values of the table in the first three rows and columns will be as follows:121121110So, the sum of values will be equal to 10.Note to the second sample test:Values of the table in the first three rows and columns will be as follows:222222222So, the sum of values will be equal to 18. | Input: 21 1 2 32 2 3 3 | Output: 10 | Beginner | 2 | 586 | 349 | 71 | 5 |
1,557 | E | 1557E | E. Assiut Chess | 2,800 | brute force; constructive algorithms; interactive | This is an interactive problem.ICPC Assiut Community decided to hold a unique chess contest, and you were chosen to control a queen and hunt down the hidden king, while a member of ICPC Assiut Community controls this king.You compete on an \(8\times8\) chessboard, the rows are numerated from top to bottom, and the colu... | The first line contains a single integer \(t\) (\(1 \le t \le 60\)) β the number of test cases. | In the example, the hidden king was at \((8, 8)\) at the start. The game follows like this: | Input: 1 Left Right Done | Output: 7 5 7 6 7 7 | Master | 3 | 2,033 | 95 | 0 | 15 | |
1,095 | E | 1095E | E. Almost Regular Bracket Sequence | 1,900 | implementation | You are given a bracket sequence \(s\) consisting of \(n\) opening '(' and closing ')' brackets.A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequence... | The first line of the input contains one integer \(n\) (\(1 \le n \le 10^6\)) β the length of the bracket sequence.The second line of the input contains the string \(s\) consisting of \(n\) opening '(' and closing ')' brackets. | Print one integer β the number of positions \(i\) such that if you change the type of the \(i\)-th bracket, then the resulting bracket sequence becomes regular. | Input: 6 (((()) | Output: 3 | Hard | 1 | 735 | 227 | 160 | 10 | |
1,348 | B | 1348B | B. Phoenix and Beauty | 1,400 | constructive algorithms; data structures; greedy; sortings | Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length \(k\) have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array \(a\) of length \(n\). He wants to insert some number of integers, possibly zero, into his array such that it become... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 50\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)).The second line of each test case contains \(n\) space-separated integers (\(1 \le a_i \l... | For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.The first line should contain the length of the beautiful array \(m\) (\(n \le m \le 10^4\)). You don't need to minimize \(m\).The second line should contain \(m\) space-separated integers (\(1 \le b_i \le n\)) β a... | In the first test case, we can make array \(a\) beautiful by inserting the integer \(1\) at index \(3\) (in between the two existing \(2\)s). Now, all subarrays of length \(k=2\) have the same sum \(3\). There exists many other possible solutions, for example: \(2, 1, 2, 1, 2, 1\) \(1, 2, 1, 2, 1, 2\) In the second tes... | Input: 4 4 2 1 2 2 1 4 3 1 2 2 1 3 2 1 2 3 4 4 4 3 4 2 | Output: 5 1 2 1 2 1 4 1 2 2 1 -1 7 4 3 2 1 4 3 2 | Easy | 4 | 548 | 414 | 660 | 13 |
1,878 | F | 1878F | F. Vasilije Loves Number Theory | 1,900 | brute force; math; number theory | Vasilije is a smart student and his discrete mathematics teacher Sonja taught him number theory very well.He gave Ognjen a positive integer \(n\).Denote \(d(n)\) as the number of positive integer divisors of \(n\), and denote \(gcd(a, b)\) as the largest integer \(g\) such that \(a\) is divisible by \(g\) and \(b\) is ... | The first line contains a positive integer \(t\), (\(1 \le t \le 100\)) β the number of test cases.The first line of each test case contains \(2\) integers, \(n\) and \(q\) (\(1 \le n \le 10^{6}\), \(1\le q \le 1000\)) β the number \(n\) and the number of queries.The following \(q\) lines contain an integer \(k\) (\(1 ... | For each type 1 query, you should output ""YES"" if there exist such positive integer \(a\) that \(gcd(a, n) = 1\) and \(d(n \cdot a)=n\), and ""NO"" if he can't.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer). | In the first test case, we initially have \(n=1\).After the first query: \(n=1\), \(d(n)=1\), so by taking \(a = 1\), \(d(n \cdot a) = n\), and the answer to this query is ""YES"".After the second query: \(n=2\), \(d(n)=2\), we can, again, take \(a = 1\), \(d(n \cdot a) = n\), and the answer to this query is ""YES"".Af... | Input: 71 51 11 221 81 920 41 321 71 1216 101 61 61 101 91 11 91 71 31 21 109 11 38 11 28 31 51 81 1011 51 81 21 11 31 1 | Output: YES YES YES YES YES NO YES YES NO YES YES YES NO YES NO YES YES NO NO YES NO NO YES NO NO NO NO | Hard | 3 | 924 | 626 | 306 | 18 |
1,741 | G | 1741G | G. Kirill and Company | 2,200 | bitmasks; brute force; dfs and similar; dp; flows; graphs; shortest paths | Kirill lives on a connected undirected graph of \(n\) vertices and \(m\) edges at vertex \(1\). One fine evening he gathered \(f\) friends, the \(i\)-th friend lives at the vertex \(h_i\). So all friends are now in the vertex \(1\), the \(i\)-th friend must get to his home to the vertex \(h_i\).The evening is about to ... | The first line of input data contains an integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases in the test.The first line of the test case contains two integers \(n\) and \(m\) (\(2 \le n \le 10^4\), \(n-1 \le m \le min (10^4, \)\( \frac{n \cdot (n - 1)}{2} \)\()\)) β the number of vertices and edges, respect... | Output \(t\) lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer β the minimum possible number of friends who will have to walk. | The first test case of the first example is explained in the statement.In the second test case of the first example, two friends with cars live at vertex \(5\), one can give a ride to friends from vertices \(2\) and \(3\), and the second from vertex \(4\), only a friend from vertex \(6\) will have to walk. | Input: 36 71 22 32 43 54 53 66 552 3 4 5 641 2 3 56 71 22 32 43 54 53 66 562 3 4 5 6 541 2 3 54 41 21 32 33 433 4 221 3 | Output: 2 1 1 | Hard | 7 | 1,087 | 1,291 | 187 | 17 |
21 | B | 21B | B. Intersection | 2,000 | implementation; math | You are given two set of points. The first set is determined by the equation A1x + B1y + C1 = 0, and the second one is determined by the equation A2x + B2y + C2 = 0.Write the program which finds the number of points in the intersection of two given sets. | The first line of the input contains three integer numbers A1, B1, C1 separated by space. The second line contains three integer numbers A2, B2, C2 separated by space. All the numbers are between -100 and 100, inclusive. | Print the number of points in the intersection or -1 if there are infinite number of points. | Input: 1 1 02 2 0 | Output: -1 | Hard | 2 | 254 | 220 | 92 | 0 | |
1,662 | E | 1662E | E. Round Table | 0 | math | There are \(n\) people, numbered from \(1\) to \(n\), sitting at a round table. Person \(i+1\) is sitting to the right of person \(i\) (with person \(1\) sitting to the right of person \(n\)).You have come up with a better seating arrangement, which is given as a permutation \(p_1, p_2, \dots, p_n\). More specifically,... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 200\,000\)) β the number of people sitting at the table. Th... | For each test case, print the minimum number of swaps necessary to achieve the desired order. | In the first test case, we can swap person \(4\) and person \(1\) (who are adjacent) in the initial configuration and get the order \([4, 2, 3, 1]\) which is equivalent to the desired one. Hence in this case a single swap is sufficient. | Input: 342 3 1 455 4 3 2 174 1 6 5 3 7 2 | Output: 1 10 22 | Beginner | 1 | 965 | 584 | 93 | 16 |
1,850 | H | 1850H | H. The Third Letter | 1,700 | dfs and similar; dsu; graphs; greedy; implementation | In order to win his toughest battle, Mircea came up with a great strategy for his army. He has \(n\) soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the \(x\)-axis (at points \(\cdots, -2, -1, 0, 1, 2, \cdots\... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two positive integers \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\); \(1 \leq m \leq n\)) β the number of soldiers, and the number of conditions respectively.Then \(m\) lines fol... | For each test case, output ""YES"" if there is an arrangement of the \(n\) soldiers that satisfies all of the \(m\) conditions and ""NO"" otherwise. | For the first test case, we can partition the soldiers into camps in the following way: soldier: Soldier \(1\) in the camp with the coordinate \(x = 3\). Soldier \(2\) in the camp with the coordinate \(x = 5\). Soldier \(3\) in the camp with the coordinate \(x = 9\). Soldier \(4\) in the camp with the coordinate \(x = ... | Input: 45 31 2 22 3 44 2 -66 51 2 22 3 44 2 -65 4 43 5 1002 21 2 51 2 44 11 2 3 | Output: YES NO NO YES | Medium | 5 | 908 | 760 | 148 | 18 |
1,015 | E1 | 1015E1 | E1. Stars Drawing (Easy Edition) | 1,700 | brute force; dp; greedy | A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length \(0\) are not allowed).Let's consid... | The first line of the input contains two integers \(n\) and \(m\) (\(3 \le n, m \le 100\)) β the sizes of the given grid.The next \(n\) lines contains \(m\) characters each, the \(i\)-th line describes the \(i\)-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. | If it is impossible to draw the given grid using stars only, print ""-1"".Otherwise in the first line print one integer \(k\) (\(0 \le k \le n \cdot m\)) β the number of stars needed to draw the given grid. The next \(k\) lines should contain three integers each β \(x_j\), \(y_j\) and \(s_j\), where \(x_j\) is the row ... | In the first example the output 23 4 13 5 2is also correct. | Input: 6 8....*......**.....*****....**.......*........... | Output: 33 4 13 5 23 5 1 | Medium | 3 | 1,175 | 299 | 499 | 10 |
542 | B | 542B | B. Duck Hunt | 3,100 | data structures | A duck hunter is doing his favorite thing, hunting. He lives in a two dimensional world and is located at point (0, 0). As he doesn't like walking for his prey, he prefers to shoot only vertically up (because in this case, the ducks fall straight into his hands). The hunter doesn't reload the gun immediately β r or mor... | The first line of the input contains integers n, r (1 β€ n β€ 200 000, 1 β€ r β€ 109) β the number of ducks and the minimum time in seconds between the shots. Then n lines follow, each of them contains two integers hi, ti ( - 109 β€ hi < ti β€ 109) β the x-coordinate of the head and tail of the i-th duck at the moment 0. | Print a single integer β the maximum number of ducks that can be shot by the hunter. | In the first sample the hunter must shoot at time 0, this shot kills ducks 1 and 3. Then the hunter needs to reload the gun and shoot again at time 3. His second shot hits the tail of duck 2.In the second sample the hunter can make shots at times 0 and 6 to hit three ducks. | Input: 3 3-3 01 3-1 2 | Output: 3 | Master | 1 | 1,263 | 316 | 84 | 5 |
2,038 | K | 2038K | K. Grid Walk | 2,100 | brute force; dp; greedy; math; number theory | You have an \(n \times n\) grid and two integers \(a\) and \(b\). Both the rows and the columns are numbered from \(1\) to \(n\). Let's denote the cell at the intersection of the \(i\)-th row and the \(j\)-th column as \((i, j)\).You are standing in the cell \((1, 1)\) and want to move into the cell \((n, n)\).Suppose ... | The only line contains three integers \(n\), \(a\), and \(b\) (\(2 \le n \le 10^6\); \(1 \le a, b \le 10^6\)). | Print one integer β the cost of the cheapest route from \((1, 1)\) to \((n, n)\). | The first example is described in the picture above. | Input: 4 2 4 | Output: 21 | Hard | 5 | 851 | 110 | 81 | 20 |
253 | A | 253A | A. Boys and Girls | 1,100 | greedy | There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 β€ i < n + m) such that positions with indexes i... | The single line of the input contains two integers n and m (1 β€ n, m β€ 100), separated by a space. | Print a line of n + m characters. Print on the i-th position of the line character ""B"", if the i-th position of your arrangement should have a boy and ""G"", if it should have a girl. Of course, the number of characters ""B"" should equal n and the number of characters ""G"" should equal m. If there are multiple opti... | In the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal. | Input: 3 3 | Output: GBGBGB | Easy | 1 | 519 | 98 | 353 | 2 |
1,582 | F2 | 1582F2 | F2. Korney Korneevich and XOR (hard version) | 2,400 | binary search; brute force; dp; greedy; two pointers | This is a harder version of the problem with bigger constraints.Korney Korneevich dag up an array \(a\) of length \(n\). Korney Korneevich has recently read about the operation bitwise XOR, so he wished to experiment with it. For this purpose, he decided to find all integers \(x \ge 0\) such that there exists an increa... | The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 5000\)) β the elements of the array \(a\). | In the first line print a single integer \(k\) β the number of found \(x\) values.In the second line print \(k\) integers in increasing order \(x_1, x_2, \ldots x_k\) (\(0 \le x_1 < \ldots < x_k\)) β found \(x\) values. | In the first test case: To get value \(x = 0\) it is possible to choose and empty subsequence To get value \(x = 2\) it is possible to choose a subsequence \([2]\) To get value \(x = 4\) it is possible to choose a subsequence \([4]\) To get value \(x = 6\) it is possible to choose a subsequence \([2, 4]\) | Input: 4 4 2 2 4 | Output: 4 0 2 4 6 | Expert | 5 | 801 | 195 | 219 | 15 |
1,989 | C | 1989C | C. Two Movies | 1,400 | greedy; math | A movie company has released \(2\) movies. These \(2\) movies were watched by \(n\) people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.If a person is asked to leave a review for the movie, then: if that person liked the movie, they wi... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-1 \le a_i \le 1\)), where \(a_i\) is equal to \(-1\) if th... | For each test case, print a single integer β the maximum possible rating of the company, if for each person, choose which movie to leave a review on. | Input: 42-1 1-1 -11-1-150 -1 1 0 1-1 1 0 0 14-1 -1 -1 1-1 1 1 1 | Output: 0 -1 1 1 | Easy | 2 | 847 | 820 | 149 | 19 | |
1,168 | B | 1168B | B. Good Triple | 1,900 | brute force; two pointers | Toad Rash has a binary string \(s\). A binary string consists only of zeros and ones.Let \(n\) be the length of \(s\).Rash needs to find the number of such pairs of integers \(l\), \(r\) that \(1 \leq l \leq r \leq n\) and there is at least one pair of integers \(x\), \(k\) such that \(1 \leq x, k \leq n\), \(l \leq x ... | The first line contains the string \(s\) (\(1 \leq |s| \leq 300\,000\)), consisting of zeros and ones. | Output one integer: the number of such pairs of integers \(l\), \(r\) that \(1 \leq l \leq r \leq n\) and there is at least one pair of integers \(x\), \(k\) such that \(1 \leq x, k \leq n\), \(l \leq x < x + 2k \leq r\), and \(s_x = s_{x+k} = s_{x+2k}\). | In the first example, there are three \(l\), \(r\) pairs we need to count: \(1\), \(6\); \(2\), \(6\); and \(1\), \(5\).In the second example, there are no values \(x\), \(k\) for the initial string, so the answer is \(0\). | Input: 010101 | Output: 3 | Hard | 2 | 407 | 102 | 255 | 11 |
1,991 | A | 1991A | A. Maximize the Last Element | 800 | greedy; implementation | You are given an array \(a\) of \(n\) integers, where \(n\) is odd.In one operation, you will remove two adjacent elements from the array \(a\), and then concatenate the remaining parts of the array. For example, given the array \([4,7,4,2,9]\), we can obtain the arrays \([4,2,9]\) and \([4,7,9]\) by the operations \([... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 99\); \(n\) is odd) β the length of the array \(a\).The second l... | For each test case, output a single integer β the maximum possible value of the remaining element in \(a\). | In the first test case, the array \(a\) is \([6]\). Since there is only one element, no operations are needed. The maximum possible value of the remaining element is \(6\).In the second test case, the array \(a\) is \([1, 3, 2]\). We can remove the first two elements \([\underline{1, 3}, 2] \to [2]\), or remove the las... | Input: 41631 3 254 7 4 2 973 1 4 1 5 9 2 | Output: 6 2 9 5 | Beginner | 2 | 702 | 518 | 107 | 19 |
1,061 | B | 1061B | B. Views Matter | 1,400 | greedy; implementation; sortings | You came to the exhibition and one exhibit has drawn your attention. It consists of \(n\) stacks of blocks, where the \(i\)-th stack consists of \(a_i\) blocks resting on the surface.The height of the exhibit is equal to \(m\). Consequently, the number of blocks in each stack is less than or equal to \(m\).There is a c... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\,000\), \(1 \le m \le 10^9\)) β the number of stacks and the height of the exhibit.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le m\)) β the number of blocks in each stack from left to right. | Print exactly one integer β the maximum number of blocks that can be removed. | The following pictures illustrate the first example and its possible solution.Blue cells indicate removed blocks. There are \(10\) blue cells, so the answer is \(10\). | Input: 5 63 3 3 3 3 | Output: 10 | Easy | 3 | 873 | 299 | 77 | 10 |
1,119 | A | 1119A | A. Ilya and a Colorful Walk | 1,100 | greedy; implementation | Ilya lives in a beautiful city of Chordalsk.There are \(n\) houses on the street Ilya lives, they are numerated from \(1\) to \(n\) from left to right; the distance between every two neighboring houses is equal to \(1\) unit. The neighboring houses are \(1\) and \(2\), \(2\) and \(3\), ..., \(n-1\) and \(n\). The house... | The first line contains a single integer \(n\) (\(3 \leq n \leq 300\,000\)) β the number of cities on the street.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \leq c_i \leq n\)) β the colors of the houses.It is guaranteed that there is at least one pair of indices \(i\) and \(j\) so that \(1 \l... | Print a single integer β the maximum possible distance Ilya can walk. | In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of \(5-1 = 4\) units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of \(1\) unit.In the t... | Input: 5 1 2 3 2 3 | Output: 4 | Easy | 2 | 965 | 359 | 69 | 11 |
1,943 | E2 | 1943E2 | E2. MEX Game 2 (Hard Version) | 3,300 | binary search; greedy; two pointers | This is the hard version of the problem. The only difference between the two versions is the constraint on \(t\), \(m\) and the sum of \(m\). You can make hacks only if both versions of the problem are solved.Alice and Bob play yet another game on an array \(a\) of size \(n\). Alice starts with an empty array \(c\). Bo... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(m\) and \(k\) (\(1 \le m \le 2 \cdot 10^5, 1 \le k \le 10^9\)).The second line co... | For each test case, find Alice's score if both players play optimally. | In the first test case, the array \(a\) is \([0, 0, 0, 0, 1, 1, 1, 1, 1]\). A possible game with a score of \(2\) is as follows: Alice chooses the element \(0\). After this move, \(a = [0, 0, 0, 1, 1, 1, 1, 1]\) and \(c=[0]\). Bob chooses to remove the \(3\) elements \(0\), \(0\) and \(1\). After this move, \(a = [0, 1... | Input: 51 44 52 10000000001000000000 1000000000 10000000003 22 3 100 11 12 23 11 1 1 1 | Output: 2 1 3 2 1 | Master | 3 | 1,618 | 484 | 70 | 19 |
1,251 | B | 1251B | B. Binary Palindromes | 1,400 | greedy; strings | A palindrome is a string \(t\) which reads the same backward as forward (formally, \(t[i] = t[|t| + 1 - i]\) for all \(i \in [1, |t|]\)). Here \(|t|\) denotes the length of a string \(t\). For example, the strings 010, 1001 and 0 are palindromes.You have \(n\) binary strings \(s_1, s_2, \dots, s_n\) (each \(s_i\) consi... | The first line contains single integer \(Q\) (\(1 \le Q \le 50\)) β the number of test cases.The first line on each test case contains single integer \(n\) (\(1 \le n \le 50\)) β the number of binary strings you have.Next \(n\) lines contains binary strings \(s_1, s_2, \dots, s_n\) β one per line. It's guaranteed that ... | Print \(Q\) integers β one per test case. The \(i\)-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the \(i\)-th test case. | In the first test case, \(s_1\) is palindrome, so the answer is \(1\).In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make \(s_1 = \text{0110}\), \(s_2 = \text{111111}\) and \(s_3 = \text{010000}\).In the third t... | Input: 4 1 0 3 1110 100110 010101 2 11111 000001 2 001 11100111 | Output: 1 2 2 2 | Easy | 2 | 933 | 390 | 215 | 12 |
65 | A | 65A | A. Harry Potter and Three Spells | 1,800 | implementation; math | A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert a grams of sand into b grams of lead, the second one allows you to convert c grams of lead into d grams of gold a... | The first line contains 6 integers a, b, c, d, e, f (0 β€ a, b, c, d, e, f β€ 1000). | Print ""Ron"", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print ""Hermione"". | Consider the first sample. Let's start with the 500 grams of sand. Apply the first spell 5 times and turn the sand into 1000 grams of lead. Then apply the second spell 4 times to get 600 grams of gold. Letβs take 400 grams from the resulting amount of gold turn them back into sand. We get 500 grams of sand and 200 gram... | Input: 100 200 250 150 200 250 | Output: Ron | Medium | 2 | 1,658 | 82 | 207 | 0 |
1,366 | A | 1366A | A. Shovels and Swords | 1,100 | binary search; greedy; math | Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types β shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many e... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains two integers \(a\) and \(b\) (\(0 \le a, b \le 10^9\)) β the number of sticks and the number of diamonds, respectively. | For each test case print one integer β the maximum number of emeralds Polycarp can earn. | In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.In the second test case Polycarp does not have any diamonds, so he cannot craft anything. | Input: 4 4 4 1000000000 0 7 15 8 7 | Output: 2 0 7 5 | Easy | 3 | 389 | 252 | 88 | 13 |
977 | B | 977B | B. Two-gram | 900 | implementation; strings | Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, ""AZ"", ""AA"", ""ZA"" β three distinct two-grams.You are given a string \(s\) consisting of \(n\) capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive ch... | The first line of the input contains integer number \(n\) (\(2 \le n \le 100\)) β the length of string \(s\). The second line of the input contains the string \(s\) consisting of \(n\) capital Latin letters. | Print the only line containing exactly two capital Latin letters β any two-gram contained in the given string \(s\) as a substring (i.e. two consecutive characters of the string) maximal number of times. | In the first example ""BA"" is also valid answer.In the second example the only two-gram ""ZZ"" can be printed because it contained in the string ""ZZZAA"" two times. | Input: 7ABACABA | Output: AB | Beginner | 2 | 597 | 207 | 203 | 9 |
723 | C | 723C | C. Polycarp at the Radio | 1,600 | greedy | Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the gro... | The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song. | In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.In the second line print the changed playlist.If there ar... | In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes... | Input: 4 21 2 3 2 | Output: 2 11 2 1 2 | Medium | 1 | 748 | 188 | 358 | 7 |
2,020 | D | 2020D | D. Connect the Dots | 1,800 | brute force; dp; dsu; graphs; math; trees | One fine evening, Alice sat down to play the classic game ""Connect the Dots"", but with a twist.To play the game, Alice draws a straight line and marks \(n\) points on it, indexed from \(1\) to \(n\). Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs \(m\) operations... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)).The \(i\)-th of the followi... | For each test case, output the number of connected components. | In the first test case, there are \(n = 10\) points. The first operation joins the points \(1\), \(3\), \(5\), \(7\), and \(9\). The second operation joins the points \(2\), \(4\), \(6\), \(8\), and \(10\). There are thus two connected components: \(\{1, 3, 5, 7, 9\}\) and \(\{2, 4, 6, 8, 10\}\).In the second test case... | Input: 310 21 2 42 2 4100 119 2 4100 31 2 57 2 617 2 31 | Output: 2 96 61 | Medium | 6 | 859 | 593 | 62 | 20 |
8 | D | 8D | D. Two Friends | 2,600 | binary search; geometry | Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it.Bob wants ... | The first line contains two integers: t1, t2 (0 β€ t1, t2 β€ 100). The second line contains the cinema's coordinates, the third one β the house's, and the last line β the shop's. All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building... | In the only line output one number β the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places. | Input: 0 20 04 0-3 0 | Output: 1.0000000000 | Expert | 2 | 1,270 | 321 | 179 | 0 | |
345 | D | 345D | D. Chain Letter | 2,200 | *special; dfs and similar; graphs | A chain letter is a kind of a message which urges the recipient to forward it to as many contacts as possible, usually with some kind of mystic explanation. Of course, this is only a superstition, and you don't believe in it, but all your friends do. You know that today there will be one of these letters going around, ... | The first line of the input contains an integer n (2 β€ n β€ 50) β the number of people involved. Next n following lines contain elements of f, strings of length n. Each character in f is either ""0"" or ""1"". It's guaranteed that two following equations hold: f[i][j] = f[j][i], f[i][i] = 0, for all i, j (1 β€ i, j β€ n). | Output a single integer β the number of copies of the letter you will receive eventually. | In the first case, everybody sends letters to everyone, so you get copies from all three of your friends.In the second case, you don't know any of these people, so they don't bother you with their superstitious stuff.In the third case, two of your friends send you copies of the letter but the third friend doesn't know ... | Input: 40111101111011110 | Output: 3 | Hard | 3 | 943 | 320 | 89 | 3 |
630 | J | 630J | J. Divisibility | 1,100 | math; number theory | IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is divisible by all numbers from 2 to 10 every developer ... | The only line of the input contains one integer n (1 β€ n β€ 1018) β the prediction on the number of people who will buy the game. | Output one integer showing how many numbers from 1 to n are divisible by all numbers from 2 to 10. | Input: 3000 | Output: 1 | Easy | 2 | 653 | 128 | 98 | 6 | |
1,901 | B | 1901B | B. Chip and Ribbon | 1,100 | greedy; math | There is a ribbon divided into \(n\) cells, numbered from \(1\) to \(n\) from left to right. Initially, an integer \(0\) is written in each cell.Monocarp plays a game with a chip. The game consists of several turns. During the first turn, Monocarp places the chip in the \(1\)-st cell of the ribbon. During each turn exc... | 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 \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le 10^9\); \(c_1 \ge 1\)). It can be ... | For each test case, print one integer β the minimum number of times Monocarp has to teleport the chip. | In the first test case of the example, Monocarp can perform the turns as follows: place the chip in the \(1\)-st cell; the numbers in the cells are \([1, 0, 0, 0]\); move the chip to the next (\(2\)-nd) cell; the numbers in the cells are \([1, 1, 0, 0]\); move the chip to the next (\(3\)-rd) cell; the numbers in the ce... | Input: 441 2 2 151 0 1 0 155 4 3 2 1112 | Output: 1 2 4 11 | Easy | 2 | 1,119 | 605 | 102 | 19 |
1,571 | A | 1571A | A. Sequence of Comparisons | 800 | *special | Once upon a time, Petya had an array of integers \(a\) of length \(n\). But over time, the array itself was lost, and only \(n-1\) results of comparisons of neighboring array elements remained. In other words, for every \(i\) from \(1\) to \(n-1\), Petya knows exactly one of these three facts: \(a_i < a_{i+1}\); \(a_i ... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The only line of the test case contains the string \(s\) (\(1 \le |s| \le 100\)), where \(s_i\) is: <, if \(a_i < a_{i + 1}\); >, if \(a_i > a_{i + 1}\); =, if \(a_i = a_{i + 1}\). | For each test case, print a single string equal to: <, if \(a_1 < a_n\); >, if \(a_1 > a_n\); =, if \(a_1 = a_n\); ?, if it is impossible to uniquely determine the result of the comparison. | Consider the test cases of the example: in the first test case, it's easy to see that \(a_1 > a_4\) since \(a_1 > a_2 > a_3 > a_4\); in the second test case, both sequences \([1, 2, 0, 10, 10, 15]\) and \([10, 11, 1, 2, 2, 5]\) meet the constraints; in the first one, \(a_1 < a_6\), and in the second one, \(a_1 > a_6\),... | Input: 4 >>> <><=< = <<== | Output: > ? = < | Beginner | 1 | 588 | 276 | 189 | 15 |
1,395 | A | 1395A | A. Boboniu Likes to Color Balls | 1,000 | brute force; math | Boboniu gives you \(r\) red balls, \(g\) green balls, \(b\) blue balls, \(w\) white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palind... | The first line contains one integer \(T\) (\(1\le T\le 100\)) denoting the number of test cases.For each of the next \(T\) cases, the first line contains four integers \(r\), \(g\), \(b\) and \(w\) (\(0\le r,g,b,w\le 10^9\)). | For each test case, print ""Yes"" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print ""No"". | In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.In the second test case, after doing one operation, changing \((8,1,9,3)\) to \((7,0,8,6)\), one of those possible palindromes may be ""rrrwwwbbbbrbbbbwwwrrr"".A palindrome is a word, p... | Input: 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 | Output: No Yes Yes Yes | Beginner | 2 | 386 | 225 | 186 | 13 |
1,458 | E | 1458E | E. Nim Shortcuts | 3,100 | data structures; games | After your debut mobile game ""Nim"" blew up, you decided to make a sequel called ""Nim 2"". This game will expand on the trusted Nim game formula, adding the much awaited second heap! In the game, there are two heaps, each containing a non-negative number of stones. Two players make moves in turn. On their turn, a pla... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β the number of shortcut positions, and the number of initial positions that need to be evaluated.The following \(n\) lines describe shortcut positions. The \(i\)-th of these lines contains two integers \(x_i, y_i\) (\(0 \leq x_i, y_i \leq... | For each initial position, on a separate line print ""WIN"" if the starting player is able to win from this position, and ""LOSE"" otherwise. | Input: 3 5 3 0 0 1 2 2 0 0 1 1 2 2 3 3 5 4 | Output: LOSE WIN LOSE WIN LOSE | Master | 2 | 1,483 | 741 | 141 | 14 | |
910 | A | 910A | A. The Way to Home | 800 | dfs and similar; dp; greedy; implementation | A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.For each point from 1 to n is known if there is a... | The first line contains two integers n and d (2 β€ n β€ 100, 1 β€ d β€ n - 1) β the point, which the frog wants to reach, and the maximal length of the frog jump.The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point ther... | If the frog can not reach the home, print -1.In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1. | In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).In the second example the frog can not reach home, because to make it she need to jump on a di... | Input: 8 410010101 | Output: 2 | Beginner | 4 | 650 | 490 | 177 | 9 |
1,548 | D1 | 1548D1 | D1. Gregor and the Odd Cows (Easy) | 2,300 | bitmasks; geometry; math; number theory | This is the easy version of the problem. The only difference from the hard version is that in this version all coordinates are even.There are \(n\) fence-posts at distinct coordinates on a plane. It is guaranteed that no three fence posts lie on the same line.There are an infinite number of cows on the plane, one at ev... | The first line contains the integer \(n\) (\(3 \le n \le 6000\)), the number of fence posts which Gregor can choose to form the vertices of a fence.Each of the next \(n\) line contains two integers \(x\) and \(y\) (\(0 \le x,y \le 10^7\), \(x\) and \(y\) are even), where \((x,y)\) is the coordinate of a fence post. All... | Print a single integer, the number of interesting fences. Two fences are considered different if they are constructed with a different set of three fence posts. | In the first example, there is only \(1\) fence. That fence is interesting since its area is \(4\) and there is \(1\) enclosed cow, marked in red. In the second example, there are \(3\) interesting fences. \((0,0)\) β \((30,14)\) β \((2,10)\) \((2,16)\) β \((30,14)\) β \((2,10)\) \((30,14)\) β \((4,6)\) β \((2,10)\) | Input: 3 0 0 2 0 0 4 | Output: 1 | Expert | 4 | 695 | 404 | 160 | 15 |
1,534 | F1 | 1534F1 | F1. Falling Sand (Easy Version) | 2,500 | dfs and similar; graphs; greedy | This is the easy version of the problem. The difference between the versions is the constraints on \(a_i\). You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with \(n\) ro... | The first line consists of two space-separated positive integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 400\,000\)).Each of the next \(n\) lines contains \(m\) characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of san... | Print one non-negative integer, the minimum amount of operations needed to solve the puzzle. | For example \(1\), by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that th... | Input: 5 7 #....#. .#.#... #....#. #....## #.#.... 4 1 1 1 0 3 1 | Output: 3 | Expert | 3 | 1,689 | 613 | 92 | 15 |
128 | B | 128B | B. String | 2,100 | brute force; constructive algorithms; hashing; implementation; string suffix structures; strings | One day in the IT lesson Anna and Maria learned about the lexicographic order.String x is lexicographically less than string y, if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that xi < yi, and for any j (1 β€ j < i) xj = yj. Here |a| denotes the length of the string a. The lexi... | The first line contains a non-empty string that only consists of small Latin letters (""a""-""z""), whose length does not exceed 105. The second line contains the only integer k (1 β€ k β€ 105). | Print the string Anna and Maria need β the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying ""No such line."" (without the quotes). | In the second sample before string ""bc"" follow strings ""a"", ""ab"", ""abc"", ""b"". | Input: aa2 | Output: a | Hard | 6 | 974 | 192 | 221 | 1 |
2,056 | F1 | 2056F1 | F1. Xor of Median (Easy Version) | 2,700 | bitmasks; brute force; combinatorics; dp; math | This is the easy version of the problem. The difference between the versions is that in this version, the constraints on \(t\), \(k\), and \(m\) are lower. You can hack only if you solved all versions of this problem. A sequence \(a\) of \(n\) integers is called good if the following condition holds: Let \(\text{cnt}_x... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows. The first line of each test case contains two integers \(k\) and \(m\) (\(1 \le k \le 200\), \(1 \le m \le 500\)) β the number of bits in \(n\) and the upper bo... | For each test case, output a single integer representing the bitwise XOR of the median of all good sequences \(a\) of length \(n\) where \(0\le a_i < m\). | In the first example, \(n = 10_2 = 2\) and \(m = 3\). All possible sequences with elements less than \(m\) are: \([0, 0]\), \([0, 1]\), \([0, 2]\), \([1, 0]\), \([1, 1]\), \([1, 2]\), \([2, 0]\), \([2, 1]\), \([2, 2]\). All of them are good, so the answer is: \(0 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \o... | Input: 62 3102 3115 1111017 9110101117 34110010100010100101 5001 | Output: 3 2 0 8 32 0 | Master | 5 | 1,166 | 575 | 154 | 20 |
1,657 | D | 1657D | D. For Gamers. By Gamers. | 2,000 | binary search; brute force; greedy; math; sortings | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has \(C\) coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with \(C\) coins.There... | The first line contains two integers \(n\) and \(C\) (\(1 \le n \le 3 \cdot 10^5\); \(1 \le C \le 10^6\)) β the number of types of units and the amount of coins Monocarp has before each battle.The \(i\)-th of the next \(n\) lines contains three integers \(c_i, d_i\) and \(h_i\) (\(1 \le c_i \le C\); \(1 \le d_i, h_i \l... | Print \(m\) integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than \(C\), then print \(-1\). | Consider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster \(0.75\) seconds to kill each other. He can recruit two units for the cost of \(6\) coins and kill the monster in \(0.375\) second.Monocarp can recruit one unit of the second ... | Input: 3 10 3 4 6 5 5 5 10 3 4 3 8 3 5 4 10 15 | Output: 5 3 -1 | Hard | 5 | 1,377 | 589 | 174 | 16 |
1,153 | A | 1153A | A. Serval and Bus | 1,000 | brute force; math | It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, th... | The first line contains two space-separated integers \(n\) and \(t\) (\(1\leq n\leq 100\), \(1\leq t\leq 10^5\)) β the number of bus routes and the time Serval goes to the station. Each of the next \(n\) lines contains two space-separated integers \(s_i\) and \(d_i\) (\(1\leq s_i,d_i\leq 10^5\)) β the time when the fir... | Print one number β what bus route Serval will use. If there are several possible answers, you can print any of them. | In the first example, the first bus of the first route arrives at time \(6\), and the first bus of the second route arrives at time \(9\), so the first route is the answer.In the second example, a bus of the third route arrives at time \(5\), so it is the answer.In the third example, buses of the first route come at ti... | Input: 2 2 6 4 9 5 | Output: 1 | Beginner | 2 | 851 | 398 | 116 | 11 |
1,452 | B | 1452B | B. Toy Blocks | 1,400 | binary search; greedy; math; sortings | You are asked to watch your nephew who likes to play with toy blocks in a strange way.He has \(n\) boxes and the \(i\)-th box has \(a_i\) blocks. His game consists of two steps: he chooses an arbitrary box \(i\); he tries to move all blocks from the \(i\)-th box to other boxes. If he can make the same number of blocks ... | 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 the integer \(n\) (\(2 \le n \le 10^5\)) β the number of boxes.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^9\)) β the numb... | For each test case, print a single integer β the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite. | In the first test case, you can, for example, put one extra block into the first box and make \(a = [4, 2, 2]\). If your nephew chooses the box with \(4\) blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with \(2\) blocks then he will move these two blocks to... | Input: 3 3 3 2 2 4 2 2 3 2 3 0 3 0 | Output: 1 0 3 | Easy | 4 | 761 | 423 | 176 | 14 |
1,402 | A | 1402A | A. Fancy Fence | 1,800 | *special; data structures; dsu; implementation; math; sortings | Everybody knows that BalΓ‘zs has the fanciest fence in the whole town. It's built up from \(N\) fancy sections. The sections are rectangles standing closely next to each other on the ground. The \(i\)th section has integer height \(h_i\) and integer width \(w_i\). We are looking for fancy rectangles on this fancy fence.... | The first line contains \(N\) (\(1\leq N \leq 10^{5}\)) β the number of sections. The second line contains \(N\) space-separated integers, the \(i\)th number is \(h_i\) (\(1 \leq h_i \leq 10^{9}\)). The third line contains \(N\) space-separated integers, the \(i\)th number is \(w_i\) (\(1 \leq w_i \leq 10^{9}\)). | You should print a single integer, the number of fancy rectangles modulo \(10^9+7\). So the output range is \(0,1,2,\ldots, 10^9+6\). | The fence looks like this: There are 5 fancy rectangles of shape: There are 3 fancy rectangles of shape: There is 1 fancy rectangle of shape: There are 2 fancy rectangles of shape: There is 1 fancy rectangle of shape: | Input: 2 1 2 1 2 | Output: 12 | Medium | 6 | 709 | 314 | 133 | 14 |
33 | C | 33C | C. Wonderful Randomized Sum | 1,800 | greedy | Learn, learn and learn again β Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite th... | The first line contains integer n (1 β€ n β€ 105) β amount of elements in the sequence. The second line contains n integers ai ( - 104 β€ ai β€ 104) β the sequence itself. | The first and the only line of the output should contain the answer to the problem. | Input: 3-1 -2 -3 | Output: 6 | Medium | 1 | 1,116 | 167 | 83 | 0 | |
1,324 | C | 1324C | C. Frog Jumps | 1,100 | binary search; data structures; dfs and similar; greedy; implementation | There is a frog staying to the left of the string \(s = s_1 s_2 \ldots s_n\) consisting of \(n\) characters (to be more precise, the frog initially stays at the cell \(0\)). Each character of \(s\) is either 'L' or 'R'. It means that if the frog is staying at the \(i\)-th cell and the \(i\)-th character is 'L', the fro... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The next \(t\) lines describe test cases. The \(i\)-th test case is described as a string \(s\) consisting of at least \(1\) and at most \(2 \cdot 10^5\) characters 'L' and 'R'.It is guaranteed that the sum of lengt... | For each test case, print the answer β the minimum possible value of \(d\) such that the frog can reach the cell \(n+1\) from the cell \(0\) if it jumps by no more than \(d\) at once. | The picture describing the first test case of the example and one of the possible answers:In the second test case of the example, the frog can only jump directly from \(0\) to \(n+1\).In the third test case of the example, the frog can choose \(d=3\), jump to the cell \(3\) from the cell \(0\) and then to the cell \(4\... | Input: 6 LRLRRLL L LLR RRRR LLLLLL R | Output: 3 2 3 1 7 1 | Easy | 5 | 1,369 | 419 | 183 | 13 |
1,659 | A | 1659A | A. Red Versus Blue | 1,000 | constructive algorithms; greedy; implementation; math | Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of \(n\) matches.In the end, it turned out Team Red won \(r\) times and Team Blue won \(b\) times. Team Blue was less skilled than Team Red, so \(b\) was strictly less than \(r\).You missed the stream s... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case has a single line containing three integers \(n\), \(r\), and \(b\) (\(3 \leq n \leq 100\); \(1 \leq b < r \leq n\), \(r+b=n\)). | For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any. | The first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is \(1\). We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in ... | Input: 37 4 36 5 119 13 6 | Output: RBRBRBR RRRBRR RRBRRBRRBRRBRRBRRBR | Beginner | 4 | 895 | 240 | 135 | 16 |
2,124 | D | 2124D | D. Make a Palindrome | 1,700 | greedy; sortings; two pointers | You are given an array \(a\) of size \(n\), and an integer \(k\). You may perform the following operation any number of times: Select two integers \(l\) and \(r\) \((1 \le l \le r \le |a|)\) such that \(r-l+1 \geq k\). Then, select an index \(i\) such that \(l\leq i \leq r\) where \(a_i\) is the \(k\)-th smallest numbe... | 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 contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2\cdot 10^5\)).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(... | For each test case, output YES if it is possible to create a palindrome, and NO otherwise. You can output in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, \(a\) is already a palindrome.In the second test case, we can perform two operations as follows: \([\mathbf{1,1},2,1]\rightarrow [1,\mathbf{2},1]\rightarrow[1,1]\)In the third test case, we can perform one operation as follows: \([\mathbf{2,3,4,5,3,2}]\rightarrow[2,3,4,3,2]\).In the fourth and f... | Input: 85 35 4 3 4 54 11 1 2 16 62 3 4 5 3 25 45 2 4 3 18 54 7 1 2 3 1 3 45 41 2 1 2 23 31 2 24 42 1 2 2 | Output: YES YES YES NO NO YES NO YES | Medium | 3 | 931 | 459 | 241 | 21 |
207 | D4 | 207D4 | D4. The Beaver's Problem - 3 | 2,100 | The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj... | The first line contains integer id (0 β€ id β€ 106) β the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in... | Print an integer from 1 to 3, inclusive β the number of the subject the given document corresponds to. | Hard | 0 | 1,472 | 653 | 102 | 2 | |||
285 | D | 285D | D. Permutation Sum | 1,900 | bitmasks; combinatorics; dp; implementation; meet-in-the-middle | Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.Petya decided to introduce the sum operation on the set ... | The single line contains integer n (1 β€ n β€ 16). | In the single line print a single non-negative integer β the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7). | Input: 3 | Output: 18 | Hard | 5 | 1,124 | 48 | 185 | 2 | |
2,049 | F | 2049F | F. MEX OR Mania | 2,700 | bitmasks; brute force; data structures; dsu; implementation | An integer sequence \(b_1, b_2, \ldots, b_n\) is good if \(\operatorname{mex}(b_1, b_2, \ldots, b_n) - (b_1 | b_2 | \ldots | b_n) = 1\). Here, \(\operatorname{mex(c)}\) denotes the MEX\(^{\text{β}}\) of the collection \(c\), and \(|\) is the bitwise OR operator.Shohag has an integer sequence \(a_1, a_2, \ldots, a_n\). ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two space-separated integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)).The second line of each test case contains \(... | For each test case, output \(q\) lines β on the \(i\)-th line output the length of the longest good subarray of \(a\) after the \(i\)-th update. | In the first test case, after the first update, the array becomes \([0, 0, 1, 0, 1, 1]\), and here the whole array is good because \(\operatorname{mex}([0, 0, 1, 0, 1, 1]) - (0 | 0 | 1 | 0 | 1 | 1) = 2 - 1 = 1\).After the second update, the array becomes \([0, 0, 3, 0, 1, 1]\), and here the subarray \([0, 1, 1]\) has t... | Input: 26 30 0 1 0 1 06 13 26 33 11 3 11 1 | Output: 6 3 2 0 | Master | 5 | 943 | 659 | 144 | 20 |
46 | F | 46F | F. Hercule Poirot Problem | 2,300 | dsu; graphs | Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only ... | The first line contains three preset integers n, m ΠΈ k (1 β€ n, m, k β€ 1000) β the number of rooms, the number of doors and the number of house residents respectively. The next m lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to n. There cannot be more that one door... | Print ""YES"" (without quotes) if the second arrangement can result from the first one, otherwise, print ""NO"". | Input: 2 1 21 2Dmitry 1 1 1Natalia 2 0Natalia 1 1 1Dmitry 2 0 | Output: YES | Expert | 2 | 1,452 | 1,315 | 112 | 0 | |
739 | C | 739C | C. Alyona and towers | 2,500 | data structures | Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ 1 Γ 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes... | The first line contain single integer n (1 β€ n β€ 3Β·105) β the number of towers.The second line contain n integers a1, a2, ..., an (1 β€ ai β€ 109) β the number of cubes in each tower. The third line contain single integer m (1 β€ m β€ 3Β·105) β the number of additions.The next m lines contain 3 integers each. The i-th of th... | Print m lines. In i-th line print the maximum width of the hills after the i-th addition. | The first sample is as follows:After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.After addition of 1 cube on the second tower, the number of cubes in the t... | Input: 55 5 5 5 531 3 22 2 14 4 1 | Output: 245 | Expert | 1 | 879 | 476 | 89 | 7 |
1,030 | D | 1030D | D. Vasya and Triangle | 1,800 | geometry; number theory | Vasya has got three integers \(n\), \(m\) and \(k\). He'd like to find three integer points \((x_1, y_1)\), \((x_2, y_2)\), \((x_3, y_3)\), such that \(0 \le x_1, x_2, x_3 \le n\), \(0 \le y_1, y_2, y_3 \le m\) and the area of the triangle formed by these points is equal to \(\frac{nm}{k}\).Help Vasya! Find such points... | The single line contains three integers \(n\), \(m\), \(k\) (\(1\le n, m \le 10^9\), \(2 \le k \le 10^9\)). | If there are no such points, print ""NO"".Otherwise print ""YES"" in the first line. The next three lines should contain integers \(x_i, y_i\) β coordinates of the points, one point per line. If there are multiple solutions, print any of them.You can print each letter in any case (upper or lower). | In the first example area of the triangle should be equal to \(\frac{nm}{k} = 4\). The triangle mentioned in the output is pictured below: In the second example there is no triangle with area \(\frac{nm}{k} = \frac{16}{7}\). | Input: 4 3 3 | Output: YES1 02 34 1 | Medium | 2 | 392 | 107 | 298 | 10 |
1,933 | A | 1933A | A. Turtle Puzzle: Rearrange and Negate | 800 | greedy; math; sortings | You are given an array \(a\) of \(n\) integers. You must perform the following two operations on the array (the first, then the second): Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged. Choose at most one contiguous segment of elements and replace the signs of all elements i... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β the number of elements in array \(a\). The second line of each test case contai... | For each test case, output the maximum sum of the array elements after sequentially performing the two given operations. | In the first test case, you can first rearrange the array to get \([3,-2,-3]\) (operation 1), then choose \(l = 2, r = 3\) and get the sum \(3 + -((-2) + (-3)) = 8\) (operation 2).In the second test case, you can do nothing in both operations and get the sum \(0\).In the third test case, you can do nothing in both oper... | Input: 83-2 3 -31020 11-99410 -2 -3 75-1 -2 -3 -4 -56-41 22 -69 73 -15 -50121 2 3 4 5 6 7 8 9 10 11 12 | Output: 8 0 1 99 22 15 270 78 | Beginner | 3 | 735 | 415 | 120 | 19 |
340 | C | 340C | C. Tourist Problem | 1,600 | combinatorics; implementation; math | Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents t... | The first line contains integer n (2 β€ n β€ 105). Next line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 107). | Output two integers β the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. | Consider 6 possible routes: [2, 3, 5]: total distance traveled: |2 β 0| + |3 β 2| + |5 β 3| = 5; [2, 5, 3]: |2 β 0| + |5 β 2| + |3 β 5| = 7; [3, 2, 5]: |3 β 0| + |2 β 3| + |5 β 2| = 7; [3, 5, 2]: |3 β 0| + |5 β 3| + |2 β 5| = 8; [5, 2, 3]: |5 β 0| + |2 β 5| + |3 β 2| = 9; [5, 3, 2]: |5 β 0| + |3 β 5| + |2 β 3| = 8. The... | Input: 32 3 5 | Output: 22 3 | Medium | 3 | 1,361 | 119 | 144 | 3 |
1,102 | F | 1102F | F. Elongated Matrix | 2,000 | binary search; bitmasks; brute force; dp; graphs | You are given a matrix \(a\), consisting of \(n\) rows and \(m\) columns. Each cell contains an integer in it.You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the followi... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 16\), \(1 \le m \le 10^4\), \(2 \le nm\)) β the number of rows and the number of columns, respectively.Each of the next \(n\) lines contains \(m\) integers (\(1 \le a_{i, j} \le 10^9\)) β the description of the matrix. | Print a single integer \(k\) β the maximum number such that there exists some order of rows of matrix \(a\) that it produces an \(k\)-acceptable traversal. | In the first example you can rearrange rows as following to get the \(5\)-acceptable traversal:5 310 84 39 9Then the sequence \(s\) will be \([5, 10, 4, 9, 3, 8, 3, 9]\). Each pair of neighbouring elements have at least \(k = 5\) difference between them.In the second example the maximum \(k = 0\), any order is \(0\)-ac... | Input: 4 2 9 9 10 8 5 3 4 3 | Output: 5 | Hard | 5 | 857 | 286 | 155 | 11 |
1,988 | E | 1988E | E. Range Minimum Sum | 2,300 | binary search; brute force; data structures; divide and conquer; implementation | For an array \([a_1,a_2,\ldots,a_n]\) of length \(n\), define \(f(a)\) as the sum of the minimum element over all subsegments. That is, $$$\(f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.\)\(A permutation is a sequence of integers from \)1\( to \)n\( of length \)n\( containing each number exactly once. You are gi... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 5\cdot 10^5\)).The second line of each test case contains \(n\) distinct integers \(a_1,\ld... | For each test case, print one line containing \(n\) integers. The \(i\)-th integer should be the answer when erasing \(a_i\). | In the second test case, \(a=[3,1,2]\). When removing \(a_1\), \(b=[1,2]\). \(f(b)=1+2+\min\{1,2\}=4\). When removing \(a_2\), \(b=[3,2]\). \(f(b)=3+2+\min\{3,2\}=7\). When removing \(a_3\), \(b=[3,1]\). \(f(b)=3+1+\min\{3,1\}=5\). | Input: 41133 1 254 2 1 5 388 1 4 6 7 3 5 2 | Output: 0 4 7 5 19 21 27 17 19 79 100 72 68 67 80 73 80 | Expert | 5 | 574 | 434 | 125 | 19 |
2,084 | F | 2084F | F. Skyscape | 2,900 | constructive algorithms; data structures; greedy | You are given a permutation \(a\) of length \(n\)\(^{\text{β}}\).We say that a permutation \(b\) of length \(n\) is good if the two permutations \(a\) and \(b\) can become the same after performing the following operation at most \(n\) times (possibly zero): Choose two integers \(l, r\) such that \(1 \le l < r \le n\) ... | 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 5 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_... | For each test case: If it is impossible to find such a good permutation \(b\), output a single integer \(-1\). Otherwise, output \(n\) integers \(b_1, b_2, \ldots, b_n\) β the good permutation \(b\) you've found. You need to ensure that for all \(1 \le i \le n\), if \(c_i \ne 0\), then \(b_i = c_i\). If there are multi... | In the first test case, \(b = [1, 2]\) is a valid answer since after performing the following operation, \(a\) and \(b\) will become the same: Choose \(l = 1, r = 2\) and cyclically shift the subsegment \([a_1, a_2]\) one position to the right. Then \(a\) will become \([1, 2]\). In the second test case, \(b = [2, 3, 4,... | Input: 922 11 243 2 4 12 0 0 153 2 1 5 41 3 0 0 053 2 1 5 43 2 1 5 453 2 1 5 43 2 5 1 463 5 6 2 1 40 2 0 5 0 063 5 6 2 1 40 2 0 6 4 096 9 2 4 1 7 8 3 50 2 5 9 0 0 0 8 098 5 3 9 1 7 4 6 20 0 8 0 7 0 4 0 2 | Output: 1 2 2 3 4 1 1 3 2 4 5 3 2 1 5 4 -1 3 2 1 5 4 6 -1 -1 1 3 8 5 7 9 4 6 2 | Master | 3 | 1,292 | 700 | 352 | 20 |
513 | B1 | 513B1 | B1. Permutations | 1,400 | brute force | You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum:Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). | The single line of input contains two integers n and m (1 β€ m β€ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subp... | Output n number forming the required permutation. | In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order. | Input: 2 2 | Output: 2 1 | Easy | 1 | 219 | 502 | 49 | 5 |
1,650 | A | 1650A | A. Deletions of Two Adjacent Letters | 800 | implementation; strings | The string \(s\) is given, the string length is odd number. The string consists of lowercase letters of the Latin alphabet.As long as the string length is greater than \(1\), the following operation can be performed on it: select any two adjacent letters in the string \(s\) and delete them from the string. For example,... | The first line of input data contains an integer \(t\) (\(1 \le t \le 10^3\)) β the number of input test cases.The descriptions of the \(t\) cases follow. Each test case is represented by two lines: string \(s\), which has an odd length from \(1\) to \(49\) inclusive and consists of lowercase letters of the Latin alpha... | For each test case in a separate line output: YES, if the string \(s\) can be converted so that \(s=c\) is true; NO otherwise. You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response). | In the first test case, \(s\)=""abcde"". You need to get \(s\)=""c"". For the first operation, delete the first two letters, we get \(s\)=""cde"". In the second operation, we delete the last two letters, so we get the expected value of \(s\)=""c"".In the third test case, \(s\)=""x"", it is required to get \(s\)=""y"". ... | Input: 5abcdecabcdebxyaaaaaaaaaaaaaaaacontestt | Output: YES NO NO YES YES | Beginner | 2 | 1,043 | 422 | 256 | 16 |
1,002 | D3 | 1002D3 | D3. Oracle for majority function | 1,600 | *special | Implement a quantum oracle on 3 qubits which implements a majority function. Majority function on 3-bit vectors is defined as follows: if vector has two or three 1s, and 0 if it has zero or one 1s.For an explanation on how this type of quantum oracles works, see Introduction to quantum oracles.You have to implement an ... | Medium | 1 | 772 | 0 | 0 | 10 | ||||
1,352 | F | 1352F | F. Binary String Reconstruction | 1,500 | constructive algorithms; dfs and similar; math | For some binary string \(s\) (i.e. each character \(s_i\) is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length \(2\) were written. For each pair (substring of length \(2\)), the number of '1' (ones) in it was calculated.You are given three numbers:... | The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases in the input. Then test cases follow.Each test case consists of one line which contains three integers \(n_0, n_1, n_2\) (\(0 \le n_0, n_1, n_2 \le 100\); \(n_0 + n_1 + n_2 > 0\)). It is guaranteed that the answer for given \(n_0... | Print \(t\) lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. | Input: 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 | Output: 1110011110 0011 0110001100101011 10 0000111 1111 000 | Medium | 3 | 1,098 | 340 | 157 | 13 | |
1,779 | G | 1779G | G. The Game of the Century | 3,000 | constructive algorithms; graphs; shortest paths | The time has finally come, MKnez and Baltic are to host The Game of the Century. For that purpose, they built a village to lodge its participants.The village has the shape of an equilateral triangle delimited by three roads of length \(n\). It is cut into \(n^2\) smaller equilateral triangles, of side length \(1\), by ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows.The first line of each test case contains a positive integer \(n\) (\(1\leq n\leq 10^5\)) β the size of the triangular village's sides.Three lines follow,... | For each test case, print the minimum number of road segments for which you need to invert the traffic direction. | The first example corresponds to the picture in the statement. There exist multiple solutions that invert the traffic direction of exactly \(2\) road segments, but inverting only \(1\) road segment never makes it possible to reach every intersection from any other. One of the possible solutions is shown in the picture ... | Input: 3300100101010003111011100 | Output: 2 0 3 | Master | 3 | 1,102 | 1,182 | 113 | 17 |
2,125 | F | 2125F | F. Timofey and Docker | 3,000 | binary search; divide and conquer; dp | Not so long ago, a certain Timofey learned about docker and now wants to present a report about it at a conference. He has already prepared the text \(s\).There are \(n\) people attending the conference. The \(i\)-th attendee will understand Timofey's report if the number of occurrences of the word ""docker"" as a cont... | Each test consists of several test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains one string \(s\) (\(1 \le |s| \le 5 \cdot 10^{5}\)) β Timofey's text, which consists of lowercase La... | For each test case, print one integer β the minimum number of characters that need to be changed so that the topic is understood by the maximum number of attendees. | Let's consider the first test in more detail: in the first test case, it is possible to change all the characters '\(x\)' at the end of the string to the whole word ""\(docker\)"", so that all \(3\) people will understand the topic; in the second test case, it is possible to change some characters in \(s\) as follows: ... | Input: 2dockerdockerxxxxxx33 32 41 5ljglsjfkdieufj51 53 32 43 72 9 | Output: 6 11 | Master | 3 | 663 | 761 | 164 | 21 |
515 | B | 515B | B. Drazil and His Happy Friends | 1,300 | brute force; dsu; meet-in-the-middle; number theory | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl... | The first line contains two integer n and m (1 β€ n, m β€ 100).The second line contains integer b (0 β€ b β€ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 β€ xi < n), denoting the list of indices of happy boys.The third line conatins integer g (0 β€ g β€ ... | If Drazil can make all his friends become happy by this plan, print ""Yes"". Otherwise, print ""No"". | By we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this... | Input: 2 301 0 | Output: Yes | Easy | 4 | 706 | 580 | 101 | 5 |
830 | E | 830E | E. Perpetual Motion Machine | 3,100 | constructive algorithms; dp; graphs; implementation; math; trees | Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of elements, which work in the following way.Each element has one controller that can be set to any non-negative real value. If a controller is set on some value x, then the controller consumes x2 energy units per second. At the sam... | There are several (at least one) test cases in the input. The first line contains single integer β the number of test cases.There is an empty line before each test case. The first line of test case contains two integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of elements in the scheme and the number of wires.Af... | Print answer for each test case.For each test case print ""YES"" if it's possible to set the controllers in such a way that the consumed power is not greater than the power produced, and the required values on the next line. The settings should be integers from 0 to 106, inclusive, and at least one value should be diff... | In the first example it's possible to set the controllers in the required way, for example, in the following way: set 1 on the first element, set 2 on the second and on the third, set 1 on the fourth. The consumed power is then equal to 12 + 22 + 22 + 12 = 10 energy units per second, the produced power is equal to 1Β·2 ... | Input: 4 4 41 22 33 44 2 3 22 33 1 4 61 23 44 21 41 33 2 10 92 13 25 26 22 72 82 92 104 2 | Output: YES1 2 2 1NOYES1 1 1 1YES1 5 1 1 1 1 1 1 1 1 | Master | 6 | 1,011 | 671 | 470 | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.