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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
952 | A | 952A | A. Quirky Quantifiers | 800 | math | The input contains a single integer a (10 β€ a β€ 999). | Output 0 or 1. | Input: 13 | Output: 1 | Beginner | 1 | 0 | 53 | 14 | 9 | ||
1,902 | C | 1902C | C. Insert and Equalize | 1,300 | brute force; constructive algorithms; greedy; math; number theory | You are given an integer array \(a_1, a_2, \dots, a_n\), all its elements are distinct.First, you are asked to insert one more integer \(a_{n+1}\) into this array. \(a_{n+1}\) should not be equal to any of \(a_1, a_2, \dots, a_n\).Then, you will have to make all elements of the array equal. At the start, you choose a p... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)). All \(a_i\) are distinct.The sum o... | For each testcase, print a single integer β the smallest number of operations it can take you to make all elements equal, after you choose integers \(a_{n+1}\) and \(x\). | In the first testcase, you can choose \(a_{n+1} = 4\), the array becomes \([1, 2, 3, 4]\). Then choose \(x = 1\) and apply the operation \(3\) times to the first element, \(2\) times to the second element, \(1\) time to the third element and \(0\) times to the fourth element.In the second testcase, you can choose \(a_{... | Input: 331 2 351 -19 17 -3 -15110 | Output: 6 27 1 | Easy | 5 | 595 | 379 | 170 | 19 |
720 | A | 720A | A. Closing ceremony | 2,000 | greedy | The closing ceremony of Squanch Code Cup is held in the big hall with n Γ m seats, arranged in n rows, m seats in a row. Each seat has two coordinates (x, y) (1 β€ x β€ n, 1 β€ y β€ m). There are two queues of people waiting to enter the hall: k people are standing at (0, 0) and nΒ·m - k people are standing at (0, m + 1). E... | The first line of input contains two integers n and m (1 β€ nΒ·m β€ 104) β the size of the hall.The second line contains several integers. The first integer k (0 β€ k β€ nΒ·m) β the number of people at (0, 0). The following k integers indicate stamina of each person there.The third line also contains several integers. The fi... | If it is possible to distribute tickets between people in the described manner print ""YES"", otherwise print ""NO"". | Input: 2 23 3 3 21 3 | Output: YES | Hard | 1 | 706 | 525 | 117 | 7 | |
793 | F | 793F | F. Julia the snail | 3,000 | data structures; divide and conquer; dp | After hard work Igor decided to have some rest.He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium.Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the snail I... | The first line contains single integer n (1 β€ n β€ 100000) β the height of the trunk.The second line contains single integer m (1 β€ m β€ 100000) β the number of ropes.The next m lines contain information about the ropes.The i-th of these lines contains two integers li and ri (1 β€ li β€ ri β€ n) β the heights on which the l... | For each question print the maximum reachable for Julia height. | The picture of the first sample is on the left, the picture of the second sample is on the right. Ropes' colors are just for clarity, they don't mean anything. | Input: 841 23 42 56 751 21 41 62 76 8 | Output: 22557 | Master | 3 | 1,310 | 769 | 63 | 7 |
1,638 | E | 1638E | E. Colorful Operations | 2,400 | brute force; data structures; implementation | You have an array \(a_1,a_2, \dots, a_n\). Each element initially has value \(0\) and color \(1\). You are also given \(q\) queries to perform: Color \(l\) \(r\) \(c\): Change the color of elements \(a_l,a_{l+1},\cdots,a_r\) to \(c\) (\(1 \le l \le r \le n\), \(1 \le c \le n\)). Add \(c\) \(x\): Add \(x\) to values of ... | The first line of input contains two integers \(n\) and \(q\) (\(1 \le n,q \le 10^6\)) β the length of array \(a\) and the number of queries you have to perform.Each of the next \(q\) lines contains the query given in the form described in the problem statement. | Print the answers to the queries of the third type on separate lines. | The first sample test is explained below. Blue, red and green represent colors \(1\), \(2\) and \(3\) respectively. | Input: 5 8 Color 2 4 2 Add 2 2 Query 3 Color 4 5 3 Color 2 2 3 Add 3 3 Query 2 Query 5 | Output: 2 5 3 | Expert | 3 | 470 | 262 | 69 | 16 |
2,044 | A | 2044A | A. Easy Problem | 800 | brute force; math | Cube is given an integer \(n\). She wants to know how many ordered pairs of positive integers \((a,b)\) there are such that \(a=n-b\). Since Cube is not very good at math, please help her! | The first line contains an integer \(t\) (\(1 \leq t \leq 99\)) β the number of test cases.The only line of each test case contains an integer \(n\) (\(2 \leq n \leq 100\)). | For each test case, output the number of ordered pairs \((a, b)\) on a new line. | In the first test case, the only ordered pair that works is \((a,b)=(1,1)\). In the second test case, the three ordered pairs of \((a,b)\) that work are \((3,1), (2,2), (1,3)\). | Input: 3246 | Output: 1 3 5 | Beginner | 2 | 188 | 173 | 80 | 20 |
855 | A | 855A | A. Tom Riddle's Diary | 800 | brute force; implementation; strings | Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they ... | First line of input contains an integer n (1 β€ n β€ 100) β the number of names in the list.Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100. | Output n lines each containing either ""YES"" or ""NO"" (without quotes), depending on whether this string was already present in the stream or not.You can print each letter in any case (upper or lower). | In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is ""YES"". | Input: 6tomluciusginnyharryginnyharry | Output: NONONONOYESYES | Beginner | 3 | 672 | 217 | 203 | 8 |
1,578 | D | 1578D | D. Dragon Curve | 3,200 | A dragon curve is a self-similar fractal curve. In this problem, it is a curve that consists of straight-line segments of the same length connected at right angles. A simple way to construct a dragon curve is as follows: take a strip of paper, fold it in half \(n\) times in the same direction, then partially unfold it ... | The first line contains an integer \(n\) (\(1\le n\le2\cdot10^5\)) β the number of test cases.Each of the following \(n\) lines contains two integers \(x\) and \(y\) (\(-10^9\le x,y\le10^9\)) β the coordinates. | For each test case, print a line containing two integers β the first is the index of the curve (an integer between \(1\) and \(4\), inclusive), and the second is the position on the curve (the first segment has the position \(1\)). | You can use this illustration to debug your solution: | Input: 5 0 0 -2 0 -7 -7 5 -9 9 9 | Output: 1 1 2 2 3 189 4 186 2 68 | Master | 0 | 1,710 | 210 | 231 | 15 | |
1,282 | B1 | 1282B1 | B1. K for the Price of One (Easy Version) | 1,400 | dp; greedy; sortings | This is the easy version of this problem. The only difference is the constraint on \(k\) β the number of gifts in the offer. In this version: \(k=2\).Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β today the offer ""\(k\) of goods for the price of one"" is h... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The next lines contain a description of \(t\) test cases. The first line of each test case contains three integers \(n, p, k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le p \le 2\cdot10^9\), \(k=2\)) β the number of goods... | For each test case in a separate line print one integer \(m\) β the maximum number of goods that Vasya can buy. | Input: 6 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 2 10000 2 10000 10000 2 9999 2 10000 10000 5 13 2 8 2 8 2 5 3 18 2 1 2 3 | Output: 3 4 2 0 4 3 | Easy | 3 | 1,913 | 736 | 111 | 12 | |
1,704 | F | 1704F | F. Colouring Game | 2,600 | constructive algorithms; dp; games | Alice and Bob are playing a game. There are \(n\) cells in a row. Initially each cell is either red or blue. Alice goes first.On each turn, Alice chooses two neighbouring cells which contain at least one red cell, and paints that two cells white. Then, Bob chooses two neighbouring cells which contain at least one blue ... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of test cases follows.For each test case, the first line contains an integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) β the number of cells.The second line contains a string \(s\) of length \(n\) β the initial ... | For each test case, output the name of the winner on a separate line. | In the notes, the cell numbers increase from left to right.In the first testcase for Alice, she has two choices: paint the first and the second cells, or paint the second and the third cells. No matter what choice Alice makes, there will be exactly one blue cell after Alice's move. Bob just needs to paint the blue cell... | Input: 83BRB5RRBBB6RBRBRB8BBRRBRRB6BRRBRB12RBRBRBRBRRBB12RBRBRBRBBBRR4RBBR | Output: Bob Bob Alice Alice Alice Alice Bob Bob | Expert | 3 | 542 | 495 | 69 | 17 |
287 | B | 287B | B. Pipeline | 1,700 | binary search; math | Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only on... | The first line contains two space-separated integers n and k (1 β€ n β€ 1018, 2 β€ k β€ 109).Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print a single integer β the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1. | Input: 4 3 | Output: 2 | Medium | 2 | 1,148 | 236 | 162 | 2 | |
656 | C | 656C | C. Without Text | 2,000 | *special | You can preview the image in better quality by the link: http://assets.codeforces.com/files/656/without-text.png | The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character or a full stop ""."". | Output the required answer. | Input: Codeforces | Output: -87 | Hard | 1 | 112 | 156 | 27 | 6 | |
262 | B | 262B | B. Roma and Changing Signs | 1,200 | greedy | Roma works in a company that sells TVs. Now he has to prepare a report for the last year.Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several... | The first line contains two integers n and k (1 β€ n, k β€ 105), showing, how many numbers are in the sequence and how many swaps are to be made.The second line contains a non-decreasing sequence, consisting of n integers ai (|ai| β€ 104).The numbers in the lines are separated by single spaces. Please note that the given ... | In the single line print the answer to the problem β the maximum total income that we can obtain after exactly k changes. | In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1. | Input: 3 2-1 -1 1 | Output: 3 | Easy | 1 | 681 | 363 | 121 | 2 |
1,830 | B | 1830B | B. The BOSS Can Count Pairs | 2,000 | brute force; math | You are given two arrays \(a\) and \(b\), both of length \(n\).Your task is to count the number of pairs of integers \((i,j)\) such that \(1 \leq i < j \leq n\) and \(a_i \cdot a_j = b_i+b_j\). | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the arrays.The second l... | For each test case, output the number of good pairs. | In the first sample, there are \(2\) good pairs: \((1,2)\), \((1,3)\). In the second sample, there are \(7\) good pairs: \((1,2)\), \((1,5)\), \((2,8)\), \((3,4)\), \((4,7)\), \((5,6)\), \((5,7)\). | Input: 332 3 23 3 184 2 8 2 1 2 7 53 5 8 8 1 1 6 584 4 8 8 8 8 8 88 8 8 8 8 8 8 8 | Output: 2 7 1 | Hard | 2 | 193 | 667 | 52 | 18 |
939 | B | 939B | B. Hamster Farm | 1,000 | implementation | Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produc... | The first line contains two integers N and K (0 β€ N β€ 1018, 1 β€ K β€ 105) β the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.The second line contains K integers a1, a2, ..., aK (1 β€ ai β€ 1018 for all i) β the capacities of boxes. | Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.If there are many correct answers, output any of them. | Input: 19 35 4 10 | Output: 2 4 | Beginner | 1 | 805 | 294 | 245 | 9 | |
959 | D | 959D | D. Mahmoud and Ehab and another array construction task | 1,900 | constructive algorithms; greedy; math; number theory | Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that: b is lexicographically greater than or equal to a. bi β₯ 2. b is pairwise coprime: for every 1 β€ i < j β€ n, bi and bj are coprime, i. e. GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of ... | The first line contains an integer n (1 β€ n β€ 105), the number of elements in a and b.The second line contains n integers a1, a2, ..., an (2 β€ ai β€ 105), the elements of a. | Output n space-separated integers, the i-th of them representing bi. | Note that in the second sample, the array is already pairwise coprime so we printed it. | Input: 52 3 5 4 13 | Output: 2 3 5 7 11 | Hard | 4 | 651 | 172 | 68 | 9 |
495 | A | 495A | A. Digital Counter | 1,100 | implementation | Malek lives in an apartment block with 100 floors numbered from 0 to 99. The apartment has an elevator with a digital counter showing the floor that the elevator is currently on. The elevator shows each digit of a number with 7 light sticks by turning them on or off. The picture below shows how the elevator shows each ... | The only line of input contains exactly two digits representing number n (0 β€ n β€ 99). Note that n may have a leading zero. | In the only line of the output print the number of good integers. | In the first sample the counter may be supposed to show 88 or 89.In the second sample the good integers are 00, 08, 80 and 88.In the third sample the good integers are 03, 08, 09, 33, 38, 39, 73, 78, 79, 83, 88, 89, 93, 98, 99. | Input: 89 | Output: 2 | Easy | 1 | 1,193 | 123 | 65 | 4 |
151 | B | 151B | B. Phone Numbers | 1,200 | implementation; strings | Winters are just damn freezing cold in Nvodsk! That's why a group of n friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size si (that's the number of phone numbers). We know that taxi numb... | The first line contains an integer n (1 β€ n β€ 100) β the number of friends. Then follow n data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer si and string namei (0 β€ si β€ 100) β the number of phone numbers in the phone book of th... | In the first line print the phrase ""If you want to call a taxi, you should call: "". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase ""If you want to order a pizza, you should call: "". Then print names of all friends who have maximal ... | In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number.Thus, if ... | Input: 42 Fedorov22-22-2298-76-543 Melnikov75-19-0923-45-6799-99-987 Rogulenko22-22-2211-11-1133-33-3344-44-4455-55-5566-66-6695-43-213 Kaluzhin11-11-1199-99-9998-65-32 | Output: If you want to call a taxi, you should call: Rogulenko.If you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.If you wan... | Easy | 2 | 926 | 568 | 867 | 1 |
1,218 | H | 1218H | H. Function Composition | 2,900 | dfs and similar | We are definitely not going to bother you with another generic story when Alice finds about an array or when Alice and Bob play some stupid game. This time you'll get a simple, plain text.First, let us define several things. We define function \(F\) on the array \(A\) such that \(F(i, 1) = A[i]\) and \(F(i, m) = A[F(i,... | The first line contains one integer \(N\) \((1 \leq N \leq 2 \cdot 10^5)\) β the size of the array \(A\). The next line contains \(N\) non-negative integers β the array \(A\) itself \((1 \leq A_i \leq N)\). The next line contains one integer \(Q\) \((1 \leq Q \leq 10^5)\) β the number of queries. Each of the next \(Q\)... | Output exactly \(Q\) lines with a single integer in each that represent the solution. Output the solutions in the order the queries were asked in. | For the first query we can notice that \(F(3, 10) = 1,\ F(9, 10) = 1\) and \(F(10, 10) = 1\).For the second query no \(x\) satisfies condition \(F(x, 5) = 7\).For the third query \(F(5, 10) = 6\) holds.For the fourth query \(F(3, 1) = 1.\)For the fifth query no \(x\) satisfies condition \(F(x, 10) = 8\). | Input: 10 2 3 1 5 6 4 2 10 7 7 5 10 1 5 7 10 6 1 1 10 8 | Output: 3 0 1 1 0 | Master | 1 | 683 | 408 | 146 | 12 |
1,896 | F | 1896F | F. Bracket Xoring | 2,600 | constructive algorithms; greedy; implementation; math | You are given a binary string \(s\) of length \(2n\) where each element is \(\mathtt{0}\) or \(\mathtt{1}\). You can do the following operation: Choose a balanced bracket sequence\(^\dagger\) \(b\) of length \(2n\). For every index \(i\) from \(1\) to \(2n\) in order, where \(b_i\) is an open bracket, let \(p_i\) denot... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β where \(2n\) is the length of string \(s\).The second line of ... | For each test case, output \(-1\) in a single line if it is impossible to change all elements of \(s\) to \(\mathtt{0}\).Otherwise, output a single integer \(k\) (\(0 \le k \le 10\)) representing the number of operations needed to change all elements of \(s\) to \(\mathtt{0}\). Then, on each of the next \(k\) lines, ou... | In the first test case, it can be proven that it is impossible to change all elements of \(s\) to \(\mathtt{0}\).In the second test case, the first operation using the bracket sequence \(b = \mathtt{()()}\) will convert the binary string \(s=\mathtt{0000}\) to \(s=\mathtt{1111}\). Then, the second operation using the s... | Input: 4101200003100111401011100 | Output: -1 2 ()() ()() 1 (())() 2 (((()))) ()()(()) | Expert | 4 | 1,709 | 521 | 597 | 18 |
45 | D | 45D | D. Event Dates | 1,900 | greedy; meet-in-the-middle; sortings | On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose... | The first line contains one integer n (1 β€ n β€ 100) β the number of known events. Then follow n lines containing two integers li and ri each (1 β€ li β€ ri β€ 107) β the earliest acceptable date and the latest acceptable date of the i-th event. | Print n numbers β the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. | Input: 31 22 33 4 | Output: 1 2 3 | Hard | 3 | 423 | 241 | 151 | 0 | |
1,076 | D | 1076D | D. Edge Deletion | 1,800 | graphs; greedy; shortest paths | You are given an undirected connected weighted graph consisting of \(n\) vertices and \(m\) edges. Let's denote the length of the shortest path from vertex \(1\) to vertex \(i\) as \(d_i\). You have to erase some edges of the graph so that at most \(k\) edges remain. Let's call a vertex \(i\) good if there still exists... | The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le m \le 3 \cdot 10^5\), \(n - 1 \le m\), \(0 \le k \le m\)) β the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.Then \(m\) lines follow, each co... | In the first line print \(e\) β the number of edges that should remain in the graph (\(0 \le e \le k\)).In the second line print \(e\) distinct integers from \(1\) to \(m\) β the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertic... | Input: 3 3 2 1 2 1 3 2 1 1 3 3 | Output: 2 1 2 | Medium | 3 | 484 | 713 | 354 | 10 | |
1,153 | B | 1153B | B. Serval and Toy Bricks | 1,200 | constructive algorithms; greedy | Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many \(1 \times 1 \times 1\) toy bricks, he builds up a 3-dimensional object. We c... | The first line contains three positive space-separated integers \(n, m, h\) (\(1\leq n, m, h \leq 100\)) β the length, width and height.The second line contains \(m\) non-negative space-separated integers \(a_1,a_2,\dots,a_m\), where \(a_i\) is the height in the \(i\)-th column from left to right of the front view (\(0... | Output \(n\) lines, each of them contains \(m\) integers, the \(j\)-th number in the \(i\)-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them. | The graph above illustrates the object in the first example. The first graph illustrates the object in the example output for the second example, and the second graph shows the three-view drawing of it. | Input: 3 7 3 2 3 0 0 2 0 1 2 1 3 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 | Output: 1 0 0 0 2 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0 | Easy | 2 | 1,298 | 810 | 250 | 11 |
1,667 | F | 1667F | F. Yin Yang | 3,500 | implementation | You are given a rectangular grid with \(n\) rows and \(m\) columns. \(n\) and \(m\) are divisible by \(4\). Some of the cells are already colored black or white. It is guaranteed that no two colored cells share a corner or an edge.Color the remaining cells in a way that both the black and the white cells becomes orthog... | The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \le t \le 4000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) (\(8 \le n, m \le 500\), \(n\) and \(m\) are divisible b... | For each testcase print ""NO"" if there is no solution, otherwise print ""YES"" and a grid with the same format. If there are multiple solutions, you can print any. | Solution for test case 1: Test case 2: one can see that the black and the white part can't be connected in the same time. So the answer is ""NO"". | Input: 4 8 8 .W.W.... .....B.W .W.W.... .....W.W B.B..... ....B.B. B.W..... ....B.B. 8 8 B.W..B.W ........ W.B..W.B ........ ........ B.W..B.W ........ W.B..W.B 8 12 W.B......... ....B...B.W. B.B......... ....B...B.B. .B.......... ........B... .W..B.B...W. ............ 16 16 .W............W. ...W..W..W.W.... .B........... | Master | 1 | 594 | 679 | 164 | 16 |
38 | C | 38C | C. Blinds | 1,400 | brute force | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they ma... | The first output line contains two space-separated integers n and l (1 β€ n, l β€ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 β€ ... | Print the single number β the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | In the first sample test the required window is 2 Γ 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length ... | Input: 4 21 2 3 4 | Output: 8 | Easy | 1 | 1,418 | 330 | 255 | 0 |
1,388 | D | 1388D | D. Captain Flint and Treasure | 2,000 | data structures; dfs and similar; graphs; greedy; implementation; trees | Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like t... | The first line contains the integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of arrays \(a\) and \(b\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(β10^6 \le a_i \le 10^6\)).The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\) or \(b_i = -1\)).Additional... | In the first line, print the maximum \(ans\) you can get.In the second line, print the order of operations: \(n\) different integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)). The \(p_i\) is the position which should be chosen at the \(i\)-th step. If there are multiple orders, print any of them. | Input: 3 1 2 3 2 3 -1 | Output: 10 1 2 3 | Hard | 6 | 833 | 500 | 303 | 13 | |
2,096 | H | 2096H | H. Wonderful XOR Problem | 3,200 | bitmasks; combinatorics; dp; fft; math | You are the proud... never mind, just solve this problem.There are \(n\) intervals \([l_1, r_1], [l_2, r_2], \ldots [l_n, r_n]\). For each \(x\) from \(0\) to \(2^m - 1\), find the number, modulo \(998\,244\,353\), of sequences \(a_1, a_2, \ldots a_n\) such that: \(l_i \leq a_i \leq r_i\) for all \(i\) from \(1\) to \(... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 18\)).The \(i\)-th of the next \(n\) lines contains two ... | For each \(x\) from \(0\) to \(2^m - 1\), let: \(f_x\) be the number of valid sequences, modulo \(998\,244\,353\); \(g_x = f_x \cdot 2^x \mod 998\,244\,353\). Here, \(f_x\) and \(g_x\) are both integers in the interval \([0, 998\,244\,352]\).Let \(h = g_0 \oplus g_1 \oplus \ldots \oplus g_{2^m - 1}\).Output a single in... | For the first test case, the values of \(f_x\) are as follows: \(f_0 = 2\), because there are \(2\) valid sequences: \([1, 1]\) and \([2, 2]\); \(f_1 = 2\), because there are \(2\) valid sequences: \([0, 1]\) and \([2, 3]\); \(f_2 = 2\), because there are \(2\) valid sequences: \([0, 2]\) and \([1, 3]\); \(f_3 = 3\), b... | Input: 42 20 21 35 33 71 30 21 53 610 14314 1592653 5897932 3846264 3383279 5028841 9716939 9375105 8209749 4459230 78161 50 29 | Output: 22 9812 75032210 1073741823 | Master | 5 | 424 | 544 | 389 | 20 |
1,116 | B2 | 1116B2 | B2. Not A, not B or not C? | 0 | *special | You are given a qubit which is guaranteed to be in one of the following states: \(|A\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + |1\rangle \big)\), \(|B\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + \omega |1\rangle \big)\), or \(|C\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + \omega^2 |1\rangle \big)\), where \(\om... | Beginner | 1 | 1,414 | 0 | 0 | 11 | ||||
731 | C | 731C | C. Socks | 1,600 | dfs and similar; dsu; graphs; greedy | Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular cloth... | The first line of input contains three integers n, m and k (2 β€ n β€ 200 000, 0 β€ m β€ 200 000, 1 β€ k β€ 200 000) β the number of socks, the number of days and the number of available colors respectively.The second line contain n integers c1, c2, ..., cn (1 β€ ci β€ k) β current colors of Arseniy's socks.Each of the followi... | Print one integer β the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors. | In the first sample, Arseniy can repaint the first and the third socks to the second color.In the second sample, there is no need to change any colors. | Input: 3 2 31 2 31 22 3 | Output: 2 | Medium | 4 | 1,638 | 454 | 201 | 7 |
1,523 | B | 1523B | B. Lord of the Values | 1,100 | constructive algorithms | While trading on his favorite exchange trader William realized that he found a vulnerability. Using this vulnerability he could change the values of certain internal variables to his advantage. To play around he decided to change the values of all internal variables from \(a_1, a_2, \ldots, a_n\) to \(-a_1, -a_2, \ldot... | 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 a single even integer \(n\) (\(2 \le n \le 10^3\)), which is the number of internal variables.The second line of each test ... | For each test case print the answer in the following format:The first line of output must contain the total number of actions \(k\), which the strategy will perform. Note that you do not have to minimize \(k\). The inequality \(k \le 5\,000\) must be satisfied. Each of the next \(k\) lines must contain actions formatte... | For the first sample test case one possible sequence of operations is as follows: ""2 1 2"". Values of variables after performing the operation: [1, 0, 1, 1] ""2 1 2"". Values of variables after performing the operation: [1, -1, 1, 1] ""2 1 3"". Values of variables after performing the operation: [1, -1, 0, 1] ""2 1 3"... | Input: 2 4 1 1 1 1 4 4 3 1 2 | Output: 8 2 1 2 2 1 2 2 1 3 2 1 3 2 1 4 2 1 4 1 1 2 1 1 2 8 2 1 4 1 2 4 1 2 4 1 2 4 1 3 4 1 1 2 1 1 2 1 1 4 | Easy | 1 | 972 | 448 | 587 | 15 |
1,427 | H | 1427H | H. Prison Break | 3,500 | binary search; games; geometry; ternary search | A prisoner wants to escape from a prison. The prison is represented by the interior of the convex polygon with vertices \(P_1, P_2, P_3, \ldots, P_{n+1}, P_{n+2}, P_{n+3}\). It holds \(P_1=(0,0)\), \(P_{n+1}=(0, h)\), \(P_{n+2}=(-10^{18}, h)\) and \(P_{n+3}=(-10^{18}, 0)\). The prison walls \(P_{n+1}P_{n+2}\), \(P_{n+2... | The first line of the input contains \(n\) (\(1 \le n \le 50\)).The following \(n+1\) lines describe \(P_1, P_2,\dots, P_{n+1}\). The \(i\)-th of such lines contain two integers \(x_i\), \(y_i\) (\(0\le x_i, y_i\le 1,000\)) β the coordinates of \(P_i=(x_i, y_i)\).It is guaranteed that \(P_1=(0,0)\) and \(x_{n+1}=0\). T... | Print a single real number, the minimum speed \(v\) that allows the guards to guarantee that the prisoner will not escape. Your answer will be considered correct if its relative or absolute error does not exceed \(10^{-6}\). | Input: 2 0 0 223 464 0 749 | Output: 1 | Master | 4 | 1,592 | 561 | 224 | 14 | |
1,973 | B | 1973B | B. Cat, Fox and the Lonely Array | 1,300 | binary search; bitmasks; data structures; greedy; math; two pointers | Today, Cat and Fox found an array \(a\) consisting of \(n\) non-negative integers. Define the loneliness of \(a\) as the smallest positive integer \(k\) (\(1 \le k \le n\)) such that for any two positive integers \(i\) and \(j\) (\(1 \leq i, j \leq n - k +1\)), the following holds: $$$\(a_i | a_{i+1} | \ldots | a_{i+k-... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4 \)) β the number of test cases. The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the array \(a\).The second line o... | For each test case, print one integer β the loneliness of the given array. | In the first example, the loneliness of an array with a single element is always \(1\), so the answer is \(1\). In the second example, the OR of each subarray of length \(k = 1\) is \(2\), so the loneliness of the whole array is \(1\).In the seventh example, it's true that \((0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2... | Input: 71032 2 231 0 253 0 1 4 252 0 4 0 270 0 0 0 1 2 480 1 3 2 2 1 0 3 | Output: 1 1 3 4 4 7 3 | Easy | 6 | 726 | 524 | 74 | 19 |
1,646 | D | 1646D | D. Weight the Tree | 2,000 | constructive algorithms; dfs and similar; dp; implementation; trees | You are given a tree of \(n\) vertices numbered from \(1\) to \(n\). A tree is a connected undirected graph without cycles. For each \(i=1,2, \ldots, n\), let \(w_i\) be the weight of the \(i\)-th vertex. A vertex is called good if its weight is equal to the sum of the weights of all its neighbors.Initially, the weight... | The first line contains one integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.Then, \(nβ1\) lines follow. Each of them contains two integers \(u\) and \(v\) (\(1\le u,v\le n\)) denoting an edge between vertices \(u\) and \(v\). It is guaranteed that the edges form a tree. | In the first line print two integers β the maximum number of good vertices and the minimum possible sum of weights for that maximum.In the second line print \(n\) integers \(w_1, w_2, \ldots, w_n\) (\(1\le w_i\le 10^9\)) β the corresponding weight assigned to each vertex. It can be proven that there exists an optimal s... | This is the tree for the first test case: In this case, if you assign a weight of \(1\) to each vertex, then the good vertices (which are painted black) are \(1\), \(3\) and \(4\). It impossible to assign weights so that all vertices are good vertices. The minimum sum of weights in this case is \(1+1+1+1=4\), and it is... | Input: 4 1 2 2 3 2 4 | Output: 3 4 1 1 1 1 | Hard | 5 | 595 | 301 | 416 | 16 |
2,061 | D | 2061D | D. Kevin and Numbers | 1,600 | bitmasks; data structures | Kevin wrote an integer sequence \(a\) of length \(n\) on the blackboard.Kevin can perform the following operation any number of times: Select two integers \(x, y\) on the blackboard such that \(|x - y| \leq 1\), erase them, and then write down an integer \(x + y\) instead. Kevin wants to know if it is possible to trans... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(1\leq m \leq n \leq 2\cdot 10^5\)) β the length of \(a\) and the length of \(b\).The... | For each test case, output ""Yes"" if it is possible to transform \(a\) into \(b\), and ""No"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, you can erase \(4, 5\), and write down \(9\).In the second test case, you can't erase \(3, 6\).In the third test case, one possible way could be: Erase \(2, 2\), and write down \(4\). The remaining numbers are \(1, 2, 4\) now. Erase \(1, 2\), and write down \(3\). The remaining numbers are \(3, ... | Input: 92 14 592 13 694 21 2 2 23 44 21 1 3 33 54 21 2 3 43 55 51 2 3 4 55 4 3 2 14 21 1 1 11 14 41 1 1 11 1 1 21 111000000000 | Output: Yes No Yes Yes No Yes No No No | Medium | 2 | 658 | 591 | 266 | 20 |
1,334 | F | 1334F | F. Strange Function | 2,500 | binary search; data structures; dp; greedy | Let's denote the following function \(f\). This function takes an array \(a\) of length \(n\) and returns an array. Initially the result is an empty array. For each integer \(i\) from \(1\) to \(n\) we add element \(a_i\) to the end of the resulting array if it is greater than all previous elements (more formally, if \... | The first line contains one integer \(n\) \((1 \le n \le 5 \cdot 10^5)\) β the length of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le n)\) β the array \(a\).The third line contains \(n\) integers \(p_1, p_2, \dots, p_n\) \((|p_i| \le 10^9)\) β the array \(p\).The fourth ... | If the answer exists, in the first line print YES. In the second line, print the minimum number of coins you have to spend for fulfilling equality \(f(a) = b\).Otherwise in only line print NO. | Input: 11 4 1 3 3 7 8 7 9 10 7 11 3 5 0 -2 5 3 6 7 8 2 4 3 3 7 10 | Output: YES 20 | Expert | 4 | 1,056 | 519 | 192 | 13 | |
1,010 | D | 1010D | D. Mars rover | 2,000 | dfs and similar; graphs; implementation; trees | Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely β the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex \(1\), in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root,... | The first line contains a single integer \(n\) (\(2 \le n \le 10^6\)) β the number of vertices in the graph (both inputs and elements).The \(i\)-th of the next \(n\) lines contains a description of \(i\)-th vertex: the first word ""AND"", ""OR"", ""XOR"", ""NOT"" or ""IN"" (means the input of the scheme) is the vertex ... | Print a string of characters '0' and '1' (without quotes) β answers to the problem for each input in the ascending order of their vertex indices. | The original scheme from the example (before the input is changed):Green indicates bits '1', yellow indicates bits '0'.If Natasha changes the input bit \(2\) to \(0\), then the output will be \(1\).If Natasha changes the input bit \(3\) to \(0\), then the output will be \(0\).If Natasha changes the input bit \(6\) to \... | Input: 10AND 9 4IN 1IN 1XOR 6 5AND 3 7IN 0NOT 10IN 1IN 1AND 2 8 | Output: 10110 | Hard | 4 | 901 | 694 | 145 | 10 |
1,423 | K | 1423K | K. Lonely Numbers | 1,600 | binary search; math; number theory; two pointers | In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.More precisely, two different numbers \(a\) and \(b\) are friends if \(gcd(a,b)\), \(\frac{a}{gcd(a,b)}\), \(\frac{b}{gcd(a,b)}\) can form sides of a triangle.Three numbers \(a\), \(b\) and \(c\) can form... | The first line contains a single integer \(t\) \((1 \leq t \leq 10^6)\) - number of test cases.On next line there are \(t\) numbers, \(n_i\) \((1 \leq n_i \leq 10^6)\) - meaning that in case \(i\) you should solve for numbers \(1, 2, 3, ..., n_i\). | For each test case, print the answer on separate lines: number of lonely numbers in group \(1, 2, 3, ..., n_i\). | For first test case, \(1\) is the only number and therefore lonely.For second test case where \(n=5\), numbers \(1\), \(3\) and \(5\) are lonely.For third test case where \(n=10\), numbers \(1\), \(5\) and \(7\) are lonely. | Input: 3 1 5 10 | Output: 1 3 3 | Medium | 4 | 594 | 248 | 112 | 14 |
771 | B | 771B | B. Bear and Different Names | 1,500 | constructive algorithms; greedy | In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).A group of soldiers is effective if and only if their names a... | The first line of the input contains two integers n and k (2 β€ k β€ n β€ 50) β the number of soldiers and the size of a group respectively.The second line contains n - k + 1 strings s1, s2, ..., sn - k + 1. The string si is ""YES"" if the group of soldiers i through i + k - 1 is effective, and ""NO"" otherwise. | Find any solution satisfying all given conditions. In one line print n space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10.If t... | In the first sample, there are 8 soldiers. For every 3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output: First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the string s1 is ""NO"". Soldiers 2 through 4 ... | Input: 8 3NO NO YES YES YES NO | Output: Adam Bob Bob Cpqepqwer Limak Adam Bob Adam | Medium | 2 | 1,559 | 310 | 373 | 7 |
766 | A | 766A | A. Mahmoud and Longest Uncommon Subsequence | 1,000 | constructive algorithms; strings | While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one... | The first line contains string a, and the second line β string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters. | If there's no uncommon subsequence, print ""-1"". Otherwise print the length of the longest uncommon subsequence of a and b. | In the first example: you can choose ""defgh"" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a. | Input: abcddefgh | Output: 5 | Beginner | 2 | 734 | 216 | 124 | 7 |
1,374 | E2 | 1374E2 | E2. Reading Books (hard version) | 2,500 | data structures; greedy; implementation; sortings; ternary search; two pointers | Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly \(m\) books before all entertainments. Alice and Bob will rea... | The first line of the input contains three integers \(n\), \(m\) and \(k\) (\(1 \le k \le m \le n \le 2 \cdot 10^5\)).The next \(n\) lines contain descriptions of books, one description per line: the \(i\)-th line contains three integers \(t_i\), \(a_i\) and \(b_i\) (\(1 \le t_i \le 10^4\), \(0 \le a_i, b_i \le 1\)), w... | If there is no solution, print only one integer -1.If the solution exists, print \(T\) in the first line β the minimum total reading time of the suitable set of books. In the second line print \(m\) distinct integers from \(1\) to \(n\) in any order β indices of books which are in the set you found.If there are several... | Input: 6 3 16 0 011 1 09 0 121 1 110 1 08 0 1 | Output: 246 5 1 | Expert | 6 | 1,323 | 542 | 348 | 13 | |
1,906 | B | 1906B | B. Button Pressing | 2,600 | bitmasks; constructive algorithms; hashing | You are given \(N\) buttons (numbered from \(1\) to \(N\)) and \(N\) lamps (numbered from \(1\) to \(N\)). Each lamp can either be on or off. Initially, lamp \(i\) is on if \(A_i = 1\), and off if \(A_i = 0\).Button \(i\) is connected to lamp \(i - 1\) (if \(i > 1\)) and lamp \(i + 1\) (if \(i < N\)). In one move, you ... | This problem has multiple test cases. The first line consists of an integer \(T\) (\(1 \leq T \leq 1000\)), which represents the number of test cases. Each test case consists of three lines.The first line of each test case consists of an integer \(N\) (\(3 \le N \le 200\,000\)). The sum of \(N\) over all test cases doe... | For each test case, output YES in a single line if the final state of all lamps can be reached after zero or more moves, or NO otherwise. | Explanation for the sample input/output #1For the first test case, by pressing the buttons \(4, 2, 4, 3, 1, 2\) in sequence, the condition of the buttons changes as: \(0101 \rightarrow 0111 \rightarrow 1101 \rightarrow 1111 \rightarrow 1010 \rightarrow 1110 \rightarrow 0100\).For the second test case, you are unable to... | Input: 2 4 0101 0100 3 000 010 | Output: YES NO | Expert | 3 | 837 | 731 | 137 | 19 |
683 | F | 683F | F. Reformat the String | 1,800 | *special | In this problem you are given a string s consisting of uppercase and lowercase Latin letters, spaces, dots and commas. Your task is to correct the formatting of this string by removing and inserting spaces, as well as changing the case of the letters.After formatting, the resulting string must meet the following requir... | The first line contains a non-empty string s, consisting of uppercase and lowercase Latin letters, spaces, dots and commas. The length of the given string does not exceed 255. The string is guaranteed to have at least one character other than the space. | Output the corrected string which meets all the requirements described in the statement. | Input: hello ,i AM veRy GooD.Boris | Output: Hello, i am very good. Boris | Medium | 1 | 1,133 | 253 | 88 | 6 | |
1,120 | E | 1120E | E. The very same Munchhausen | 2,600 | brute force | A positive integer \(a\) is given. Baron Munchausen claims that he knows such a positive integer \(n\) that if one multiplies \(n\) by \(a\), the sum of its digits decreases \(a\) times. In other words, \(S(an) = S(n)/a\), where \(S(x)\) denotes the sum of digits of the number \(x\). Find out if what Baron told can be ... | The only line contains a single integer \(a\) (\(2 \le a \le 10^3\)). | If there is no such number \(n\), print \(-1\).Otherwise print any appropriate positive integer \(n\). Your number must not consist of more than \(5\cdot10^5\) digits. We can show that under given constraints either there is no answer, or there is an answer no longer than \(5\cdot10^5\) digits. | Input: 2 | Output: 6 | Expert | 1 | 325 | 69 | 295 | 11 | |
2,075 | A | 2075A | A. To Zero | 800 | greedy; math | You are given two integers \(n\) and \(k\); \(k\) is an odd number not less than \(3\). Your task is to turn \(n\) into \(0\).To do this, you can perform the following operation any number of times: choose a number \(x\) from \(1\) to \(k\) and subtract it from \(n\). However, if the current value of \(n\) is even (div... | The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(3 \le k \le n \le 10^9\), \(k\) is odd). | For each test case, output one integer β the minimum number of operations required to turn \(n\) into \(0\). | In the first example from the statement, you can first subtract \(5\) from \(39\) to get \(34\). Then subtract \(6\) five times to get \(4\). Finally, subtract \(4\) to get \(0\).In the second example, you can subtract \(3\) once, and then subtract \(2\) three times.In the third example, you can subtract \(2\) three ti... | Input: 839 79 36 3999967802 35 56 5999999999 31000000000 3 | Output: 7 4 3 499983901 1 2 499999999 500000000 | Beginner | 2 | 686 | 212 | 108 | 20 |
743 | C | 743C | C. Vladik and fractions | 1,500 | brute force; constructive algorithms; math; number theory | Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form .Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't c... | The single line contains single integer n (1 β€ n β€ 104). | If the answer exists, print 3 distinct numbers x, y and z (1 β€ x, y, z β€ 109, x β y, x β z, y β z). Otherwise print -1.If there are multiple answers, print any of them. | Input: 3 | Output: 2 7 42 | Medium | 4 | 451 | 56 | 168 | 7 | |
1,237 | B | 1237B | B. Balanced Tunnel | 1,300 | data structures; sortings; two pointers | Consider a tunnel on a one-way road. During a particular day, \(n\) cars numbered from \(1\) to \(n\) entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the... | The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), denoting the number of cars.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)), denoting the ids of cars in order of entering the tunnel. All \(a_i\) are pairwise distinct.The third line contains \(n\) integers \... | Output the number of cars to be fined. | The first example is depicted below:Car \(2\) definitely overtook car \(5\), while car \(4\) definitely overtook cars \(1\), \(2\), \(3\) and \(5\). Cars \(2\) and \(4\) must be fined.In the second example car \(5\) was definitely overtaken by all other cars.In the third example no car must be fined. | Input: 5 3 5 2 1 4 4 3 2 5 1 | Output: 2 | Easy | 3 | 979 | 459 | 38 | 12 |
1,242 | B | 1242B | B. 0-1 MST | 1,900 | dfs and similar; dsu; graphs; sortings | Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph.It is an undirected weighted graph on \(n\) vertices. It is a complete graph: each pair of vertices is con... | The first line of the input contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq \min(\frac{n(n-1)}{2},10^5)\)), the number of vertices and the number of edges of weight \(1\) in the graph. The \(i\)-th of the next \(m\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n... | Output a single integer, the weight of the minimum spanning tree of the graph. | The graph from the first sample is shown below. Dashed edges have weight \(0\), other edges have weight \(1\). One of the minimum spanning trees is highlighted in orange and has total weight \(2\). In the second sample, all edges have weight \(0\) so any spanning tree has total weight \(0\). | Input: 6 11 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 | Output: 2 | Hard | 4 | 710 | 451 | 78 | 12 |
22 | B | 22B | B. Bargaining Table | 1,500 | brute force; dp | Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that it... | The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free. | Output one number β the maximum possible perimeter of a bargaining table for Bob's office room. | Input: 3 3000010000 | Output: 8 | Medium | 2 | 683 | 320 | 95 | 0 | |
1,770 | A | 1770A | A. Koxia and Whiteboards | 1,000 | brute force; greedy | Kiyora has \(n\) whiteboards numbered from \(1\) to \(n\). Initially, the \(i\)-th whiteboard has the integer \(a_i\) written on it.Koxia performs \(m\) operations. The \(j\)-th operation is to choose one of the whiteboards and change the integer written on it to \(b_j\).Find the maximum possible sum of integers writte... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n,m \le 100\)).The second line of each test case contains \... | For each test case, output a single integer β the maximum possible sum of integers written on whiteboards after performing all \(m\) operations. | In the first test case, Koxia can perform the operations as follows: Choose the \(1\)-st whiteboard and rewrite the integer written on it to \(b_1=4\). Choose the \(2\)-nd whiteboard and rewrite to \(b_2=5\). After performing all operations, the numbers on the three whiteboards are \(4\), \(5\) and \(3\) respectively, ... | Input: 43 21 2 34 52 31 23 4 51 110015 31 1 1 1 11000000000 1000000000 1000000000 | Output: 12 9 1 3000000002 | Beginner | 2 | 379 | 493 | 144 | 17 |
1,738 | A | 1738A | A. Glory Addicts | 800 | greedy; implementation; sortings | The hero is addicted to glory, and is fighting against a monster. The hero has \(n\) skills. The \(i\)-th skill is of type \(a_i\) (either fire or frost) and has initial damage \(b_i\). The hero can perform all of the \(n\) skills in any order (with each skill performed exactly once). When performing each skill, the he... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)), indicating the number of skills. The se... | For each test case, output the maximum damage the hero can deal. | In the first test case, we can order the skills by \([3, 1, 4, 2]\), and the total damage is \(100 + 2 \times 1 + 2 \times 1000 + 10 = 2112\).In the second test case, we can order the skills by \([1, 4, 2, 5, 3, 6]\), and the total damage is \(3 + 2 \times 6 + 2 \times 4 + 2 \times 7 + 2 \times 5 + 2 \times 8 = 63\).In... | Input: 440 1 1 11 10 100 100060 0 0 1 1 13 4 5 6 7 831 1 11000000000 1000000000 1000000000111 | Output: 2112 63 3000000000 1 | Beginner | 3 | 1,093 | 837 | 64 | 17 |
221 | A | 221A | A. Little Elephant and Function | 1,000 | implementation; math | The Little Elephant enjoys recursive functions.This time he enjoys the sorting function. Let a is a permutation of an integers from 1 to n, inclusive, and ai denotes the i-th element of the permutation. The Little Elephant's recursive function f(x), that sorts the first x permutation's elements, works as follows: If x ... | A single line contains integer n (1 β€ n β€ 1000) β the size of permutation. | In a single line print n distinct integers from 1 to n β the required permutation. Numbers in a line should be separated by spaces.It is guaranteed that the answer exists. | Input: 1 | Output: 1 | Beginner | 2 | 808 | 74 | 171 | 2 | |
737 | F | 737F | F. Dirty plates | 3,300 | constructive algorithms; math | After one of celebrations there is a stack of dirty plates in Nikita's kitchen. Nikita has to wash them and put into a dryer. In dryer, the plates should be also placed in a stack also, and the plates sizes should increase down up. The sizes of all plates are distinct.Nikita has no so much free space, specifically, he ... | The first line contains three integers n, a and b (1 β€ n β€ 2000, 1 β€ a, b β€ n). The second line contains integers s1, s2, ..., sn (1 β€ si β€ n) β the sizes of the plates in down up order. All the sizes are distinct. | In the first line print ""YES"" if there is a solution. In this case, in the second line print integer k β the number of operations. Then in k lines print the operations, one per line. Each operation is described by two integers tj and cj, where tj = 1, if the operation is to wash the top cj places from the dirty stack... | In the first example the initial order of plates was 2, 3, 6, 4, 1, 5. Here is how the stacks look like after each of the operations: [1 2]: Dirty stack: 6, 4, 1, 5. Intermediary stack: 2, 3. The dryer is empty. [1 1]: Dirty stack: 4, 1, 5. Intermediary stack: 6, 2, 3. The dryer is empty. [2 1]: Dirty stack: 4, 1, 5. I... | Input: 6 2 32 3 6 4 1 5 | Output: YES81 21 12 11 21 12 12 12 3 | Master | 2 | 1,153 | 214 | 636 | 7 |
901 | D | 901D | D. Weighting a Tree | 2,700 | constructive algorithms; dfs and similar; graphs | You are given a connected undirected graph with n vertices and m edges. The vertices are enumerated from 1 to n. You are given n integers c1, c2, ..., cn, each of them is between - n and n, inclusive. It is also guaranteed that the parity of cv equals the parity of degree of vertex v. The degree of a vertex is the numb... | The first line contains two integers n and m (2 β€ n β€ 105, n - 1 β€ m β€ 105) β the number of vertices and the number of edges.The next line contains n integers c1, c2, ..., cn ( - n β€ ci β€ n), where ci is the required sum of weights of edges connected to vertex i. It is guaranteed that the parity of ci equals the parity... | If there is no solution, print ""NO"".Otherwise print ""YES"" and then m lines, the i-th of them is the weight of the i-th edge wi ( - 2Β·n2 β€ wi β€ 2Β·n2). | Input: 3 32 2 21 22 31 3 | Output: YES111 | Master | 3 | 567 | 624 | 153 | 9 | |
1,322 | F | 1322F | F. Assigning Fares | 3,500 | dp; trees | Mayor of city M. decided to launch several new metro lines during 2020. Since the city has a very limited budget, it was decided not to dig new tunnels but to use the existing underground network.The tunnel system of the city M. consists of \(n\) metro stations. The stations are connected with \(n - 1\) bidirectional t... | The first line contains an integers \(n\), \(m\) (\(2 \le n, \le 500\,000,\ 1 \le m \le 500\,000\)) β the number of stations in the city and the number of metro lines.Each of the following \(n-1\) lines describes another metro tunnel. Each tunnel is described with integers \(v_i\), \(u_i\) (\(1 \le v_i,\ u_i \le n\), \... | In the first line print integer \(k\) β the maximum fare zone used.In the next line print integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le k\)) β stations' fare zones. In case there are several possible answers, print any of them. In case it's impossible to assign fare zones, print ""-1"". | In the first example, line \(1 \rightarrow 3\) goes through the stations 1, 2, 3 in this order. In this order the fare zones of the stations are increasing. Since this line has 3 stations, at least three fare zones are needed. So the answer 1, 2, 3 is optimal.In the second example, it's impossible to assign fare zones ... | Input: 3 1 1 2 2 3 1 3 | Output: 3 1 2 3 | Master | 2 | 2,084 | 581 | 293 | 13 |
317 | A | 317A | A. Perfect Pair | 1,600 | brute force | Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the numbers,... | Single line of the input contains three integers x, y and m ( - 1018 β€ x, y, m β€ 1018).Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier. | Print the minimum number of operations or ""-1"" (without quotes), if it is impossible to transform the given pair to the m-perfect one. | In the first sample the following sequence of operations is suitable: (1, 2) (3, 2) (5, 2).In the second sample: (-1, 4) (3, 4) (7, 4) (11, 4) (15, 4).Finally, in the third sample x, y cannot be made positive, hence there is no proper sequence of operations. | Input: 1 2 5 | Output: 2 | Medium | 1 | 448 | 234 | 136 | 3 |
1,482 | E | 1482E | E. Skyline Photo | 2,100 | data structures; divide and conquer; dp | Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. There are \(n\) buildings in the city, the \(i\)-th of them has positive height \(h_... | The first line contains an integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)), the number of buildings on the skyline. The second line contains \(n\) distinct integers \(h_1, h_2, \ldots, h_n\) (\(1 \le h_i \le n\)). The \(i\)-th number represents the height of building \(i\).The third line contains \(n\) integers \(b_1, b_2... | Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline. | In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building. In the second example, Alice can achieve a maximum beauty of \(10\) by taking four pictures: three just containing one building, on buildings \(1\), \(2\) and \(5\), each photo with beauty \(-3\), \(4\) and \... | Input: 5 1 2 3 5 4 1 5 3 2 4 | Output: 15 | Hard | 3 | 1,137 | 426 | 108 | 14 |
2,095 | G | 2095G | G. Definitely a Geometry Problem | 0 | *special; geometry | Megumin is a wizard who can cast powerful magic spells that affect large areas. She is obsessed with the extremely devastating Explosion spell that can destroy everything around its target.There are currently \(n\) slimes in the wild. Each slime's location can be described in 2D Cartesian coordinates. Megumin wishes to... | The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) β the number of slimes and how many slimes Megumin needs to defeat. Each of the next \(n\) lines contains two integers \(x\) and \(y\) (\(-10^9 \le x, y \le 10^9\)), denoting the coordinates \((x, y)\) of a slime's location. It is guarant... | Print one real number β the answer.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a-b|}{\max(1,|b|)} \le 10^{-6}\). | In the first example, Megumin can hit the slime with a circle centered at \((0, 0)\) with radius \(0\).In the second example, Megumin can hit the first and last slimes with a circle centered at \((1, 0)\) with radius \(1\). | Input: 1 10 0 | Output: 0.000000000000000 | Beginner | 2 | 842 | 404 | 281 | 20 |
1,275 | B | 1275B | B. Code Review | 0 | *special | Π£ ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΡΠ°Π·ΡΠ°Π±ΠΎΡΠΊΠΈ Π΄Π²ΠΈΠΆΠΊΠΎΠ² ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅ Π΅ΡΡΡ ΠΎΠ±ΡΠΈΠΉ ΡΠ°Ρ. ΠΠΎΡΠ»Π΅ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»ΠΎΠΆΠ½ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π² ΡΠ΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΠΉ, Π°Π²ΡΠΎΡ ΡΡΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° ΠΏΡΠΈΡΡΠ»Π°Π΅Ρ Π² ΡΠ°Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Ρ ΠΏΡΠ΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ ΠΏΡΠΎΠ²Π΅ΡΡΠΈ ΡΠ΅Π²ΡΡ. ΠΠ»Ρ ΠΎΠ΄ΠΎΠ±ΡΠ΅Π½ΠΈΡ ΠΈΠ»ΠΈ ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΡ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π΅Π³ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΠ» ΠΎΠ΄ΠΈΠ½ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊ, Π½Π΅ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π²ΡΠΈΠΉ ΡΡΠ°ΡΡΠΈΠ΅ Π² Π΅Π³ΠΎ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠΈ.ΠΠ΅ΡΠ΅Π΄ ΡΠ΅ΠΌ, ΠΊΠ°ΠΊ ΠΎΡ... | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ n (1 β€ n β€ 50 000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΡΠΎΡΡΠ± ΠΎ code review Π² ΡΠ°ΡΠ΅.Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
n ΡΡΡΠΎΠΊΠ°Ρ
Π·Π°ΠΏΠΈΡΠ°Π½Ρ Π²Π½ΡΡΡΠ΅Π½Π½ΠΈΠΉ ΡΠ΅Π»ΠΎΡΠΈΡΠ»Π΅Π½Π½ΡΠΉ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ° i ΠΈ Ρ
Π΅Ρ ΠΊΠΎΠΌΠΌΠΈΡΠ° h (1 β€ i β€ 50 000; h ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· ΡΡΡΠΎΡΠ½ΡΡ
Π±ΡΠΊΠ² Π»Π°ΡΠΈΠ½ΡΠΊΠΎΠ³ΠΎ Π°Π»ΡΠ°Π²ΠΈΡΠ° ΠΎΡ a Π΄ΠΎ f ΠΈ ΡΠΈΡΡ). ΠΡΠ΅ Ρ
Π΅ΡΠΈ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ² ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½Ρ ΠΈ ΠΈΠΌΠ΅Ρ... | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π²ΡΠ΅ Ρ
Π΅ΡΠΈ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΠΏΠΎΠΏΠ°Π»ΠΈ Π½Π° ΡΠ΅Π²ΡΡ, Π² ΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ΠΈ Π±ΡΠ»ΠΈ Π΄Π°Π½Ρ Π²ΠΎ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
. | Input: 7 1 0e813c50 1 00e9422b 1 520cb7b4 2 052dd9ad 3 9dd5f347 3 e35f067b 1 bb4d4a99 | Output: 0e813c50 00e9422b 9dd5f347 bb4d4a99 | Beginner | 1 | 670 | 362 | 116 | 12 | |
878 | B | 878B | B. Teams Formation | 2,300 | data structures; implementation | This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in on... | The first line contains three integers n, k and m (1 β€ n β€ 105, 2 β€ k β€ 109, 1 β€ m β€ 109).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105), where ai is the number of city, person from which must take seat i in the bus. | Output the number of remaining participants in the line. | In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line. | Input: 4 2 51 2 3 1 | Output: 12 | Expert | 2 | 977 | 236 | 56 | 8 |
1,768 | A | 1768A | A. Greatest Convex | 800 | greedy; math; number theory | You are given an integer \(k\). Find the largest integer \(x\), where \(1 \le x < k\), such that \(x! + (x - 1)!^\dagger\) is a multiple of \(^\ddagger\) \(k\), or determine that no such \(x\) exists.\(^\dagger\) \(y!\) denotes the factorial of \(y\), which is defined recursively as \(y! = y \cdot (y-1)!\) for \(y \geq... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The only line of each test case contains a single integer \(k\) (\(2 \le k \le 10^9\)). | For each test case output a single integer β the largest possible integer \(x\) that satisfies the conditions above. If no such \(x\) exists, output \(-1\). | In the first test case, \(2! + 1! = 2 + 1 = 3\), which is a multiple of \(3\).In the third test case, \(7! + 6! = 5040 + 720 = 5760\), which is a multiple of \(8\). | Input: 436810 | Output: 2 5 7 9 | Beginner | 3 | 653 | 223 | 156 | 17 |
1,181 | E2 | 1181E2 | E2. A Story of One Country (Hard) | 3,000 | brute force; greedy; sortings | This problem differs from the previous problem only in constraints.Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.Initially, there were \(n\) different countries on the land that is now Berland. Each country had its own territory that was repr... | The first line contains a single integer \(n\) (\(1 \leq n \leq 100\,000\)) β the number of countries and castles.Each of the next \(n\) lines contains four integers \(a_i, b_i, c_i, d_i\) (\(0 \leq a_i < c_i \leq 10^9\), \(0 \leq b_i < d_i \leq 10^9\)) β the coordinates of the \(i\)-th castle, where \((a_i, b_i)\) are... | If there exists a possible set of territories that satisfies the story, print ""YES"", otherwise print ""NO"".You can print each letter in any case (upper or lower). | The castles in the first and second examples are shown on the pictures below. | Input: 4 0 0 1 2 0 2 1 3 1 0 2 1 1 1 2 3 | Output: YES | Master | 3 | 1,522 | 499 | 165 | 11 |
223 | D | 223D | D. Spider | 3,000 | geometry; graphs | A plane contains a not necessarily convex polygon without self-intersections, consisting of n vertexes, numbered from 1 to n. There is a spider sitting on the border of the polygon, the spider can move like that: Transfer. The spider moves from the point p1 with coordinates (x1, y1), lying on the polygon border, to the... | The first line contains integer n (3 β€ n β€ 105) β the number of vertexes of the given polygon. Next n lines contain two space-separated integers each β the coordinates of the polygon vertexes. The vertexes are listed in the counter-clockwise order. The coordinates of the polygon vertexes do not exceed 104 in their abso... | In the output print a single real number β the length of the shortest way from vertex s to vertex t. The answer is considered correct, if its absolute or relative error does not exceed 10 - 6. | In the first sample the spider transfers along the side that connects vertexes 1 and 4.In the second sample the spider doesn't have to transfer anywhere, so the distance equals zero.In the third sample the best strategy for the spider is to transfer from vertex 3 to point (2,3), descend to point (2,1), and then transfe... | Input: 40 01 01 10 11 4 | Output: 1.000000000000000000e+000 | Master | 2 | 1,245 | 818 | 192 | 2 |
1,970 | A1 | 1970A1 | A1. Balanced Shuffle (Easy) | 1,000 | implementation; sortings | A parentheses sequence is a string consisting of characters ""("" and "")"", for example ""(()(("".A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example ""(()(()))"".The balance of a parentheses sequence is ... | The only line of input contains a string \(s\) consisting only of characters ""("" and "")"". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding \(500\,000\). | Print the parentheses sequence \(t\) β the balanced shuffle of \(s\). | Input: (()(())) | Output: ()(()()) | Beginner | 2 | 1,575 | 211 | 69 | 19 | |
1,884 | D | 1884D | D. Counting Rhyme | 2,100 | dp; math; number theory | You are given an array of integers \(a_1, a_2, \ldots, a_n\).A pair of integers \((i, j)\), such that \(1 \le i < j \le n\), is called good, if there does not exist an integer \(k\) (\(1 \le k \le n\)) such that \(a_i\) is divisible by \(a_k\) and \(a_j\) is divisible by \(a_k\) at the same time.Please, find the number... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)).The second line of each test case contains \(n\) integers \(a_1... | For each test case, output the number of good pairs. | In the first test case, there are no good pairs. In the second test case, here are all the good pairs: \((1, 2)\), \((2, 3)\), and \((2, 4)\). | Input: 642 4 4 442 3 4 496 8 9 4 6 8 9 4 997 7 4 4 9 9 6 2 91810 18 18 15 14 4 5 6 8 9 10 12 15 16 18 17 13 112112 19 19 18 18 12 2 18 19 12 12 3 12 12 12 18 19 16 18 19 12 | Output: 0 3 26 26 124 82 | Hard | 3 | 335 | 447 | 52 | 18 |
1,272 | F | 1272F | F. Two Bracket Sequences | 2,200 | dp; strings; two pointers | You are given two bracket sequences (not necessarily regular) \(s\) and \(t\) consisting only of characters '(' and ')'. You want to construct the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous).Recall what is the regular bracket sequence: () is ... | The first line of the input contains one bracket sequence \(s\) consisting of no more than \(200\) characters '(' and ')'.The second line of the input contains one bracket sequence \(t\) consisting of no more than \(200\) characters '(' and ')'. | Print one line β the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous). If there are several answers, you can print any. | Input: (())(() ()))() | Output: (())()() | Hard | 3 | 839 | 245 | 192 | 12 | |
297 | B | 297B | B. Fish Weight | 1,600 | constructive algorithms; greedy | It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 β€ w2 β€ ... β€ wk holds.Polar bears Alice and Bob each have caught some fish, and they are gue... | The first line contains three integers n, m, k (1 β€ n, m β€ 105, 1 β€ k β€ 109) β the number of fish caught by Alice and Bob respectively, and the number of fish species.The second line contains n integers each from 1 to k, the list of fish type caught by Alice. The third line contains m integers each from 1 to k, the lis... | Output ""YES"" (without quotes) if it is possible, and ""NO"" (without quotes) otherwise. | In the first sample, if w1 = 1, w2 = 2, w3 = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5.In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bobβs fish is always not less than the total weight of Aliceβs fish. | Input: 3 3 32 2 21 1 3 | Output: YES | Medium | 2 | 695 | 417 | 89 | 2 |
1,277 | B | 1277B | B. Make Them Odd | 1,200 | greedy; number theory | There are \(n\) positive integers \(a_1, a_2, \dots, a_n\). For the one move you can choose any even value \(c\) and divide by two all elements that equal \(c\).For example, if \(a=[6,8,12,6,3,12]\) and you choose \(c=6\), and \(a\) is transformed into \(a=[3,8,12,3,3,12]\) after the move.You need to find the minimal n... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains \(n\) (\(1 \le n \le 2\cdot10^5\)) β the number of integers in the sequence \(a\). The second line contains positive integers \(a_1, ... | For \(t\) test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by \(2\)). | In the first test case of the example, the optimal sequence of moves can be as follows: before making moves \(a=[40, 6, 40, 3, 20, 1]\); choose \(c=6\); now \(a=[40, 3, 40, 3, 20, 1]\); choose \(c=40\); now \(a=[20, 3, 20, 3, 20, 1]\); choose \(c=20\); now \(a=[10, 3, 10, 3, 10, 1]\); choose \(c=10\); now \(a=[5, 3, 5,... | Input: 4 6 40 6 40 3 20 1 1 1024 4 2 4 8 16 3 3 1 7 | Output: 4 10 4 0 | Easy | 2 | 438 | 442 | 218 | 12 |
359 | C | 359C | C. Prime Number | 1,900 | math; number theory | Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to re... | The first line contains two positive integers n and x (1 β€ n β€ 105, 2 β€ x β€ 109) β the size of the array and the prime number.The second line contains n space-separated integers a1, a2, ..., an (0 β€ a1 β€ a2 β€ ... β€ an β€ 109). | Print a single number β the answer to the problem modulo 1000000007 (109 + 7). | In the first sample . Thus, the answer to the problem is 8.In the second sample, . The answer to the problem is 27, as 351 = 13Β·27, 729 = 27Β·27.In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.In the fourth sample . Thus, the answer to the problem is 1. | Input: 2 22 2 | Output: 8 | Hard | 2 | 513 | 225 | 78 | 3 |
2,110 | B | 2110B | B. Down with Brackets | 900 | strings | In 2077, robots decided to get rid of balanced bracket sequences once and for all!A bracket sequence is called balanced if it can be constructed by the following formal grammar. The empty sequence \(\varnothing\) is balanced. If the bracket sequence \(A\) is balanced, then \(\mathtt{(}A\mathtt{)}\) is also balanced. If... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. Each test case consists of a single string \(s\) (\(2 \leq |s| \leq 2 \cdot 10^5\)) β a sequence of the characters ( and ).It is guaranteed that \(s\) is a ba... | For each test case, output ""YES"" if the robots can make the string stop being a balanced bracket sequence, and ""NO"" otherwise.You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first test case, it can be shown that the robots will not be able to break the correct bracket sequence.In the second test case, one of the options for removing brackets is as follows:\(\texttt{(())}\color{red}{\texttt{(}}\texttt{)(}\color{red}{\texttt{)}} \rightarrow \texttt{(()))(}\), which is not a correct br... | Input: 4(())(())()()()(())(()) | Output: NO YES NO YES | Beginner | 1 | 987 | 444 | 297 | 21 |
2,030 | G1 | 2030G1 | G1. The Destruction of the Universe (Easy Version) | 2,900 | combinatorics; greedy; math | This is the easy version of the problem. In this version, \(n \leq 5000\). You can only make hacks if both versions of the problem are solved.Orangutans are powerful beingsβso powerful that they only need \(1\) unit of time to destroy every vulnerable planet in the universe!There are \(n\) planets in the universe. Each... | The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 5000\)) β the number of planets in the universe.The following \(n\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β t... | For each test case, output an integer β the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo \(998\,244\,353\). | In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets \(\{[1,1]\}, \{[2,3]\}, \{[3,3]\}\), the score is \(0\). For the subset \(\{[2,3], [3,3]\}\), the score is \(0\), because the point \(3\) is already contained in both planets' interval of vulnerability. For the... | Input: 331 12 33 341 42 32 41 151 22 33 44 51 5 | Output: 5 6 24 | Master | 3 | 1,147 | 464 | 148 | 20 |
155 | B | 155B | B. Combination | 1,100 | greedy; sortings | Ilya plays a card game by the following rules.A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom cont... | The first line contains a single integer n (1 β€ n β€ 1000) β the number of cards Ilya has.Each of the next n lines contains two non-negative space-separated integers β ai and bi (0 β€ ai, bi β€ 104) β the numbers, written at the top and the bottom of the i-th card correspondingly. | Print the single number β the maximum number of points you can score in one round by the described rules. | In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards. | Input: 21 02 0 | Output: 2 | Easy | 2 | 1,168 | 278 | 105 | 1 |
1,867 | D | 1867D | D. Cyclic Operations | 1,800 | brute force; constructive algorithms; dfs and similar; graphs; greedy; implementation | Egor has an array \(a\) of length \(n\), initially consisting of zeros. However, he wanted to turn it into another array \(b\) of length \(n\).Since Egor doesn't take easy paths, only the following operation can be used (possibly zero or several times): choose an array \(l\) of length \(k\) (\(1 \leq l_i \leq n\), all ... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) - the number of test cases.Each test case consists of two lines. The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)).The second line contains the array \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq n\)).It is ... | For each test case, output ""YES"" (without quotes) if there is a way to get the array \(b\) using only the given operation. Otherwise, output ""NO"" (without quotes). You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be accepted as a po... | Let's consider the first example: Apply the operation with \(l\) = \([1,2,3]\). Now \(a\) = \([2,3,1,0,0]\). Apply the operation with \(l\) = \([3,5,4]\). Now \(a\) = \([2,3,5,3,4]\) = \(b\). We see that it is possible to get the array \(b\). Therefore, the answer is YES.In the second example, it can be proven that the... | Input: 65 32 3 5 3 44 22 4 3 11 113 11 2 35 35 4 3 2 16 11 2 3 1 5 6 | Output: YES NO YES YES NO NO | Medium | 6 | 749 | 406 | 334 | 18 |
1,436 | B | 1436B | B. Prime Square | 900 | constructive algorithms; math | Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square β a prime square. A square of size \(n \times n\) is called pr... | The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(2 \le n \le 100\)) β the required size of a square. | For each test case print \(n\) lines, each containing \(n\) integers β the prime square you built. If there are multiple answers, print any. | Input: 2 4 2 | Output: 4 6 8 1 4 9 9 9 4 10 10 65 1 4 4 4 1 1 1 1 | Beginner | 2 | 716 | 210 | 140 | 14 | |
234 | G | 234G | G. Practice | 1,600 | constructive algorithms; divide and conquer; implementation | Little time is left before Berland annual football championship. Therefore the coach of team ""Losewille Rangers"" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are n players in ""Losewille Rangers"". Each player on the team has a number β a unique integer from ... | A single input line contains integer n (2 β€ n β€ 1000). | In the first line print m β the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in m lines.In the i-th of those lines print fi β the number of players in the first team during the i-th practice (1 β€ fi < n), and fi numbers from 1 to n β the numbers of players in... | Input: 2 | Output: 11 1 | Medium | 3 | 1,096 | 54 | 558 | 2 | |
2,027 | B | 2027B | B. Stalin Sort | 1,100 | brute force; greedy | Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an \(\mathcal{O}(n)\) time complexity.It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ig... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. This is followed by descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)) β the size of the array.The second line of... | For each test case, output a single integer β the minimum number of integers which must be removed from the array to make it vulnerable. | In the first test case, the optimal answer is to remove the numbers \(3\) and \(9\). Then we are left with \(a = [6, 4, 2, 5, 2]\). To show this array is vulnerable, we can first apply a Stalin Sort on the subarray \([4, 2, 5]\) to get \(a = [6, 4, 5, 2]\) and then apply a Stalin Sort on the subarray \([6, 4, 5]\) to g... | Input: 673 6 4 9 2 5 255 4 4 2 282 2 4 4 6 6 10 101100096 8 9 10 12 9 7 5 47300000000 600000000 400000000 900000000 200000000 400000000 200000000 | Output: 2 0 6 0 4 2 | Easy | 2 | 1,122 | 495 | 136 | 20 |
1,656 | E | 1656E | E. Equal Tree Sums | 2,200 | constructive algorithms; dfs and similar; math; trees | You are given an undirected unrooted tree, i.e. a connected undirected graph without cycles.You must assign a nonzero integer weight to each vertex so that the following is satisfied: if any vertex of the tree is removed, then each of the remaining connected components has the same sum of weights in its vertices. | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 10^5\)) β the number of vertices of the tree.The next \(n-1\) l... | For each test case, you must output one line with \(n\) space separated integers \(a_1, a_2, \ldots, a_n\), where \(a_i\) is the weight assigned to vertex \(i\). The weights must satisfy \(-10^5 \leq a_i \leq 10^5\) and \(a_i \neq 0\).It can be shown that there always exists a solution satisfying these constraints. If ... | In the first case, when removing vertex \(1\) all remaining connected components have sum \(5\) and when removing vertex \(3\) all remaining connected components have sum \(2\). When removing other vertices, there is only one remaining connected component so all remaining connected components have the same sum. | Input: 2 5 1 2 1 3 3 4 3 5 3 1 2 1 3 | Output: -3 5 1 2 2 1 1 1 | Hard | 4 | 314 | 568 | 378 | 16 |
296 | A | 296A | A. Yaroslav and Permutations | 1,100 | greedy; math | Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav. | The first line contains integer n (1 β€ n β€ 100) β the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 1000) β the array elements. | In the single line print ""YES"" (without the quotes) if Yaroslav can obtain the array he needs, and ""NO"" (without the quotes) otherwise. | In the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs. | Input: 11 | Output: YES | Easy | 2 | 251 | 176 | 139 | 2 |
1,264 | D2 | 1264D2 | D2. Beautiful Bracket Sequence (hard version) | 2,900 | combinatorics; probabilities | This is the hard version of this problem. The only difference is the limit of \(n\) - the length of the input string. In this version, \(1 \leq n \leq 10^6\).Let's define a correct bracket sequence and its depth as follow: An empty string is a correct bracket sequence with depth \(0\). If ""s"" is a correct bracket seq... | The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most \(10^6\). | Print the answer modulo \(998244353\) in a single line. | In the first test case, we can obtain \(4\) bracket sequences by replacing all characters '?' with either '(' or ')': ""(("". Its depth is \(0\); ""))"". Its depth is \(0\); "")("". Its depth is \(0\); ""()"". Its depth is \(1\). So, the answer is \(1 = 0 + 0 + 0 + 1\).In the second test case, we can obtain \(4\) brack... | Input: ?? | Output: 1 | Master | 2 | 1,365 | 121 | 55 | 12 |
483 | A | 483A | A. Counterexample | 1,100 | brute force; implementation; math; number theory | Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the p... | The single line contains two positive space-separated integers l, r (1 β€ l β€ r β€ 1018; r - l β€ 50). | Print three positive space-separated integers a, b, c β three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order. If the counterexample does not exist, print the single number -1. | In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are. In the second sample you cannot form a group of three distinct integers, so the answer is -1. In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three. | Input: 2 4 | Output: 2 3 4 | Easy | 4 | 718 | 99 | 302 | 4 |
272 | A | 272A | A. Dima and Friends | 1,000 | implementation; math | Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place.To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and the... | The first line contains integer n (1 β€ n β€ 100) β the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show. The numbers in the lines are separated by a single space. | In a single line print the answer to the problem. | In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend.In the second sample Dima can show 2 or 4 fingers. | Input: 11 | Output: 3 | Beginner | 2 | 1,048 | 313 | 49 | 2 |
1,734 | F | 1734F | F. Zeros and Ones | 2,500 | bitmasks; divide and conquer; dp; math | Let \(S\) be the Thue-Morse sequence. In other words, \(S\) is the \(0\)-indexed binary string with infinite length that can be constructed as follows: Initially, let \(S\) be ""0"". Then, we perform the following operation infinitely many times: concatenate \(S\) with a copy of itself with flipped bits.For example, he... | Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first and only line of each test case contains two positive integers, \(n\) and \(m\) respectively (\(1 \leq n,m \leq 10^{18... | For each testcase, output a non-negative integer β the Hamming distance between the two required strings. | The string \(S\) is equal to 0110100110010110....In the first test case, \(S_0\) is ""0"", and \(S_1\) is ""1"". The Hamming distance between the two strings is \(1\).In the second test case, \(S_0 S_1 \ldots S_9\) is ""0110100110"", and \(S_5 S_6 \ldots S_{14}\) is ""0011001011"". The Hamming distance between the two ... | Input: 61 15 1034 21173 3419124639 5634877212073412269 96221437021 | Output: 1 6 95 20 28208137 48102976088 | Expert | 4 | 732 | 325 | 105 | 17 |
23 | B | 23B | B. Party | 1,600 | constructive algorithms; graphs; math | n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same.What is the maximum amount of peop... | The first input line contains one number t β amount of tests (1 β€ t β€ 105). Each of the following t lines contains one integer number n (1 β€ n β€ 105). | For each test output in a separate line one number β the maximum amount of people that could stay in the end. | Input: 13 | Output: 1 | Medium | 3 | 363 | 150 | 109 | 0 | |
1,372 | C | 1372C | C. Omkar and Baseball | 1,500 | constructive algorithms; math | Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across \(n\) sessions follow the identity permutation (ie. in the first game he scores \(1\) point, in the second game he scores \(2\) points and so on). However... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the given permutation.The second line of each test case con... | For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. | In the first permutation, it is already sorted so no exchanges are needed.It can be shown that you need at least \(2\) exchanges to sort the second permutation.\([3, 2, 4, 5, 1, 6, 7]\)Perform special exchange on range (\(1, 5\))\([4, 1, 2, 3, 5, 6, 7]\)Perform special exchange on range (\(1, 4\))\([1, 2, 3, 4, 5, 6, 7... | Input: 251 2 3 4 573 2 4 5 1 6 7 | Output: 0 2 | Medium | 2 | 1,186 | 511 | 111 | 13 |
1,904 | F | 1904F | F. Beautiful Tree | 2,800 | data structures; dfs and similar; graphs; implementation; trees | Lunchbox has a tree of size \(n\) rooted at node \(1\). Each node is then assigned a value. Lunchbox considers the tree to be beautiful if each value is distinct and ranges from \(1\) to \(n\). In addition, a beautiful tree must also satisfy \(m\) requirements of \(2\) types: ""1 a b c"" β The node with the smallest va... | The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)).The next \(n - 1\) lines contain two integers \(u\) and \(v\) (\(1 \le u, v \le n, u \ne v\)) β denoting an edge between nodes \(u\) and \(v\). It is guaranteed that the given edges form a tree.The next \(m\) lines each contain four ... | If it is impossible to assign values such that the tree is beautiful, output \(-1\). Otherwise, output \(n\) integers, the \(i\)-th of which denotes the value of node \(i\). | Input: 7 5 1 2 1 3 1 4 3 5 4 6 3 7 1 6 5 1 2 6 7 3 1 2 7 1 1 7 5 7 2 4 2 2 | Output: 1 6 7 5 3 4 2 | Master | 5 | 634 | 484 | 173 | 19 | |
475 | E | 475E | E. Strongly Connected City 2 | 2,700 | dfs and similar | Imagine a city with n junctions and m streets. Junctions are numbered from 1 to n.In order to increase the traffic flow, mayor of the city has decided to make each street one-way. This means in the street between junctions u and v, the traffic moves only from u to v or only from v to u. The problem is to direct the tra... | The first line of input contains integers n and m, (), denoting the number of junctions and streets of the city.Each of the following m lines contains two integers u and v, (u β v), denoting endpoints of a street in the city.Between every two junctions there will be at most one street. It is guaranteed that before mayo... | Print the maximal number of pairs (u, v) such that that it is possible to reach junction v from u after directing the streets. | In the first sample, if the mayor makes first and second streets one-way towards the junction 1 and third and fourth streets in opposite direction, there would be 13 pairs of reachable junctions: {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (2, 1), (3, 1), (1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)} | Input: 5 41 21 31 41 5 | Output: 13 | Master | 1 | 573 | 426 | 126 | 4 |
316 | A2 | 316A2 | A2. Special Task | 1,400 | math | Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the ... | The first line contains string s β the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0.The input limits for scoring 30 points are (subproblem A1): 1 β€ |s| β€ 5. The input limits for scoring 100 points ar... | Print the number of codes that match the given hint. | Input: AJ | Output: 81 | Easy | 1 | 2,595 | 396 | 52 | 3 | |
1,799 | A | 1799A | A. Recent Actions | 800 | data structures; greedy; implementation; math | On Codeforces the ""Recent Actions"" field shows the last \(n\) posts with recent actions.Initially, there are posts \(1, 2, \ldots, n\) in the field (this is in order from top to down). Also there are infinitely many posts not in the field, numbered with integers \(n + 1, n + 2, \ldots\).When recent action happens in ... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Descriptions of test cases follow.The first line of each test case contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 5 \cdot 10^4\)) β the size of the ""Recent Actions"" field and the number of actions.The next line... | For each test case print \(n\) integers \(t_1, t_2, \ldots, t_n\), where \(t_i=-1\) if the post \(i\) won't be removed or \(t_i\) equals to the first moment of time the post \(i\) will be removed (\(1 \leq t_i \leq m\)). | In the first test case, the only post \(1\) will be removed at the moment \(1\) and replaced by the post \(2\).In the second test case the ""Recent Actions"" field will be (given an order from top to down): Before moment \(1\): \([1, 2, 3]\), after moment \(1\): \([5, 1, 2]\). Post number \(3\) was removed. Before mome... | Input: 101 123 25 44 55 9 9 5 75 56 7 8 9 103 44 4 4 44 45 5 6 63 54 5 5 5 44 205 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 205 77 8 7 11 7 12 106 78 11 7 8 8 8 12 | Output: 1 -1 2 1 -1 5 2 1 5 4 3 2 1 -1 -1 1 -1 -1 3 1 -1 2 1 8 7 3 1 7 6 4 2 1 -1 -1 7 3 2 1 | Beginner | 4 | 948 | 516 | 220 | 17 |
1,970 | F1 | 1970F1 | F1. Playing Quidditch (Easy) | 2,300 | implementation | This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get inv... | On the first line, the integers \(N\) and \(M\).The description of the field follows: \(N\) lines of \(M\) pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either: .. to represent an empty cell R0, ..., R9, B0, ..., B9 to represent a player. The first charac... | You must output the description of the main events of the game, one event per line. More precisely: Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where a p... | In the first example, a blue player goes to the middle of the grid, at the position of the Quaffle. He takes it, goes to the goal of the red team, and lets the ball there. He scores! The ball gets back to the middle of the field, a red player takes it and scores too.In the second example, the red player takes the Quaff... | Input: 5 5.. R1 .. B1 ..RG .. .. .. BGRG R0 .Q B0 BGRG .. .. .. BG.. R2 .. B2 ..13B2 UB2 UB2 LB2 C .QB2 LB2 LB2 TR0 RR0 C .QR0 DR0 RR0 RR0 T | Output: 6 BLUE GOAL 12 RED GOAL FINAL SCORE: 1 1 | Expert | 1 | 2,023 | 1,816 | 714 | 19 |
568 | B | 568B | B. Symmetric and Transitive | 1,900 | combinatorics; dp; math | Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term ""equivalence relation"". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation.A set Ο of pairs (a, b) of elements ... | A single line contains a single integer n (1 β€ n β€ 4000). | In a single line print the answer to the problem modulo 109 + 7. | If n = 1 there is only one such relation β an empty one, i.e. . In other words, for a single element x of set A the following is hold: .If n = 2 there are three such relations. Let's assume that set A consists of two elements, x and y. Then the valid relations are , Ο = {(x, x)}, Ο = {(y, y)}. It is easy to see that th... | Input: 1 | Output: 1 | Hard | 3 | 1,369 | 57 | 64 | 5 |
2,059 | E2 | 2059E2 | E2. Stop Gaming (Hard Version) | 2,900 | brute force; constructive algorithms; data structures; hashing; strings | This is the hard version of the problem. The difference between the versions is that in this version you need to output all the operations that need to be performed. You can hack only if you solved all versions of this problem.You are given \(n\) arrays, each of which has a length of \(m\). Let the \(j\)-th element of ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of arrays and the n... | For each test case, output a single integer β the minimum number of operations that need to be performed.Next, for each operation output two integers \(i\) and \(x\) (\(1 \le i \le n\), \(1 \le x \le 2 \cdot n \cdot m\)) β the index of the array where the element is inserted and the value of the element, respectively.I... | In the first test case, the following sequence of \(3\) operations is suitable: Apply the operation to the first array with \(x = 1\). Then the element \(1\) will be added to the beginning of the first array, and the value of \(x\) will become \(6\). The last element will be removed, and the first array will look like ... | Input: 42 22 63 41 27 81 55 4 1 2 35 4 3 2 13 31 2 34 5 67 8 911 1 212 3 413 5 64 41 2 3 45 6 7 89 10 11 1213 14 15 1617 1 2 34 18 5 67 19 8 209 21 22 10 | Output: 3 1 1 2 8 2 7 5 1 1 1 2 1 3 1 4 1 5 3 1 11 2 12 3 13 6 3 20 2 18 3 19 4 22 4 21 1 17 | Master | 5 | 1,472 | 965 | 418 | 20 |
1,151 | C | 1151C | C. Problem for Nazar | 1,800 | constructive algorithms; math | Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.Consider two infinite sets of numbers. The first set consists of odd positive numbers (\(1, 3, 5, 7, \ldots\)), and the second set consists of ev... | The first line contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq 10^{18}\)) β the range in which you need to find the sum. | Print a single integer β the answer modulo \(1000000007\) (\(10^9+7\)). | In the first example, the answer is the sum of the first three numbers written out (\(1 + 2 + 4 = 7\)).In the second example, the numbers with numbers from \(5\) to \(14\): \(5, 7, 9, 6, 8, 10, 12, 14, 16, 18\). Their sum is \(105\). | Input: 1 3 | Output: 7 | Medium | 2 | 1,308 | 134 | 71 | 11 |
472 | E | 472E | E. Design Tutorial: Learn from a Game | 2,800 | constructive algorithms; implementation | One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task.Let's have a try. Puzzle and Dragon was a popular game in Japan, we focus on the puzzle part of that game, it is a tile-matching puzzle.(Picture from Wikipedia page: http://e... | The first line contains two integers: n and m (1 β€ n, m β€ 30).The next n lines each contains m integers β the description of the initial board. The j-th integer in the i-th line is si, j (1 β€ si, j β€ 900), where si, j denotes the type of the orb located in the i-th row and j-th column of the board.The next n lines cont... | If there is no solution, then output: -1.If there is a solution, then in the first line output an integer k (1 β€ k β€ 106) β the number of finger moves.In the next line print two integers x0 and y0 (1 β€ x0 β€ n; 1 β€ y0 β€ m) β the position of the cell you touch at the beginning. In each of the next k lines print two integ... | Input: 2 21 32 31 33 2 | Output: 31 12 22 11 1 | Master | 2 | 1,254 | 429 | 687 | 4 | |
397 | A | 397A | A. On Segment's Own Points | 0 | implementation | Our old friend Alexey has finally entered the University of City N β the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm. The dorm has exactly one straight dryer β a 100 cent... | The first line contains a positive integer n (1 β€ n β€ 100). The (i + 1)-th line contains integers li and ri (0 β€ li < ri β€ 100) β the endpoints of the corresponding segment for the i-th student. | On a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments. | Note that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments.In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6) and (... | Input: 30 52 81 6 | Output: 1 | Beginner | 1 | 1,086 | 194 | 165 | 3 |
1,163 | A | 1163A | A. Eating Soup | 900 | greedy; math | The three friends, Kuro, Shiro, and Katie, met up again! It's time for a party...What the cats do when they unite? Right, they have a party. Since they wanted to have as much fun as possible, they invited all their friends. Now \(n\) cats are at the party, sitting in a circle and eating soup. The rules are simple: anyo... | The only line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 1000\), \(0 \leq m \leq n\)) β the initial number of cats at the party and the number of cats who left the circle at the moment Katie observes, respectively. | Print a single integer β the maximum number of groups of cats at the moment Katie observes. | In the first example, originally there are \(7\) cats sitting as shown below, creating a single group: At the observed moment, \(4\) cats have left the table. Suppose the cats \(2\), \(3\), \(5\) and \(7\) have left, then there are \(3\) groups remaining. It is possible to show that it is the maximum possible number of... | Input: 7 4 | Output: 3 | Beginner | 2 | 894 | 226 | 91 | 11 |
1,403 | A | 1403A | A. The Potion of Great Power | 2,400 | *special; 2-sat; binary search; data structures; graphs; interactive; sortings; two pointers | Once upon a time, in the Land of the Shamans, everyone lived on the Sky-High Beanstalk. Each shaman had a unique identifying number \(i\) between \(0\) and \(N-1\), and an altitude value \(H_i\), representing how high he lived above ground level. The distance between two altitudes is the absolute value of their differe... | Example queries: Evolution of friendships: | Input: 6 5 11 4 2 42 1000 54 68 234 0 1 2 0 3 4 3 5 3 5 1 3 5 3 0 5 3 0 1 3 3 5 0 3 4 3 0 8 0 5 5 3 0 11 | Output: 26 0 1000000000 14 | Expert | 8 | 2,358 | 0 | 0 | 14 | ||
1,780 | B | 1780B | B. GCD Partition | 1,100 | brute force; greedy; math; number theory | While at Kira's house, Josuke saw a piece of paper on the table with a task written on it.The task sounded as follows. There is an array \(a\) of length \(n\). On this array, do the following: select an integer \(k > 1\); split the array into \(k\) subsegments \(^\dagger\); calculate the sum in each of \(k\) subsegment... | The first line contains a single number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.For each test case, the first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\) (\(1 \le a_i \le 10^9 \)) β ... | For each test case print a single integer β the maximum score for the optimal partition. | In the first test case, you can choose \(k = 2\) and split the array into subsegments \((1, 2)\) and \((3, 4)\).Then the score of such a partition will be equal to \(\gcd(a_1 + a_2, a_3 + a_4) = \gcd(2 + 2, 1 + 3) = \gcd(4, 4) = 4\).In the fourth test case, you can choose \(k = 3\) and split the array into subsegments ... | Input: 642 2 1 321 231 4 561 2 1 1 1 31012 30 37 88 12 78 89 17 2 1267 7 7 7 7 7 | Output: 4 1 5 3 1 21 | Easy | 4 | 1,114 | 435 | 88 | 17 |
2,124 | C | 2124C | C. Subset Multiplication | 1,300 | constructive algorithms; greedy; math; number theory | Alice has an array \(a\), consisting of \(n\) positive integers. The array satisfies the beautiful property that \(a_i\) divides \(a_{i+1}\) for each \(1 \leq i \leq n - 1\). Bob sees Alice's beautiful array and is jealous. To sabotage her, Bob first creates an array \(b\) of size \(n\) such that \(b_i=a_i\) for each \... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot10^5\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 6\cdot10^5\)) β the length of the array \(b\).The second line of each test c... | For each test case, output any possible value of \(x\) (\(1 \leq x \leq 10^9\)) on a new line. It is guaranteed at least one value of \(x\) exists. | In the first test case, it is possible Bob selected \(x=343\) and \(S=\{\}\) (meaning he did not change the array \(a\) at all). In the third test case, it is possible Bob selected \(x=4\) and \(S=\{1,2\}\), meaning he multiplied both \(b_1\) and \(b_2\) by \(4\). The original array was \(\{1,2,4,8\}\), which satisfies... | Input: 422 431 1000000000 50000000044 8 4 8742 42 14 84 28 73080 255780 | Output: 343 2 4 6 | Easy | 4 | 956 | 660 | 147 | 21 |
1,304 | F1 | 1304F1 | F1. Animal Observation (easy version) | 2,300 | data structures; dp | The only difference between easy and hard versions is the constraint on \(k\).Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for \(n\) days, starting from day \(1\) to... | The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 50\), \(1 \le m \le 2 \cdot 10^4\), \(1 \le k \le min(m,20)\)) β the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next \(n\) lines contain \(m\) integers each. The \(... | Print one integer β the maximum number of animals that can be observed. | The optimal way to observe animals in the four examples are as follows:Example 1: Example 2: Example 3: Example 4: | Input: 4 5 2 0 2 1 1 0 0 0 3 1 2 1 0 4 3 1 3 3 0 0 4 | Output: 25 | Expert | 2 | 1,369 | 506 | 71 | 13 |
145 | E | 145E | E. Lucky Queries | 2,400 | data structures | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya brought home string s with the length of n. The string only consists of lucky digits. The digi... | The first line contains two integers n and m (1 β€ n β€ 106, 1 β€ m β€ 3Β·105) β the length of the string s and the number of queries correspondingly. The second line contains n lucky digits without spaces β Petya's initial string. Next m lines contain queries in the form described in the statement. | For each query count print an answer on a single line. | In the first sample the chronology of string s after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold): 47 74 74 In the second sample: 747 447 447 774 774 | Input: 2 347countswitch 1 2count | Output: 21 | Expert | 1 | 985 | 295 | 54 | 1 |
615 | B | 615B | B. Longtail Hedgehog | 1,600 | dp; graphs | This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hed... | First line of the input contains two integers n and m(2 β€ n β€ 100 000, 1 β€ m β€ 200 000) β the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the numbers of points connected by corresponding segment. It's guara... | Print the maximum possible value of the hedgehog's beauty. | The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3 = 9. | Input: 8 64 53 52 51 22 86 7 | Output: 9 | Medium | 2 | 1,390 | 379 | 58 | 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.