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,715 | E | 1715E | E. Long Way Home | 2,400 | data structures; divide and conquer; dp; geometry; graphs; greedy; shortest paths | Stanley lives in a country that consists of \(n\) cities (he lives in city \(1\)). There are bidirectional roads between some of the cities, and you know how long it takes to ride through each of them. Additionally, there is a flight between each pair of cities, the flight between cities \(u\) and \(v\) takes \((u - v)... | In the first line of input there are three integers \(n\), \(m\), and \(k\) (\(2 \leq n \leq 10^{5}\), \(1 \leq m \leq 10^{5}\), \(1 \leq k \leq 20\)) β the number of cities, the number of roads, and the maximal number of flights Stanley can take.The following \(m\) lines describe the roads. Each contains three integer... | Print \(n\) integers, \(i\)-th of which is equal to the minimum time of traveling to city \(i\). | In the first sample, it takes no time to get to city 1; to get to city 2 it is possible to use a flight between 1 and 2, which will take 1 unit of time; to city 3 you can get via a road from city 1, which will take 1 unit of time. In the second sample, it also takes no time to get to city 1. To get to city 2 Stanley sh... | Input: 3 1 2 1 3 1 | Output: 0 1 1 | Expert | 7 | 565 | 547 | 96 | 17 |
659 | B | 659B | B. Qualifying Contest | 1,300 | constructive algorithms; sortings | Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m ... | The first line of the input contains two integers n and m (2 β€ n β€ 100 000, 1 β€ m β€ 10 000, n β₯ 2m) β the number of participants of the qualifying contest and the number of regions in Berland.Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of... | Print m lines. On the i-th line print the team of the i-th region β the surnames of the two team members in an arbitrary order, or a single character ""?"" (without the quotes) if you need to spend further qualifying contests in the region. | In the first sample region teams are uniquely determined.In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: ""Petrov""-""Sidorov"", ""Ivanov""-""Sidorov"", ""Ivanov"" -""Petrov"", so it is impossible to determine a team uniquely. | Input: 5 2Ivanov 1 763Andreev 2 800Petrov 1 595Sidorov 1 790Semenov 2 503 | Output: Sidorov IvanovAndreev Semenov | Easy | 2 | 1,379 | 740 | 240 | 6 |
1,714 | B | 1714B | B. Remove Prefix | 800 | data structures; greedy; implementation | Polycarp was presented with some sequence of integers \(a\) of length \(n\) (\(1 \le a_i \le n\)). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he c... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines.The first line contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the given sequence \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a... | For each test case print your answer on a separate line β the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different. | The following are the sequences that will remain after the removal of prefixes: \([1, 4, 3]\); \([1]\); \([1]\); \([6, 5, 4, 3, 2, 1]\); \([2, 1]\). It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed. | Input: 543 1 4 351 1 1 1 11166 5 4 3 2 171 2 1 7 1 2 1 | Output: 1 4 0 0 5 | Beginner | 3 | 774 | 485 | 190 | 17 |
1,500 | E | 1500E | E. Subset Trick | 3,300 | binary search; data structures | Vanya invented an interesting trick with a set of integers.Let an illusionist have a set of positive integers \(S\). He names a positive integer \(x\). Then an audience volunteer must choose some subset (possibly, empty) of \(S\) without disclosing it to the illusionist. The volunteer tells the illusionist the size of ... | The first line contains two integers \(n\), \(q\) (\(1 \leq n, q \leq 200\,000\)) β the size of the initial set \(S\) and the number of changes.The next line contains \(n\) distinct integers \(s_1, s_2, \ldots, s_n\) (\(1 \leq s_i \leq 10^{13}\)) β the initial elements of \(S\).Each of the following \(q\) lines contain... | Print \(q + 1\) lines.In the first line print the number of unsuitable integers for the initial set \(S\). In the next \(q\) lines print the number of unsuitable integers for \(S\) after each change. | In the first example the initial set is \(S = \{1, 2, 3\}\). For this set the trick can be unsuccessful for \(x \in \{1, 2, 3, 4\}\). For example, if \(x = 4\), the volunteer can choose the subset \(\{1, 2\}\) with sum \(3 \leq x\), and can choose the subset \(\{2, 3\}\) with sum \(5 > x\). However, in both cases the i... | Input: 3 11 1 2 3 2 1 1 5 1 6 1 7 2 6 2 2 2 3 1 10 2 5 2 7 2 10 | Output: 4 1 6 12 19 13 8 2 10 3 0 0 | Master | 2 | 1,258 | 723 | 199 | 15 |
813 | A | 813A | A. The Contest | 1,100 | implementation | Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solu... | The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these ... | If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.Otherwise print ""-1"" (without brackets). | In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.In the second example Pasha invents t... | Input: 23 421 47 9 | Output: 7 | Easy | 1 | 1,218 | 585 | 202 | 8 |
1,778 | B | 1778B | B. The Forbidden Permutation | 1,300 | greedy; math | You are given a permutation \(p\) of length \(n\), an array of \(m\) distinct integers \(a_1, a_2, \ldots, a_m\) (\(1 \le a_i \le n\)), and an integer \(d\).Let \(\mathrm{pos}(x)\) be the index of \(x\) in the permutation \(p\). The array \(a\) is not good if \(\mathrm{pos}(a_{i}) < \mathrm{pos}(a_{i + 1}) \le \mathrm{... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\) and \(d\) (\(2\leq n \leq 10^5\), \(2\leq m\leq n\), \(1 \le d \le n\)), the length of th... | For each test case, print the minimum number of moves needed such that the array \(a\) becomes good. | In the first case, \(pos(a_1)=1\), \(pos(a_2)=3\). To make the array good, one way is to swap \(p_3\) and \(p_4\). After that, the array \(a\) will be good because the condition \(\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d\) won't be satisfied.In the second case, \(pos(a_1)=1\), \(pos(a_2)=4\). The \(3\) moves could b... | Input: 54 2 21 2 3 41 35 2 45 4 3 2 15 25 3 33 4 1 5 23 1 22 2 11 22 16 2 41 2 3 4 5 62 5 | Output: 1 3 2 0 2 | Easy | 2 | 1,377 | 717 | 100 | 17 |
254 | A | 254A | A. Cards with Numbers | 1,200 | constructive algorithms; sortings | Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards i... | The first line contains integer n (1 β€ n β€ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 β€ ai β€ 5000) β the numbers that are written on the cards. The numbers on the line are separated by single spaces. | If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β the indices of the cards that form the pairs.Separate the numbers on the lines by spaces. You can... | Input: 320 30 10 30 20 10 | Output: 4 21 56 3 | Easy | 2 | 399 | 246 | 431 | 2 | |
1,359 | D | 1359D | D. Yet Another Yet Another Task | 2,000 | data structures; dp; implementation; two pointers | Alice and Bob are playing yet another card game. This time the rules are the following. There are \(n\) cards lying in a row in front of them. The \(i\)-th card has value \(a_i\). First, Alice chooses a non-empty consecutive segment of cards \([l; r]\) (\(l \le r\)). After that Bob removes a single card \(j\) from that... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of cards.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-30 \le a_i \le 30\)) β the values on the cards. | Print a single integer β the final score of the game. | In the first example Alice chooses a segment \([1;5]\) β the entire row of cards. Bob removes card \(3\) with the value \(10\) from the segment. Thus, the final score is \(5 + (-2) + (-1) + 4 = 6\).In the second example Alice chooses a segment \([1;4]\), so that Bob removes either card \(1\) or \(3\) with the value \(5... | Input: 5 5 -2 10 -1 4 | Output: 6 | Hard | 4 | 839 | 208 | 53 | 13 |
977 | A | 977A | A. Wrong Subtraction | 800 | implementation | Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm: if the last digit of the number is non-zero, she decreases the number by one; if the last digit of the number is zero, she di... | The first line of the input contains two integer numbers \(n\) and \(k\) (\(2 \le n \le 10^9\), \(1 \le k \le 50\)) β the number from which Tanya will subtract and the number of subtractions correspondingly. | Print one integer number β the result of the decreasing \(n\) by one \(k\) times.It is guaranteed that the result will be positive integer number. | The first example corresponds to the following sequence: \(512 \rightarrow 511 \rightarrow 510 \rightarrow 51 \rightarrow 50\). | Input: 512 4 | Output: 50 | Beginner | 1 | 585 | 207 | 146 | 9 |
1 | C | 1C | C. Ancient Berland Circus | 2,100 | geometry; math | Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a specia... | The input file consists of three lines, each of them contains a pair of numbers ββ coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point. | Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100. | Input: 0.000000 0.0000001.000000 1.0000000.000000 1.000000 | Output: 1.00000000 | Hard | 2 | 671 | 221 | 221 | 0 | |
1,346 | E | 1346E | F. Dune II: Battle For Arrakis | 1,700 | *special; dp; graphs | You're at the last mission in one very old and very popular strategy game Dune II: Battle For Arrakis. The map of the mission can be represented as a rectangular matrix of size \(n \times m\). Initially, there are \(a_{i, j}\) units of your army in the cell \((i, j)\).You want to prepare for the final battle, so you wa... | The first line of the input contains three integers \(n, m\) and \(q\) (\(1 \le n, m \le 1000; 1 \le q \le 5000\)) β the size of the matrix and the number of updates correspondingly.The next \(n\) lines contain \(m\) integers each, where the \(j\)-th integer in the \(i\)-th line is \(a_{i, j}\) (\(1 \le a_{i, j} \le 10... | Print \(q+1\) integers \(r_0, r_1, r_2, \dots, r_n\), where \(r_0\) is the minimum number of moves you need to move all your army into exactly one cell, and \(r_i\) for all \(i\) from \(1\) to \(q\) is the minimum number of moves you need to move all your army into exactly one cell after the first \(i\) updates. | Input: 3 3 1 1 2 3 2 1 2 1 1 2 2 3 100 | Output: 21 22 | Medium | 3 | 1,575 | 658 | 313 | 13 | |
2,043 | C | 2043C | C. Sums on Segments | 1,600 | binary search; brute force; data structures; dp; greedy; math | You are given an array \(a\) of \(n\) integers, where all elements except for at most one are equal to \(-1\) or \(1\). The remaining element \(x\) satisfies \(-10^9 \le x \le 10^9\).Find all possible sums of subarrays of \(a\), including the empty subarray, whose sum is defined as \(0\). In other words, find all integ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then, \(t\) test cases follow.Each test case consists of two lines: The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of the array. The second line contains \(n\) integers \(a_1, a_2,... | For each test case, output two lines: In the first line, print a single integer β the number of distinct subarray sums. In the second line, print these sums in ascending order. Each sum should be printed only once, even if it is produced by multiple subarrays. | Let's define \(a[i,j]\) as the subarray of \(a\) from position \(i\) to position \(j\).Consider the first test case of the example: \(-1\) is produced by \(a[2,2]\); \(0\) is produced by the empty subarray; \(1\) is produced by \(a[4,4]\); \(2\) is produced by \(a[4,5]\); \(9\) is produced by \(a[2,3]\); \(10\) is prod... | Input: 551 -1 10 1 15-1 -1 -1 -1 -12-1 227 131 4 -1 | Output: 8 -1 0 1 2 9 10 11 12 6 -5 -4 -3 -2 -1 0 4 -1 0 1 2 4 0 1 7 8 6 -1 0 1 3 4 5 | Medium | 6 | 595 | 587 | 260 | 20 |
80 | A | 80A | A. Panoramix's Prediction | 800 | brute force | A prime number is a number which has exactly two distinct divisors: one and itself. For example, numbers 2, 7, 3 are prime, and 1, 6, 4 are not.The next prime number after x is the smallest prime number greater than x. For example, the next prime number after 2 is 3, and the next prime number after 3 is 5. Note that th... | The first and only input line contains two positive integers β n and m (2 β€ n < m β€ 50). It is guaranteed that n is prime.Pretests contain all the cases with restrictions 2 β€ n < m β€ 4. | Print YES, if m is the next prime number after n, or NO otherwise. | Input: 3 5 | Output: YES | Beginner | 1 | 1,154 | 185 | 66 | 0 | |
1,160 | A2 | 1160A2 | A2. Collaboration | 0 | *special | Consider several locations in integer points on the plane. One of the locations is the base where workers wait. Every other location contains one job.Each job has the following characteristics: work duration, the required number of workers, and also the earliest possible moment of starting the job and the latest possib... | Beginner | 1 | 6,959 | 0 | 0 | 11 | ||||
1,993 | B | 1993B | B. Parity and Sum | 1,100 | constructive algorithms; greedy | Given an array \(a\) of \(n\) positive integers.In one operation, you can pick any pair of indexes \((i, j)\) such that \(a_i\) and \(a_j\) have distinct parity, then replace the smaller one with the sum of them. More formally: If \(a_i < a_j\), replace \(a_i\) with \(a_i + a_j\); Otherwise, replace \(a_j\) with \(a_i ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of array \(a\).It is ... | For each test case, output a single integer β the minimum number of operations required. | In the first test case, all integers already have the same parity. Therefore, no operation is needed.In the third test case, we can perform two operations \((1, 2)\) and \((1, 3)\). The array \(a\) transforms as follows: \(a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarr... | Input: 751 3 5 7 944 4 4 432 3 443 2 2 864 3 6 1 2 163 6 1 2 1 25999999996 999999997 999999998 999999999 1000000000 | Output: 0 0 2 4 3 3 3 | Easy | 2 | 429 | 406 | 88 | 19 |
1,422 | D | 1422D | D. Returning Home | 2,300 | graphs; shortest paths; sortings | Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.Let's represent the city as an area of \(n \times n\) square blocks. Yura needs to move from the block with coordinates \((s_x,s_y... | The first line contains two integers \(n\) and \(m\) β the size of the city and the number of instant-movement locations (\(1 \le n \le 10^9\), \(0 \le m \le 10^5\)).The next line contains four integers \(s_x\) \(s_y\) \(f_x\) \(f_y\) β the coordinates of Yura's initial position and the coordinates of his home (\( 1 \l... | In the only line print the minimum time required to get home. | In the first example Yura needs to reach \((5, 5)\) from \((1, 1)\). He can do that in \(5\) minutes by first using the second instant-movement location (because its \(y\) coordinate is equal to Yura's \(y\) coordinate), and then walking \((4, 1) \to (4, 2) \to (4, 3) \to (5, 3) \to (5, 4) \to (5, 5)\). | Input: 5 3 1 1 5 5 1 2 4 1 3 3 | Output: 5 | Expert | 3 | 807 | 500 | 61 | 14 |
1,889 | C1 | 1889C1 | C1. Doremy's Drying Plan (Easy Version) | 2,000 | brute force; data structures; dp; greedy; sortings | The only differences between the two versions of this problem are the constraint on \(k\), the time limit and the memory limit. You can make hacks only if all versions of the problem are solved.Doremy lives in a rainy country consisting of \(n\) cities numbered from \(1\) to \(n\).The weather broadcast predicted the di... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases. The description of the test cases follows.The first line contains three integers \(n\), \(m\) and \(k\) (\(1\le n\le 2\cdot 10^5\), \(2 \le m \le 2\cdot 10^5\), \(k = 2\)) β the num... | For each test case, output one integer β the maximum number of dry cities. | In the first test case, if Doremy prevents rain \(1,2\), then city \(2\) will be dry; rain \(2,3\), then no city will be dry; rain \(1,3\), then no city will be dry; So there is at most \(1\) dry city.In the second test case, if Doremy prevents rain \(1,2\), then city \(1,2\) will be dry; rain \(2,3\), then city \(4,5\... | Input: 62 3 21 21 21 15 3 21 32 43 510 6 21 56 102 23 75 81 4100 6 21 1001 1001 1001 1001 1001 1001000 2 21 11 120 5 29 203 310 1111 136 18 | Output: 1 2 3 0 1000 15 | Hard | 5 | 757 | 663 | 74 | 18 |
1,845 | E | 1845E | E. Boxes and Balls | 2,500 | dp; implementation; math | There are \(n\) boxes placed in a line. The boxes are numbered from \(1\) to \(n\). Some boxes contain one ball inside of them, the rest are empty. At least one box contains a ball and at least one box is empty.In one move, you have to choose a box with a ball inside and an adjacent empty box and move the ball from one... | The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 1500\); \(1 \le k \le 1500\)) β the number of boxes and the number of moves.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(a_i \in \{0, 1\}\)) β \(0\) denotes an empty box and \(1\) denotes a box with a ball inside. There is at lea... | Print a single integer β the number of different arrangements of balls that can exist after exactly \(k\) moves are performed, modulo \(10^9+7\). | In the first example, there are the following possible arrangements: 0 1 1 0 β obtained after moving the ball from box \(1\) to box \(2\); 1 0 0 1 β obtained after moving the ball from box \(3\) to box \(4\); 1 1 0 0 β obtained after moving the ball from box \(3\) to box \(2\). In the second example, there are the foll... | Input: 4 1 1 0 1 0 | Output: 3 | Expert | 3 | 802 | 356 | 145 | 18 |
274 | D | 274D | D. Lovely Matrix | 2,200 | dfs and similar; graphs; greedy; sortings | Lenny had an n Γ m matrix of positive integers. He loved the matrix so much, because each row of the matrix was sorted in non-decreasing order. For the same reason he calls such matrices of integers lovely.One day when Lenny was at school his little brother was playing with Lenny's matrix in his room. He erased some of... | The first line of the input contains two positive integers n and m (1 β€ nΒ·m β€ 105). Each of the next n lines contains m space-separated integers representing the matrix. An integer -1 shows an erased entry of the matrix. All other integers (each of them is between 0 and 109 inclusive) represent filled entries. | If there exists no possible reordering of the columns print -1. Otherwise the output should contain m integers p1, p2, ..., pm showing the sought permutation of columns. So, the first column of the lovely matrix will be p1-th column of the initial matrix, the second column of the lovely matrix will be p2-th column of t... | Input: 3 31 -1 -11 2 12 -1 1 | Output: 3 1 2 | Hard | 4 | 706 | 311 | 348 | 2 | |
293 | D | 293D | D. Ksusha and Square | 2,700 | geometry; math; probabilities; two pointers | Ksusha is a vigorous mathematician. She is keen on absolutely incredible mathematical riddles. Today Ksusha came across a convex polygon of non-zero area. She is now wondering: if she chooses a pair of distinct points uniformly among all integer points (points with integer coordinates) inside or on the border of the po... | The first line contains integer n (3 β€ n β€ 105) β the number of vertices of Ksusha's convex polygon. Next n lines contain the coordinates of the polygon vertices in clockwise or counterclockwise order. The i-th line contains integers xi, yi (|xi|, |yi| β€ 106) β the coordinates of the vertex that goes i-th in that order... | Print a single real number β the required expected area. The answer will be considered correct if its absolute and relative error doesn't exceed 10 - 6. | Input: 30 05 55 0 | Output: 4.6666666667 | Master | 4 | 692 | 321 | 152 | 2 | |
2,080 | B | 2080B | 1,700 | *special | Medium | 1 | 0 | 0 | 0 | 20 | ||||||
995 | E | 995E | E. Number Clicker | 2,700 | divide and conquer; graphs; meet-in-the-middle; number theory | Allen is playing Number Clicker on his phone.He starts with an integer \(u\) on the screen. Every second, he can press one of 3 buttons. Turn \(u \to u+1 \pmod{p}\). Turn \(u \to u+p-1 \pmod{p}\). Turn \(u \to u^{p-2} \pmod{p}\). Allen wants to press at most 200 buttons and end up with \(v\) on the screen. Help him! | The first line of the input contains 3 positive integers: \(u, v, p\) (\(0 \le u, v \le p-1\), \(3 \le p \le 10^9 + 9\)). \(p\) is guaranteed to be prime. | On the first line, print a single integer \(\ell\), the number of button presses. On the second line, print integers \(c_1, \dots, c_\ell\), the button presses. For \(1 \le i \le \ell\), \(1 \le c_i \le 3\).We can show that the answer always exists. | In the first example the integer on the screen changes as \(1 \to 2 \to 3\).In the second example the integer on the screen changes as \(3 \to 2\). | Input: 1 3 5 | Output: 21 1 | Master | 4 | 317 | 154 | 249 | 9 |
607 | E | 607E | E. Cross Sum | 3,300 | binary search; geometry | Genos has been given n distinct lines on the Cartesian plane. Let be a list of intersection points of these lines. A single point might appear multiple times in this list if it is the intersection of multiple pairs of lines. The order of the list does not matter.Given a query point (p, q), let be the corresponding list... | The first line of the input contains a single integer n (2 β€ n β€ 50 000) β the number of lines.The second line contains three integers x, y and m (|x|, |y| β€ 1 000 000, ) β the encoded coordinates of the query point and the integer m from the statement above. The query point (p, q) is obtained as . In other words, divi... | Print a single real number, the sum of m smallest elements of . Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.To clarify, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if . | In the first sample, the three closest points have distances and .In the second sample, the two lines y = 1000x - 1000 and intersect at (2000000, 1999999000). This point has a distance of from ( - 1000, - 1000).In the third sample, the three lines all intersect at the point (1, 1). This intersection point is present th... | Input: 41000 1000 31000 0-1000 00 50000 -5000 | Output: 14.282170363 | Master | 2 | 737 | 639 | 298 | 6 |
1,611 | F | 1611F | F. ATM and Students | 1,800 | binary search; data structures; two pointers | Polycarp started working at a bank. He was assigned to monitor the ATM. The ATM initially contains \(s\) rubles.A queue of \(n\) students lined up to him. Each student wants to either withdraw a certain amount of money or deposit it into an account. If \(a_i\) is positive, then the student credits that amount of money ... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines. The first one contains integers \(n\) and \(s\) (\(1 \le n \le 2\cdot10^5\); \(0 \le s \le 10^9\)) β the length of the \(a\) array and the initial amount of rubles in the ATM. T... | Print \(t\) lines, each line must contain the answer to the corresponding set of input data: if the answer exists, print the numbers of the first and last served student. If there is no solution, then print -1 on the line.If there are several possible answers, print any. | In the first test case, the only correct answer is 2 4, since when serving students, the number of rubles at the ATM does not become negative, and this is the maximum number of students that can be served.In the second test case, the answer is -1, as there is not enough money for any student at the ATM.In the third tes... | Input: 3 4 10 -16 2 -6 8 3 1000 -100000 -100000 -100000 6 0 2 6 -164 1 -1 -6543 | Output: 2 4 -1 1 2 | Medium | 3 | 1,308 | 570 | 271 | 16 |
339 | D | 339D | D. Xenia and Bit Operations | 1,700 | data structures; trees | Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.Namely, it takes several iterations to calculate value v. At the first iteration, Xenia ... | The first line contains two integers n and m (1 β€ n β€ 17, 1 β€ m β€ 105). The next line contains 2n integers a1, a2, ..., a2n (0 β€ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 β€ pi β€ 2n, 0 β€ bi < 230) β the i-th query. | Print m integers β the i-th integer denotes value v for sequence a after the i-th query. | For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation | Input: 2 41 6 3 51 43 41 21 2 | Output: 1333 | Medium | 2 | 1,436 | 267 | 88 | 3 |
1,453 | E | 1453E | E. Dog Snacks | 2,300 | binary search; dfs and similar; dp; greedy; trees | Gildong is playing with his dog, Badugi. They're at a park that has \(n\) intersections and \(n-1\) bidirectional roads, each \(1\) meter in length and connecting two intersections with each other. The intersections are numbered from \(1\) to \(n\), and for every \(a\) and \(b\) (\(1 \le a, b \le n\)), it is possible t... | Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)).The first line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of intersections of the park.The next \(n-1\) lines contain two integers \(u\) and \(v\) (\(1 \... | For each test case, print one integer β the minimum possible value of \(k\) such that Badugi can complete the mission. | In the first case, Badugi can complete his mission with \(k=2\) by moving as follows: Initially, Badugi is at the \(1\)-st intersection. The closest snack is obviously at the \(1\)-st intersection, so he just eats it. Next, he looks for the closest snack, which can be either the one at the \(2\)-nd or the one at the \(... | Input: 3 3 1 2 1 3 4 1 2 2 3 3 4 8 1 2 2 3 3 4 1 5 5 6 6 7 5 8 | Output: 2 3 3 | Expert | 5 | 1,734 | 705 | 118 | 14 |
1,249 | B2 | 1249B2 | B2. Books Exchange (hard version) | 1,300 | dfs and similar; dsu; math | The only difference between easy and hard versions is constraints.There are \(n\) kids, each of them is reading a unique book. At the end of any day, the \(i\)-th kid will give his book to the \(p_i\)-th kid (in case of \(i = p_i\) the kid will give his book to himself). It is guaranteed that all values of \(p_i\) are ... | The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of kids in the query. The second line of the query contains \(n\) integers \(p_1, p_2, \dots... | For each query, print the answer on it: \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) is the number of the day the book of the \(i\)-th child is returned back to him for the first time in this query. | Input: 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 | Output: 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 | Easy | 3 | 1,537 | 590 | 208 | 12 | |
147 | B | 147B | B. Smile House | 2,500 | binary search; graphs; matrices | A smile house is created to raise the mood. It has n rooms. Some of the rooms are connected by doors. For each two rooms (number i and j), which are connected by a door, Petya knows their value cij β the value which is being added to his mood when he moves from room i to room j.Petya wondered whether he can raise his m... | The first line contains two positive integers n and m (), where n is the number of rooms, and m is the number of doors in the Smile House. Then follows the description of the doors: m lines each containing four integers i, j, cij ΠΈ cji (1 β€ i, j β€ n, i β j, - 104 β€ cij, cji β€ 104). It is guaranteed that no more than on... | Print the minimum number of rooms that one needs to visit during one traverse of the cycle that can raise mood infinitely. If such cycle does not exist, print number 0. | Cycle is such a sequence of rooms a1, a2, ..., ak, that a1 is connected with a2, a2 is connected with a3, ..., ak - 1 is connected with ak, ak is connected with a1. Some elements of the sequence can coincide, that is, the cycle should not necessarily be simple. The number of rooms in the cycle is considered as k, the s... | Input: 4 41 2 -10 31 3 1 -102 4 -10 -13 4 0 -3 | Output: 4 | Expert | 3 | 461 | 389 | 168 | 1 |
1,535 | A | 1535A | A. Fair Playoff | 800 | brute force; implementation | Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.It is known that in a match between two players, the one... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.A single line of test case contains four integers \(s_1, s_2, s_3, s_4\) (\(1 \le s_i \le 100\)) β skill of the players. It is guaranteed that all the numbers in the array are different. | For each testcase, output YES if the tournament is fair, or NO otherwise. | Consider the example: in the first test case, players \(2\) and \(3\) with skills \(7\) and \(9\) advance to the finals; in the second test case, players \(2\) and \(4\) with skills \(5\) and \(9\) advance to the finals. The player with skill \(6\) does not advance, but the player with skill \(5\) advances to the final... | Input: 4 3 7 9 5 4 5 6 9 5 3 8 1 6 5 3 2 | Output: YES NO YES NO | Beginner | 2 | 651 | 283 | 73 | 15 |
837 | C | 837C | C. Two Seals | 1,500 | brute force; implementation | One very important person has a piece of paper in the form of a rectangle a Γ b.Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi Γ yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).A very important p... | The first line contains three integer numbers n, a and b (1 β€ n, a, b β€ 100).Each of the next n lines contain two numbers xi, yi (1 β€ xi, yi β€ 100). | Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0. | In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest... | Input: 2 2 21 22 1 | Output: 4 | Medium | 2 | 634 | 148 | 105 | 8 |
1,608 | C | 1608C | C. Game Master | 1,700 | data structures; dfs and similar; dp; graphs; greedy; two pointers | \(n\) players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to org... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of players.The second line of each test case contains \(n\) integers \(a_1, a_2, \... | For each test case print a string of length \(n\). \(i\)-th character should be ""1"" if the \(i\)-th player can win the tournament, or ""0"" otherwise. | In the first test case, the \(4\)-th player will beat any other player on any game, so he will definitely win the tournament.In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament. | Input: 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 | Output: 0001 1111 1 | Medium | 6 | 707 | 767 | 152 | 16 |
1,385 | G | 1385G | G. Columns Swaps | 2,300 | 2-sat; dfs and similar; dsu; graphs; implementation | You are given a table \(a\) of size \(2 \times n\) (i.e. two rows and \(n\) columns) consisting of integers from \(1\) to \(n\).In one move, you can choose some column \(j\) (\(1 \le j \le n\)) and swap values \(a_{1, j}\) and \(a_{2, j}\) in it. Each column can be chosen no more than once.Your task is to find the mini... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of columns in the table. The second line of the test case contains \(n\) i... | For each test case print the answer: -1 if it is impossible to obtain permutation of size \(n\) in both first and the second rows of the table, or one integer \(k\) in the first line, where \(k\) is the minimum number of moves required to obtain permutations in both rows, and \(k\) distinct integers \(pos_1, pos_2, \do... | Input: 6 4 1 2 3 4 2 3 1 4 5 5 3 5 1 4 1 2 3 2 4 3 1 2 1 3 3 2 4 1 2 2 1 3 4 3 4 4 4 3 1 4 3 2 2 1 3 1 1 2 3 2 2 | Output: 0 2 2 3 1 1 2 3 4 2 3 4 -1 | Expert | 5 | 693 | 770 | 527 | 13 | |
938 | B | 938B | B. Run For Your Prize | 1,100 | brute force; greedy | You and your friend are participating in a TV show ""Run For Your Prize"".At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend β at position 106 (and there is no prize in any of these two positi... | The first line contains one integer n (1 β€ n β€ 105) β the number of prizes.The second line contains n integers a1, a2, ..., an (2 β€ ai β€ 106 - 1) β the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order. | Print one integer β the minimum number of seconds it will take to collect all prizes. | In the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5. | Input: 32 3 9 | Output: 8 | Easy | 2 | 1,008 | 264 | 85 | 9 |
1,096 | C | 1096C | C. Polygon for the Angle | 1,600 | brute force; geometry | You are given an angle \(\text{ang}\). The Jury asks You to find such regular \(n\)-gon (regular polygon with \(n\) vertices) that it has three vertices \(a\), \(b\) and \(c\) (they can be non-consecutive) with \(\angle{abc} = \text{ang}\) or report that there is no such \(n\)-gon. If there are several answers, print t... | The first line contains single integer \(T\) (\(1 \le T \le 180\)) β the number of queries. Each of the next \(T\) lines contains one integer \(\text{ang}\) (\(1 \le \text{ang} < 180\)) β the angle measured in degrees. | For each query print single integer \(n\) (\(3 \le n \le 998244353\)) β minimal possible number of vertices in the regular \(n\)-gon or \(-1\) if there is no such \(n\). | The answer for the first query is on the picture above.The answer for the second query is reached on a regular \(18\)-gon. For example, \(\angle{v_2 v_1 v_6} = 50^{\circ}\).The example angle for the third query is \(\angle{v_{11} v_{10} v_{12}} = 2^{\circ}\).In the fourth query, minimal possible \(n\) is \(180\) (not \... | Input: 4 54 50 2 178 | Output: 10 18 90 180 | Medium | 2 | 412 | 218 | 169 | 10 |
1,580 | B | 1580B | B. Mathematics Curriculum | 2,600 | brute force; combinatorics; dp; trees | Let \(c_1, c_2, \ldots, c_n\) be a permutation of integers \(1, 2, \ldots, n\). Consider all subsegments of this permutation containing an integer \(x\). Given an integer \(m\), we call the integer \(x\) good if there are exactly \(m\) different values of maximum on these subsegments.Cirno is studying mathematics, and ... | The first line contains four integers \(n, m, k, p\) (\(1 \le n \le 100, 1 \le m \le n, 1 \le k \le n, 1 \le p \le 10^9\)). | Output the number of permutations modulo \(p\). | In the first test case, there are four permutations: \([1, 3, 2, 4]\), \([2, 3, 1, 4]\), \([4, 1, 3, 2]\) and \([4, 2, 3, 1]\).Take permutation \([1, 3, 2, 4]\) as an example:For number \(1\), all subsegments containing it are: \([1]\), \([1, 3]\), \([1, 3, 2]\) and \([1, 3, 2, 4]\), and there're three different maxima... | Input: 4 3 2 10007 | Output: 4 | Expert | 4 | 1,171 | 123 | 47 | 15 |
2,035 | H | 2035H | H. Peak Productivity Forces | 3,500 | constructive algorithms | I'm peakly productive and this is deep.You are given two permutations\(^{\text{β}}\) \(a\) and \(b\), both of length \(n\).You can perform the following three-step operation on permutation \(a\): Choose an index \(i\) (\(1 \le i \le n\)). Cyclic shift \(a_1, a_2, \ldots, a_{i-1}\) by \(1\) to the right. If you had chos... | The first line contains a single integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the lengths of permutations \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ld... | For each test case:If there is a sequence of operations to transform \(a\) into \(b\), output a single integer \(q\) (\(0\le q\le 2n\)) β the number of operations in the first line and \(q\) integers with the \(i\)-th number representing the index of the \(i\)-th operation in the second line.If there is no sequence of ... | In the first case, you can do no operation since \(a=b\).In the second case, it can be proved \(a\) can not be transformed into \(b\).In the third case, \(a\) is transformed into \([2,3,1]\) after the first operation and into \(b\) after the second operation. | Input: 411121 22 132 1 33 2 187 8 3 5 4 6 1 22 1 6 4 5 3 8 7 | Output: 0 -1 2 1 3 7 3 4 5 1 2 1 1 | Master | 1 | 1,741 | 618 | 363 | 20 |
690 | D1 | 690D1 | D1. The Wall (easy) | 1,200 | ""The zombies are lurking outside. Waiting. Moaning. And when they come...""""When they come?""""I hope the Wall is high enough.""Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segment... | The first line of the input consists of two space-separated integers R and C, 1 β€ R, C β€ 100. The next R lines provide a description of the columns as follows: each of the R lines contains a string of length C, the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise. The inp... | The number of wall segments in the input configuration. | In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second. | Input: 3 7...............BB.B.. | Output: 2 | Easy | 0 | 828 | 382 | 55 | 6 | |
1,005 | F | 1005F | F. Berland and the Shortest Paths | 2,100 | brute force; dfs and similar; graphs; shortest paths | There are \(n\) cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from \(1\) to \(n\).It is known that, from the capital (the city with the number \(1\)), you c... | The first line of the input contains integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5\)), where \(n\) is the number of cities in the country, \(m\) is the number of roads and \(k\) is the number of options to choose a set of roads for repair. It is guaranteed t... | Print \(t\) (\(1 \le t \le k\)) β the number of ways to choose a set of roads for repair. Recall that you need to find \(k\) different options; if there are fewer than \(k\) of them, then you need to find all possible different valid options.In the following \(t\) lines, print the options, one per line. Print an option... | Input: 4 4 31 22 31 44 3 | Output: 211101011 | Hard | 4 | 1,389 | 694 | 810 | 10 | |
1,839 | C | 1839C | C. Insert Zero and Invert Prefix | 1,300 | constructive algorithms | You have a sequence \(a_1, a_2, \ldots, a_n\) of length \(n\), each element of which is either \(0\) or \(1\), and a sequence \(b\), which is initially empty.You are going to perform \(n\) operations. On each of them you will increase the length of \(b\) by \(1\). On the \(i\)-th operation you choose an integer \(p\) b... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^5\)) β the length of the sequence \(a\).The second line of each test case contains \(n\) integers \(a_1, ... | For each test case: output ""NO"", if it is impossible to make \(b\) equal to \(a\) using the given operations; otherwise, output ""YES"" in the first line and \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(0 \le p_i \le i-1\)) in the second line β the description of sequence of operations that makes \(b\) equal to \(a\).... | In the first test case, Before the first operation, \(b = [\,]\). You choose \(p = 0\) and replace \(b\) with \([\, \underline{0} \,]\) On the second operation you choose \(p = 0\) and replace \(b\) with \([\, \underline{0}, 0 \,]\). On the third operation you choose \(p = 2\) and replace \(b\) with \([\, 1, 1, \underl... | Input: 451 1 0 0 01130 1 161 0 0 1 1 0 | Output: YES 0 0 2 1 3 NO NO YES 0 1 0 2 4 2 | Easy | 1 | 1,123 | 466 | 455 | 18 |
1,194 | E | 1194E | E. Count The Rectangles | 2,200 | bitmasks; brute force; data structures; geometry; sortings | There are \(n\) segments drawn on a plane; the \(i\)-th segment connects two points (\(x_{i, 1}\), \(y_{i, 1}\)) and (\(x_{i, 2}\), \(y_{i, 2}\)). Each segment is non-degenerate, and is either horizontal or vertical β formally, for every \(i \in [1, n]\) either \(x_{i, 1} = x_{i, 2}\) or \(y_{i, 1} = y_{i, 2}\) (but on... | The first line contains one integer \(n\) (\(1 \le n \le 5000\)) β the number of segments.Then \(n\) lines follow. The \(i\)-th line contains four integers \(x_{i, 1}\), \(y_{i, 1}\), \(x_{i, 2}\) and \(y_{i, 2}\) denoting the endpoints of the \(i\)-th segment. All coordinates of the endpoints are in the range \([-5000... | Print one integer β the number of ways to choose four segments so they form a rectangle. | The following pictures represent sample cases: | Input: 7 -1 4 -1 -2 6 -1 -2 -1 -2 3 6 3 2 -2 2 4 4 -1 4 3 5 3 5 1 5 2 1 2 | Output: 7 | Hard | 5 | 1,129 | 537 | 88 | 11 |
1,368 | D | 1368D | D. AND, OR and square sum | 1,700 | bitmasks; greedy; math | Gottfried learned about binary number representation. He then came up with this task and presented it to you.You are given a collection of \(n\) non-negative integers \(a_1, \ldots, a_n\). You are allowed to perform the following operation: choose two distinct indices \(1 \leq i, j \leq n\). If before the operation \(a... | The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, \ldots, a_n\) (\(0 \leq a_i < 2^{20}\)). | Print a single integer β the largest possible sum of squares that can be achieved after several (possibly zero) operations. | In the first sample no operation can be made, thus the answer is \(123^2\).In the second sample we can obtain the collection \(1, 1, 7\), and \(1^2 + 1^2 + 7^2 = 51\).If \(x\) and \(y\) are represented in binary with equal number of bits (possibly with leading zeros), then each bit of \(x~\mathsf{AND}~y\) is set to \(1... | Input: 1 123 | Output: 15129 | Medium | 3 | 772 | 167 | 123 | 13 |
1,076 | E | 1076E | E. Vasya and a Tree | 1,900 | data structures; trees | Vasya has a tree consisting of \(n\) vertices with root in vertex \(1\). At first all vertices has \(0\) written on it.Let \(d(i, j)\) be the distance between vertices \(i\) and \(j\), i.e. number of edges in the shortest path from \(i\) to \(j\). Also, let's denote \(k\)-subtree of vertex \(x\) β set of vertices \(y\)... | The first line contains single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β number of vertices in the tree.Each of next \(n - 1\) lines contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\)) β edge between vertices \(x\) and \(y\). It is guarantied that given graph is a tree.Next line contains single integer \(... | Print \(n\) integers. The \(i\)-th integers is the value, written in the \(i\)-th vertex after processing all queries. | In the first exapmle initial values in vertices are \(0, 0, 0, 0, 0\). After the first query values will be equal to \(1, 1, 1, 0, 0\). After the second query values will be equal to \(1, 11, 1, 0, 0\). After the third query values will be equal to \(1, 11, 1, 100, 0\). | Input: 5 1 2 1 3 2 4 2 5 3 1 1 1 2 0 10 4 10 100 | Output: 1 11 1 100 0 | Hard | 2 | 730 | 556 | 118 | 10 |
178 | D2 | 178D2 | D2. Magic Squares | 1,900 | The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n Γ n. The elements of this matrix are integers. T... | The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 β€ ai β€ 108), separated by single spaces.The input limitations for getting 20 points are: 1 β€ n β€ 3 The input limitations for getting 50 points are: 1 β€ n β€ 4 It is guaranteed that there are no more than 9 distinct numbers am... | The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed... | Input: 31 2 3 4 5 6 7 8 9 | Output: 152 7 69 5 14 3 8 | Hard | 0 | 970 | 387 | 342 | 1 | ||
72 | D | 72D | D. Perse-script | 2,300 | *special; expression parsing | Two good friends were trying to make a new programming language called Perse-script.The most important part of this language is strings. A string in Perse-script is put between characters ""So for example ""Hello"" is a string. But Hello is a variable name or a keyword, which are not considered in this problem.Perse-sc... | A single line containing the correct expression. It is guaranteed that the total length of this expression does not exceed 103 and that all the integers used in it are less than or equal to 100 by absolute value. The given string is non-empty.All strings in the input which are placed between ""s consist of uppercase an... | Print in single line the resulting string. It is guaranteed that an answer exists and that the length of the answer does not exceed 104. It is guaranteed that the answer is non-empty. | Input: ""HelloWorld"" | Output: ""HelloWorld"" | Expert | 2 | 1,735 | 351 | 183 | 0 | |
856 | C | 856C | C. Eleventh Birthday | 2,400 | combinatorics; dp; math | It is Borya's eleventh birthday, and he has got a great present: n cards with numbers. The i-th card has the number ai written on it. Borya wants to put his cards in a row to get one greater number. For example, if Borya has cards with numbers 1, 31, and 12, and he puts them in a row in this order, he would get a numbe... | Input data contains multiple test cases. The first line of the input data contains an integer t β the number of test cases (1 β€ t β€ 100). The descriptions of test cases follow.Each test is described by two lines.The first line contains an integer n (1 β€ n β€ 2000) β the number of cards in Borya's present.The second line... | For each test case output one line: the number of ways to put the cards to the table so that the resulting big number was divisible by 11, print the number modulo 998244353. | Input: 421 131 31 12312345 67 8491 2 3 4 5 6 7 8 9 | Output: 22231680 | Expert | 3 | 1,102 | 489 | 173 | 8 | |
13 | E | 13E | E. Holes | 2,700 | data structures; dsu | Little Petya likes to play a lot. Most of all he likes to play a game Β«HolesΒ». This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into ho... | The first line contains two integers N and M (1 β€ N β€ 105, 1 β€ M β€ 105) β the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N β initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types... | For each move of the type 1 output two space-separated numbers on a separate line β the number of the last hole the ball visited before leaving the row and the number of jumps it made. | Input: 8 51 1 1 1 1 2 8 21 10 1 31 10 3 41 2 | Output: 8 78 57 3 | Master | 2 | 881 | 528 | 184 | 0 | |
1,023 | F | 1023F | F. Mobile Phone Network | 2,600 | dfs and similar; dsu; graphs; trees | You are managing a mobile phone network, and want to offer competitive prices to connect a network.The network has \(n\) nodes.Your competitor has already offered some connections between some nodes, with some fixed prices. These connections are bidirectional. There are initially \(m\) connections the competitor is off... | The first line of input will contain three integers \(n\), \(k\) and \(m\) (\(1 \leq n, k, m \leq 5 \cdot 10^5, k \leq n-1\)), the number of nodes, the number of your connections, and the number of competitor connections, respectively.The next \(k\) lines contain two integers \(ga_i\) and \(gb_i\) (\(1 \leq ga_i, gb_i ... | Print a single integer, the maximum possible profit you can achieve if you set the prices on your connections appropriately. If the profit is unbounded, print \(-1\). | In the first sample, it's optimal to give connection \(1-3\) cost \(3\), connection \(1-2\) cost \(3\), and connection \(3-4\) cost \(8\). In this case, the cheapest connected network has cost \(14\), and the customer will choose one that chooses all of your connections.In the second sample, as long as your first conne... | Input: 4 3 61 23 41 32 3 33 1 41 2 44 2 84 3 84 1 10 | Output: 14 | Expert | 4 | 1,445 | 1,261 | 166 | 10 |
1,250 | E | 1250E | E. The Coronation | 2,300 | graphs; implementation | The coronation of King Berl XXII is soon! The whole royal family, including \(n\) daughters of Berl XXII, will be present.The King has ordered his jeweler to assemble \(n\) beautiful necklaces, so each of the princesses could wear exactly one necklace during the ceremony β and now these necklaces are finished. Each nec... | The first line contains one integer \(t\) (\(1 \le t \le 50\)) β the number of test cases. Then the test cases follow.Each test case begins with a line containing three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 50\), \(1 \le k \le m \le 50\)) β the number of necklaces, the number of gems in each necklace, and the ... | For each test case, print the answer as follows.If it is impossible to avoid the conflict, print -1 on a single line. In this case you should not output anything else for that test case.Otherwise, the first line of the test case answer should contain the single integer \(d\) β the minimum number of necklaces that are t... | Input: 5 5 7 2 1010100 0010101 1111010 1000010 0000101 6 9 3 011111110 100111000 111100000 000111111 110100111 111110111 3 4 2 0001 1000 0000 3 4 4 0001 1000 0000 2 4 3 0001 1000 | Output: 2 1 3 1 3 0 -1 1 1 | Expert | 2 | 2,284 | 572 | 599 | 12 | |
61 | B | 61B | B. Hard Work | 1,300 | strings | After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher g... | The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs (""-"", "";"" and ""_""). All the initial strings have length from 1 to 100, inclusively.In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students... | For each student write in a different line. Print ""WA"" if his answer is wrong or ""ACC"" if his answer is OK. | Input: Iran_Persian;W_o;n;d;e;r;f;u;l;7WonderfulPersianIranwonderful_PersIAN_IRAN;;_WONDERFUL___IRAN__PERSIAN__;;Ira__Persiann__WonderfulWonder;;fulPersian___;I;r;a;n;__________IranPersianWonderful__________PersianIran_is_Wonderful | Output: ACCACCACCWAACCACCWA | Easy | 1 | 2,569 | 570 | 111 | 0 | |
1 | A | 1A | A. Theatre Square | 1,000 | math | Theatre Square in the capital city of Berland has a rectangular shape with the size n Γ m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a Γ a.What is the least number of flagstones needed to pave the Square? It's ... | The input contains three positive integer numbers in the first line: n, m and a (1 β€ n, m, a β€ 109). | Write the needed number of flagstones. | Input: 6 6 4 | Output: 4 | Beginner | 1 | 527 | 100 | 38 | 0 | |
489 | C | 489C | C. Given Length and Sum of Digits... | 1,400 | dp; greedy; implementation | You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes. | The single line of the input contains a pair of integers m, s (1 β€ m β€ 100, 0 β€ s β€ 900) β the length and the sum of the digits of the required numbers. | In the output print the pair of the required non-negative integer numbers β first the minimum possible number, then β the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers ""-1 -1"" (without the quotes). | Input: 2 15 | Output: 69 96 | Easy | 3 | 269 | 152 | 256 | 4 | |
160 | C | 160C | C. Find Pair | 1,700 | implementation; math; sortings | You've got another problem dealing with arrays. Let's consider an arbitrary sequence containing n (not necessarily different) integers a1, a2, ..., an. We are interested in all possible pairs of numbers (ai, aj), (1 β€ i, j β€ n). In other words, let's consider all n2 pairs of numbers, picked from the given array.For exa... | The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ n2). The second line contains the array containing n integers a1, a2, ..., an ( - 109 β€ ai β€ 109). The numbers in the array can coincide. All numbers are separated with spaces.Please do not use the %lld specificator to read or write 64-bit integers in Π‘... | In the single line print two numbers β the sought k-th pair. | In the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2).The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3). | Input: 2 42 1 | Output: 2 2 | Medium | 3 | 973 | 400 | 60 | 1 |
1,701 | C | 1701C | C. Schedule Management | 1,400 | binary search; greedy; implementation; two pointers | There are \(n\) workers and \(m\) tasks. The workers are numbered from \(1\) to \(n\). Each task \(i\) has a value \(a_i\) β the index of worker who is proficient in this task.Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in \(1\) hour. Otherwise, it takes them ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 2 \cdot 10^5\)) β the number of workers and the number of tasks.The second line contains \(m\) integers \(a_1, a_2, \dots, a_m\) (\(1... | For each testcase, print a single integer β the minimum time all tasks can be completed by. | In the first testcase, the first worker works on tasks \(1\) and \(3\), and the second worker works on tasks \(2\) and \(4\). Since they both are proficient in the corresponding tasks, they take \(1\) hour on each. Both of them complete \(2\) tasks in \(2\) hours. Thus, all tasks are completed by \(2\) hours.In the sec... | Input: 42 41 2 1 22 41 1 1 15 55 1 3 2 41 11 | Output: 2 3 1 1 | Easy | 4 | 619 | 464 | 91 | 17 |
1,553 | A | 1553A | A. Digits Sum | 800 | math; number theory | Let's define \(S(x)\) to be the sum of digits of number \(x\) written in decimal system. For example, \(S(5) = 5\), \(S(10) = 1\), \(S(322) = 7\).We will call an integer \(x\) interesting if \(S(x + 1) < S(x)\). In each test you will be given one integer \(n\). Your task is to calculate the number of integers \(x\) suc... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β number of test cases.Then \(t\) lines follow, the \(i\)-th line contains one integer \(n\) (\(1 \le n \le 10^9\)) for the \(i\)-th test case. | Print \(t\) integers, the \(i\)-th should be the answer for the \(i\)-th test case. | The first interesting number is equal to \(9\). | Input: 5 1 9 10 34 880055535 | Output: 0 1 1 3 88005553 | Beginner | 2 | 370 | 208 | 83 | 15 |
758 | B | 758B | B. Blown Garland | 1,100 | brute force; implementation; number theory | Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland.Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working.... | The first and the only line contains the string s (4 β€ |s| β€ 100), which describes the garland, the i-th symbol of which describes the color of the i-th light bulb in the order from the beginning of garland: 'R' β the light bulb is red, 'B' β the light bulb is blue, 'Y' β the light bulb is yellow, 'G' β the light bulb ... | In the only line print four integers kr, kb, ky, kg β the number of dead light bulbs of red, blue, yellow and green colors accordingly. | In the first example there are no dead light bulbs.In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements. | Input: RYBGRYBGR | Output: 0 0 0 0 | Easy | 3 | 911 | 712 | 135 | 7 |
1,270 | G | 1270G | G. Subset with Zero Sum | 2,700 | constructive algorithms; dfs and similar; graphs; math | You are given \(n\) integers \(a_1, a_2, \dots, a_n\), such that for each \(1\le i \le n\) holds \(i-n\le a_i\le i-1\).Find some nonempty subset of these integers, whose sum is equal to \(0\). It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^6\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n \le 10^6\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \d... | For each test case, output two lines.In the first line, output \(s\) (\(1\le s \le n\)) β the number of elements in your subset.In the second line, output \(s\) integers \(i_1, i_2, \dots, i_s\) (\(1\le i_k \le n\)). All integers have to be pairwise different, and \(a_{i_1} + a_{i_2} + \dots + a_{i_s}\) has to be equal... | In the first example, we get sum is \(a_1 = 0\).In the second example, we get sum is \(a_1 + a_4 + a_3 + a_2 = 0\). | Input: 2 5 0 1 2 3 4 4 -3 1 1 1 | Output: 1 1 4 1 4 3 2 | Master | 4 | 338 | 441 | 409 | 12 |
1,146 | G | 1146G | G. Zoning Restrictions | 2,700 | dp; flows; graphs | You are planning to build housing on a street. There are \(n\) spots available on the street on which you can build a house. The spots are labeled from \(1\) to \(n\) from left to right. In each spot, you can build a house with an integer height between \(0\) and \(h\).In each spot, if a house has height \(a\), you can... | The first line contains three integers \(n,h,m\) (\(1 \leq n,h,m \leq 50\)) β the number of spots, the maximum height, and the number of restrictions, respectively.Each of the next \(m\) lines contains four integers \(l_i, r_i, x_i, c_i\) (\(1 \leq l_i \leq r_i \leq n\), \(0 \leq x_i \leq h\), \(1 \leq c_i \leq 5\,000\... | Print a single integer denoting the maximum profit you can make. | In the first example, it's optimal to build houses with heights \([1, 3, 2]\). We get a gain of \(1^2+3^2+2^2 = 14\). We don't violate any restrictions, so there are no fees, so the total profit is \(14 - 0 = 14\).In the second example, it's optimal to build houses with heights \([10, 8, 8, 10]\). We get a gain of \(10... | Input: 3 3 3 1 1 1 1000 2 2 3 1000 3 3 2 1000 | Output: 14 | Master | 3 | 675 | 323 | 64 | 11 |
1,689 | D | 1689D | D. Lena and Matrix | 1,900 | data structures; dp; geometry; shortest paths | Lena is a beautiful girl who likes logical puzzles.As a gift for her birthday, Lena got a matrix puzzle!The matrix consists of \(n\) rows and \(m\) columns, and each cell is either black or white. The coordinates \((i,j)\) denote the cell which belongs to the \(i\)-th row and \(j\)-th column for every \(1\leq i \leq n\... | There are several test cases in the input data. The first line contains a single integer \(t\) (\(1\leq t\leq 10\,000\)) β the number of test cases. This is followed by the test cases description.The first line of each test case contains two integers \(n\) and \(m\) (\(2\leq n,m \leq 1000\)) β the dimensions of the mat... | For each test case, output the optimal cell \((a,b)\) to choose. If multiple answers exist, output any. | In the first test case the two black cells have coordinates \((1,1)\) and \((3,2)\). The four optimal cells are \((1,2)\), \((2,1)\), \((2,2)\) and \((3,1)\). It can be shown that no other cell minimizes the maximum Manhattan distance to every black cell.In the second test case it is optimal to choose the black cell \(... | Input: 5 3 2 BW WW WB 3 3 WWB WBW BWW 2 3 BBB BBB 5 5 BWBWB WBWBW BWBWB WBWBW BWBWB 9 9 WWWWWWWWW WWWWWWWWW BWWWWWWWW WWWWWWWWW WWWWBWWWW WWWWWWWWW WWWWWWWWW WWWWWWWWW WWWWWWWWB | Output: 2 1 2 2 1 2 3 3 6 5 | Hard | 4 | 790 | 676 | 103 | 16 |
1,648 | D | 1648D | D. Serious Business | 2,800 | data structures; divide and conquer; dp; implementation; shortest paths | Dima is taking part in a show organized by his friend Peter. In this show Dima is required to cross a \(3 \times n\) rectangular field. Rows are numbered from \(1\) to \(3\) and columns are numbered from \(1\) to \(n\).The cell in the intersection of the \(i\)-th row and the \(j\)-th column of the field contains an int... | The first input line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 500\,000\)) β the number of columns in the field and the number of special offers.The next three lines describe the field, \(i\)-th of them contains \(n\) integers \(a_{i1}\), \(a_{i2}\), ..., \(a_{in}\) (\(-10^9 \le a_{ij} \le 10^9)\) β the v... | Output one integer β the maximum final score Dima can achieve. | In the first example, it is optimal to use Peter's second offer of \(4\) rubles and go through the cells \((1, 1)\), \((1, 2)\), \((1, 3)\), \((2, 3)\), \((3, 3)\), \((3, 4)\), earning \(1 + 0 + 2 + 9 + 4 + 1 - 4 = 13\) rubles in total.In the second example, it is optimal to use Peter's second and third offers of \(2\)... | Input: 4 3 1 0 2 -1 -3 1 9 2 3 2 4 1 1 2 5 2 3 4 1 4 14 | Output: 13 | Master | 5 | 1,572 | 596 | 62 | 16 |
13 | B | 13B | B. Letter A | 2,000 | geometry; implementation | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (let... | The first line contains one integer t (1 β€ t β€ 10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have pos... | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | Input: 34 4 6 04 1 5 24 0 4 40 0 0 60 6 2 -41 1 0 10 0 0 50 5 2 -11 2 0 1 | Output: YESNOYES | Hard | 2 | 732 | 333 | 119 | 0 | |
1,762 | D | 1762D | D. GCD Queries | 2,100 | constructive algorithms; interactive; number theory | This is an interactive problem.There is a secret permutation \(p\) of \([0,1,2,\ldots,n-1]\). Your task is to find \(2\) indices \(x\) and \(y\) (\(1 \leq x, y \leq n\), possibly \(x=y\)) such that \(p_x=0\) or \(p_y=0\). In order to find it, you are allowed to ask at most \(2n\) queries.In one query, you give two inte... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^4\)).After reading the integer \(n\) for each test case, you sho... | In the first test, the interaction proceeds as follows. SolutionJuryExplanation\(\texttt{2}\)There are 2 test cases.\(\texttt{2}\)In the first test case, the hidden permutation is \([1,0]\), with length \(2\).\(\texttt{? 1 2}\)\(\texttt{1}\)The solution requests \(\gcd(p_1,p_2)\), and the jury responds with \(1\).\(\te... | Input: 2 2 1 1 5 2 4 1 | Output: ? 1 2 ! 1 2 ? 1 2 ? 2 3 ! 3 3 | Hard | 3 | 702 | 438 | 0 | 17 | |
1,768 | C | 1768C | C. Elemental Decompress | 1,300 | constructive algorithms; greedy; implementation; sortings | You are given an array \(a\) of \(n\) integers.Find two permutations\(^\dagger\) \(p\) and \(q\) of length \(n\) such that \(\max(p_i,q_i)=a_i\) for all \(1 \leq i \leq n\) or report that such \(p\) and \(q\) do not exist.\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \l... | For each test case, if there do not exist \(p\) and \(q\) that satisfy the conditions, output ""NO"" (without quotes).Otherwise, output ""YES"" (without quotes) and then output \(2\) lines. The first line should contain \(n\) integers \(p_1,p_2,\ldots,p_n\) and the second line should contain \(n\) integers \(q_1,q_2,\l... | In the first test case, \(p=q=[1]\). It is correct since \(a_1 = max(p_1,q_1) = 1\).In the second test case, \(p=[1,3,4,2,5]\) and \(q=[5,2,3,1,4]\). It is correct since: \(a_1 = \max(p_1, q_1) = \max(1, 5) = 5\), \(a_2 = \max(p_2, q_2) = \max(3, 2) = 3\), \(a_3 = \max(p_3, q_3) = \max(4, 3) = 4\), \(a_4 = \max(p_4, q_... | Input: 31155 3 4 2 521 1 | Output: YES 1 1 YES 1 3 4 2 5 5 2 3 1 4 NO | Easy | 4 | 559 | 453 | 531 | 17 |
1,344 | E | 1344E | E. Train Tracks | 3,100 | data structures; trees | That's right. I'm a Purdue student, and I shamelessly wrote a problem about trains.There are \(n\) stations and \(m\) trains. The stations are connected by \(n-1\) one-directional railroads that form a tree rooted at station \(1\). All railroads are pointed in the direction from the root station \(1\) to the leaves. A ... | The first line contains two integers \(n\) and \(m\) (\(1\le n,m\le 10^5\)) β the number of stations and trains, respectively.The next \(n-1\) lines describe the railroads. The \(i\)-th line contains three integers \(u,v,d\) (\(1\le u,v\le n\), \(1\le d\le 10^9\)), denoting a railroad from station \(u\) to station \(v\... | Output two integers: the latest possible time of the first explosion (or \(-1\) if it is possible to never have an explosion) and the minimum number of switch changes to achieve it. | For the first test, here's an example timeline: At time \(1\), train \(1\) enters station \(1\). We switch station \(1\) to point to station \(2\). Train \(1\) is directed to station \(2\). At time \(2\), train \(2\) enters station \(1\), and train \(1\) enters station \(2\), where it stops permanently. We switch stati... | Input: 5 4 1 2 1 1 3 2 3 4 1 3 5 3 2 1 4 2 2 6 5 10 | Output: -1 6 | Master | 2 | 1,808 | 784 | 181 | 13 |
1,907 | F | 1907F | F. Shift and Reverse | 1,800 | greedy; sortings | Given an array of integers \(a_1, a_2, \ldots, a_n\). You can make two types of operations with this array: Shift: move the last element of array to the first place, and shift all other elements to the right, so you get the array \(a_n, a_1, a_2, \ldots, a_{n-1}\). Reverse: reverse the whole array, so you get the array... | The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Descriptions of test cases follow.The first line of each test case contains an integer \(n\) (\(1\le n \le 10^5\)) β size of the array. The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots,... | For each test case output the number \(k\), the minimal number of operations you need to sort the array. If it is impossible to sort the array using these operations, output \(-1\). | In the first test case of the example, to sort the array [\(3, 2, 1, 5, 4\)] you need to perform \(3\) operations: Shift to obtain the array [\(4, 3, 2, 1, 5\)]; Shift to obtain the array [\(5, 4, 3, 2, 1\)]; Reverse to obtain the array [\(1, 2, 3, 4, 5\)]. In the third test case of the example, it can be shown that it... | Input: 1153 2 1 5 451 1 2 1 143 7 10 551 2 3 4 525 133 4 154 1 3 4 435 1 142 5 5 452 2 1 1 225 5 | Output: 3 2 -1 0 1 1 3 1 2 2 0 | Medium | 2 | 476 | 464 | 181 | 19 |
2,057 | H | 2057H | H. Coffee Break | 3,500 | data structures; greedy; math | There are very long classes in the T-Generation. In one day, you need to have time to analyze the training and thematic contests, give a lecture with new material, and, if possible, also hold a mini-seminar. Therefore, there is a break where students can go to drink coffee and chat with each other.There are a total of ... | The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases.In the first line of each test case, there is an integer \(n\) (\(1 \le n \le 10^6\)).In the second line of each test case, there are integers \(a_1, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the number of students around... | For each test case, output \(n\) integers \(b_1, \ldots, b_n\), where \(b_i\) is the maximum number of students that can be around coffee machines number \(i\). | Let's analyze the first test case: To maximize the number of students at the \(1\)-st coffee machine, all that needs to be done is nothing. To maximize the number of students at the \(2\)-nd coffee machine, it is sufficient to turn off the lights in the \(1\)-st room once. | Input: 328 052 2 2 2 250 0 9 0 0 | Output: 8 4 4 5 4 5 4 4 6 9 6 4 | Master | 3 | 1,874 | 452 | 160 | 20 |
240 | E | 240E | E. Road Repairs | 2,800 | dfs and similar; graphs; greedy | A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them are in good condition. For each road we know whether it needs repairing or not. If a road needs... | The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of cities and the number of roads in Berland.Next m lines contain three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 0 β€ ci β€ 1), describing the road from city ai to city bi. If ci equals 0, than the given road i... | If even after all roads are repaired, it is still impossible to get to some city from the capital, print - 1. Otherwise, on the first line print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces.The roads are numbered starting from ... | Input: 3 21 3 03 2 1 | Output: 12 | Master | 3 | 1,195 | 479 | 660 | 2 | |
757 | G | 757G | G. Can Bash Save the Day? | 3,400 | data structures; divide and conquer; graphs; trees | Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.Initially, Meowth gives Bash a... | The first line contains two integers n and q (1 β€ n β€ 2Β·105, 1 β€ q β€ 2Β·105) β the number of nodes in the tree and the number of queries, respectively.The next line contains n space-separated integers β the sequence a1, a2, ..., an which is a permutation of 1, 2, ..., n.Each of the next n - 1 lines contain three space-s... | For each query of type 1, output a single integer in a separate line, denoting the answer to the query. | In the sample, the actual queries are the following: 1 1 5 4 1 1 3 3 2 3 2 2 1 1 3 3 | Input: 5 54 5 1 3 24 2 41 3 94 1 44 5 211 5 4122 20 20238239136 38 38 | Output: 233728 | Master | 4 | 759 | 1,223 | 103 | 7 |
997 | D | 997D | D. Cycles in product | 2,900 | combinatorics; divide and conquer; trees | Consider a tree (that is, an undirected connected graph without loops) \(T_1\) and a tree \(T_2\). Let's define their cartesian product \(T_1 \times T_2\) in a following way.Let \(V\) be the set of vertices in \(T_1\) and \(U\) be the set of vertices in \(T_2\).Then the set of vertices of graph \(T_1 \times T_2\) is \(... | First line of input contains three integers β \(n_1\), \(n_2\) and \(k\) (\(2 \le n_1, n_2 \le 4000\), \(2 \le k \le 75\)) β number of vertices in the first tree, number of vertices in the second tree and the cycle length respectively.Then follow \(n_1 - 1\) lines describing the first tree. Each of this lines contains ... | Print one integer β number of cycles modulo \(998244353\). | The following three pictures illustrate graph, which are products of the trees from sample tests.In the first example, the list of cycles of length \(2\) is as follows: Β«ABΒ», Β«BAΒ» Β«BCΒ», Β«CBΒ» Β«ADΒ», Β«DAΒ» Β«CDΒ», Β«DCΒ» | Input: 2 2 21 21 2 | Output: 8 | Master | 3 | 1,267 | 542 | 58 | 9 |
1,718 | E | 1718E | E. Impressionism | 3,500 | constructive algorithms; graphs; implementation; math | Burenka has two pictures \(a\) and \(b\), which are tables of the same size \(n \times m\). Each cell of each painting has a color β a number from \(0\) to \(2 \cdot 10^5\), and there are no repeating colors in any row or column of each of the two paintings, except color \(0\).Burenka wants to get a picture \(b\) from ... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 2 \cdot 10^5\)) β the sizes of the table.The \(i\)-th of the next \(n\) lines contains \(m\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\) (\(0 \leq a_{i,j} \leq 2 \cdot 10^5\)) β the colors of the \(i\)-th row of picture \(a\). It is gua... | In the first line print the number \(-1\) if it is impossible to achieve what Burenka wants, otherwise print the number of actions in your solution \(k\) (\(0 \le k \le 2 \cdot 10^5\)). It can be proved that if a solution exists, then there exists a solution where \(k \le 2 \cdot 10^5\).In the next \(k\) lines print th... | Input: 3 3 1 0 2 0 0 0 2 0 1 2 0 1 0 0 0 1 0 2 | Output: 1 1 1 3 | Master | 4 | 677 | 704 | 556 | 17 | |
1,845 | B | 1845B | B. Come Together | 900 | geometry; implementation; math | Bob and Carol hanged out with Alice the whole day, but now it's time to go home. Alice, Bob and Carol live on an infinite 2D grid in cells \(A\), \(B\), and \(C\) respectively. Right now, all of them are in cell \(A\).If Bob (or Carol) is in some cell, he (she) can move to one of the neighboring cells. Two cells are ca... | The first line contains the single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(x_A\) and \(y_A\) (\(1 \le x_A, y_A \le 10^8\)) β the position of cell \(A\), where both Bob and Carol are right now.The second line contains two integers \(x_B\) a... | For each test case, print the single integer β the maximum number of cells Bob and Carol can walk together if each of them goes home along one of the shortest paths. | In all pictures, red color denotes cells belonging only to Bob's path, light blue color β cells belonging only to Carol's path, and dark blue color β cells belonging to both paths.One of the optimal routes for the first test case is shown below: Bob's route contains \(5\) cells, Carol's route β \(7\) cells, and they wi... | Input: 33 11 36 45 22 27 21 14 35 5 | Output: 3 1 6 | Beginner | 3 | 848 | 646 | 165 | 18 |
1,970 | A3 | 1970A3 | A3. Balanced Unshuffle (Hard) | 2,400 | constructive algorithms; trees | The only difference with the medium version is the maximum length of the input.A parentheses sequence is a string consisting of characters ""("" and "")"", for example ""(()(("".A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operati... | The only line of input contains a string \(s\) consisting only of characters ""("" and "")"". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding \(500\,000\). | Print the balanced parentheses sequence \(t\) such that the balanced shuffle of \(t\) is equal to \(s\). It is guaranteed that the answer always exists and is unique. | Input: ()(()()) | Output: (()(())) | Expert | 2 | 2,185 | 211 | 166 | 19 | |
1,207 | E | 1207E | E. XOR Guessing | 1,900 | bitmasks; interactive; math | This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You m... | To give the answer, your program should print one line \(!\) \(x\) with a line break in the end. After that, it should flush the output and terminate gracefully. | The example of interaction is not correct β you should sumbit exactly \(100\) integers in each query. Everything else is correct.Hacks are forbidden in this problem. | Input: 0 32 | Output: ? 3 5 6 ? 32 24 37 ! 5 | Hard | 3 | 1,185 | 0 | 161 | 12 | |
1,847 | E | 1847E | E. Triangle Platinum? | 2,900 | brute force; combinatorics; implementation; interactive; math; probabilities | This is an interactive problem.Made in Heaven is a rather curious Stand. Of course, it is (arguably) the strongest Stand in existence, but it is also an ardent puzzle enjoyer. For example, it gave Qtaro the following problem recently:Made in Heaven has \(n\) hidden integers \(a_1, a_2, \dots, a_n\) (\(3 \le n \le 5000\... | In the first example, the interaction process happens as follows: StdinStdoutExplanation3Read \(n = 3\). There are \(3\) hidden integers? 1 2 3Ask for the area formed by \(a_1\), \(a_2\) and \(a_3\)63Received \(16\Delta^2 = 63\). So the area \(\Delta = \sqrt{\frac{63}{16}} \approx 1.984313\)! -1Answer that there is no ... | Input: 3 63 | Output: ? 1 2 3 ! -1 | Master | 6 | 1,285 | 0 | 0 | 18 | ||
536 | E | 536E | E. Tavas on the Path | 3,100 | data structures; divide and conquer; trees | Tavas lives in Tavaspolis. Tavaspolis has n cities numbered from 1 to n connected by n - 1 bidirectional roads. There exists a path between any two cities. Also each road has a length. Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like s = s1s2... sk, T(s) is its Goodness... | The first line of input contains integers n and q (2 β€ n β€ 105 and 1 β€ q β€ 105).The next line contains n - 1 space separated integers f1, f2, ..., fn - 1 (|fi| β€ 1000).The next n - 1 lines contain the details of the roads. Each line contains integers v, u and w and it means that there's a road between cities v and u of... | Print the answer of each query in a single line. | Input: 2 3101 2 31 2 21 2 31 2 4 | Output: 10100 | Master | 3 | 943 | 488 | 48 | 5 | |
1,109 | C | 1109C | C. Sasha and a Patient Friend | 2,800 | binary search; data structures; implementation | Fedya and Sasha are friends, that's why Sasha knows everything about Fedya.Fedya keeps his patience in an infinitely large bowl. But, unlike the bowl, Fedya's patience isn't infinite, that is why let \(v\) be the number of liters of Fedya's patience, and, as soon as \(v\) becomes equal to \(0\), the bowl will burst imm... | The first line contans one integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.Each of the next \(q\) lines have one of the following formats: 1 t s (\(1 \le t \le 10^9\), \(-10^9 \le s \le 10^9\)), means that a new event is added, which means that starting from the \(t\)-th second the tap's speed will be equa... | For each query of the \(3\)-rd type, print in a new line the moment when the bowl will burst or print \(-1\) if it won't happen.Your answer will be considered correct if it's absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepte... | In the first example all the queries of the \(3\)-rd type cover all the events, it's simulation is following: | Input: 6 1 2 1 1 4 -3 3 1 6 1 3 1 6 3 3 1 6 4 3 1 6 5 | Output: 5 5.666667 6 -1 | Master | 3 | 1,632 | 1,010 | 384 | 11 |
348 | D | 348D | D. Turtles | 2,500 | dp; matrices | You've got a table of size n Γ m. We'll consider the table rows numbered from top to bottom 1 through n, and the columns numbered from left to right 1 through m. Then we'll denote the cell in row x and column y as (x, y).Initially cell (1, 1) contains two similar turtles. Both turtles want to get to cell (n, m). Some c... | The first line contains two integers n, m (2 β€ n, m β€ 3000). Each of the following n lines contains m characters describing the table. The empty cells are marked by characters ""."", the cells with obstacles are marked by ""#"".It is guaranteed that the upper left and the lower right cells are empty. | In a single line print a single integer β the number of pairs of non-intersecting paths from cell (1, 1) to cell (n, m) modulo 1000000007 (109 + 7). | Input: 4 5......###..###...... | Output: 1 | Expert | 2 | 1,043 | 301 | 148 | 3 | |
1,909 | G | 1909G | G. Pumping Lemma | 3,000 | hashing; strings | Tanchiky & Siromaru - Crystal Gravityβ You are given two strings \(s\), \(t\) of length \(n\), \(m\), respectively. Both strings consist of lowercase letters of the English alphabet.Count the triples \((x, y, z)\) of strings such that the following conditions are true: \(s = x+y+z\) (the symbol \(+\) represents the conc... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n < m \leq 10^7\)) β the length of the strings \(s\) and \(t\), respectively.The second line contains the string \(s\) of length \(n\), consisting of lowercase letters of the English alphabet.The third line contains the string \(t\) of length \(m\), consist... | Output a single integer: the number of valid triples \((x, y, z)\). | In the first test case, the only valid triple is \((x, y, z) = (\texttt{""a""}, \texttt{""bc""}, \texttt{""d""})\). In fact, \(\texttt{""abcd""} = \texttt{""a""} + \texttt{""bc""} + \texttt{""d""}\); \(\texttt{""abcbcbcd""} = \texttt{""a""} + \texttt{""bc""} + \texttt{""bc""} + \texttt{""bc""} + \texttt{""d""}\). In th... | Input: 4 8abcdabcbcbcd | Output: 1 | Master | 2 | 412 | 369 | 67 | 19 |
427 | B | 427B | B. Prison Transfer | 1,100 | data structures; implementation | The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime ... | The first line of input will contain three space separated integers n (1 β€ n β€ 2Β·105), t (0 β€ t β€ 109) and c (1 β€ c β€ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109. | Print a single integer β the number of ways you can choose the c prisoners. | Input: 4 3 32 3 1 1 | Output: 2 | Easy | 2 | 875 | 306 | 75 | 4 | |
717 | H | 717H | H. Pokermon League challenge | 2,400 | math; probabilities | Welcome to the world of Pokermon, yellow little mouse-like creatures, who absolutely love playing poker! Yeah, rightβ¦ In the ensuing Pokermon League, there are n registered Pokermon trainers, and t existing trainer teams each of which belongs to one of two conferences. Since there is a lot of jealousy between trainers,... | The first line of the input contains two integer n (4 β€ n β€ 50 000) and e (2 β€ e β€ 100 000) β the total number of Pokermon trainers and the number of pairs of trainers who hate each other.Pokermon trainers are numbered from 1 to n. Next e lines contain two integers a and b (1 β€ a, b β€ n) indicating that Pokermon traine... | Print two lines. The first line should contain n numbers, specifying for each trainer the team he is in.The second line should contain T numbers, specifying the conference for each team (1 or 2). | Input: 4 31 22 34 1161 2 3 4 5 6 7 8 9 10 11 12 13 14 16 15162 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18162 3 4 5 6 7 8 9 10 11 12 13 14 15 18 19161 2 3 4 5 6 7 8 9 10 11 12 13 14 16 19 | Output: 16 15 19 14 2 2 2 1 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 | Expert | 2 | 974 | 908 | 195 | 7 | |
66 | C | 66C | C. Petya and File System | 1,800 | data structures; implementation | Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested fo... | Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file... | Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as fol... | In the first sample we have one folder on the ""C"" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.In the second sample we have several different folders. Consider the ""folder1"" folder on the ""C"" disk. This fol... | Input: C:\folder1\file1.txt | Output: 0 1 | Medium | 2 | 1,764 | 392 | 325 | 0 |
954 | F | 954F | F. Runner's Problem | 2,100 | dp; matrices; sortings | You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to: (i - 1, j + 1) β only if i > 1, (i, j + 1), or (... | The first line contains two integers n and m (1 β€ n β€ 104, 3 β€ m β€ 1018) β the number of obstacles and the number of columns in the matrix, respectively.Then n lines follow, each containing three integers ak, lk and rk (1 β€ ak β€ 3, 2 β€ lk β€ rk β€ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk β€ j β€... | Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0. | Input: 2 51 3 42 2 3 | Output: 2 | Hard | 3 | 626 | 373 | 161 | 9 | |
2,070 | E | 2070E | E. Game with Binary String | 2,200 | constructive algorithms; data structures; divide and conquer; games; greedy; math | Consider the following game. Two players have a binary string (a string consisting of characters 0 and/or 1). The players take turns, the first player makes the first turn. During a player's turn, he or she has to choose exactly two adjacent elements of the string and remove them (the first element and the last element... | The first line contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line contains the string \(s\), consisting of exactly \(n\) characters. Each character of the string is either 0 or 1. | Print one integer β the number of substrings such that, if the game is played on that substring, the first player wins. | In the first example, the following substrings are winning for the first player (\(s[l:r]\) denotes \(s_l s_{l+1} \dots s_r\)): \(s[1:2]\); \(s[1:3]\); \(s[1:7]\); \(s[2:4]\); \(s[2:8]\); \(s[3:5]\); \(s[4:5]\); \(s[4:6]\); \(s[5:7]\); \(s[6:8]\); \(s[7:8]\); \(s[7:9]\). | Input: 100010010011 | Output: 12 | Hard | 6 | 1,145 | 202 | 119 | 20 |
285 | E | 285E | E. Positions in Permutations | 2,600 | combinatorics; dp; math | Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.We'll call position i (1 β€ i β€ n) in permutation p1, p2,... | The single line contains two space-separated integers n and k (1 β€ n β€ 1000, 0 β€ k β€ n). | Print the number of permutations of length n with exactly k good positions modulo 1000000007 (109 + 7). | The only permutation of size 1 has 0 good positions.Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions.Permutations of size 3: (1, 2, 3) β 0 positions β 2 positions β 2 positions β 2 positions β 2 positions (3, 2, 1) β 0 positions | Input: 1 0 | Output: 1 | Expert | 3 | 473 | 88 | 103 | 2 |
569 | A | 569A | A. Music | 1,500 | implementation; math | Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk.Unfortunately, internet is not that fast in the city of Ekaterinozavodsk and the song takes a lot of time to download. But Lesha is quite im... | The single line contains three integers T, S, q (2 β€ q β€ 104, 1 β€ S < T β€ 105). | Print a single integer β the number of times the song will be restarted. | In the first test, the song is played twice faster than it is downloaded, which means that during four first seconds Lesha reaches the moment that has not been downloaded, and starts the song again. After another two seconds, the song is downloaded completely, and thus, Lesha starts the song twice.In the second test, t... | Input: 5 2 2 | Output: 2 | Medium | 2 | 911 | 79 | 72 | 5 |
225 | D | 225D | D. Snake | 2,200 | bitmasks; dfs and similar; graphs; implementation | Let us remind you the rules of a very popular game called ""Snake"" (or sometimes ""Boa"", ""Python"" or ""Worm"").The game field is represented by an n Γ m rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable.You control a snake, the snake consist... | The first line contains two space-separated integers n and m (1 β€ n, m β€ 15) β the number of rows and columns of the game field.Next n lines describe the game field. Each of these lines contains m characters. Character ""#"" represents a wall, ""."" is a passable square, ""@"" is an apple. The snake's first segment is ... | Print a single integer to the output β the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1. | Input: 4 5##.....1#@432#....#. | Output: 4 | Hard | 4 | 1,517 | 707 | 139 | 2 | |
117 | C | 117C | C. Cycle | 2,000 | dfs and similar; graphs | A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u β v) exists either an edge going from u to v, or an edge from v to u.You are given a tournament consisting of n vertexes. Your task is to find there a c... | The first line contains an integer n (1 β€ n β€ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line.It is guaranteed that the given graph is a tournam... | Print three distinct vertexes of the graph a1, a2, a3 (1 β€ ai β€ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or ""-1"", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. | Input: 50010010000010011110111000 | Output: 1 3 2 | Hard | 2 | 341 | 381 | 225 | 1 | |
161 | C | 161C | C. Abracadabra | 2,400 | divide and conquer | Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm: On the first step the string consists of a single character ""a"". On the k-th step Polycarpus concatenates two copies of the string obtained on the (k - 1)-th step, while inserting the k-th character of the alpha... | The input consists of a single line containing four integers l1, r1, l2, r2 (1 β€ li β€ ri β€ 109, i = 1, 2). The numbers are separated by single spaces. li and ri give the indices of the first and the last characters of the i-th chosen substring, correspondingly (i = 1, 2). The characters of string abracadabra are number... | Print a single number β the length of the longest common substring of the given strings. If there are no common substrings, print 0. | In the first sample the first substring is ""acab"", the second one is ""abac"". These two substrings have two longest common substrings ""ac"" and ""ab"", but we are only interested in their length β 2.In the second sample the first substring is ""a"", the second one is ""c"". These two substrings don't have any commo... | Input: 3 6 1 4 | Output: 2 | Expert | 1 | 1,686 | 339 | 132 | 1 |
2,104 | A | 2104A | A. Three Decks | 800 | math | Monocarp placed three decks of cards in a row on the table. The first deck consists of \(a\) cards, the second deck consists of \(b\) cards, and the third deck consists of \(c\) cards, with the condition \(a < b < c\).Monocarp wants to take some number of cards (at least one, but no more than \(c\)) from the third deck... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains three integers \(a, b\), and \(c\) (\(1 \le a, b, c \le 10^8\)) β the number of cards in the first, second, and third decks, respectively.Additional constraint on the input: \(a < b ... | For each test case, output ""YES"" (without quotes) if Monocarp can make the number of cards in all three decks equal using the described operation. Otherwise, output ""NO"" (without quotes). | In the first test case, Monocarp has to take \(4\) cards from the third deck, put \(3\) cards in the first deck, and \(1\) card in the second deck. Thus, there will be \(6\) cards in all three decks.In the second test case, it is impossible to make the number of cards in all three decks equal.In the third test case, Mo... | Input: 43 5 1012 20 303 5 71 5 6 | Output: YES NO YES NO | Beginner | 1 | 663 | 326 | 191 | 21 |
1,731 | D | 1731D | D. Valiant's New Map | 1,700 | binary search; brute force; data structures; dp; two pointers | Game studio ""DbZ Games"" wants to introduce another map in their popular game ""Valiant"". This time, the map named ""Panvel"" will be based on the city of Mumbai.Mumbai can be represented as \(n \times m\) cellular grid. Each cell \((i, j)\) (\(1 \le i \le n\); \(1 \le j \le m\)) of the grid is occupied by a cuboid b... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 1000\)). Description of the test cases follows.The first line of each test case contains two positive integers \(n\) and \(m\) (\(1 \le n \le m\); \(1 \leq n \cdot m \leq 10^6\)).The \(i\)-th of next \(n\) li... | For each test case, print the maximum side length \(l\) of the square DbZ Games can choose. | In the first test case, we can choose the square of side \(l = 2\) (i. e. the whole grid) since the heights of all buildings are greater than or equal to \(2\).In the second test case, we can only choose the side as \(1\), so the answer is \(1\).In the third test case, there are no squares of size \(2\) that have all b... | Input: 42 22 34 51 31 2 32 34 4 32 1 45 61 9 4 6 5 810 9 5 8 11 624 42 32 8 11 123 1 9 69 13 313 22 60 12 14 17 | Output: 2 1 1 3 | Medium | 5 | 647 | 549 | 91 | 17 |
901 | B | 901B | B. GCD of Polynomials | 2,200 | constructive algorithms; math | Suppose you have two polynomials and . Then polynomial can be uniquely represented in the following way:This can be done using long division. Here, denotes the degree of polynomial P(x). is called the remainder of division of polynomial by polynomial , it is also denoted as . Since there is a way to divide polynomials ... | You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach. | Print two polynomials in the following format.In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial. In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading. The degree of the first polynomial should be greater than the deg... | In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is(x2 - 1, x) β (x, - 1) β ( - 1, 0).There are two steps in it. | Input: 1 | Output: 10 101 | Hard | 2 | 1,274 | 104 | 579 | 9 |
1,658 | D2 | 1658D2 | D2. 388535 (Hard Version) | 2,300 | bitmasks; brute force; data structures; math | This is the hard version of the problem. The difference in the constraints between both versions are colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially perform the following steps: First, Gojou chooses \(2\) int... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.In the first line of each test case contains two integers \(l\) and \(r\) (\(\color{red}{\boldsymbol{0} \boldsymbol{\le} \boldsymbol{l}} \le r < 2^{17}\)).The second line contains \(r - l + 1\) space-seperated integers of... | For each test case print an integer \(x\). If there are multiple answers, print any. | In the first test case, the original array is \([7, 6, 5, 4]\). In the second test case, the original array is \([4, 7, 6, 5]\).In the third test case, the original array is \([3, 1, 2]\). | Input: 34 73 2 1 04 74 7 6 51 30 2 1 | Output: 4 0 3 | Expert | 4 | 933 | 513 | 84 | 16 |
549 | G | 549G | G. Happy Line | 2,200 | constructive algorithms; greedy; sortings | Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, s... | The first line contains integer n (1 β€ n β€ 200 000) β the number of residents who stand in the line.The second line contains n space-separated integers ai (0 β€ ai β€ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the... | If it is impossible to make all the residents happy, print "":("" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. | In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result.In the third sample the first person c... | Input: 211 8 | Output: 9 10 | Hard | 3 | 1,121 | 326 | 298 | 5 |
761 | A | 761A | A. Dasha and Stairs | 1,000 | brute force; constructive algorithms; implementation; math | On her way to programming school tiger Dasha faced her first test β a huge staircase! The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values β the number o... | In the only line you are given two integers a, b (0 β€ a, b β€ 100) β the number of even and odd steps, accordingly. | In the only line print ""YES"", if the interval of steps described above exists, and ""NO"" otherwise. | In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps β 2 and 4, and three odd: 1, 3 and 5. | Input: 2 3 | Output: YES | Beginner | 4 | 500 | 114 | 102 | 7 |
913 | A | 913A | A. Modular Exponentiation | 900 | implementation; math | The following problem is well-known: given integers n and m, calculate , where 2n = 2Β·2Β·...Β·2 (n factors), and denotes the remainder of division of x by y.You are asked to solve the ""reverse"" problem. Given integers n and m, calculate . | The first line contains a single integer n (1 β€ n β€ 108).The second line contains a single integer m (1 β€ m β€ 108). | Output a single integer β the value of . | In the first example, the remainder of division of 42 by 24 = 16 is equal to 10.In the second example, 58 is divisible by 21 = 2 without remainder, and the answer is 0. | Input: 442 | Output: 10 | Beginner | 2 | 238 | 115 | 40 | 9 |
1,016 | B | 1016B | B. Segment Occurrences | 1,300 | brute force; implementation | You are given two strings \(s\) and \(t\), both consisting only of lowercase Latin letters.The substring \(s[l..r]\) is the string which is obtained by taking characters \(s_l, s_{l + 1}, \dots, s_r\) without changing the order.Each of the occurrences of string \(a\) in a string \(b\) is a position \(i\) (\(1 \le i \le... | The first line contains three integer numbers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 10^3\), \(1 \le q \le 10^5\)) β the length of string \(s\), the length of string \(t\) and the number of queries, respectively.The second line is a string \(s\) (\(|s| = n\)), consisting only of lowercase Latin letters.The third line... | Print \(q\) lines β the \(i\)-th line should contain the answer to the \(i\)-th query, that is the number of occurrences of string \(t\) in a substring \(s[l_i..r_i]\). | In the first example the queries are substrings: ""cod"", ""deforces"", ""fo"" and ""for"", respectively. | Input: 10 3 4codeforcesfor1 33 105 65 7 | Output: 0101 | Easy | 2 | 569 | 544 | 168 | 10 |
542 | C | 542C | C. Idempotent functions | 2,000 | constructive algorithms; graphs; math | Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., n} is such function , that for any the formula g(g(x)) = g(x) holds.Let's denote as f(k)(x) the function f applied k times to the value x. More formally, f(1)(x) = f(x), f(k)(x) = f(f(k - 1)(x)) for each k > 1.Y... | In the first line of the input there is a single integer n (1 β€ n β€ 200) β the size of function f domain.In the second line follow f(1), f(2), ..., f(n) (1 β€ f(i) β€ n for each 1 β€ i β€ n), the values of a function. | Output minimum k such that function f(k)(x) is idempotent. | In the first sample test function f(x) = f(1)(x) is already idempotent since f(f(1)) = f(1) = 1, f(f(2)) = f(2) = 2, f(f(3)) = f(3) = 2, f(f(4)) = f(4) = 4.In the second sample test: function f(x) = f(1)(x) isn't idempotent because f(f(1)) = 3 but f(1) = 2; function f(x) = f(2)(x) is idempotent since for any x it is tr... | Input: 41 2 2 4 | Output: 1 | Hard | 3 | 438 | 213 | 58 | 5 |
223 | E | 223E | E. Planar Graph | 3,000 | flows; geometry; graphs | A graph is called planar, if it can be drawn in such a way that its edges intersect only at their vertexes.An articulation point is such a vertex of an undirected graph, that when removed increases the number of connected components of the graph.A bridge is such an edge of an undirected graph, that when removed increas... | The first line contains two space-separated integers n and m (3 β€ n, m β€ 105) β the number of vertexes and edges of the graph. Next m lines contain the edges of the graph: the i-th line contains two space-separated integers ui and vi (1 β€ ui, vi β€ n) β the numbers of vertexes, connecting the i-th edge. The next n lines... | For each query print a single integer β the number of vertexes inside the cycle or on it. Print the answers in the order, in which the queries follow in the input. Separate the numbers by spaces. | Input: 3 31 22 33 10 01 00 113 1 2 3 | Output: 3 | Master | 3 | 857 | 1,307 | 195 | 2 | |
713 | D | 713D | D. Animals and Puzzle | 2,700 | binary search; data structures | Owl Sonya gave a huge lake puzzle of size n Γ m to hedgehog Filya as a birthday present. Friends immediately started to assemble the puzzle, but some parts of it turned out to be empty β there was no picture on them. Parts with picture on it are denoted by 1, while empty parts are denoted by 0. Rows of the puzzle are n... | The first line of the input contains two integers n and m (1 β€ n, m β€ 1000) β sizes of the puzzle.Each of the following n lines contains m integers aij. Each of them is equal to 1 if the corresponding cell contains a picture and 0 if it's empty.Next line contains an integer t (1 β€ t β€ 1 000 000) β the number of queries... | Print t lines. The i-th of them should contain the maximum size of the square consisting of 1-s and lying fully inside the query rectangle. | Input: 3 41 1 0 10 1 1 00 1 1 051 1 2 32 1 3 23 2 3 41 1 3 41 2 3 4 | Output: 11122 | Master | 2 | 1,011 | 530 | 139 | 7 | |
1,675 | A | 1675A | A. Food for Animals | 800 | greedy; math | In the pet store on sale there are: \(a\) packs of dog food; \(b\) packs of cat food; \(c\) packs of universal food (such food is suitable for both dogs and cats). Polycarp has \(x\) dogs and \(y\) cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his c... | The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input.Then \(t\) lines are given, each containing a description of one test case. Each description consists of five integers \(a, b, c, x\) and \(y\) (\(0 \le a,b,c,x,y \le 10^8\)). | For each test case in a separate line, output: YES, if suitable food can be bought for each of \(x\) dogs and for each of \(y\) cats; NO else. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). | Input: 71 1 4 2 30 0 0 0 05 5 0 4 61 1 1 1 150000000 50000000 100000000 100000000 1000000000 0 0 100000000 1000000001 3 2 2 5 | Output: YES YES NO YES YES NO NO | Beginner | 2 | 372 | 288 | 268 | 16 | |
1,779 | D | 1779D | D. Boris and His Amazing Haircut | 1,700 | constructive algorithms; data structures; dp; dsu; greedy; sortings | Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy.His current hair can be described by an array \(a_1,a_2,\ldots, a_n\), where \(a_i\) is the height of the hair standing at position \(i\). His desired haircut can be described by an array \(b... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 20\,000\)). The description of the test cases follows.The first line of each test case contains a positive integer \(n\) (\(3\leq n\leq 2\cdot 10^5\)) β the length of arrays \(a\) and \(b\).The second line of... | For each test case, print ""YES"" if the barber can cut Boris's hair as desired. Otherwise, print ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, Boris's hair is initially \([3,3,3]\). Let us describe a sequence of \(2\) operations the barber can perform: The barber uses the razor with size \(1\) on the segment \([2,2]\); hence Boris's hair becomes \([3,1,3]\). The barber uses the razor with size \(2\) on the segment \([1,3]\); hence Bori... | Input: 733 3 32 1 221 263 4 4 6 3 43 1 2 3 2 333 2 3101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10101 2 3 4 5 6 7 8 9 1031 1 11 1 2124 2 4 3 1 5 6 3 5 6 2 1137 9 4 5 3 3 3 6 8 10 3 2 55 3 1 5 3 2 2 5 8 5 1 1 581 5 3 5 4 2 3 1137 9 4 5 3 3 3 6 8 10 3 2 55 3 1 5 3 2 2 5 8 5 1 1 571 5 3 4 2 3 1319747843 2736467 93857839720398... | Medium | 6 | 1,134 | 964 | 266 | 17 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.