contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,325
D
1325D
D. Ehab the Xorcist
1,700
bitmasks; constructive algorithms; greedy; number theory
Given 2 integers \(u\) and \(v\), find the shortest array such that bitwise-xor of its elements is \(u\), and the sum of its elements is \(v\).
The only line contains 2 integers \(u\) and \(v\) \((0 \le u,v \le 10^{18})\).
If there's no array that satisfies the condition, print ""-1"". Otherwise:The first line should contain one integer, \(n\), representing the length of the desired array. The next line should contain \(n\) positive integers, the array itself. If there are multiple possible answers, print any.
In the first sample, \(3\oplus 1 = 2\) and \(3 + 1 = 4\). There is no valid array of smaller length.Notice that in the fourth sample the array is empty.
Input: 2 4 | Output: 2 3 1
Medium
4
143
78
292
13
1,572
C
1572C
C. Paint
2,700
dp; greedy
You are given a \(1\) by \(n\) pixel image. The \(i\)-th pixel of the image has color \(a_i\). For each color, the number of pixels of that color is at most \(20\).You can perform the following operation, which works like the bucket tool in paint programs, on this image: pick a color — an integer from \(1\) to \(n\); c...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)).The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3\cdot10^3\)) — the number of pixels in the image.The second line of each test case contains \(n\) integers \(a_1, a_2, \l...
For each test case, print one integer: the minimum number of operations needed to make all the pixels in the image have the same color.
In the first example, the optimal solution is to apply the operation on the third pixel changing its color to \(2\) and then to apply the operation on any pixel that has color \(2\) changing its color and the color of all pixels connected to it to \(1\). The sequence of operations is then: \([1, 2, 3, 2, 1] \to [1, 2, ...
Input: 3 5 1 2 3 2 1 4 1 1 2 2 5 1 2 1 4 2 | Output: 2 1 3
Master
2
670
559
135
15
63
B
63B
B. Settlers' Training
1,200
implementation
In a strategic computer game ""Settlers II"" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either i...
The first line contains two integers n and k (1 ≤ n, k ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1 ≤ i ≤ n, 1 ≤ ai...
Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
In the first example the ranks will be raised in the following manner:1 2 2 3 → 2 2 3 4 → 2 3 4 4 → 3 4 4 4 → 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins.
Input: 4 41 2 2 3 | Output: 4
Easy
1
1,284
326
105
0
949
C
949C
C. Data Center Maintenance
1,900
dfs and similar; graphs
BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).Main feature of services offered by BigData Inc. is the access availability guarantee even under the circ...
The first line of input contains three integers n, m and h (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000, 2 ≤ h ≤ 100 000), the number of company data centers, number of clients and the day length of day measured in hours. The second line of input contains n integers u1, u2, ..., un (0 ≤ uj < h), j-th of these numbers is an index ...
In the first line print the minimum possible number of data centers k (1 ≤ k ≤ n) that have to be included in an experiment in order to keep the data available for any client.In the second line print k distinct integers x1, x2, ..., xk (1 ≤ xi ≤ n), the indices of data centers whose maintenance time will be shifted by ...
Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.On the other hand, ...
Input: 3 3 54 4 01 33 23 1 | Output: 13
Hard
2
2,628
661
535
9
2,085
A
2085A
A. Serval and String Theory
900
constructive algorithms; implementation
A string \(r\) consisting only of lowercase Latin letters is called universal if and only if \(r\) is lexicographically smaller\(^{\text{∗}}\) than the reversal\(^{\text{†}}\) of \(r\).You are given a string \(s\) consisting of \(n\) lowercase Latin letters. You are required to make \(s\) universal. To achieve this, yo...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1\le n\le 100\), \(0\le k\le 10^4\)) — the length of the string \(s\), and the maximu...
For each test case, print ""YES"" if it is possible to make \(s\) universal by performing the operation at most \(k\) times. Otherwise, print ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
In the first test case, \(s\) will keep the same after any operations. However, the reversal of \(\texttt{a}\) is still \(\texttt{a}\), so it is impossible to make \(s\) universal.In the second test case, the string \(\texttt{rev}\) is lexicographically smaller than \(\texttt{ver}\). Thus, \(s\) is already universal.In...
Input: 81 10000a3 3rev6 0string6 0theory9 2universal19 0codeforcesecrofedoc19 1codeforcesecrofedoc3 1zzz | Output: NO YES NO YES YES NO YES NO
Beginner
2
1,095
443
310
20
763
C
763C
C. Timofey and remoduling
2,600
brute force; implementation; math; number theory
Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere.One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. Ti...
The first line contains two integers m and n (2 ≤ m ≤ 109 + 7, 1 ≤ n ≤ 105, m is prime) — Timofey's favorite prime module and the length of the sequence.The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai < m) — the elements of the sequence.
Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo m.Otherwise, print two integers — the first element of the obtained progression x (0 ≤ x < m) and its difference d (0 ≤ d < m).If there are multiple answers, print any of them.
Input: 17 50 2 4 13 15 | Output: 13 2
Expert
4
624
258
303
7
928
C
928C
C. Dependency management
1,900
*special; graphs; implementation
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length ...
The first line contains an only integer n (1 ≤ n ≤ 1 000) — the number of projects in Vaja.The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the depe...
Output all Polycarp's project's dependencies in lexicographical order.
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «a» (version 3) depends on are painted black. The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «code...
Input: 4a 32b 1c 1 b 20 b 11b 2 c 11b 2 | Output: 2b 1c 1
Hard
3
1,756
672
70
9
1,714
D
1714D
D. Color with Occurrences
1,600
brute force; data structures; dp; greedy; strings
You are given some text \(t\) and a set of \(n\) strings \(s_1, s_2, \dots, s_n\). In one step, you can choose any occurrence of any string \(s_i\) in the text \(t\) and color the corresponding characters of the text in red. For example, if \(t=\texttt{bababa}\) and \(s_1=\texttt{ba}\), \(s_2=\texttt{aba}\), you can ge...
The first line of the input contains an integer \(q\) (\(1 \le q \le 100\)) —the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains the text \(t\) (\(1 \le |t| \le 100\)), consisting only of lowercase Latin letters, where \(|t|\) is the length of the tex...
For each test case, print the answer on a separate line.If it is impossible to color all the letters of the text in red, print a single line containing the number -1.Otherwise, on the first line, print the number \(m\) — the minimum number of steps it will take to turn all the letters \(t\) red.Then in the next \(m\) l...
The first test case is explained in the problem statement.In the second test case, it is impossible to color all the letters of the text in red.
Input: 6bababa2baabacaba2bacacababacabaca3ababacacabaca3acbcodeforces4defcodeefoforcesaaaabbbbcccceeee4eeeeccccaaaabbbb | Output: 3 2 2 1 1 2 4 -1 4 1 1 2 6 3 3 3 7 4 3 1 1 2 2 3 1 4 2 4 5 2 1 4 3 1 4 5 2 9 1 13
Medium
5
1,250
632
623
17
1,296
A
1296A
A. Array with Odd Sum
800
math
You are given an array \(a\) consisting of \(n\) integers.In one move, you can choose two indices \(1 \le i, j \le n\) such that \(i \ne j\) and set \(a_i := a_j\). You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operatio...
The first line of the input contains one integer \(t\) (\(1 \le t \le 2000\)) — the number of test cases.The next \(2t\) lines describe test cases. The first line of the test case contains one integer \(n\) (\(1 \le n \le 2000\)) — the number of elements in \(a\). The second line of the test case contains \(n\) integer...
For each test case, print the answer on it — ""YES"" (without quotes) if it is possible to obtain the array with an odd sum of elements, and ""NO"" otherwise.
Input: 5 2 2 3 4 2 2 8 8 3 3 3 3 4 5 5 5 5 4 1 1 1 1 | Output: YES NO YES NO NO
Beginner
1
561
526
158
12
231
D
231D
D. Magic Box
1,600
brute force; geometry
One day Vasya was going home when he saw a box lying on the road. The box can be represented as a rectangular parallelepiped. Vasya needed no time to realize that the box is special, as all its edges are parallel to the coordinate axes, one of its vertices is at point (0, 0, 0), and the opposite one is at point (x1, y1...
The fist input line contains three space-separated integers x, y and z (|x|, |y|, |z| ≤ 106) — the coordinates of Vasya's position in space. The second line contains three space-separated integers x1, y1, z1 (1 ≤ x1, y1, z1 ≤ 106) — the coordinates of the box's vertex that is opposite to the vertex at point (0, 0, 0). ...
Print a single integer — the sum of all numbers on the box faces that Vasya sees.
The first sample corresponds to perspective, depicted on the picture. Vasya sees numbers a2 (on the top face that is the darkest), a6 (on the right face that is the lightest) and a4 (on the left visible face).In the second sample Vasya can only see number a4.
Input: 2 2 21 1 11 2 3 4 5 6 | Output: 12
Medium
2
1,623
527
81
2
1,286
B
1286B
B. Numbers on Tree
1,800
constructive algorithms; data structures; dfs and similar; graphs; greedy; trees
Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from \(1\) to \(n\). Each of its vertices also has an integer \(a_i\) written on it. For each vertex \(i\), Evlampiy calculated \(c_i\) — the number of vertices \(j\) in the subtree of vertex \(i\), such that \(a_j < a_i\). Illustration for the se...
The first line contains an integer \(n\) \((1 \leq n \leq 2000)\) — the number of vertices in the tree.The next \(n\) lines contain descriptions of vertices: the \(i\)-th line contains two integers \(p_i\) and \(c_i\) (\(0 \leq p_i \leq n\); \(0 \leq c_i \leq n-1\)), where \(p_i\) is the parent of vertex \(i\) or \(0\)...
If a solution exists, in the first line print ""YES"", and in the second line output \(n\) integers \(a_i\) \((1 \leq a_i \leq {10}^{9})\). If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all \(a_i\) are between \(1\) and \(10^9\).If ...
Input: 3 2 0 0 2 2 0 | Output: YES 1 2 1
Medium
6
634
533
357
12
1,182
C
1182C
C. Beautiful Lyrics
1,700
data structures; greedy; strings
You are given \(n\) words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible.Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beau...
The first line contains single integer \(n\) (\(1 \le n \le 10^{5}\)) — the number of words.The \(i\)-th of the next \(n\) lines contains string \(s_{i}\) consisting lowercase alphabet letters — the \(i\)-th word. It is guaranteed that the sum of the total word length is equal or less than \(10^{6}\). Each word contain...
In the first line, print \(m\) — the number of maximum possible beautiful lyrics.In next \(2m\) lines, print \(m\) beautiful lyrics (two lines per lyric).If there are multiple answers, print any.
In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. ""about proud hooray round"" forms a beautiful lyric because ""about"" and ""hooray"" have same number of vowels, ""proud"" and ""round"" have same number of vowels, and...
Input: 14 wow this is the first mcdics codeforces round hooray i am proud about that | Output: 3 about proud hooray round wow first this is i that mcdics am
Medium
3
1,614
341
195
11
436
A
436A
A. Feed with Candy
1,500
greedy
The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. One day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of hi centimeters above the f...
The first line contains two integers, n and x (1 ≤ n, x ≤ 2000) — the number of sweets Evan has and the initial height of Om Nom's jump. Each of the following n lines contains three integers ti, hi, mi (0 ≤ ti ≤ 1; 1 ≤ hi, mi ≤ 2000) — the type, height and the mass of the i-th candy. If number ti equals 0, then the cur...
Print a single integer — the maximum number of candies Om Nom can eat.
One of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7. Now...
Input: 5 30 2 41 3 10 8 30 20 101 5 5 | Output: 4
Medium
1
707
379
70
4
632
B
632B
B. Alice, Bob, Two Teams
1,400
brute force; constructive algorithms
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.The way to split up game pieces is split into several steps: First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignm...
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).
Print the only integer a — the maximum strength Bob can achieve.
In the first sample Bob should flip the suffix of length one.In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.In the third sample Bob should do nothing.
Input: 51 2 3 4 5ABABA | Output: 11
Easy
2
863
277
64
6
1,137
F
1137F
F. Matches Are Not a Child's Play
3,400
data structures; trees
Lena is playing with matches. The natural question arising in the head of any child playing with matches is whether it's possible to set a tree on fire with a matches, or not.Let's say, that the tree is a connected graph without cycles and the vertices are labeled with integers \(1, 2, \ldots, n\). Also every vertex \(...
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 200\,000\), \(1 \le q \le 200\,000\)) — the number of vertices in the tree and the number of queries.The \(i\)-th of the following \(n - 1\) lines contains two integers \(v_i\), \(u_i\) (\(1 \le v_i, u_i \le n\)), denoting the endpoints of the \(i\)-th...
For every query of type ""when"" print one integer in range from \(1\) to \(n\) — the step at which the vertex \(v\) will burn out.For every query of type ""compare"" print either \(v\) or \(u\), depending on which one will burn out earlier.
In the first example, the process of burning of the tree is illustrated on the following picture: In particular, the vertices of the tree will burn out in the following order: \([2, 4, 3, 1, 5]\).In the second example, after applying the ""up"" operation, the order of vertices will change to: \([2, 4, 3, 5, 1]\).
Input: 5 7 1 5 1 2 1 3 4 3 when 1 when 2 when 3 when 4 when 5 compare 2 3 compare 3 4 | Output: 4 1 3 2 5 2 4
Master
2
1,919
814
241
11
1,129
A2
1129A2
A2. Toy Train
1,800
brute force; greedy
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of \(n\) stations, enumerated from \(1\) through \(n\). The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train wil...
The first line contains two space-separated integers \(n\) and \(m\) (\(2 \leq n \leq 5\,000\); \(1 \leq m \leq 20\,000\)) — the number of stations and the number of candies, respectively.The \(i\)-th of the following \(m\) lines contains two space-separated integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\); \(a...
In the first and only line, print \(n\) space-separated integers, the \(i\)-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station \(i\).
Consider the second sample.If the train started at station \(1\), the optimal strategy is as follows. Load the first candy onto the train. Proceed to station \(2\). This step takes \(1\) second. Deliver the first candy. Proceed to station \(1\). This step takes \(1\) second. Load the second candy onto the train. Procee...
Input: 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 | Output: 10 9 10 10 9
Medium
2
1,441
440
205
11
1,474
E
1474E
E. What Is It?
2,500
constructive algorithms; greedy
Lunar rover finally reached planet X. After landing, he met an obstacle, that contains permutation \(p\) of length \(n\). Scientists found out, that to overcome an obstacle, the robot should make \(p\) an identity permutation (make \(p_i = i\) for all \(i\)).Unfortunately, scientists can't control the robot. Thus the o...
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each of next \(t\) lines contains the single integer \(n\) (\(2 \leq n \leq 10^5\)) – the length of \(p\).Note, that \(p\) is not given to you. You should find the maximum possible time over all permutations of l...
For each test case in the first line, print how many seconds will the robot spend in the worst case.In the next line, print the initial value of \(p\) that you used to construct an answer.In the next line, print the number of operations \(m \leq n\) that the robot makes in your example.In the each of next \(m\) lines p...
For \(n = 2\), \(p\) can be either \([1, 2]\) or \([2, 1]\). In the first case \(p\) is already identity, otherwise robot will make it an identity permutation in \(1\) second regardless of choise \(i\) and \(j\) on the first operation.For \(n = 3\), \(p\) can be equals \([2, 3, 1]\). If robot will select \(i = 3, j = 2...
Input: 3 2 3 3 | Output: 1 2 1 1 2 1 5 2 3 1 2 1 3 3 2 5 2 3 1 2 1 3 2 3
Expert
2
1,335
422
481
14
213
A
213A
A. Game
1,700
dfs and similar; greedy
Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies....
The first line contains integer n (1 ≤ n ≤ 200) — the number of game parts. The next line contains n integers, the i-th integer — ci (1 ≤ ci ≤ 3) represents the number of the computer, on which you can complete the game part number i. Next n lines contain descriptions of game parts. The i-th line first contains integer...
On a single line print the answer to the problem.
Note to the second sample: before the beginning of the game the best strategy is to stand by the third computer. First we complete part 5. Then we go to the 1-st computer and complete parts 3 and 4. Then we go to the 2-nd computer and complete parts 1 and 2. In total we get 1+1+2+1+2, which equals 7 hours.
Input: 110 | Output: 1
Medium
2
1,398
668
49
2
1,285
E
1285E
E. Delete a Segment
2,300
brute force; constructive algorithms; data structures; dp; graphs; sortings; trees; two pointers
There are \(n\) segments on a \(Ox\) axis \([l_1, r_1]\), \([l_2, r_2]\), ..., \([l_n, r_n]\). Segment \([l, r]\) covers all points from \(l\) to \(r\) inclusive, so all \(x\) such that \(l \le x \le r\).Segments can be placed arbitrarily — be inside each other, coincide and so on. Segments can degenerate into points, ...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases in the test. Then the descriptions of \(t\) test cases follow.The first of each test case contains a single integer \(n\) (\(2 \le n \le 2\cdot10^5\)) — the number of segments in the given set. Then \(n\) lines follow, each cont...
Print \(t\) integers — the answers to the \(t\) given test cases in the order of input. The answer is the maximum number of segments in the union of \(n-1\) segments if you erase any of the given \(n\) segments.
Input: 3 4 1 4 2 3 3 6 5 7 3 5 5 5 5 5 5 6 3 3 1 1 5 5 1 5 2 2 4 4 | Output: 2 1 5
Expert
8
1,989
677
211
12
2,085
B
2085B
B. Serval and Final MEX
1,200
constructive algorithms; implementation
You are given an array \(a\) consisting of \(n\ge 4\) non-negative integers.You need to perform the following operation on \(a\) until its length becomes \(1\): Select two indices \(l\) and \(r\) (\(1\le {\color{red}{ l < r }} \le |a|\)), and replace the subarray \([a_l,a_{l+1},\ldots,a_r]\) with a single integer \(\op...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(4\le n\le 5000\)) — the length of the array \(a\).The second line contains \(n\) integers ...
For each test case, output a single integer \(k\) (\(0\le k\le n\)) in the first line of output — the length of the operation sequence.Then, output \(k\) lines, the \(i\)-th line containing two integers \(l_i\) and \(r_i\) (\(1\le l_i<r_i\le |a|\)) — the two indices you choose in the \(i\)-th operation, where \(|a|\) d...
In the first test case, since \(\operatorname{mex}([1,2,3,4])=0\), after the only operation, the array becomes \([0]\).In the second test case, the array \(a\) changes as follows: $$$\( [\underline{0,1},0,0,1]\to [\underline{2,0},0,1]\to [\underline{1,0},1]\to [\underline{2,1}]\to [0]. \)\(In the third test case, the a...
Input: 641 2 3 450 1 0 0 160 0 0 0 0 065 4 3 2 1 040 0 1 141 0 0 0 | Output: 1 1 4 4 1 2 1 2 1 2 1 2 4 5 6 3 4 1 2 1 3 3 4 5 4 5 1 4 2 1 2 1 3 2 2 4 1 2
Easy
2
1,392
484
430
20
1,012
A
1012A
A. Photo of The Sky
1,500
brute force; implementation; math; sortings
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.Strictly speaking, it makes a photo of all points with coordinates \((x, y)\), such that \(x_1 \leq x \leq x_2\) and \(y_1 \leq y \leq y_2\), w...
The first line of the input contains an only integer \(n\) (\(1 \leq n \leq 100\,000\)), the number of points in Pavel's records.The second line contains \(2 \cdot n\) integers \(a_1\), \(a_2\), ..., \(a_{2 \cdot n}\) (\(1 \leq a_i \leq 10^9\)), coordinates, written by Pavel in some order.
Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records.
In the first sample stars in Pavel's records can be \((1, 3)\), \((1, 3)\), \((2, 3)\), \((2, 4)\). In this case, the minimal area of the rectangle, which contains all these points is \(1\) (rectangle with corners at \((1, 3)\) and \((2, 4)\)).
Input: 44 1 3 2 3 2 1 3 | Output: 1
Medium
4
1,283
290
117
10
198
B
198B
B. Jumping on Walls
1,400
shortest paths
Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some a...
The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly.The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character ""X"" re...
Print ""YES"" (without the quotes) if the ninja can get out from the canyon, otherwise, print ""NO"" (without the quotes).
In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon.
Input: 7 3---X--X-X--XX- | Output: YES
Easy
1
1,603
519
122
1
1,637
F
1637F
F. Towers
2,500
constructive algorithms; dfs and similar; dp; greedy; trees
You are given a tree with \(n\) vertices numbered from \(1\) to \(n\). The height of the \(i\)-th vertex is \(h_i\). You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency \(e\) costs \(e\) coins, wh...
The first line contains an integer \(n\) (\(2 \le n \le 200\,000\)) — the number of vertices in the tree.The second line contains \(n\) integers \(h_i\) (\(1 \le h_i \le 10^9\)) — the heights of the vertices.Each of the next \(n - 1\) lines contain a pair of numbers \(v_i, u_i\) (\(1 \le v_i, u_i \le n\)) — an edge of ...
Print one integer — the minimum required number of coins.
In the first test case it's optimal to install two towers with efficiencies \(2\) at vertices \(1\) and \(3\).In the second test case it's optimal to install a tower with efficiency \(1\) at vertex \(1\) and two towers with efficiencies \(3\) at vertices \(2\) and \(5\).In the third test case it's optimal to install tw...
Input: 3 1 2 1 1 2 2 3 | Output: 4
Expert
5
763
380
57
16
1,025
F
1025F
F. Disjoint Triangles
2,700
geometry
A point belongs to a triangle if it lies inside the triangle or on one of its sides. Two triangles are disjoint if there is no point on the plane that belongs to both triangles.You are given \(n\) points on the plane. No two points coincide and no three points are collinear.Find the number of different ways to choose t...
The first line of the input contains an integer \(n\) (\(6 \le n \le 2000\)) – the number of points.Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(|x_i|, |y_i| \le 10^9\)) – the coordinates of a point.No two points coincide and no three points are collinear.
Print one integer – the number of ways to choose two disjoint triangles.
In the first example there are six pairs of disjoint triangles, they are shown on the picture below. All other pairs of triangles are not disjoint, for example the following pair:
Input: 61 12 24 64 57 25 3 | Output: 6
Master
1
488
285
72
10
1,882
C
1882C
C. Card Game
1,500
brute force; greedy
There are \(n\) cards stacked in a deck. Initially, \(a_{i}\) is written on the \(i\)-th card from the top. The value written on a card does not change.You will play a game. Initially your score is \(0\). In each turn, you can do one of the following operations: Choose an odd\(^{\dagger}\) positive integer \(i\), which...
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 2 \cdot 10^{5}\)).The second line of each test case contains \(n\) integers \...
For each test case, print a single integer — the maximum score you can get when the game ends.
In the first test case, one can get the score of \(5\) as follows: In the first turn, choose \(i=2\). Your score remains \(0\) and the numbers written on the cards from the top will become \([-4, -3, 5]\). In the second turn, choose \(i=3\). Your score will become \(5\) and the numbers written on the cards from the top...
Input: 44-4 1 -3 541 -2 3 -43-1 3 -51-1 | Output: 5 4 2 0
Medium
2
1,138
472
94
18
675
D
675D
D. Tree Construction
1,800
data structures; trees
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.You are given a sequence \(a\), consisting of \(n\) distinct integers, that is used to construct the binary search tree. Below is the fo...
The first line of the input contains a single integer \(n\) (\(2 \leq n \leq 100\,000\)) — the length of the sequence \(a\).The second line contains \(n\) distinct integers \(a_i\) (\(1 \leq a_i \leq 10^9\)) — the sequence \(a\) itself.
Output \(n - 1\) integers. For all \(i > 1\) print the value written in the node that is the parent of the node with value \(a_i\) in it.
Input: 31 2 3 | Output: 1 2
Medium
2
971
236
137
6
981
D
981D
D. Bookshelves
1,900
bitmasks; dp; greedy
Mr Keks is a typical white-collar in Byteland.He has a bookshelf in his office with some books on it, each book has an integer positive price.Mr Keks defines the value of a shelf as the sum of books prices on it. Miraculously, Mr Keks was promoted and now he is moving into a new office.He learned that in the new office...
The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 50\)) — the number of books and the number of shelves in the new office.The second line contains \(n\) integers \(a_1, a_2, \ldots a_n\), (\(0 < a_i < 2^{50}\)) — the prices of the books in the order they stand on the old shelf.
Print the maximum possible beauty of \(k\) shelves in the new office.
In the first example you can split the books as follows:$$$\((9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.\)\(In the second example you can split the books as follows:\)\((3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.\)$$$
Input: 10 49 14 28 1 7 13 15 29 2 31 | Output: 24
Hard
3
877
305
69
9
203
A
203A
A. Two Problems
1,200
brute force; implementation
A boy Valera registered on site Codeforces as Valera, and wrote his first Codeforces Round #300. He boasted to a friend Arkady about winning as much as x points for his first contest. But Arkady did not believe his friend's words and decided to check whether Valera could have shown such a result.He knows that the conte...
The single line of the input contains six integers x, t, a, b, da, db (0 ≤ x ≤ 600; 1 ≤ t, a, b, da, db ≤ 300) — Valera's result, the contest's duration, the initial cost of the first problem, the initial cost of the second problem, the number of points that the first and the second problem lose per minute, correspondi...
If Valera could have earned exactly x points at a contest, print ""YES"", otherwise print ""NO"" (without the quotes).
In the first sample Valera could have acted like this: he could have submitted the first problem at minute 0 and the second problem — at minute 2. Then the first problem brings him 20 points and the second problem brings him 10 points, that in total gives the required 30 points.
Input: 30 5 20 20 3 5 | Output: YES
Easy
2
1,409
476
118
2
1,896
G
1896G
G. Pepe Racing
3,200
constructive algorithms; implementation; interactive; sortings
This is an interactive problem.There are \(n^2\) pepes labeled \(1, 2, \ldots, n^2\) with pairwise distinct speeds. You would like to set up some races to find out the relative speed of these pepes.In one race, you can choose exactly \(n\) distinct pepes and make them race against each other. After each race, you will ...
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 a single integer \(n\) (\(2 \le n \le 20\)) — the number of pepes in one race.After reading the integer \(n\) for each...
Input: 1 2 2 4 4 3 2 | Output: ? 1 2 ? 3 4 ? 2 4 ? 2 3 ? 2 1 ! 4 3 2
Master
4
816
459
0
18
2,107
F1
2107F1
F1. Cycling (Easy Version)
2,300
binary search; brute force; dp; greedy
This is the easy version of the problem. The difference between the versions is that in this version, \(1\le n\le 5\cdot 10^3\) and you don't need to output the answer for each prefix. You can hack only if you solved all versions of this problem.Leo works as a programmer in the city center, and his lover teaches at a h...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. The first line of each test case contains a positive integer \(n\) (\(1 \leq n \leq 5\cdot 10^3\)), representing the number of the cyclists.The second line of...
For each test case, print one integer representing the minimum cost for Leo to go from behind the \(n\)-th cyclist to in front of the \(1\)-st cyclist.
In the first test case, one possible way to move from the position behind the \(n\)-th cyclist to the position in front of the \(1\)-st cyclist is: Leo swaps \(a_2\) \((i=2)\) and \(a_3\) \((j=3)\), then the array becomes \([1,4,2]\); it costs \(j-i=3-2=1\). Leo is behind the \(3\)-rd cyclist and moves behind the \(2\)...
Input: 431 2 441 1 1 121 244 1 3 2 | Output: 7 4 3 8
Expert
4
1,221
499
151
21
177
G2
177G2
G2. Fibonacci Strings
2,600
matrices; strings
Fibonacci strings are defined as follows: f1 = «a» f2 = «b» fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: ""a"", ""b"", ""ba"", ""bab"", ""babba"".You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substrin...
The first line contains two space-separated integers k and m — the number of a Fibonacci string and the number of queries, correspondingly.Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters ""a"" and ""b"".The input limitations fo...
For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.
Input: 6 5ababbaaba | Output: 35331
Expert
2
322
689
264
1
743
A
743A
A. Vladik and flights
1,200
constructive algorithms; greedy; implementation
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.Vladik knows n airports. All the airports are located on a straight line. Each airport has uniqu...
The first line contains three integers n, a, and b (1 ≤ n ≤ 105, 1 ≤ a, b ≤ n) — the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach. The second line contains a string with length n, which consists only of characters 0 and 1. If the i-th chara...
Print single integer — the minimum cost Vladik has to pay to get to the olympiad.
In the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to th...
Input: 4 1 41010 | Output: 1
Easy
3
1,004
425
81
7
204
B
204B
B. Little Elephant and Cards
1,500
binary search; data structures
The Little Elephant loves to play with color cards.He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks t...
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the ...
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0.In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
Input: 34 74 77 4 | Output: 0
Medium
2
570
499
130
2
1,898
A
1898A
A. Milica and String
800
brute force; implementation; strings
Milica has a string \(s\) of length \(n\), consisting only of characters A and B. She wants to modify \(s\) so it contains exactly \(k\) instances of B. In one operation, she can do the following: Select an integer \(i\) (\(1 \leq i \leq n\)) and a character \(c\) (\(c\) is equal to either A or B). Then, replace each o...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 500\)). The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \leq n \leq 100\), \(0 \leq k \leq n\)) — the length of the string \(s\) and the numbe...
For each test case, in the first line output a single integer \(m\) — the minimum number of operations Milica should perform.In the \(j\)-th of the next \(m\) lines output an integer \(i\) (\(1 \le i \le n\)) and a character \(c\) (\(c\) is 'A' or 'B') — the parameters of the \(j\)-th operation as described in the stat...
In the first test case, there are already \(2\) characters B in \(s\), so Milica does not have to perform any operations.In the second test case, the only way to achieve \(3\) characters B in \(s\) in one operation is to replace the first character of \(s\) by B on the first operation: AABAB \(\rightarrow\) BABAB.In th...
Input: 5 5 2 AAABB 5 3 AABAB 5 0 BBBBB 3 0 BAA 10 3 BBBABBBBAB | Output: 0 1 1 B 1 5 A 1 2 A 1 6 A
Beginner
3
761
496
425
18
2,081
C
2081C
C. Quaternary Matrix
2,700
bitmasks; constructive algorithms; greedy; implementation; matrices
A matrix is called quaternary if all its elements are \(0\), \(1\), \(2\), or \(3\).Ecrade calls a quaternary matrix \(A\) good if the following two properties hold. The bitwise XOR of all numbers in each row of matrix \(A\) is equal to \(0\). The bitwise XOR of all numbers in each column of matrix \(A\) is equal to \(...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^5\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^3\)).This is followed by \(n\) lines, each containing exac...
For each test case, print the minimum number of elements that need to be changed for the matrix to become good on the first line, then print \(n\) lines, each containing exactly \(m\) characters and consisting only of \(0\), \(1\), \(2\), and \(3\), describing one of the possible resulting matrices.If there are multipl...
Input: 53 33131213133 30000000004 401231230230130124 412322110312213114 41232211031221312 | Output: 3 213 101 312 0 000 000 000 0 0123 1230 2301 3012 6 0132 2310 3131 1313 5 0132 2310 3120 1302
Master
5
608
530
370
20
1,103
E
1103E
E. Radix sum
3,400
fft; math; number theory
Let's define radix sum of number \(a\) consisting of digits \(a_1, \ldots ,a_k\) and number \(b\) consisting of digits \(b_1, \ldots ,b_k\)(we add leading zeroes to the shorter number to match longer length) as number \(s(a,b)\) consisting of digits \((a_1+b_1)\mod 10, \ldots ,(a_k+b_k)\mod 10\). The radix sum of sever...
The first line contains integer \(n\) — the length of the array(\(1 \leq n \leq 100000\)).The second line contains \(n\) integers \(x_1, \ldots x_n\) — array elements(\(0 \leq x_i < 100000\)).
Output \(n\) integers \(y_0, \ldots, y_{n-1}\) — \(y_i\) should be equal to corresponding number of ways modulo \(2^{58}\).
In the first example there exist sequences: sequence \((5,5)\) with radix sum \(0\), sequence \((5,6)\) with radix sum \(1\), sequence \((6,5)\) with radix sum \(1\), sequence \((6,6)\) with radix sum \(2\).
Input: 2 5 6 | Output: 1 2
Master
3
696
192
123
11
892
B
892B
B. Wrath
1,200
greedy; implementation; two pointers
Hands that shed innocent blood!There are n guilty people in a line, the i-th of them holds a claw with length Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j ≥ i - Li.You are given...
The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people.Second line contains n space-separated integers L1, L2, ..., Ln (0 ≤ Li ≤ 109), where Li is the length of the i-th person's claw.
Print one integer — the total number of alive people after the bell rings.
In first sample the last person kills everyone in front of him.
Input: 40 1 0 10 | Output: 1
Easy
3
414
211
74
8
150
C
150C
C. Smart Cheater
2,200
data structures; math; probabilities
I guess there's not much point in reminding you that Nvodsk winters aren't exactly hot. That increased the popularity of the public transport dramatically. The route of bus 62 has exactly n stops (stop 1 goes first on its way and stop n goes last). The stops are positioned on a straight line and their coordinates are 0...
The first line contains three integers n, m and c (2 ≤ n ≤ 150 000, 1 ≤ m ≤ 300 000, 1 ≤ c ≤ 10 000).The next line contains n integers xi (0 ≤ xi ≤ 109, x1 = 0, xi < xi + 1) — the coordinates of the stops on the bus's route.The third line contains n - 1 integer pi (0 ≤ pi ≤ 100) — the probability of inspection in perce...
Print the single real number — the maximum expectation of the conductor's profit. 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 .
A comment to the first sample:The first and third passengers get tickets from stop 1 to stop 2. The second passenger doesn't get a ticket. There always is inspection on the segment 1-2 but both passengers have the ticket for it. There never is an inspection on the segment 2-3, that's why the second passenger gets away ...
Input: 3 3 100 10 100100 01 22 31 3 | Output: 90.000000000
Hard
3
1,528
557
315
1
1,923
E
1923E
E. Count Paths
2,000
data structures; dfs and similar; dp; dsu; graphs; trees
You are given a tree, consisting of \(n\) vertices, numbered from \(1\) to \(n\). Every vertex is colored in some color, denoted by an integer from \(1\) to \(n\).A simple path of the tree is called beautiful if: it consists of at least \(2\) vertices; the first and the last vertices of the path have the same color; no...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of vertices in the tree.The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le n\)) — the...
For each testcase, print a single integer — the number of the beautiful simple paths of the tree.
Input: 431 2 11 22 352 1 2 1 21 21 33 44 551 2 3 4 51 21 33 44 542 2 2 23 13 23 4 | Output: 1 3 0 3
Hard
6
569
605
97
19
721
D
721D
D. Maxim and Array
2,000
constructive algorithms; data structures; greedy; math
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of ...
The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.
Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.If there are multiple answers, print any of them.
Input: 5 3 15 4 3 5 2 | Output: 5 4 3 5 -1
Hard
4
660
314
293
7
1,929
C
1929C
C. Sasha and the Casino
1,400
binary search; brute force; constructive algorithms; games; greedy; math
Sasha decided to give his girlfriend the best handbag, but unfortunately for Sasha, it is very expensive. Therefore, Sasha wants to earn it. After looking at earning tips on the internet, he decided to go to the casino.Sasha knows that the casino operates under the following rules. If Sasha places a bet of \(y\) coins ...
Each test 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 single line of each test case contains three integers \(k, x\) and \(a\) (\(2 \leq k \leq 30\), \(1 \leq x \leq 100\), \(1 \leq a \le...
For each test case, output ""YES"" (without quotes) if Sasha can achieve it and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive answer).
In the first test case, Sasha can proceed as follows: If Sasha places a bet for the first time or if he won the previous bet, then he places \(1\) coin. If Sasha lost the previous bet, then he places \(2\) coins.Note that Sasha cannot lose more than once in a row.It can be proven that with this strategy, Sasha can obta...
Input: 92 1 72 1 12 3 153 3 64 4 55 4 74 88 100000000025 69 23113 97 18806 | Output: YES NO YES NO NO YES NO NO NO
Easy
6
1,150
475
256
19
1,822
G2
1822G2
G2. Magic Triples (Hard Version)
2,200
brute force; data structures; math; number theory
This is the hard version of the problem. The only difference is that in this version, \(a_i \le 10^9\).For a given sequence of \(n\) integers \(a\), a triple \((i, j, k)\) is called magic if: \(1 \le i, j, k \le n\). \(i\), \(j\), \(k\) are pairwise distinct. there exists a positive integer \(b\) such that \(a_i \cdot ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of the test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) — the length of the sequence.The second line of the test contains \(n\) integers \(a_...
For each test case, output a single integer — the number of magic triples for the sequence \(a\).
In the first example, there are \(6\) magic triples for the sequence \(a\) — \((2, 3, 5)\), \((2, 5, 3)\), \((3, 2, 5)\), \((3, 5, 2)\), \((5, 2, 3)\), \((5, 3, 2)\).In the second example, there is a single magic triple for the sequence \(a\) — \((2, 1, 3)\).
Input: 751 7 7 2 736 2 1891 2 3 4 5 6 7 8 941000 993 986 17971 10 100 1000 10000 100000 100000081 1 2 2 4 4 8 891 1 1 2 2 2 4 4 4 | Output: 6 1 3 0 9 16 45
Hard
4
577
478
97
18
1,810
A
1810A
A. Beautiful Sequence
800
brute force; greedy
A sequence of \(m\) integers \(a_{1}, a_{2}, \ldots, a_{m}\) is good, if and only if there exists at least one \(i\) (\(1 \le i \le m\)) such that \(a_{i} = i\). For example, \([3,2,3]\) is a good sequence, since \(a_{2} = 2\), \(a_{3} = 3\), while \([3,1,1]\) is not a good sequence, since there is no \(i\) such that \...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. Their description follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) — the length of the given sequence.The second line of each test case contain...
For each test case, output ""YES"" or ""NO""(without quotes) in one line, representing whether the given sequence is beautiful.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
In the first test case, the good subsequence is \(b=[3,2]\), where \(b_{2} = 2\).In the second test case, the good subsequence is \(b=[2,4,3]\), where \(b_{3} = 3\).In the fourth test case, the good subsequence is \(b=[1]\), where \(b_{1} = 1\).In the fifth test case, the good subsequence is \(b=[2,2]\), where \(b_{2} ...
Input: 733 2 142 4 3 552 3 5 5 623 152 4 5 2 345 6 7 866 5 4 3 2 1 | Output: YES YES NO YES YES NO YES
Beginner
2
823
423
288
18
459
D
459D
D. Pashmak and Parmida's problem
1,800
data structures; divide and conquer; sortings
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices...
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the answer to the problem.
Input: 71 2 1 1 2 2 1 | Output: 8
Medium
3
497
148
51
4
1,625
A
1625A
A. Ancient Civilization
800
bitmasks; greedy; math
Martian scientists explore Ganymede, one of Jupiter's numerous moons. Recently, they have found ruins of an ancient civilization. The scientists brought to Mars some tablets with writings in a language unknown to science.They found out that the inhabitants of Ganymede used an alphabet consisting of two letters, and eac...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The following are descriptions of the test cases.The first line contains two integers \(n\) and \(\ell\) (\(1 \le n \le 100\), \(1 \le \ell \le 30\)) — the amount of word forms, and the number of letters in one word.The second li...
For each test, print a single integer, the initial form of the word, i. e. such \(y\) (\(0 \le y \le 2^\ell - 1\)) that the sum \(d(x_i, y)\) over all \(1 \le i \le n\) is minimal possible. Note that \(y\) can differ from all the integers \(x_i\).If there are multiple ways to restore the initial form, print any.
In the first test case, the words can be written as \(x_1 = 10010_2\), \(x_2 = 01001_2\) and \(x_3 = 10101_2\) in binary. Let \(y = 10001_2\). Then, \(d(x_1, y) = 2\) (the difference is in the fourth and the fifth positions), \(d(x_2, y) = 2\) (the difference is in the first and the second positions), \(d(x_3, y) = 1\)...
Input: 73 518 9 213 518 18 181 115 301 2 3 4 56 1099 35 85 46 78 552 10 18 85 16 42 15 83 65 78 42 | Output: 17 18 1 1 39 0 2
Beginner
3
1,752
443
313
16
388
D
388D
D. Fox and Perfect Sets
2,700
math
Fox Ciel studies number theory.She thinks a non-empty set S contains non-negative integers is perfect if and only if for any (a can be equal to b), . Where operation xor means exclusive or operation (http://en.wikipedia.org/wiki/Exclusive_or).Please calculate the number of perfect sets consisting of integers not greate...
The first line contains an integer k (0 ≤ k ≤ 109).
Print a single integer — the number of required sets modulo 1000000007 (109 + 7).
In example 1, there are 2 such sets: {0} and {0, 1}. Note that {1} is not a perfect set since 1 xor 1 = 0 and {1} doesn't contain zero.In example 4, there are 6 such sets: {0}, {0, 1}, {0, 2}, {0, 3}, {0, 4} and {0, 1, 2, 3}.
Input: 1 | Output: 2
Master
1
400
51
81
3
232
C
232C
C. Doe Graphs
2,600
constructive algorithms; divide and conquer; dp; graphs; shortest paths
John Doe decided that some mathematical object must be named after him. So he invented the Doe graphs. The Doe graphs are a family of undirected graphs, each of them is characterized by a single non-negative number — its order. We'll denote a graph of order k as D(k), and we'll denote the number of vertices in the grap...
The first line contains two integers t and n (1 ≤ t ≤ 105; 1 ≤ n ≤ 103) — the number of queries and the order of the given graph. The i-th of the next t lines contains two integers ai and bi (1 ≤ ai, bi ≤ 1016, ai ≠ bi) — numbers of two vertices in the i-th query. It is guaranteed that ai, bi ≤ |D(n)|.Please, do not us...
For each query print a single integer on a single line — the length of the shortest path between vertices ai and bi. Print the answers to the queries in the order, in which the queries are given in the input.
Input: 10 51 21 31 41 52 32 42 53 43 54 5 | Output: 1112123121
Expert
5
1,689
446
208
2
732
A
732A
A. Buy a Shovel
800
brute force; constructive algorithms; implementation; math
Polycarp urgently needs a shovel! He comes to the shop and chooses an appropriate one. The shovel that Policarp chooses is sold for k burles. Assume that there is an unlimited number of such shovels in the shop.In his pocket Polycarp has an unlimited number of ""10-burle coins"" and exactly one coin of r burles (1 ≤ r ...
The single line of input contains two integers k and r (1 ≤ k ≤ 1000, 1 ≤ r ≤ 9) — the price of one shovel and the denomination of the coin in Polycarp's pocket that is different from ""10-burle coins"". Remember that he has an unlimited number of coins in the denomination of 10, that is, Polycarp has enough money to b...
Print the required minimum number of shovels Polycarp has to buy so that he can pay for them without any change.
In the first example Polycarp can buy 9 shovels and pay 9·117 = 1053 burles. Indeed, he can pay this sum by using 10-burle coins and one 3-burle coin. He can't buy fewer shovels without any change.In the second example it is enough for Polycarp to buy one shovel.In the third example Polycarp should buy two shovels and ...
Input: 117 3 | Output: 9
Beginner
4
703
345
112
7
1,644
A
1644A
A. Doors and Keys
800
implementation
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the d...
The first line contains a single integer \(t\) (\(1 \le t \le 720\)) — the number of testcases.Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
In the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it...
Input: 4rgbBRGRgbrBGbBrRgGrgRGBb | Output: YES NO YES NO
Beginner
1
1,051
250
83
16
1,499
B
1499B
B. Binary Removals
1,000
brute force; dp; greedy; implementation
You are given a string \(s\), consisting only of characters '0' or '1'. Let \(|s|\) be the length of \(s\).You are asked to choose some integer \(k\) (\(k > 0\)) and find a sequence \(a\) of length \(k\) such that: \(1 \le a_1 < a_2 < \dots < a_k \le |s|\); \(a_{i-1} + 1 < a_i\) for all \(i\) from \(2\) to \(k\). The c...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of testcases.Then the descriptions of \(t\) testcases follow.The only line of each testcase contains a string \(s\) (\(2 \le |s| \le 100\)). Each character is either '0' or '1'.
For each testcase print ""YES"" if there exists a sequence \(a\) such that removing the characters at positions \(a_1, a_2, \dots, a_k\) and concatenating the parts without changing the order produces a sorted string.Otherwise, print ""NO"".You may print every letter in any case you want (so, for example, the strings y...
In the first testcase you can choose a sequence \(a=[1,3,6,9]\). Removing the underlined letters from ""10101011011"" will produce a string ""0011111"", which is sorted.In the second and the third testcases the sequences are already sorted.In the fourth testcase you can choose a sequence \(a=[3]\). \(s'=\) ""11"", whic...
Input: 5 10101011011 0000 11111 110 1100 | Output: YES YES YES YES NO
Beginner
4
734
260
380
14
1,859
D
1859D
D. Andrey and Escape from Capygrad
1,800
binary search; data structures; dp; dsu; greedy; sortings
An incident occurred in Capygrad, the capital of Tyagoland, where all the capybaras in the city went crazy and started throwing mandarins. Andrey was forced to escape from the city as far as possible, using portals.Tyagoland is represented by a number line, and the city of Capygrad is located at point \(0\). There are ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of sets 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 number of portals.Each of the n...
For each test case, output a single line of \(q\) integers, containing the answers to Andrey's questions.
In the first test case: Optimal actions when starting from each position: Use portal \(1\) and teleport to point \(b_1 = 14\). Use portal \(2\) first and teleport to point \(6\), which is on the segment \([l_1, r_1] = [6, 17]\), then use portal \(1\) and teleport to point \(b_1 = 14\). Stay at point \(x_3=23\) without ...
Input: 536 17 7 141 12 3 816 24 20 22610 2 23 15 28 1833 14 7 1016 24 20 221 16 3 1492 4 6 8 18 23 11 13 1521 4 2 33 9 6 734 8 1518 24 18 241 8 2 411 16 14 1426 32 28 305 10 6 8915 14 13 27 22 17 31 1 769 22 14 2011 26 13 2421 33 22 2321 33 25 321 6 3 418 29 20 21811 23 16 5 8 33 2 21 | Output: 14 14 23 15 28 22 14 14 ...
Medium
6
1,244
873
105
18
700
D
700D
D. Huffman Coding on Segment
3,100
data structures; greedy
Alice wants to send an important message to Bob. Message a = (a1, ..., an) is a sequence of positive integers (characters).To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function f, that maps each letter that appears in the string to some...
The first line of the input contains the single integer n (1 ≤ n ≤ 100 000) — the length of the initial message. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000) — characters of the message.Next line contains the single integer q (1 ≤ q ≤ 100 000) — the number of queries.Then follow q lines with q...
Print q lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring ali... ari.
In the first query, one of the optimal ways to encode the substring is to map 1 to ""0"", 2 to ""10"" and 3 to ""11"".Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample).
Input: 71 2 1 3 1 2 151 71 33 52 44 4 | Output: 103350
Master
2
1,226
617
139
7
629
D
629D
D. Babaei and Birthday Cake
2,000
data structures; dp
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake...
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.
Print the maximum volume of the cake that Babaei can make. 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 .
In first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
Input: 2100 3040 10 | Output: 942477.796077000
Hard
2
822
245
291
6
644
B
644B
B. Processing Queries
1,700
*special; constructive algorithms; data structures; two pointers
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.When a query appears server may react in three possible ways: If server is free and query qu...
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment o...
Print the sequence of n integers e1, e2, ..., en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2...
Input: 5 12 94 810 915 219 1 | Output: 11 19 -1 21 22
Medium
4
1,068
450
234
6
1,995
B1
1995B1
B1. Bouquet (Easy Version)
1,100
binary search; brute force; greedy; sortings; two pointers
This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration.A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of \(n\) flowers in the store, each of which is characterized by the number of petals, and a ...
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) — the number of test cases. This is followed by descriptions of the test cases.The first line of each test case contains two integers \(n\), \(m\) (\(1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18}\)) — the ...
For each test case, output a single integer — the maximum possible number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.
In the first test case, you can assemble a bouquet with \((1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)\). The maximum over all valid bouquets not greater than \(10\) is \(7\) for \((2, 2, 3)\). In the third test case, you can assemble a bouquet with only one flower of any type, so the answer is \(610\). In the fourth test c...
Input: 55 101 1 2 2 38 204 2 7 5 6 1 1 18 100000239 30 610 122 24 40 8 211 132 4 11 1 1 2 3 5 4 3 28 1033206 206 206 207 207 207 207 1000 | Output: 7 13 610 13 1033
Easy
5
746
665
172
19
1,753
D
1753D
D. The Beach
2,400
constructive algorithms; dfs and similar; graphs; shortest paths
Andrew loves the sea. That's why, at the height of the summer season, he decided to go to the beach, taking a sunbed with him to sunbathe.The beach is a rectangular field with \(n\) rows and \(m\) columns. Some cells of the beach are free, some have roads, stones, shops and other non-movable objects. Some of two adjace...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 300\,000\), \(1 \le n \cdot m \le 300\,000\)) — the number of rows and columns in rectangle.The second line contains two integers \(p\) and \(q\) (\(1 \le p, q \le 10^9\)) — the number of units of discomfort caused by rotation and shift of a sunbed,...
Print one integer — the minimum possible number of units of discomfort, caused to other tourists, to free a space for a sunbed. If it is impossible to free a space for a sunbed, print \(-1\).
In the first example we can shift upper sunbed to the left and lower sunbed — to the right. Andrew will be able to put his sunbed vertically in the middle of the beach. We well cause \(2 + 2 = 4\) units of discomfort. It is easy to prove that it is an optimal answer. Optimal strategy in the first example (Andrew's sunb...
Input: 2 55 2.LR####LR. | Output: 4
Expert
4
1,744
778
191
17
306
B
306B
B. Optimizer
2,100
data structures; greedy; sortings
A process RAM is a sequence of bytes that are indexed from 1 to n. Polycarpus's program contains such instructions as ""memset"", that is, the operations of filling memory cells on a segment with some value. The details are: the code only contains m instructions that look like ""set13 a_i l_i"". Instruction i fills a c...
The first line contains integers n and m (1 ≤ n ≤ 2·106, 1 ≤ m ≤ 2·105) — the number of bytes (memory cells) and the number of instructions in Polycarpus's code. Then m lines follow, each line contains a pair of integers ai, li (1 ≤ ai ≤ n, 1 ≤ li ≤ n - ai + 1).
Print in the first line the sought maximum number of instructions that can be removed from the code. In the second line print the numbers of the instructions. The instructions are numbered from 1 to m in the order they appeared in the input. If there are multiple solutions, print any of them.
Input: 10 43 33 14 19 2 | Output: 22 3
Hard
3
883
262
293
3
1,107
A
1107A
A. Digits Sequence Dividing
900
greedy; strings
You are given a sequence \(s\) consisting of \(n\) digits from \(1\) to \(9\).You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segme...
The first line of the input contains one integer \(q\) (\(1 \le q \le 300\)) — the number of queries.The first line of the \(i\)-th query contains one integer number \(n_i\) (\(2 \le n_i \le 300\)) — the number of digits in the \(i\)-th query.The second line of the \(i\)-th query contains one string \(s_i\) of length \...
If the sequence of digits in the \(i\)-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line ""NO"" for this query.Otherwise in the first line of the answer to this query print ""YES"", on the second line print \(k_i\) — the number of parts in your divisio...
Input: 4 6 654321 4 1337 2 33 4 2122 | Output: YES 3 6 54 321 YES 3 1 3 37 NO YES 2 21 22
Beginner
2
1,216
373
676
11
1,560
F1
1560F1
F1. Nearest Beautiful Number (easy version)
1,900
binary search; bitmasks; brute force; constructive algorithms; dfs and similar; greedy
It is a simplified version of problem F2. The difference between them is the constraints (F1: \(k \le 2\), F2: \(k \le 10\)).You are given an integer \(n\). Find the minimum integer \(x\) such that \(x \ge n\) and the number \(x\) is \(k\)-beautiful.A number is called \(k\)-beautiful if its decimal representation havin...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then \(t\) test cases follow.Each test case consists of one line containing two integers \(n\) and \(k\) (\(1 \le n \le 10^9\), \(1 \le k \le 2\)).
For each test case output on a separate line \(x\) — the minimum \(k\)-beautiful integer such that \(x \ge n\).
Input: 4 1 1 221 2 177890 2 998244353 1 | Output: 1 221 181111 999999999
Hard
6
546
240
111
15
1,843
B
1843B
B. Long Long
800
greedy; math; two pointers
Today Alex was brought array \(a_1, a_2, \dots, a_n\) of length \(n\). He can apply as many operations as he wants (including zero operations) to change the array elements.In \(1\) operation Alex can choose any \(l\) and \(r\) such that \(1 \leq l \leq r \leq n\), and multiply all elements of the array from \(l\) to \(...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — length of the array.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n...
For each test case output two space-separated numbers: the maximum possible sum of numbers in the array and the minimum number of operations to get this sum.Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type.
Below, for each test case, only one of the possible shortest sequences of operations is provided among many. There are others that have the same length and lead to the maximum sum of elements.In the first test case, Alex can make operations: \((1, 4)\), \((2, 2)\), \((6, 6)\).In the second test case, to get the largest...
Input: 56-1 7 -4 -2 5 -88-1 0 0 -2 1 0 -3 052 -1 0 -3 -750 -17 0 1 04-1 0 -2 -1 | Output: 27 3 7 2 13 1 18 1 4 1
Beginner
3
868
469
270
18
1,663
A
1663A
A. Who Tested?
0
*special; expression parsing; trees
Beginner
3
0
0
0
16
1,744
B
1744B
B. Even-Odd Increments
800
implementation; math
You are given \(n\) of integers \(a_1, a_2, \ldots, a_n\). Process \(q\) queries of two types: query of the form ""0 \(x_j\)"": add the value \(x_j\) to all even elements of the array \(a\), query of the form ""1 \(x_j\)"": add the value \(x_j\) to all odd elements of the array \(a\).Note that when processing the query...
The first line of the input contains an integer \(t\) \((1 \leq t \leq 10^4\)) — the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n\), \(q \leq 10^5\)) — the length of array \(a\) and the number of queries.The second line...
For each test case, print \(q\) numbers: the sum of the elements of the array \(a\) after processing a query.
In the first test case, the array \(a = [2]\) after the first query.In the third test case, the array \(a\) is modified as follows: \([1, 3, 2, 4, 10, 48]\) \(\rightarrow\) \([7, 9, 2, 4, 10, 48]\) \(\rightarrow\) \([7, 9, 7, 9, 15, 53]\) \(\rightarrow\) \([7, 9, 7, 9, 15, 53]\) \(\rightarrow\) \([10, 12, 10, 12, 18, 5...
Input: 41 111 13 31 2 40 21 30 56 71 3 2 4 10 481 60 50 40 51 30 120 16 71000000000 1000000000 1000000000 11 15 170 171 100001 510 920 531 160 1 | Output: 2 11 14 29 80 100 100 100 118 190 196 3000000094 3000060094 3000060400 3000060952 3000061270 3000061366 3000061366
Beginner
2
654
771
109
17
1,938
J
1938J
2,100
Hard
0
0
0
0
19
877
E
877E
E. Danil and a Part-time Job
2,000
bitmasks; data structures; trees
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room...
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i.The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if ...
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
The tree before the task pow 1. The tree after the task pow 1.
Input: 41 1 11 0 0 19get 1get 2get 3get 4pow 1get 1get 2get 3get 4 | Output: 20012110
Hard
3
1,262
531
99
8
251
B
251B
B. Playing with Permutations
1,800
implementation; math
Little Petya likes permutations a lot. Recently his mom has presented him permutation q1, q2, ..., qn of length n.A permutation a of length n is a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n), all integers there are distinct. There is only one thing Petya likes more than permutations: playing with little Masha. As...
The first line contains two integers n and k (1 ≤ n, k ≤ 100). The second line contains n space-separated integers q1, q2, ..., qn (1 ≤ qi ≤ n) — the permutation that Petya's got as a present. The third line contains Masha's permutation s, in the similar format.It is guaranteed that the given sequences q and s are corr...
If the situation that is described in the statement is possible, print ""YES"" (without the quotes), otherwise print ""NO"" (without the quotes).
In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before k moves were performed.In the second sample the described situation is possible, in...
Input: 4 12 3 4 11 2 3 4 | Output: NO
Medium
2
1,785
337
145
2
1,144
G
1144G
G. Two Merged Sequences
2,400
dp; greedy
Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing.Strictly increasing sequence is a sequence of integers \([x_1 < x_2 < \dots < x_k]\). And strictly decreasing sequence is a sequence of integers \([y_1 > y_2 > \dots > y_l]\). Note that the empty sequence...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).
If there is a contradiction in the input and it is impossible to split the given sequence \(a\) into one increasing sequence and one decreasing sequence, print ""NO"" in the first line.Otherwise print ""YES"" in the first line. In the second line, print a sequence of \(n\) integers \(res_1, res_2, \dots, res_n\), where...
Input: 9 5 1 3 6 8 2 9 0 10 | Output: YES 1 0 0 0 0 1 0 1 0
Expert
2
1,487
279
661
11
1,566
G
1566G
G. Four Vertices
3,100
constructive algorithms; data structures; graphs; greedy; implementation; shortest paths
You are given an undirected weighted graph, consisting of \(n\) vertices and \(m\) edges.Some queries happen with this graph: Delete an existing edge from the graph. Add a non-existing edge to the graph. At the beginning and after each query, you should find four different vertices \(a\), \(b\), \(c\), \(d\) such that ...
The first line contains two integers \(n\) and \(m\) \((4 \le n, m \le 10^5)\) — the number of vertices and edges in the graph respectively.Each of the next \(m\) lines contain three integers \(v\), \(u\), \(w\) (\(1 \le v, u \le n, v \neq u\), \(1 \le w \le 10^9\)) — this triple means that there is an edge between ver...
Print \(q + 1\) integers — the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them.
Before the queries you can choose vertices \((a, b) = (3, 2)\) and \((c, d) = (1, 4)\). The sum of lengths of two shortest paths is \(3 + 1 = 4\).After the first query you can choose vertices \((a, b) = (2, 5)\) and \((c, d) = (1, 4)\). The sum of lengths of two shortest paths is \(2 + 1 = 3\).After the second query yo...
Input: 6 6 1 3 6 4 3 1 1 4 1 2 6 4 2 4 2 5 4 3 4 1 2 5 2 0 1 4 0 3 4 1 6 1 3 | Output: 4 3 3 7 5
Master
6
666
1,345
147
15
70
A
70A
A. Cookies
1,300
math
Fangy collects cookies. Once he decided to take a box and put cookies into it in some way. If we take a square k × k in size, divided into blocks 1 × 1 in size and paint there the main diagonal together with cells, which lie above it, then the painted area will be equal to the area occupied by one cookie k in size. Fan...
The first line contains a single integer n (0 ≤ n ≤ 1000).
Print the single number, equal to the number of empty cells in the box. The answer should be printed modulo 106 + 3.
If the box possesses the base of 23 × 23 (as in the example), then the cookies will be put there in the following manner:
Input: 3 | Output: 9
Easy
1
995
58
116
0
1,910
A
1910A
A. Username
1,100
*special; implementation
On the official website of Berland, each account has two parameters: username and ID.Username is a string consisting of lowercase Latin letters and/or digits, which contains at least one letter. For example, user0, 1fox1, zzzz are correct usernames, but 1337 and User22 are not. The ID of the account for a given usernam...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) — the number of test cases.The only line of each test case contains a string \(s\) (\(2 \le |s| \le 50\)), consisting of lowercase Latin letters and/or digits.Additional constraint on input: there is at least one valid username that can generate the ...
For each test case on a separate line, print a string — any valid username that can generate the given ID. If there are multiple correct usernames, you can print any of them.
Input: 4user0125a1kotlin9900001code0forces101 | Output: user0 a kotlin9 1code0forces
Easy
2
740
329
174
19
1,778
D
1778D
D. Flexible String Revisit
2,100
combinatorics; dp; math; probabilities
You are given two binary strings \(a\) and \(b\) of length \(n\). In each move, the string \(a\) is modified in the following way. An index \(i\) (\(1 \leq i \leq n\)) is chosen uniformly at random. The character \(a_i\) will be flipped. That is, if \(a_i\) is \(0\), it becomes \(1\), and if \(a_i\) is \(1\), it become...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) — 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 \leq n \leq 10^6\)) — the length of the strings.The second line of each test case contains the binary string...
For each test case, output a single line containing the expected number of moves modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the inte...
In the first test case, index \(1\) is chosen randomly and \(a_1\) is flipped. After the move, the strings \(a\) and \(b\) are equal. The expected number of moves is \(1\).The strings \(a\) and \(b\) are already equal in the second test case. So, the expected number of moves is \(0\).The expected number of moves for th...
Input: 41012000041000111050100110111 | Output: 1 0 665496254 665496277
Hard
4
508
509
463
17
1,812
D
1812D
D. Trivial Conjecture
0
*special; constructive algorithms; math; number theory
$$$\(f(n) = \left\{ \begin{array}{ll} \frac{n}{2} & n \equiv 0 \pmod{2}\\ 3n+1 & n \equiv 1 \pmod{2}\\ \end{array} \right.\)\(Find an integer \)n\( so that none of the first \)k\( terms of the sequence \)n, f(n), f(f(n)), f(f(f(n))), \dots\( are equal to \)1$$$.
The only line contains an integer \(k\) (\(1 \leq k \leq \min(\textbf{[REDACTED]}, 10^{18})\)).
Output a single integer \(n\) such that none of the first \(k\) terms of the sequence \(n, f(n), f(f(n)), f(f(f(n))), \dots\) are equal to \(1\).Integer \(n\) should have at most \(10^3\) digits.
In the first test, the sequence created with \(n = 5\) looks like \(5, 16, 8, 4, 2, 1, 4, \dots\), and none of the first \(k=1\) terms are equal to \(1\).In the second test, the sequence created with \(n = 6\) looks like \(6, 3, 10, 5, 16, 8, 4, \dots\), and none of the first \(k=5\) terms are equal to \(1\).
Input: 1 | Output: 5
Beginner
4
262
95
195
18
897
B
897B
B. Chtholly's request
1,300
brute force
— Thanks a lot for today.— I experienced so many great things.— You gave me memories like dreams... But I have to leave now...— One last request, can you...— Help me solve a Codeforces problem?— ......— What?Chtholly has been thinking about a problem for days:If a number is palindrome and length of its decimal represen...
The first line contains two integers k and p (1 ≤ k ≤ 105, 1 ≤ p ≤ 109).
Output single integer — answer to the problem.
In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22.In the second example, .
Input: 2 100 | Output: 33
Easy
1
832
72
46
8
873
B
873B
B. Balanced Substring
1,500
dp; implementation
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.You have to determine the length of the longest balanced...
The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.
If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.
In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.In the second example it's impossible to find a non-empty balanced substring.
Input: 811010111 | Output: 4
Medium
2
336
187
121
8
1,767
C
1767C
C. Count Binary Strings
2,100
data structures; dp
You are given an integer \(n\). You have to calculate the number of binary (consisting of characters 0 and/or 1) strings \(s\) meeting the following constraints.For every pair of integers \((i, j)\) such that \(1 \le i \le j \le n\), an integer \(a_{i,j}\) is given. It imposes the following constraint on the string \(s...
The first line contains one integer \(n\) (\(2 \le n \le 100\)).Then \(n\) lines follow. The \(i\)-th of them contains \(n-i+1\) integers \(a_{i,i}, a_{i,i+1}, a_{i,i+2}, \dots, a_{i,n}\) (\(0 \le a_{i,j} \le 2\)).
Print one integer — the number of strings meeting the constraints, taken modulo \(998244353\).
In the first example, the strings meeting the constraints are 001, 010, 011, 100, 101, 110.In the second example, the strings meeting the constraints are 001, 110.
Input: 3 1 0 2 1 0 1 | Output: 6
Hard
2
815
214
94
17
1,286
E
1286E
E. Fedya the Potter Strikes Back
3,200
data structures; strings
Fedya has a string \(S\), initially empty, and an array \(W\), also initially empty.There are \(n\) queries to process, one at a time. Query \(i\) consists of a lowercase English letter \(c_i\) and a nonnegative integer \(w_i\). First, \(c_i\) must be appended to \(S\), and \(w_i\) must be appended to \(W\). The answer...
The first line contains an integer \(n\) \((1 \leq n \leq 600\,000)\) — the number of queries.The \(i\)-th of the following \(n\) lines contains the query \(i\): a lowercase letter of the Latin alphabet \(c_i\) and an integer \(w_i\) \((0 \leq w_i \leq 2^{30} - 1)\).All queries are given in an encrypted form. Let \(ans...
Print \(n\) lines, \(i\)-th line should contain a single integer — the answer to the \(i\)-th query.
For convenience, we will call ""suspicious"" those subsegments for which the corresponding lines are prefixes of \(S\), that is, those whose suspiciousness may not be zero.As a result of decryption in the first example, after all requests, the string \(S\) is equal to ""abacaba"", and all \(w_i = 1\), that is, the susp...
Input: 7 a 1 a 0 y 3 y 5 v 4 u 6 r 8 | Output: 1 2 4 5 7 9 12
Master
2
1,029
719
100
12
1,292
D
1292D
D. Chaotic V.
2,700
dp; graphs; greedy; math; number theory; trees
Æsir - CHAOS Æsir - V.""Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time.""The emotion samples are now sufficient. After almost 3 years, it's time for Ivy to awake her bonded sister, Vanessa.The system inside A.R.C.'s Libra...
The first line contains an integer \(n\) (\(1 \le n \le 10^6\)) — number of fragments of Vanessa's mind.The second line contains \(n\) integers: \(k_1, k_2, \ldots, k_n\) (\(0 \le k_i \le 5000\)), denoting the nodes where fragments of Vanessa's mind are located: the \(i\)-th fragment is at the node with a number \(k_i!...
Print a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node \(P\)).As a reminder, if there are multiple fragments at the same node, the distance from that node to \(P\) needs to be counted multiple times as well.
Considering the first \(24\) nodes of the system, the node network will look as follows (the nodes \(1!\), \(2!\), \(3!\), \(4!\) are drawn bold):For the first example, Ivy will place the emotion samples at the node \(1\). From here: The distance from Vanessa's first fragment to the node \(1\) is \(1\). The distance fr...
Input: 3 2 1 4 | Output: 5
Master
6
1,485
323
279
12
343
A
343A
A. Rational Resistance
1,600
math; number theory
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will cons...
The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 1018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists.
Print a single number — the answer to the problem.Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
In the first sample, one resistor is enough.In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance . We cannot make this element using two resistors.
Input: 1 1 | Output: 1
Medium
2
852
185
198
3
1,701
E
1701E
E. Text Editor
2,500
brute force; dp; greedy; strings
You wanted to write a text \(t\) consisting of \(m\) lowercase Latin letters. But instead, you have written a text \(s\) consisting of \(n\) lowercase Latin letters, and now you want to fix it by obtaining the text \(t\) from the text \(s\).Initially, the cursor of your text editor is at the end of the text \(s\) (afte...
The first line of the input contains one integer \(T\) (\(1 \le T \le 5000\)) — the number of test cases. Then \(T\) test cases follow.The first line of the test case contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 5000\)) — the length of \(s\) and the length of \(t\), respectively.The second line of the tes...
For each test case, print one integer — the minimum number of moves required to obtain the text \(t\) from the text \(s\) using the given set of actions, or -1 if it is impossible to obtain the text \(t\) from the text \(s\) in the given test case.
Input: 69 4aaaaaaaaaaaaa7 3abacabaaaa5 4aabcdabcd4 2abbabb6 4barakabaka8 7questionproblem | Output: 5 6 3 4 4 -1
Expert
4
1,385
605
248
17
1,719
B
1719B
B. Mathematical Circus
800
constructive algorithms; math
A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — \(n\) and \(k\), where \(n\) is even. Next, he takes all the integers from \(1\) to \(n\), and splits them all into pairs \((a, b)\) (each integer must be in exactly one pair) so that for each pair the...
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. The following is a description of the input data sets.The single line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq k \leq 10^9\), \(n\) is even) — the number of integers an...
For each test case, first output the string ""YES"" if there is a split into pairs, and ""NO"" if there is none.If there is a split, then in the following \(\frac{n}{2}\) lines output pairs of the split, in each line print \(2\) numbers — first the integer \(a\), then the integer \(b\).
In the first test case, splitting into pairs \((1, 2)\) and \((3, 4)\) is suitable, same as splitting into \((1, 4)\) and \((3, 2)\).In the second test case, \((1 + 0) \cdot 2 = 1 \cdot (2 + 0) = 2\) is not divisible by \(4\), so there is no partition.
Input: 44 12 012 1014 11 | Output: YES 1 2 3 4 NO YES 3 4 7 8 11 12 2 1 6 5 10 9 YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Beginner
2
860
443
287
17
1,791
D
1791D
D. Distinct Split
1,000
brute force; greedy; strings
Let's denote the \(f(x)\) function for a string \(x\) as the number of distinct characters that the string contains. For example \(f(\texttt{abc}) = 3\), \(f(\texttt{bbbbb}) = 1\), and \(f(\texttt{babacaba}) = 3\).Given a string \(s\), split it into two non-empty strings \(a\) and \(b\) such that \(f(a) + f(b)\) is the...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2\cdot10^5\)) — the length of the string \(s\).The second line co...
For each test case, output a single integer — the maximum possible value of \(f(a) + f(b)\) such that \(a + b = s\).
For the first test case, there is only one valid way to split \(\texttt{aa}\) into two non-empty strings \(\texttt{a}\) and \(\texttt{a}\), and \(f(\texttt{a}) + f(\texttt{a}) = 1 + 1 = 2\).For the second test case, by splitting \(\texttt{abcabcd}\) into \(\texttt{abc}\) and \(\texttt{abcd}\) we can get the answer of \...
Input: 52aa7abcabcd5aaaaa10paiumoment4aazz | Output: 2 7 2 10 3
Beginner
3
508
475
116
17
990
C
990C
C. Bracket Sequences Concatenation Problem
1,500
implementation
A bracket sequence is a string containing only characters ""("" and "")"".A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, bracket sequences ""()()"", ""(())...
The first line contains one integer \(n \, (1 \le n \le 3 \cdot 10^5)\) — the number of bracket sequences. The following \(n\) lines contain bracket sequences — non-empty strings consisting only of characters ""("" and "")"". The sum of lengths of all bracket sequences does not exceed \(3 \cdot 10^5\).
In the single line print a single integer — the number of pairs \(i, j \, (1 \le i, j \le n)\) such that the bracket sequence \(s_i + s_j\) is a regular bracket sequence.
In the first example, suitable pairs are \((3, 1)\) and \((2, 2)\).In the second example, any pair is suitable, namely \((1, 1), (1, 2), (2, 1), (2, 2)\).
Input: 3)()( | Output: 2
Medium
1
958
303
170
9
2,046
D
2046D
D. For the Emperor!
3,100
flows; graphs
In Ancient Rome, a plan to defeat the barbarians was developed, but for its implementation, each city must be informed about it.The northern part of the Roman Empire consists of \(n\) cities connected by \(m\) one-way roads. Initially, the \(i\)-th city has \(a_i\) messengers, and each messenger can freely move between...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 200\), \(1 \le m \le 800\)) — the number of cities and roads.The second line contains \(n\)...
Output a single line containing a single integer — the smallest number of messengers you need to give a copy of the plan in the beginning, or \(-1\) if it is not possible to inform all cities.
Input: 27 62 1 0 1 2 3 41 21 32 42 53 63 74 41 1 1 11 21 32 43 4 | Output: 2 2
Master
2
880
804
192
20
1,902
E
1902E
E. Collapsing Strings
1,900
data structures; strings; trees
You are given \(n\) strings \(s_1, s_2, \dots, s_n\), consisting of lowercase Latin letters. Let \(|x|\) be the length of string \(x\).Let a collapse \(C(a, b)\) of two strings \(a\) and \(b\) be the following operation: if \(a\) is empty, \(C(a, b) = b\); if \(b\) is empty, \(C(a, b) = a\); if the last letter of \(a\)...
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)).Each of the next \(n\) lines contains a string \(s_i\) (\(1 \le |s_i| \le 10^6\)), consisting of lowercase Latin letters.The total length of the strings doesn't exceed \(10^6\).
Print a single integer — \(\sum\limits_{i=1}^n \sum\limits_{j=1}^n |C(s_i, s_j)|\).
Input: 3abaabba | Output: 20
Hard
3
636
247
83
19
1,407
E
1407E
E. Egor in the Republic of Dagestan
2,500
constructive algorithms; dfs and similar; dp; graphs; greedy; shortest paths
Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan.There are \(n\) cities in the republic, some of them are connected by \(m\) directed roads without any additional conditions. In other words, road system of Dagestan represents an arbit...
The first line contains two integers \(n\), \(m\) (\(1 \leq n \leq 500000\), \(0 \leq m \leq 500000\)) — the number of cities and the number of roads.The \(i\)-th of next \(m\) lines contains three integers — \(u_i\), \(v_i\) and \(t_i\) (\(1 \leq u_i, v_i \leq n\), \(t_i \in \{0, 1\}\)) — numbers of cities connected b...
In the first line output the length of the desired path (or \(-1\), if it's possible to choose such schedule that there's no path from \(1\) to \(n\)).In the second line output the desired schedule — a string of \(n\) digits, where \(i\)-th digit is \(0\), if the \(i\)-th city is a night one, and \(1\) if it's a mornin...
For the first sample, if we paint city \(1\) white, the shortest path is \(1 \rightarrow 3\). Otherwise, it's \(1 \rightarrow 2 \rightarrow 3\) regardless of other cities' colors.For the second sample, we should paint city \(3\) black, and there are both black and white roads going from \(2\) to \(4\). Note that there ...
Input: 3 4 1 2 0 1 3 1 2 3 0 2 3 1 | Output: 2 011
Expert
6
1,688
397
367
14
555
A
555A
A. Case of Matryoshkas
1,500
implementation
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be ne...
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matry...
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Input: 3 22 1 21 3 | Output: 1
Medium
1
1,310
679
121
5
10
A
10A
A. Power Consumption Calculation
900
implementation
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minut...
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start a...
Output the answer to the problem.
Input: 1 3 2 1 5 100 10 | Output: 30
Beginner
1
896
358
33
0
822
B
822B
B. Crossword solving
1,000
brute force; implementation; strings
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple ta...
The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly.The second line contains n lowercase English letters — string s.The third line contains m lowercase English letters — string t.
In the first line print single integer k — the minimal number of symbols that need to be replaced.In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of...
Input: 3 5abcxaybz | Output: 22 3
Beginner
3
1,122
267
351
8
261
B
261B
B. Maxim and Restaurant
1,900
dp; math; probabilities
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters.Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai...
The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters. The numbers in the lines are separated by single spaces.
In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4.
In the first sample the people will come in the following orders: (1, 2, 3) — there will be two people in the restaurant; (1, 3, 2) — there will be one person in the restaurant; (2, 1, 3) — there will be two people in the restaurant; (2, 3, 1) — there will be one person in the restaurant; (3, 1, 2) — there will be one ...
Input: 31 2 33 | Output: 1.3333333333
Hard
3
1,240
317
161
2
1,472
B
1472B
B. Fair Division
800
dp; greedy; math
Alice and Bob received \(n\) candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) — the number of candies that Alice and Bob received.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) —...
For each test case, output on a separate line: ""YES"", if all candies can be divided into two sets with the same weight; ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
In the first test case, Alice and Bob can each take one candy, then both will have a total weight of \(1\).In the second test case, any division will be unfair.In the third test case, both Alice and Bob can take two candies, one of weight \(1\) and one of weight \(2\).In the fourth test case, it is impossible to divide...
Input: 5 2 1 1 2 1 2 4 1 2 1 2 3 2 2 2 3 2 1 2 | Output: YES NO YES NO NO
Beginner
3
329
483
266
14
1,783
A
1783A
A. Make it Beautiful
800
constructive algorithms; math; sortings
An array \(a\) is called ugly if it contains at least one element which is equal to the sum of all elements before it. If the array is not ugly, it is beautiful.For example: the array \([6, 3, 9, 6]\) is ugly: the element \(9\) is equal to \(6 + 3\); the array \([5, 5, 7]\) is ugly: the element \(5\) (the second one) i...
The first line contains one integer \(t\) (\(1 \le t \le 2000\)) — the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(2 \le n \le 50\)). The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_1 \le a_2 \le \dots \le a_n \le 100\)).
For each test case, print the answer as follows: if it is impossible to reorder the elements of \(a\) in such a way that it becomes beautiful, print NO; otherwise, in the first line, print YES. In the second line, print \(n\) integers — any beautiful array which can be obtained from \(a\) by reordering its elements. If...
Input: 443 3 6 6210 1051 2 3 4 531 4 4 | Output: YES 3 6 3 6 NO YES 2 4 1 5 3 YES 1 4 4
Beginner
3
906
309
371
17
1,555
A
1555A
A. PizzaForces
900
brute force; math
PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of \(6\) slices, medium ones consist of \(8\) slices, and large pizzas consist of \(10\) slices each. Baking them takes \(15\), \(20\) and \(25\) minutes, respectively.Petya's birthday is today, and \(n\) o...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.Each testcase consists of a single line that contains a single integer \(n\) (\(1 \le n \le 10^{16}\)) — the number of Petya's friends.
For each testcase, print one integer — the minimum number of minutes that is needed to bake pizzas containing at least \(n\) slices in total.
Input: 6 12 15 300 1 9999999999999999 3 | Output: 30 40 750 15 25000000000000000 15
Beginner
2
1,656
231
141
15
2,019
B
2019B
B. All Pairs Segments
1,200
implementation; math
Shirobon - FOX⠀You are given \(n\) points on the \(x\) axis, at increasing positive integer coordinates \(x_1 < x_2 < \ldots < x_n\).For each pair \((i, j)\) with \(1 \leq i < j \leq n\), you draw the segment \([x_i, x_j]\). The segments are closed, i.e., a segment \([a, b]\) contains the points \(a, a+1, \ldots, b\).Y...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\), \(q\) (\(2 \le n \le 10^5\), \(1 \le q \le 10^5\)) — the number of points and the number of queri...
For each test case, output a single line with \(q\) integers: the \(i\)-th integer is the answer to the \(i\)-th query.
In the first example, you only draw the segment \([101, 200]\). No point is contained in exactly \(2\) segments, and the \(100\) points \(101, 102, \ldots, 200\) are contained in exactly \(1\) segment.In the second example, you draw \(15\) segments: \([1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [2, 3], [2, 5], [2, 6], [2, ...
Input: 32 2101 2002 16 151 2 3 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 155 8254618033 265675151 461318786 557391198 8480837786 9 15 10 6 9 4 4294967300 | Output: 0 100 0 0 0 0 2 0 0 0 3 0 2 0 0 0 0 291716045 0 0 0 291716045 0 301749698 0
Easy
2
522
795
119
20
1,037
B
1037B
B. Reach Median
1,300
greedy
You are given an array \(a\) of \(n\) integers and an integer \(s\). It is guaranteed that \(n\) is odd.In one operation you can either increase or decrease any single element by one. Calculate the minimum number of operations required to make the median of the array being equal to \(s\).The median of the array with od...
The first line contains two integers \(n\) and \(s\) (\(1\le n\le 2\cdot 10^5-1\), \(1\le s\le 10^9\)) — the length of the array and the required value of median.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\le a_i \le 10^9\)) — the elements of the array \(a\).It is guaranteed that \(n\) is odd...
In a single line output the minimum number of operations to make the median being equal to \(s\).
In the first sample, \(6\) can be increased twice. The array will transform to \(8, 5, 8\), which becomes \(5, 8, 8\) after sorting, hence the median is equal to \(8\).In the second sample, \(19\) can be increased once and \(15\) can be increased five times. The array will become equal to \(21, 20, 12, 11, 20, 20, 12\)...
Input: 3 86 5 8 | Output: 2
Easy
1
589
321
97
10
1,823
E
1823E
E. Removing Graph
2,500
brute force; dp; games; graphs; math
Alice and Bob are playing a game on a graph. They have an undirected graph without self-loops and multiple edges. All vertices of the graph have degree equal to \(2\). The graph may consist of several components. Note that if such graph has \(n\) vertices, it will have exactly \(n\) edges.Alice and Bob take turn. Alice...
The first line contains three integers \(n\), \(l\) and \(r\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le l < r \le n\)) — the number of vertices in the graph, and the constraints on the number of vertices Alice or Bob can choose in one move.Next \(n\) lines contains edges of the graph: one edge per line. The \(i\)-th line ...
Print Alice (case-insensitive) if Alice wins, or Bob otherwise.
In the first test the same input as in legend is shown.In the second test the same graph as in legend is shown, but with \(l = 1\) and \(r = 2\).
Input: 6 2 3 1 2 2 3 3 1 4 5 5 6 6 4 | Output: Bob
Expert
5
1,225
526
63
18
1,861
B
1861B
B. Two Binary Strings
1,000
constructive algorithms; dp; greedy
You are given two strings \(a\) and \(b\) of equal length, consisting of only characters 0 and/or 1; both strings start with character 0 and end with character 1. You can perform the following operation any number of times (possibly zero): choose one of the strings and two equal characters in it; then turn all characte...
The first line contains a single integer \(t\) (\(1 \le t \le 2000\)) — the number of test cases.Each test case consists of two lines: the first line contains the string \(a\) (\(2 \le |a| \le 5000\)), consisting of only characters 0 and/or 1. the second line contains the string \(b\) (\(2 \le |b| \le 5000\)), consisti...
For each test case, print YES if it is possible to make the strings equal. Otherwise, print NO. You can print each letter in any register.
In the first test case, we can perform the following operations: choose the string \(a\), \(l = 2\), \(r = 4\); after this operation, \(a\) is 01110001, \(b\) is 01110101; choose the string \(b\), \(l = 5\), \(r = 7\); after this operation, \(a\) is 01110001, \(b\) is 01110001. In the second test case, the strings are ...
Input: 7010100010111010101001010010001010101110000101111011001001001011011010001011011 | Output: YES YES YES NO NO NO YES
Beginner
3
1,126
599
138
18
1,893
A
1893A
A. Anonymous Informant
1,400
brute force; dfs and similar; graphs; implementation
You are given an array \(b_1, b_2, \ldots, b_n\).An anonymous informant has told you that the array \(b\) was obtained as follows: initially, there existed an array \(a_1, a_2, \ldots, a_n\), after which the following two-component operation was performed \(k\) times: A fixed point\(^{\dagger}\) \(x\) of the array \(a\...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n, k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)) — the length of the array \(b...
For each test case, output ""Yes"" if the words of the anonymous informant can be true, and ""No"" if they are guaranteed to be false.
In the first test case, the array \(a\) could be equal to \([3, 2, 3, 4, 3]\). In the first operation, a fixed point \(x = 2\) was chosen, and after \(2\) left shifts, the array became \([3, 4, 3, 3, 2]\). In the second operation, a fixed point \(x = 3\) was chosen, and after \(3\) left shifts, the array became \([3, 2...
Input: 65 34 3 3 2 33 1007 2 15 56 1 1 1 11 100000000018 489 10 11 12 13 14 15 82 11 42 | Output: Yes Yes No Yes Yes No
Easy
4
866
610
134
18
2,055
F
2055F
F. Cosmic Divide
3,200
brute force; geometry; hashing; math; strings
With the artifact in hand, the fabric of reality gives way to its true master — Florida Man.A polyomino is a connected\(^{\text{∗}}\) figure constructed by joining one or more equal \(1 \times 1\) unit squares edge to edge. A polyomino is convex if, for any two squares in the polyomino that share the same row or the sa...
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 2\cdot 10^5\)) — the number of rows of the polyomino.The \(i\)-th of the next \(...
For each test case, print a single line containing either ""YES"" or ""NO"", representing whether or not the polyomino can be partitioned as described in the problem.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 res...
The first and second test cases are the polyominoes depicted in the problem statement and can be partitioned as shown.The polyomino in the third test case, shown below, can be shown to be impossible to partition. None of the following partitions are valid: The partition on the left does not use polyominoes that are tra...
Input: 721 22 344 42 41 41 231 21 22 321 33 321 32 231 21 31 348 96 86 85 6 | Output: YES YES NO NO NO NO YES
Master
5
1,742
688
327
20
2,117
H
2117H
H. Incessant Rain
2,500
data structures; divide and conquer; sortings
Note the unusual memory limit.Silver Wolf gives you an array \(a\) of length \(n\) and \(q\) queries. In each query, she replaces an element in \(a\). After each query, she asks you to output the maximum integer \(k\) such that there exists an integer \(x\) such that it is the \(k\)-majority of a subarray\(^{\text{∗}}\...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 3 \cdot 10^5\)) — the length of \(a\) and the number of queries.The following line contains \(n\) space-separated integers \(a_1, a_2, \...
For each test case, output the answer to all queries on a single new line, separated by a space.
Input: 25 51 2 3 4 53 41 42 44 32 37 83 2 3 3 2 2 32 35 36 33 44 47 46 42 4 | Output: 1 1 2 1 0 2 2 3 2 1 1 1 2
Expert
3
805
612
96
21
290
F
290F
F. Greedy Petya
2,800
*special; dfs and similar; graphs; greedy
Petya is an unexperienced programming contestant. Recently he has come across the following problem:You are given a non-directed graph which consists of n nodes and m edges. Your task is to determine whether the graph contains a Hamiltonian path.Petya wrote a quick bug-free code which he believes solves this problem. A...
The first line contains two integers n, m (1 ≤ n ≤ 20; 0 ≤ m ≤ 400). Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n).
Follow the format of Petya's code output.
Input: 2 31 22 11 1 | Output: Yes
Master
4
584
132
41
2
659
E
659E
E. New Reform
1,600
data structures; dfs and similar; dsu; graphs; greedy
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.The President of Berland decided to make changes to the road syst...
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000). Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi...
Print a single integer — the minimum number of separated cities after the reform.
In the first sample the following road orientation is allowed: , , .The second sample: , , , , .The third sample: , , , , .
Input: 4 32 11 34 3 | Output: 1
Medium
5
825
551
81
6