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,146 | F | 1146F | F. Leaf Partition | 2,500 | dp; trees | You are given a rooted tree with \(n\) nodes, labeled from \(1\) to \(n\). The tree is rooted at node \(1\). The parent of the \(i\)-th node is \(p_i\). A leaf is node with no children. For a given set of leaves \(L\), let \(f(L)\) denote the smallest connected subgraph that contains all leaves \(L\).You would like to ... | The first line contains an integer \(n\) (\(2 \leq n \leq 200\,000\)) β the number of nodes in the tree.The next line contains \(n-1\) integers \(p_2, p_3, \ldots, p_n\) (\(1 \leq p_i < i\)). | Print a single integer, the number of ways to partition the leaves, modulo \(998244353\). | In the first example, the leaf nodes are \(2,3,4,5\). The ways to partition the leaves are in the following image In the second example, the only leaf is node \(10\) so there is only one partition. Note that node \(1\) is not a leaf. | Input: 5 1 1 1 1 | Output: 12 | Expert | 2 | 641 | 191 | 89 | 11 |
1,856 | A | 1856A | A. Tales of a Sort | 800 | implementation | Alphen has an array of positive integers \(a\) of length \(n\).Alphen can perform the following operation: For all \(i\) from \(1\) to \(n\), replace \(a_i\) with \(\max(0, a_i - 1)\). Alphen will perform the above operation until \(a\) is sorted, that is \(a\) satisfies \(a_1 \leq a_2 \leq \ldots \leq a_n\). How many ... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 50\)) β the length of the array \(a\).The second lin... | For each test case, output a single integer β the number of operations that Alphen will perform. | In the first test case, we have \(a=[1,2,3]\). Since \(a\) is already sorted, Alphen will not need to perform any operations. So, the answer is \(0\).In the second test case, we have \(a=[2,1,2,1,2]\). Since \(a\) is not initially sorted, Alphen will perform one operation to make \(a=[1,0,1,0,1]\). After performing one... | Input: 731 2 352 1 2 1 243 1 5 427 754 1 3 2 552 3 1 4 531000000000 1 2 | Output: 0 2 5 0 4 3 1000000000 | Beginner | 1 | 462 | 450 | 96 | 18 |
2,107 | A | 2107A | A. LRC and VIP | 800 | greedy; number theory | You have an array \(a\) of size \(n\) β \(a_1, a_2, \ldots a_n\). You need to divide the \(n\) elements into \(2\) sequences \(B\) and \(C\), satisfying the following conditions: Each element belongs to exactly one sequence. Both sequences \(B\) and \(C\) contain at least one element. \(\gcd\) \((B_1, B_2, \ldots, B_{|... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \le n \le 100\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n... | For each test case, first output \(\texttt{Yes}\) if a solution exists or \(\texttt{No}\) if no solution exists. You may print each character in either case, for example \(\texttt{YES}\) and \(\texttt{yEs}\) will also be accepted.Only when there is a solution, output \(n\) integers on the second line. The \(i\)-th numb... | In the first test case, \(B = [51, 9]\) and \(C = [1, 20]\). You can verify \(\gcd(B_1, B_2) = 3 \ne 1 = \gcd(C_1, C_2)\).In the second test case, it is impossible to find a solution. For example, suppose you distributed the first \(3\) elements to array \(B\) and then the last element to array \(C\). You have \(B = [5... | Input: 341 20 51 945 5 5 531 2 2 | Output: Yes 2 2 1 1 No Yes 1 2 2 | Beginner | 2 | 479 | 348 | 549 | 21 |
967 | A | 967A | A. Mind the Gap | 1,100 | implementation | These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts \(1\) minute.He was asked to insert one takeoff in the schedule. The takeoff takes \(1\) min... | The first line of input contains two integers \(n\) and \(s\) (\(1 \le n \le 100\), \(1 \le s \le 60\)) β the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff.Each of next \(n\) lines contains two integers \(h\) and \(m\) (\(0 \le h \le 23\), \(0 \le m \le 59\... | Print two integers \(h\) and \(m\) β the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff. | In the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than \(24\) hours to insert th... | Input: 6 600 01 203 215 019 3023 40 | Output: 6 1 | Easy | 1 | 520 | 493 | 136 | 9 |
1,980 | G | 1980G | G. Yasya and the Mysterious Tree | 2,300 | bitmasks; data structures; dfs and similar; graphs; greedy; strings; trees | Yasya was walking in the forest and accidentally found a tree with \(n\) vertices. A tree is a connected undirected graph with no cycles.Next to the tree, the girl found an ancient manuscript with \(m\) queries written on it. The queries can be of two types.The first type of query is described by the integer \(y\). The... | The first line contains an integer \(t\) (\(1 \le t \le 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 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the number of vertices in the tree and the number of que... | For each test case, output the answers to the queries of the second type. | Input: 23 71 2 13 1 8^ 5? 2 9^ 1? 1 10^ 6? 3 1? 2 95 61 2 7773 2 28124 1 165 3 1000000000^ 4? 3 123? 5 1000000000^ 1000000000? 1 908070? 2 1 | Output: 13 15 11 10 1000000127 2812 999756331 999999756 | Expert | 7 | 1,280 | 915 | 73 | 19 | |
249 | E | 249E | E. Endless Matrix | 2,600 | math | A Russian space traveller Alisa Selezneva, like any other schoolgirl of the late 21 century, is interested in science. She has recently visited the MIT (Moscow Institute of Time), where its chairman and the co-inventor of the time machine academician Petrov told her about the construction of a time machine.During the d... | The first input line contains a single integer t (1 β€ t β€ 105) β the number of test sets for which you should solve the problem. Each of the next t lines contains the description of a test β four positive integers x1, y1, x2 and y2 (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109), separated by spaces. | For each query print the meaning of the expression if it contains at most 10 characters. Otherwise, print three characters ""."" (without the quotes), and then ten last digits of the time expression. Print the answer to each query on a single line. Follow the format, given in the sample as closely as possible. | Input: 51 1 1 12 2 3 32 3 5 6100 87 288 20024 2 5 4 | Output: 124300...5679392764111 | Expert | 1 | 1,661 | 292 | 311 | 2 | |
198 | D | 198D | D. Cube Snake | 2,700 | constructive algorithms | You've got an n Γ n Γ n cube, split into unit cubes. Your task is to number all unit cubes in this cube with positive integers from 1 to n3 so that: each number was used as a cube's number exactly once; for each 1 β€ i < n3, unit cubes with numbers i and i + 1 were neighbouring (that is, shared a side); for each 1 β€ i <... | The first line contains a single integer n (1 β€ n β€ 50) β the size of the cube, whose unit cubes need to be numbered. | Print all layers of the cube as n n Γ n matrices. Separate them with new lines. Print the layers in the order in which they follow in the cube. See the samples for clarifications. It is guaranteed that there always is a solution that meets the conditions given in the problem statement. | In the sample the cubes with sizes 2 Γ 2 Γ 2 are numbered with integers 1, ..., 8 and 5, ..., 12. | Input: 3 | Output: 1 4 17 2 3 18 27 26 19 8 5 16 7 6 15 24 25 20 9 12 13 10 11 14 23 22 21 | Master | 1 | 749 | 117 | 286 | 1 |
429 | B | 429B | B. Working out | 1,600 | dp | Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.Iahub starts with wo... | The first line of the input contains two integers n and m (3 β€ n, m β€ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 β€ a[i][j] β€ 105). | The output contains a single number β the maximum total gain possible. | Iahub will choose exercises a[1][1] β a[1][2] β a[2][2] β a[3][2] β a[3][3]. Iahubina will choose exercises a[3][1] β a[2][1] β a[2][2] β a[2][3] β a[1][3]. | Input: 3 3100 100 100100 1 100100 100 100 | Output: 800 | Medium | 1 | 1,231 | 194 | 70 | 4 |
2,073 | G | 2073G | 2,200 | Hard | 0 | 0 | 0 | 0 | 20 | |||||||
2,050 | B | 2050B | B. Transfusion | 1,100 | brute force; greedy; math | You are given an array \(a\) of length \(n\). In one operation, you can pick an index \(i\) from \(2\) to \(n-1\) inclusive, and do one of the following actions: Decrease \(a_{i-1}\) by \(1\), then increase \(a_{i+1}\) by \(1\). Decrease \(a_{i+1}\) by \(1\), then increase \(a_{i-1}\) by \(1\). After each operation, al... | First line of input consists of one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.First line of each test case consists of one integer \(n\) (\(3 \le n \le 2\cdot 10^5\)).Second line of each test case consists of \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)).It is guaranteed that the sum of \(n\) of... | For each test case, print ""YES"" without quotation marks if it is possible to make all the elements equal after any number of operations; otherwise, print ""NO"" without quotation marks.You can print answers in any register: ""yes"", ""YeS"", ""nO"" β will also be considered correct. | Input: 833 2 131 1 341 2 5 441 6 6 156 2 1 4 241 4 2 153 1 2 1 332 4 2 | Output: YES NO YES NO YES NO NO NO | Easy | 3 | 422 | 367 | 285 | 20 | |
1,771 | E | 1771E | E. Hossam and a Letter | 2,500 | brute force; dp; implementation; two pointers | Hossam bought a new piece of ground with length \(n\) and width \(m\), he divided it into an \(n \cdot m\) grid, each cell being of size \(1\times1\).Since Hossam's name starts with the letter 'H', he decided to draw the capital letter 'H' by building walls of size \(1\times1\) on some squares of the ground. Each squar... | The first line of the input contains two integer numbers \(n\), \(m\) (\(1 \le n, m \le 400\)).The next \(n\) lines of the input contain \(m\) characters each, describing the grid. The character '.' stands for a perfect square, the character 'm' stands for a medium square, and the character '#' stands for a bad square. | Print a single integer β the maximum number of walls that form a capital letter 'H'.If it is not possible to draw any letter 'H', print \(0\). | In the first test case, we can't build the letter 'H'.For the second test case, the figure below represents the grid and some of the valid letters 'H'. Perfect, medium, and bad squares are represented with white, yellow, and black colors respectively. | Input: 2 3 #m. .#. | Output: 0 | Expert | 4 | 1,300 | 320 | 142 | 17 |
1,205 | F | 1205F | F. Beauty of a Permutation | 3,400 | constructive algorithms; math | Define the beauty of a permutation of numbers from \(1\) to \(n\) \((p_1, p_2, \dots, p_n)\) as number of pairs \((L, R)\) such that \(1 \le L \le R \le n\) and numbers \(p_L, p_{L+1}, \dots, p_R\) are consecutive \(R-L+1\) numbers in some order. For example, the beauty of the permutation \((1, 2, 5, 3, 4)\) equals \(9... | The first line contains a single integer \(q\) (\(1\le q \le 10\,000\)) β the number of queries.Follow \(q\) lines. Each line contains two integers \(n\), \(k\) (\(1 \le n \le 100\), \(1 \le k \le \frac{n(n+1)}{2}\)) β the length of permutation and needed beauty respectively. | For a query output ""NO"", if such a permutation doesn't exist. Otherwise, output ""YES"", and in the next line output \(n\) numbers β elements of permutation in the right order. | Let's look at the first example.The first query: in \((1)\) there is only one segment consisting of consecutive numbers β the entire permutation.The second query: in \((2, 4, 1, 5, 3)\) there are \(6\) such segments: \([2]\), \([4]\), \([1]\), \([5]\), \([3]\), \([2, 4, 1, 5, 3]\).There is no such permutation for the s... | Input: 4 1 1 5 6 5 8 5 10 | Output: YES 1 YES 2 4 1 5 3 NO YES 2 3 1 4 5 | Master | 2 | 702 | 276 | 178 | 12 |
1,761 | G | 1761G | G. Centroid Guess | 3,500 | interactive; probabilities; trees | This in an interactive problem.There is an unknown tree consisting of \(n\) nodes, which has exactly one centroid. You only know \(n\) at first, and your task is to find the centroid of the tree.You can ask the distance between any two vertices for at most \(2\cdot10^5\) times. Note that the interactor is not adaptive.... | The only line of the input contains an integer \(n\) (\(3\le n\le 7.5\cdot10^4\)) β the number of nodes in the tree. | Here is an image of the tree from the sample. | Input: 5 2 1 2 3 1 1 1 | Output: ? 1 2 ? 1 3 ? 1 4 ? 1 5 ? 2 3 ? 3 4 ? 4 5 ! 3 | Master | 3 | 540 | 116 | 0 | 17 | |
1,480 | A | 1480A | A. Yet Another String Game | 800 | games; greedy; strings | Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string \(s = s_1 s_2 \dots s_n\) of length \(n\) consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index \(i\) (\... | Each test contains multiple test cases. The first line contains \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Description of the test cases follows.The only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 50\)) consisting of lowercase English letters. | For each test case, print the final string in a single line. | In the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to 'b'.In the second test case: Alice changes the first letter to 'a', then Bob changes the second letter to 'z', Alice changes the third letter to 'a' and then Bob changes the fourth letter to 'z'.I... | Input: 3 a bbbb az | Output: b azaz by | Beginner | 3 | 1,132 | 287 | 60 | 14 |
189 | B | 189B | B. Counting Rhombi | 1,300 | brute force; math | You have two positive integers w and h. Your task is to count the number of rhombi which have the following properties: Have positive area. With vertices at integer points. All vertices of the rhombi are located inside or on the border of the rectangle with vertices at points (0, 0), (w, 0), (w, h), (0, h). In other wo... | The first line contains two integers w and h (1 β€ w, h β€ 4000) β the rectangle's sizes. | Print a single number β the number of sought rhombi.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | In the first example there exists only one such rhombus. Its vertices are located at points (1, 0), (2, 1), (1, 2), (0, 1). | Input: 2 2 | Output: 1 | Easy | 2 | 600 | 87 | 194 | 1 |
136 | A | 136A | A. Presents | 800 | implementation | Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.If the... | The first line contains one integer n (1 β€ n β€ 100) β the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi β the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible th... | Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i. | Input: 42 3 4 1 | Output: 4 1 2 3 | Beginner | 1 | 878 | 440 | 123 | 1 | |
158 | B | 158B | B. Taxi | 1,100 | *special; greedy; implementation | After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 β€ si β€ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum nu... | The first line contains integer n (1 β€ n β€ 105) β the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 β€ si β€ 4). The integers are separated by a space, si is the number of children in the i-th group. | Print the single number β the minimum number of taxis necessary to drive all children to Polycarpus. | In the first test we can sort the children into four cars like this: the third group (consisting of four children), the fourth group (consisting of three children), the fifth group (consisting of three children), the first and the second group (consisting of one and two children, correspondingly). There are other ways ... | Input: 51 2 4 3 3 | Output: 4 | Easy | 3 | 458 | 254 | 100 | 1 |
452 | D | 452D | D. Washer, Dryer, Folder | 1,900 | greedy; implementation | You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n1 washing machines, n2 drying machines and n3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before i... | The only line of the input contains seven integers: k, n1, n2, n3, t1, t2, t3 (1 β€ k β€ 104; 1 β€ n1, n2, n3, t1, t2, t3 β€ 1000). | Print one integer β smallest number of minutes to do all your laundry. | In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, ... | Input: 1 1 1 1 5 5 5 | Output: 15 | Hard | 2 | 772 | 127 | 70 | 4 |
1,729 | F | 1729F | F. Kirei and the Linear Function | 1,900 | hashing; math | Given the string \(s\) of decimal digits (0-9) of length \(n\).A substring is a sequence of consecutive characters of a string. The substring of this string is defined by a pair of indexes β with its left and right ends. So, each pair of indexes (\(l, r\)), where \(1 \le l \le r \le n\), corresponds to a substring of t... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of input test cases.The first line of each test case contains a string \(s\), which contains only the characters 0-9 and has a length \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains two integers \(w, m\) (\(1 \le w < n, 1 \le m... | For each request, print in a separate line: left borders of the required substrings: \(L_1\) and \(L_2\); -1 -1 otherwise, if there is no solution. If there are several solutions, minimize \(L_1\) first, and minimize \(L_2\) second. | Consider the first test case of example inputs. In this test case \(n=7\), \(s=\)""1003004"", \(w=4\) and one query \(l_1=1\), \(r_1=2\), \(k_1=1\). Note that \(v(1,2)=10\). We need to find a pair of substrings of length \(4\) such that \(v(L_1, L_1+3)\cdot10+v(L_2,L_2+3)\) has a remainder of \(k_1=1\) when divided by ... | Input: 510030044 11 2 11795720074 22 7 32 7 41112 12 2 600001 21 4 01 4 14841 52 2 02 3 71 2 53 3 82 2 6 | Output: 2 4 1 5 1 2 -1 -1 1 2 -1 -1 1 3 1 3 -1 -1 -1 -1 -1 -1 | Hard | 2 | 1,298 | 852 | 232 | 17 |
1,389 | C | 1389C | C. Good String | 1,500 | brute force; dp; greedy; two pointers | Let's call left cyclic shift of some string \(t_1 t_2 t_3 \dots t_{n - 1} t_n\) as string \(t_2 t_3 \dots t_{n - 1} t_n t_1\).Analogically, let's call right cyclic shift of string \(t\) as string \(t_n t_1 t_2 t_3 \dots t_{n - 1}\).Let's say string \(t\) is good if its left cyclic shift is equal to its right cyclic shi... | The first line contains single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Next \(t\) lines contains test cases β one per line. The first and only line of each test case contains string \(s\) (\(2 \le |s| \le 2 \cdot 10^5\)). Each character \(s_i\) is digit 0β9.It's guaranteed that the total length ... | For each test case, print the minimum number of characters you need to erase from \(s\) to make it good. | In the first test case, you can erase any \(3\) characters, for example, the \(1\)-st, the \(3\)-rd, and the \(4\)-th. You'll get string 51 and it is good.In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.In the third test case, the given string \(s\) is already g... | Input: 3 95831 100120013 252525252525 | Output: 3 5 0 | Medium | 4 | 465 | 363 | 104 | 13 |
2,009 | G2 | 2009G2 | G2. Yunli's Subarray Queries (hard version) | 2,200 | binary search; data structures; dp | This is the hard version of the problem. In this version, it is guaranteed that \(r \geq l+k-1\) for all queries.For an arbitrary array \(b\), Yunli can perform the following operation any number of times: Select an index \(i\). Set \(b_i = x\) where \(x\) is any integer she desires (\(x\) is not limited to the interva... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(k\), and \(q\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\), \(1 \leq q \leq 2 \cdot 10^5\)) β the length of the array, the length of the consecutive subarray, and the number of ... | Output \(\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])\) for each query on a new line. | In the second query of the first testcase, we calculate the following function values: \(f([2,3,2,1,2])=3\) because Yunli can set \(b_3=4\), \(b_4=5\), and \(b_5=6\), making a consecutive subarray of size \(5\) in \(3\) moves. \(f([2,3,2,1,2,3])=2\) because we can set \(b_3=0\) and \(b_2=-1\), making a consecutive suba... | Input: 37 5 31 2 3 2 1 2 31 72 73 78 4 24 3 1 1 2 4 3 23 61 55 4 24 5 1 2 31 41 5 | Output: 6 5 2 2 5 2 3 | Hard | 3 | 837 | 715 | 90 | 20 |
1,802 | B | 1802B | B. Settlement of Guinea Pigs | 1,000 | greedy; implementation; math | Dasha loves guinea pigs very much. In this regard, she decided to settle as many guinea pigs at home as possible and developed a plan for the next \(n\) days. Every day, she will either buy a new guinea pig or call a doctor to examine all her pets.Unfortunately, the store where she was going to buy guinea pigs does not... | The first line of input data contains one number \(t\) (\(1 \leqslant t \leqslant 10^5\)) β the number of input data sets.The first line of each input data set contains one number \(n\) (\(1 \leqslant n \leqslant 10^5\)) β the number of days Dasha has a plan for.The next line contains \(n\) numbers \(b_1, b_2, b_3, \ld... | For each set of input data, output one number β the minimum number of aviaries Dasha needs to buy so that no matter what the genders of the pigs turn out to be, we can settle them so that at no point in time do two guinea pigs of different genders live together. | In the first set of input data, Dasha needs to put each guinea pig in a separate enclosure, since she does not know their gender.In the second set of input data, Dasha will buy \(0\) guinea pigs, which means she will need \(0\) aviaries.In the third set of input data, you even need \(3\) aviaries to put each guinea pig... | Input: 631 1 132 2 251 1 1 2 1101 2 1 2 1 2 1 2 1 2201 2 1 1 1 1 1 2 1 2 1 2 2 1 1 1 1 1 1 1202 1 1 2 1 1 2 1 2 2 1 1 1 2 2 1 1 1 1 2 | Output: 3 0 3 4 12 9 | Beginner | 3 | 1,052 | 681 | 262 | 18 |
448 | D | 448D | D. Multiplication Table | 1,800 | binary search; brute force | Bizon the Champion isn't just charming, he also is very smart.While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n Γ m multiplication table, where the element on the intersection of the i-th row and j-th column equals iΒ·j (the rows and co... | The single line contains integers n, m and k (1 β€ n, m β€ 5Β·105; 1 β€ k β€ nΒ·m). | Print the k-th largest number in a n Γ m multiplication table. | A 2 Γ 3 multiplication table looks like this:1 2 32 4 6 | Input: 2 2 2 | Output: 2 | Medium | 2 | 718 | 77 | 62 | 4 |
1,081 | F | 1081F | F. Tricky Interactor | 2,600 | constructive algorithms; implementation; interactive | This is an interactive problem.Chouti was tired of studying, so he opened the computer and started playing a puzzle game.Long long ago, the boy found a sequence \(s_1, s_2, \ldots, s_n\) of length \(n\), kept by a tricky interactor. It consisted of \(0\)s and \(1\)s only and the number of \(1\)s is \(t\). The boy knows... | For first query \(1,1\), the interactor should flip \([1,1]\) or \([1,4]\). It chose to flip \([1,4]\), so the sequence became 1100.For second query \(1,1\), the interactor should flip \([1,1]\) or \([1,4]\). It again chose to flip \([1,4]\), so the sequence became 0011.For third query \(3,4\), the interactor should fl... | Input: 4 2220 | Output: ? 1 1? 1 1? 3 4! 0011 | Expert | 3 | 1,255 | 0 | 0 | 10 | ||
345 | E | 345E | E. Black Cat Rush | 2,700 | *special | Today you go out of your house and immediately notice that something is weird. Around your door there is a swarm of black cats β all tense paws and twitching tails. As you do your first step, they all dart off and start running towards you. It looks like they want to thwart you!You are moving in a straight line from po... | The first line contains four integers a, v, u, n (1 β€ a β€ 10000; 1 β€ v, u β€ 100; 1 β€ n β€ 1000). Each of the next n lines contains two integers xi, yi ( - 100 β€ xi, yi β€ 100) β location of the i-th cat.It's guaranteed that all cats are located in distinct points. | Output a single integer β what is the greatest number of cats who manage to cross your path? | Input: 1 1 5 40 34 -47 0-2 -2 | Output: 3 | Master | 1 | 771 | 262 | 92 | 3 | |
1,855 | B | 1855B | B. Longest Divisors Interval | 900 | brute force; combinatorics; greedy; math; number theory | Given a positive integer \(n\), find the maximum size of an interval \([l, r]\) of positive integers such that, for every \(i\) in the interval (i.e., \(l \leq i \leq r\)), \(n\) is a multiple of \(i\).Given two integers \(l\le r\), the size of the interval \([l, r]\) is \(r-l+1\) (i.e., it coincides with the number of... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of the description of each test case contains one integer \(n\) (\(1 \leq n \leq 10^{18}\)). | For each test case, print a single integer: the maximum size of a valid interval. | In the first test case, a valid interval with maximum size is \([1, 1]\) (it's valid because \(n = 1\) is a multiple of \(1\)) and its size is \(1\).In the second test case, a valid interval with maximum size is \([4, 5]\) (it's valid because \(n = 40\) is a multiple of \(4\) and \(5\)) and its size is \(2\).In the thi... | Input: 1014099099042044745601699589137065729723659882203458280803877017195378264306201968835781298538648023412808056621000000000000000000 | Output: 1 2 3 6 4 22 3 1 2 2 | Beginner | 5 | 357 | 203 | 81 | 18 |
1,530 | B | 1530B | B. Putting Plates | 800 | constructive algorithms; implementation | To celebrate your birthday you have prepared a festive table! Now you want to seat as many guests as possible.The table can be represented as a rectangle with height \(h\) and width \(w\), divided into \(h \times w\) cells. Let \((i, j)\) denote the cell in the \(i\)-th row and the \(j\)-th column of the rectangle (\(1... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Each of the following \(t\) lines describes one test case and contains two integers \(h\) and \(w\) (\(3 \le h, w \le 20\)) β the height and the width of the table. | For each test case, print \(h\) lines containing \(w\) characters each. Character \(j\) in line \(i\) must be equal to \(1\) if you are putting a plate into cell \((i, j)\), and \(0\) otherwise. If there are multiple answers, print any.All plates must be put on the edge of the table. No two plates can be put into cells... | For the first test case, example output contains the only way to put \(6\) plates on the table.For the second test case, there are many ways to put \(4\) plates on the table, example output contains one of them.Putting more than \(6\) plates in the first test case or more than \(4\) plates in the second test case is im... | Input: 3 3 5 4 4 5 6 | Output: 10101 00000 10101 0100 0001 1000 0010 010101 000000 100001 000000 101010 | Beginner | 2 | 1,187 | 260 | 494 | 15 |
1,639 | D | 1639D | D. Treasure Hunt | 0 | graphs; interactive | All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient ... | Beginner | 2 | 1,839 | 0 | 0 | 16 | ||||
1,285 | D | 1285D | D. Dr. Evil Underscores | 1,900 | bitmasks; brute force; dfs and similar; divide and conquer; dp; greedy; strings; trees | Today, as a friendship gift, Bakry gave Badawy \(n\) integers \(a_1, a_2, \dots, a_n\) and challenged him to choose an integer \(X\) such that the value \(\underset{1 \leq i \leq n}{\max} (a_i \oplus X)\) is minimum possible, where \(\oplus\) denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decid... | The first line contains integer \(n\) (\(1\le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2^{30}-1\)). | Print one integer β the minimum possible value of \(\underset{1 \leq i \leq n}{\max} (a_i \oplus X)\). | In the first sample, we can choose \(X = 3\).In the second sample, we can choose \(X = 5\). | Input: 3 1 2 3 | Output: 2 | Hard | 8 | 426 | 154 | 102 | 12 |
578 | A | 578A | A. A Problem about Polyline | 1,700 | geometry; math | There is a polyline going through points (0, 0) β (x, x) β (2x, 0) β (3x, x) β (4x, 0) β ... - (2kx, 0) β (2kx + x, x) β .... We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x. | Only one line containing two positive integers a and b (1 β€ a, b β€ 109). | Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output - 1 as the answer. | You can see following graphs for sample 1 and sample 3. | Input: 3 1 | Output: 1.000000000000 | Medium | 2 | 273 | 72 | 192 | 5 |
13 | A | 13A | A. Numbers | 1,000 | implementation; math | Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1.Note that all computation... | Input contains one integer number A (3 β€ A β€ 1000). | Output should contain required average value in format Β«X/YΒ», where X is the numerator and Y is the denominator. | In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively. | Input: 5 | Output: 7/3 | Beginner | 2 | 423 | 51 | 112 | 0 |
177 | C2 | 177C2 | C2. Party | 1,500 | brute force; dfs and similar; dsu; graphs | To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friends... | The first line of input contains an integer n β the number of the Beaver's acquaintances. The second line contains an integer k β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi β indices of people who form the i-th pair of friends.The next line contains an integer m β the ... | Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0. | Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members ar... | Input: 981 21 32 34 56 77 88 99 621 67 9 | Output: 3 | Medium | 4 | 1,052 | 757 | 175 | 1 |
1,249 | D1 | 1249D1 | D1. Too Many Segments (easy version) | 1,800 | greedy | The only difference between easy and hard versions is constraints.You are given \(n\) segments on the coordinate axis \(OX\). Segments can intersect, lie inside each other and even coincide. The \(i\)-th segment is \([l_i; r_i]\) (\(l_i \le r_i\)) and it covers all integer points \(j\) such that \(l_i \le j \le r_i\).T... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 200\)) β the number of segments and the maximum number of segments by which each integer point can be covered.The next \(n\) lines contain segments. The \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le... | In the first line print one integer \(m\) (\(0 \le m \le n\)) β the minimum number of segments you need to remove so that there are no bad points.In the second line print \(m\) distinct integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i \le n\)) β indices of segments you remove in any order. If there are multiple answers, ... | Input: 7 2 11 11 9 11 7 8 8 9 7 8 9 11 7 9 | Output: 3 1 4 7 | Medium | 1 | 498 | 368 | 346 | 12 | |
1,474 | D | 1474D | D. Cleaning | 2,200 | data structures; dp; greedy; math | During cleaning the coast, Alice found \(n\) piles of stones. The \(i\)-th pile has \(a_i\) stones.Piles \(i\) and \(i + 1\) are neighbouring for all \(1 \leq i \leq n - 1\). If pile \(i\) becomes empty, piles \(i - 1\) and \(i + 1\) doesn't become neighbouring.Alice is too lazy to remove these stones, so she asked you... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains the single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of piles.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \l... | For each test case, print YES or NO β is it possible to remove all stones using the superability not more than once or not. | In the first test case, you can remove all stones without using a superability: \([1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]\).In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.In the third test case, you can apply superability to th... | Input: 5 3 1 2 1 3 1 1 2 5 2 2 2 1 3 5 2100 1900 1600 3000 1600 2 2443 2445 | Output: YES YES YES YES NO | Hard | 4 | 817 | 464 | 123 | 14 |
1,955 | B | 1955B | B. Progressive Square | 1,000 | constructive algorithms; data structures; implementation; sortings | A progressive square of size \(n\) is an \(n \times n\) matrix. Maxim chooses three integers \(a_{1,1}\), \(c\), and \(d\) and constructs a progressive square according to the following rules:$$$\(a_{i+1,j} = a_{i,j} + c\)$$$$$$\(a_{i,j+1} = a_{i,j} + d\)\(For example, if \)n = 3\(, \)a_{1,1} = 1\(, \)c=2\(, and \)d=3\... | The first line contains an integer \(t\) (\(1 \le t \le {10} ^ 4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(c\), and \(d\) (\(2 \le n \le 500\), \(1 \le c, d \le 10^6\)) β the size of the square and the values of \(c\) and \(d\) as described in the statement.The seco... | For each test case, output ""YES"" in a separate line if a progressive square for the given \(n\), \(c\), and \(d\) can be constructed from the array elements \(a\), otherwise output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" w... | Input: 53 2 33 9 6 5 7 1 10 4 83 2 33 9 6 5 7 1 11 4 82 100 100400 300 400 5003 2 33 9 6 6 5 1 11 4 84 4 415 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15 | Output: NO YES YES NO NO | Beginner | 4 | 853 | 569 | 357 | 19 | |
490 | E | 490E | E. Restoring Increasing Sequence | 2,000 | binary search; brute force; greedy; implementation | Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board. | The first line of the input contains integer n (1 β€ n β€ 105) β the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive. | If the answer exists, print in the first line ""YES"" (without the quotes). Next n lines must contain the sequence of positive integers β a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. A... | Input: 3?181? | Output: YES11819 | Hard | 4 | 312 | 293 | 519 | 4 | |
182 | C | 182C | C. Optimal Sum | 2,000 | data structures; greedy | And here goes another problem on arrays. You are given positive integer len and array a which consists of n integers a1, a2, ..., an. Let's introduce two characteristics for the given array. Let's consider an arbitrary interval of the array with length len, starting in position i. Value , is the modular sum on the chos... | The first line contains two integers n, len (1 β€ len β€ n β€ 105) β the number of elements in the array and the length of the chosen subinterval of the array, correspondingly. The second line contains a sequence consisting of n integers a1, a2, ..., an (|ai| β€ 109) β the original array. The third line contains a single i... | In a single line print the maximum possible optimal sum after no more than k acceptable operations are fulfilled. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | Input: 5 30 -2 3 -5 12 | Output: 10 | Hard | 2 | 1,244 | 438 | 256 | 1 | |
142 | A | 142A | A. Help Farmer | 1,600 | brute force; math | Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simple... | The only line contains integer n from the problem's statement (1 β€ n β€ 109). | Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves.Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred t... | Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32 = 2 Γ 4 Γ 4 hay blocks in his barn, then after the theft the barn has 4 = (2 - 1) Γ (4 - 2) Γ (4 - 2) hay blocks left. Thus, the thieves could have stolen 32 - 4 = 28 hay blocks. If Sam initially had a parallelepiped consisting... | Input: 4 | Output: 28 41 | Medium | 2 | 1,487 | 76 | 370 | 1 |
656 | A | 656A | A. Da Vinci Powers | 1,900 | *special | The input contains a single integer a (0 β€ a β€ 35). | Output a single integer. | Input: 3 | Output: 8 | Hard | 1 | 0 | 51 | 24 | 6 | ||
525 | B | 525B | B. Pasha and String | 1,400 | constructive algorithms; greedy; math; strings | Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m ... | The first line of the input contains Pasha's string s of length from 2 to 2Β·105 characters, consisting of lowercase Latin letters.The second line contains a single integer m (1 β€ m β€ 105) β the number of days when Pasha changed his string.The third line contains m space-separated elements ai (1 β€ ai; 2Β·ai β€ |s|) β the ... | In the first line of the output print what Pasha's string s will look like after m days. | Input: abcdef12 | Output: aedcbf | Easy | 4 | 617 | 394 | 88 | 5 | |
639 | C | 639C | C. Bear and Polynomials | 2,200 | hashing; implementation; math | Limak is a little polar bear. He doesn't have many toys and thus he often plays with polynomials.He considers a polynomial valid if its degree is n and its coefficients are integers not exceeding k by the absolute value. More formally:Let a0, a1, ..., an denote the coefficients, so . Then, a polynomial P(x) is valid if... | The first line contains two integers n and k (1 β€ n β€ 200 000, 1 β€ k β€ 109) β the degree of the polynomial and the limit for absolute values of coefficients.The second line contains n + 1 integers a0, a1, ..., an (|ai| β€ k, an β 0) β describing a valid polynomial . It's guaranteed that P(2) β 0. | Print the number of ways to change one coefficient to get a valid polynomial Q that Q(2) = 0. | In the first sample, we are given a polynomial P(x) = 10 - 9x - 3x2 + 5x3.Limak can change one coefficient in three ways: He can set a0 = - 10. Then he would get Q(x) = - 10 - 9x - 3x2 + 5x3 and indeed Q(2) = - 10 - 18 - 12 + 40 = 0. Or he can set a2 = - 8. Then Q(x) = 10 - 9x - 8x2 + 5x3 and indeed Q(2) = 10 - 18 - 32... | Input: 3 100000000010 -9 -3 5 | Output: 3 | Hard | 3 | 767 | 296 | 93 | 6 |
360 | B | 360B | B. Levko and Array | 2,000 | binary search; dp | Levko has an array that consists of integers: a1, a2, ... , an. But he doesnβt like this array at all.Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula: The less value c(a) is, the more beautiful the array is.Itβs time to change the world and Levko is goi... | The first line contains two integers n and k (1 β€ k β€ n β€ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 β€ ai β€ 109). | A single number β the minimum value of c(a) Levko can get. | In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.In the third sample he can get array: 1, 2, 3, 4, 5, 6. | Input: 5 24 7 4 7 4 | Output: 0 | Hard | 2 | 621 | 152 | 58 | 3 |
1,493 | A | 1493A | A. Anti-knapsack | 800 | constructive algorithms; greedy | You are given two integers \(n\) and \(k\). You are asked to choose maximum number of distinct integers from \(1\) to \(n\) so that there is no subset of chosen numbers with sum equal to \(k\).A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it. | The first line contains the number of test cases \(T\) (\(1 \le T \le 100\)).Each of the next \(T\) lines contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1000\)) β the description of test cases. | For each test case output two lines. In the first line output a single integer \(m\) β the number of chosen integers.In the second line output \(m\) distinct integers from \(1\) to \(n\) β the chosen numbers.If there are multiple answers, print any. You can print the numbers in any order. | Input: 3 3 2 5 3 1 1 | Output: 2 3 1 3 4 5 2 0 | Beginner | 2 | 313 | 205 | 289 | 14 | |
1,796 | F | 1796F | F. Strange Triples | 2,900 | brute force; math; number theory | Let's call a triple of positive integers (\(a, b, n\)) strange if the equality \(\frac{an}{nb} = \frac{a}{b}\) holds, where \(an\) is the concatenation of \(a\) and \(n\) and \(nb\) is the concatenation of \(n\) and \(b\). For the purpose of concatenation, the integers are considered without leading zeroes.For example,... | The only line contains three integers \(A\), \(B\) and \(N\) (\(1 \le A, B \le 10^5\); \(1 \le N \le 10^9\)). | Print one integer β the number of strange triples \((a, b, n\)) such that \(1 \le a < A\), \(1 \le b < B\) and \(1 \le n < N\). | In the first example, there are \(7\) strange triples: \((1, 1, 1\)), (\(1, 4, 6\)), (\(1, 5, 9\)), (\(2, 2, 2\)), (\(2, 5, 6\)), (\(3, 3, 3\)) and (\(4, 4, 4\)). | Input: 5 6 10 | Output: 7 | Master | 3 | 700 | 109 | 127 | 17 |
978 | F | 978F | F. Mentors | 1,500 | binary search; data structures; implementation | In BerSoft \(n\) programmers work, the programmer \(i\) is characterized by a skill \(r_i\).A programmer \(a\) can be a mentor of a programmer \(b\) if and only if the skill of the programmer \(a\) is strictly greater than the skill of the programmer \(b\) \((r_a > r_b)\) and programmers \(a\) and \(b\) are not in a qu... | The first line contains two integers \(n\) and \(k\) \((2 \le n \le 2 \cdot 10^5\), \(0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))\) β total number of programmers and number of pairs of programmers which are in a quarrel.The second line contains a sequence of integers \(r_1, r_2, \dots, r_n\) \((1 \le r_i... | Print \(n\) integers, the \(i\)-th number should be equal to the number of programmers, for which the \(i\)-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. | In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can b... | Input: 4 210 4 10 151 24 3 | Output: 0 0 1 2 | Medium | 3 | 569 | 767 | 223 | 9 |
1,486 | D | 1486D | D. Max Median | 2,100 | binary search; data structures; dp | You are a given an array \(a\) of length \(n\). Find a subarray \(a[l..r]\) with length at least \(k\) with the largest median.A median in an array of length \(n\) is an element which occupies position number \(\lfloor \frac{n + 1}{2} \rfloor\) after we sort the elements in non-decreasing order. For example: \(median([... | The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)). | Output one integer \(m\) β the maximum median you can get. | In the first example all the possible subarrays are \([1..3]\), \([1..4]\), \([1..5]\), \([2..4]\), \([2..5]\) and \([3..5]\) and the median for all of them is \(2\), so the maximum possible median is \(2\) too.In the second example \(median([3..4]) = 3\). | Input: 5 3 1 2 3 2 1 | Output: 2 | Hard | 3 | 566 | 183 | 58 | 14 |
1,845 | D | 1845D | D. Rating System | 1,800 | binary search; brute force; data structures; dp; dsu; greedy; math; two pointers | You are developing a rating system for an online game. Every time a player participates in a match, the player's rating changes depending on the results.Initially, the player's rating is \(0\). There are \(n\) matches; after the \(i\)-th match, the rating change is equal to \(a_i\) (the rating increases by \(a_i\) if \... | 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 3 \cdot 10^5\)) β the number of matches.The second line contains \(n\) integer \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\); \(a_i \ne ... | For each test case, print one integer \(m\) (\(-10^{18} \le m \le 10^{18}\)) β the value of \(k\) such that the rating of the player will be the maximum possible when using this value. It can be shown that at least one of the optimal answers meets the constraint \(-10^{18} \le m \le 10^{18}\). | In the first example, if \(k=3\), then the rating changes as follows: \(0 \rightarrow 3 \rightarrow 3 \rightarrow 4 \rightarrow 6\).In the second example, if \(k=0\), then the rating changes as follows: \(0 \rightarrow 0 \rightarrow 0 \rightarrow 0\).In the third example, if \(k=25\), then the rating changes as follows... | Input: 443 -2 1 23-1 -2 -124 275 1 -3 2 -1 -2 2 | Output: 3 0 25 6 | Medium | 8 | 944 | 439 | 294 | 18 |
981 | F | 981F | F. Round Marriage | 2,500 | binary search; graph matchings; greedy | It's marriage season in Ringland!Ringland has a form of a circle's boundary of length \(L\). There are \(n\) bridegrooms and \(n\) brides, and bridegrooms decided to marry brides.Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.All objects in Ringland... | The first line contains two integers \(n\) and \(L\) (\(1 \leq n \leq 2 \cdot 10^{5}\), \(1 \leq L \leq 10^{9}\)) β the number of bridegrooms and brides and the length of Ringland.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < L\)) β the distances from the capital to the castles of brid... | In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride. | In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of \(1\), and the first bride should also walk the same distance. Thus, the inconvenience is equal to \(1\).In the second example let \(p_i\) b... | Input: 2 40 12 3 | Output: 1 | Expert | 3 | 1,009 | 517 | 143 | 9 |
1,365 | G | 1365G | G. Secure Password | 2,800 | bitmasks; combinatorics; constructive algorithms; interactive; math | This is an interactive problem.Ayush devised yet another scheme to set the password of his lock. The lock has \(n\) slots where each slot can hold any non-negative integer. The password \(P\) is a sequence of \(n\) integers, \(i\)-th element of which goes into the \(i\)-th slot of the lock.To set the password, Ayush co... | The first line of input contains one integer \(n\) \((2 \le n \le 1000)\) β the number of slots in the lock. | The array \(A\) in the example is \(\{{1, 2, 4\}}\). The first element of the password is bitwise OR of \(A_2\) and \(A_3\), the second element is bitwise OR of \(A_1\) and \(A_3\) and the third element is bitwise OR of \(A_1\) and \(A_2\). Hence the password sequence is \(\{{6, 5, 3\}}\). | Input: 3 1 2 4 | Output: ? 1 1 ? 1 2 ? 1 3 ! 6 5 3 | Master | 5 | 726 | 108 | 0 | 13 | |
1,776 | H | 1776H | H. Beppa and SwerChat | 1,300 | two pointers | Beppa and her circle of geek friends keep up to date on a group chat in the instant messaging app SwerChat\(^{\text{TM}}\).The group has \(n\) members, excluding Beppa. Each of those members has a unique ID between \(1\) and \(n\). When a user opens a group chat, SwerChat\(^{\text{TM}}\) displays the list of other memb... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of members of the group excluding Bepp... | For each test case, print the minimum number of members that must have been online between 9:00 and 22:00. | In the first test case, members \(4, 5\) must have been online between 9:00 and 22:00.In the second test case, it is possible that nobody has been online between 9:00 and 22:00. | Input: 451 4 2 5 34 5 1 2 361 2 3 4 5 61 2 3 4 5 688 2 4 7 1 6 5 35 6 1 4 8 2 7 3111 | Output: 2 0 4 0 | Easy | 1 | 978 | 882 | 106 | 17 |
1,147 | D | 1147D | D. Palindrome XOR | 2,400 | dfs and similar; graphs | You are given a string \(s\) consisting of characters ""1"", ""0"", and ""?"". The first character of \(s\) is guaranteed to be ""1"". Let \(m\) be the number of characters in \(s\).Count the number of ways we can choose a pair of integers \(a, b\) that satisfies the following: \(1 \leq a < b < 2^m\) When written witho... | The first line contains a single string \(s\) (\(1 \leq |s| \leq 1\,000\)). \(s\) consists only of characters ""1"", ""0"" and ""?"". It is guaranteed that the first character of \(s\) is a ""1"". | Print a single integer, the count of pairs that satisfy the conditions modulo \(998244353\). | For the first example, the pairs in base-2 are \((111, 10001), (11, 10101), (1001, 11111)\). | Input: 10110 | Output: 3 | Expert | 2 | 755 | 196 | 92 | 11 |
1,995 | A | 1995A | A. Diagonals | 800 | brute force; greedy; implementation; math | Vitaly503 is given a checkered board with a side of \(n\) and \(k\) chips. He realized that all these \(k\) chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).Let's denote the cell in the \(i\)-th row and \(j\)-th column as \((i ,j)\). A diagonal is the set of cells... | Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of sets of input data. Then follow the descriptions of the sets of input data.The only line of each set of input data contains two integers \(n\), \(k\) (\(1 \le n \le 100, 0 \le k \le n^2... | For each set of input data, output a single integer β the minimum number of occupied diagonals with at least one chip that he can get after placing all \(k\) chips. | In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal \((2, 1), (1, 2)\), so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on \((1, 2), (2, 1), (1, 1)\) makes 2 diagonals occupied. I... | Input: 71 02 22 32 410 50100 2393 9 | Output: 0 1 2 3 6 3 5 | Beginner | 4 | 654 | 406 | 164 | 19 |
1,821 | F | 1821F | F. Timber | 2,600 | combinatorics; dp; fft; math | There is a beautiful alley with trees in front of a shopping mall. Unfortunately, it has to go to make space for the parking lot.The trees on the alley all grow in a single line. There are \(n\) spots for trees, index \(0\) is the shopping mall, index \(n+1\) is the road and indices from \(1\) to \(n\) are the spots fo... | The only line contains three integers \(n, m\) and \(k\) (\(1 \le m, k \le n \le 3 \cdot 10^5\)) β the number of spots for the trees, the number of trees and the height of each tree. | Print a single integer β the number of different unfortunate alleys with \(m\) trees of height \(k\), modulo \(998\,244\,353\). | Input: 6 1 4 | Output: 4 | Expert | 4 | 1,239 | 182 | 127 | 18 | |
1,893 | C | 1893C | C. Freedom of Choice | 2,000 | brute force; greedy; implementation | Let's define the anti-beauty of a multiset \(\{b_1, b_2, \ldots, b_{len}\}\) as the number of occurrences of the number \(len\) in the multiset.You are given \(m\) multisets, where the \(i\)-th multiset contains \(n_i\) distinct elements, specifically: \(c_{i, 1}\) copies of the number \(a_{i,1}\), \(c_{i, 2}\) copies ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(m\) (\(1 \le m \le 10^5\)) β the number of given multisets.Then, for each \(... | For each test case, output the minimum possible anti-beauty of the multiset \(X\) that you can achieve. | In the first test case, the multisets have the following form: \(\{10, 10, 10, 11, 11, 11, 12\}\). From this multiset, you need to select between \(5\) and \(6\) numbers. \(\{12, 12, 12, 12\}\). From this multiset, you need to select between \(1\) and \(3\) numbers. \(\{12, 13, 13, 13, 13, 13\}\). From this multiset, y... | Input: 733 5 610 11 123 3 11 1 31242 4 412 131 517 1000 10061000 1001 1002 1003 1004 1005 1006147 145 143 143 143 143 14212 48 5048 5025 2521 1 1111 1 12111 1 11221 1 1112 1 11 21 124 8 1011 12 13 143 3 3 32 3 411 122 2 | Output: 1 139 0 1 1 0 0 | Hard | 3 | 1,054 | 1,264 | 103 | 18 |
1,490 | G | 1490G | G. Old Floppy Drive | 1,900 | binary search; data structures; math | Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with \(n\) integers written on it.Polycarp wrote the numbers from the disk into the \(a\) array. It turned out that the drive works according to the following algorithm: the drive takes one positive number \... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of two positive integers \(n\), \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of numbers on the disk and the number of asked questions.The second lin... | Print \(m\) numbers on a separate line for each test case. The \(i\)-th number is: \(-1\) if the drive will run infinitely; the number of seconds the drive will run, otherwise. | Input: 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 | Output: 0 6 2 -1 -1 1 3 | Hard | 3 | 1,486 | 635 | 176 | 14 | |
1,618 | G | 1618G | G. Trader Problem | 2,200 | data structures; dsu; greedy; sortings | Monocarp plays a computer game (yet again!). This game has a unique trading mechanics.To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price \(x\), then he can trade it ... | The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m, q \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the prices of the items Monocarp has.The third line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_i \le 10^9\)) β t... | For each query, print one integer β the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of \(k\) from the query. | Input: 3 4 5 10 30 15 12 31 14 18 0 1 2 3 4 | Output: 55 56 60 64 64 | Hard | 4 | 1,517 | 503 | 166 | 16 | |
1,023 | D | 1023D | D. Array Restoration | 1,700 | constructive algorithms; data structures | Initially there was an array \(a\) consisting of \(n\) integers. Positions in it are numbered from \(1\) to \(n\).Exactly \(q\) queries were performed on the array. During the \(i\)-th query some segment \((l_i, r_i)\) \((1 \le l_i \le r_i \le n)\) was selected and values of elements on positions from \(l_i\) to \(r_i\... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the number of elements of the array and the number of queries perfomed on it.The second line contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le q\)) β the resulting array. If element at some position \(j\) is ... | Print ""YES"" if the array \(a\) can be obtained by performing \(q\) queries. Segments \((l_i, r_i)\) \((1 \le l_i \le r_i \le n)\) are chosen separately for each query. Every position from \(1\) to \(n\) should be covered by at least one segment. Otherwise print ""NO"".If some array can be obtained then print \(n\) in... | In the first example you can also replace \(0\) with \(1\) but not with \(3\).In the second example it doesn't really matter what segments to choose until query \(10\) when the segment is \((1, 3)\).The third example showcases the fact that the order of queries can't be changed, you can't firstly set \((1, 3)\) to \(6\... | Input: 4 31 0 2 3 | Output: YES1 2 2 3 | Medium | 2 | 1,142 | 417 | 603 | 10 |
64 | F | 64F | F. Domain | 2,000 | *special; expression parsing | This problem doesn't contain real-world specifications about domains, just use the problem statement as a formal document to solve the problem.The given string s is a domain name if it contains the characters ""a""-""z"", ""0""-""9"" and dots. No two dots shoud follow one after another (consecutive). The dots split the... | The only line of the input contains given string s. The string may contain any characters with ASCII codes from 33 to 127, inclusive. The string length is between 1 and 100, inclusive. | Print ""YES"" if the given string s is a domain name, or print ""NO"" if it is not. | Input: codeforces.com | Output: YES | Hard | 2 | 502 | 184 | 83 | 0 | |
68 | E | 68E | E. Contact | 2,900 | geometry | Little Petya is preparing for the first contact with aliens. He knows that alien spaceships have shapes of non-degenerate triangles and there will be exactly 4 ships. Landing platform for a ship can be made of 3 special columns located at some points of a Cartesian plane such that these 3 points form a triangle equal t... | Each of 4 lines will contain 6 integers x1 y1 x2 y2 x3 y3 (0 β€ x1, y1, x2, y2, x3, y3 β€ 20), representing 3 points that describe the shape of each of 4 ships. It is guaranteed that 3 points in each line will represent a non-degenerate triangle. | First line should contain minimum number of columns enough to land all spaceships. | In the first test case columns can be put in these points: (0, 0), (1, 0), (3, 0), (1, 2). Note that the second ship can land using last 3 columns.In the second test case following points can be chosen: (0, 0), (0, 1), (1, 0), (0, 2), (2, 0), (0, 5), (5, 0), (0, 17), (17, 0). It is impossible to use less than 9 columns... | Input: 0 0 1 0 1 20 0 0 2 2 20 0 3 0 1 20 0 3 0 2 2 | Output: 4 | Master | 1 | 743 | 244 | 82 | 0 |
749 | B | 749B | B. Parallelogram is Back | 1,200 | brute force; constructive algorithms; geometry | Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwis... | The input consists of three lines, each containing a pair of integer coordinates xi and yi ( - 1000 β€ xi, yi β€ 1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide. | First print integer k β the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.Then print k lines, each containing a pair of ... | If you need clarification of what parallelogram is, please check Wikipedia page:https://en.wikipedia.org/wiki/Parallelogram | Input: 0 01 00 1 | Output: 31 -1-1 11 1 | Easy | 3 | 611 | 214 | 371 | 7 |
119 | E | 119E | E. Alternative Reality | 2,400 | geometry | In the year of 3000 travelling around parallel realities became a routine thing. However one has to take into consideration that travelling like that is highly dangerous as you never know beforehand where you're gonna get...Little Vasya, for instance, found himself in a gaming reality and now he has to successfully com... | The first line contains two integers n and m (1 β€ n β€ 900, 1 β€ m β€ 100) β the number of energetic spheres and the number of levels in the game correspondingly. Each of the following n lines contains three integers xi, yi, zi (0 β€ xi, yi, zi β€ 104) β the coordinates of the center of the i-th sphere. Assume that these po... | Print m numbers, one per line: the i-th line should contain the minimum sum of money needed to complete the i-th level. The absolute or relative error should not exceed 10 - 6. | Input: 4 10 0 00 1 01 0 01 1 00 0 1 | Output: 0.7071067812 | Expert | 1 | 1,583 | 635 | 176 | 1 | |
976 | D | 976D | D. Degree Set | 2,500 | constructive algorithms; graphs; implementation | You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that: there are exactly dn + 1 vertices; there are no self-loops; there are no multiple edges; there are no more than 106 edges; its degree set is equal to d. Vertices should be numbe... | The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set. | In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of ... | Input: 32 3 4 | Output: 83 14 24 52 55 13 22 15 3 | Expert | 3 | 734 | 186 | 334 | 9 | |
141 | E | 141E | E. Clearing Up | 2,300 | constructive algorithms; dp; dsu; graphs | After Santa Claus and his assistant Elf delivered all the presents and made all the wishes come true, they returned to the North Pole and found out that it is all covered with snow. Both of them were quite tired and they decided only to remove the snow from the roads connecting huts. The North Pole has n huts connected... | The first input line contains two positive integers n and m (1 β€ n β€ 103, 1 β€ m β€ 105) β the number of huts and the number of roads. Then follow m lines, each of them contains a road description: the numbers of huts it connects β x and y (1 β€ x, y β€ n) and the person responsible for clearing out this road (""S"" β for ... | Print ""-1"" without the quotes if it is impossible to choose the roads that will be cleared by the given rule. Otherwise print in the first line how many roads should be cleared and in the second line print the numbers of those roads (the roads are numbered from 1 in the order of occurrence in the input). It is allowe... | A path is called simple if all huts on it are pairwise different. | Input: 1 21 1 S1 1 M | Output: 0 | Expert | 4 | 895 | 513 | 466 | 1 |
643 | C | 643C | C. Levels and Regions | 2,400 | dp | Radewoosh is playing a computer game. There are n levels, numbered 1 through n. Levels are divided into k regions (groups). Each region contains some positive number of consecutive levels.The game repeats the the following process: If all regions are beaten then the game ends immediately. Otherwise, the system finds th... | The first line of the input contains two integers n and k (1 β€ n β€ 200 000, 1 β€ k β€ min(50, n)) β the number of levels and the number of regions, respectively.The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 100 000). | Print one real number β the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.Namely: let's assume that your answer is a, and the an... | In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels.In the second sample, it's optimal to split levels into two regions with 3 levels each. | Input: 4 2100 3 5 7 | Output: 5.7428571429 | Expert | 1 | 1,242 | 230 | 401 | 6 |
1,070 | B | 1070B | B. Berkomnadzor | 2,400 | data structures; greedy | Berkomnadzor β Federal Service for Supervision of Communications, Information Technology and Mass Media β is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet.Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 su... | The first line of the input contains single integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β total number of IPv4 subnets in the input.The following \(n\) lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It ... | Output -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output \(t\) β the length of the optimised blacklist, followed by \(t\) subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optim... | Input: 1-149.154.167.99 | Output: 10.0.0.0/0 | Expert | 2 | 3,876 | 645 | 561 | 10 | |
42 | C | 42C | C. Safe cracking | 2,200 | brute force; constructive algorithms | Right now you are to solve a very, very simple problem β to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both... | The single line of the input contains four space-separated integer positive numbers not greater than 109 each β four numbers on the circle in consecutive order. | The output should contain ""-1"" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To ma... | Input: 1 1 1 1 | Output: | Hard | 2 | 423 | 160 | 689 | 0 | |
1,956 | A | 1956A | A. Nene's Game | 800 | binary search; brute force; data structures; games; greedy | Nene invented a new game based on an increasing sequence of integers \(a_1, a_2, \ldots, a_k\).In this game, initially \(n\) players are lined up in a row. In each of the rounds of this game, the following happens: Nene finds the \(a_1\)-th, \(a_2\)-th, \(\ldots\), \(a_k\)-th players in a row. They are kicked out of th... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 250\)). The description of test cases follows.The first line case contains two integers \(k\) and \(q\) (\(1 \le k, q \le 100\)) β the length of the sequence \(a\) and the number of values \(n_i\) you should so... | For each test case, output \(q\) integers: the \(i\)-th (\(1\le i \le q\)) of them should be the number of players declared as winners if initially \(n_i\) players join the game. | The first test case was explained in the statement.In the second test case, when \(n=1\), the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner. | Input: 62 13 555 32 4 6 7 91 3 55 43 4 5 6 71 2 3 42 369 961 10 1001 1100503 310 20 301 10 100 | Output: 2 1 1 1 1 2 2 2 1 10 68 50 1 9 9 | Beginner | 5 | 1,614 | 551 | 178 | 19 |
1,750 | G | 1750G | G. Doping | 3,300 | combinatorics; dp; math | We call an array \(a\) of length \(n\) fancy if for each \(1 < i \le n\) it holds that \(a_i = a_{i-1} + 1\).Let's call \(f(p)\) applied to a permutation\(^\dagger\) of length \(n\) as the minimum number of subarrays it can be partitioned such that each one of them is fancy. For example \(f([1,2,3]) = 1\), while \(f([3... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2000\), \(10 \le m \le 10^9\)) β the length of the permutation and the required modulo.The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the permutation \(p\). | Print \(n\) integers, where the \(k\)-th integer is the number of \(k\)-special permutations modulo \(m\). | In the first example, the permutations that are lexicographically smaller than \([1,3,4,2]\) are: \([1,2,3,4]\), \(f([1,2,3,4])=1\); \([1,2,4,3]\), \(f([1,2,4,3])=3\); \([1,3,2,4]\), \(f([1,3,2,4])=4\). Thus our answer is \([1,0,1,1]\).In the second example, the permutations that are lexicographically smaller than \([3... | Input: 4 666012 1 3 4 2 | Output: 1 0 1 1 | Master | 3 | 1,287 | 276 | 106 | 17 |
626 | C | 626C | C. Block Towers | 1,600 | brute force; greedy; math; number theory | Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.The students donβt want to use too many blocks, but they also want to be unique... | The first line of the input contains two space-separated integers n and m (0 β€ n, m β€ 1 000 000, n + m > 0) β the number of students using two-block pieces and the number of students using three-block pieces, respectively. | Print a single integer, denoting the minimum possible height of the tallest tower. | In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers ... | Input: 1 3 | Output: 9 | Medium | 4 | 462 | 222 | 82 | 6 |
1,695 | A | 1695A | A. Subrectangle Guess | 800 | games | Michael and Joe are playing a game. The game is played on a grid with \(n\) rows and \(m\) columns, filled with distinct integers. We denote the square on the \(i\)-th (\(1\le i\le n\)) row and \(j\)-th (\(1\le j\le m\)) column by \((i, j)\) and the number there by \(a_{ij}\).Michael starts by saying two numbers \(h\) ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 40\)) β the size of the grid.Each of the following \(n\) lines contains \(m\... | For each test case print a single positive integer β the minimum possible area the subrectangle can have while still ensuring that Michael can guarantee the victory. | In the first test case, the grid is \(1\times 1\), so the only possible choice for \(h, w\) is \(h = 1, w = 1\), giving an area of \(h\cdot w = 1\).The grid from the second test case is drawn in the statement. It can be shown that with \(h = 3, w = 3\) Michael can guarantee the victory and that any choice with \(h\cdot... | Input: 31 134 42 12 6 103 15 16 41 13 8 1114 7 9 52 3-7 5 20 8 -3 | Output: 1 9 4 | Beginner | 1 | 1,198 | 581 | 165 | 16 |
921 | 14 | 92114 | 14. Labyrinth-14 | 3,200 | See the problem statement here: http://codeforces.com/contest/921/problem/01. | Master | 0 | 77 | 0 | 0 | 9 | |||||
317 | B | 317B | B. Ants | 2,000 | brute force; implementation | It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x ... | First input line contains integers n (0 β€ n β€ 30000) and t (1 β€ t β€ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 β€ xi, yi β€ 109). Queries may coincide.It is guaranteed that there will be a c... | Print t integers, one per line β the number of ants at the corresponding junctions when the movement of the ants stops. | In the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. | Input: 1 30 10 00 -1 | Output: 010 | Hard | 2 | 633 | 430 | 119 | 3 |
1,912 | L | 1912L | L. LOL Lovers | 800 | strings | There are \(n\) food items lying in a row on a long table. Each of these items is either a loaf of bread (denoted as a capital Latin letter 'L' with ASCII code 76) or an onion (denoted as a capital Latin letter 'O' with ASCII code 79). There is at least one loaf of bread and at least one onion on the table.You and your... | The first line contains one integer \(n\) (\(2 \le n \le 200\)) β the number of food items on the table. The second line contains a string of length \(n\) consisting of letters 'L' and 'O'. \(i\)-th symbol represents the type of the \(i\)-th food item on the table: 'L' stands for a loaf of bread, and 'O' stands for an ... | Print one integer β a number \(k\) such that, if you take \(k\) leftmost items and your friend takes the remaining \(n - k\) items, each of you and your friend get at least one item, your number of loaves is different from your friend's, and your number of onions is different from your friend's. If there are several po... | In the first example, in any division the left and the right part contain one loaf of bread.In the second example, the division is 'L' and 'O', and in these two strings the number of loaves is different (1 and 0) and the number of onions is different (0 and 1).In the third example, any number 1, 2 or 3 is a correct ans... | Input: 3LOL | Output: -1 | Beginner | 1 | 807 | 422 | 413 | 19 |
1,223 | F | 1223F | F. Stack Exterminable Arrays | 2,600 | data structures; divide and conquer; dp; hashing | Let's look at the following process: initially you have an empty stack and an array \(s\) of the length \(l\). You are trying to push array elements to the stack in the order \(s_1, s_2, s_3, \dots s_{l}\). Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then... | The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the length of array \(a\).The second line of each query contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the ... | For each test case print one integer in single line β the number of stack exterminable subarrays of the array \(a\). | In the first query there are four stack exterminable subarrays: \(a_{1 \ldots 4} = [2, 1, 1, 2], a_{2 \ldots 3} = [1, 1], a_{2 \ldots 5} = [1, 1, 2, 2], a_{4 \ldots 5} = [2, 2]\).In the second query, only one subarray is exterminable subarray β \(a_{3 \ldots 4}\).In the third query, there are eight stack exterminable s... | Input: 3 5 2 1 1 2 2 6 1 2 1 1 3 2 9 3 1 2 2 1 6 6 3 3 | Output: 4 1 8 | Expert | 4 | 1,597 | 422 | 116 | 12 |
67 | C | 67C | C. Sequence of Balls | 2,600 | dp | You are given a sequence of balls A by your teacher, each labeled with a lowercase Latin letter 'a'-'z'. You don't like the given sequence. You want to change it into a new sequence, B that suits you better. So, you allow yourself four operations: You can insert any ball with any label into the sequence at any position... | The first line contains four space-separated integers ti, td, tr, te (0 < ti, td, tr, te β€ 100). The following two lines contain sequences A and B on separate lines. The length of each line is between 1 and 4000 characters inclusive. | Print a single integer representing minimum time to convert A into B. | In the second sample, you could delete the ball labeled 'a' from the first position and then insert another 'a' at the new second position with total time 6. However exchanging the balls give total time 3. | Input: 1 1 1 1youshouldnotthoushaltnot | Output: 5 | Expert | 1 | 809 | 233 | 69 | 0 |
1,508 | C | 1508C | C. Complete the MST | 2,500 | bitmasks; brute force; data structures; dfs and similar; dsu; graphs; greedy; trees | As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.You are given an undirected complete graph with \(n\) nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign a... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(0 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2} - 1)\)) β the number of nodes and the number of pre-assigned edges. The inputs are given so that there is at least one unassigned edge.The \(i\)-th of the following \(m\) lines contains ... | Print on one line one integer β the minimum ugliness among all weight assignments with XOR sum equal to \(0\). | The following image showcases the first test case. The black weights are pre-assigned from the statement, the red weights are assigned by us, and the minimum spanning tree is denoted by the blue edges. | Input: 4 4 2 1 14 1 4 14 3 2 15 4 3 8 | Output: 15 | Expert | 8 | 985 | 567 | 110 | 15 |
938 | C | 938C | C. Constructing Tests | 1,700 | binary search; brute force; constructive algorithms | Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m Γ m of this matrix contains at least one zero. Consider the following problem:You are given two integers n and m. You have to construct an m-free square matrix of size n Γ n such that the... | The first line contains one integer t (1 β€ t β€ 100) β the number of tests you have to construct.Then t lines follow, i-th line containing one integer xi (0 β€ xi β€ 109).Note that in hacks you have to set t = 1. | For each test you have to construct, output two positive numbers ni and mi (1 β€ mi β€ ni β€ 109) such that the maximum number of 1's in a mi-free ni Γ ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1. | Input: 32101 | Output: 5 21 1-1 | Medium | 3 | 704 | 209 | 310 | 9 | |
484 | D | 484D | D. Kindergarten | 2,400 | data structures; dp; greedy | In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maxim... | The first line contains integer n β the number of children in the line (1 β€ n β€ 106).The second line contains n integers ai β the charisma of the i-th child ( - 109 β€ ai β€ 109). | Print the maximum possible total sociability of all groups. | In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group. | Input: 51 2 3 1 2 | Output: 3 | Expert | 3 | 617 | 177 | 59 | 4 |
1,515 | F | 1515F | F. Phoenix and Earthquake | 2,600 | constructive algorithms; dfs and similar; dsu; graphs; greedy; trees | Phoenix's homeland, the Fire Nation had \(n\) cities that were connected by \(m\) roads, but the roads were all destroyed by an earthquake. The Fire Nation wishes to repair \(n-1\) of these roads so that all the cities are connected again. The \(i\)-th city has \(a_i\) tons of asphalt. \(x\) tons of asphalt are used up... | The first line contains integers \(n\), \(m\), and \(x\) (\(2 \le n \le 3 \cdot 10^5\); \(n-1 \le m \le 3 \cdot 10^5\); \(1 \le x \le 10^9\)) β the number of cities, number of roads, and amount of asphalt needed to repair one road.The next line contains \(n\) space-separated integer \(a_i\) (\(0 \le a_i \le 10^9\)) β t... | If it is not possible to connect all the cities, print NO. Otherwise, print YES followed by \(n-1\) integers \(e_1, e_2, \dots, e_{n-1}\), the order in which the roads should be repaired. \(e_i\) is the index of the \(i\)-th road to repair. If there are multiple solutions, print any. | In the first example, the roads are repaired in the following order: Road \(3\) is repaired, connecting cities \(3\) and \(4\). City \(4\) originally had \(4\) tons of asphalt. After this road is constructed, \(3\) tons remain. Road \(2\) is repaired, connecting cities \(2\) and \(3\). The asphalt from city \(4\) can b... | Input: 5 4 1 0 0 0 4 0 1 2 2 3 3 4 4 5 | Output: YES 3 2 1 4 | Expert | 6 | 829 | 658 | 284 | 15 |
1,506 | F | 1506F | F. Triangular Paths | 2,000 | constructive algorithms; graphs; math; shortest paths; sortings | Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The \(k\)-th layer of the triangle contains \(k\) points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers \((r, c)\) (\(1 \le ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) is the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) is the number of points to visit.The second line contains \(n\) numbers \(r_1, r_2, \ldots, r_n\) (\(1 ... | For each test case, output the minimum cost of a path passing through all points in the corresponding test case. | Input: 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 | Output: 0 1 999999999 2 | Hard | 5 | 1,862 | 805 | 112 | 15 | |
1,355 | A | 1355A | A. Sequence with Digits | 1,200 | brute force; implementation; math | Let's define the following recurrence: $$$\(a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).\)\(Here \)minDigit(x)\( and \)maxDigit(x)\( are the minimal and maximal digits in the decimal representation of \)x\( without leading zeroes. For examples refer to notes.Your task is calculate \)a_{K}\( for given \)a_{1... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of independent test cases.Each test case consists of a single line containing two integers \(a_{1}\) and \(K\) (\(1 \le a_{1} \le 10^{18}\), \(1 \le K \le 10^{16}\)) separated by a space. | For each test case print one integer \(a_{K}\) on a separate line. | \(a_{1} = 487\) \(a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519\) \(a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528\) \(a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{... | Input: 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 | Output: 42 487 519 528 544 564 588 628 | Easy | 3 | 335 | 265 | 66 | 13 |
237 | E | 237E | E. Build String | 2,000 | flows; graphs | You desperately need to build some string t. For that you've got n more strings s1, s2, ..., sn. To build string t, you are allowed to perform exactly |t| (|t| is the length of string t) operations on these strings. Each operation looks like that: choose any non-empty string from strings s1, s2, ..., sn; choose an arbi... | The first line of the input contains string t β the string that you need to build.The second line contains a single integer n (1 β€ n β€ 100) β the number of strings to which you are allowed to apply the described operation. Each of the next n lines contains a string and an integer. The i-th line contains space-separated... | Print a single number β the minimum money (in rubles) you need in order to build string t. If there is no solution, print -1. | Notes to the samples:In the first sample from the first string you should take characters ""b"" and ""z"" with price 1 ruble, from the second string characters ""a"", ""e"" ΠΈ ""b"" with price 2 rubles. The price of the string t in this case is 2Β·1 + 3Β·2 = 8.In the second sample from the first string you should take two... | Input: bbaze3bzb 2aeb 3ba 10 | Output: 8 | Hard | 2 | 1,313 | 602 | 125 | 2 |
261 | D | 261D | D. Maxim and Increasing Subsequence | 2,600 | dp | Maxim loves sequences, especially those that strictly increase. He is wondering, what is the length of the longest increasing subsequence of the given sequence a?Sequence a is given as follows: the length of the sequence equals n Γ t; (1 β€ i β€ n Γ t), where operation means taking the remainder after dividing number x b... | The first line contains four integers k, n, maxb and t (1 β€ k β€ 10; 1 β€ n, maxb β€ 105; 1 β€ t β€ 109; n Γ maxb β€ 2Β·107). Each of the next k lines contain n integers b1, b2, ..., bn (1 β€ bi β€ maxb). Note that for each variant of the sequence a the values n, maxb and t coincide, the only arrays bs differ.The numbers in the... | Print k integers β the answers for the variants of the sequence a. Print the answers in the order the variants follow in the input. | Input: 3 3 5 23 2 11 2 32 3 1 | Output: 233 | Expert | 1 | 846 | 358 | 131 | 2 | |
1,609 | D | 1609D | D. Social Network | 1,600 | dsu; graphs; greedy; implementation; trees | William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies.The conference has \(n\) participants, who are initially unfamiliar with each other. William can introduc... | The first line contains two integers \(n\) and \(d\) (\(2 \le n \le 10^3, 1 \le d \le n - 1\)), the number of people, and number of conditions, respectively.Each of the next \(d\) lines each contain two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n, x_i \neq y_i\)), the numbers of people which must have a connec... | Output \(d\) integers. \(i\)th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed \(i\) introductions and satisfied the first \(i\) conditions. | The explanation for the first test case:In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people. | Input: 7 6 1 2 3 4 2 4 7 6 6 5 1 7 | Output: 1 1 3 3 3 6 | Medium | 5 | 1,276 | 354 | 221 | 16 |
1,341 | A | 1341A | A. Nastya and Rice | 900 | math | Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped \(n\) grains. Nastya read that each grain weighs some integer number of grams from \(a - b\) to \(a + b\), inclusive (numbers \(a\) and \(b\) are ... | The input consists of multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 1000)\) β the number of test cases. The next \(t\) lines contain descriptions of the test cases, each line contains \(5\) integers: \(n\) \((1 \leq n \leq 1000)\) β the number of grains that Nastya counted and \(a... | For each test case given in the input print ""Yes"", if the information about the weights is not inconsistent, and print ""No"" if \(n\) grains with masses from \(a - b\) to \(a + b\) cannot make a package with a total mass from \(c - d\) to \(c + d\). | In the first test case of the example, we can assume that each grain weighs \(17\) grams, and a pack \(119\) grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs \(16\) grams, and a pack \(128\) grams, then really Nastya could collect the who... | Input: 5 7 20 3 101 18 11 11 10 234 2 8 9 7 250 122 19 41 21 321 10 3 10 8 6 1 | Output: Yes No Yes No Yes | Beginner | 1 | 864 | 556 | 252 | 13 |
687 | C | 687C | C. The Values You Can Make | 1,900 | dp | Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya... | The first line contains two integers n and k (1 β€ n, k β€ 500) β the number of coins and the price of the chocolate, respectively.Next line will contain n integers c1, c2, ..., cn (1 β€ ci β€ 500) β the values of Pari's coins.It's guaranteed that one can make value k using these coins. | First line of the output must contain a single integer qβ the number of suitable values x. Then print q integers in ascending order β the values that Arya can make for some subset of coins of Pari that pays for the chocolate. | Input: 6 185 6 1 10 12 2 | Output: 160 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 | Hard | 1 | 796 | 283 | 225 | 6 | |
1,634 | E | 1634E | E. Fair Share | 2,400 | constructive algorithms; data structures; dfs and similar; graph matchings; graphs | Even a cat has things it can do that AI cannot.β Fei-Fei LiYou are given \(m\) arrays of positive integers. Each array is of even length.You need to split all these integers into two equal multisets \(L\) and \(R\), that is, each element of each array should go into one of two multisets (but not both). Additionally, fo... | The first line contains an integer \(m\) (\(1 \le m \le 10 ^ 5\)) β the number of arrays.The next \(2 \cdot m\) lines contain descriptions of the arrays.For each array, the first line contains an even integer \(n\) (\(2 \le n \le 2 \cdot 10 ^ 5\)) β the length of the array. The second line consists of \(n\) space-separ... | If the answer exists, print ""YES"", and then print \(m\) lines.On each line, for each element, print the letter ""L"" or ""R"" (capitalized, without spaces), depending on which multiset the element should go into.If there is no answer, print ""NO"" on the only line. | In the first array, we add the first element to \(R\) and the second to \(L\). Now \(L = \{2\}\), and \(R = \{1\}\).In the second array, we add the first and third elements to \(L\) and the rest to \(R\). Now \(L = \{1, 2, 3\}\) and \(R = \{1, 2, 3\}\).In the third array, we add elements 2, 3, and 6 to \(L\), and other... | Input: 3 2 1 2 4 1 2 3 3 6 1 1 2 2 3 3 | Output: YES RL LRLR RLLRRL | Expert | 5 | 510 | 491 | 267 | 16 |
1,260 | E | 1260E | E. Tournament | 2,400 | brute force; dp; greedy | You are organizing a boxing tournament, where \(n\) boxers will participate (\(n\) is a power of \(2\)), and your friend is one of them. All boxers have different strength from \(1\) to \(n\), and boxer \(i\) wins in the match against boxer \(j\) if and only if \(i\) is stronger than \(j\).The tournament will be organi... | The first line contains one integer \(n\) (\(2 \le n \le 2^{18}\)) β the number of boxers. \(n\) is a power of \(2\).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\), where \(a_i\) is the number of dollars you have to pay if you want to bribe the boxer with strength \(i\). Exactly one of \(a_i\) i... | Print one integer β the minimum number of dollars you have to pay so your friend wins. | In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.In the second test case you can distribute boxers as follows (your friend is number \(2\)):\(1 : 2, 8 : 5, 7 : 3, 6 : 4\) (boxers \(2, 8, 7\) and \(6\) advance to the next stage)... | Input: 4 3 9 1 -1 | Output: 0 | Expert | 3 | 1,234 | 448 | 86 | 12 |
218 | B | 218B | B. Airport | 1,100 | implementation | Lolek and Bolek are about to travel abroad by plane. The local airport has a special ""Choose Your Plane"" offer. The offer's conditions are as follows: it is up to a passenger to choose a plane to fly on; if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys... | The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 β€ ai β€ 1000) β ai stands for the number of empty seats in the i-th plane before the ticket office starts s... | Print two integers β the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly. | In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person β to the 2-nd plan... | Input: 4 32 1 1 | Output: 5 5 | Easy | 1 | 814 | 450 | 124 | 2 |
1,983 | G | 1983G | G. Your Loss | 3,000 | bitmasks; brute force; dp; trees | You are given a tree with \(n\) nodes numbered from \(1\) to \(n\), along with an array of size \(n\). The value of \(i\)-th node is \(a_{i}\). There are \(q\) queries. In each query, you are given 2 nodes numbered as \(x\) and \(y\). Consider the path from the node numbered as \(x\) to the node numbered as \(y\). Let ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Each test case contains several sets of input data.The first line of each set of input data contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the number of nodes.The next \(n-1\) lines of each set of input d... | For each query, output a single number β the sum from the problem statement. | Input: 141 22 33 42 3 6 531 43 41 1 | Output: 14 10 2 | Master | 4 | 614 | 1,084 | 76 | 19 | |
1,534 | F2 | 1534F2 | F2. Falling Sand (Hard Version) | 3,000 | dfs and similar; dp; graphs; greedy | This is the hard version of the problem. The difference between the versions is the constraints on \(a_i\). You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with \(n\) ro... | The first line consists of two space-separated positive integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 400\,000\)).Each of the next \(n\) lines contains \(m\) characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of san... | Print one non-negative integer, the minimum amount of operations needed to solve the puzzle. | For example \(1\), by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that th... | Input: 5 7 #....#. .#.#... #....#. #....## #.#.... 4 1 1 1 0 3 1 | Output: 3 | Master | 4 | 1,704 | 628 | 92 | 15 |
1,245 | A | 1245A | A. Good ol' Numbers Coloring | 1,000 | math; number theory | Consider the set of all nonnegative integers: \({0, 1, 2, \dots}\). Given two integers \(a\) and \(b\) (\(1 \le a, b \le 10^4\)). We paint all the numbers in increasing number first we paint \(0\), then we paint \(1\), then \(2\) and so on.Each number is painted white or black. We paint a number \(i\) according to the ... | The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then \(t\) lines follow, each line contains two space-separated integers \(a\) and \(b\) (\(1 \le a, b \le 10^4\)). | For each test case, print one line containing either ""Infinite"" or ""Finite"" (without the quotes). Output is case-insensitive (i.e. ""infinite"", ""inFiNite"" or ""finiTE"" are all valid answers). | Input: 4 10 10 1 10 6 9 7 3 | Output: Infinite Finite Infinite Finite | Beginner | 2 | 1,657 | 234 | 199 | 12 | |
1,666 | L | 1666L | L. Labyrinth | 1,800 | dfs and similar; graphs | Leslie and Leon entered a labyrinth. The labyrinth consists of \(n\) halls and \(m\) one-way passages between them. The halls are numbered from \(1\) to \(n\).Leslie and Leon start their journey in the hall \(s\). Right away, they quarrel and decide to explore the labyrinth separately. However, they want to meet again ... | The first line contains three integers \(n\), \(m\), and \(s\), where \(n\) (\(2 \le n \le 2 \cdot 10^5\)) is the number of vertices, \(m\) (\(0 \le m \le 2 \cdot 10^5\)) is the number of edges in the labyrinth, and \(s\) (\(1 \le s \le n\)) is the starting hall.Then \(m\) lines with descriptions of passages follow. Ea... | If it is possible to find the desired two paths, output ""Possible"", otherwise output ""Impossible"".If the answer exists, output two path descriptions. Each description occupies two lines. The first line of the description contains an integer \(h\) (\(2 \le h \le n\)) β the number of halls in a path, and the second l... | Input: 5 5 1 1 2 2 3 1 4 4 3 3 5 | Output: Possible 3 1 2 3 3 1 4 3 | Medium | 2 | 922 | 649 | 607 | 16 | |
331 | B2 | 331B2 | B2. Shave Beaver! | 1,900 | data structures | The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, ""Beavershave 5000"". Beavershave 5000 can shave beavers by families! How does it work? Very easily!There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., a... | The first line contains integer n β the total number of beavers, 2 β€ n. The second line contains n space-separated integers β the initial beaver permutation.The third line contains integer q β the number of queries, 1 β€ q β€ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query... | For each query with pi = 1, print the minimum number of Beavershave 5000 sessions. | Input: 51 3 4 2 561 1 51 3 42 2 31 1 52 1 51 1 5 | Output: 2135 | Hard | 1 | 1,375 | 756 | 82 | 3 | |
1,512 | E | 1512E | E. Permutation by Sum | 1,600 | brute force; greedy; math | A permutation is a sequence of \(n\) integers from \(1\) to \(n\), in which all the numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) are permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) are not.Polycarp was given four integers \(n\), \(l\), \(r\) (\(1 \le l \le r \le n)\) and ... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)). Then \(t\) test cases follow.Each test case consist of one line with four integers \(n\) (\(1 \le n \le 500\)), \(l\) (\(1 \le l \le n\)), \(r\) (\(l \le r \le n\)), \(s\) (\(1 \le s \le \frac{n (n+1)}{2}\)).It is guaranteed that the sum of \(n\) for... | For each test case, output on a separate line: \(n\) integers β a permutation of length \(n\) that fits the condition above if such a permutation exists; -1, otherwise. If there are several suitable permutations, print any of them. | Input: 5 5 2 3 5 5 3 4 1 3 1 2 4 2 2 2 2 2 1 1 3 | Output: 1 2 3 4 5 -1 1 3 2 1 2 -1 | Medium | 3 | 1,033 | 365 | 231 | 15 | |
2,114 | G | 2114G | G. Build an Array | 2,200 | brute force; constructive algorithms; dp; greedy; math; number theory | Yesterday, Dima found an empty array and decided to add some integers to it. He can perform the following operation an unlimited number of times: add any integer to the left or right end of the array. then, as long as there is a pair of identical adjacent elements in the array, they will be replaced by their sum. It ca... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case description contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(n \le k \le 10^6\)) β the length of the resulting array and the number o... | For each test case, if there is no suitable sequence of operations of length \(k\), output ""NO"". Otherwise, output ""YES"".You may output ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer). | Input: 83 32 1 43 72 1 42 152 163 10256 32 13 289768 96 13 290768 96 15 75 1 6 3 104 66 8 5 10 | Output: YES NO YES YES YES NO YES YES | Hard | 6 | 970 | 563 | 277 | 21 | |
667 | B | 667B | B. Coat of Anticubism | 1,100 | constructive algorithms; geometry | As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-di... | The first line contains an integer n (3 β€ n β€ 105) β a number of rod-blanks.The second line contains n integers li (1 β€ li β€ 109) β lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has. | Print the only integer z β the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods. | In the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}. | Input: 31 2 1 | Output: 1 | Easy | 2 | 1,462 | 301 | 189 | 6 |
1,607 | D | 1607D | D. Blue-Red Permutation | 1,300 | greedy; math; sortings | You are given an array of integers \(a\) of length \(n\). The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either yo... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of input data sets in the test.The description of each set of input data consists of three lines. The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the original array \(a\). The second line contains \(... | Print \(t\) lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise.You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positi... | In the first test case of the example, the following sequence of moves can be performed: choose \(i=3\), element \(a_3=5\) is blue, so we decrease it, we get \(a=[1,2,4,2]\); choose \(i=2\), element \(a_2=2\) is red, so we increase it, we get \(a=[1,3,4,2]\); choose \(i=3\), element \(a_3=4\) is blue, so we decrease it... | Input: 8 4 1 2 5 2 BRBR 2 1 1 BB 5 3 1 4 2 5 RBRRB 5 3 1 3 1 3 RBRRB 5 5 1 5 1 5 RBRRB 4 2 2 2 2 BRBR 2 1 -2 BR 4 -2 -1 4 0 RRRR | Output: YES NO YES YES NO YES YES YES | Easy | 3 | 980 | 686 | 331 | 16 |
1,075 | B | 1075B | B. Taxi drivers and Lyft | 1,200 | implementation; sortings | Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all \(m\) taxi drivers in the city, who every day transport the rest of the city residents β \(n\) riders.Each resident (including taxi drivers) of ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^5\)) β number of riders and taxi drivers.The second line contains \(n + m\) integers \(x_1, x_2, \ldots, x_{n+m}\) (\(1 \le x_1 < x_2 < \ldots < x_{n+m} \le 10^9\)), where \(x_i\) is the coordinate where the \(i\)-th resident lives. The third line... | Print \(m\) integers \(a_1, a_2, \ldots, a_{m}\), where \(a_i\) is the answer for the \(i\)-th taxi driver. The taxi driver has the number \(i\) if among all the taxi drivers he lives in the \(i\)-th smallest coordinate (see examples for better understanding). | In the first example, we have only one taxi driver, which means an order from any of \(n\) riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate \(2\), and the second one lives at the point with the coordinate \(6\). Obviously, the nearest taxi driver to the rider who... | Input: 3 11 2 3 100 0 1 0 | Output: 3 | Easy | 2 | 1,117 | 567 | 260 | 10 |
120 | G | 120G | G. Boom | 1,800 | implementation | Let's consider the famous game called Boom (aka Hat) with simplified rules.There are n teams playing the game. Each team has two players. The purpose of the game is to explain the words to the teammate without using any words that contain the same root or that sound similarly. Player j from team i (1 β€ i β€ n, 1 β€ j β€ 2... | The first line contains two integers n, t (1 β€ n, t β€ 100), which correspondingly denote the number of teams and a turn's duration.Next n lines of the input file contain four integers each: ai1, bi1, ai2, bi2 (1 β€ aij, bij β€ 100) β the skills of the first and the second player of the i-th team. The teams are given in t... | Print n lines. On the i-th line first print number si the number of points the i-th team wins. Then print si space-separated words β the words from the cards guessed by team i in the order in which they were guessed. | Input: 2 21 1 1 11 1 1 13home1car1brother1 | Output: 2 home car 1 brother | Medium | 1 | 2,386 | 899 | 216 | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.