contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,461 | F | 1461F | F. Mathematical Expression | 2,700 | constructive algorithms; dp; greedy | Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw \(n\) numbers \(a_1, a_2, \ldots, a_n\) without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between... | The first line of the input contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the amount of numbers on the paper.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 9\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains the string \(s... | Print \(n\) numbers separated by \(n - 1\) symbols β a mathematical expression with the greatest result. If there are multiple equally valid results β output any one of them. | The following answers also fit the first example: ""2+2+0"", ""2+2-0"", ""2*2+0"". | Input: 3 2 2 0 +-* | Output: 2*2-0 | Master | 3 | 923 | 533 | 174 | 14 |
1,428 | B | 1428B | B. Belted Rooms | 1,200 | graphs; implementation | In the snake exhibition, there are \(n\) rooms (numbered \(0\) to \(n - 1\)) arranged in a circle, with a snake in each room. The rooms are connected by \(n\) conveyor belts, and the \(i\)-th conveyor belt connects the rooms \(i\) and \((i+1) \bmod n\). In the other words, rooms \(0\) and \(1\), \(1\) and \(2\), \(\ldo... | 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 the test cases follows. The first line of each test case description contains a single integer \(n\) (\(2 \le n \le 300\,000\)): the number of rooms. The next line ... | For each test case, output the number of returnable rooms. | In the first test case, all rooms are returnable except room \(2\). The snake in the room \(2\) is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts. | Input: 4 4 -><- 5 >>>>> 3 <-- 2 <> | Output: 3 5 3 0 | Easy | 2 | 1,001 | 705 | 58 | 14 |
216 | D | 216D | D. Spider's Web | 1,700 | binary search; sortings; two pointers | Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web. There are n main threads going from the center of the web. All main threads are located in one plane and divide it into n equal infinite sectors. The sectors are indexed from... | The first line contains integer n (3 β€ n β€ 1000) β the number of main threads.The i-th of following n lines describe the bridges located in the i-th sector: first it contains integer ki (1 β€ ki β€ 105) equal to the number of bridges in the given sector. Then follow ki different integers pij (1 β€ pij β€ 105; 1 β€ j β€ ki). ... | Print a single integer β the number of unstable cells in Paw the Spider's web. | Input: 73 1 6 74 3 5 2 92 8 14 3 7 6 43 2 5 93 6 3 83 4 2 9 | Output: 6 | Medium | 3 | 1,695 | 642 | 78 | 2 | |
1,494 | A | 1494A | A. ABC String | 900 | bitmasks; brute force; implementation | You are given a string \(a\), consisting of \(n\) characters, \(n\) is even. For each \(i\) from \(1\) to \(n\) \(a_i\) is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic ... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The only line of each testcase contains a string \(a\). \(a\) consists only of uppercase letters 'A', 'B' and 'C'. Let \(n\) be the length of \(a\). It is guaranteed that \(n\)... | For each testcase print ""YES"" if there exists such a string \(b\) that: \(b\) is a regular bracket sequence; if for some \(i\) and \(j\) (\(1 \le i, j \le n\)) \(a_i=a_j\), then \(b_i=b_j\). Otherwise, print ""NO"".You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are... | In the first testcase one of the possible strings \(b\) is ""(())()"".In the second testcase one of the possible strings \(b\) is ""()()"". | Input: 4 AABBAC CACA BBBBAC ABCA | Output: YES YES NO NO | Beginner | 3 | 1,042 | 352 | 356 | 14 |
78 | D | 78D | D. Archer's Shot | 2,300 | binary search; geometry; math; two pointers | A breakthrough among computer games, ""Civilization XIII"", is striking in its scale and elaborate details. Let's take a closer look at one of them.The playing area in the game is split into congruent cells that are regular hexagons. The side of each cell is equal to 1. Each unit occupies exactly one cell of the playin... | The first and only line of input contains a single positive integer k β the archer's shot range (1 β€ k β€ 106). | Print the single number, the number of cells that are under fire.Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). | Input: 3 | Output: 7 | Expert | 4 | 988 | 110 | 227 | 0 | |
1,708 | B | 1708B | B. Difference of GCDs | 1,100 | constructive algorithms; math | You are given three integers \(n\), \(l\), and \(r\). You need to construct an array \(a_1,a_2,\dots,a_n\) (\(l\le a_i\le r\)) such that \(\gcd(i,a_i)\) are all distinct or report there's no solution.Here \(\gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\). | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases. The description of the test cases follows.The first line contains three integers \(n\), \(l\), \(r\) (\(1 \le n \le 10^5\), \(1\le l\le r\le 10^9\)).It is guaranteed that the sum of... | For each test case, if there is no solution, print ""NO"" (without quotes). You can print letters in any case (upper or lower).Otherwise, print ""YES"" (without quotes). In the next line, print \(n\) integers \(a_1,a_2,\ldots,a_n\) β the array you construct.If there are multiple solutions, you may output any. | In the first test case, \(\gcd(1,a_1),\gcd(2,a_2),\ldots,\gcd(5,a_5)\) are equal to \(1\), \(2\), \(3\), \(4\), \(5\), respectively. | Input: 4 5 1 5 9 1000 2000 10 30 35 1 1000000000 1000000000 | Output: YES 1 2 3 4 5 YES 1145 1926 1440 1220 1230 1350 1001 1000 1233 NO YES 1000000000 | Easy | 2 | 290 | 372 | 310 | 17 |
1,896 | A | 1896A | A. Jagged Swaps | 800 | sortings | You are given a permutation\(^\dagger\) \(a\) of size \(n\). You can do the following operation Select an index \(i\) from \(2\) to \(n - 1\) such that \(a_{i - 1} < a_i\) and \(a_i > a_{i+1}\). Swap \(a_i\) and \(a_{i+1}\). Determine whether it is possible to sort the permutation after a finite number of operations.\(... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 10\)) β the size of the permutation.The second line of each test case contains \(n\... | For each test case, print ""YES"" if it is possible to sort the permutation, and ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer). | In the first test case, the permutation is already sorted.In the second test case, we can choose index \(i=2\) as \(1<3\) and \(3>2\) to form \([1, 2, 3, 5, 4]\). Then, we can choose index \(i=4\) as \(3<5\) and \(5>4\) to form \([1, 2, 3, 4, 5]\).In the third test case, it can be proven that it is impossible to sort t... | Input: 631 2 351 3 2 5 455 4 3 2 133 1 242 3 1 455 1 2 3 4 | Output: YES YES NO NO NO NO | Beginner | 1 | 638 | 415 | 228 | 18 |
1,922 | E | 1922E | E. Increasing Subsequences | 1,800 | bitmasks; constructive algorithms; divide and conquer; greedy; math | Let's recall that an increasing subsequence of the array \(a\) is a sequence that can be obtained from it by removing some elements without changing the order of the remaining elements, and the remaining elements are strictly increasing (i. e \(a_{b_1} < a_{b_2} < \dots < a_{b_k}\) and \(b_1 < b_2 < \dots < b_k\)). Not... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains a single integer \(X\) (\(2 \le X \le 10^{18}\)). | For each query, print the answer to it. If it is impossible to find the required array, print -1 on the first line. Otherwise, print a positive integer \(n\) on the first line β the length of the array. On the second line, print \(n\) integers β the required array itself. If there are several answers, you can print any... | Input: 4251337 | Output: 1 0 3 0 1 0 5 2 2 3 4 2 7 -1 -1 0 0 2 3 -1 | Medium | 5 | 843 | 187 | 397 | 19 | |
142 | D | 142D | D. Help Shrek and Donkey 2 | 2,600 | games | Having learned (not without some help from the Codeforces participants) to play the card game from the previous round optimally, Shrek and Donkey (as you may remember, they too live now in the Kingdom of Far Far Away) have decided to quit the boring card games and play with toy soldiers.The rules of the game are as fol... | The first line contains space-separated integers n, m and k (1 β€ n, m, k β€ 100). Then n lines contain m characters each. These characters belong to the set {""-"", ""G"", ""R""}, denoting, respectively, a battlefield's free cell, a cell occupied by Shrek's soldiers and a cell occupied by Donkey's soldiers.It is guarant... | Print ""First"" (without the quotes) if Shrek wins in the given Toy Soldier game. If Donkey wins, print ""Second"" (without the quotes). If the game continues forever, print ""Draw"" (also without the quotes). | Input: 2 3 1R-GRG- | Output: First | Expert | 1 | 2,634 | 374 | 209 | 1 | |
500 | C | 500C | C. New Year Book Reading | 1,600 | constructive algorithms; greedy; implementation; math | New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a c... | The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.The third line contains m space separate... | Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books. | Here's a picture depicting the example. Each vertical column presents the stacked books. | Input: 3 51 2 31 3 2 3 1 | Output: 12 | Medium | 4 | 1,258 | 452 | 120 | 5 |
1,065 | B | 1065B | B. Vasya and Isolated Vertices | 1,300 | constructive algorithms; graphs | Vasya has got an undirected graph consisting of \(n\) vertices and \(m\) edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of... | The only line contains two integers \(n\) and \(m~(1 \le n \le 10^5, 0 \le m \le \frac{n (n - 1)}{2})\).It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges. | In the only line print two numbers \(min\) and \(max\) β the minimum and maximum number of isolated vertices, respectively. | In the first example it is possible to construct a graph with \(0\) isolated vertices: for example, it should contain edges \((1, 2)\) and \((3, 4)\). To get one isolated vertex, we may construct a graph with edges \((1, 2)\) and \((1, 3)\). In the second example the graph will always contain exactly one isolated verte... | Input: 4 2 | Output: 0 1 | Easy | 2 | 649 | 227 | 123 | 10 |
1,468 | C | 1468C | C. Berpizza | 1,400 | data structures; implementation | Monocarp and Polycarp are working as waiters in Berpizza, a pizzeria located near the center of Bertown. Since they are waiters, their job is to serve the customers, but they choose whom they serve first differently.At the start of the working day, there are no customers at the Berpizza. They come there one by one. Whe... | The first line contains one integer \(q\) (\(2 \le q \le 5 \cdot 10^5\)) β the number of queries.Then \(q\) lines follow, each describing a query in one of the following formats: \(1\) \(m\) (\(1 \le m \le 5 \cdot 10^5\)) β a customer comes to Berpizza, and Polycarp estimates the amount of money that they will spend as... | For each query of type \(2\) or \(3\), print one integer β the number of the customer that has been served in that event. The customers are numbered in the order in which they come to the pizzeria, starting from \(1\). | Input: 8 1 8 1 10 1 6 3 2 1 9 2 3 | Output: 2 1 3 4 | Easy | 2 | 2,047 | 774 | 218 | 14 | |
683 | D | 683D | D. Chocolate Bar | 1,400 | *special; math | A chocolate bar has a rectangular shape and consists of n Γ m slices. In other words, a bar consists of n rows with m slices of chocolate in each row.Each slice of chocolate is known to weigh 1 gram. Your task is to determine for each of the q chocolate bars whether it is possible to obtain a piece weighing p grams by ... | The first line contains the positive integer q (1 β€ q β€ 100) β the number of chocolate bars. Each of the following q lines contains three positive integers n, m and p (1 β€ n, m, p β€ 1000) β the size of the chocolate bar, and the weight of the piece which should be obtained. | The output should contain q lines and the i-th line must contain ""Yes"" (without the quotes), if it is possible to perform the task for i-th chocolate bar, or ""No"" otherwise. | Input: 23 3 44 4 7 | Output: YesNo | Easy | 2 | 518 | 274 | 177 | 6 | |
148 | D | 148D | D. Bag of mice | 1,800 | dp; games; math; probabilities | The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chanc... | The only line of input data contains two integers w and b (0 β€ w, b β€ 1000). | Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. | Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β one black and one white; one... | Input: 1 3 | Output: 0.500000000 | Medium | 4 | 1,136 | 76 | 144 | 1 |
437 | E | 437E | E. The Child and Polygon | 2,500 | dp; geometry | This time our child has a simple polygon. He has to find the number of ways to split the polygon into non-degenerate triangles, each way must satisfy the following requirements: each vertex of each triangle is one of the polygon vertex; each side of the polygon must be the side of exactly one triangle; the area of inte... | The first line contains one integer n (3 β€ n β€ 200) β the number of vertices of the polygon. Then follow n lines, each line containing two integers. The i-th line contains xi, yi (|xi|, |yi| β€ 107) β the i-th vertex of the polygon in clockwise or counterclockwise order.It's guaranteed that the polygon is simple. | Output the number of ways modulo 1000000007 (109 + 7). | In the first sample, there are two possible splittings: In the second sample, there are only one possible splitting: | Input: 40 00 11 11 0 | Output: 2 | Expert | 2 | 731 | 313 | 54 | 4 |
750 | H | 750H | H. New Year and Snowy Grid | 3,500 | dfs and similar; dsu; graphs; interactive | Pay attention to the output section below, where you will see the information about flushing the output.Bearland is a grid with h rows and w columns. Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Every cell is either allowed (denoted by '.' in the input) or perma... | The first line of the input contains three integers h, w and q (2 β€ h, w β€ 1000, 1 β€ q β€ 10 000) β the height and the width of the grid, and the number of days, respectively.Next h lines describe which cells are allowed and which permanently blocked. The i-th line contains a string of length w, describing the i-th row.... | For each of q days print ""YES"" if that day is interesting, and otherwise print ""NO"", both without the quotes. After printing an answer, you have to both print the end-of-line character and flush the output. Then you can proceed to the next day. You can get Idleness Limit Exceeded if you don't print anything or if y... | In the first sample, there are 4 days. Drawings below show how Limak could go to school and return to his home in the second and the third day (on the left and on the right respectively). A permanently blocked cell is painted red, while cells temporarily blocked by snow are painted orange. Black and green arrows should... | Input: 3 5 4...........#...11 411 522 43 121 53 3 | Output: NOYESYESNO | Master | 4 | 1,747 | 1,020 | 563 | 7 |
989 | A | 989A | A. A Blend of Springtime | 900 | implementation; strings | When the curtains are opened, a canvas unfolds outside. Kanno marvels at all the blonde colours along the riverside β not tangerines, but blossoms instead.""What a pity it's already late spring,"" sighs Mino with regret, ""one more drizzling night and they'd be gone.""""But these blends are at their best, aren't they?"... | The first and only line of input contains a non-empty string \(s\) consisting of uppercase English letters 'A', 'B', 'C' and characters '.' (dots) only (\(\lvert s \rvert \leq 100\)) β denoting cells containing an amber flower, a buff one, a canary yellow one, and no flowers, respectively. | Output ""Yes"" if it's possible that all three colours appear in some cell, and ""No"" otherwise.You can print each letter in any case (upper or lower). | In the first example, the buff and canary yellow flowers can leave their petals in the central cell, blending all three colours in it.In the second example, it's impossible to satisfy the requirement because there is no way that amber and buff meet in any cell. | Input: .BAC. | Output: Yes | Beginner | 2 | 1,080 | 290 | 152 | 9 |
241 | A | 241A | A. Old Peykan | 1,300 | greedy | There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kil... | The first line of the input contains two space-separated integers m and k (1 β€ m, k β€ 1000). The value m specifies the number of roads between cities which is equal to n - 1.The next line contains m space-separated integers d1, d2, ..., dm (1 β€ di β€ 1000) and the following line contains m space-separated integers s1, s... | In the only line of the output print a single integer β the minimum time required for The Old Peykan to reach city cn from city c1. | In the second sample above, the Old Peykan stays in c1 for 3 hours. | Input: 4 61 2 5 22 3 3 4 | Output: 10 | Easy | 1 | 1,054 | 347 | 131 | 2 |
1,250 | J | 1250J | J. The Parade | 1,800 | binary search; greedy | The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into \(k\) rows, and all rows will contain the same number of soldiers.Of course, not every arrangement of soldiers into \(k\) rows is suitable. Heights of all soldiers in the same row ... | The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases. Then the test cases follow. Each test case begins with a line containing two integers \(n\) and \(k\) (\(1 \le n \le 30000\), \(1 \le k \le 10^{12}\)) β the number of different heights of soldiers and the number of rows of sol... | For each test case, print one integer β the maximum number of soldiers that can participate in the parade. | Explanations for the example test cases: the heights of soldiers in the rows can be: \([3, 3, 3, 3]\), \([1, 2, 1, 1]\), \([1, 1, 1, 1]\), \([3, 3, 3, 3]\) (each list represents a row); all soldiers can march in the same row; \(33\) soldiers with height \(1\) in each of \(3\) rows; all soldiers can march in the same ro... | Input: 5 3 4 7 1 13 1 1 100 1 3 100 2 1 1000000000000 1000000000000 4 1 10 2 11 1 | Output: 16 100 99 2000000000000 13 | Medium | 2 | 886 | 649 | 106 | 12 |
1,092 | A | 1092A | A. Uniform String | 800 | implementation | You are given two integers \(n\) and \(k\).Your task is to construct such a string \(s\) of length \(n\) that for each \(i\) from \(1\) to \(k\) there is at least one \(i\)-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. ... | The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of queries.The next \(t\) lines are contain queries, one per line. The \(i\)-th line contains two integers \(n_i\) and \(k_i\) (\(1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)\)) β the length of the string in the \(i\)-th query an... | Print \(t\) lines. In the \(i\)-th line print the answer to the \(i\)-th query: any string \(s_i\) satisfying the conditions in the problem statement with constraints from the \(i\)-th query. | In the first example query the maximum possible minimal frequency is \(2\), it can be easily seen that the better answer doesn't exist. Other examples of correct answers: ""cbcabba"", ""ccbbaaa"" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acce... | Input: 3 7 3 4 4 6 2 | Output: cbcacab abcd baabab | Beginner | 1 | 565 | 369 | 191 | 10 |
2,066 | A | 2066A | A. Object Identification | 1,400 | graphs; greedy; implementation; interactive | This is an interactive problem. You are given an array \(x_1, \ldots, x_n\) of integers from \(1\) to \(n\). The jury also has a fixed but hidden array \(y_1, \ldots, y_n\) of integers from \(1\) to \(n\). The elements of array \(y\) are unknown to you. Additionally, it is known that for all \(i\), \(x_i \neq y_i\), an... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. | In the first test case, \(x = [2,2,3]\), \(y = [1,3,1]\) and Object A is guessed.In the second test case, \(x = [5,1,4,2,3]\), \(y = [3,3,2,4,1]\) and Object B is guessed. | Input: 2 3 2 2 3 1 0 5 5 1 4 2 3 4 4 | Output: ? 2 3 ? 1 2 ! A ? 1 5 ? 5 1 ! B | Easy | 4 | 1,266 | 161 | 0 | 20 | |
1,302 | F | 1302F | F. Keep talking and nobody explodes β easy | 0 | bitmasks; brute force; expression parsing | This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You have the safe lock which consists of 5 decimal digits. If you rotate some digit, it increases by one, except 9 which becomes 0.Initially, the lock contains number \(x\). To unlock the safe you must do t... | Input contains single number \(x\) consisting of exactly 5 digits, leading zeroes are allowed. | Output the number after applying all operations. | Input: 00000 | Output: 61376 | Beginner | 3 | 3,113 | 94 | 48 | 13 | |
187 | E | 187E | E. Heaven Tour | 2,900 | data structures; greedy | The story was not finished as PMP thought. God offered him one more chance to reincarnate and come back to life. But before he can come back, God told him that PMP should ask n great men including prominent programmers about their life experiences.The men are standing on a straight line. They are numbered 1 through n f... | The first line of input contains three space-separated integers n, l, s (2 β€ n β€ 105, 0 β€ l < n, 1 β€ s β€ n) β the number of people to visit, the number left tickets PMP got, and initial location of PMP. Next line contains n space-separated integers. The i-th integer in this line is xi (0 = x1 < x2 < ... < xn β€ 109) β t... | If PMP cannot visit all men with the tickets he got print -1 in the only line of output. Otherwise, in the first line you should print the minimum time PMP can visit all men. In the second line you should print n - 1 integers that are the numbers of the men that PMP should visit in order in one optimal solution. If the... | Let us remind here, a great contestant of all times, who left us about a year ago. May Renat Mullakhanov rest in peace. | Input: 5 2 20 10 11 21 22 | Output: 331 3 5 4 | Master | 2 | 1,496 | 344 | 506 | 1 |
1,678 | B1 | 1678B1 | B1. Tokitsukaze and Good 01-String (easy version) | 800 | implementation | This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string \(s\) of length \(n\), consisting only of zeros and ones, \(n\) is even.Now Tokitsukaze divides \(s\) into the minimum nu... | The first contains a single positive integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of \(s\), it is guaranteed that \(n\) is even.The second line contains a binary string \(s\) of lengt... | For each test case, print a single line with one integer β the minimum number of operations to make \(s\) good. | In the first test case, one of the ways to make \(s\) good is the following.Change \(s_3\), \(s_6\) and \(s_7\) to '0', after that \(s\) becomes ""1100000000"", it can be divided into ""11"" and ""00000000"", which lengths are \(2\) and \(8\) respectively. There are other ways to operate \(3\) times to make \(s\) good,... | Input: 51011100110008110011112002116100110 | Output: 3 0 0 0 3 | Beginner | 1 | 1,176 | 455 | 111 | 16 |
1,669 | E | 1669E | E. 2-Letter Strings | 1,200 | data structures; math; strings | Given \(n\) strings, each of length \(2\), consisting of lowercase Latin alphabet letters from 'a' to 'k', output the number of pairs of indices \((i, j)\) such that \(i < j\) and the \(i\)-th string and the \(j\)-th string differ in exactly one position.In other words, count the number of pairs \((i, j)\) (\(i < j\)) ... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β 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 10^5\)) β the number of strings.Then follows \(n\) lines, the \(i\)-th of which containing a... | For each test case, print a single integer β the number of pairs \((i, j)\) (\(i < j\)) such that the \(i\)-th string and the \(j\)-th string have exactly one position \(p\) (\(1 \leq p \leq 2\)) such that \({s_{i}}_{p} \neq {s_{j}}_{p}\). Please note, that the answer for some test cases won't fit into 32-bit integer t... | For the first test case the pairs that differ in exactly one position are: (""ab"", ""cb""), (""ab"", ""db""), (""ab"", ""aa""), (""cb"", ""db"") and (""cb"", ""cc"").For the second test case the pairs that differ in exactly one position are: (""aa"", ""ac""), (""aa"", ""ca""), (""cc"", ""ac""), (""cc"", ""ca""), (""ac... | Input: 46abcbdbaaccef7aabbccaccabbaa4kkkkabab5jfjfjkjkjk | Output: 5 6 0 6 | Easy | 3 | 602 | 498 | 426 | 16 |
982 | C | 982C | C. Cut 'em all! | 1,500 | dfs and similar; dp; graphs; greedy; trees | You're given a tree with \(n\) vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) denoting the size of the tree. The next \(n - 1\) lines contain two integers \(u\), \(v\) (\(1 \le u, v \le n\)) each, describing the vertices connected by the \(i\)-th edge.It's guaranteed that the given edges form a tree. | Output a single integer \(k\) β the maximum number of edges that can be removed to leave all connected components with even size, or \(-1\) if it is impossible to remove edges in order to satisfy this property. | In the first example you can remove the edge between vertices \(1\) and \(4\). The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is \(-1\). | Input: 42 44 13 1 | Output: 1 | Medium | 5 | 201 | 287 | 210 | 9 |
1,777 | D | 1777D | D. Score of a Tree | 1,900 | bitmasks; combinatorics; dfs and similar; dp; math; probabilities; trees | You are given a tree of \(n\) nodes, rooted at \(1\). Every node has a value of either \(0\) or \(1\) at time \(t=0\).At any integer time \(t>0\), the value of a node becomes the bitwise XOR of the values of its children at time \(t - 1\); the values of leaves become \(0\) since they don't have any children.Let \(S(t)\... | 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 \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of nodes in the tree.The next \(n-1\) lines of each test case conta... | Output the sum modulo \(10^9+7\) for each test case. | Let us find \(F(A)\) for the configuration \(A = [0,1,0,0,1,1]\) (\(A[i]\) denotes the value of node \(i\)). Initially (at \(t = 0\)) our tree is as shown in the picture below. In each node, two values are shown: the number and the value of this node. \(S(0)\) for this configuration is \(3\). At \(t = 1\) the configura... | Input: 161 21 33 43 53 6 | Output: 288 | Hard | 7 | 694 | 514 | 52 | 17 |
1,396 | C | 1396C | C. Monster Invaders | 2,300 | dp; greedy; implementation | Ziota found a video game called ""Monster Invaders"".Similar to every other shooting RPG game, ""Monster Invaders"" involves killing monsters and bosses with guns.For the sake of simplicity, we only consider two different types of monsters and three different types of guns.Namely, the two types of monsters are: a norma... | The first line of the input contains five integers separated by single spaces: \(n\) \((2 \le n \le 10^6)\) β the number of stages, \(r_1, r_2, r_3\) \((1 \le r_1 \le r_2 \le r_3 \le 10^9)\) β the reload time of the three guns respectively, \(d\) \((1 \le d \le 10^9)\) β the time of moving between adjacent levels.The s... | Print one integer, the minimum time to finish the game. | In the first test case, the optimal strategy is: Use the pistol to kill three normal monsters and AWP to kill the boss (Total time \(1\cdot3+4=7\)) Move to stage two (Total time \(7+3=10\)) Use the pistol twice and AWP to kill the boss (Total time \(10+1\cdot2+4=16\)) Move to stage three (Total time \(16+3=19\)) Use th... | Input: 4 1 3 4 3 3 2 5 1 | Output: 34 | Expert | 3 | 2,017 | 460 | 55 | 13 |
2,061 | F1 | 2061F1 | F1. Kevin and Binary String (Easy Version) | 2,100 | greedy; implementation | This is the easy version of the problem. The difference between the versions is that in this version, string \(t\) consists of only '0' and '1'. You can hack only if you solved all versions of this problem. Kevin has a binary string \(s\) of length \(n\). Kevin can perform the following operation: Choose two adjacent b... | 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 string \(s\) consisting of '0' and '1'.The second line of each test case contains a string \(t\) consisting of '0'... | For each test case, output one integer β the minimum number of operations required. If it is impossible, output \(-1\). | In the first test case, the possible way is shown in the statement.In the second test case, one possible way could be: Swap blocks \([2, 2], [3, 3]\), \(s\) will become \(\mathtt{001101}\). Swap blocks \([3, 4], [5, 5]\), \(s\) will become \(\mathtt{000111}\). Swap blocks \([1, 3], [4, 6]\), \(s\) will become \(\mathtt... | Input: 600011001110000011111010101111000010101100101101001100100111001 | Output: 1 3 1 -1 -1 -1 | Hard | 2 | 1,455 | 500 | 119 | 20 |
1,814 | A | 1814A | A. Coins | 800 | implementation; math | In Berland, there are two types of coins, having denominations of \(2\) and \(k\) burles.Your task is to determine whether it is possible to represent \(n\) burles in coins, i. e. whether there exist non-negative integers \(x\) and \(y\) such that \(2 \cdot x + k \cdot y = n\). | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^{18}\); \(k \ne 2\)). | For each test case, print YES if it is possible to represent \(n\) burles in coins; otherwise, print NO. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). | In the first test case, you can take one coin with denomination \(2\) and one coin with denomination \(k = 3\).In the second test case, you can take three coins with denomination \(2\). Alternatively, you can take six coins with denomination \(k = 1\).In the third test case, there is no way to represent \(7\) burles.In... | Input: 45 36 17 48 8 | Output: YES YES NO YES | Beginner | 2 | 278 | 212 | 258 | 18 |
1,482 | H | 1482H | H. Exam | 3,400 | data structures; string suffix structures; trees | This year a Chunin Selection Exam is held again in Konoha, and taking part in it are \(n\) ninjas named \(s_1\), \(s_2\), ..., \(s_n\). All names are distinct. One of the exam stages consists of fights between the participants. This year the rules determining the ninjas for each fight are the following: ninjas \(i\) an... | The first line consists of the only integer \(n\) (\(1 \leq n \leq 10^{6}\)) standing for the number of examinees.The following \(n\) lines contain their names. No two names coincide, all names are non-empty and consist of lowercase English letters. The total length of all names doesn't exceed \(10^6\). | Print the only integer standing for the number of fights. | In the first example hidan fights against dan, and hanabi fights against nabi, who also fights bi. Ninjas named hanabi and bi don't fight each other since there is the ninja called nabi who breaks the third condition for them.In the second example the fights are held between abacaba and acaba, abacaba and abaca, acaba ... | Input: 5 hidan dan hanabi bi nabi | Output: 3 | Master | 3 | 868 | 304 | 57 | 14 |
209 | A | 209A | A. Multicolored Marbles | 1,600 | dp; math | Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid.A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroi... | The first line contains a single integer n (1 β€ n β€ 106) β the number of marbles in Polycarpus's sequence. | Print a single number β the answer to the problem modulo 1000000007 (109 + 7). | Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: pick the first marble; pick the second marble; pick the third marble; pick the first and second marbles; pick the second and third marbles; pick the first, second and thir... | Input: 3 | Output: 6 | Medium | 2 | 542 | 106 | 78 | 2 |
44 | B | 44B | B. Cola | 1,500 | implementation | To celebrate the opening of the Winter Computer School the organizers decided to buy in n liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly a bottles 0.5 in volume, b one-liter bottles and c of two... | The first line contains four integers β n, a, b, c (1 β€ n β€ 10000, 0 β€ a, b, c β€ 5000). | Print the unique number β the solution to the problem. If it is impossible to buy exactly n liters of cola, print 0. | Input: 10 5 5 5 | Output: 9 | Medium | 1 | 1,134 | 87 | 116 | 0 | |
669 | A | 669A | A. Little Artem and Presents | 800 | math | Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem... | The only line of the input contains a single integer n (1 β€ n β€ 109) β number of stones Artem received on his birthday. | Print the maximum possible number of times Artem can give presents to Masha. | In the first sample, Artem can only give 1 stone to Masha.In the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times.In the third sample, Atrem can first give Masha 2 stones, a then 1 more stone.In the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finall... | Input: 1 | Output: 1 | Beginner | 1 | 572 | 119 | 76 | 6 |
559 | C | 559C | C. Gerald and Giant Chess | 2,200 | combinatorics; dp; math; number theory | Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h Γ w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of gia... | The first line of the input contains three integers: h, w, n β the sides of the board and the number of black cells (1 β€ h, w β€ 105, 1 β€ n β€ 2000). Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 β€ ri β€ h, 1 β€ ci β€ w) β the number of the row and column of the i-th... | Print a single line β the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109 + 7. | Input: 3 4 22 22 3 | Output: 2 | Hard | 4 | 985 | 440 | 141 | 5 | |
1,740 | C | 1740C | C. Bricks and Bags | 1,400 | constructive algorithms; games; greedy; sortings | There are \(n\) bricks numbered from \(1\) to \(n\). Brick \(i\) has a weight of \(a_i\).Pak Chanek has \(3\) bags numbered from \(1\) to \(3\) that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.After Pak Chanek distributes the bri... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 2 \cdot 10^5\)) β the number of bricks.T... | For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally. | In the first test case, one way of achieving a final score of \(6\) is to do the following: Put bricks \(1\), \(4\), and \(5\) into bag \(1\). Put brick \(3\) into bag \(2\). Put brick \(2\) into bag \(3\). If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is: Take brick \(5\) from ba... | Input: 353 1 5 2 3417 8 19 458265 265 265 265 265 265 265 265 | Output: 6 63 0 | Easy | 4 | 739 | 550 | 149 | 17 |
2,115 | F2 | 2115F2 | F2. Gellyfish and Lycoris Radiata (Hard Version) | 3,500 | data structures | This is the hard version of the problem. The difference between the versions is that in this version, the time limit and the constraints on \(n\) and \(q\) are higher. You can hack only if you solved all versions of this problem. Gellyfish has an array consisting of \(n\) sets. Initially, all the sets are empty.Now Gel... | The first line contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 3 \cdot 10^5\)) β the number of the sets and the number of operations.As you need to respond to the operations online, the operations will be encoded.The \(i\)-th line of the following \(q\) lines contains three integers \(a\), \(b\), and \(c\) (\... | For each query operation, output the answer to the query. | All the sets are empty in the beginning, so the array is \([\{\}, \{\}, \{\}, \{\}, \{\}]\).With the decoding method given before, we can see what happens in each operation: For the first operation: \(a = 1, r = 2, p = 2\). The modification operation is an Insert operation; element \(1\) is inserted into the first two ... | Input: 5 101 2 22 3 11 5 32 2 51 5 22 4 43 2 23 1 23 10 53 2 4 | Output: 1 0 1 1 3 1 0 5 0 0 | Master | 1 | 1,474 | 1,422 | 57 | 21 |
2,062 | E1 | 2062E1 | E1. The Game (Easy Version) | 2,000 | data structures; dfs and similar; games; graphs; greedy; trees | This is the easy version of the problem. The difference between the versions is that in this version, you only need to find one of the possible nodes Cirno may choose. You can hack only if you solved all versions of this problem. Cirno and Daiyousei are playing a game with a tree\(^{\text{β}}\) of \(n\) nodes, rooted a... | The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of input test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 4\cdot 10^5\)) β the number of nodes in the tree.The second line contains \(n\) integers \(w_1,w_2,\ldots,w_n\) (\(1 \le w_i \le n\... | For each test case, print one line.If Cirno wins the game, print any possible node she may choose in the first turn.Otherwise, print ""0"" (without quotes). | In the first test case: If Cirno chooses \(1\) or \(3\) in the first turn, Daiyousei cannot make a move, so Daiyousei wins. If Cirno chooses \(2\) or \(4\) in the first turn, Daiyousei can only choose \(3\), but after it Cirno cannot make a move, so Cirno wins.Therefore, all possible nodes Cirno may choose are \(2\) an... | Input: 542 2 4 31 21 32 451 2 3 4 51 22 33 44 531 2 31 21 353 1 3 4 51 22 33 44 5101 2 3 2 4 3 3 4 4 31 44 67 46 96 57 81 22 32 10 | Output: 2 0 2 2 10 | Hard | 6 | 1,048 | 680 | 156 | 20 |
914 | D | 914D | D. Bash and a Tough Math Puzzle | 1,900 | data structures; number theory | Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.Suppose he guesses that the gcd of the eleme... | The first line contains an integer n (1 β€ n β€ 5Β·105) β the size of the array.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.The third line contains an integer q (1 β€ q β€ 4Β·105) β the number of queries.The next q lines describe the queries and may have one of the following... | For each query of first type, output ""YES"" (without quotes) if Bash's guess is almost correct and ""NO"" (without quotes) otherwise. | In the first sample, the array initially is {2, 6, 3}. For query 1, the first two numbers already have their gcd as 2.For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array. After que... | Input: 32 6 341 1 2 21 1 3 32 1 91 1 3 2 | Output: YESYESNO | Hard | 2 | 1,041 | 457 | 134 | 9 |
587 | E | 587E | E. Duff as a Queen | 2,900 | data structures | Duff is the queen of her country, Andarz Gu. She's a competitive programming fan. That's why, when he saw her minister, Malek, free, she gave her a sequence consisting of n non-negative integers, a1, a2, ..., an and asked him to perform q queries for her on this sequence. There are two types of queries: given numbers l... | The first line of input contains two integers, n and q (1 β€ n β€ 2 Γ 105 and 1 β€ q β€ 4 Γ 104).The second line of input contains n integers, a1, a2, ..., an separated by spaces (0 β€ ai β€ 109 for each 1 β€ i β€ n).The next q lines contain the queries. Each line starts with an integer t (1 β€ t β€ 2), type of the corresponding... | Print the answer of each query of the second type in one line. | In the first query, we want all Kheshtaks of sequence 1, 2, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7.In the third query, we want all Khestaks of sequence 1, 10, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.In the fifth query, we want all Kheshtaks of sequence 0 which is 0. | Input: 5 51 2 3 4 22 1 51 2 2 82 1 51 1 3 102 2 2 | Output: 8161 | Master | 1 | 932 | 478 | 62 | 5 |
1,808 | A | 1808A | A. Lucky Numbers | 900 | brute force; implementation | Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively.Each starship has a number βsome positive integer \(x\). Let's define the luckiness of a number \(x\) as the difference between the largest and smallest digits of that number. For... | The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) βthe number of test cases.Each of the following \(t\) lines contains a description of the test case. The description consists of two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^6\)) β the largest and smallest numbers of the starships in the store. | Print \(t\) lines, one line for each test case, containing the luckiest starship number in the store.If there are several ways to choose the luckiest number, output any of them. | Let's look at two test examples: the luckiness of the number \(59\) is \(9 - 5 = 4\); the luckiness of \(60\) equals \(6 - 0 = 6\); the luckiness of \(61\) equals \(6 - 1 = 5\); the luckiness of \(62\) equals \(6 - 2 = 4\); the luckiness of \(63\) is \(6 - 3 = 3\). Thus, the luckiest number is \(60\).In the fifth test ... | Input: 559 6342 4915 1553 571 100 | Output: 60 49 15 57 90 | Beginner | 2 | 1,010 | 319 | 177 | 18 |
961 | C | 961C | C. Chessboard | 1,400 | bitmasks; brute force; implementation | Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being ... | The first line contains odd integer n (1 β€ n β€ 100) β the size of all pieces of the board. Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty lin... | Print one number β minimum number of squares Magnus should recolor to be able to obtain a valid chessboard. | Input: 10010 | Output: 1 | Easy | 3 | 695 | 322 | 107 | 9 | |
470 | C | 470C | C. Eval | 1,900 | *special | You are given a simple arithmetic expression of the form a?b, where a and b are integer constants, and ? can be one of the following operations: '+' (addition), '-' (subtraction), '*' (multiplication), '/' (integer division) or '%' (modulo operation).Output the result of evaluation of this expression. | The input is a single line containing an expression a?b. Here a and b are integers between 1 and 999, inclusive; ? is an operation character: '+', '-' (ASCII code 45), '*', '/' or '%'. | Output a single integer β the result of evaluation of this expression. | Input: 123+456 | Output: 579 | Hard | 1 | 302 | 184 | 70 | 4 | |
1,221 | G | 1221G | G. Graph And Numbers | 2,900 | bitmasks; brute force; combinatorics; dp; meet-in-the-middle | You are given an undirected graph with \(n\) vertices and \(m\) edges. You have to write a number on each vertex of this graph, each number should be either \(0\) or \(1\). After that, you write a number on each edge equal to the sum of numbers on vertices incident to that edge.You have to choose the numbers you will w... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 40\), \(0 \le m \le \frac{n(n - 1)}{2}\)) β the number of vertices and the number of edges, respectively.Then \(m\) lines follow, each line contains two numbers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \ne y_i\)) β the endpoints of the \(i\... | Print one integer β the number of ways to write numbers on all vertices so that there exists at least one edge with \(0\) written on it, at least one edge with \(1\) and at least one edge with \(2\). | Input: 6 5 1 2 2 3 3 4 4 5 5 1 | Output: 20 | Master | 5 | 636 | 408 | 199 | 12 | |
1,555 | F | 1555F | F. Good Graph | 2,700 | data structures; dsu; graphs; trees | You have an undirected graph consisting of \(n\) vertices with weighted edges.A simple cycle is a cycle of the graph without repeated vertices. Let the weight of the cycle be the XOR of weights of edges it consists of.Let's say the graph is good if all its simple cycles have weight \(1\). A graph is bad if it's not goo... | The first line contains two integers \(n\) and \(q\) (\(3 \le n \le 3 \cdot 10^5\); \(1 \le q \le 5 \cdot 10^5\)) β the number of vertices and queries.Next \(q\) lines contain queries β one per line. Each query contains three integers \(u\), \(v\) and \(x\) (\(1 \le u, v \le n\); \(u \neq v\); \(0 \le x \le 1\)) β the ... | For each query, print YES if the edge was added to the graph, or NO otherwise (both case-insensitive). | Input: 9 12 6 1 0 1 3 1 3 6 0 6 2 0 6 4 1 3 4 1 2 4 0 2 5 0 4 5 0 7 8 1 8 9 1 9 7 0 | Output: YES YES YES YES YES NO YES YES NO YES YES NO | Master | 4 | 570 | 418 | 102 | 15 | |
1,687 | C | 1687C | C. Sanae and Giant Robot | 2,500 | binary search; brute force; data structures; dsu; greedy; sortings | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!β Kochiya Sanae Sanae made a giant robot β Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented ... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2\cdot 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\), \(m\) (\(2 \leq n\leq 2\cdot 10^5\), \(1 \leq m\leq 2\cdot 10^5\)) β ... | For each test case, print ""YES"" (without quotes) if \(a\) can be turned into \(b\), or ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response). | Test case 1:One possible way of turning \(a\) to \(b\):First, select \([1,3]\). After the operation, \(a=[3,2,5,2,3]\).Then, select \([2,5]\). After the operation, \(a=[3,2,5,4,1]=b\).Test case 2:It can be shown that it is impossible to turn \(a\) into \(b\). | Input: 25 21 5 4 2 33 2 5 4 11 32 55 21 5 4 2 33 2 4 5 11 22 4 | Output: YES NO | Expert | 6 | 1,124 | 883 | 263 | 16 |
379 | G | 379G | G. New Year Cactus | 3,100 | dp | Jack and Jill are tired of the New Year tree, now they've got a New Year cactus at home! A cactus is a connected undirected graph where any two simple cycles have at most one common vertex. In other words, this graph doesn't have any edges that lie on more than one simple cycle.On the 31st of December they are going to... | The first line contains two integers n and m (1 β€ n β€ 2500, n - 1 β€ m) β the number of vertices and the number of edges, correspondingly. The next m lines contain two integers a, b each (1 β€ a, b β€ n, a β b) that mean that there is an edge connecting vertices a ΠΈ b. Any pair of vertices has at most one edge between the... | The first line must contain space-separated ba (for all 0 β€ a β€ n) where ba equals the maximum number of Jill's toys on the cactus considering that it has a Jack's toys. Numbers ba go in the order of increasing a. | The cactus from the second example is: | Input: 1 0 | Output: 1 0 | Master | 1 | 917 | 322 | 213 | 3 |
625 | D | 625D | D. Finals in arithmetic | 2,400 | constructive algorithms; implementation; math | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.Let's denote a flip operation of an integer as follows: number is considered i... | The first line of the input contains a single integer n (1 β€ n β€ 10100 000). | If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any. | In the first sample 4 = 2 + 2, a = 2 is the only possibility.In the second sample 11 = 10 + 1, a = 10 β the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.It's easy to check that there is no suitable a in the third sample.In the fourth sample 33 = 30 + 3 = 12 + 21, so there are... | Input: 4 | Output: 2 | Expert | 3 | 1,080 | 76 | 186 | 6 |
199 | A | 199A | A. Hexadecimal's theorem | 900 | brute force; constructive algorithms; implementation; number theory | Recently, a chaotic virus Hexadecimal advanced a new theorem which will shake the Universe. She thinks that each Fibonacci number can be represented as sum of three not necessary different Fibonacci numbers.Let's remember how Fibonacci numbers can be calculated. F0 = 0, F1 = 1, and all the next numbers are Fi = Fi - 2 ... | The input contains of a single integer n (0 β€ n < 109) β the number that should be represented by the rules described above. It is guaranteed that n is a Fibonacci number. | Output three required numbers: a, b and c. If there is no answer for the test you have to print ""I'm too stupid to solve this problem"" without the quotes.If there are multiple answers, print any of them. | Input: 3 | Output: 1 1 1 | Beginner | 4 | 608 | 171 | 205 | 1 | |
1,172 | A | 1172A | A. Nauuo and Cards | 1,800 | greedy; implementation | Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are \(n\) cards numbered from \(1\) to \(n\), and they were mixed with another \(n\) empty cards. She piled up the \(2n\) cards and drew \(n\) of them. The \(n\) cards in Nauuo's hands ar... | The first line contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the number of numbered cards.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0\le a_i\le n\)) β the initial cards in Nauuo's hands. \(0\) represents an empty card.The third line contains \(n\) integers \(b_1,b_2,\ldots,b_n\) ... | The output contains a single integer β the minimum number of operations to make the \(n\) numbered cards piled up in increasing order. | Example 1We can play the card \(2\) and draw the card \(3\) in the first operation. After that, we have \([0,3,0]\) in hands and the cards in the pile are \([0,1,2]\) from top to bottom.Then, we play the card \(3\) in the second operation. The cards in the pile are \([1,2,3]\), in which the cards are piled up in increa... | Input: 3 0 2 0 3 0 1 | Output: 2 | Medium | 2 | 768 | 554 | 134 | 11 |
685 | D | 685D | D. Kay and Eternity | 2,600 | brute force; implementation; sortings | Snow Queen told Kay to form a word ""eternity"" using pieces of ice. Kay is eager to deal with the task, because he will then become free, and Snow Queen will give him all the world and a pair of skates.Behind the palace of the Snow Queen there is an infinite field consisting of cells. There are n pieces of ice spread ... | The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 300) β the number of pieces of the ice and the value k, respectively. Each of the next n lines contains two integers xi and yi ( - 109 β€ xi, yi β€ 109) β coordinates of the cell containing i-th piece of the ice. It's guaranteed, that no ... | Print n integers: the number of squares of size k Γ k containing exactly 1, 2, ..., n pieces of the ice. | Input: 5 34 54 65 55 67 7 | Output: 10 8 1 4 0 | Expert | 3 | 1,033 | 363 | 104 | 6 | |
455 | E | 455E | E. Function | 2,900 | data structures | Serega and Fedor play with functions. One day they came across a very interesting function. It looks like that: f(1, j) = a[j], 1 β€ j β€ n. f(i, j) = min(f(i - 1, j), f(i - 1, j - 1)) + a[j], 2 β€ i β€ n, i β€ j β€ n. Here a is an integer array of length n.Serega and Fedya want to know what values this function takes at som... | The first line contains integer n (1 β€ n β€ 105) β the length of array a. The next line contains n integers: a[1], a[2], ..., a[n] (0 β€ a[i] β€ 104).The next line contains integer m (1 β€ m β€ 105) β the number of queries. Each of the next m lines contains two integers: xi, yi (1 β€ xi β€ yi β€ n). Each line means that Fedor ... | Print m lines β the answers to the guys' queries. | Input: 62 2 3 4 3 444 53 43 42 3 | Output: 12995 | Master | 1 | 413 | 367 | 49 | 4 | |
1,676 | F | 1676F | F. Longest Strike | 1,300 | data structures; greedy; implementation; sortings; two pointers | Given an array \(a\) of length \(n\) and an integer \(k\), you are tasked to find any two numbers \(l\) and \(r\) (\(l \leq r\)) such that: For each \(x\) \((l \leq x \leq r)\), \(x\) appears in \(a\) at least \(k\) times (i.e. \(k\) or more array elements are equal to \(x\)). The value \(r-l\) is maximized. If no numb... | The first line of the input 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 the integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \leq k \leq n\)) β the length of the array \(a\) and the minimum ... | For each test case output \(2\) numbers, \(l\) and \(r\) that satisfy the conditions, or ""-1"" if no numbers satisfy the conditions.If multiple answers exist, you can output any. | Input: 47 211 11 12 13 13 14 145 16 3 5 2 16 44 3 4 3 3 414 21 1 2 2 2 3 3 3 3 4 4 4 4 4 | Output: 13 14 1 3 -1 1 4 | Easy | 5 | 903 | 599 | 179 | 16 | |
801 | B | 801B | B. Valued Keys | 900 | constructive algorithms; greedy; strings | You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 a... | The first line of input contains the string x.The second line of input contains the string y.Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100. | If there is no string z such that f(x, z) = y, print -1.Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters. | The first case is from the statement.Another solution for the second case is ""zizez""There is no solution for the third case. That is, there is no z such that f(""ab"", z) = ""ba"". | Input: abaa | Output: ba | Beginner | 3 | 608 | 211 | 262 | 8 |
750 | G | 750G | G. New Year and Binary Tree Paths | 3,200 | bitmasks; brute force; combinatorics; dp | The New Year tree is an infinite perfect binary tree rooted in the node 1. Each node v has two children: nodes indexed (2Β·v) and (2Β·v + 1). Polar bears love decorating the New Year tree and Limak is no exception. As he is only a little bear, he was told to decorate only one simple path between some pair of nodes. Thoug... | The only line of the input contains a single integer s (1 β€ s β€ 1015). | Print one integer, denoting the number of unordered pairs of nodes indices defining simple paths with the sum of indices of vertices equal to s. | In sample test, there are 4 paths with the sum of indices equal to 10: | Input: 10 | Output: 4 | Master | 4 | 609 | 70 | 144 | 7 |
1,712 | E2 | 1712E2 | E2. LCM Sum (hard version) | 2,500 | brute force; data structures; math; number theory; two pointers | We are sum for we are manySome NumberThis version of the problem differs from the previous one only in the constraint on \(t\). You can make hacks only if both versions of the problem are solved.You are given two positive integers \(l\) and \(r\).Count the number of distinct triplets of integers \((i, j, k)\) such that... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(\bf{1 \le t \le 10^5}\)). Description of the test cases follows.The only line for each test case contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 2 \cdot 10^5\), \(l + 2 \le r\)). | For each test case print one integer β the number of suitable triplets. | In the first test case, there are \(3\) suitable triplets: \((1,2,3)\), \((1,3,4)\), \((2,3,4)\). In the second test case, there is \(1\) suitable triplet: \((3,4,5)\). | Input: 51 43 58 8668 866 86868 | Output: 3 1 78975 969 109229059713337 | Expert | 5 | 508 | 287 | 71 | 17 |
1,934 | E | 1934E | E. Weird LCM Operations | 3,000 | brute force; constructive algorithms; number theory | Given an integer \(n\), you construct an array \(a\) of \(n\) integers, where \(a_i = i\) for all integers \(i\) in the range \([1, n]\). An operation on this array is defined as follows: Select three distinct indices \(i\), \(j\), and \(k\) from the array, and let \(x = a_i\), \(y = a_j\), and \(z = a_k\). Update the ... | The first line contains one integer \(t\) (\(1 \le t \le 10^2\)) β the number of test cases. The description of the test cases follows.The first and only line of each test case contains an integer \(n\) (\(3 \leq n \leq 3 \cdot 10^{4}\)) β the length of the array.It is guaranteed that the sum of \(n\) over all test cas... | The first line should contain an integer \(k\) (\(0 \leq k \leq \lfloor \frac{n}{6} \rfloor + 5\)) β where \(k\) is the number of operations.The next \(k\) lines should contain the description of each operation i.e. \(3\) integers \(i\), \(j\) and \(k\), where \(1 \leq i, j, k \leq n\) and all must be distinct. | In the third test case, \(a = [1, 2, 3, 4, 5, 6, 7]\).First operation:\(i = 3\), \(j = 5\), \(k = 7\)\(x = 3\), \(y = 5\), \(z = 7\).\(a = [1, 2, \operatorname{lcm}(y,z), 4, \operatorname{lcm}(x,z), 6, \operatorname{lcm}(x,y)]\) = \([1, 2, \color{red}{35}, 4, \color{red}{21}, 6, \color{red}{15}]\).Second operation:\(i ... | Input: 3347 | Output: 1 1 2 3 1 1 3 4 3 3 5 7 5 6 7 2 3 4 | Master | 3 | 996 | 358 | 312 | 19 |
1,585 | A | 1585A | A. Life of a Flower | 800 | implementation | Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given \(n\) days from Petya's live and you have to determine what happened with his flower in the end.The flower grows as follows: If the flower isn't watered for two days in a row, it dies. If the flower is watere... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains the only integer \(n\) (\(1 \leq n \leq 100\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots... | For each test case print a single integer \(k\) β the flower's height after \(n\) days, or \(-1\), if the flower dies. | Input: 4 3 1 0 1 3 0 1 1 4 1 0 0 1 1 0 | Output: 3 7 -1 1 | Beginner | 1 | 686 | 445 | 118 | 15 | |
1,974 | B | 1974B | B. Symmetric Encoding | 800 | implementation; sortings; strings | Polycarp has a string \(s\), which consists of lowercase Latin letters. He encodes this string using the following algorithm: first, he constructs a new auxiliary string \(r\), which consists of all distinct letters of the string \(s\), written in alphabetical order; then the encoding happens as follows: each character... | 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 length of the string \(b\).The second line of each test case contains a string \(b\) of length \(n\), consisting of lower... | For each test case, output the string \(s\) from which the encoding result \(b\) was obtained. | Input: 510serofedsoc3ttf9tlrhgmaoi1w15hnndledmnhlttin | Output: codeforces fft algorithm w meetinthemiddle | Beginner | 3 | 1,118 | 508 | 94 | 19 | |
1,070 | E | 1070E | E. Getting Deals Done | 2,100 | binary search; data structures | Polycarp has a lot of work to do. Recently he has learned a new time management rule: ""if a task takes five minutes or less, do it immediately"". Polycarp likes the new rule, however he is not sure that five minutes is the optimal value. He supposes that this value \(d\) should be chosen based on existing task list.Po... | The first line of the input contains single integer \(c\) (\(1 \le c \le 5 \cdot 10^4\)) β number of test cases. Then description of \(c\) test cases follows. Solve test cases separately, test cases are completely independent and do not affect each other.Each test case is described by two lines. The first of these line... | Print \(c\) lines, each line should contain answer for the corresponding test case β the maximum possible number of tasks Polycarp can complete and the integer value \(d\) (\(1 \le d \le t\)) Polycarp should use in time management rule, separated by space. If there are several possible values \(d\) for a test case, out... | In the first test case of the first example \(n=5\), \(m=2\) and \(t=16\). The sequence of difficulties is \([5, 6, 1, 4, 7]\). If Polycarp chooses \(d=5\) then he will complete \(3\) tasks. Polycarp will work by the following schedule: Polycarp reads the first task, its difficulty is not greater than \(d\) (\(p_1=5 \l... | Input: 45 2 165 6 1 4 75 3 305 6 1 4 76 4 1512 5 15 7 20 171 1 50100 | Output: 3 54 72 100 25 | Hard | 2 | 2,884 | 864 | 336 | 10 |
706 | B | 706B | B. Interesting drink | 1,100 | binary search; dp; implementation | Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink ""Beecola"", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.Vasiliy plans to buy his favorite drink fo... | The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of shops in the city that sell Vasiliy's favourite drink.The second line contains n integers xi (1 β€ xi β€ 100 000) β prices of the bottles of the drink in the i-th shop.The third line contains a single integer q (1 β€ q β€ 100 000) β t... | Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day. | On the first day, Vasiliy won't be able to buy a drink in any of the shops.On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.On the third day, Vasiliy can buy a drink only in the shop number 1.Finally, on the last day Vasiliy can buy a drink in any shop. | Input: 53 10 8 6 114110311 | Output: 0415 | Easy | 3 | 513 | 491 | 146 | 7 |
369 | A | 369A | A. Valera and Plates | 900 | greedy; implementation | Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dis... | The first line of the input contains three integers n, m, k (1 β€ n, m, k β€ 1000) β the number of the planned days, the number of clean bowls and the number of clean plates.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals tw... | Print a single integer β the minimum number of times Valera will need to wash a plate/bowl. | In the first sample Valera will wash a bowl only on the third day, so the answer is one.In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once.In the third sample, Valera will have the second type of dish for all thr... | Input: 3 1 11 2 1 | Output: 1 | Beginner | 2 | 695 | 372 | 91 | 3 |
821 | A | 821A | A. Okabe and Future Gadget Laboratory | 800 | implementation | Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In o... | The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab. The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105). | Print ""Yes"" if the given lab is good and ""No"" otherwise.You can output each letter in upper or lower case. | In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is ""Yes"".In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an inte... | Input: 31 1 22 3 16 4 1 | Output: Yes | Beginner | 1 | 564 | 223 | 110 | 8 |
1,291 | A | 1291A | A. Even But Not Even | 900 | greedy; math; strings | Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by \(2\) but the number itself is not divisible by \(2\). For example, \(13\), \(1227\), \(185217\) are ebne numbers, while \(12\), \(2\), \(177013\), \(265918\) are not. If you're still unsure what ebne numbers are, you can lo... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3000\)) β the number of digits in the original number.The... | For each test case given in the input print the answer in the following format: If it is impossible to create an ebne number, print ""-1"" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can pr... | In the first test case of the example, \(1227\) is already an ebne number (as \(1 + 2 + 2 + 7 = 12\), \(12\) is divisible by \(2\), while in the same time, \(1227\) is not divisible by \(2\)) so we don't need to delete any digits. Answers such as \(127\) and \(17\) will also be accepted.In the second test case of the e... | Input: 4 4 1227 1 0 6 177013 24 222373204424185217171912 | Output: 1227 -1 17703 2237344218521717191 | Beginner | 3 | 1,269 | 549 | 481 | 12 |
989 | E | 989E | E. A Trance of Nightfall | 2,700 | dp; geometry; matrices; probabilities | The cool breeze blows gently, the flowing water ripples steadily.""Flowing and passing like this, the water isn't gone ultimately; Waxing and waning like that, the moon doesn't shrink or grow eventually.""""Everything is transient in a way and perennial in another.""Kanno doesn't seem to make much sense out of Mino's i... | The first line contains a positive integer \(n\) (\(2 \leq n \leq 200\)) β the number of points in \(S\).The \(i\)-th of the following \(n\) lines contains two space-separated integers \(x_i\) and \(y_i\) (\(-10^4 \leq x_i, y_i \leq 10^4\)) β the coordinates of the \(i\)-th point in \(S\). The input guarantees that for... | Output \(q\) lines each containing a decimal number β the \(i\)-th among them denotes the maximum probability of staying on the \(t_i\)-th point after \(m_i\) steps, with a proper choice of starting position \(P\).Your answer will be considered correct if each number in your output differs from the corresponding one in... | The points in \(S\) and possible candidates for line \(l\) are depicted in the following figure. For the first query, when \(P = (-1, -3)\), \(l\) is uniquely determined to be \(3x = y\), and thus Kanno will move to \((0, 0)\) with a probability of \(\frac 1 2\).For the third query, when \(P = (2, 2)\), \(l\) is chosen... | Input: 50 01 32 23 14 4101 12 13 14 15 13 23 33 43 53 6 | Output: 0.500000000000000000000.500000000000000000000.333333333333333314830.500000000000000000000.500000000000000000000.185185185185185174910.152263374485596708620.144947416552354824140.143321648122745504140.14296036624949901017 | Master | 4 | 1,610 | 693 | 487 | 9 |
1,878 | B | 1878B | B. Aleksa and Stack | 800 | constructive algorithms; math | After the Serbian Informatics Olympiad, Aleksa was very sad, because he didn't win a medal (he didn't know stack), so Vasilije came to give him an easy problem, just to make his day better.Vasilije gave Aleksa a positive integer \(n\) (\(n \ge 3\)) and asked him to construct a strictly increasing array of size \(n\) of... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of elements in array.It is guara... | For each test case, output \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(1 \le a_i \le 10^9\)).It can be proved that the solution exists for any \(n\). If there are multiple solutions, output any of them. | In the first test case, \(a_1=6\), \(a_2=8\), \(a_3=12\), so \(a_1+a_2=14\) and \(3 \cdot a_3=36\), so \(3 \cdot a_3\) is not divisible by \(a_1+a_2\). | Input: 3367 | Output: 6 8 12 7 11 14 20 22 100 9 15 18 27 36 90 120 | Beginner | 2 | 665 | 401 | 206 | 18 |
1,826 | C | 1826C | C. Dreaming of Freedom | 1,300 | greedy; math; number theory | Because to take away a man's freedom of choice, even his freedom to make the wrong choice, is to manipulate him as though he were a puppet and not a person.β Madeleine L'EngleThere are \(n\) programmers choosing their favorite algorithm amongst \(m\) different choice options. Before the first round, all \(m\) options a... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.Each test case consists of a single line containing two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^6\)) β the number of people and choice options respectively. | For each test case output ""YES"" if the programmers will eventually choose a single option, and ""NO"" otherwise.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as a positive answer). | In the first example, there are \(8\) ways people could vote: \(\{1|1|1, 1|1|2, 1|2|1, 1|2|2, 2|1|1, 2|1|2, 2|2|1, 2|2|2\}\).In cases \(1\), \(2\), \(3\), and \(5\), the programmers are left with the first algorithm, and in the remaining cases people are left with the second one, so the voting ends in one round in any ... | Input: 53 24 25 31000000 10000001 1000000 | Output: YES NO YES NO YES | Easy | 3 | 726 | 263 | 230 | 18 |
560 | A | 560A | A. Currency System in Geraldion | 1,000 | implementation; sortings | A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of ea... | The first line contains number n (1 β€ n β€ 1000) β the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a1, a2, ..., an (1 β€ ai β€ 106) β the values of the banknotes. | Print a single line β the minimum unfortunate sum. If there are no unfortunate sums, print - 1. | Input: 51 2 3 4 5 | Output: -1 | Beginner | 2 | 415 | 233 | 95 | 5 | |
1,499 | A | 1499A | A. Domino on Windowsill | 800 | combinatorics; constructive algorithms; math | You have a board represented as a grid with \(2 \times n\) cells.The first \(k_1\) cells on the first row and first \(k_2\) cells on the second row are colored in white. All other cells are colored in black.You have \(w\) white dominoes (\(2 \times 1\) tiles, both cells are colored in white) and \(b\) black dominoes (\... | The first line contains a single integer \(t\) (\(1 \le t \le 3000\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(k_1\) and \(k_2\) (\(1 \le n \le 1000\); \(0 \le k_1, k_2 \le n\)).The second line of each test case contains two integers \(w\) and \(b\) (\(0 \le w, b \le ... | For each test case, print YES if it's possible to place all \(w + b\) dominoes on the board and NO, otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). | In the first test case, \(n = 1\), \(k_1 = 0\) and \(k_2 = 1\). It means that \(2 \times 1\) board has black cell \((1, 1)\) and white cell \((2, 1)\). So, you can't place any white domino, since there is only one white cell.In the second test case, the board of the same size \(2 \times 1\), but both cell are white. Si... | Input: 5 1 0 1 1 0 1 1 1 0 0 3 0 0 1 3 4 3 1 2 2 5 4 3 3 1 | Output: NO YES NO YES YES | Beginner | 3 | 702 | 325 | 249 | 14 |
2,073 | L | 2073L | 1,300 | Easy | 0 | 0 | 0 | 0 | 20 | |||||||
784 | D | 784D | D. Touchy-Feely Palindromes | 1,900 | *special; implementation | The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive. | Output ""Yes"" or ""No"". | Input: 373 | Output: Yes | Hard | 2 | 0 | 112 | 25 | 7 | ||
1,223 | D | 1223D | D. Sequence Sorting | 2,000 | dp; greedy; two pointers | You are given a sequence \(a_1, a_2, \dots, a_n\), consisting of integers.You can apply the following operation to this sequence: choose some integer \(x\) and move all elements equal to \(x\) either to the beginning, or to the end of \(a\). Note that you have to move all these elements in one direction in one operatio... | The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of the queries. Each query is represented by two consecutive lines.The first line of each query contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of elements.The second line of each query contains \(n\) integers \... | For each query print one integer β the minimum number of operation for sorting sequence \(a\) in non-descending order. | In the first query, you can move all \(1\)-elements to the beginning (after that sequence turn into \([1, 1, 1, 3, 6, 6, 3]\)) and then move all \(6\)-elements to the end.In the second query, the sequence is sorted initially, so the answer is zero.In the third query, you have to move all \(2\)-elements to the beginning... | Input: 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 | Output: 2 0 1 | Hard | 3 | 1,219 | 458 | 118 | 12 |
2,038 | H | 2038H | H. Galactic Council | 3,000 | flows | Monocarp plays a computer game. In this game, he maintains a space empire. The empire is governed by \(n\) political parties. Initially, every party has political power equal to \(0\), and there is no ruling party.During each of the next \(m\) turns, the following happens: initially, Monocarp has to choose which party ... | The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 50\)) β the number of political parties and the number of turns, respectively.The second line contains \(m\) integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_j \le n\)), where \(p_j\) is the index of the party which should be the ruling party at the end... | If Monocarp loses the game no matter how he acts, print one integer \(-1\).Otherwise, print \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_j \le n\)), where \(c_j\) is the index of the party Monocarp should support during the \(j\)-th turn. If there are multiple answers, print any of them. | Input: 2 32 1 21 2 34 5 6 | Output: 2 1 2 | Master | 1 | 1,213 | 594 | 294 | 20 | |
552 | B | 552B | B. Vanya and Books | 1,200 | implementation; math | Vanya got an important task β he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.Vanya wants to know how many digits he will have to write down as he labels the book... | The first line contains integer n (1 β€ n β€ 109) β the number of books in the library. | Print the number of digits needed to number all the books. | Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits. | Input: 13 | Output: 17 | Easy | 2 | 322 | 85 | 58 | 5 |
53 | D | 53D | D. Physical Education | 1,500 | sortings | Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line... | The first line contains an integer n (1 β€ n β€ 300) which is the number of students. The second line contains n space-separated integers ai (1 β€ ai β€ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 β€ bi β€ 109) which represent ... | In the first line print an integer k (0 β€ k β€ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 β€ pi β€ n - 1) means that Vasya should swap students occupying places pi and pi + 1. | Input: 41 2 3 23 2 1 2 | Output: 42 31 23 42 3 | Medium | 1 | 807 | 577 | 309 | 0 | |
1,684 | E | 1684E | E. MEX vs DIFF | 2,100 | binary search; brute force; constructive algorithms; data structures; greedy; two pointers | You are given an array \(a\) of \(n\) non-negative integers. In one operation you can change any number in the array to any other non-negative integer.Let's define the cost of the array as \(\operatorname{DIFF}(a) - \operatorname{MEX}(a)\), where \(\operatorname{MEX}\) of a set of non-negative integers is the smallest ... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(0 \le k \le 10^5\)) β the length of the array... | For each test case output a single integer β minimal cost that it is possible to get making at most \(k\) operations. | In the first test case no operations are needed to minimize the value of \(\operatorname{DIFF} - \operatorname{MEX}\).In the second test case it is possible to replace \(5\) by \(1\). After that the array \(a\) is \([0,\, 2,\, 4,\, 1]\), \(\operatorname{DIFF} = 4\), \(\operatorname{MEX} = \operatorname{MEX}(\{0, 1, 2, ... | Input: 44 13 0 1 24 10 2 4 57 24 13 0 0 13 1337 10000000006 21 2 8 0 0 0 | Output: 0 1 2 0 | Hard | 6 | 645 | 612 | 117 | 16 |
1,979 | E | 1979E | E. Manhattan Triangle | 2,400 | binary search; constructive algorithms; data structures; geometry; implementation; two pointers | The Manhattan distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) is defined as: $$$\(|x_1 - x_2| + |y_1 - y_2|.\)\(We call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal.You are given a set of pairwise distinct points and an even integer \)d\(. Yo... | Each test consists of multiple test cases. The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(d\) (\(3 \le n \le 2 \cdot 10^5\), \(2 \le d \le 4 \cdot 10^5\), \(d\) is e... | For each test case, output three distinct integers \(i\), \(j\), and \(k\) (\(1 \le i,j,k \le n\)) β the indices of the points forming the Manhattan triangle. If there is no solution, output ""\(0\ 0\ 0\)"" (without quotes).If there are multiple solutions, output any of them. | In the first test case: Points \(A\), \(B\), and \(F\) form a Manhattan triangle, the Manhattan distance between each pair of vertices is \(4\). Points \(D\), \(E\), and \(F\) can also be the answer. In the third test case: Points \(A\), \(C\), and \(E\) form a Manhattan triangle, the Manhattan distance between each pa... | Input: 66 43 10 00 -25 -33 -52 -25 40 00 -25 -33 -52 -26 63 10 00 -25 -33 -52 -24 43 00 3-3 00 -310 82 1-5 -1-4 -1-5 -30 1-2 5-4 4-4 20 0-4 14 400000100000 100000-100000 100000100000 -100000-100000 -100000 | Output: 2 6 1 4 3 5 3 5 1 0 0 0 6 1 3 0 0 0 | Expert | 6 | 494 | 727 | 276 | 19 |
1,872 | D | 1872D | D. Plus Minus Permutation | 1,200 | math | You are given \(3\) integers β \(n\), \(x\), \(y\). Let's call the score of a permutation\(^\dagger\) \(p_1, \ldots, p_n\) the following value:$$$\((p_{1 \cdot x} + p_{2 \cdot x} + \ldots + p_{\lfloor \frac{n}{x} \rfloor \cdot x}) - (p_{1 \cdot y} + p_{2 \cdot y} + \ldots + p_{\lfloor \frac{n}{y} \rfloor \cdot y})\)\(I... | The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follows the description of each test case.The only line of each test case description contains \(3\) integers \(n\), \(x\), \(y\) (\(1 \le n \le 10^9\), \(1 \le x, y \le n\)). | For each test case, output a single integer β the maximum score among all permutations of length \(n\). | The first test case is explained in the problem statement above.In the second test case, one of the optimal permutations will be \([12,11,\color{blue}{\underline{\color{black}{2}}},4,8,\color{blue}{\underline{\color{red}{\underline{\color{black}{9}}}}},10,6,\color{blue}{\underline{\color{black}{1}}},5,3,\color{blue}{\u... | Input: 87 2 312 6 39 1 92 2 2100 20 5024 4 61000000000 5575 254504 4 1 | Output: 12 -3 44 0 393 87 179179179436104 -6 | Easy | 1 | 1,276 | 280 | 103 | 18 |
2,071 | D1 | 2071D1 | D1. Infinite Sequence (Easy Version) | 1,800 | bitmasks; brute force; dp; implementation; math | This is the easy version of the problem. The difference between the versions is that in this version, \(l=r\). You can hack only if you solved all versions of this problem. You are given a positive integer \(n\) and the first \(n\) terms of an infinite binary sequence \(a\), which is defined as follows: For \(m>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 three integers \(n\), \(l\), and \(r\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le l=r\le 10^{18}\)).The second line cont... | For each test case, output a single integer β the sum of elements in the given range. | In the first test case, the sequence \(a\) is equal to $$$\([\underline{\color{red}{1}}, 1, 1, 0, 0, 1, 1, 1, 1, 1, \ldots]\)\( where \)l = 1\(, and \)r = 1\(. The sum of elements in the range \)[1, 1]\( is equal to \)\(a_1 = 1.\)\(In the second test case, the sequence \)a\( is equal to \)\([\color{red}{1}, \color{red}... | Input: 91 1 112 3 31 03 5 51 1 11 234 23405 1111 11111 0 1 0 11 1000000000000000000 1000000000000000000110 87 870 1 1 1 1 1 1 1 0 012 69 691 0 0 0 0 1 0 1 0 1 1 013 46 460 1 0 1 1 1 1 1 1 0 1 1 1 | Output: 1 1 0 0 1 0 1 0 0 | Medium | 5 | 580 | 540 | 85 | 20 |
802 | L | 802L | L. Send the Fool Further! (hard) | 2,400 | dfs and similar; dp; math; trees | Heidi is terrified by your estimate and she found it unrealistic that her friends would collaborate to drive her into debt. She expects that, actually, each person will just pick a random friend to send Heidi to. (This randomness assumption implies, however, that she can now visit the same friend an arbitrary number of... | The first line contains the number of friends n (3 β€ n β€ 105). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104) meaning that u and v are friends and the cost for traveling between u and v is c (paid every time!).It is again guaranteed that the social network of... | Assume that the expected cost of the trips is written as an irreducible fraction a / b (that is, a and b are coprime). Then Heidi, the weird cow that she is, asks you to output . (Output a single integer between 0 and 109 + 6.) | In the first example, with probability 1 / 2 Heidi will go to 1 from 0, and with probability 1 / 2 she will go to 2. In the first case the cost would be 10, and in the second it would be 20. After reaching 1 or 2 she will stop, as 1 and 2 are leaves of the social tree. Hence, the expected cost she has to pay is 10Β·1 / ... | Input: 30 1 100 2 20 | Output: 15 | Expert | 4 | 874 | 344 | 227 | 8 |
696 | B | 696B | B. Puzzles | 1,700 | dfs and similar; math; probabilities; trees | Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city h... | The first line of input contains a single integer n (1 β€ n β€ 105) β the number of cities in USC.The second line contains n - 1 integers p2, p3, ..., pn (1 β€ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC. | In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6. | Input: 71 2 1 1 4 4 | Output: 1.0 4.0 5.0 3.5 4.5 5.0 5.0 | Medium | 4 | 1,235 | 302 | 224 | 6 | |
309 | C | 309C | C. Memory for Arrays | 1,900 | binary search; bitmasks; greedy | You get to work and turn on the computer. You start coding and give little thought to the RAM role in the whole process. In this problem your task is to solve one of the problems you encounter in your computer routine.We'll consider the RAM as a sequence of cells that can contain data. Some cells already contain some d... | The first line of the input contains two integers n and m (1 β€ n, m β€ 106). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109). The next line contains m integers b1, b2, ..., bm (1 β€ 2bi β€ 109). | Print a single integer β the answer to the problem. | In the first example you are given memory clusters with sizes 8, 4, 3, 2, 2 and arrays with sizes 8, 4, 4. There are few ways to obtain an answer equals 2: you can locate array with size 8 to the cluster with size 8, and one of the arrays with size 4 to the cluster with size 4. Another way is to locate two arrays with ... | Input: 5 38 4 3 2 23 2 2 | Output: 2 | Hard | 3 | 919 | 208 | 51 | 3 |
1,758 | E | 1758E | E. Tick, Tock | 2,500 | combinatorics; dfs and similar; dsu; graphs | Tannhaus, the clockmaker in the town of Winden, makes mysterious clocks that measure time in \(h\) hours numbered from \(0\) to \(h-1\). One day, he decided to make a puzzle with these clocks. The puzzle consists of an \(n \times m\) grid of clocks, and each clock always displays some hour exactly (that is, it doesn't ... | The first line of input contains \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β the number of test cases.The first line of each test case consists of 3 integers \(n\), \(m\), and \(h\) (\(1 \leq n, m \leq 2 \cdot 10^5\); \(1 \leq h \leq 10^9\)) β the number of rows in the grid, the number of columns in the grid, and the numb... | For each test case, output the number of ways to assign clocks in the empty cells so that the grid is solvable. The answer can be huge, so output it modulo \(10^9 + 7\). | For the first sample, this is a possible configuration for the clocks: 103032This is solvable since we can: Move the middle column forward one hour. Move the third column forward one hour. Move the third column forward one hour. Move the second row forward one hour. After that all the clocks show one hour.For the secon... | Input: 52 3 41 0 -1-1 -1 22 2 101 23 54 5 10241 -1 -1 -1 -1-1 -1 -1 1000 -1-1 -1 -1 -1 69420 -1 -1 999 -13 3 3-1 -1 12 -1 12 -1 23 3 31 -1 2-1 0 -1-1 1 0 | Output: 4 0 73741817 0 1 | Expert | 4 | 1,284 | 707 | 169 | 17 |
870 | B | 870B | B. Maximum of Maximums of Minimums | 1,200 | greedy | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Def... | The first line contains two integers n and k (1 β€ k β€ n β€ 105) β the size of the array a and the number of subsegments you have to split the array to.The second line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109). | Print single integer β the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | A subsegment [l, r] (l β€ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into sub... | Input: 5 21 2 3 4 5 | Output: 5 | Easy | 1 | 382 | 222 | 164 | 8 |
1,046 | C | 1046C | C. Space Formula | 1,400 | greedy | Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next rac... | The first line contains two integer numbers \(N\) (\(1 \leq N \leq 200000\)) representing number of F1 astronauts, and current position of astronaut \(D\) (\(1 \leq D \leq N\)) you want to calculate best ranking for (no other competitor will have the same number of points before the race).The second line contains \(N\)... | Output contains one integer number β the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking. | If the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position. | Input: 4 350 30 20 1015 10 7 3 | Output: 2 | Easy | 1 | 410 | 769 | 184 | 10 |
1,239 | D | 1239D | D. Catowice City | 2,400 | 2-sat; dfs and similar; graph matchings; graphs | In the Catowice city next weekend the cat contest will be held. However, the jury members and the contestants haven't been selected yet. There are \(n\) residents and \(n\) cats in the Catowice, and each resident has exactly one cat living in his house. The residents and cats are numbered with integers from \(1\) to \(... | The first line contains an integer \(t\) (\(1 \le t \le 100\,000\)), the number of test cases. Then description of \(t\) test cases follow, where each description is as follows:The first line contains integers \(n\) and \(m\) (\(1 \le n \le m \le 10^6\)), the number of Catowice residents and the number of friendship pa... | For every test case print: ""No"", if it's impossible to select the jury and contestants. Otherwise print ""Yes"".In the second line print two integers \(j\) and \(p\) (\(1 \le j\), \(1 \le p\), \(j + p = n\)) β the number of jury members and the number of contest participants.In the third line print \(j\) distinct int... | In the first test case, we can select the first and the third resident as a jury. Both of them are not acquaintances with a second cat, so we can select it as a contestant.In the second test case, we can select the second resident as a jury. He is not an acquaintances with a first and a third cat, so they can be select... | Input: 4 3 4 1 1 2 2 3 3 1 3 3 7 1 1 1 2 1 3 2 2 3 1 3 2 3 3 1 1 1 1 2 4 1 1 1 2 2 1 2 2 | Output: Yes 2 1 1 3 2 Yes 1 2 2 1 3 No No | Expert | 4 | 937 | 902 | 585 | 12 |
242 | B | 242B | B. Big Segment | 1,100 | implementation; sortings | A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want t... | The first line contains integer n (1 β€ n β€ 105) β the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li, ri (1 β€ li β€ ri β€ 109) β the borders of the i-th segment.It is guaranteed that no two segments coincide. | Print a single integer β the number of the segment that covers all other segments in the set. If there's no solution, print -1.The segments are numbered starting from 1 in the order in which they appear in the input. | Input: 31 12 23 3 | Output: -1 | Easy | 2 | 584 | 289 | 216 | 2 | |
1,246 | F | 1246F | F. Cursor Distance | 3,500 | There is a string \(s\) of lowercase English letters. A cursor is positioned on one of the characters. The cursor can be moved with the following operation: choose a letter \(c\) and a direction (left or right). The cursor is then moved to the closest occurence of \(c\) in the chosen direction. If there is no letter \(... | The only line contains a non-empty string \(s\) of at most \(10^5\) lowercase English letters. | Print a single integer \(\displaystyle \sum_{i = 1}^n \sum_{j = 1}^n \mathrm{dist}(i, j)\). | In the first sample case, \(\mathrm{dist}(i, j) = 0\) for any pair \(i = j\), and \(1\) for all other pairs. | Input: abcde | Output: 20 | Master | 0 | 1,110 | 94 | 91 | 12 | |
1,862 | B | 1862B | B. Sequence Game | 800 | constructive algorithms | Tema and Vika are playing the following game.First, Vika comes up with a sequence of positive integers \(a\) of length \(m\) and writes it down on a piece of paper. Then she takes a new piece of paper and writes down the sequence \(b\) according to the following rule: First, she writes down \(a_1\). Then, she writes do... | Each test consists of multiple test cases. The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. This is followed by a description of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of t... | For each test case, output two lines. In the first line, output a single integer \(m\) β the length of the sequence (\(n \le m \le 2 \cdot n\)). In the second line, output \(m\) integers \(a_1, a_2, a_3, \dots, a_m\) (\(1 \le a_i \le 10^9\)) β the assumed sequence that Vika could have written on the first piece of pape... | The first sample is explained in the problem statement.In the second sample, Vika could have chosen the original sequence. | Input: 634 6 331 2 351 7 9 5 7114421 151 2 2 1 1 | Output: 6 4 3 2 6 3 3 3 1 2 3 6 1 7 9 3 5 7 1 144 2 1 1 6 1 2 2 1 1 1 | Beginner | 1 | 973 | 566 | 391 | 18 |
41 | E | 41E | E. 3-cycles | 1,900 | constructive algorithms; graphs; greedy | During a recent research Berland scientists found out that there were n cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three... | The first line contains an integer n (1 β€ n β€ 100) β the number of cities in Berland. | On the first line must be printed number m β the maximal number of roads in Berland. Then print m lines containing two numbers each β the numbers of cities that the given road joins. The cities are numbered with integers from 1 to n. If there are several variants of solving the problem, print any of them. | Input: 3 | Output: 21 22 3 | Hard | 3 | 713 | 85 | 306 | 0 | |
1,193 | A | 1193A | A. Amusement Park | 0 | *special; dp; math | You have been hired to supervise the project of a new amusement park. The park will have a special gimmick: directed slides that can get customers from one attraction to another quickly and in an entertaining way.The park owner has given you the current project: a list of planned attractions and a list of slides that s... | The first line contains two space-separated integers \(n\), \(m\) (\(1 \leq n \leq 18\), \(0 \leq m \leq n(n-1)/2\)) β the number of attractions and the number of slides, respectively. The attractions are numbered \(1\) through \(n\).Then, \(m\) lines follow. The \(i\)-th of these lines contains two space-separated int... | Output one line with a single integer, the sum of costs of all legal proposals modulo \(998,244,353\). | In the first example, there are two proposals: The slide direction is not flipped. This proposal has cost \(0\). The slide direction is flipped. This proposal has cost \(1\). As both proposals are valid, the answer is \(0 + 1 = 1\).In the second example, there are eight proposals with the slide directions as follows: \... | Input: 2 1 1 2 | Output: 1 | Beginner | 3 | 1,519 | 690 | 102 | 11 |
958 | E2 | 958E2 | E2. Guard Duty (medium) | 2,200 | binary search; dp; greedy; sortings | Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board he... | The first line contains two integers K, N (2 β€ 2K β€ N β€ 500000, K β€ 5000). The second line contains N distinct integers t1, t2, ..., tN (1 β€ ti β€ 109) representing the times when Heidi leaves hyperspace. | Output only one integer: the minimum time spent on meetings. | In the first example, there are five valid schedules: [1, 4], [6, 7] with total time 4, [1, 4], [6, 12] with total time 9, [1, 4], [7, 12] with total time 8, [1, 6], [7, 12] with total time 10, and [4, 6], [7, 12] with total time 7. So the answer is 4.In the second example, there is only 1 valid schedule: [1, 2], [3, 4... | Input: 2 51 4 6 7 12 | Output: 4 | Hard | 4 | 785 | 203 | 60 | 9 |
921 | 09 | 92109 | 09. Labyrinth-9 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 | |||||
604 | B | 604B | B. More Cowbell | 1,400 | binary search; greedy | Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single ... | The first line of the input contains two space-separated integers n and k (1 β€ n β€ 2Β·k β€ 100 000), denoting the number of cowbells and the number of boxes, respectively.The next line contains n space-separated integers s1, s2, ..., sn (1 β€ s1 β€ s2 β€ ... β€ sn β€ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed... | Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s. | In the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.In the third sample, the optimal solution is {3, 5} and {7}. | Input: 2 12 5 | Output: 7 | Easy | 2 | 911 | 373 | 124 | 6 |
1,840 | G1 | 1840G1 | G1. In Search of Truth (Easy Version) | 2,200 | constructive algorithms; interactive; math; meet-in-the-middle; probabilities | The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most \(2023\) queries.This is an interactive problem.You are playing a game. The circle is divided into \(n\) sectors, sectors are numbered from \(1\) to \(n\) in some order. You are in the ad... | The input consists of a single integer \(x\) (\(1 \le x \le n\)) β the number of the initial sector. | After you determine the integer \(n\) β the number of sectors, you should output ""! n"" (\(1 \le n \le 10^6\)). After that the program should immediately terminate.Note that, printing the answer does not count as a query.It is guaranteed that the integer \(n\) and the numbers of the sectors are fixed initially and wil... | HacksTo hack, use the following test format.In the first line, output a single integer \(n\) (\(1 \le n \le 10^6\)) β the number of sectors.In the second line, output \(n\) different integers \(1 \le a_1, a_2, \dots, a_n \le n\) β the numbers of the sectors in clockwise order, the arrow initially points to the sector w... | Input: 1 5 6 7 2 10 9 8 4 3 1 | Output: + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10 | Hard | 5 | 874 | 100 | 382 | 18 |
1,810 | C | 1810C | C. Make It Permutation | 1,300 | brute force; greedy; sortings | You have an integer array \(a\) of length \(n\). There are two kinds of operations you can make. Remove an integer from \(a\). This operation costs \(c\). Insert an arbitrary positive integer \(x\) to any position of \(a\) (to the front, to the back, or between any two consecutive elements). This operation costs \(d\).... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Their description follows.The first line of each test case contains three integers \(n\), \(c\), \(d\) (\(1 \le n \le 10^5\), \(1 \le c,d \le 10^9\)).The second line of each test cas... | For each test case, output in one line the minimum cost to make the final array a permutation. | In the first test case, the array is already a permutation, so there's no need for operations.In the second test case, we can remove numbers \(5\), \(6\) to get the permutation \([1,2,3]\) in cost \(2\). Note that we can also get a permutation by inserting a number \(4\), but it costs \(5\).In the third test case, we c... | Input: 83 3 31 2 35 1 51 2 3 5 65 2 31 1 1 3 35 1 102 4 6 8 106 2 87 3 5 4 4 84 10 11 2 6 74 3 32 5 8 72 1000000000 11000000000 1 | Output: 0 2 8 14 20 3 12 999999998 | Easy | 3 | 877 | 497 | 94 | 18 |
362 | B | 362B | B. Petya and Staircases | 1,100 | implementation; sortings | Little boy Petya loves stairs very much. But he is bored from simple going up and down them β he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now ... | The first line contains two integers n and m (1 β€ n β€ 109, 0 β€ m β€ 3000) β the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 β€ di β€ n) β the numbers of the dirty stairs (in an arbitrary order). | Print ""YES"" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print ""NO"". | Input: 10 52 4 8 3 6 | Output: NO | Easy | 2 | 763 | 311 | 107 | 3 | |
1,469 | B | 1469B | B. Red and Blue | 1,000 | dp; greedy | Monocarp had a sequence \(a\) consisting of \(n + m\) integers \(a_1, a_2, \dots, a_{n + m}\). He painted the elements into two colors, red and blue; \(n\) elements were painted red, all other \(m\) elements were painted blue.After painting the elements, he has written two sequences \(r_1, r_2, \dots, r_n\) and \(b_1, ... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then the test cases follow. Each test case consists of four lines.The first line of each test case contains one integer \(n\) (\(1 \le n \le 100\)).The second line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(-100 \le r_... | For each test case, print one integer β the maximum possible value of \(f(a)\). | In the explanations for the sample test cases, red elements are marked as bold.In the first test case, one of the possible sequences \(a\) is \([\mathbf{6}, 2, \mathbf{-5}, 3, \mathbf{7}, \mathbf{-3}, -4]\).In the second test case, one of the possible sequences \(a\) is \([10, \mathbf{1}, -3, \mathbf{1}, 2, 2]\).In the... | Input: 4 4 6 -5 7 -3 3 2 3 -4 2 1 1 4 10 -3 2 2 5 -1 -2 -3 -4 -5 5 -1 -2 -3 -4 -5 1 0 1 0 | Output: 13 13 0 0 | Beginner | 2 | 975 | 489 | 79 | 14 |
1,996 | B | 1996B | B. Scale | 800 | greedy; implementation | Tina has a square grid with \(n\) rows and \(n\) columns. Each cell in the grid is either \(0\) or \(1\). Tina wants to reduce the grid by a factor of \(k\) (\(k\) is a divisor of \(n\)). To do this, Tina splits the grid into \(k \times k\) nonoverlapping blocks of cells such that every cell belongs to exactly one bloc... | The first line contains \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 1000\), \(1 \le k \le n\), \(k\) is a divisor of \(n\)) β the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid... | For each test case, output the grid reduced by a factor of \(k\) on a new line. | Input: 44 400000000000000006 30001110001110001111110001110001110006 20011000011001111111111111100001100008 11111111111111111111111111111111111111111111111111111111111111111 | Output: 0 01 10 010 111 100 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 | Beginner | 2 | 861 | 601 | 79 | 19 | |
2,106 | C | 2106C | C. Cherry Bomb | 1,000 | greedy; math; sortings | Two integer arrays \(a\) and \(b\) of size \(n\) are complementary if there exists an integer \(x\) such that \(a_i + b_i = x\) over all \(1 \le i \le n\). For example, the arrays \(a = [2, 1, 4]\) and \(b = [3, 4, 1]\) are complementary, since \(a_i + b_i = 5\) over all \(1 \le i \le 3\). However, the arrays \(a = [1,... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le k \le 10^9\)) β the size of the arrays and the maximum possible value of their elements.The second l... | Output exactly one integer, the number of ways Cherry Bomb can fill in the missing elements from \(b\) such that \(a\) and \(b\) are complementary. | In the first example, the only way to fill in the missing elements in \(b\) such that \(a\) and \(b\) are complementary is if \(b = [2, 0, 1]\).In the second example, there is no way to fill in the missing elements of \(b\) such that \(a\) and \(b\) are complementary.In the fourth example, some \(b\) arrays that are co... | Input: 73 101 3 2-1 -1 15 10 1 0 0 1-1 0 1 0 -15 10 1 0 0 1-1 1 -1 1 -15 101 3 2 5 4-1 -1 -1 -1 -15 41 3 2 1 31 -1 -1 1 -15 41 3 2 1 32 -1 -1 2 05 55 0 5 4 35 -1 -1 -1 -1 | Output: 1 0 0 7 0 1 0 | Beginner | 3 | 867 | 617 | 147 | 21 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.