contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,933 | F | 1933F | F. Turtle Mission: Robot and the Earthquake | 2,100 | dfs and similar; dp; graphs; shortest paths | The world is a grid with \(n\) rows and \(m\) columns. The rows are numbered \(0, 1, \ldots, n-1\), while the columns are numbered \(0, 1, \ldots, m-1\). In this world, the columns are cyclic (i.e. the top and the bottom cells in each column are adjacent). The cell on the \(i\)-th row and the \(j\)-th column (\(0 \le i... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.In each test case, the first line contains two integers \(n\), \(m\) (\(3 \le n, m \le 10^3\)) β the size of the planet's boundaries. Each of the next \(n\) lines contains \(m\) integers. The \((j+1)\)-th integer on... | For each test case: If the destination can be reached without colliding with any rocks, output a single integer β the minimum amount of time RT needs to reach \((n-1,m-1)\). Otherwise, output \(-1\). | Visual explanation of the first test case in the example: | Input: 64 50 1 0 0 00 0 1 0 01 0 1 1 00 0 0 0 03 30 0 01 0 00 0 05 30 0 00 0 01 0 00 0 01 0 03 70 0 1 0 0 1 01 0 1 0 1 0 00 1 0 0 0 0 03 40 1 0 01 0 0 00 1 1 05 50 0 0 0 00 1 0 1 00 1 0 1 00 1 0 1 00 0 0 1 0 | Output: 7 3 3 8 -1 12 | Hard | 4 | 2,428 | 739 | 199 | 19 |
1,012 | F | 1012F | F. Passports | 3,400 | dp; implementation | Gleb is a famous competitive programming teacher from Innopolis. He is planning a trip to N programming camps in the nearest future. Each camp will be held in a different country. For each of them, Gleb needs to apply for a visa. For each of these trips Gleb knows three integers: the number of the first day of the trip... | In the first line of the input there are two integers N (1 β€ N β€ 22) and P (1 β€ P β€ 2)βthe number of trips and the number of passports Gleb has, respectively.The next N lines describe Gleb's trips. Each line contains three positive integers si, leni, ti (1 β€ si, leni, ti β€ 109)βthe first day of the trip, the length of ... | If it is impossible to get all visas on time, just print ""NO"" (quotes for clarity). Otherwise, print ""YES"" and N lines describing trips. For each trip, first print number of the passport Gleb should put this country's visa in, and then print number of the day he should apply for it. Print trips in the same order as... | Examples with answer ""YES"" are depicted below.Each cell of the stripe represents a single day. Rectangles represent trips, each trip starts in the morning and ends in the evening. Rectangles with angled corners represent visa applications. Each application starts in the middle of a day and ends ti days after. The tri... | Input: 2 13 1 16 1 1 | Output: YES1 11 4 | Master | 2 | 1,776 | 460 | 528 | 10 |
42 | E | 42E | E. Baldman and the military | 2,700 | dfs and similar; graphs; trees | Baldman is a warp master. He possesses a unique ability β creating wormholes! Given two positions in space, Baldman can make a wormhole which makes it possible to move between them in both directions. Unfortunately, such operation isn't free for Baldman: each created wormhole makes him lose plenty of hair from his head... | First line of the input contains a single natural number n (2 β€ n β€ 100000) β the number of objects on the military base. The second line β one number m (1 β€ m β€ 200000) β the number of the wormholes Baldman can make. The following m lines describe the wormholes: each line contains three integer numbers a, b, c (1 β€ a,... | Your program should output q lines, one for each query. The i-th line should contain a single integer number β the answer for i-th query: the minimum cost (in hair) of a system of wormholes allowing the optimal patrol for any system of tunnels (satisfying the given conditions) if ai and bi are the two objects connected... | Input: 211 2 311 2 | Output: 0 | Master | 3 | 2,210 | 749 | 388 | 0 | |
1,411 | F | 1411F | F. The Thorny Path | 3,000 | greedy; math | According to a legend the Hanoi Temple holds a permutation of integers from \(1\) to \(n\). There are \(n\) stones of distinct colors lying in one line in front of the temple. Monks can perform the following operation on stones: choose a position \(i\) (\(1 \le i \le n\)) and cyclically shift stones at positions \(i\),... | Each test consists of multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^3\)). Description of the test cases follows.The first line of each test case contains \(n\) (\(3 \leq n \leq 10^6\)). The next line contains \(n\) integers \(p_1, \dots, p_n\) (\(1 \le p_i \le n\)). It ... | For each of the \(t\) test cases, print two integers on a new line: the largest possible number of days the world can last, modulo \(10^9 + 7\), and the minimum number of exchanges required for that. | Let's label the colors of the stones with letters. Explanations for the first two test cases of the first example: Using the permutation \([2, 3, 1]\), we can additionally obtain the arrangements CAB and BCA from ABC. This is already the maximum possible result. Using the permutation \([2, 1, 3]\), the only BAC can be ... | Input: 3 3 2 3 1 3 2 1 3 3 1 2 3 | Output: 3 0 3 1 3 2 | Master | 2 | 1,228 | 446 | 199 | 14 |
1,950 | E | 1950E | E. Nearly Shortest Repeating Substring | 1,500 | brute force; implementation; number theory; strings | You are given a string \(s\) of length \(n\) consisting of lowercase Latin characters. Find the length of the shortest string \(k\) such that several (possibly one) copies of \(k\) can be concatenated together to form a string with the same length as \(s\) and, at most, one different character.More formally, find the l... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2\cdot10^5\)) β the length of string \(s\).The second line of each test case contains the string \(s\), consisting of lowercase Latin chara... | For each test case, print the length of the shortest string \(k\) satisfying the constraints in the statement. | In the first test case, you can select \(k = \texttt{a}\) and \(k+k+k+k = \texttt{aaaa}\), which only differs from \(s\) in the second position.In the second test case, you cannot select \(k\) of length one or two. We can have \(k = \texttt{abba}\), which is equal to \(s\). | Input: 54abaa4abba13slavicgslavic8hshahaha20stormflamestornflame | Output: 1 4 13 2 10 | Medium | 4 | 591 | 394 | 110 | 19 |
1,178 | F2 | 1178F2 | F2. Long Colorful Strip | 2,600 | dp | This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of \(m\) and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one.There are \(n+1\) distinct colours... | The first line contains a two integers \(n\), \(m\) (\(1 \leq n \leq 500\), \(n \leq m \leq 10^6\)) β the number of colours excluding the colour \(0\) and the length of the paper, respectively.The second line contains \(m\) space separated integers \(c_1, c_2, \ldots, c_m\) (\(1 \leq c_i \leq n\)) β the colour visible ... | Output a single integer β the number of ways Alice can perform the painting, modulo \(998244353\). | In the first example, there are \(5\) ways, all depicted in the figure below. Here, \(0\) is white, \(1\) is red, \(2\) is green and \(3\) is blue.Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour \(2\).In t... | Input: 3 3 1 2 3 | Output: 5 | Expert | 1 | 1,176 | 478 | 98 | 11 |
1,647 | E | 1647E | E. Madoka and the Sixth-graders | 2,500 | data structures; dfs and similar; greedy | After the most stunning success with the fifth-graders, Madoka has been trusted with teaching the sixth-graders.There's \(n\) single-place desks in her classroom. At the very beginning Madoka decided that the student number \(b_i\) (\(1 \le b_i \le n\)) will sit at the desk number \(i\). Also there's an infinite line o... | The first line of input data contains an integer \(n\) (\(2 \le n \le 10^5\)) β a number of desks in the classroom.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β desks where the students move. It is guaranteed that \(p\) has at least two equal elements.The third line contain... | In the only line print \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\)) β lexicographically minimum permutation describing the initial seating of the sixth-graders that can lead to the final seating \(a\). | The description of the first test is below: The first picture shows the starting permutation, which is the answer. Then the students sitting at desks \(1, 2\) are transferred to a \(5\) desk. Also, a \(1\) student moved from a \(5\) desk, and a student from a \(4\) disk is transferred to a \(3\) desk.Thus, after all th... | Input: 5 5 5 3 3 1 1 8 2 9 4 | Output: 1 3 2 5 4 | Expert | 3 | 1,962 | 526 | 219 | 16 |
630 | B | 630B | B. Moore's Law | 1,200 | math | The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time.Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately ever... | The only line of the input contains a pair of integers n (1000 β€ n β€ 10 000) and t (0 β€ t β€ 2 000 000 000) β the number of transistors in the initial time and the number of seconds passed since the initial time. | Output one number β the estimate of the number of transistors in a dence integrated circuit in t seconds since the initial time. The relative error of your answer should not be greater than 10 - 6. | Input: 1000 1000000 | Output: 1011.060722383550382782399454922040 | Easy | 1 | 634 | 211 | 197 | 6 | |
1,637 | D | 1637D | D. Yet Another Minimization Problem | 1,800 | dp; greedy; math | You are given two arrays \(a\) and \(b\), both of length \(n\).You can perform the following operation any number of times (possibly zero): select an index \(i\) (\(1 \leq i \leq n\)) and swap \(a_i\) and \(b_i\).Let's define the cost of the array \(a\) as \(\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2\). Similarly,... | Each test case consists of several test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 40\)) β the number of test cases. The following is a description of the input data sets.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)) β the length of both arrays.The second... | For each test case, print the minimum possible total cost. | In the second test case, in one of the optimal answers after all operations \(a = [2, 6, 4, 6]\), \(b = [3, 7, 6, 1]\).The cost of the array \(a\) equals to \((2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508\).The cost of the array \(b\) equals to \((3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)... | Input: 313643 6 6 62 7 4 146 7 2 42 5 3 5 | Output: 0 987 914 | Medium | 3 | 458 | 673 | 58 | 16 |
1,555 | D | 1555D | D. Say No to Palindromes | 1,600 | brute force; constructive algorithms; dp; strings | Let's call the string beautiful if it does not contain a substring of length at least \(2\), which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of the string \(s\) and the number of queries.The second line contains the string \(s\), it consists of \(n\) characters, each character one of the first \(3\) Latin letters.The following \(m\) lines contain two integers... | For each query, print a single integer β the cost of the substring of the string \(s\) from \(l_i\)-th to \(r_i\)-th position, inclusive. | Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the s... | Input: 5 4 baacb 1 3 1 5 4 5 2 3 | Output: 1 2 0 1 | Medium | 4 | 897 | 406 | 137 | 15 |
290 | D | 290D | D. Orange | 1,400 | *special; implementation | The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.The second line of the input is an integer between 0 and 26, inclusive. | Output the required string. | Input: AprilFool14 | Output: AprILFooL | Easy | 2 | 0 | 234 | 27 | 2 | ||
859 | E | 859E | E. Desk Disorder | 2,100 | combinatorics; dfs and similar; dsu; graphs; trees | A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at,... | Input will begin with a line containing N (1 β€ N β€ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2Β·N. It is gua... | Print the number of possible assignments, modulo 1000000007 = 109 + 7. | These are the possible assignments for the first example: 1 5 3 7 1 2 3 7 5 2 3 7 1 5 7 3 1 2 7 3 5 2 7 3 | Input: 41 55 23 77 3 | Output: 6 | Hard | 5 | 810 | 371 | 70 | 8 |
371 | D | 371D | D. Vessels | 1,800 | data structures; dsu; implementation; trees | There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters. Initially, all the vessels are empty. In some vessels water is poured. All the water that o... | The first line contains integer n β the number of vessels (1 β€ n β€ 2Β·105). The second line contains n integers a1, a2, ..., an β the vessels' capacities (1 β€ ai β€ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m ... | For each query, print on a single line the number of liters of water in the corresponding vessel. | Input: 25 1061 1 42 11 2 51 1 42 12 2 | Output: 458 | Medium | 4 | 799 | 579 | 97 | 3 | |
1,835 | D | 1835D | D. Doctor's Brown Hypothesis | 2,900 | dfs and similar; graphs; math; number theory | The rebels have been crushed in the most recent battle with the imperial forces, but there is a ray of new hope.Meanwhile, on one of the conquered planets, Luke was getting ready for an illegal street race (which should come as no surprise, given his family history). Luke arrived at the finish line with 88 miles per ho... | In the first line of input, there are three integer numbers \(n\), \(m\), and \(k\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq 2 \cdot 10^5\), \(n^3 \leq k \leq 10^{18}\)) denoting respectively the number of planets, wormholes and hours left until the battle starts.The following \(m\) lines contain two integers each, \(x... | In the first and only line, your program should output the number of possible ways of choosing a pair or a single warship for the mission. | In the first sample test, one can choose pairs of ships from the following planets: \(2\) and \(5\), \(3\) and \(5\), \(1\) and \(4\). Individual ships from planets \(6\) and \(7\) could also be chosen.In the second sample test, one can choose a pair of ships from the following planets: \(2\) and \(3\). Individual ship... | Input: 7 8 346 1 2 1 3 2 4 3 4 4 5 5 1 6 7 7 6 | Output: 5 | Master | 4 | 1,558 | 575 | 138 | 18 |
1,100 | E | 1100E | E. Andrew and Taxi | 2,200 | binary search; dfs and similar; graphs | Andrew prefers taxi to other means of transport, but recently most taxi drivers have been acting inappropriately. In order to earn more money, taxi drivers started to drive in circles. Roads in Andrew's city are one-way, and people are not necessary able to travel from one part to another, but it pales in comparison to... | The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 100\,000\), \(1 \leq m \leq 100\,000\)) β the number of crossroads and the number of roads in the city, respectively.Each of the following \(m\) lines contain three integers \(u_{i}\), \(v_{i}\) and \(c_{i}\) (\(1 \leq u_{i}, v_{i} \leq n\), \(1 \leq... | In the first line output two integers the minimal amount of traffic controllers required to complete the task and amount of roads \(k\) which should be reversed. \(k\) should not be minimized.In the next line output \(k\) integers separated by spaces β numbers of roads, the directions of which should be reversed. The r... | There are two simple cycles in the first example: \(1 \rightarrow 5 \rightarrow 2 \rightarrow 1\) and \(2 \rightarrow 3 \rightarrow 4 \rightarrow 5 \rightarrow 2\). One traffic controller can only reverse the road \(2 \rightarrow 1\) and he can't destroy the second cycle by himself. Two traffic controllers can reverse ... | Input: 5 62 1 15 2 62 3 23 4 34 5 51 5 4 | Output: 2 21 3 | Hard | 3 | 1,138 | 498 | 441 | 11 |
551 | B | 551B | B. ZgukistringZ | 1,800 | brute force; constructive algorithms; implementation; strings | Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x i... | The first line contains string a, the second line contains string b, and the third line contains string c (1 β€ |a|, |b|, |c| β€ 105, where |s| denotes the length of string s).All three strings consist only of lowercase English letters. It is possible that b and c coincide. | Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them. | In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 β 2 (ab), 3 β 4 (ab), 5 β 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc. | Input: aaaab | Output: aaa | Medium | 4 | 613 | 272 | 129 | 5 |
2,056 | A | 2056A | A. Shape Perimeter | 800 | constructive algorithms; math | There is an \(m\) by \(m\) square stamp on an infinite piece of paper. Initially, the bottom-left corner of the square stamp is aligned with the bottom-left corner of the paper. You are given two integer sequences \(x\) and \(y\), each of length \(n\). For each step \(i\) from \(1\) to \(n\), the following happens: Mov... | 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 two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(2 \le m \le 100\)) β the number of operations performed and the... | For each test case, output a single integer representing the perimeter of the colored shape on the paper. | In the first example, the stamp has a side length of \(3\) and is pressed \(4\) times at coordinates \((1, 1)\), \((3, 3)\), \((5, 4)\), and \((6, 6)\). The piece of paper looks like that afterwards:Here, the square formed by the first press is colored blue, the second red, the third green, and the fourth purple. The c... | Input: 34 31 12 22 11 21 21 16 73 61 13 16 65 46 1 | Output: 32 8 96 | Beginner | 2 | 889 | 639 | 105 | 20 |
69 | E | 69E | E. Subsegments | 1,800 | data structures; implementation | Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maxi... | The first line contains two positive integers n and k (1 β€ n β€ 105, 1 β€ k β€ n) β the number of array elements and the length of the segment. Then follow n lines: the i-th one contains a single number ai ( - 109 β€ ai β€ 109). | Print nβk + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 β¦ ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print ""Nothing"". | Input: 5 312233 | Output: 132 | Medium | 2 | 417 | 223 | 249 | 0 | |
1,922 | C | 1922C | C. Closest Cities | 1,300 | greedy; implementation; math | There are \(n\) cities located on the number line, the \(i\)-th city is in the point \(a_i\). The coordinates of the cities are given in ascending order, so \(a_1 < a_2 < \dots < a_n\).The distance between two cities \(x\) and \(y\) is equal to \(|a_x - a_y|\).For each city \(i\), let's define the closest city \(j\) as... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case is given in the following format: the first line contains one integer \(n\) (\(2 \le n \le 10^5\)); the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_1 < a_2 < \dots < a_n \le 10^9\)); th... | For each query, print one integer β the minimum number of coins you have to spend. | Let's consider the first two queries in the example from the statement: in the first query, you are initially in the city \(1\). You can travel to the closest city (which is the city \(2\)), paying \(1\) coin. Then you travel to the closest city (which is the city \(3\)) again, paying \(1\) coin. Then you travel to the... | Input: 150 8 12 15 2051 41 53 43 25 1 | Output: 3 8 1 4 14 | Easy | 3 | 1,528 | 913 | 82 | 19 |
1,016 | A | 1016A | A. Death Note | 900 | greedy; implementation; math | You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: ""You have to write names in this notebook during \(n\) consecutive days. During the \(i\)-th day you have to write exactly \(a_i\) names."". You got scared... | The first line of the input contains two integers \(n\), \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \l... | Print exactly \(n\) integers \(t_1, t_2, \dots, t_n\), where \(t_i\) is the number of times you will turn the page during the \(i\)-th day. | In the first example pages of the Death Note will look like this \([1, 1, 1, 2, 2], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [3, 3, 3, 3]\). Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the se... | Input: 3 53 7 9 | Output: 0 2 1 | Beginner | 3 | 1,338 | 434 | 139 | 10 |
1,475 | E | 1475E | E. Advertising Agency | 1,600 | combinatorics; math; sortings | Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of \(n\) different bloggers. Blogger numbered \(i\) has \(a_i\) followers.Since Masha has a limited budget, she can only sign a contract with \(k\) different blogg... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1000\)) β the number of bloggers and how many of them you can sign a contract with.The second line of each... | For each test case, on a separate line output one integer β the number of ways to select \(k\) bloggers so that the total number of their followers is maximum possible. | The test case is explained in the statements.In the second test case, the following ways are valid: conclude contracts with bloggers with numbers \(1\) and \(2\). In this case, the number of followers will be equal to \(a_1 + a_2 = 2\); conclude contracts with bloggers with numbers \(1\) and \(3\). In this case, the nu... | Input: 3 4 3 1 3 1 2 4 2 1 1 1 1 2 1 1 2 | Output: 2 6 1 | Medium | 3 | 1,357 | 528 | 168 | 14 |
1,620 | A | 1620A | A. Equal or Not Equal | 800 | constructive algorithms; dsu; implementation | You had \(n\) positive integers \(a_1, a_2, \dots, a_n\) arranged in a circle. For each pair of neighboring numbers (\(a_1\) and \(a_2\), \(a_2\) and \(a_3\), ..., \(a_{n - 1}\) and \(a_n\), and \(a_n\) and \(a_1\)), you wrote down: are the numbers in the pair equal or not.Unfortunately, you've lost a piece of paper wi... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains a non-empty string \(s\) consisting of characters E and/or N. The length of \(s\) is equal to the size of array \(n\) and \(2 \le n \le 50\). For e... | For each test case, print YES if it's possible to choose array \(a\) that are consistent with information from \(s\) you know. Otherwise, print NO.It can be proved, that if there exists some array \(a\), then there exists an array \(a\) of positive integers with values less or equal to \(10^9\). | In the first test case, you can choose, for example, \(a_1 = a_2 = a_3 = 5\).In the second test case, there is no array \(a\), since, according to \(s_1\), \(a_1\) is equal to \(a_2\), but, according to \(s_2\), \(a_2\) is not equal to \(a_1\).In the third test case, you can, for example, choose array \(a = [20, 20, 4,... | Input: 4 EEE EN ENNEENE NENN | Output: YES NO YES YES | Beginner | 3 | 595 | 527 | 296 | 16 |
2,026 | B | 2026B | B. Black Cells | 1,300 | binary search; brute force; constructive algorithms; greedy | You are given a strip divided into cells, numbered from left to right from \(0\) to \(10^{18}\). Initially, all cells are white.You can perform the following operation: choose two white cells \(i\) and \(j\), such that \(i \ne j\) and \(|i - j| \le k\), and paint them black.A list \(a\) is given. All cells from this li... | The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 < a_i < 10^{18}\); \(a_i < a_{i + 1}\)).Additional constraint on t... | For each test case, print a single integer β the minimum value of \(k\) for which it is possible to paint all the given cells black. | In the first example, with \(k=1\), it is possible to paint the cells \((1, 2)\).In the second example, with \(k=1\), it is possible to paint the cells \((7, 8)\).In the third example, with \(k=2\), it is possible to paint the cells \((2, 4)\) and \((8, 9)\).In the fourth example, with \(k=3\), it is possible to paint ... | Input: 421 21732 4 951 5 8 10 13 | Output: 1 1 2 3 | Easy | 4 | 509 | 394 | 132 | 20 |
2,068 | I | 2068I | I. Pinball | 3,500 | graphs; shortest paths | You are playing a pinball-like game on a \(h \times w\) grid.The game begins with a small ball located at the center of a specific cell marked as \(\texttt{S}\). Each cell of the grid is either: A block-type wall (\(\texttt{#}\)) that prevents the ball from entering the cell, reflecting it instead. A thin oblique wall,... | The first line contains two integers \(h\) and \(w\) (\(1\le h, w \le 1000\)) β the size of the grid.The next \(h\) lines describe the grid at the beginning of the game.The \(i\)-th of these lines contains \(w\) characters, describing the cells on the \(i\)-th row. A dot (\(\texttt{.}\)) denotes a free cell, a hash sig... | Print \(\texttt{YES}\) if it is possible to make the ball escape the grid. Otherwise, print \(\texttt{NO}\).If it is possible, print the following extra information.On the second line, print a single character \(d \in \{ \texttt{U}, \texttt{D}, \texttt{L}, \texttt{R} \}\) β the direction of the starting nudge of the ba... | In the first sample, the minimum number of walls to be destroyed is 2. We describe the relevant moments of the solution given as sample output. At time \(t=0\), the ball is in its initial position and gets nudged towards the left direction. At time \(t=4.5\), the ball hits a block-type wall and reflects off of it. Righ... | Input: 4 6#\###.#./S###\\..####### | Output: YES L 2 7 3 3 8 1 2 | Master | 2 | 1,612 | 760 | 1,374 | 20 |
1,436 | F | 1436F | F. Sum Over Subsets | 2,800 | combinatorics; math; number theory | You are given a multiset \(S\). Over all pairs of subsets \(A\) and \(B\), such that: \(B \subset A\); \(|B| = |A| - 1\); greatest common divisor of all elements in \(A\) is equal to one; find the sum of \(\sum_{x \in A}{x} \cdot \sum_{x \in B}{x}\), modulo \(998\,244\,353\). | The first line contains one integer \(m\) (\(1 \le m \le 10^5\)): the number of different values in the multiset \(S\).Each of the next \(m\) lines contains two integers \(a_i\), \(freq_i\) (\(1 \le a_i \le 10^5, 1 \le freq_i \le 10^9\)). Element \(a_i\) appears in the multiset \(S\) \(freq_i\) times. All \(a_i\) are d... | Print the required sum, modulo \(998\,244\,353\). | A multiset is a set where elements are allowed to coincide. \(|X|\) is the cardinality of a set \(X\), the number of elements in it.\(A \subset B\): Set \(A\) is a subset of a set \(B\).In the first example \(B=\{1\}, A=\{1,2\}\) and \(B=\{2\}, A=\{1, 2\}\) have a product equal to \(1\cdot3 + 2\cdot3=9\). Other pairs o... | Input: 2 1 1 2 1 | Output: 9 | Master | 3 | 276 | 329 | 49 | 14 |
743 | D | 743D | D. Chloe and pleasant prizes | 1,800 | dfs and similar; dp; graphs; trees | Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer fro... | The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the desc... | If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.Otherwise print Impossible. | Input: 80 5 -1 4 3 2 6 51 22 42 51 33 66 76 8 | Output: 25 | Medium | 4 | 1,892 | 659 | 187 | 7 | |
530 | B | 530B | B. String inside out | 1,600 | *special | You are given a string S of even length s1..s2n . Perform the following manipulations: divide it into two halves s1..sn and sn + 1..s2n reverse each of them sn..s1 and s2n..sn + 1 concatenate the resulting strings into sn..s1s2n..sn + 1Output the result of these manipulations. | The only line of the input contains a string of lowercase Latin letters. The length of the string is between 2 and 20, inclusive, and it is even. | Output the string which is the result of the described manipulations. | Input: codeforces | Output: fedocsecro | Medium | 1 | 277 | 145 | 69 | 5 | |
177 | D2 | 177D2 | D2. Encrypting Messages | 1,500 | data structures | The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a sequence of m integers b... | The first input line contains three integers n, m and c, separated by single spaces. The second input line contains n integers ai (0 β€ ai < c), separated by single spaces β the original message. The third input line contains m integers bi (0 β€ bi < c), separated by single spaces β the encryption key.The input limitatio... | Print n space-separated integers β the result of encrypting the original message. | In the first sample the encryption is performed in two steps: after the first step a = (0, 0, 0, 1) (remember that the calculations are performed modulo 2), after the second step a = (0, 1, 1, 0), and that is the answer. | Input: 4 3 21 1 1 11 1 1 | Output: 0 1 1 0 | Medium | 1 | 1,048 | 455 | 81 | 1 |
198 | C | 198C | C. Delivering Carcinogen | 2,400 | binary search; geometry | Qwerty the Ranger arrived to the Diatar system with a very important task. He should deliver a special carcinogen for scientific research to planet Persephone. This is urgent, so Qwerty has to get to the planet as soon as possible. A lost day may fail negotiations as nobody is going to pay for an overdue carcinogen.You... | The first line contains space-separated integers xp, yp and vp ( - 104 β€ xp, yp β€ 104, 1 β€ vp < 104) β Persephone's initial position and the speed at which it goes round Diatar.The second line contains space-separated integers x, y, v and r ( - 104 β€ x, y β€ 104, 1 < v β€ 104, 1 β€ r β€ 104) β The intial position of Qwerty... | Print a single real number β the minimum possible delivery time. The answer will be considered valid if its absolute or relative error does not exceed 10 - 6. | Input: 10 0 1-10 0 2 8 | Output: 9.584544103 | Expert | 2 | 1,098 | 454 | 158 | 1 | |
1,987 | H | 1987H | H. Fumo Temple | 3,500 | interactive | This temple only magnifies the mountain's power.BadelineThis is an interactive problem.You are given two positive integers \(n\) and \(m\) (\(\bf{n \le m}\)).The jury has hidden from you a rectangular matrix \(a\) with \(n\) rows and \(m\) columns, where \(a_{i,j} \in \{ -1, 0, 1 \}\) for all \(1 \le i \le n\) and \(1 ... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 50\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 5000\)) β the numbers of rows and the numb... | The hidden matrix in the first test case: \(1\)\(0\)\(1\)\(\color{red}{\textbf{0}}\)\(1\)\(0\)\(0\)\(1\)\(0\)\(-1\)\(-1\)\(-1\)The hidden matrix in the second test case: \(\color{red}{\textbf{0}}\)Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interac... | Input: 2 3 4 5 3 5 1 1 0 | Output: ? 1 1 ? 3 3 ? 3 2 ! 1 4 ? 1 1 ! 1 1 | Master | 1 | 944 | 475 | 0 | 19 | |
708 | D | 708D | D. Incorrect Flow | 2,900 | flows | At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to... | The first line of the input contains two integers n and m (2 β€ n β€ 100, 0 β€ m β€ 100) β the number of nodes and edges in the graph respectively. Each of the following m lines contains the description of the edges, consisting of four integers ui, vi, ci and fi (1 β€ ui, vi β€ n, ui β vi, 0 β€ ci, fi β€ 1 000 000) β index of ... | Print one integer β the minimum total sum of changes that Sasha has to do in order to get the correct flow description. | In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required.In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1.In the third sample, ther... | Input: 2 11 2 2 1 | Output: 0 | Master | 1 | 2,346 | 631 | 119 | 7 |
835 | D | 835D | D. Palindromic characteristics | 1,900 | brute force; dp; hashing; strings | Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.A string is 1-palindrome if and only if it reads the same backward as forward.A string is k-palindrome (k > 1) if and only if: Its left half e... | The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters. | Print |s| integers β palindromic characteristics of string s. | In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here. | Input: abba | Output: 6 1 0 0 | Hard | 4 | 732 | 94 | 61 | 8 |
1,857 | C | 1857C | C. Assembly via Minimums | 1,200 | greedy; sortings | Sasha has an array \(a\) of \(n\) integers. He got bored and for all \(i\), \(j\) (\(i < j\)), he wrote down the minimum value of \(a_i\) and \(a_j\). He obtained a new array \(b\) of size \(\frac{n\cdot (n-1)}{2}\).For example, if \(a=\) [\(2,3,5,1\)], he would write [\(\min(2, 3), \min(2, 5), \min(2, 1), \min(3, 5), ... | The first line contains a single integer \(t\) (\(1\le t\le 200\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2\le n\le 10^3\)) β the length of array \(a\).The second line of each test case contains \(\frac{n\cdot (n-1)}{2}\) integers \(b_1,b_2,\dots,b_{\frac{n\cdot (... | For each test case, output any possible array \(a\) of length \(n\). | In the first sample, Sasha chose the array \([1,3,3]\), then the array \(b\) will look like \([\min(a_1,a_2)=1, \min(a_1,a_3)=1, \min(a_2,a_3)=3]\), after shuffling its elements, the array can look like \([1,3,1]\).In the second sample, there is only one pair, so the array \([10,10]\) is suitable. Another suitable arra... | Input: 531 3 121047 5 3 5 3 352 2 2 2 2 2 2 2 2 253 0 0 -2 0 -2 0 0 -2 -2 | Output: 1 3 3 10 10 7 5 3 12 2 2 2 2 2 0 -2 0 3 5 | Easy | 2 | 652 | 537 | 68 | 18 |
520 | C | 520C | C. DNA Alignment | 1,500 | math; strings | Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which ... | The first line of the input contains a single integer n (1 β€ n β€ 105).The second line of the input contains a single string of length n, consisting of characters ""ACGT"". | Print a single number β the answer modulo 109 + 7. | Please note that if for two distinct strings t1 and t2 values Ο(s, t1) ΠΈ Ο(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.In the first sample, there is Ο(""C"", ""C"") = 1, for the remaining strin... | Input: 1C | Output: 1 | Medium | 2 | 1,186 | 171 | 50 | 5 |
2,068 | D | 2068D | D. Morse Code | 3,100 | dp; sortings; trees | Morse code is a classical way to communicate over long distances, but there are some drawbacks that increase the transmission time of long messages. In Morse code, each character in the alphabet is assigned a sequence of dots and dashes such that no sequence is a prefix of another. To transmit a string of characters, t... | The first line contains an integer \(n\) (\(2\le n\le 200\)) β the number of characters in the alphabet.The second line contains \(n\) real numbers \(f_1\), \(f_2\), \(\ldots\), \(f_n\) (\(0 < f_i < 1\)) β \(f_i\) is the frequency of the \(i\)-th character. All values \(f_1\), \(f_2\), \(\ldots\), \(f_n\) are given wit... | Print \(n\) lines, each containing one string consisting of dots \(\texttt{.}\) and dashes \(\texttt{-}\). The \(i\)-th line corresponds to the sequence of dots and dashes that you assign to the \(i\)-th character.If there are multiple valid assignments with the minimum possible expected transmission time, any of them ... | In the first sample, the alphabet contains three letters, say \(a\), \(b\), and \(c\), with respective frequencies \(0.3\), \(0.6\), and \(0.1\). In the optimal assignment, we assign \(a\) to '\(\texttt{-.}\)', \(b\) to '\(\texttt{.}\)', and \(c\) to '\(\texttt{--}\)'. This gives an expected transmission time of \(0.3 ... | Input: 30.3000 0.6000 0.1000 | Output: -. . -- | Master | 3 | 919 | 407 | 342 | 20 |
2,059 | E1 | 2059E1 | E1. Stop Gaming (Easy Version) | 2,500 | brute force; constructive algorithms; greedy; hashing; strings | This is the easy version of the problem. The difference between the versions is that in this version you only need to find the minimum number of operations. 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 the \(i\)... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains 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. | 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 5 3 6 | Expert | 5 | 1,408 | 965 | 105 | 20 |
286 | E | 286E | E. Ladies' Shop | 2,800 | constructive algorithms; fft; math | A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought n bags. Each bag is characterised by the total weight ai of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than ai. However ... | The first line contains space-separated integers n and m (1 β€ n, m β€ 106). The second line contains n distinct space-separated integers a1, a2, ..., an (1 β€ a1 < a2 < ... < an β€ m) β the bags' weight limits. | In the first line print ""NO"" (without the quotes) if there isn't set pi, that would meet the conditions.Otherwise, in the first line print ""YES"" (without the quotes), in the second line print an integer k (showing how many numbers are in the suitable set with the minimum number of weights), in the third line print ... | Input: 6 105 6 7 8 9 10 | Output: YES55 6 7 8 9 | Master | 3 | 1,188 | 207 | 440 | 2 | |
919 | B | 919B | B. Perfect Number | 1,100 | binary search; brute force; dp; implementation; number theory | We consider a positive integer perfect, if and only if the sum of its digits is exactly \(10\). Given a positive integer \(k\), your task is to find the \(k\)-th smallest perfect positive integer. | A single line with a positive integer \(k\) (\(1 \leq k \leq 10\,000\)). | A single number, denoting the \(k\)-th smallest perfect integer. | The first perfect integer is \(19\) and the second one is \(28\). | Input: 1 | Output: 19 | Easy | 5 | 196 | 72 | 64 | 9 |
1,538 | C | 1538C | C. Number of Pairs | 1,300 | binary search; data structures; math; two pointers | You are given an array \(a\) of \(n\) integers. Find the number of pairs \((i, j)\) (\(1 \le i < j \le n\)) where the sum of \(a_i + a_j\) is greater than or equal to \(l\) and less than or equal to \(r\) (that is, \(l \le a_i + a_j \le r\)).For example, if \(n = 3\), \(a = [5, 1, 2]\), \(l = 4\) and \(r = 7\), then tw... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains three integers \(n, l, r\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le l \le r \le 10^9\)) β the length of the array and the limits on the sum in the pair.The second line contains \(n\) int... | For each test case, output a single integer β the number of index pairs \((i, j)\) (\(i < j\)), such that \(l \le a_i + a_j \le r\). | Input: 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 | Output: 2 7 0 1 | Easy | 4 | 431 | 468 | 132 | 15 | |
40 | D | 40D | D. Interesting Sequence | 2,600 | math | Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). A... | The first line contains integer A (1 β€ A < 10300). It is guaranteed that the number doesn't contain leading zeros. | On the first output line print YES, if there could be a year in which the total population of the country equaled A, otherwise print NO. If the answer is YES, then you also have to print number k β the number of years in which the population could equal A. On the next line you have to output precisely k space-separated... | Input: 2 | Output: YES110 | Expert | 1 | 1,224 | 114 | 906 | 0 | |
1,283 | D | 1283D | D. Christmas Trees | 1,800 | graphs; greedy; shortest paths | There are \(n\) Christmas trees on an infinite number line. The \(i\)-th tree grows at the position \(x_i\). All \(x_i\) are guaranteed to be distinct.Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.There are \(m\) p... | The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of Christmas trees and the number of people.The second line of the input contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(-10^9 \le x_i \le 10^9\)), where \(x_i\) is the position of the \(i\)-th Christmas... | In the first line print one integer \(res\) β the minimum possible value of \(\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j|\) (in other words, the sum of distances to the nearest Christmas tree for all people).In the second line print \(m\) integers \(y_1, y_2, \dots, y_m\) (\(-2 \cdot 10^9 \le y_j \le 2 \cdot ... | Input: 2 6 1 5 | Output: 8 -1 2 6 4 0 3 | Medium | 3 | 1,055 | 374 | 543 | 12 | |
585 | E | 585E | E. Present for Vitalik the Philatelist | 2,900 | combinatorics; math; number theory | Vitalik the philatelist has a birthday today!As he is a regular customer in a stamp store called 'Robin Bobin', the store management decided to make him a gift.Vitalik wants to buy one stamp and the store will give him a non-empty set of the remaining stamps, such that the greatest common divisor (GCD) of the price of ... | The first line of the input contains integer n (2 β€ n β€ 5Β·105) β the number of distinct stamps, available for sale in the 'Robin Bobin' store. The second line contains a sequence of integers a1, a2, ..., an (2 β€ ai β€ 107), where ai is the price of the i-th stamp. | Print a single integer β the remainder of the sought number of situations modulo 109 + 7. | In the first sample the following situations are possible: Vitalik buys the 1-st stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 3-rd stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 1-st stamp as a present; Vitalik buys the 2-nd stamp... | Input: 32 3 2 | Output: 5 | Master | 3 | 931 | 263 | 89 | 5 |
1,324 | B | 1324B | B. Yet Another Palindrome Problem | 1,100 | brute force; strings | You are given an array \(a\) consisting of \(n\) integers.Your task is to determine if \(a\) has some subsequence of length at least \(3\) that is a palindrome.Recall that an array \(b\) is called a subsequence of the array \(a\) if \(b\) can be obtained by removing some (possibly, zero) elements from \(a\) (not necess... | The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Next \(2t\) lines describe test cases. The first line of the test case contains one integer \(n\) (\(3 \le n \le 5000\)) β the length of \(a\). The second line of the test case contains \(n\) integers \(a_1, a_2, \do... | For each test case, print the answer β ""YES"" (without quotes) if \(a\) has some subsequence of length at least \(3\) that is a palindrome and ""NO"" otherwise. | In the first test case of the example, the array \(a\) has a subsequence \([1, 2, 1]\) which is a palindrome.In the second test case of the example, the array \(a\) has two subsequences of length \(3\) which are palindromes: \([2, 3, 2]\) and \([2, 2, 2]\).In the third test case of the example, the array \(a\) has no s... | Input: 5 3 1 2 1 5 1 2 2 3 2 3 1 1 2 4 1 2 2 1 10 1 1 2 2 3 3 4 4 5 5 | Output: YES YES NO YES NO | Easy | 2 | 939 | 506 | 161 | 13 |
2,055 | E | 2055E | E. Haystacks | 2,800 | brute force; constructive algorithms; data structures; greedy; sortings | On the next new moon, the universe will reset, beginning with Florida. It's up to Florida Man to stop it, but he first needs to find an important item.There are \(n\) haystacks labelled from \(1\) to \(n\), where haystack \(i\) contains \(a_i\) haybales. One of the haystacks has a needle hidden beneath it, but you do n... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2\le n\le 5\cdot 10^5\)) β the number of haystacks.The \(i\)-th of the next \(n\) lines co... | For each test case, print a single integer β the minimum number of moves required to ensure that each haystack is emptied at least once. If it is not possible to empty each haystack at least once, output -1. | In the first test case, we can do the following sequence of moves: Move \(3\) haybales from haystack \(1\) to haystack \(2\). Haystack \(1\) is now emptied, and is assigned a height limit of \(5\). Move \(5\) haybales from haystack \(2\) to haystack \(1\). Haystack \(2\) is now emptied, and is assigned a height limit o... | Input: 723 52 4210 11 1031 34 31 135 42 41 1062 13 35 41 51 61 853 21 21 11 36 525 107 12 | Output: 8 -1 8 9 14 15 19 | Master | 5 | 1,208 | 619 | 207 | 20 |
1,746 | C | 1746C | C. Permutation Operations | 1,300 | constructive algorithms; greedy; implementation; math | You are given a permutation \(a\) of size \(n\) and you should perform \(n\) operations on it. In the \(i\)-th operation, you can choose a non-empty suffix of \(a\) and increase all of its elements by \(i\). How can we perform the operations to minimize the number of inversions in the final array?Note that you can perf... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the size of the array.The second line contains \(n\) distinct integer... | For each test case, print \(n\) integers \(x_{1}, x_{2}, \ldots, x_{n}\) (\(1 \le x_{i} \le n\) for each \(1 \le i \le n\)) indicating that the \(i\)-th operation must be applied to the suffix starting at index \(x_{i}\). If there are multiple answers, print any of them. | In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index \(1\)). The final array \([11, 12, 13, 14]\) contains \(0\) inversions.In the second test case, \(a\) will be equal to \([2, 4, 3, 5, 6]\), \([2, 4, 3, 7, 8]\), \([2, 4, 6, ... | Input: 441 2 3 451 3 2 4 532 3 111 | Output: 1 1 1 1 1 4 3 2 1 1 3 3 1 | Easy | 4 | 717 | 497 | 271 | 17 |
1,011 | B | 1011B | B. Planning The Expedition | 1,200 | binary search; brute force; implementation | Natasha is planning an expedition to Mars for \(n\) people. One of the important tasks is to provide food for each participant.The warehouse has \(m\) daily food packages. Each package has some food type \(a_i\).Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(1 \le m \le 100\)) β the number of the expedition participants and the number of the daily food packages available.The second line contains sequence of integers \(a_1, a_2, \dots, a_m\) (\(1 \le a_i \le 100\)), where \(a_i\) is the type of \(i... | Print the single integer β the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0. | In the first example, Natasha can assign type \(1\) food to the first participant, the same type \(1\) to the second, type \(5\) to the third and type \(2\) to the fourth. In this case, the expedition can last for \(2\) days, since each participant can get two food packages of his food type (there will be used \(4\) pa... | Input: 4 101 5 2 1 1 1 2 5 7 2 | Output: 2 | Easy | 3 | 759 | 339 | 142 | 10 |
709 | B | 709B | B. Checkpoints | 1,500 | greedy; implementation; sortings | Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order... | The first line of the input contains two integers n and a (1 β€ n β€ 100 000, - 1 000 000 β€ a β€ 1 000 000) β the number of checkpoints and Vasya's starting position respectively.The second line contains n integers x1, x2, ..., xn ( - 1 000 000 β€ xi β€ 1 000 000) β coordinates of the checkpoints. | Print one integer β the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint. | In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.In the second sample it's enough to visit only one checkpo... | Input: 3 101 7 12 | Output: 7 | Medium | 3 | 490 | 293 | 105 | 7 |
1,244 | D | 1244D | D. Paint the Tree | 1,800 | brute force; constructive algorithms; dp; graphs; implementation; trees | You are given a tree consisting of \(n\) vertices. A tree is an undirected connected acyclic graph. Example of a tree. You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.You have to paint the vertices so that any path consisting of exactly three dist... | The first line contains one integer \(n\) \((3 \le n \le 100\,000)\) β the number of vertices.The second line contains a sequence of integers \(c_{1, 1}, c_{1, 2}, \dots, c_{1, n}\) \((1 \le c_{1, i} \le 10^{9})\), where \(c_{1, i}\) is the cost of painting the \(i\)-th vertex into the first color.The third line contai... | If there is no good painting, print \(-1\).Otherwise, print the minimum cost of a good painting in the first line. In the second line print \(n\) integers \(b_1, b_2, \dots, b_n\) \((1 \le b_i \le 3)\), where the \(i\)-th integer should denote the color of the \(i\)-th vertex. If there are multiple good paintings with ... | All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color \(1\), the second vertex β into color \(3\), and the third vertex β into color \(2\). The cost of this painting is \(3 + 2 + 1 = 6\). | Input: 3 3 2 3 4 3 2 3 1 3 1 2 2 3 | Output: 6 1 3 2 | Medium | 6 | 836 | 947 | 352 | 12 |
238 | B | 238B | B. Boring Partition | 1,800 | constructive algorithms | This problem is the most boring one you've ever seen. Given a sequence of integers a1, a2, ..., an and a non-negative integer h, our goal is to partition the sequence into two subsequences (not necessarily consist of continuous elements). Each element of the original sequence should be contained in exactly one of the r... | The first line of input contains integers n and h (2 β€ n β€ 105, 0 β€ h β€ 108). In the second line there is a list of n space-separated integers representing a1, a2, ..., an (0 β€ ai β€ 108). | The first line of output should contain the required minimum goodness. The second line describes the optimal partition. You should print n whitespace-separated integers in the second line. The i-th integer is 1 if ai is in the first subsequence otherwise it should be 2.If there are several possible correct answers you ... | In the first sample the values of f are as follows: f(1, 2) = 1 + 2 + 2 = 5, f(1, 3) = 1 + 3 + 2 = 6 and f(2, 3) = 2 + 3 = 5. So the difference between maximum and minimum values of f is 1.In the second sample the value of h is large, so it's better for one of the sub-sequences to be empty. | Input: 3 21 2 3 | Output: 11 2 2 | Medium | 1 | 953 | 187 | 353 | 2 |
1,361 | D | 1361D | D. Johnny and James | 2,900 | greedy; implementation; math; trees | James Bond, Johnny's favorite secret agent, has a new mission. There are \(n\) enemy bases, each of them is described by its coordinates so that we can think about them as points in the Cartesian plane. The bases can communicate with each other, sending a signal, which is the ray directed from the chosen point to the o... | The first line contains two integers \(n\) and \(k\) \((2 \leq k \leq n \leq 5 \cdot 10^5)\) β the total number of bases and number of bases that have to remain, respectively.Each of the next \(n\) lines contains two integers \(x\) and \(y\) \((-10^9 \leq x, y \leq 10^9)\), \(i\)-th line contains coordinates of the \(i... | You should output one number β the maximal possible sum of distances between all pairs of some \(k\) from given bases. Your answer will be accepted if the absolute or relative error is less than \(10^{-6}\). | In the first example, in an optimal solution Bond doesn't destroy bases with indices \(4\) and \(6\) (marked in orange): The following picture represents an optimal solution for the second example. These bases are are not destroyed: \(2\), \(3\), \(4\), \(5\), \(6\) (marked in orange). An optimal solution for the third... | Input: 6 2 0 0 1 1 2 2 3 3 0 1 0 2 | Output: 6.24264069 | Master | 4 | 1,358 | 410 | 207 | 13 |
1,205 | E | 1205E | E. Expected Value Again | 3,100 | combinatorics; strings | You are given integers \(n\), \(k\). Let's consider the alphabet consisting of \(k\) different elements.Let beauty \(f(s)\) of the string \(s\) be the number of indexes \(i\), \(1\le i<|s|\), for which prefix of \(s\) of length \(i\) equals to suffix of \(s\) of length \(i\). For example, beauty of the string \(abacaba... | The first and the only line contains two integers \(n\), \(k\) (\(1\le n \le 10^5\), \(1\le k\le 10^9\)) β the length of a string and the size of alphabet respectively. | Output a single integer β \(P\times Q^{-1} \bmod 10^9 + 7\). | In the first example, there are \(9\) words of length \(2\) in alphabet of size \(3\) β \(aa\), \(ab\), \(ac\), \(ba\), \(bb\), \(bc\), \(ca\), \(cb\), \(cc\). \(3\) of them have beauty \(1\) and \(6\) of them have beauty \(0\), so the average value is \(\frac{1}{3}\).In the third example, there is only one such word, ... | Input: 2 3 | Output: 333333336 | Master | 2 | 707 | 168 | 60 | 12 |
1,852 | E | 1852E | E. Rivalries | 3,400 | constructive algorithms; data structures; greedy | Ntarsis has an array \(a\) of length \(n\).The power of a subarray \(a_l \dots a_r\) (\(1 \leq l \leq r \leq n\)) is defined as: The largest value \(x\) such that \(a_l \dots a_r\) contains \(x\) and neither \(a_1 \dots a_{l-1}\) nor \(a_{r+1} \dots a_n\) contains \(x\). If no such \(x\) exists, the power is \(0\). Cal... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case has a single integer \(n\) (\(1 \leq n \leq 10^5\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq ... | For each test case, output \(n\) integers \(b_1, b_2, \ldots, b_n\) β a valid rival to \(a\) such that \(b_1 + b_2 + \cdots + b_n\) is maximal. If there exist multiple rivals with the maximum sum, output any of them. | For the first test case, one rival with the maximal sum is \([2, 4, 2, 3, 3]\).\([2, 4, 2, 3, 3]\) can be shown to be a rival to \([1, 4, 1, 3, 3]\).All possible subarrays of \(a\) and \(b\) and their corresponding powers are listed below: The power of \(a[1:1] = [1] = 0\), the power of \(b[1:1] = [2] = 0\). The power ... | Input: 751 4 1 3 351 4 1 8 852 1 1 1 283 2 3 5 2 2 5 381 1 1 1 4 3 3 3101 9 5 9 8 1 5 8 9 1161 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7 | Output: 2 4 2 3 3 3 4 3 8 8 2 1 2 1 2 4 2 4 5 5 2 5 4 1 2 2 1 4 3 2 3 7 9 5 9 8 9 5 8 9 7 1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7 | Master | 3 | 737 | 431 | 216 | 18 |
50 | C | 50C | C. Happy Farm 5 | 2,000 | geometry | The Happy Farm 5 creators decided to invent the mechanism of cow grazing. The cows in the game are very slow and they move very slowly, it can even be considered that they stand still. However, carnivores should always be chased off them. For that a young player Vasya decided to make the shepherd run round the cows alo... | The first line contains an integer N which represents the number of cows in the herd (1 β€ N β€ 105). Each of the next N lines contains two integers Xi and Yi which represent the coordinates of one cow of (|Xi|, |Yi| β€ 106). Several cows can stand on one point. | Print the single number β the minimum number of moves in the sought path. | Picture for the example test: The coordinate grid is painted grey, the coordinates axes are painted black, the cows are painted red and the sought route is painted green. | Input: 41 15 15 31 3 | Output: 16 | Hard | 1 | 1,357 | 259 | 73 | 0 |
1,794 | D | 1794D | D. Counting Factorizations | 1,900 | combinatorics; divide and conquer; dp; math; number theory | The prime factorization of a positive integer \(m\) is the unique way to write it as \(\displaystyle m=p_1^{e_1}\cdot p_2^{e_2}\cdot \ldots \cdot p_k^{e_k}\), where \(p_1, p_2, \ldots, p_k\) are prime numbers, \(p_1 < p_2 < \ldots < p_k\) and \(e_1, e_2, \ldots, e_k\) are positive integers. For each positive integer \(... | The first line contains one integer \(n\) (\(1\le n \le 2022\)).The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(1\le a_i\le 10^6\)) β the given list. | Print one integer, the number of positive integers \(m\) satisfying \(f(m)=\{a_1, a_2, \ldots, a_{2n}\}\) modulo \(998\,244\,353\). | In the first sample, the two values of \(m\) such that \(f(m)=\{1,2,3,3\}\) are \(m=24\) and \(m=54\). Their prime factorizations are \(24=2^3\cdot 3^1\) and \(54=2^1\cdot 3^3\).In the second sample, the five values of \(m\) such that \(f(m)=\{2,2,3,5\}\) are \(200, 225, 288, 500\) and \(972\).In the third sample, ther... | Input: 2 1 3 2 3 | Output: 2 | Hard | 5 | 765 | 174 | 131 | 17 |
446 | D | 446D | D. DZY Loves Games | 2,800 | math; matrices; probabilities | Today DZY begins to play an old game. In this game, he is in a big maze with n rooms connected by m corridors (each corridor allows to move in both directions). You can assume that all the rooms are connected with corridors directly or indirectly.DZY has got lost in the maze. Currently he is in the first room and has k... | The first line contains three integers n, m, k (2 β€ n β€ 500; 1 β€ m β€ 105; 2 β€ k β€ 109).The second line contains n integers, each of them is either 0 or 1. If the i-th number is 1, then the i-th room has a trap, otherwise it has not a trap. Please note, that the number of rooms with a trap is no more than 101. It is gua... | Print the only real number β the probability for DZY to open the bonus round. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. | Input: 5 5 30 0 1 0 11 22 33 44 51 2 | Output: 0.25000000 | Master | 3 | 974 | 596 | 172 | 4 | |
1,851 | B | 1851B | B. Parity Sort | 800 | greedy; sortings; two pointers | You have an array of integers \(a\) of length \(n\). You can apply the following operation to the given array: Swap two elements \(a_i\) and \(a_j\) such that \(i \neq j\), \(a_i\) and \(a_j\) are either both even or both odd. Determine whether it is possible to sort the array in non-decreasing order by performing the ... | The first line of input data 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 one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains exact... | For each test case, output on a separate line: YES if the array can be sorted by applying the operation to it some number of times; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). | The first test case is explained in the problem statement. | Input: 657 10 1 3 2411 9 3 5511 3 15 3 2610 7 8 1 2 311056 6 4 1 6 | Output: YES YES NO NO YES NO | Beginner | 3 | 777 | 519 | 269 | 18 |
538 | D | 538D | D. Weird Chess | 1,800 | brute force; constructive algorithms; implementation | Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.Igor's chessboard is a square of size n Γ n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces.... | The first line contains a single integer n (1 β€ n β€ 50).The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o β in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other... | If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) Γ (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the in... | In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | Input: 5oxxxxx...xx...xx...xxxxxo | Output: YES....x........x........x........x....xxxxoxxxx....x........x........x........x.... | Medium | 3 | 1,654 | 506 | 514 | 5 |
991 | B | 991B | B. Getting an A | 900 | greedy; sortings | Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.The term is coming to an end and students start thinking about their grades. Today, a professor told his students that t... | The first line contains a single integer \(n\) β the number of Vasya's grades (\(1 \leq n \leq 100\)).The second line contains \(n\) integers from \(2\) to \(5\) β Vasya's grades for his lab works. | Output a single integer β the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a \(5\). | In the first sample, it is enough to redo two lab works to make two \(4\)s into \(5\)s.In the second sample, Vasya's average is already \(4.75\) so he doesn't have to redo anything to get a \(5\).In the second sample Vasya has to redo one lab work to get rid of one of the \(3\)s, that will make the average exactly \(4.... | Input: 34 4 4 | Output: 2 | Beginner | 2 | 1,183 | 197 | 157 | 9 |
1,944 | B | 1944B | B. Equal XOR | 1,100 | bitmasks; constructive algorithms | You are given an array \(a\) of length \(2n\), consisting of each integer from \(1\) to \(n\) exactly twice.You are also given an integer \(k\) (\(1 \leq k \leq \lfloor \frac{n}{2} \rfloor \)).You need to find two arrays \(l\) and \(r\) each of length \(\mathbf{2k}\) such that: \(l\) is a subset\(^\dagger\) of \([a_1, ... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains \(2\) integers \(n\) and \(k\) (\(2 \le n \le 5 \cdot 10^4\), \(1 \leq k \leq \lfloor \frac{n}{2... | For each test case, output two lines. On the first line of output, output \(2k\) integers \(l_1, l_2, \ldots, l_{2k}\).On the second line of output, output \(2k\) integers \(r_1, r_2, \ldots r_{2k}\).If there are multiple solutions, you may output any one of them. | In the first test case, we choose \(l=[2,1]\) and \(r=[2,1]\). \([2, 1]\) is a subset of \([a_1, a_2]\) and \([2, 1]\) is a subset of \([a_3, a_4]\), and \(2 \oplus 1 = 2 \oplus 1 = 3\).In the second test case, \(6 \oplus 4 = 1 \oplus 3 = 2\). | Input: 42 11 2 2 16 16 4 2 1 2 3 1 6 3 5 5 44 11 2 3 4 1 2 3 46 25 1 3 3 5 1 2 6 4 6 4 2 | Output: 2 1 2 1 6 4 1 3 1 2 1 2 5 1 3 3 6 4 2 4 | Easy | 2 | 1,082 | 605 | 264 | 19 |
584 | A | 584A | A. Olesya and Rodion | 1,000 | math | Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.Your task is: given the n and t print an integer strictly larger than zero consisting of n digits that is divisible by t. If such number doesn't exist, print - 1. | The single line contains two numbers, n and t (1 β€ n β€ 100, 2 β€ t β€ 10) β the length of the number and the number it should be divisible by. | Print one such positive number without leading zeroes, β the answer to the problem, or - 1, if such number doesn't exist. If there are multiple possible answers, you are allowed to print any of them. | Input: 3 2 | Output: 712 | Beginner | 1 | 307 | 140 | 199 | 5 | |
439 | A | 439A | A. Devu, the Singer and Churu, the Joker | 900 | greedy; implementation | Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to ""All World Classical Singing Festival"". Other than Devu, comedian Churu was also invited.Devu has provided organizers a list of the songs and required time for singing them. He will sing n songs, ith song wi... | The first line contains two space separated integers n, d (1 β€ n β€ 100; 1 β€ d β€ 10000). The second line contains n space-separated integers: t1, t2, ..., tn (1 β€ ti β€ 100). | If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event. | Consider the first example. The duration of the event is 30 minutes. There could be maximum 5 jokes in the following way: First Churu cracks a joke in 5 minutes. Then Devu performs the first song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now Devu performs second song for 2 minutes. Then Churu cracks 2 jok... | Input: 3 302 2 1 | Output: 5 | Beginner | 2 | 1,100 | 172 | 149 | 4 |
144 | A | 144A | A. Arrival of the General | 800 | implementation | A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their hei... | The first input line contains the only integer n (2 β€ n β€ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 β€ ai β€ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. T... | Print the only integer β the minimum number of seconds the colonel will need to form a line-up the general will like. | In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 5... | Input: 433 44 11 22 | Output: 2 | Beginner | 1 | 1,127 | 406 | 117 | 1 |
1,078 | E | 1078E | E. Negative Time Summation | 3,400 | constructive algorithms | Everyone knows that computers become faster and faster. Recently Berland scientists have built a machine that can move itself back in time!More specifically, it works as follows. It has an infinite grid and a robot which stands on one of the cells. Each cell of the grid can either be empty or contain 0 or 1. The machin... | The first line contains the only integer \(t\) (\(1\le t\le 1000\)) standing for the number of testcases. Each of the next \(t\) lines consists of two positive integers \(a\) and \(b\) (\(1\le a, b < 2^{30}\)) in decimal. | Output the only line consisting of no more than \(10^5\) symbols from 01eslrudt standing for your program.Note that formally you may output different programs for different tests. | Input: 2123456789 987654321555555555 555555555 | Output: 0l1l1l0l0l0l1l1l1l0l1l0l1l1l0l0l0l1l0l1l1l1l0l0l0l1l0l0l0l0l1l0lr | Master | 1 | 3,595 | 221 | 179 | 10 | |
1,550 | C | 1550C | C. Manhattan Subarrays | 1,700 | brute force; geometry; greedy; implementation | Suppose you have two points \(p = (x_p, y_p)\) and \(q = (x_q, y_q)\). Let's denote the Manhattan distance between them as \(d(p, q) = |x_p - x_q| + |y_p - y_q|\).Let's say that three points \(p\), \(q\), \(r\) form a bad triple if \(d(p, r) = d(p, q) + d(q, r)\).Let's say that an array \(b_1, b_2, \dots, b_m\) is good... | The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).I... | For each test case, print the number of good subarrays of array \(a\). | In the first test case, it can be proven that any subarray of \(a\) is good. For example, subarray \([a_2, a_3, a_4]\) is good since it contains only three elements and: \(d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3\) \(<\) \(d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7\); \(d((a_2, 2), (a_3, 3))... | Input: 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 | Output: 10 12 3 | Medium | 4 | 767 | 389 | 70 | 15 |
1,088 | C | 1088C | C. Ehab and a 2-operation task | 1,400 | constructive algorithms; greedy; math | You're given an array \(a\) of length \(n\). You can perform the following operations on it: choose an index \(i\) \((1 \le i \le n)\), an integer \(x\) \((0 \le x \le 10^6)\), and replace \(a_j\) with \(a_j+x\) for all \((1 \le j \le i)\), which means add \(x\) to all the elements in the prefix ending at \(i\). choose... | The first line contains an integer \(n\) \((1 \le n \le 2000)\), the number of elements in the array \(a\).The second line contains \(n\) space-separated integers \(a_1\), \(a_2\), \(\dots\), \(a_n\) \((0 \le a_i \le 10^5)\), the elements of the array \(a\). | On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations.To print an adding operation, use the format ""\(1\) \(i\) \(x\)""; to print a modding operation, use the format ""\(2\) \(i\) \(x\)"". If \(i\) or \(x\) don't satisfy the limitations above, or you ... | In the first sample, the array is already increasing so we don't need any operations.In the second sample:In the first step: the array becomes \([8,6,3]\).In the second step: the array becomes \([0,2,3]\). | Input: 31 2 3 | Output: 0 | Easy | 3 | 654 | 258 | 386 | 10 |
2,038 | I | 2038I | I. Polyathlon | 2,500 | binary search; data structures; hashing; string suffix structures; strings | Berland is this year's host country of the International Collegiate Polyathlon Competition! Similar to biathlon being a competition of two sports, polyathlon is a competition of many sports. This year, there are \(m\) sports. Also, there are \(n\) participants in the event. The sports are numbered from \(1\) to \(m\), ... | The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 10^6\); \(n \le 2^m\); \(nm \le 2 \cdot 10^6\)) β the number of participants and the number of sports, respectively.The \(i\)-th of the next \(n\) lines contains a binary string consisting of exactly \(m\) characters 0 or 1 β the skillset of the \(i... | Print \(m\) integers. For each \(x\) from \(1\) to \(m\), print the index of the winner if the competition starts with the sport \(x\). | Input: 3 5100100110010101 | Output: 3 2 3 1 3 | Expert | 5 | 1,848 | 737 | 135 | 20 | |
1,165 | E | 1165E | E. Two Arrays and Sum of Functions | 1,600 | greedy; math; sortings | You are given two arrays \(a\) and \(b\), both of length \(n\).Let's define a function \(f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i\).Your task is to reorder the elements (choose an arbitrary order of elements) of the array \(b\) to minimize the value of \(\sum\limits_{1 \le l \le r \le n} f(l, r)\). Since the... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\) and \(b\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains \(... | Print one integer β the minimum possible value of \(\sum\limits_{1 \le l \le r \le n} f(l, r)\) after rearranging elements of \(b\), taken modulo \(998244353\). Note that you should minimize the answer but not its remainder. | Input: 5 1 8 7 2 4 9 7 2 9 3 | Output: 646 | Medium | 3 | 453 | 431 | 224 | 11 | |
2,124 | A | 2124A | A. Deranged Deletions | 800 | greedy; sortings | Call an array \(b\) of length \(m\) a derangement if the following property holds: Let \(c\) be an array of length \(m\) such that \(c_i = b_i\) over all \(1 \leq i \leq m\). Sort \(c\) in non-decreasing order. If \(b_i\neq c_i\) over all \(1\leq i \leq m\), then \(b\) is a derangement. For example, If \(b = [4,8,3,1]\... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)) β the length of array \(a\).The second line of each test case contains \(n\)... | For each test case, on a new line, if it is possible to perform operations such that the remaining array is a derangement, output YES. Otherwise, output NO.You can output in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.If your respon... | In the second test case, we can delete one \(5\) from the array so that it becomes \([4,5,2,4]\). It can be shown this array is a derangement. This is not the only solution β it can be shown that the original array \([4,5,5,2,4]\) is another valid solution. | Input: 332 2 354 5 5 2 411 | Output: NO YES 4 4 5 2 4 NO | Beginner | 2 | 941 | 407 | 719 | 21 |
2,005 | D | 2005D | D. Alter the GCD | 2,400 | binary search; brute force; data structures; divide and conquer; implementation; number theory | You are given two arrays \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).You must perform the following operation exactly once: choose any indices \(l\) and \(r\) such that \(1 \le l \le r \le n\); swap \(a_i\) and \(b_i\) for all \(i\) such that \(l \leq i \leq r\). Find the maximum possible value of \(\text{g... | In the first line of the input, you are given a single integer \(t\) (\(1 \le t \le 10^5\)), the number of test cases. Then the description of each test case follows.In the first line of each test case, you are given a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), representing the number of integers in each arra... | For each test case, output a line with two integers: the maximum value of \(\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)\) after performing the operation exactly once, and the number of ways. | In the first, third, and fourth test cases, there's no way to achieve a higher GCD than \(1\) in any of the arrays, so the answer is \(1 + 1 = 2\). Any pair \((l, r)\) achieves the same result; for example, in the first test case there are \(36\) such pairs.In the last test case, you must choose \(l = 1\), \(r = 2\) to... | Input: 5811 4 16 17 3 24 25 88 10 4 21 17 18 25 2146 4 24 1315 3 1 14213 145 8820 17 15 11 21 10 3 79 9 4 20 14 9 13 1218 1315 20 | Output: 2 36 3 2 2 3 2 36 6 1 | Expert | 6 | 512 | 666 | 216 | 20 |
627 | B | 627B | B. Factory Repairs | 1,700 | data structures | A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but ... | The first line contains five integers n, k, a, b, and q (1 β€ k β€ n β€ 200 000, 1 β€ b < a β€ 10 000, 1 β€ q β€ 200 000) β the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.The next q lines contain the descriptions of the queries. Each query is of ... | For each query of the second type, print a line containing a single integer β the maximum number of orders that the factory can fill over all n days. | Consider the first sample.We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that ... | Input: 5 2 2 1 81 1 21 5 31 2 12 21 4 21 3 22 12 3 | Output: 364 | Medium | 1 | 920 | 676 | 149 | 6 |
575 | F | 575F | F. Bulbo | 2,100 | dp; greedy | Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.In Bananistan people play this crazy game β Bulbo. Thereβs an array of bulbs and player at the position, which represents one of the bulbs. The distance between t... | The first line contains number of turns n and initial position x. Next n lines contain two numbers lstart and lend, which represent that all bulbs from interval [lstart, lend] are shining this turn. 1 β€ n β€ 5000 1 β€ x β€ 109 1 β€ lstart β€ lend β€ 109 | Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game. | Before 1. turn move to position 5Before 2. turn move to position 9Before 5. turn move to position 8 | Input: 5 42 79 168 109 171 6 | Output: 8 | Hard | 2 | 973 | 247 | 136 | 5 |
2,113 | B | 2113B | B. Good Start | 1,200 | constructive algorithms; math | The roof is a rectangle of size \(w \times h\) with the bottom left corner at the point \((0, 0)\) on the plane. Your team needs to completely cover this roof with identical roofing sheets of size \(a \times b\), with the following conditions: The sheets cannot be rotated (not even by \(90^\circ\)). The sheets must not... | 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 four integers \(w\), \(h\), \(a\), and \(b\) (\(1 \le w, h, a, b \le 10^9\)) β the dimensions of the roof and the di... | For each test case, output ""Yes"" (without quotes) if it is possible to completely tile the roof without removing either of the two already placed tiles, and ""No"" (without quotes) otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be... | In the first test case, it is possible to add \(8\) roofing sheets as follows: In the second test case, it is impossible to completely tile the roof: | Input: 76 5 2 3-1 -2 5 44 4 2 20 0 3 110 9 3 20 0 4 310 9 3 20 0 6 35 5 2 2-1 -1 4 -15 5 2 2-1 -1 2 37 8 2 40 0 0 5 | Output: Yes No No Yes No Yes No | Easy | 2 | 726 | 661 | 354 | 21 |
1,131 | B | 1131B | B. Draw! | 1,400 | greedy; implementation | You still have partial information about the score during the historic football match. You are given a set of pairs \((a_i, b_i)\), indicating that at some point during the match the score was ""\(a_i\): \(b_i\)"". It is known that if the current score is Β«\(x\):\(y\)Β», then after the goal it will change to ""\(x+1\):\... | The first line contains a single integer \(n\) (\(1 \le n \le 10000\)) β the number of known moments in the match.Each of the next \(n\) lines contains integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)), denoting the score of the match at that moment (that is, the number of goals by the first team and the numbe... | Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted. | In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4. | Input: 32 03 13 4 | Output: 2 | Easy | 2 | 609 | 509 | 148 | 11 |
461 | E | 461E | E. Appleman and a Game | 3,000 | binary search; shortest paths; strings | Appleman and Toastman like games. Today they play a game with strings with the following rules. Firstly Toastman tells Appleman two strings s and t both consisting only of letters 'A', 'B', 'C', 'D'. Then Appleman must build string s as quickly as possible. Initially he has empty string, and in one second he can append... | The first line contains an integer n (1 β€ n β€ 1018). The second line contains string t (1 β€ |t| β€ 105). String t consists of only letters 'A', 'B', 'C', 'D'. Each letter appears at least once in string t. | Print a single integer β the largest possible time Appleman needs. | In the first example, Toastman can choose s equal to ""AAAAA"".In the second example, Toastman can choose s equal to ""DADDA"". | Input: 5ABCCAD | Output: 5 | Master | 3 | 962 | 204 | 66 | 4 |
1,771 | F | 1771F | F. Hossam and Range Minimum Query | 2,500 | binary search; bitmasks; data structures; hashing; probabilities; strings; trees | Hossam gives you a sequence of integers \(a_1, \, a_2, \, \dots, \, a_n\) of length \(n\). Moreover, he will give you \(q\) queries of type \((l, \, r)\). For each query, consider the elements \(a_l, \, a_{l + 1}, \, \dots, \, a_r\). Hossam wants to know the smallest number in this sequence, such that it occurs in this... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), the length of the sequence.The second line contains \(n\) integers \(a_1, \, a_2, \, \dots, \, a_n\) (\(1 \le a_i \le 10^9\)).The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)), the number of queries.Each of the... | For each query, print the smallest number that occurs an odd number of times on the given segment of the sequence.If there is no such number, print \(0\). | In the example,$$$\(l_1 = 1, \, r_1 = 2,\)\( \)\(l_2 = 1, \, r_2 = 3,\)\( \)\(l_3 = 2, \, r_3 = 4,\)\( \)\(l_4 = 1, \, r_4 = 4,\)\( \)\(l_5 = 2, \, r_5 = 2,\)\( \)\(l_6 = 1, \, r_6 = 5.\)$$$ | Input: 5 1 2 1 2 2 6 1 2 0 2 0 6 0 5 2 2 3 7 | Output: 1 2 1 0 2 2 | Expert | 7 | 429 | 809 | 154 | 17 |
2,044 | G2 | 2044G2 | G2. Medium Demon Problem (hard version) | 1,900 | dfs and similar; dp; dsu; graphs; implementation; trees | This is the hard version of the problem. The key difference between the two versions is highlighted in bold.A group of \(n\) spiders has come together to exchange plushies. Initially, each spider has \(1\) plushie. Every year, if spider \(i\) has at least one plushie, he will give exactly one plushie to spider \(r_i\).... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of spiders.The following line contains \(n\) integers \(r_1, r_2, \ldots, r_n\) (\(1 \leq r_i \leq n, r_i \neq i\)) β the ... | For each test case, output an integer on a new line, the first year in which the process becomes stable. | For the second test case: At year \(1\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Then, year \(1\)'s exchange happens. At year \(2\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Since this array is the same as the previous year, th... | Input: 522 152 3 4 5 152 1 4 2 354 1 1 5 4104 3 9 1 6 7 9 10 10 3 | Output: 2 2 5 5 5 | Hard | 6 | 788 | 452 | 104 | 20 |
1,835 | B | 1835B | B. Lottery | 2,500 | binary search; brute force; greedy; math; two pointers | \(n\) people indexed with integers from \(1\) to \(n\) came to take part in a lottery. Each received a ticket with an integer from \(0\) to \(m\).In a lottery, one integer called target is drawn uniformly from \(0\) to \(m\). \(k\) tickets (or less, if there are not enough participants) with the closest numbers to the ... | In the first line of the input, there are the integers \(n\), \(m\), and \(k\) (\(1 \leq n \leq 10^6\), \(0 \leq m \leq 10^{18}\), \(1 \leq k \leq 10^6\)).In the following line, there are \(n\) integers separated by a single space, denoting the numbers on tickets received by people participating in a lottery. These num... | You should output two integers separated by a single space on the standard output. The first should be equal to the number of target values (from \(0\) to \(m\)), upon drawing which Baytek wins, given that he chooses his ticket optimally. The second should be equal to the integer Bytek should pick to maximize his chanc... | In the first example, Bytek wins for \(4\) target values (namely \(0, 1, 2, 3\)) if he chooses integer \(2\), which is the lowest optimal value. If he chooses \(3\), he also wins in four cases, but it is not the lowest value. | Input: 3 6 2 1 4 5 | Output: 4 2 | Expert | 5 | 949 | 371 | 345 | 18 |
1,497 | B | 1497B | B. M-arrays | 1,200 | constructive algorithms; greedy; math | You are given an array \(a_1, a_2, \ldots, a_n\) consisting of \(n\) positive integers and a positive integer \(m\).You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.Let's call an array \(m\)-divisible if for each two adjacent numbers in the array (two n... | The first line contains a single integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The first line of each test case contains two integers \(n\), \(m\) \((1 \le n \le 10^5, 1 \le m \le 10^5)\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((1 \le a_i \le 10^9)\).It is... | For each test case print the answer to the problem. | In the first test case we can divide the elements as follows: \([4, 8]\). It is a \(4\)-divisible array because \(4+8\) is divisible by \(4\). \([2, 6, 2]\). It is a \(4\)-divisible array because \(2+6\) and \(6+2\) are divisible by \(4\). \([9]\). It is a \(4\)-divisible array because it consists of one element. | Input: 4 6 4 2 2 8 6 9 4 10 8 1 1 1 5 2 4 4 8 6 7 1 1 666 2 2 2 4 | Output: 3 6 1 1 | Easy | 3 | 583 | 418 | 51 | 14 |
1,252 | E | 1252E | E. Songwriter | 2,200 | greedy; two pointers | Andi is a mathematician, a computer scientist, and a songwriter. After spending so much time writing songs, he finally writes a catchy melody that he thought as his best creation. However, the singer who will sing the song/melody has a unique vocal range, thus, an adjustment may be needed.A melody is defined as a seque... | Input begins with a line containing four integers: \(N\) \(L\) \(R\) \(K\) (\(1 \le N \le 100\,000\); \(1 \le L \le R \le 10^9\); \(1 \le K \le 10^9\)) representing the number of notes in the melody, the vocal range (\(L\) and \(R\)), and the maximum difference between two successive notes in the new melody, respective... | Output in a line \(N\) integers (each separated by a single space) representing the lexicographically smallest melody satisfying all the requirements, or output -1 if there is no melody satisfying all the requirements. Note that it might be possible that the lexicographically smallest melody which satisfies all the req... | Explanation for the sample input/output #1This is the example from the problem description. | Input: 16 1 8 6 1 3 5 6 7 8 9 10 3 7 8 9 10 11 12 12 | Output: 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 8 | Hard | 2 | 1,725 | 429 | 368 | 12 |
630 | A | 630A | A. Again Twenty Five! | 800 | number theory | The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. ""Do I give such a hard task?"" β the HR manager thought. ""Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power usi... | The only line of the input contains a single integer n (2 β€ n β€ 2Β·1018) β the power in which you need to raise number 5. | Output the last two digits of 5n without spaces between them. | Input: 2 | Output: 25 | Beginner | 1 | 484 | 120 | 61 | 6 | |
1,220 | G | 1220G | G. Geolocation | 3,400 | geometry | You are working for the Gryzzl company, headquartered in Pawnee, Indiana.The new national park has been opened near Pawnee recently and you are to implement a geolocation system, so people won't get lost. The concept you developed is innovative and minimalistic. There will be \(n\) antennas located somewhere in the par... | The first line of input contains a single integer \(n\) (\(2 \leq n \leq 10^5\)) which is the number of antennas.The following \(n\) lines contain coordinates of antennas, \(i\)-th line contain two integers \(x_i\) and \(y_i\) (\(0 \leq x_i,y_i \leq 10^8\)). It is guaranteed that no two antennas coincide.The next line ... | For each query output \(k\), the number of possible a user's locations matching the given input and then output the list of these locations in lexicographic order.It is guaranteed that the sum of all \(k\) over all points does not exceed \(10^6\). | As you see in the second example, although initially a user's location is picked to have non-negative coordinates, you have to output all possible integer locations. | Input: 3 0 0 0 1 1 0 1 1 1 2 | Output: 1 1 1 | Master | 1 | 870 | 899 | 247 | 12 |
338 | D | 338D | D. GCD Table | 2,900 | chinese remainder theorem; math; number theory | Consider a table G of size n Γ m such that G(i, j) = GCD(i, j) for all 1 β€ i β€ n, 1 β€ j β€ m. GCD(a, b) is the greatest common divisor of numbers a and b.You have a sequence of positive integer numbers a1, a2, ..., ak. We say that this sequence occurs in table G if it coincides with consecutive elements in some row, sta... | The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 1012; 1 β€ k β€ 10000). The second line contains k space-separated integers a1, a2, ..., ak (1 β€ ai β€ 1012). | Print a single word ""YES"", if the given sequence occurs in table G, otherwise print ""NO"". | Sample 1. The tenth row of table G starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence a.Sample 2. This time the width of table G equals 8. Sequence a doesn't occur there. | Input: 100 100 55 2 1 2 1 | Output: YES | Master | 3 | 509 | 184 | 93 | 3 |
1,857 | A | 1857A | A. Array Coloring | 800 | greedy; math | You are given an array consisting of \(n\) integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.For example, if the array is [\(1,2,4,3,2,3,5,4\)]... | The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case begins with a line containing an integer \(n\) (\(2 \le n \le 50\)) β the length of the array \(a\).The next line contains \(n\) integers \(a_1,a_2, \dots, a_n\) (\(1 \le a_i \le 50\)) β the elements of the array \... | For each test case, output ""YES"" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and ""NO"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, the str... | The first sample is described in the statement.In the second sample, there are only two colorings \([\color{blue}{4},\color{red}{7}]\) and \([\color{red}{4},\color{blue}{7}]\) , but in both cases the parity of sums is different.In the third sample, you can color \([\color{blue}{3},\color{blue}{9},\color{red}{8}]\) and ... | Input: 781 2 4 3 2 3 5 424 733 9 821 755 4 3 2 144 3 4 5250 48 | Output: YES NO YES YES NO YES YES | Beginner | 2 | 566 | 325 | 394 | 18 |
1,836 | B | 1836B | B. Astrophysicists | 1,100 | greedy; math | In many, many years, far, far away, there will be a launch of the first flight to Mars. To celebrate the success, \(n\) astrophysicists working on the project will be given bonuses of a total value of \(k\) gold coins.You have to distribute the money among the astrophysicists, and to make it easier, you have to assign ... | In the first line of input, there is one integer \(t\) (\(1 \leq t \leq 10^4\)) denoting the number of test cases.Each of the following \(t\) lines describes one test case and contains three integers \(n\), \(k\), \(g\) (\(1 \le n \le 10^9\), \(0 \le k \le 10^9\), \(2 \le g \le 10^9\)) β respectively the number of astr... | In a separate line for each test case, output a single integer β the maximum number of silver coins that could be saved due to rounding. | In the first test case, one of the optimal assignments could be the following: First person: \(x = 30\) silver coins: company pays \(0\), saves \(30\) silver coins, Second person: \(x = 140\) silver coins: company pays \(100\), saves \(40\) silver coins, Third person: \(x = 130\) silver coins: company pays \(100\), sav... | Input: 53 3 1002 1 1491 2 1336 16 673 8 22 | Output: 100 0 26 72 176 | Easy | 2 | 1,547 | 450 | 136 | 18 |
1,970 | E3 | 1970E3 | E3. Trails (Hard) | 2,200 | dp; matrices | Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long tra... | The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabi... | The number of possible combinations of trails, modulo \(10^9 + 7\). | Input: 3 21 0 10 1 1 | Output: 18 | Hard | 2 | 754 | 448 | 67 | 19 | |
1,921 | A | 1921A | A. Square | 800 | greedy; math | A square of positive (strictly greater than \(0\)) area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square. | Each test consists of several testcases. The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of testcases. The following is a description of the testcases.Each testcase contains four lines, each line contains two integers \(x_i, y_i\) (\(-1000\le x_i, y_i\le 1000\)), coordinates of the corners ... | For each test case, print a single integer, the area of the square. | Input: 31 24 51 54 2-1 11 -11 1-1 -145 1145 3917 1117 39 | Output: 9 4 784 | Beginner | 2 | 243 | 498 | 67 | 19 | |
1,940 | D | 1940D | 0 | *special; constructive algorithms; data structures; sortings | Beginner | 4 | 0 | 0 | 0 | 19 | ||||||
965 | C | 965C | C. Greedy Arkady | 2,000 | math | \(k\) people want to split \(n\) candies between them. Each candy should be given to exactly one of them or be thrown away.The people are numbered from \(1\) to \(k\), and Arkady is the first of them. To split the candies, Arkady will choose an integer \(x\) and then give the first \(x\) candies to himself, the next \(... | The only line contains four integers \(n\), \(k\), \(M\) and \(D\) (\(2 \le n \le 10^{18}\), \(2 \le k \le n\), \(1 \le M \le n\), \(1 \le D \le \min{(n, 1000)}\), \(M \cdot D \cdot k \ge n\)) β the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of t... | Print a single integer β the maximum possible number of candies Arkady can give to himself.Note that it is always possible to choose some valid \(x\). | In the first example Arkady should choose \(x = 4\). He will give \(4\) candies to himself, \(4\) candies to the second person, \(4\) candies to the third person, then \(4\) candies to the fourth person and then again \(4\) candies to himself. No person is given candies more than \(2\) times, and Arkady receives \(8\) ... | Input: 20 4 5 2 | Output: 8 | Hard | 1 | 809 | 354 | 150 | 9 |
2,103 | A | 2103A | A. Common Multiple | 800 | brute force; greedy; implementation; math | You are given an array of integers \(a_1, a_2, \ldots, a_n\). An array \(x_1, x_2, \ldots, x_m\) is beautiful if there exists an array \(y_1, y_2, \ldots, y_m\) such that the elements of \(y\) are distinct (in other words, \(y_i\neq y_j\) for all \(1 \le i < j \le m\)), and the product of \(x_i\) and \(y_i\) is the sam... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contai... | For each test case, output the maximum size of a subsequence of array \(a\) that is beautiful. | In the first test case, the entire array \(a = [1, 2, 3]\) is already beautiful. A possible array \(y\) is \([6, 3, 2]\), which is valid since the elements of \(y\) are distinct, and \(1\cdot 6 = 2\cdot 3 = 3\cdot 2\).In the second test case, the subsequence \([3, 1, 4, 5]\) is beautiful. A possible array \(y\) is \([2... | Input: 331 2 353 1 4 1 511 | Output: 3 4 1 | Beginner | 4 | 725 | 491 | 94 | 21 |
1,548 | E | 1548E | E. Gregor and the Two Painters | 3,400 | data structures; divide and conquer; graphs; greedy; math | Two painters, Amin and Benj, are repainting Gregor's living room ceiling! The ceiling can be modeled as an \(n \times m\) grid.For each \(i\) between \(1\) and \(n\), inclusive, painter Amin applies \(a_i\) layers of paint to the entire \(i\)-th row. For each \(j\) between \(1\) and \(m\), inclusive, painter Benj appli... | The first line contains three integers \(n\), \(m\) and \(x\) (\(1 \le n,m \le 2\cdot 10^5\), \(1 \le x \le 2\cdot 10^5\)) β the dimensions of Gregor's ceiling, and the maximum number of paint layers in a badly painted cell.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2\cdot 10^5\)... | Print a single integer, the number of badly painted regions. | The diagram below represents the first example. The numbers to the left of each row represent the list \(a\), and the numbers above each column represent the list \(b\). The numbers inside each cell represent the number of paint layers in that cell.The colored cells correspond to badly painted cells. The red and blue c... | Input: 3 4 11 9 8 5 10 6 7 2 | Output: 2 | Master | 5 | 903 | 528 | 60 | 15 |
300 | A | 300A | A. Array | 1,100 | brute force; constructive algorithms; implementation | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero ( < 0). The product of all numbers in the second set is greater than zero ( > 0). The product of all numbers in the t... | The first line of the input contains integer n (3 β€ n β€ 100). The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| β€ 103) β the array elements. | In the first line print integer n1 (n1 > 0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set.In the next line print integer n2 (n2 > 0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set.In the next line pr... | Input: 3-1 2 0 | Output: 1 -11 21 0 | Easy | 3 | 449 | 173 | 615 | 3 | |
862 | E | 862E | E. Mahmoud and Ehab and the function | 2,100 | binary search; data structures; sortings | Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 β€ j β€ m - n. Suppose, ci = ai - bi + j. Then f(j) = |c1 - c2 + c3 - c4... cn|. More formally, . Dr. Evil wants Mahmoud and... | The first line contains three integers n, m and q (1 β€ n β€ m β€ 105, 1 β€ q β€ 105) β number of elements in a, number of elements in b and number of queries, respectively.The second line contains n integers a1, a2, ..., an. ( - 109 β€ ai β€ 109) β elements of a.The third line contains m integers b1, b2, ..., bm. ( - 109 β€ b... | The first line should contain the minimum value of the function f before any update.Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update . | For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.After the second update a becom... | Input: 5 6 31 2 3 4 51 2 3 4 5 61 1 101 1 -91 5 -1 | Output: 0900 | Hard | 3 | 730 | 508 | 207 | 8 |
1,213 | D2 | 1213D2 | D2. Equalizing by Division (hard version) | 1,600 | brute force; math; sortings | The only difference between easy and hard versions is the number of elements in the array.You are given an array \(a\) consisting of \(n\) integers. In one move you can choose any \(a_i\) and divide it by \(2\) rounding down (in other words, in one move you can set \(a_i := \lfloor\frac{a_i}{2}\rfloor\)).You can perfor... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of elements in the array and the number of equal numbers required.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)... | Print one integer β the minimum possible number of operations required to obtain at least \(k\) equal numbers in the array. | Input: 5 3 1 2 2 4 5 | Output: 1 | Medium | 3 | 629 | 341 | 123 | 12 | |
331 | A1 | 331A1 | A1. Oh Sweet Beaverette | 1,400 | brute force; implementation | β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming wa... | The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints... | In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.... | Input: 51 2 3 1 2 | Output: 8 11 | Easy | 2 | 1,133 | 444 | 440 | 3 | |
1,624 | C | 1624C | C. Division by Two and Permutation | 1,100 | constructive algorithms; flows; graph matchings; greedy; math | You are given an array \(a\) consisting of \(n\) positive integers. You can perform operations on it.In one operation you can replace any element of the array \(a_i\) with \(\lfloor \frac{a_i}{2} \rfloor\), that is, by an integer part of dividing \(a_i\) by \(2\) (rounding down).See if you can apply the operation some ... | The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases.Each test case contains exactly two lines. The first one contains an integer \(n\) (\(1 \le n \le 50\)), the second one contains integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)). | For each test case, output on a separate line: YES if you can make the array \(a\) become a permutation of numbers from \(1\) to \(n\), NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). | The first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation. | Input: 641 8 25 221 199 8 3 4 2 7 1 5 638 2 1424 7 16 7522 6 22 4 22 | Output: YES NO YES NO NO YES | Easy | 5 | 950 | 292 | 275 | 16 |
1,013 | A | 1013A | A. Piles With Stones | 800 | math | There is a beautiful garden of stones in Innopolis.Its most beautiful place is the \(n\) piles with stones numbered from \(1\) to \(n\).EJOI participants have visited this place twice. When they first visited it, the number of stones in piles was \(x_1, x_2, \ldots, x_n\), correspondingly. One of the participants wrote... | The first line of the input file contains a single integer \(n\), the number of piles with stones in the garden (\(1 \leq n \leq 50\)).The second line contains \(n\) integers separated by spaces \(x_1, x_2, \ldots, x_n\), the number of stones in piles recorded in the notebook when the participants came to the place wit... | If the records can be consistent output ""Yes"", otherwise output ""No"" (quotes for clarity). | In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.In the second example, the jury took stones from the second and fourth piles.It can b... | Input: 51 2 3 4 52 1 4 3 5 | Output: Yes | Beginner | 1 | 1,004 | 615 | 94 | 10 |
1,290 | F | 1290F | F. Making Shapes | 3,500 | dp | You are given \(n\) pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin \((0, 0)\). Choose a vector and add the segment of the vector to the current point. For example, if your current point is at \((x, y)\) an... | The first line contains two integers \(n\) and \(m\) β the number of vectors and the size of the square (\(1 \leq n \leq 5\), \(1 \leq m \leq 10^9\)).Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) β the \(x\)-coordinate and \(y\)-coordinate of the \(i\)-th vector (\(|x_i|, |y_i| \leq 4\), \((x_i... | Output a single integer β the number of satisfiable shapes by modulo \(998244353\). | The shapes for the first sample are: The only shape for the second sample is: The only shape for the fourth sample is: | Input: 3 3 -1 0 1 1 0 -1 | Output: 3 | Master | 1 | 1,246 | 555 | 83 | 12 |
1,987 | D | 1987D | D. World is Mine | 1,800 | dp; games | Alice and Bob are playing a game. Initially, there are \(n\) cakes, with the \(i\)-th cake having a tastiness value of \(a_i\).Alice and Bob take turns eating them, with Alice starting first: In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of ... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)) β the number of cakes.The second line of eac... | For each test case, output a single integer β the number of cakes Alice will eat if both players play optimally. | In the first test case, one possible sequence of turns is: Alice eats a cake with a tastiness value of \(1\). The remaining cakes are \([4, 2, 3]\). Bob eats a cake with a tastiness value of \(2\). The remaining cakes are \([4, 3]\). Alice eats a cake with a tastiness of \(3\). The remaining cakes are \([4]\). Bob eats... | Input: 941 4 2 331 1 151 4 2 3 443 4 1 41184 3 2 5 6 8 3 476 1 1 3 5 3 1116 11 6 8 7 5 3 11 2 3 5172 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6 | Output: 2 1 3 2 1 3 2 4 4 | Medium | 2 | 721 | 524 | 112 | 19 |
1,492 | B | 1492B | B. Card Deck | 1,100 | data structures; greedy; math | You have a deck of \(n\) cards, and you'd like to reorder it to a new one.Each card has a value between \(1\) and \(n\) equal to \(p_i\). All \(p_i\) are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. \(p_1\) stands for the bottom card, \(p_n\) is the top card. In each step you pick some inte... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains the single integer \(n\) (\(1 \le n \le 10^5\)) β the size of deck you have.The second line contains \(n\) integers \(p_1, p_2,\dots, p_n\) (\(1 \le p_i \le n\); \(p_i \neq p_j\) if... | For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.If there are multiple answers, print any of them. | In the first test case, one of the optimal strategies is the next one: take \(1\) card from the top of \(p\) and move it to \(p'\): \(p\) becomes \([1, 2, 3]\), \(p'\) becomes \([4]\); take \(1\) card from the top of \(p\): \(p\) becomes \([1, 2]\), \(p'\) becomes \([4, 3]\); take \(1\) card from the top of \(p\): \(p\... | Input: 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 | Output: 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 | Easy | 3 | 761 | 465 | 165 | 14 |
1,700 | B | 1700B | B. Palindromic Numbers | 1,100 | constructive algorithms; implementation; math | During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome. Recall that a number is called a palindrome, if it reads the same right to left and left to right. For exampl... | The first line of input data contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. Next, descriptions of \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 100\,000\)) β the length of the number that is written on the ground.The second line ... | For each of \(t\) test cases print an answer β a positive \(n\)-digit integer without leading zeros, such that the sum of the input integer and this number is a palindrome.We can show that at least one number satisfying the constraints exists. If there are multiple solutions, you can output any of them. | In the first test case \(99 + 32 = 131\) is a palindrome. Note that another answer is \(12\), because \(99 + 12 = 111\) is also a palindrome.In the second test case \(1023 + 8646 = 9669\).In the third test case \(385 + 604 = 989\). | Input: 3 2 99 4 1023 3 385 | Output: 32 8646 604 | Easy | 3 | 480 | 507 | 304 | 17 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.