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
7
D
7D
D. Palindrome Degree
2,200
hashing; strings
String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length are (k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, ""abaaba"" has de...
The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5Β·106. The string is case-sensitive.
Output the only number β€” the sum of the polindrome degrees of all the string's prefixes.
Input: a2A | Output: 1
Hard
2
436
179
88
0
670
B
670B
B. Game of Robots
1,000
implementation
In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier β€” an integer from 1 to 109.At some moment, robots decided to play the game ""Snowball"". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. Aft...
The first line contains two positive integers n and k (1 ≀ n ≀ 100 000, 1 ≀ k ≀ min(2Β·109, nΒ·(n + 1) / 2).The second line contains the sequence id1, id2, ..., idn (1 ≀ idi ≀ 109) β€” identifiers of roborts. It is guaranteed that all identifiers are different.
Print the k-th pronounced identifier (assume that the numeration starts from 1).
In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.
Input: 2 21 2 | Output: 1
Beginner
1
699
257
80
6
382
D
382D
D. Ksenia and Pawns
2,200
dfs and similar; graphs; implementation; trees
Ksenia has a chessboard of size n Γ— m. Each cell of the chessboard contains one of the characters: ""<"", "">"", ""^"", ""v"", ""#"". The cells that contain character ""#"" are blocked. We know that all chessboard cells that touch the border are blocked.Ksenia is playing with two pawns on this chessboard. Initially, sh...
The first line contains two integers, n and m (1 ≀ n, m ≀ 2000) β€” the sizes of the board. Each of the following n lines contains m characters – the board's description. Each character is one of the characters: ""<"", "">"", ""^"", ""v"", ""#"".It is guaranteed that the border cells of the table are blocked cells (with ...
If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get.
Input: 1 1# | Output: 0
Hard
4
1,235
337
110
3
533
C
533C
C. Board Game
1,700
games; greedy; implementation; math
Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: ...
The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates.It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0).
Output the name of the winner: ""Polycarp"" or ""Vasiliy"".
In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn.
Input: 2 1 2 2 | Output: Polycarp
Medium
4
748
240
59
5
516
E
516E
E. Drazil and His Happy Friends
3,100
math; number theory
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl...
The first line contains two integer n and m (1 ≀ n, m ≀ 109).The second line contains integer b (0 ≀ b ≀ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≀ xi < n), denoting the list of indices of happy boys.The third line conatins integer g...
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
By we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this...
Input: 2 301 0 | Output: 4
Master
2
716
600
119
5
2,007
C
2007C
C. Dora and C++
1,500
math; number theory
Dora has just learned the programming language C++!However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array \(c\) with \(n\) elements. Dora has two integers \(a\) and \(b\). In one operation, she can choose one of the following things to do. Choose an...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(a\), and \(b\) (\(1 \leq n \leq 10^5\), \(1 \leq a, b \leq 10^9\)) β€” the ...
For each test case, output a single integer β€” the minimum possible range of the array after any number of operations.
In the first test case, we can increase \(c_1 = 1\) by \(a = 5\). The array \(c\) will become \([6, 3, 4, 4]\), and the range is \(3\). Note that there is more than one way to reach the answer.In the second test case, we can increase \(c_1 = 1\) by \(a = 2\) and then increase \(c_1 = 3\) by \(b = 3\). Also, we can incr...
Input: 104 5 51 3 4 44 2 31 3 4 64 7 71 1 2 63 15 91 9 53 18 121 4 57 27 3633 13 23 12 35 24 4110 6 915 5 6 9 8 2 12 15 3 82 1 10000000001 10000000006 336718728 709848696552806726 474775724 15129785 371139304 178408298 131060716 335734893 671469786138885253 70095920 456876775 9345665 214704906 375508929 | Output: 3 0 3...
Medium
2
1,039
621
117
20
1,896
E
1896E
E. Permutation Sorting
2,100
data structures; sortings
You are given a permutation\(^\dagger\) \(a\) of size \(n\). We call an index \(i\) good if \(a_i=i\) is satisfied. After each second, we rotate all indices that are not good to the right by one position. Formally, Let \(s_1,s_2,\ldots,s_k\) be the indices of \(a\) that are not good in increasing order. That is, \(s_j ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β€” the size of permutation \(a\).The second line of each test case conta...
For each test case, output a single line containing \(n\) integers where the \(i\)-th integer represents the first time that index \(i\) becomes good.
In the first test case, \(2\) and \(5\) are already in the correct position so indices \(2\) and \(5\) become good at \(0\) second. After \(1\) second, a cyclic shift will be done with \(s=[1, 3, 4]\), resulting in array \(a=[1, 2, 3, 4, 5]\). Notice that indices \(1\), \(3\) and \(4\) become good at \(1\) second.In th...
Input: 253 2 4 1 562 1 4 6 5 3 | Output: 1 0 1 1 0 2 1 2 1 0 1
Hard
2
909
507
150
18
2,029
G
2029G
G. Balanced Problem
3,000
data structures; dp
There is an array \(a\) consisting of \(n\) integers. Initially, all elements of \(a\) are equal to \(0\).Kevin can perform several operations on the array. Each operation is one of the following two types: Prefix addition β€” Kevin first selects an index \(x\) (\(1\le x\le n\)), and then for each \(1\le j\le x\), increa...
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 1000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(V\) (\(1\le n, m\le 2\cdot 10^5\), \(1\le V\le 2000\)) β€”...
For each test case, output \(V\) integers in a single line, the \(i\)-th integer denoting the maximum possible beauty after Kevin performs some new operations when \(v=i\).
In the first test case, the array \(a\) changes as follows for the initial operations: \([0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]\). For \(v=1\), it is optimal to not perform any new operations, and the beauty is \(b=c_2=2\); For \(v=2\)...
Input: 53 3 21 2 4L 3R 3L 13 3 25 1 4L 3R 3L 15 4 51 1 1 1 1L 3R 2L 5L 410 12 910 9 8 7 6 5 4 3 2 1L 2L 4R 4R 4L 6R 8L 3L 2R 1R 10L 8L 11 1 41000000000L 1 | Output: 2 6 1 9 0 1 3 5 5 0 0 0 6 25 32 35 44 51 1000000000 1000000000 1000000000 1000000000
Master
2
1,274
1,137
172
20
1,419
E
1419E
E. Decryption
2,100
constructive algorithms; implementation; math; number theory
An agent called Cypher is decrypting a message, that contains a composite number \(n\). All divisors of \(n\), which are greater than \(1\), are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common m...
The first line contains an integer \(t\) \((1 \le t \le 100)\) β€” the number of test cases. Next \(t\) lines describe each test case.In a single line of each test case description, there is a single composite number \(n\) \((4 \le n \le 10^9)\) β€” the number from the message.It's guaranteed that the total number of divis...
For each test case in the first line output the initial order of divisors, which are greater than \(1\), in the circle. In the second line output, the minimal number of moves needed to decrypt the message.If there are different possible orders with a correct answer, print any of them.
In the first test case \(6\) has three divisors, which are greater than \(1\): \(2, 3, 6\). Regardless of the initial order, numbers \(2\) and \(3\) are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes \(2, 6, 3, 6\), and every two adjacent numbers are not coprim...
Input: 3 6 4 30 | Output: 2 3 6 1 2 4 0 2 30 6 3 15 5 10 0
Hard
4
672
385
285
14
763
B
763B
B. Timofey and rectangles
2,100
constructive algorithms; geometry
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.Help Timofey to color his rectangles in 4 different c...
The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles.n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th recta...
Print ""NO"" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.Otherwise, print ""YES"" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the co...
Input: 80 0 5 32 -1 5 0-3 -4 2 -1-1 -1 2 0-3 0 0 55 2 10 37 -3 10 24 -2 7 -1 | Output: YES12232241
Hard
2
674
435
342
7
1,272
B
1272B
B. Snow Walking Robot
1,200
constructive algorithms; greedy; implementation
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell \((0, 0)\) on an infinite grid.You also have the sequence of instructions of this robot. It is written as the string \(s\) consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell \((x, y)\) right now, he c...
The first line of the input contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^4\)) β€” the number of test cases.The next \(q\) lines contain test cases. The \(i\)-th test case is given as the string \(s\) consisting of at least \(1\) and no more than \(10^5\) characters 'L', 'R', 'U' and 'D' β€” the initial sequence of ...
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions \(t\) the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several ans...
There are only two possible answers in the first test case: ""LR"" and ""RL"".The picture corresponding to the second test case: Note that the direction of traverse does not matter Another correct answer to the third test case: ""URDDLLLUURDR"".
Input: 6 LRU DURLDRUDRULRDURDDL LRUDDLRUDRUL LLLLRRRR URDUR LLL | Output: 2 LR 14 RUURDDDDLLLUUR 12 ULDDDRRRUULL 2 LR 2 UD 0
Easy
3
2,231
482
437
12
1,566
B
1566B
B. MIN-MEX Cut
800
bitmasks; constructive algorithms; dp; greedy
A binary string is a string that consists of characters \(0\) and \(1\).Let \(\operatorname{MEX}\) of a binary string be the smallest digit among \(0\), \(1\), or \(2\) that does not occur in the string. For example, \(\operatorname{MEX}\) of \(001011\) is \(2\), because \(0\) and \(1\) occur in the string at least onc...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Description of the test cases follows.Each test case contains a single binary string \(s\) (\(1 \le |s| \le 10^5\)).It's guaranteed that the sum of lengths of \(s\) over all test ...
For each test case print a single integer β€” the minimal sum of \(\operatorname{MEX}\) of all substrings that it is possible to get by cutting \(s\) optimally.
In the first test case the minimal sum is \(\operatorname{MEX}(0) + \operatorname{MEX}(1) = 1 + 0 = 1\).In the second test case the minimal sum is \(\operatorname{MEX}(1111) = 0\).In the third test case the minimal sum is \(\operatorname{MEX}(01100) = 2\).
Input: 6 01 1111 01100 101 0000 01010 | Output: 1 0 2 1 1 2
Beginner
4
946
351
158
15
1,120
D
1120D
D. Power Tree
2,500
dfs and similar; dp; dsu; graphs; greedy; trees
You are given a rooted tree with \(n\) vertices, the root of the tree is the vertex \(1\). Each vertex has some non-negative price. A leaf of the tree is a non-root vertex that has degree \(1\).Arkady and Vasily play a strange game on the tree. The game consists of three stages. On the first stage Arkady buys some non-...
The first line contains a single integer \(n\) (\(2 \le n \le 200\,000\)) β€” the number of vertices in the tree.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \le c_i \le 10^9\)), where \(c_i\) is the price of the \(i\)-th vertex.Each of the next \(n - 1\) lines contains two integers \(a\) and \(...
In the first line print two integers: the minimum possible cost \(s\) Arkady has to pay to guarantee his own win, and the number of vertices \(k\) that belong to at least one optimal set.In the second line print \(k\) distinct integers in increasing order the indices of the vertices that belong to at least one optimal ...
In the second example all sets of two vertices are optimal. So, each vertex is in at least one optimal set.
Input: 5 5 1 3 2 1 1 2 2 3 2 4 1 5 | Output: 4 3 2 4 5
Expert
6
1,286
377
324
11
375
E
375E
E. Red and Black Tree
3,000
dp; implementation; math
You have a weighted tree, consisting of n vertices. Each vertex is either painted black or is painted red. A red and black tree is called beautiful, if for any its vertex we can find a black vertex at distance at most x.The distance between two nodes is the shortest path between them.You have a red and black tree. Your...
The first line contains two integers n and x (2 ≀ n ≀ 500; 1 ≀ x ≀ 109). The next line contains n integers, each of them is either a zero or one. If the i-th number equals 1, then vertex i of the tree is black, otherwise vertex i is red. Next n - 1 lines contain the tree edges. The j-th line contains integers uj vj wj ...
Print a single integer β€” the minimum number of required swap operations.If it is impossible to get a beautiful tree at any number of operations, print -1.
Input: 3 21 0 01 2 22 3 2 | Output: 1
Master
3
698
493
154
3
704
C
704C
C. Black Widow
2,900
dp; graphs; implementation; math
Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form:Where represents logical OR and represents logical exclusive OR (XOR), and vi, j are some boolean variabl...
The first line of input contains two integers n and m (1 ≀ n, m ≀ 100 000) β€” the number of clauses and the number of variables respectively.The next n lines contain the formula. The i-th of them starts with an integer ki β€” the number of literals in the i-th clause. It is followed by ki non-zero integers ai, 1, ..., ai,...
Print the answer modulo 1 000 000 007 (109 + 7) in one line.
The equation in the first sample is:The equation in the second sample is:The equation in the third sample is:
Input: 6 72 4 -22 6 32 -7 12 -5 12 3 62 -2 -5 | Output: 48
Master
4
1,625
437
60
7
903
A
903A
A. Hungry Student Problem
900
greedy; implementation
Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one β€” 7 chunks. Ivan wants to eat exactly x chunks. Now he wonders whether he can buy exactly this...
The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of testcases.The i-th of the following n lines contains one integer xi (1 ≀ xi ≀ 100) β€” the number of chicken chunks Ivan wants to eat.
Print n lines, in i-th line output YES if Ivan can buy exactly xi chunks. Otherwise, print NO.
In the first example Ivan can buy two small portions.In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.
Input: 265 | Output: YESNO
Beginner
2
555
200
94
9
1,194
D
1194D
D. 1-2-K Game
1,700
games; math
Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip...
The first line contains the single integer T (1 ≀ T ≀ 100) β€” the number of games. Next T lines contain one game per line. All games are independent.Each of the next T lines contains two integers n and k (0 ≀ n ≀ 109, 3 ≀ k ≀ 109) β€” the length of the strip and the constant denoting the third move, respectively.
For each game, print Alice if Alice wins this game and Bob otherwise.
Input: 40 33 33 44 4 | Output: BobAliceBobAlice
Medium
2
753
311
69
11
193
E
193E
E. Fibonacci Number
2,900
brute force; math; matrices
John Doe has a list of all Fibonacci numbers modulo 1013. This list is infinite, it starts with numbers 0 and 1. Each number in the list, apart from the first two, is a sum of previous two modulo 1013. That is, John's list is made from the Fibonacci numbers' list by replacing each number there by the remainder when div...
The first line contains the single integer f (0 ≀ f < 1013) β€” the number, which position in the list we should find.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print a single number β€” the number of the first occurrence of the given number in John's list. If this number doesn't occur in John's list, print -1.
Input: 13 | Output: 7
Master
3
908
263
149
1
1,647
F
1647F
F. Madoka and Laziness
3,100
dp; greedy
Madoka has become too lazy to write a legend, so let's go straight to the formal description of the problem.An array of integers \(a_1, a_2, \ldots, a_n\) is called a hill if it is not empty and there is an index \(i\) in it, for which the following is true: \(a_1 < a_2 < \ldots < a_i > a_{i + 1} > a_{i + 2} > \ldots >...
The first line of input contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β€” array size.The second line of input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the elements of the array. It is guaranteed that all \(a_i\) are pairwise distinct.
In a single line, print exactly one number β€” the number of different pairs of maxima of the first and second subsequences among all good splits.
In the first test case there are 3 possible pairs: \((3, 4)\), \((2, 4)\), \((1, 4)\). And they are achieved with the following partitions: \([1, 2, 3], [4]\); \([4, 3], [1, 2]\); \([1], [2, 4, 3]\)
Input: 4 1 2 4 3 | Output: 3
Master
2
1,076
284
144
16
1,822
A
1822A
A. TubeTube Feed
800
brute force; implementation
Mushroom Filippov cooked himself a meal and while having his lunch, he decided to watch a video on TubeTube. He can not spend more than \(t\) seconds for lunch, so he asks you for help with the selection of video.The TubeTube feed is a list of \(n\) videos, indexed from \(1\) to \(n\). The \(i\)-th video lasts \(a_i\) ...
The first line of the input data contains a single integer \(q\) (\(1 \le q \le 1000\)) β€” the number of test cases in the test.The description of the test cases follows.The first line of a test case contains two integers \(n\) and \(t\) (\(1 \le n \le 50\), \(1 \le t \le 200\)) β€” the number of videos in the feed and se...
Output \(q\) integers, each of which is the answer to the corresponding test case. As an answer, output the index of the most entertaining video that Mushroom will have time to watch. If there are several answers, you are allowed to output any of them. Output \(-1\), if there is no video he can watch during his lunch b...
Input: 55 91 5 7 6 63 4 7 1 94 44 3 3 21 2 3 45 75 5 5 5 52 1 3 9 74 3354 71 69 9642 24 99 12 17955 6677 88 | Output: 3 2 3 -1 2
Beginner
2
788
623
325
18
1,488
G
1488G
G. Painting Numbers
2,500
*special; data structures; greedy; number theory
You are given \(n\) integers, each integer is from \(1\) to \(n\), all of them are pairwise distinct. You have to paint them red and blue (each integer should have exactly one color).The cost of painting is the number of pairs \((x, y)\) such that \(y \bmod x = 0\), \(y\) is red and \(x\) is blue.For each \(k \in [1, n...
The first line contains one integer \(n\) (\(2 \le n \le 10^5\)).
For each \(k \in [1,n]\) print one integer β€” the maximum cost of painting, if exactly \(k\) integers should be red.
Input: 6 | Output: 3 5 6 6 5 0
Expert
4
414
65
115
14
2,120
A
2120A
A. Square of Rectangles
800
geometry; math
Aryan is an ardent lover of squares but a hater of rectangles (Yes, he knows all squares are rectangles). But Harshith likes to mess with Aryan. Harshith gives Aryan three rectangles of sizes \(l_1\times b_1\), \(l_2\times b_2\), and \(l_3\times b_3\) such that \(l_3\leq l_2\leq l_1\) and \(b_3\leq b_2\leq b_1\). Aryan...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. Each test case contains a single line with \(6\) space-separated integers \(l_1, b_1, l_2, b_2, l_3\), and \(b_3\) (\(1 \leq l_3\le l_2\le l_1\le 100\), \(1\le...
For each testcase, print ""YES"" if the rectangles can be arranged to form a square; otherwise, ""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 second test case, the three rectangles \(5\times 3\), \(5\times 1\), and \(5\times 1\) can be arranged as follows to form a square. In the fourth test case, it can be proven that the rectangles can't be arranged to form a square with the given constraints.
Input: 5100 100 10 10 1 15 3 5 1 5 12 3 1 2 1 18 5 3 5 3 33 3 3 3 2 1 | Output: NO YES YES NO NO
Beginner
2
573
392
264
21
1,809
D
1809D
D. Binary String Sorting
1,800
constructive algorithms; greedy
You are given a binary string \(s\) consisting of only characters 0 and/or 1.You can perform several operations on this string (possibly zero). There are two types of operations: choose two consecutive elements and swap them. In order to perform this operation, you pay \(10^{12}\) coins; choose any element from the str...
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 the string \(s\) (\(1 \le |s| \le 3 \cdot 10^5\)), consisting of only characters 0 and/or 1.The sum of lengths of all given strings doesn't exceed \(3 \cdot 10^5\).
For each test case, print a single integer β€” the minimum number of coins required to sort the string \(s\) in non-decreasing order.
In the first example, you have to remove the \(1\)-st element, so the string becomes equal to 00.In the second example, the string is already sorted.In the third example, you have to swap the \(2\)-nd and the \(3\)-rd elements, so the string becomes equal to 0011.In the fourth example, you have to swap the \(3\)-rd and...
Input: 61000010100101101100110111111 | Output: 1000000000001 0 1000000000000 2000000000001 2000000000002 0
Medium
2
720
302
131
18
1,344
C
1344C
C. Quantifier Question
2,600
dfs and similar; dp; graphs; math
Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal (\(\forall\)) and existential (\(\exists\)). You can read more about them...
The first line contains two integers \(n\) and \(m\) (\(2\le n\le 2\cdot 10^5\); \(1\le m\le 2\cdot 10^5\)) β€” the number of variables and the number of inequalities in the formula, respectively.The next \(m\) lines describe the formula. The \(i\)-th of these lines contains two integers \(j_i\),\(k_i\) (\(1\le j_i,k_i\l...
If there is no assignment of quantifiers for which the statement is true, output a single integer \(-1\).Otherwise, on the first line output an integer, the maximum possible number of universal quantifiers.On the next line, output a string of length \(n\), where the \(i\)-th character is ""A"" if \(Q_i\) should be a un...
For the first test, the statement \(\forall x_1, \exists x_2, x_1<x_2\) is true. Answers of ""EA"" and ""AA"" give false statements. The answer ""EE"" gives a true statement, but the number of universal quantifiers in this string is less than in our answer.For the second test, we can show that no assignment of quantifi...
Input: 2 1 1 2 | Output: 1 AE
Expert
4
2,778
343
555
13
1,175
A
1175A
A. From Hero to Zero
900
implementation; math
You are given an integer \(n\) and an integer \(k\).In one step you can do one of the following moves: decrease \(n\) by \(1\); divide \(n\) by \(k\) if \(n\) is divisible by \(k\). For example, if \(n = 27\) and \(k = 3\) you can do the following steps: \(27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of queries.The only line of each query contains two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(2 \le k \le 10^{18}\)).
For each query print the minimum number of steps to reach \(0\) from \(n\) in single line.
Steps for the first test case are: \(59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0\).In the second test case you have to divide \(n\) by \(k\) \(18\) times and then decrease \(n\) by \(1\).
Input: 2 59 3 1000000000000000000 10 | Output: 8 19
Beginner
2
472
205
90
11
924
B
924B
B. Three-level Laser
1,600
binary search; greedy; two pointers
An atom of element X can exist in n distinct states with energies E1 < E2 < ... < En. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. Three distinct states i, j and k are selected, where i < j < k. After that the following process happens: initi...
The first line contains two integers n and U (3 ≀ n ≀ 105, 1 ≀ U ≀ 109) β€” the number of states and the maximum possible difference between Ek and Ei.The second line contains a sequence of integers E1, E2, ..., En (1 ≀ E1 < E2... < En ≀ 109). It is guaranteed that all Ei are given in increasing order.
If it is not possible to choose three states that satisfy all constraints, print -1.Otherwise, print one real number Ξ· β€” the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10 - 9.Formally, let your answer be a, and the jury's answer be b. ...
In the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to .In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to .
Input: 4 41 3 5 7 | Output: 0.5
Medium
3
923
301
358
9
875
C
875C
C. National Property
2,100
2-sat; dfs and similar; graphs; implementation
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.Some long and uninteresting story was removed...The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large versio...
The first line contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 100 000) β€” the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m.Each of the next n lines contains a description of one word in format li, si, 1, si...
In the first line print ""Yes"" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print ""No"" (without quotes).If the required is possible, in the second line print k β€” the number of letters Denis has to capitali...
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: 2' 1 1 3' 2' 1 1 The condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first let...
Input: 4 31 21 13 1 3 22 1 1 | Output: Yes22 3
Hard
4
2,069
602
540
8
1,401
E
1401E
E. Divide Square
2,400
data structures; geometry; implementation; sortings
There is a square of size \(10^6 \times 10^6\) on the coordinate plane with four points \((0, 0)\), \((0, 10^6)\), \((10^6, 0)\), and \((10^6, 10^6)\) as its vertices.You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square.Now you ar...
The first line contains two integers \(n\) and \(m\) (\(0 \le n, m \le 10^5\)) β€” the number of horizontal segments and the number of vertical segments.The next \(n\) lines contain descriptions of the horizontal segments. The \(i\)-th line contains three integers \(y_i\), \(lx_i\) and \(rx_i\) (\(0 < y_i < 10^6\); \(0 \...
Print the number of pieces the square is divided into after drawing all the segments.
The sample is like this:
Input: 3 3 2 3 1000000 4 0 4 3 0 1000000 4 0 1 2 0 5 3 1 1000000 | Output: 7
Expert
4
464
810
85
14
1,616
H
1616H
H. Keep XOR Low
3,000
bitmasks; combinatorics; data structures; divide and conquer; dp; math
You are given an array \(a_1, a_2, \ldots, a_n\) and an integer \(x\).Find the number of non-empty subsets of indices of this array \(1 \leq b_1 < b_2 < \ldots < b_k \leq n\), such that for all pairs \((i, j)\) where \(1 \leq i < j \leq k\), the inequality \(a_{b_i} \oplus a_{b_j} \leq x\) is held. Here, \(\oplus\) den...
The first line of the input contains two integers \(n\) and \(x\) (\(1 \leq n \leq 150\,000\), \(0 \leq x < 2^{30}\)). Here, \(n\) is the size of the array.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < 2^{30}\)): the array itself.
Print one integer: the number of non-empty subsets such that the bitwise XOR of every pair of elements is at most \(x\), modulo \(998\,244\,353\).
Input: 4 2 0 1 2 3 | Output: 8
Master
6
420
264
146
16
733
F
733F
F. Drivers Dissatisfaction
2,200
data structures; dsu; graphs; trees
In one kingdom there are n cities and m two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfaction β€” the value wi.For each road we know the value ci β€” how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce th...
The first line contains two integers n and m (2 ≀ n ≀ 2Β·105, n - 1 ≀ m ≀ 2Β·105) β€” the number of cities and the number of roads in the kingdom, respectively.The second line contains m integers w1, w2, ..., wm (1 ≀ wi ≀ 109), where wi is the drivers dissatisfaction with the i-th road.The third line contains m integers c1...
In the first line print K β€” the minimum possible total dissatisfaction with main roads.In each of the next n - 1 lines print two integers x, vx, which mean that the road x is among main roads and the road x, after the reform, has the level of dissatisfaction vx.Consider that roads are numbered from 1 to m in the order ...
Input: 6 91 3 1 1 3 1 2 2 24 1 4 2 2 5 3 1 61 21 32 32 42 53 53 64 55 67 | Output: 01 13 16 17 28 -5
Hard
4
1,283
923
450
7
633
E
633E
E. Startup Funding
2,400
binary search; constructive algorithms; data structures; probabilities; two pointers
An e-commerce startup pitches to the investors to get funding. They have been functional for n weeks now and also have a website!For each week they know the number of unique visitors during this week vi and the revenue ci. To evaluate the potential of the startup at some range of weeks from l to r inclusive investors u...
The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 1 000 000).The second line contains n integers vi (1 ≀ vi ≀ 107) β€” the number of unique visitors during each week.The third line contains n integers ci (1 ≀ ci ≀ 107) β€”the revenue for each week.
Print a single real value β€” the expected grade of the startup. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: 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 .
Consider the first sample.If the investors ask for li = 1 onwards, startup will choose ri = 1, such that max number of visitors is 3 and minimum revenue is 300. Thus, potential in this case is min(3Β·100, 300) = 300.If the investors ask for li = 2 onwards, startup will choose ri = 3, such that max number of visitors is ...
Input: 3 23 2 1300 200 300 | Output: 133.3333333
Expert
5
1,139
265
296
6
946
A
946A
A. Partition
800
greedy
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences. Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then ...
The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a.The second line contains n integers a1, a2, ..., an ( - 100 ≀ ai ≀ 100) β€” the elements of sequence a.
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120.
Input: 31 -2 0 | Output: 3
Beginner
1
379
183
132
9
799
G
799G
G. Cut the pie
3,500
binary search; data structures; geometry
Arkady reached the n-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex n-gon, i.e. a polygon with n vertices.Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to Masha. Th...
The first line contains two integers n and q (3 ≀ n ≀ 104, 1 ≀ q ≀ 105) β€” the number of vertices in the pie and the number of queries.n line follow describing the polygon vertices in clockwise order. The i-th of these line contains two integers xi and yi ( - 106 ≀ xi, yi ≀ 106) β€” the coordinates of the i-th vertex. It ...
For each query print single integer β€” the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;Ο€], the angles are measured from the direction of OX axis in counter-clockwise order. For example, the polar angle of the OY axis is . If there is no answer...
Input: 3 10 00 33 01 1 | Output: 2.67794504460098710000
Master
3
761
771
672
7
1,036
D
1036D
D. Vasya and Arrays
1,600
greedy; two pointers
Vasya has two arrays \(A\) and \(B\) of lengths \(n\) and \(m\), respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from ...
The first line contains a single integer \(n~(1 \le n \le 3 \cdot 10^5)\) β€” the length of the first array.The second line contains \(n\) integers \(a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)\) β€” elements of the array \(A\).The third line contains a single integer \(m~(1 \le m \le 3 \cdot 10^5)\) β€” the length of the sec...
Print a single integer β€” the maximum length of the resulting arrays after some operations were performed on arrays \(A\) and \(B\) in such a way that they became equal.If there is no way to make array equal, print ""-1"".
Input: 511 2 3 5 7411 7 3 7 | Output: 3
Medium
2
953
447
221
10
1,942
F
1942F
F. Farmer John's Favorite Function
2,700
brute force; data structures; implementation; math
ΩΩPARTS - Camelliaβ €Farmer John has an array \(a\) of length \(n\). He also has a function \(f\) with the following recurrence: \(f(1) = \sqrt{a_1}\); For all \(i > 1\), \(f(i) = \sqrt{f(i-1)+a_i}\). Note that \(f(i)\) is not necessarily an integer.He plans to do \(q\) updates to the array. Each update, he gives you two...
The first line contains \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)), the length of \(a\) and the number of updates he will perform.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^{18}\)).The next \(q\) lines each contain two integers \(k\) and \(x\) (\(1 \leq k \leq n\), ...
For each update, output an integer, \(\lfloor f(n) \rfloor\), on a new line.
In the first test case, the array after the first update is \([4, 14, 0, 7, 6]\). The values of \(f\) are: \(f(1)=2\); \(f(2)=4\); \(f(3)=2\); \(f(4)=3\); \(f(5)=3\). Since \(\lfloor f(5) \rfloor = 3\), we output \(3\).The array after the second update is \([3, 14, 0, 7, 6]\). The values of \(f\), rounded to \(6\) deci...
Input: 5 60 14 0 7 61 41 32 154 15 25 8 | Output: 3 2 3 2 1 3
Master
4
536
417
76
19
177
A2
177A2
A2. Good Matrix Elements
800
implementation
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ— n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the ""middle"" row β€” the row which has exactly rows above...
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix.The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
Print a single integer β€” the sum of good matrix elements.
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Input: 31 2 34 5 67 8 9 | Output: 45
Beginner
1
650
309
57
1
1,446
D1
1446D1
D1. Frequency Problem (Easy Version)
2,600
data structures; greedy
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.You are given an array \([a_1, a_2, \dots, a_n]\). Your goal is to find the length of the longest subarray of this array such that t...
The first line contains a single integer \(n\) (\(1 \le n \le 200\,000\)) β€” the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le min(n, 100)\)) β€” elements of the array.
You should output exactly one integer β€” the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output \(0\).
In the first sample, the subarray \([1, 1, 2, 2, 3, 3]\) is good, but \([1, 1, 2, 2, 3, 3, 3]\) isn't: in the latter there are \(3\) occurrences of number \(3\), and no other element appears \(3\) times.
Input: 7 1 1 2 2 3 3 3 | Output: 6
Expert
2
771
222
172
14
832
B
832B
B. Petya and Exam
1,600
implementation; strings
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..There is a glob pattern in the statements (a string consisting of lowercase English letters, characters ""?"" and ""*""). It is k...
The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.The second line contains the pattern β€” a string s of lowercase English letters, characters ""?"" and ""*"" (1 ≀ |s| ≀ 105). It is guaranteed that character "...
Print n lines: in the i-th of them print ""YES"" if the pattern matches the i-th query string, and ""NO"" otherwise.You can choose the case (lower or upper) for each letter arbitrary.
In the first example we can replace ""?"" with good letters ""a"" and ""b"", so we can see that the answer for the first query is ""YES"", and the answer for the second query is ""NO"", because we can't match the third letter.Explanation of the second example. The first query: ""NO"", because character ""*"" can be rep...
Input: aba?a2aaaaab | Output: YESNO
Medium
2
969
636
183
8
1,010
A
1010A
A. Fly
1,500
binary search; math
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on \(n - 2\) intermediate planets. Formally: we number all the planets from \(1\) to \(n\). \(1\) is Earth, \(n\) is Mars. Natasha will make exactly \(n\) flights: \(1 \to 2 \to \ldots n \to 1\).Flight from \(x\) to...
The first line contains a single integer \(n\) (\(2 \le n \le 1000\)) β€” number of planets.The second line contains the only integer \(m\) (\(1 \le m \le 1000\)) β€” weight of the payload.The third line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 1000\)), where \(a_i\) is the number of tons, which c...
If Natasha can fly to Mars through \((n - 2)\) planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number \(-1\).It is guaranteed, that if Natasha can make a flight, then it takes no more than \(10^9\) tons of fuel.The answer will be considered corre...
Let's consider the first example.Initially, the mass of a rocket with fuel is \(22\) tons. At take-off from Earth one ton of fuel can lift off \(11\) tons of cargo, so to lift off \(22\) tons you need to burn \(2\) tons of fuel. Remaining weight of the rocket with fuel is \(20\) tons. During landing on Mars, one ton of...
Input: 21211 87 5 | Output: 10.0000000000
Medium
2
2,065
627
537
10
1,505
G
1505G
G. Encoded message
2,600
*special; implementation
The first line of the input contains a single integer \(N\) (\(1 \le N \le 24\)). The next \(N\) lines contain \(5\) space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to ...
Output the result – a string of lowercase English letters.
Input: 1 1 0 0 1 0 | Output: a
Expert
2
0
353
58
15
1,360
E
1360E
E. Polygon
1,300
dp; graphs; implementation; shortest paths
Polygon is not only the best platform for developing problems but also a square matrix with side \(n\), initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case starts with a line containing an integer \(n\) (\(1 \le n \le 50\)) β€” the size of the polygon.This is followed by \(n\) lines of length \(n\), consisting of 0 and 1 β€” the polygon matri...
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
The first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell (\(1, 1\)) flying out of any cannon would continue its flight further.
Input: 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 | Output: YES NO YES YES NO
Easy
4
1,661
427
190
13
999
B
999B
B. Reversing Encryption
900
implementation
A string \(s\) of length \(n\) can be encrypted by the following algorithm: iterate over all divisors of \(n\) in decreasing order (i.e. from \(n\) to \(1\)), for each divisor \(d\), reverse the substring \(s[1 \dots d]\) (i.e. the substring which starts at position \(1\) and ends at position \(d\)). For example, the a...
The first line of input consists of a single integer \(n\) (\(1 \le n \le 100\)) β€” the length of the string \(t\). The second line of input consists of the string \(t\). The length of \(t\) is \(n\), and it consists only of lowercase Latin letters.
Print a string \(s\) such that the above algorithm results in \(t\).
The first example is described in the problem statement.
Input: 10rocesfedoc | Output: codeforces
Beginner
1
830
248
68
9
1,986
C
1986C
C. Update Queries
1,100
data structures; greedy; sortings
Let's consider the following simple problem. You are given a string \(s\) of length \(n\), consisting of lowercase Latin letters, as well as an array of indices \(ind\) of length \(m\) (\(1 \leq ind_i \leq n\)) and a string \(c\) of length \(m\), consisting of lowercase Latin letters. Then, in order, you perform the up...
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of sets of input data. Then follows their description.The first line of each set of input data contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β€” the length of the stri...
For each set of input data, output the lexicographically smallest string \(s\) that can be obtained by rearranging the indices in the array \(ind\) and the letters in the string \(c\) as you like.
In the first set of input data, you can leave the array \(ind\) and the string \(c\) unchanged and simply perform all operations in that order.In the second set of input data, you can set the array \(ind = [1, 1, 4, 2]\) and \(c =\) ""zczw"". Then the string \(s\) will change as follows: \(meow \rightarrow zeow \righta...
Input: 41 2a1 1cb4 4meow1 2 1 4zcwz7 4abacaba1 3 5 7damn7 10traktor7 6 5 4 3 2 1 6 4 2codeforces | Output: b cwoz abdcmbn ccdeefo
Easy
3
1,139
941
196
19
2,023
E
2023E
E. Tree of Life
3,300
dp; greedy; trees
In the heart of an ancient kingdom grows the legendary Tree of Life β€” the only one of its kind and the source of magical power for the entire world. The tree consists of \(n\) nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are \(n-1\)...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 4 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) β€” the number of nodes in the ...
For each test case, output a single integer β€” the minimum number of paths that the sages need to select to prevent a catastrophe.
In the first test case, there are two pairs of channels emanating from a single node: \((1, 2)\) and \((2, 3)\), \((2, 3)\) and \((3, 4)\). It is sufficient to perform the ritual along the path \(1-2-3-4\). Thus, the answer is \(1\).In the second test case, there are no pairs of channels emanating from a single node, s...
Input: 541 22 33 421 241 21 31 483 72 41 22 53 61 33 862 31 23 61 51 4 | Output: 1 0 3 7 3
Master
3
1,765
711
129
20
750
F
750F
F. New Year and Finding Roots
2,800
constructive algorithms; implementation; interactive; trees
This is an interactive problem. In the interaction section below you will find the information about flushing the output.The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Y...
The first line of the input contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases.At the beginning of each testcase you should read from the input a single integer h (2 ≀ h ≀ 7) β€” the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
In the first sample, a tree corresponds to the drawing from the statement.In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
Input: 1334 5 721 212 | Output: ? 1? 5? 6! 5
Master
4
1,368
304
0
7
1,735
E
1735E
E. House Planning
2,400
constructive algorithms; data structures; graph matchings; greedy
There are \(n\) houses in your city arranged on an axis at points \(h_1, h_2, \ldots, h_n\). You want to build a new house for yourself and consider two options where to place it: points \(p_1\) and \(p_2\).As you like visiting friends, you have calculated in advance the distances from both options to all existing hous...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^3\)) β€” the length of arrays \(d_1\), \(d_2\).The next two lines contain \(n\) integers each: ...
For each test case, output a single line ""NO"" if there is no answer.Otherwise output three lines. The first line must contain ""YES"". In the second line, print \(n\) integers \(h_1, h_2, \ldots, h_n\). In the third line print two integers \(p_1\), \(p_2\).It must be satisfied that \(0 \le h_i, p_1, p_2 \le 2 \cdot 1...
In the image below you can see the sample solutions. Planned houses are shown in bright colours: pink and purple. Existing houses are dim. In test case \(1\), the first planned house is located at point \(0\), the second at point \(10\). The existing house is located at point \(5\) and is at a distance of \(5\) from bo...
Input: 4155210 125 20210 3326 6940 2 1 22 2 1 0 | Output: YES 5 0 10 NO YES 0 43 33 69 YES 1 0 3 3 1 1
Expert
4
1,570
481
463
17
1,453
A
1453A
A. Cancel the Trains
800
implementation
Gildong's town has a train system that has \(100\) trains that travel from the bottom end to the top end and \(100\) trains that travel from the left end to the right end. The trains starting from each side are numbered from \(1\) to \(100\), respectively, and all trains have the same speed. Let's take a look at the pi...
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)).Each test case contains three lines. The first line of each test case consists of two integers \(n\) and \(m\) (\(1 \le n, m \le 100\)) β€” the number of trains scheduled to depart from the bottom end, ...
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.In the second case, at \(T=4\), there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefor...
Input: 3 1 2 1 3 4 3 2 1 3 4 2 4 9 14 2 7 16 28 33 57 59 86 99 3 9 14 19 25 26 28 35 41 59 85 87 99 100 | Output: 0 1 3
Beginner
1
1,176
864
129
14
77
B
77B
B. Falling Anvils
1,800
math; probabilities
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who th...
The first line contains integer t (1 ≀ t ≀ 10000) β€” amount of testcases.Each of the following t lines contain two space-separated integers a and b (0 ≀ a, b ≀ 106).Pretests contain all the tests with 0 < a < 10, 0 ≀ b < 10.
Print t lines β€” the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10 - 6.
Input: 24 21 2 | Output: 0.62500000000.5312500000
Medium
2
1,036
223
147
0
1,766
C
1766C
C. Hamiltonian Wall
1,300
dp; implementation
Sir Monocarp Hamilton is planning to paint his wall. The wall can be represented as a grid, consisting of \(2\) rows and \(m\) columns. Initially, the wall is completely white.Monocarp wants to paint a black picture on the wall. In particular, he wants cell \((i, j)\) (the \(j\)-th cell in the \(i\)-th row) to be color...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of each testcase contains a single integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) β€” the number of columns in the wall.The \(i\)-th of the next two lines contains a string \(c_i\), consisting of \(m\) characters,...
For each testcase, print ""YES"" if Monocarp can paint a wall. Otherwise, print ""NO"".
In the first testcase, Monocarp can follow a path \((2, 1)\), \((2, 2)\), \((1, 2)\), \((1, 3)\) with his brush. All black cells appear in the path exactly once, no white cells appear in the path.In the second testcase, Monocarp can follow a path \((1, 1)\), \((2, 1)\).In the third testcase: the path \((1, 1)\), \((2, ...
Input: 63WBBBBW1BB5BWBWBBBBBB2BWWB5BBBBWBWBBB6BWBBWBBBBBBB | Output: YES YES NO NO NO YES
Easy
2
994
683
87
17
750
A
750A
A. New Year and Hurry
800
binary search; brute force; implementation; math
Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th...
The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house.
Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier.
In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenar...
Input: 3 222 | Output: 2
Beginner
4
594
200
141
7
1,116
D5
1116D5
D5. Creeper
0
*special
Implement a unitary operation on 3 qubits which is represented by a square matrix of size 8 with the following pattern:XX....XXXX....XX...XX......XX.....X..X....X..X..XX....XXXX....XXHere X denotes a ""non-zero"" element of the matrix (a complex number which has the square of the absolute value greater than or equal to...
Beginner
1
1,672
0
0
11
820
A
820A
A. Mister B and Book Reading
900
implementation
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v0 pages, at second β€” v...
First and only line contains five space-separated integers: c, v0, v1, a and l (1 ≀ c ≀ 1000, 0 ≀ l < v0 ≀ v1 ≀ 1000, 0 ≀ a ≀ 1000) β€” the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.
Print one integer β€” the number of days Mister B needed to finish the book.
In the first sample test the book contains 5 pages, so Mister B read it right at the first day.In the second sample test at first day Mister B read pages number 1 - 4, at second day β€” 4 - 11, at third day β€” 11 - 12 and finished the book.In third sample test every day Mister B read 1 page of the book, so he finished in ...
Input: 5 5 10 5 4 | Output: 1
Beginner
1
739
293
74
8
1,290
A
1290A
A. Mind Control
1,600
brute force; data structures; implementation
You and your \(n - 1\) friends have found an array of integers \(a_1, a_2, \dots, a_n\). You have decided to share it in the following way: All \(n\) of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and ...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers \(n\), \(m\) and \(k\) (\(1 \le m \le n \le 3500\), \(0 \le k \l...
For each test case, print the largest integer \(x\) such that you can guarantee to obtain at least \(x\).
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element (\(5\)) because he or she was forced by you to take the last element. After this turn the remaining array will be \([2, 9, 2, 3, ...
Input: 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 | Output: 8 4 1 1
Medium
3
1,300
668
105
12
1,789
E
1789E
E. Serval and Music Game
2,500
brute force; dp; implementation; math; number theory
Serval loves playing music games. He meets a problem when playing music games, and he leaves it for you to solve.You are given \(n\) positive integers \(s_1 < s_2 < \ldots < s_n\). \(f(x)\) is defined as the number of \(i\) (\(1\leq i\leq n\)) that exist non-negative integers \(p_i, q_i\) such that: $$$\(s_i=p_i\left\l...
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 cases contains a single integer \(n\) (\(1\leq n\leq 10^6\)).The second line of each test case contains \(n\) positive integers \(s...
For each test case, print a single integer in a single line β€” the sum of \(x\cdot f(x)\) over all possible \(x\) modulo \(998\,244\,353\).
For the first test case, \(s_n=4\), \(f(x)\) are calculated as followings: \(f(1)=1\) \(\left\lfloor s_n\over 1\right\rfloor=4\), \(\left\lceil s_n\over 1\right\rceil=4\). It can be shown that such \(p_1,p_2\) and \(q_1,q_2\) that satisfy the conditions don't exist. Let \(p_3=1\) and \(q_3=0\), then \(s_3 = p_3\cdot\le...
Input: 431 2 441 2 7 94344208 591000 4779956 540342951633 1661 1741 2134 2221 | Output: 26 158 758737625 12334970
Expert
5
636
520
138
17
1,409
E
1409E
E. Two Platforms
1,800
binary search; dp; sortings; two pointers
There are \(n\) points on a plane. The \(i\)-th point has coordinates \((x_i, y_i)\). You have two horizontal platforms, both of length \(k\). Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same \(y\)-coordinate) and have integer borders. If the left border of the platform ...
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 two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^9\)) β€” the number of points and the length of each platform...
For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally.
The picture corresponding to the first test case of the example:Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points \((1, -1)\) and \((2, -1)\) and the second one between points \((4, 3)\) and \((5, 3)\). Vectors represent how the ...
Input: 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 | Output: 6 1 5 10
Medium
4
1,076
870
118
14
2,091
D
2091D
D. Place of the Olympiad
1,200
binary search; greedy; math
For the final of the first Olympiad by IT Campus ""NEIMARK"", a rectangular venue was prepared. You may assume that the venue is divided into \(n\) rows, each containing \(m\) spots for participants' desks. A total of \(k\) participants have registered for the final, and each participant will sit at an individual desk....
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.A single line of each test case contains three positive integers β€” \(n\), \(m\), \(k\) (\(1 \leq n, m, k \leq 10^9\), \(k \leq n\cdot m\)).
For each test case, output a single number β€” the minimum possible length of the longest bench.
Input: 53 4 75 5 51 13 22 4 71 5 4 | Output: 2 1 1 4 2
Easy
3
1,258
302
94
20
2,009
A
2009A
A. Minimize!
800
brute force; math
You are given two integers \(a\) and \(b\) (\(a \leq b\)). Over all possible integer values of \(c\) (\(a \leq c \leq b\)), find the minimum value of \((c - a) + (b - c)\).
The first line contains \(t\) (\(1 \leq t \leq 55\)) β€” the number of test cases. Each test case contains two integers \(a\) and \(b\) (\(1 \leq a \leq b \leq 10\)).
For each test case, output the minimum possible value of \((c - a) + (b - c)\) on a new line.
In the first test case, you can choose \(c = 1\) and obtain an answer of \((1 - 1) + (2 - 1) = 1\). It can be shown this is the minimum value possible.In the second test case, you can choose \(c = 6\) and obtain an answer of \((6 - 3) + (10 - 6) = 7\). It can be shown this is the minimum value possible.
Input: 31 23 105 5 | Output: 1 7 0
Beginner
2
172
164
93
20
1,294
F
1294F
F. Three Paths on a Tree
2,000
dfs and similar; dp; greedy; trees
You are given an unweighted tree with \(n\) vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices \(a, b, c\) on this tree such that the number of edges which belong to at least one of the simple paths between \(a\) and \(b\), \(b\) and \(c\), or \(a\...
The first line contains one integer number \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the number of vertices in the tree. Next \(n - 1\) lines describe the edges of the tree in form \(a_i, b_i\) (\(1 \le a_i\), \(b_i \le n\), \(a_i \ne b_i\)). It is guaranteed that given graph is a tree.
In the first line print one integer \(res\) β€” the maximum number of edges which belong to at least one of the simple paths between \(a\) and \(b\), \(b\) and \(c\), or \(a\) and \(c\).In the second line print three integers \(a, b, c\) such that \(1 \le a, b, c \le n\) and \(a \ne, b \ne c, a \ne c\).If there are sever...
The picture corresponding to the first example (and another one correct answer):If you choose vertices \(1, 5, 6\) then the path between \(1\) and \(5\) consists of edges \((1, 2), (2, 3), (3, 4), (4, 5)\), the path between \(1\) and \(6\) consists of edges \((1, 2), (2, 3), (3, 4), (4, 6)\) and the path between \(5\) ...
Input: 8 1 2 2 3 3 4 4 5 4 6 3 7 3 8 | Output: 5 1 8 6
Hard
4
471
284
350
12
1,038
B
1038B
B. Non-Coprime Partition
1,100
constructive algorithms; math
Find out if it is possible to partition the first \(n\) positive integers into two non-empty disjoint sets \(S_1\) and \(S_2\) such that:\(\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1\) Here \(\mathrm{sum}(S)\) denotes the sum of all elements present in set \(S\) and \(\mathrm{gcd}\) means thegreatest common ...
The only line of the input contains a single integer \(n\) (\(1 \le n \le 45\,000\))
If such partition doesn't exist, print ""No"" (quotes for clarity).Otherwise, print ""Yes"" (quotes for clarity), followed by two lines, describing \(S_1\) and \(S_2\) respectively.Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.If there are mu...
In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is ""No"".In the second example, the sums of the sets are \(2\) and \(4\) respectively. The \(\mathrm{gcd}(2, 4) = 2 > 1\), hence that is one of the possible answers.
Input: 1 | Output: No
Easy
2
424
84
367
10
1,389
B
1389B
B. Array Walk
1,600
brute force; dp; greedy
You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) positive integers. Initially you are standing at index \(1\) and have a score equal to \(a_1\). You can perform two kinds of moves: move right β€” go from your current index \(x\) to \(x+1\) and add \(a_{x+1}\) to your score. This move can only be perfo...
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 three integers \(n, k\) and \(z\) (\(2 \le n \le 10^5\), \(1 \le k \le n - 1\), \(0 \le z \le min(5, k)\)) β€” the number of elements in the array, the total number of moves you should ...
Print \(t\) integers β€” for each testcase output the maximum score you can achieve if you make exactly \(k\) moves in total, no more than \(z\) of them are to the left and there are no two or more moves to the left in a row.
In the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score \(a_1 + a_2 + a_3 + a_4 + a_5\).In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be \(a_1 + a_2 + a_3 + a_2 + a_3\).In t...
Input: 4 5 4 0 1 5 4 3 2 5 4 1 1 5 4 3 2 5 4 4 10 20 30 40 50 10 7 3 4 6 8 2 9 9 7 4 10 9 | Output: 15 19 150 56
Medium
3
689
582
223
13
1,183
G
1183G
G. Candy Box (hard version)
2,000
greedy; implementation; sortings
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are \(n\) candies in a candy box. The type of the \(i\)-th candy is \(a_i\) (\(1 \le a_i \le n\)). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies o...
The first line of the input contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β€” the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of candies.Then \(n\) lines follow, each containing two integers \(a_i\) and \(f_i\) (\(1 \le a_i \le n\), \(0...
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having \(f_i = 1\) in a gift you can compose that contains the maximum possible number of candies.
In the first query, you can include two candies of type \(4\) and one candy of type \(5\). All of them have \(f_i = 1\) and you don't mind giving them away as part of the gift.
Input: 3 8 1 0 4 1 2 0 4 1 5 1 6 1 3 0 2 0 4 1 1 1 1 2 1 2 1 9 2 0 2 0 4 1 4 1 4 1 7 0 7 1 7 0 7 1 | Output: 3 3 3 3 9 5
Hard
3
1,522
625
267
11
161
B
161B
B. Discounts
1,700
constructive algorithms; greedy; sortings
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheape...
The first input line contains two integers n and k (1 ≀ k ≀ n ≀ 103) β€” the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as ""ci ti"" (without the quotes), where ci (1 ≀ ci ≀ 109) is an integer denoting the price of the i-th item, ti (1 ≀ ti ≀ 2) is an inte...
In the first line print a single real number with exactly one decimal place β€” the minimum total price of the items, including the discounts.In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as ""t b1 b2 ... bt"" (without the quotes), where...
In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5 + (3 + 3Β·0.5) = 1 + 4.5 = 5.5.
Input: 3 22 13 23 1 | Output: 5.52 1 21 3
Medium
3
837
448
836
1
2,117
B
2117B
B. Shrink
800
constructive algorithms
A shrink operation on an array \(a\) of size \(m\) is defined as follows: Choose an index \(i\) (\(2 \le i \le m - 1\)) such that \(a_i \gt a_{i - 1}\) and \(a_i \gt a_{i + 1}\). Remove \(a_i\) from the array. Define the score of a permutation\(^{\text{βˆ—}}\) \(p\) as the maximum number of times that you can perform the...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of test cases.Each test case contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the size of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output any permutation \(p_1, p_2, \dots, p_n\) that maximizes the number of shrink operations.
In the first test case: We choose \(p = [1, 3, 2]\). Choose index \(2\), and remove \(p_2\) from the array. The array becomes \(p = [1, 2]\). It can be shown that the maximum number of operations we can perform is \(1\). Another valid answer is \(p = [2, 3, 1]\).In the second test case: We choose \(p = [2, 3, 6, 4, 5, ...
Input: 236 | Output: 1 3 2 2 3 6 4 5 1
Beginner
1
868
298
115
21
276
C
276C
C. Little Girl and Maximum Sum
1,500
data structures; greedy; implementation; sortings
The little girl loves the problems on array queries very much.One day she came across a rather well-known problem: you've got an array of \(n\) elements (the elements of the array are indexed starting from 1); also, there are \(q\) queries, each one is defined by a pair of integers \(l_i\), \(r_i\) \((1 \le l_i \le r_i...
The first line contains two space-separated integers \(n\) (\(1 \le n \le 2\cdot10^5\)) and \(q\) (\(1 \le q \le 2\cdot10^5\)) β€” the number of elements in the array and the number of queries, correspondingly.The next line contains \(n\) space-separated integers \(a_i\) (\(1 \le a_i \le 2\cdot10^5\)) β€” the array element...
In a single line print, a single integer β€” the maximum sum of query replies after the array elements are reordered.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Input: 3 35 3 21 22 31 3 | Output: 25
Medium
4
680
465
262
2
875
F
875F
F. Royal Questions
2,500
dsu; graphs; greedy
In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his n sons-princes marry the brides with as big dowry as possible.In search of candidates, the king aske...
The first line contains two integers n, m (2 ≀ n ≀ 200 000, 1 ≀ m ≀ 200 000) β€” number of princes and princesses respectively.Each of following m lines contains three integers ai, bi, wi (1 ≀ ai, bi ≀ n, ai β‰  bi, 1 ≀ wi ≀ 10 000) β€” number of princes, which i-th princess is ready to marry and the value of her dowry.
Print the only integer β€” the maximum number of gold coins that a king can get by playing the right weddings.
Input: 2 31 2 51 2 12 1 10 | Output: 15
Expert
3
1,396
315
108
8
1,538
B
1538B
B. Friends and Candies
800
greedy; math
Polycarp has \(n\) friends, the \(i\)-th of his friends has \(a_i\) candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all \(a_i\) to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses \(k\) (\(0 \le k \le ...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^4\)).It is guaranteed that the sum of \(n\) over...
For each test case output: the minimum value of \(k\), such that Polycarp can choose exactly \(k\) friends so that he can redistribute the candies in the desired way; ""-1"" if no such value \(k\) exists.
Input: 5 4 4 5 2 5 2 0 4 5 10 8 5 1 4 1 10000 7 1 1 1 1 1 1 1 | Output: 2 1 -1 0 0
Beginner
2
1,528
369
204
15
929
D
929D
D. ΠŸΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Π΅ Π²Ρ€Π°Ρ‚Π°
2,400
Π“Π΅Ρ€ΠΎΠΉ Аркадий находится Π½Π° ΡƒΠ·ΠΊΠΎΠΉ полоскС Π·Π΅ΠΌΠ»ΠΈ, Ρ€Π°Π·Π΄Π΅Π»Π΅Π½Π½ΠΎΠΉ Π½Π° n Π·ΠΎΠ½, ΠΏΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΠΎΠ²Π°Π½Π½Ρ‹Ρ… ΠΎΡ‚ 1 Π΄ΠΎ n. Из i-ΠΉ Π·ΠΎΠ½Ρ‹ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€ΠΎΠΉΡ‚ΠΈ лишь Π² (i - 1)-ю Π·ΠΎΠ½Ρƒ ΠΈ Π² (i + 1)-ю Π·ΠΎΠ½Ρƒ, Ссли ΠΎΠ½ΠΈ ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‚. ΠŸΡ€ΠΈ этом ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°Ρ€ΠΎΠΉ сосСдних Π·ΠΎΠ½ находятся ΠΏΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Π΅ Π²Ρ€Π°Ρ‚Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°Π·Π½Ρ‹Ρ… Ρ†Π²Π΅Ρ‚ΠΎΠ², Ρ†Π²Π΅Ρ‚ Π²Ρ€Π°Ρ‚ ΠΌΠ΅ΠΆΠ΄Ρƒ i-ΠΉ ΠΈ (i + 1)-ΠΉ...
ΠŸΠ΅Ρ€Π²Π°Ρ строка содСрТит Ρ‚Ρ€ΠΈ Ρ†Π΅Π»Ρ‹Ρ… числа n, a, b (2 ≀ n ≀ 100 000, 1 ≀ a, b ≀ n, a β‰  b) β€” число Π·ΠΎΠ½, Π½ΠΎΠΌΠ΅Ρ€ Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎΠΉ Π·ΠΎΠ½Ρ‹ ΠΈ Π½ΠΎΠΌΠ΅Ρ€ ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠΉ Π·ΠΎΠ½Ρ‹, соотвСтствСнно.Вторая строка содСрТит n - 1 Ρ†Π΅Π»ΠΎΠ΅ число g1, g2, ..., gn - 1 (1 ≀ gi ≀ 100 000), Π³Π΄Π΅ gi ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Ρ†Π²Π΅Ρ‚ ΠΏΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Ρ… Π²Ρ€Π°Ρ‚ ΠΌΠ΅ΠΆΠ΄Ρƒ Π·ΠΎΠ½Π°ΠΌΠΈ i ΠΈ i + 1.Π’Ρ€Π΅Ρ‚ΡŒΡ строка содСрТ...
Если Аркадий Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠΏΠ°ΡΡ‚ΡŒ ΠΈΠ· Π·ΠΎΠ½Ρ‹ a Π² Π·ΠΎΠ½Ρƒ b, Π½Π΅ имСя ΠΈΠ·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ ΠΊΠ»ΡŽΡ‡Π΅ΠΉ, Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ -1.Π˜Π½Π°Ρ‡Π΅ Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ минимальноС количСство Ρ…ΠΎΠ΄ΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ потрСбуСтся ΠΡ€ΠΊΠ°Π΄ΠΈΡŽ.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠΏΠ°ΡΡ‚ΡŒ ΠΈΠ· Π·ΠΎΠ½Ρ‹ 4 Π² Π·ΠΎΠ½Ρƒ 1, ΠΡ€ΠΊΠ°Π΄ΠΈΡŽ Π½ΡƒΠΆΠ½ΠΎ сначала Π²Π·ΡΡ‚ΡŒ ΠΊΠ»ΡŽΡ‡ Ρ†Π²Π΅Ρ‚Π° 1, ΠΏΡ€ΠΎΠΉΡ‚ΠΈ Π² Π·ΠΎΠ½Ρƒ 3, Ρ‚Π°ΠΌ Π²Π·ΡΡ‚ΡŒ ΠΊΠ»ΡŽΡ‡ Ρ†Π²Π΅Ρ‚Π° 2 ΠΈ ΠΏΡ€ΠΎΠΉΡ‚ΠΈ ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ Π² Π·ΠΎΠ½Ρƒ 4 ΠΈ Π·Π°Ρ‚Π΅ΠΌ Π² Π·ΠΎΠ½Ρƒ 5, Π²Π·ΡΡ‚ΡŒ Ρ‚Π°ΠΌ ΠΊΠ»ΡŽΡ‡ Ρ†Π²Π΅Ρ‚Π° 3 ΠΈ Π΄ΠΎΠΉΡ‚ΠΈ Π΄ΠΎ Π·ΠΎΠ½Ρ‹ 1 Π·Π° Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅ Ρ…ΠΎΠ΄Π°.Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ Аркадий ΠΌΠΎΠΆΠ΅Ρ‚ Π΄ΠΎΠΉΡ‚ΠΈ лишь Π΄ΠΎ Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠΉ Π·ΠΎΠ½Ρ‹, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ΡˆΠ°Ρ‚Ρ€ΠΎΠ² Ρ…Ρ€Π°Π½ΠΈΡ‚Π΅Π»Π΅...
Input: 5 4 13 1 1 27 1 2 1 3 | Output: 7
Expert
0
945
428
162
9
1,904
B
1904B
B. Collecting Game
1,100
binary search; dp; greedy; sortings; two pointers
You are given an array \(a\) of \(n\) positive integers and a score. If your score is greater than or equal to \(a_i\), then you can increase your score by \(a_i\) and remove \(a_i\) from the array. For each index \(i\), output the maximum number of additional array elements that you can remove if you remove \(a_i\) an...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5000\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the length of the array.The second line of each test c...
For each test case, output \(n\) integers, the \(i\)-th of which denotes the maximum number of additional array elements that you can remove if you remove \(a_i\) from the array and then set your score to \(a_i\).
In the first test case, the answers are as follows:If we start with \(i=4\), our initial score is \(a_4=4\) and \(a=[20,5,1,2]\). We can remove \(3\) additional elements in the following order: Since \(4 \ge 1\), we can remove \(1\) and our score becomes \(5\). After this, \(a=[20,5,2]\). Since \(5 \ge 5\), we can remo...
Input: 4520 5 1 4 231434 7 1442115999999999 999999999 999999999 1000000000 1000000000 | Output: 4 3 0 3 1 1 0 2 0 4 4 4 4 4
Easy
5
423
511
213
19
1,379
C
1379C
C. Choosing flowers
2,000
binary search; brute force; data structures; dfs and similar; dp; greedy; sortings; two pointers
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly \(n\) flowers.Vladimir went to a flower shop, and he was amazed to see that there are \(m\) types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose fl...
The first line contains the only integer \(t\) (\(1 \leq t \leq 10\,000\)), the number of test cases. It is followed by \(t\) descriptions of the test cases.Each test case description starts with two integers \(n\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le m \le 100\,000\)), the number of flowers Vladimir needs to choos...
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly \(n\) flowers optimally.
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals \(5 + (1 + 2 \cdot 4) = 14\).In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case th...
Input: 2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1 | Output: 14 16
Hard
8
900
683
138
13
1,550
B
1550B
B. Maximum Cost Deletion
1,000
greedy; math
You are given a string \(s\) of length \(n\) consisting only of the characters 0 and 1.You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same orde...
The first line contains a single integer \(t\) (\(1 \le t \le 2000\)) β€” the number of testcases.The first line of each testcase contains three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 100; -100 \le a, b \le 100\)) β€” the length of the string \(s\) and the parameters \(a\) and \(b\).The second line contains the str...
For each testcase, print a single integer β€” the maximum number of points that you can score.
In the first example, it is enough to delete the entire string, then we will get \(2 \cdot 3 + 0 = 6\) points.In the second example, if we delete characters one by one, then for each deleted character we will get \((-2) \cdot 1 + 5 = 3\) points, i. e. \(15\) points in total.In the third example, we can delete the subst...
Input: 3 3 2 0 000 5 -2 5 11001 6 1 -4 100111 | Output: 6 15 -2
Beginner
2
625
388
92
15
1,694
B
1694B
B. Paranoid String
1,200
constructive algorithms; greedy
Let's call a binary string \(T\) of length \(m\) indexed from \(1\) to \(m\) paranoid if we can obtain a string of length \(1\) by performing the following two kinds of operations \(m-1\) times in any order : Select any substring of \(T\) that is equal to 01, and then replace it with 1. Select any substring of \(T\) th...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the size of \(S\).The second line of each test case contains a binary string \(S\) of \(n\) ...
For each test case, output the number of pairs of integers \((l, r)\) \(1 \le l \le r \le n\) such that \(S[l \ldots r]\) (the substring of \(S\) from \(l\) to \(r\)) is a paranoid string.
In the first sample, \(S\) already has length \(1\) and doesn't need any operations.In the second sample, all substrings of \(S\) are paranoid. For the entire string, it's enough to perform the first operation.In the third sample, all substrings of \(S\) are paranoid except \([S_2S_3]\), because we can't perform any op...
Input: 511201310041001511111 | Output: 1 3 4 8 5
Easy
2
742
502
188
16
601
A
601A
A. The Two Routes
1,600
graphs; shortest paths
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network β€” for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one r...
The first line of the input contains two integers n and m (2 ≀ n ≀ 400, 0 ≀ m ≀ n(n - 1) / 2) β€” the number of towns and the number of railways respectively.Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≀ u, v ≀ n, u β‰  v).You may assume that there is at most one rail...
Output one integer β€” the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.
In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time.In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
Input: 4 21 33 4 | Output: 2
Medium
2
1,186
349
170
6
818
B
818B
B. Permutation Game
1,600
implementation
n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.The game consists of m steps. On each step the current leader with index i counts out ai p...
The first line contains two integer numbers n, m (1 ≀ n, m ≀ 100).The second line contains m integer numbers l1, l2, ..., lm (1 ≀ li ≀ n) β€” indices of leaders in the beginning of each step.
Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them. If there is no permutation which satisfies all described conditions print -1.
Let's follow leadership in the first example: Child 2 starts. Leadership goes from 2 to 2 + a2 = 3. Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1. Leadership goes from 1 to 1 + a1 = 4. Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.
Input: 4 52 3 1 4 4 | Output: 3 1 2 4
Medium
1
757
189
268
8
666
D
666D
D. Chain Reaction
3,000
brute force; geometry
Group of Berland scientists, with whom you have a close business relationship, makes a research in the area of peaceful nuclear energy. In particular, they found that a group of four nanobots, placed on a surface of a plate, can run a powerful chain reaction under certain conditions. To be precise, researchers introduc...
The first line contains an integer number t (1 ≀ t ≀ 50) β€” the number of plates.t descriptions of plates follow. A description of each plate consists of four lines. Each line consists of a pair of integers numbers xi, yi ( - 108 ≀ xi, yi ≀ 108) β€” coordinates of the next bot. All bots are in different locations.Note, th...
Print answers for all plates separately. First goes a single integer number in a separate line. If scientists have made an unfortunate mistake and nanobots are not able to form the desired square, print -1. Otherwise, print the minimum possible length of the longest bot's path.If a solution exists, in the next four lin...
Input: 21 11 -1-1 1-1 -11 12 24 46 6 | Output: 01 11 -1-1 1-1 -1-1
Master
2
1,352
505
517
6
1,759
G
1759G
G. Restore the Permutation
1,900
binary search; constructive algorithms; data structures; greedy; math
A sequence of \(n\) numbers is called permutation if it contains all numbers from \(1\) to \(n\) exactly once. For example, the sequences [\(3, 1, 4, 2\)], [\(1\)] and [\(2,1\)] are permutations, but [\(1,2,1\)], [\(0,1\)] and [\(1,3,4\)] β€” are not.For a permutation \(p\) of even length \(n\) you can make an array \(b\...
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The description of the test cases follows.The first line of each test case contains one even integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line of each test case contains exactly \(\frac{n}{2}\) inte...
For each test case, print on a separate line: lexicographically minimal permutation \(p\) such that you can make an array \(b\) from it; or a number -1 if the permutation you are looking for does not exist.
The first test case is parsed in the problem statement.
Input: 664 3 642 488 7 2 366 4 244 488 7 4 5 | Output: 1 4 2 3 5 6 1 2 3 4 -1 5 6 3 4 1 2 -1 1 8 6 7 2 4 3 5
Hard
5
1,272
480
206
17
1,606
A
1606A
A. AB Balance
900
strings
You are given a string \(s\) of length \(n\) consisting of characters a and/or b.Let \(\operatorname{AB}(s)\) be the number of occurrences of string ab in \(s\) as a substring. Analogically, \(\operatorname{BA}(s)\) is the number of occurrences of ba in \(s\) as a substring.In one step, you can choose any index \(i\) a...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first and only line of each test case contains a single string \(s\) (\(1 \le |s| \le 100\), where \(|s|\) is the length of the string \(s\)), consisting only ...
For each test case, print the resulting string \(s\) with \(\operatorname{AB}(s) = \operatorname{BA}(s)\) you'll get making the minimum number of steps.If there are multiple answers, print any of them.
In the first test case, both \(\operatorname{AB}(s) = 0\) and \(\operatorname{BA}(s) = 0\) (there are no occurrences of ab (ba) in b), so can leave \(s\) untouched.In the second test case, \(\operatorname{AB}(s) = 2\) and \(\operatorname{BA}(s) = 2\), so you can leave \(s\) untouched. In the third test case, \(\operato...
Input: 4 b aabbbabaa abbb abbaab | Output: b aabbbabaa bbbb abbaaa
Beginner
1
833
345
201
16
483
B
483B
B. Friends and Presents
1,800
binary search; math
You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.In addition, the first...
The only line contains four positive integers cnt1, cnt2, x, y (1 ≀ cnt1, cnt2 < 109; cnt1 + cnt2 ≀ 109; 2 ≀ x < y ≀ 3Β·104) β€” the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
Print a single integer β€” the answer to the problem.
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first f...
Input: 3 1 2 3 | Output: 5
Medium
2
865
220
51
4
1,312
F
1312F
F. Attack on Red Kingdom
2,500
games; two pointers
The Red Kingdom is attacked by the White King and the Black King!The Kingdom is guarded by \(n\) castles, the \(i\)-th castle is defended by \(a_i\) soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders. Each day the White King launches an attack on one of the castles. Then, at night, the ...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Then, the test cases follow. Each test case is represented by two lines. The first line contains four integers \(n\), \(x\), \(y\) and \(z\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le x, y, z \le 5\)). The second line contains \(n\)...
For each test case, print the answer to it: the number of possible options for the first attack of the White King (or \(0\), if the Black King can launch the last attack no matter how the White King acts).
Input: 3 2 1 3 4 7 6 1 1 2 3 1 1 1 2 2 3 | Output: 2 3 0
Expert
2
2,118
504
205
13
1,628
E
1628E
E. Groceries in Meteor Town
3,100
binary search; data structures; dsu; trees
Mihai lives in a town where meteor storms are a common problem. It's annoying, because Mihai has to buy groceries sometimes, and getting hit by meteors isn't fun. Therefore, we ask you to find the most dangerous way to buy groceries so that we can trick him to go there.The town has \(n\) buildings numbered from \(1\) t...
The first line contains the two integers \(n\) and \(q\) (\(2 \le n, q \le 3\cdot 10^5\)).Then follows \(n - 1\) lines, the \(i\)-th of which containing the integers \(u_i\), \(v_i\), and \(w_i\) (\(1 \le u_i, v_i \le n, \enspace 1 \le w_i \le 10^9\)) meaning there is two way road between building \(u_i\) and \(v_i\) w...
For each query of the \(3\)rd type (\(t_j = 3\)), output the maximum meteor danger level that is on some edge on the simple path from \(x_j\) to some open store, or \(-1\) if there is no such edge.
This is an illustration of the town given in the sample input.In the first query, there are no open stores, so obviously there are no edges on the simple path from \(1\) to any open store, so the answer is \(-1\).After the second and third queries, the set of open stores is \(\{1\}\). The simple path from \(1\) to \(1\...
Input: 6 9 1 3 1 2 3 2 4 5 3 4 6 4 3 4 5 3 1 1 1 1 3 1 2 1 1 1 5 6 3 4 2 6 6 3 4 3 1 | Output: -1 -1 4 3 5
Master
4
1,240
771
197
16
2,074
F
2074F
F. Counting Necessary Nodes
2,000
bitmasks; divide and conquer; greedy; implementation; math
A quadtree is a tree data structure in which each node has at most four children and accounts for a square-shaped region.Formally, for all tuples of nonnegative integers \(k,a,b \ge 0\), there exists exactly one node accounting for the following region\(^{\text{βˆ—}}\).$$$\([a \cdot 2^k,(a+1) \cdot 2^k] \times [b \cdot 2...
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 only line of each test case contains four integers \(l_1\), \(r_1\), \(l_2\), \(r_2\) β€” the boundaries of the region in each axis (\(0 \le l_i < r_i \le 1...
For each test case, output the minimum number of nodes necessary to satisfy the condition on a separate line.
On the first test case, the given region is \([0,1] \times [1,2]\). There is one node accounting for \([0,1] \times [1,2]\). Choosing this node, the answer is \(1\).On the second test case, the given region is \([0,2] \times [0,2]\). There is one node accounting for \([0,2] \times [0,2]\). Choosing this node, the answe...
Input: 50 1 1 20 2 0 21 3 1 30 2 1 59 98 244 353 | Output: 1 1 4 5 374
Hard
5
1,826
327
109
20
1,615
B
1615B
B. And It's Non-Zero
1,300
bitmasks; greedy; math
You are given an array consisting of all integers from \([l, r]\) inclusive. For example, if \(l = 2\) and \(r = 5\), the array would be \([2, 3, 4, 5]\). What's the minimum number of elements you can delete to make the bitwise AND of the array non-zero?A bitwise AND is a binary operation that takes two equal-length bi...
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. Then \(t\) cases follow.The first line of each test case contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq 2 \cdot 10^5\)) β€” the description of the array.
For each test case, output a single integer β€” the answer to the problem.
In the first test case, the array is \([1, 2]\). Currently, the bitwise AND is \(0\), as \(1\ \& \ 2 = 0\). However, after deleting \(1\) (or \(2\)), the array becomes \([2]\) (or \([1]\)), and the bitwise AND becomes \(2\) (or \(1\)). This can be proven to be the optimal, so the answer is \(1\).In the second test case...
Input: 5 1 2 2 8 4 5 1 5 100000 200000 | Output: 1 3 0 2 31072
Easy
3
411
261
72
16
1,862
G
1862G
G. The Great Equalizer
2,000
binary search; data structures; math; sortings
Tema bought an old device with a small screen and a worn-out inscription ""The Great Equalizer"" on the side.The seller said that the device needs to be given an array \(a\) of integers as input, after which ""The Great Equalizer"" will work as follows: Sort the current array in non-decreasing order and remove duplicat...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Then follows the description of each test case.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the size of the array \(a\) that Tema initially came up with.The ...
For each test case, output \(q\) integers β€” the output values of the device after each operation.
Let's consider the first example of the input.Initially, the array of numbers given as input to the device will be \([6, 4, 8]\). It will change as follows: $$$\([6, 4, 8] \rightarrow [4, 6, 8] \rightarrow [7, 8, 9] \rightarrow [10, 10, 10] \rightarrow [10]\)\(Then, the array of numbers given as input to the device wil...
Input: 432 4 831 62 103 151 2 2 2 215 325 671 21 71 72 51 22 72 252 5 1 10 6101 74 82 51 42 83 41 93 73 43 1 | Output: 10 12 15 4 10 8 8 9 8 12 2 14 12 12 11 11 10 11 10 11 14
Hard
4
1,293
878
97
18
1,580
C
1580C
C. Train Maintenance
2,200
brute force; data structures; implementation
Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains.There are \(n\) models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following \(m\) days, one train will be added, or...
The first line contains two integers \(n\), \(m\) (\(1 \le n,m \le 2 \cdot 10^5\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i,y_i\) (\(1 \le x_i,y_i \le 10^9\)).Each of the next \(m\) lines contains two integers \(op\), \(k\) (\(1 \le k \le n\), \(op = 1\) or \(op = 2\)). If \(op=1\), it means thi...
Print \(m\) lines, The \(i\)-th of these lines contains one integers, denoting the number of trains in maintenance in the \(i\)-th day.
Consider the first example:The first day: Nitori adds a train of model \(3\). Only a train of model \(3\) is running and no train is in maintenance.The second day: Nitori adds a train of model \(1\). A train of model \(1\) is running and a train of model \(3\) is in maintenance.The third day: Nitori removes a train of ...
Input: 3 4 10 15 12 10 1 1 1 3 1 1 2 1 2 3 | Output: 0 1 0 0
Hard
3
746
607
135
15
278
B
278B
B. New Problem
1,500
brute force; strings
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems β€” the strings, consisting of lowercase English letters. Your t...
The first line contains integer n (1 ≀ n ≀ 30) β€” the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Print a string, consisting of lowercase English letters β€” the lexicographically minimum shortest original title.
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Input: 5threehorsesgoodsubstringssecretprimematrixbeautifulyear | Output: j
Medium
2
940
281
112
2
1,438
A
1438A
A. Specific Tastes of Andre
800
constructive algorithms; implementation
Andre has very specific tastes. Recently he started falling in love with arrays.Andre calls an nonempty array \(b\) good, if sum of its elements is divisible by the length of this array. For example, array \([2, 3, 1]\) is good, as sum of its elements β€” \(6\) β€” is divisible by \(3\), but array \([1, 1, 2, 3]\) isn't go...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first and only line of every test case contains a single integer \(n\) (\(1 \le n \le 100\)).
For every test, output any perfect array of length \(n\) on a separate line.
Array \([19, 33]\) is perfect as all \(3\) its subarrays: \([19]\), \([33]\), \([19, 33]\), have sums divisible by their lengths, and therefore are good.
Input: 3 1 2 4 | Output: 24 19 33 7 37 79 49
Beginner
2
914
253
76
14
401
B
401B
B. Sereja and Contests
1,200
greedy; implementation; math
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot b...
The first line contains two integers: x (1 ≀ x ≀ 4000) β€” the round Sereja is taking part in today, and k (0 ≀ k < 4000) β€” the number of rounds he took part in.Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding lin...
Print in a single line two integers β€” the minimum and the maximum number of rounds that Sereja could have missed.
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. I...
Input: 3 22 12 2 | Output: 0 0
Easy
3
1,257
699
113
4
1,851
F
1851F
F. Lisa and the Martians
1,800
bitmasks; greedy; math; strings; trees
Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than \(2^k\), for example, when \(k = 12\), the numbers \(51\), \(1960\), \(0\) are martian, and the numbers \(\pi\)...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” number of testcases.Each testcase is described by two lines.The first line contains integers \(n, k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le k \le 30\)) β€” the length of the sequence of martian numbers and the value of \(k\).The second line contains \(n\)...
For each testcase, print three integers \(i, j, x\) (\(1 \le i, j \le n\), \(i \neq j\), \(0 \le x < 2^k\)). The value of \((a_i \oplus x) \& (a_j \oplus x)\) should be the maximum possible.If there are several solutions, you can print any one.
First testcase: \((3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13\).Second testcase: \((1 \oplus 0) \& (1 \oplus 0) = 1\).Third testcase: \((9 \oplus 4082) \& (13 \oplus 4082) = 4091\).Fourth testcase: \((3 \oplus 7) \& (0 \oplus 7) = 4\).
Input: 105 43 9 1 4 133 11 0 16 12144 1580 1024 100 9 134 37 3 0 43 20 0 12 412 29 46 14 9 4 4 4 5 10 22 11 02 411 49 42 11 10 1 6 9 11 0 5 | Output: 1 3 14 1 3 0 5 6 4082 2 3 7 1 2 3 1 2 15 4 5 11 1 2 0 1 2 0 2 7 4
Medium
5
1,040
501
244
18
1,344
B
1344B
B. Monopole Magnets
2,000
constructive algorithms; dfs and similar; dsu; graphs
A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care.There is an \(n\times m\) grid. Initially, you may place some north magnets and some south magnets into the cells. You are...
The first line contains two integers \(n\) and \(m\) (\(1\le n,m\le 1000\)) β€” the number of rows and the number of columns, respectively.The next \(n\) lines describe the coloring. The \(i\)-th of these lines contains a string of length \(m\), where the \(j\)-th character denotes the color of the cell in row \(i\) and ...
Output a single integer, the minimum possible number of north magnets required.If there is no placement of magnets that satisfies all conditions, print a single integer \(-1\).
In the first test, here is an example placement of magnets: In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule \(3\) since we can move the north magnet down onto a white square. The second ex...
Input: 3 3 .#. ### ##. | Output: 1
Hard
4
1,488
478
176
13
1,179
D
1179D
D. Fedor Runs for President
2,700
data structures; dp; trees
Fedor runs for president of Byteland! In the debates, he will be asked how to solve Byteland's transport problem. It's a really hard problem because of Byteland's transport system is now a tree (connected graph without cycles). Fedor's team has found out in the ministry of transport of Byteland that there is money in t...
The first line contains one integer \(n\) (\(2 \leq n \leq 500\ 000\)) β€” number of vertices in Byteland's transport system.Each of the following \(n - 1\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \leq v_i, u_i \leq n\)). It's guaranteed that the graph is tree.
Print exactly one integer β€” a maximal number of simple paths that can be achieved after adding one edge.
Input: 2 1 2 | Output: 2
Master
3
1,035
272
104
11
566
G
566G
G. Max and Min
2,500
geometry
Two kittens, Max and Min, play with a pair of non-negative integers x and y. As you can guess from their names, kitten Max loves to maximize and kitten Min loves to minimize. As part of this game Min wants to make sure that both numbers, x and y became negative at the same time, and kitten Max tries to prevent him from...
The first line contains two integers, n and m (1 ≀ n, m ≀ 100 000) β€” the number of pairs of numbers available to Max and Min, correspondingly.The second line contains two integers x, y (1 ≀ x, y ≀ 109) β€” the initial values of numbers with which the kittens are playing.Next n lines contain the pairs of numbers ai, bi (1...
Print Β«MaxΒ» (without the quotes), if kitten Max wins, or ""Min"" (without the quotes), if kitten Min wins.
In the first test from the statement Min can respond to move (2, 3) by move (3, 10), and to move (3, 2) by move (10, 3). Thus, for each pair of Max and Min's moves the values of both numbers x and y will strictly decrease, ergo, Min will win sooner or later.In the second sample test after each pair of Max and Min's mov...
Input: 2 242 432 33 23 1010 3 | Output: Min
Expert
1
1,000
463
106
5
1,151
D
1151D
D. Stas and the Queue at the Buffet
1,600
greedy; math; sortings
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of \(n\) high school students numbered from \(1\) to \(n\). Initially, each student \(i\) is on position \(i\). Each student \(i\) is characterized by two numbers β€” \(a_i\) and \(b_i\). Dissatisfaction of the pers...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the number of people in the queue.Each of the following \(n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq 10^8\)) β€” the characteristic of the student \(i\), initially on the position \(i\).
Output one integer β€” minimum total dissatisfaction which can be achieved by rearranging people in the queue.
In the first example it is optimal to put people in this order: (\(3, 1, 2\)). The first person is in the position of \(2\), then his dissatisfaction will be equal to \(4 \cdot 1+2 \cdot 1=6\). The second person is in the position of \(3\), his dissatisfaction will be equal to \(2 \cdot 2+3 \cdot 0=4\). The third perso...
Input: 3 4 2 2 3 6 1 | Output: 12
Medium
3
856
291
108
11
1,284
A
1284A
A. New Year and Naming
800
implementation; strings
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μžλ…„, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.There are two sequences of \(n\) strings \(s_1, s_2, s_3, \ldots, s_{n}\) and \(m\) strings \(t_1, t_2...
The first line contains two integers \(n, m\) (\(1 \le n, m \le 20\)).The next line contains \(n\) strings \(s_1, s_2, \ldots, s_{n}\). Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least \(1\) and at most \(10\).The next line contains \(m\) strings \(t_1...
Print \(q\) lines. For each line, print the name of the year as per the rule described above.
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal.
Input: 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 | Output: sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja
Beginner
2
1,463
781
93
12
1,215
A
1215A
A. Yellow Cards
1,000
greedy; implementation; math
The final match of the Berland Football Cup has been held recently. The referee has shown \(n\) yellow cards throughout the match. At the beginning of the match there were \(a_1\) players in the first team and \(a_2\) players in the second team.The rules of sending players off the game are a bit different in Berland fo...
The first line contains one integer \(a_1\) \((1 \le a_1 \le 1\,000)\) β€” the number of players in the first team.The second line contains one integer \(a_2\) \((1 \le a_2 \le 1\,000)\) β€” the number of players in the second team.The third line contains one integer \(k_1\) \((1 \le k_1 \le 1\,000)\) β€” the maximum number ...
Print two integers β€” the minimum and the maximum number of players that could have been thrown out of the game.
In the first example it could be possible that no player left the game, so the first number in the output is \(0\). The maximum possible number of players that could have been forced to leave the game is \(4\) β€” one player from the first team, and three players from the second.In the second example the maximum possible...
Input: 2 3 5 1 8 | Output: 0 4
Beginner
3
975
807
111
12
2,073
M
2073M
3,300
Master
0
0
0
0
20
1,697
F
1697F
F. Too Many Constraints
2,800
2-sat; constructive algorithms; graphs; implementation
You are asked to build an array \(a\), consisting of \(n\) integers, each element should be from \(1\) to \(k\).The array should be non-decreasing (\(a_i \le a_{i+1}\) for all \(i\) from \(1\) to \(n-1\)). You are also given additional constraints on it. Each constraint is of one of three following types: \(1~i~x\): \(...
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 three integers \(n, m\) and \(k\) (\(2 \le n \le 2 \cdot 10^4\); \(0 \le m \le 2 \cdot 10^4\); \(2 \le k \le 10\)).The \(i\)-th of the next \(m\) lines contains a description of a con...
For each testcase, determine if there exists a non-decreasing array that satisfies all conditions. If there is no such array, then print -1. Otherwise, print any valid array β€” \(n\) integers from \(1\) to \(k\).
Input: 44 0 42 2 33 1 2 31 2 23 3 21 1 12 2 3 23 2 3 25 5 53 2 5 72 4 5 103 4 5 63 3 4 72 1 5 7 | Output: 1 2 3 4 1 3 -1 1 2 2 5 5
Master
4
589
843
211
16
1,605
D
1605D
D. Treelabeling
2,100
bitmasks; constructive algorithms; dfs and similar; games; greedy; implementation; trees
Eikooc and Sushi play a game.The game is played on a tree having \(n\) nodes numbered \(1\) to \(n\). Recall that a tree having \(n\) nodes is an undirected, connected graph with \(n-1\) edges.They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choic...
The first line contains a single integer \(t~(1 \le t \le 10^5)\) β€” the number of test cases. The description of each test case is as follows.The first line of each test case contains an integer \(n~(1 \le n \le 2 \cdot 10^5)\) β€” the number of nodes in the tree.The next \(n-1\) lines contain two integers \(u\) and \(v\...
For each test case print any suitable relabeling β€” a permutation of length \(n\) which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them.
In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win.In the second test case, \(1 \oplus 2 = 3 \nleq min(1, 2)\). Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both \(\{1, 2\}\) and ...
Input: 3 1 2 1 2 3 1 2 1 3 | Output: 1 2 1 1 2 3
Hard
7
2,037
496
253
16
1,519
A
1519A
A. Red and Blue Beans
800
math
You have \(r\) red and \(b\) blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans \(r_i \ge 1\)); has at least one blue bean (or the number of blue beans \(b_i \ge 1\)); the number of red and blue beans should ...
The first line contains the single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first and only line of each test case contains three integers \(r\), \(b\), and \(d\) (\(1 \le r, b \le 10^9\); \(0 \le d \le 10^9\)) β€” the number of red and blue beans and the maximum absolute difference in each pack...
For each test case, if you can distribute all beans, print YES. Otherwise, print NO.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
In the first test case, you can form one packet with \(1\) red and \(1\) blue bean. The absolute difference \(|1 - 1| = 0 \le d\).In the second test case, you can form two packets: \(1\) red and \(4\) blue beans in the first packet and \(1\) red and \(3\) blue beans in the second one.In the third test case, since \(b =...
Input: 4 1 1 0 2 7 3 6 1 4 5 4 0 | Output: YES YES NO NO
Beginner
1
405
323
223
15
1,988
C
1988C
C. Increasing Sequence with Fixed OR
1,300
bitmasks; constructive algorithms; greedy
You are given a positive integer \(n\). Find the longest sequence of positive integers \(a=[a_1,a_2,\ldots,a_k]\) that satisfies the following conditions, and print the sequence: \(a_i\le n\) for all \(1\le i\le k\). \(a\) is strictly increasing. That is, \(a_i>a_{i-1}\) for all \(2\le i\le k\). \(a_i\,|\,a_{i-1}=n\) f...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The only line of each test case contains one integer \(n\) (\(1\le n\le 10^{18}\)).It's guaranteed that the sum of lengths of the longest valid sequences does not ...
For each testcase, print two lines. In the first line, print the length of your constructed sequence, \(k\). In the second line, print \(k\) positive integers, denoting the sequence. If there are multiple longest sequences, you can print any of them.
Input: 4131423 | Output: 1 1 3 1 2 3 4 4 10 12 14 5 7 18 21 22 23
Easy
3
389
343
250
19
1,709
A
1709A
A. Three Doors
800
brute force; greedy; implementation; math
There are three doors in front of you, numbered from \(1\) to \(3\) from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.There are three keys β€” one for each door. Two of them are hidden behind the doors, so that there is no more than on...
The first line contains a single integer \(t\) (\(1 \le t \le 18\)) β€” the number of testcases.The first line of each testcase contains a single integer \(x\) (\(1 \le x \le 3\)) β€” the number on the key in your hands.The second line contains three integers \(a, b\) and \(c\) (\(0 \le a, b, c \le 3\)) β€” the number on the...
For each testcase, print ""YES"" if you can open all the doors. Otherwise, print ""NO"".
Input: 430 1 210 3 223 1 021 3 0 | Output: YES NO YES NO
Beginner
4
557
490
88
17
1,404
E
1404E
E. Bricks
2,800
flows; graph matchings; graphs
A brick is defined as a rectangle with integer side lengths with either width \(1\) or height \(1\) (or both).There is an \(n\times m\) grid, and each cell is colored either black or white. A tiling is a way to place bricks onto the grid such that each black cell is covered by exactly one brick, and each white cell is ...
The first line contains two integers \(n\), \(m\) (\(1\le n, m\le 200\)) β€” the number of rows and columns, respectively.The next \(n\) lines describe the grid. The \(i\)-th of these lines contains a string of length \(m\), where the \(j\)-th character denotes the color of the cell in row \(i\), column \(j\). A black ce...
Output a single integer, the minimum number of bricks required.
The first test case can be tiled with \(4\) bricks placed vertically.The third test case can be tiled with \(18\) bricks like this:
Input: 3 4 #.## #### ##.. | Output: 4
Master
3
635
432
63
14