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
718
E
718E
E. Matvey's Birthday
3,300
bitmasks; graphs
Today is Matvey's birthday. He never knows what to ask as a present so friends gave him a string s of length n. This string consists of only first eight English letters: 'a', 'b', ..., 'h'.First question that comes to mind is: who might ever need some string? Matvey is a special boy so he instantly found what to do wit...
The first line of the input contains a single integer n (2 ≀ n ≀ 100 000) β€” the length of the string.The second line contains the string s itself. It's guaranteed that s consists of only first eight letters of English alphabet.
Print two integers β€” the diameter of the graph and the number of pairs of positions with the distance equal to the diameter.
Consider the second sample. The maximum distance is 2. It's obtained for pairs (1, 4), (2, 4), (4, 6) and (4, 7).
Input: 3abc | Output: 2 1
Master
2
1,041
227
124
7
424
D
424D
D. Biathlon Track
2,300
binary search; brute force; constructive algorithms; data structures; dp
Recently an official statement of the world Olympic Committee said that the Olympic Winter Games 2030 will be held in Tomsk. The city officials decided to prepare for the Olympics thoroughly and to build all the necessary Olympic facilities as early as possible. First, a biathlon track will be built.To construct a biat...
The first line of the input contains three integers n, m and t (3 ≀ n, m ≀ 300, 1 ≀ t ≀ 109) β€” the sizes of the land plot and the desired distance covering time.The second line also contains three integers tp, tu and td (1 ≀ tp, tu, td ≀ 100) β€” the time the average biathlete needs to cover a flat piece of the track, an...
In a single line of the output print four positive integers β€” the number of the row and the number of the column of the upper left corner and the number of the row and the number of the column of the lower right corner of the rectangle that is chosen for the track.
Input: 6 7 483 6 25 4 8 3 3 7 94 1 6 8 7 1 11 6 4 6 4 8 67 2 6 1 6 9 41 9 8 6 3 9 24 5 6 8 4 3 7 | Output: 4 3 6 7
Expert
5
2,511
536
265
4
659
A
659A
A. Round House
1,000
implementation; math
Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent.Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the directio...
The single line of the input contains three space-separated integers n, a and b (1 ≀ n ≀ 100, 1 ≀ a ≀ n, - 100 ≀ b ≀ 100) β€” the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively.
Print a single integer k (1 ≀ k ≀ n) β€” the number of the entrance where Vasya will be at the end of his walk.
The first example is illustrated by the picture in the statements.
Input: 6 2 -5 | Output: 3
Beginner
2
749
234
109
6
620
F
620F
F. Xors on Segments
2,800
data structures; strings; trees
You are given an array with n integers ai and m queries. Each query is described by two integers (lj, rj).Let's define the function . The function is defined for only u ≀ v.For each query print the maximal value of the function f(ax, ay) over all lj ≀ x, y ≀ rj, ax ≀ ay.
The first line contains two integers n, m (1 ≀ n ≀ 5Β·104, 1 ≀ m ≀ 5Β·103) β€” the size of the array and the number of the queries.The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a.Each of the next m lines contains two integers lj, rj (1 ≀ lj ≀ rj ≀ n) – the parameters of the j-th query.
For each query print the value aj on a separate line β€” the maximal value of the function f(ax, ay) over all lj ≀ x, y ≀ rj, ax ≀ ay.
Input: 6 31 2 3 4 5 61 62 53 4 | Output: 777
Master
3
271
318
132
6
661
F
661F
F. Primes in Interval
1,900
*special
You are given two integers a and b (a ≀ b). How many prime numbers are there on the interval from a to b, inclusive?
The input contains two integers a and b (2 ≀ a ≀ b ≀ 1 000 000), separated by a single space.
Output a single integer β€” the number of primes between a and b, inclusive.
Input: 10 20 | Output: 4
Hard
1
116
93
74
6
1,033
B
1033B
B. Square Difference
1,100
math; number theory
Alice has a lovely piece of cloth. It has the shape of a square with a side of length \(a\) centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length \(b\) centimeters (where \(b < a\)). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and...
The first line contains a number \(t\) (\(1 \leq t \leq 5\)) β€” the number of test cases.Each of the next \(t\) lines describes the \(i\)-th test case. It contains two integers \(a\) and \(b~(1 \leq b < a \leq 10^{11})\) β€” the side length of Alice's square and the side length of the square that Bob wants.
Print \(t\) lines, where the \(i\)-th line is the answer to the \(i\)-th test case. Print ""YES"" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print ""NO"".You can print each letter in an arbitrary case (upper or lower).
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is \(6^2 - 5^2 = 36 - 25 = 11\), which is prime, so the answer is ""YES"". In the second case, the area is \(16^2 - 13^2 = 87\),...
Input: 46 516 1361690850361 2477762263034 33 | Output: YESNONOYES
Easy
2
534
305
256
10
311
A
311A
A. The Closest Pair
1,300
constructive algorithms; implementation
Currently Tiny is learning Computational Geometry. When trying to solve a problem called ""The Closest Pair Of Points In The Plane"", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between...
A single line which contains two space-separated integers n and k (2 ≀ n ≀ 2000, 1 ≀ k ≀ 109).
If there doesn't exist such a data which let the given code get TLE, print ""no solution"" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≀ 109) representing the coordinates of the i-th point.The conditions below must be held: All the points must be distinct. |xi|, |yi|...
Input: 4 3 | Output: 0 00 11 01 1
Easy
2
1,162
94
399
3
1,187
C
1187C
C. Vasya And Array
1,800
constructive algorithms; greedy; implementation
Vasya has an array \(a_1, a_2, \dots, a_n\).You don't know this array, but he told you \(m\) facts about this array. The \(i\)-th fact is a triple of numbers \(t_i\), \(l_i\) and \(r_i\) (\(0 \le t_i \le 1, 1 \le l_i < r_i \le n\)) and it means: if \(t_i=1\) then subbarray \(a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}\) is so...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 1000, 1 \le m \le 1000\)).Each of the next \(m\) lines contains three integers \(t_i\), \(l_i\) and \(r_i\) (\(0 \le t_i \le 1, 1 \le l_i < r_i \le n\)).If \(t_i = 1\) then subbarray \(a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}\) is sorted. Otherwise (if \(...
If there is no array that satisfies these facts in only line print NO (in any letter case).If there is a solution, print YES (in any letter case). In second line print \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the array \(a\), satisfying all the given facts. If there are multiple satisfying arr...
Input: 7 4 1 1 3 1 2 5 0 5 6 1 6 7 | Output: YES 1 2 2 3 5 4 4
Medium
3
1,005
397
350
11
394
B
394B
B. Very Beautiful Number
0
math
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a ""very beautiful number"". But the problem is, he's left his paper with the number in the teachers' office.The teacher remembers that the ""very bea...
The single line contains integers p, x (1 ≀ p ≀ 106, 1 ≀ x ≀ 9).
If the teacher's made a mistake and such number doesn't exist, then print on a single line ""Impossible"" (without the quotes). Otherwise, print the ""very beautiful number"" without leading zeroes.
Sample 1: 142857Β·5 = 714285.Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is ""Impossible"".
Input: 6 5 | Output: 142857
Beginner
1
855
64
198
3
1,860
F
1860F
F. Evaluate RBS
2,900
data structures; geometry; implementation; math; sortings
You are given \(2n\) tuples of values \((a, b, c)\), where \(a\) and \(b\) are positive integers and \(c\) is a bracket '(' or ')'. Exactly \(n\) tuples have \(c = \)'(' and the other \(n\) tuples have \(c =\) ')'.You are asked to choose two positive values \(x\) and \(y\) (\(x > 0\) and \(y > 0\); not necessarily inte...
The first line contains a single integer \(t\) (\(1 \le t \le 1500\)) β€” the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 1500\)).The \(i\)-th of the next \(2n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^6\)) and a character \(c_i\) (\(...
For each testcase, print ""YES"" if it's possible to choose such \(x\) and \(y\) that taking brackets \(c\) from the tuples in the resulting order produces a regular bracket sequence. Otherwise, print ""NO"".
In the first testcase, you can choose \(x = 10, y = 0.1\). The values for tuples will be \(10 \cdot 1 + 0.1 \cdot 4 = 10.4\) and \(10 \cdot 2 + 0.1 \cdot 3 = 20.3\). Thus, the first tuple will go first, then the second one, making the bracket sequence ""()"", which is regular.In the second testcase, you can't choose po...
Input: 411 4 (2 3 )11 2 )3 4 (416 5 (12 3 (19 6 )4 10 )3 10 )19 11 (19 7 )7 14 (416 8 (11 9 )20 10 )20 19 )2 13 (18 7 (15 19 )5 6 ( | Output: YES NO NO YES
Master
5
834
497
208
18
2,126
A
2126A
A. Only One Digit
800
brute force; implementation; math
You are given an integer \(x\). You need to find the smallest non-negative integer \(y\) such that the numbers \(x\) and \(y\) share at least one common digit. In other words, there must exist a decimal digit \(d\) that appears in both the representation of the number \(x\) and the number \(y\).
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains one integer \(x\) (\(1 \le x \le 1000\)).
For each test case, output one integer \(y\) β€” the minimum non-negative number that satisfies the condition.
In the first test case, the numbers \(6\) and \(6\) share the common digit '6'. Moreover, there is no natural number smaller than this that shares a common digit.In the second test case, the numbers \(6\) and \(96\) share the common digit '6'.
Input: 569678122696 | Output: 6 6 7 1 6
Beginner
3
296
174
108
21
101
E
101E
E. Candies and Stones
2,500
divide and conquer; dp
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b ston...
The first line contains three integers n, m, p (1 ≀ n, m ≀ 20000, 1 ≀ p ≀ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 ≀ xi ≀ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 ≀ yi ≀ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by numbe...
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a ""C"" or ""S"", the i-th character should be ""C"" if Gerald's i-th move should be eating a candy and ""S"" if he should eat a stone.
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first ea...
Input: 2 2 100 00 1 | Output: 2SC
Expert
2
851
324
315
1
557
B
557B
B. Pasha and Tea
1,500
constructive algorithms; implementation; math; sortings
Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n g...
The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters.The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1...
Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.
Pasha also has candies that he is going to give to girls but that is another task...
Input: 2 41 1 1 1 | Output: 3
Medium
4
1,020
383
248
5
1,265
E
1265E
E. Beautiful Mirrors
2,100
data structures; dp; math; probabilities
Creatnx has \(n\) mirrors, numbered from \(1\) to \(n\). Every day, Creatnx asks exactly one mirror ""Am I beautiful?"". The \(i\)-th mirror will tell Creatnx that he is beautiful with probability \(\frac{p_i}{100}\) for all \(1 \le i \le n\).Creatnx asks the mirrors one by one, starting from the \(1\)-st mirror. Every...
The first line contains one integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β€” the number of mirrors.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq 100\)).
Print the answer modulo \(998244353\) in a single line.
In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability \(\frac{1}{2}\). So, the expected number of days until Creatnx becomes happy is \(2\).
Input: 1 50 | Output: 2
Hard
4
1,168
186
55
12
1,679
E
1679E
E. Typical Party in Dorm
2,400
bitmasks; combinatorics; dp; strings
Today is a holiday in the residence hall β€” Oleh arrived, in honor of which the girls gave him a string. Oleh liked the gift a lot, so he immediately thought up and offered you, his best friend, the following problem.You are given a string \(s\) of length \(n\), which consists of the first \(17\) lowercase Latin letters...
The first line contains a single integer \(n\) (\(1 \le n \le 1\,000\)) β€” the length of the string \(s\).The second line contains the string \(s\), which consists of \(n\) lowercase Latin letters and question marks. It is guaranteed that all letters in the string belong to the set {\(a\), \(b\), \(c\), \(\ldots\), \(p\...
For each query print one number β€” the total numbers of palindromic substrings in all strings that can be obtained from the string \(s\), modulo \(998\,244\,353\).
Consider the first example and the first query in it. We can get only one string as a result of replacing the question marks β€” abaaaba. It has the following palindrome substrings: a β€” substring [\(1\); \(1\)]. b β€” substring [\(2\); \(2\)]. a β€” substring [\(3\); \(3\)]. a β€” substring [\(4\); \(4\)]. a β€” substring [\(5\)...
Input: 7 ab??aba 8 a b ab abc abcd abcde abcdef abcdefg | Output: 14 13 55 105 171 253 351 465
Expert
4
1,419
716
162
16
479
D
479D
D. Long Jumps
1,700
binary search; greedy; implementation
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measureme...
The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ...
In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler.In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any...
In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.In the third sa...
Input: 3 250 185 2300 185 250 | Output: 1230
Medium
3
1,526
375
379
4
2,103
C
2103C
C. Median Splits
1,600
binary search; greedy; implementation; sortings
The median of an array \(b_1, b_2, \ldots b_m\), written as \(\operatorname{med}(b_1, b_2, \ldots, b_m)\), is the \(\left\lceil \frac{m}{2} \right\rceil\)-th\(^{\text{βˆ—}}\) smallest element of array \(b\).You are given an array of integers \(a_1, a_2, \ldots, a_n\) and an integer \(k\). You need to determine whether th...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)) β€” the length of the array \(a\) an...
For each testcase, output ""YES"" if such a split exists, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
In the first and second test case, the only possible partition of the array into three contiguous subarrays is \([3]\), \([2]\), \([1]\). Their respective medians are \(3\), \(2\), and \(1\). The median of the three subarray medians is \(\operatorname{med}(3, 2, 1) = 2\). Therefore, the answer for the first test case i...
Input: 63 23 2 13 13 2 16 38 5 3 1 6 48 710 7 12 16 3 15 6 116 87 11 12 4 9 173 5000000001000 1000000000 1000 | Output: YES NO NO YES YES YES
Medium
4
1,113
576
240
21
2,066
F
2066F
F. Curse
3,300
constructive algorithms; dp; math
You are given two arrays of integers: \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_m\).You need to determine if it is possible to transform array \(a\) into array \(b\) using the following operation several (possibly, zero) times. Among all non-empty subarrays\(^{\text{βˆ—}}\) of \(a\), choose any with the maximum...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases follows. The first line of each test case contains two integers \(n, m\) (\(1 \le n, m \le 500\)) β€” the lengths of arrays \(a\) and \(b\).The second line of each test c...
For each test case, output \(-1\) if it is impossible to transform array \(a\) into array \(b\).Otherwise, in the first line, output the number of operations \(0 \leq q \leq n + m\). Then output the operations in the following format in the order they are performed.In the first line of each operation, print three numbe...
In the first test, the initial array is modified as follows:$$$\( [2, -3, 2, 0] \to [2, -3, -3] \to [-3, -3, -3] \to [-3, -7, -3] \to [-3, -7, 0] \)$$$You may choose to output empty lines or not. Empty lines in the example are added for convenience.
Input: 34 32 -3 2 0-3 -7 02 1-2 -225 4-5 9 -3 5 -9-6 6 -1 -9 | Output: 4 3 4 1 -3 1 1 1 -3 2 2 1 -7 3 3 1 0 -1 3 2 4 1 -5 1 1 1 -6 2 2 2 6 -1
Master
3
897
773
705
20
72
F
72F
F. Oil
1,900
*special; greedy; math
After the nationalization of the oil industry, Dr. Mosaddegh wants to dig some oil wells to extract all the oil in Persian Gulf. But Persian Gulf is huge and has an infinite amount of oil. So Dr. Mosaddegh works only on a rectangular plane of size n Γ— m of the Persian Gulf. Each of the cells in this rectangle either co...
In the first line there are two positive integers n and m (1 ≀ n, m ≀ 100).In the second line there is an integer t (0 ≀ t ≀ n), the number of empty rows. t distinct positive integers follow, these are the numbers of empty rows and are in range [1, n].In the second line there is an integer s (0 ≀ s ≀ m) that shows the ...
A single integer, the minimum number of wells that Dr. Mossadegh has to dig.This is actually finding how many regions are made by removing the given rows and columns.
Input: 2 31 21 2 | Output: 2
Hard
3
1,129
580
166
0
1,540
B
1540B
B. Tree Array
2,300
brute force; combinatorics; dp; graphs; math; probabilities; trees
You are given a tree consisting of \(n\) nodes. You generate an array from the tree by marking nodes one by one.Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked no...
The first line contains a single integer \(n\) (\(2 \le n \le 200\)) β€” the number of nodes in the tree.The next \(n - 1\) lines each contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\); \(x \neq y\)), denoting an edge between node \(x\) and \(y\).It's guaranteed that the given edges form a tree.
Output the expected number of inversions in the generated array modulo \(10^9+7\).Formally, let \(M = 10^9+7\). 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 integer equal to \(p \cdot q^{-1} \...
This is the tree from the first sample: For the first sample, the arrays are almost fixed. If node \(2\) is chosen initially, then the only possible array is \([2, 1, 3]\) (\(1\) inversion). If node \(3\) is chosen initially, then the only possible array is \([3, 1, 2]\) (\(2\) inversions). If node \(1\) is chosen init...
Input: 3 1 2 1 3 | Output: 166666669
Expert
7
878
304
432
15
1,175
G
1175G
G. Yet Another Partiton Problem
3,000
data structures; divide and conquer; dp; geometry; two pointers
You are given array \(a_1, a_2, \dots, a_n\). You need to split it into \(k\) subsegments (so every element is included in exactly one subsegment).The weight of a subsegment \(a_l, a_{l+1}, \dots, a_r\) is equal to \((r - l + 1) \cdot \max\limits_{l \le i \le r}(a_i)\). The weight of a partition is a total weight of al...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^4\), \(1 \le k \le \min(100, n)\)) β€” the length of the array \(a\) and the number of subsegments in the partition.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^4\)) β€” the array \(a\).
Print single integer β€” the minimal weight among all possible partitions.
The optimal partition in the first example is next: \(6\) \(1\) \(7\) \(\bigg|\) \(4\).The optimal partition in the second example is next: \(6\) \(\bigg|\) \(1\) \(\bigg|\) \(7\) \(4\).One of the optimal partitions in the third example is next: \(5\) \(\bigg|\) \(1\) \(5\) \(\bigg|\) \(1\) \(\bigg|\) \(5\).
Input: 4 2 6 1 7 4 | Output: 25
Master
5
372
309
72
11
313
C
313C
C. Ilya and Matrix
1,400
constructive algorithms; greedy; implementation; sortings
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve.He's got a square 2n Γ— 2n-sized matrix and 4n integers. You need to arrange all these numbers in the matrix (put each number in a single individual ...
The first line contains integer 4n (1 ≀ 4n ≀ 2Β·106). The next line contains 4n integers ai (1 ≀ ai ≀ 109) β€” the numbers you need to arrange in the 2n Γ— 2n-sized matrix.
On a single line print the maximum value of the beauty of the described matrix.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Consider the second sample. You need to arrange the numbers in the matrix as follows:1 23 4Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
Input: 113 | Output: 13
Easy
4
922
168
226
3
130
J
130J
J. Date calculation
1,800
*special
In Gregorian calendar a typical year consists of 365 days and 12 months. The numbers of days in the months are: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. If year index is divisible by 400, or divisible by 4 but not by 100, the year becomes leap year, with one extra day in the second month (the one which typically...
The first line of input contains the year index, between 1600 and 2400, inclusive. The second line contains the day index, between 1 and 366, inclusive. It is guaranteed that the day index will be valid for this year, i.e., day 366 will occur only in a leap year.
Output the index of the day and the index of the month, separated with a space.
All indexes are 1-based.
Input: 2011324 | Output: 20 11
Medium
1
472
263
79
1
1,916
B
1916B
B. Two Divisors
1,000
constructive algorithms; math; number theory
A certain number \(1 \le x \le 10^9\) is chosen. You are given two integers \(a\) and \(b\), which are the two largest divisors of the number \(x\). At the same time, the condition \(1 \le a < b < x\) is satisfied.For the given numbers \(a\), \(b\), you need to find the value of \(x\).\(^{\dagger}\) The number \(y\) is...
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then follows the description of the test cases.The only line of each test cases contains two integers \(a\), \(b\) (\(1 \le a < b \le 10^9\)).It is guaranteed that \(a\), \(b\) are...
For each test case, output the number \(x\), such that \(a\) and \(b\) are the two largest divisors of the number \(x\).If there are several answers, print any of them.
For the first test case, all divisors less than \(6\) are equal to \([1, 2, 3]\), among them the two largest will be \(2\) and \(3\).For the third test case, all divisors less than \(33\) are equal to \([1, 3, 11]\), among them the two largest will be \(3\) and \(11\).For the fifth test case, all divisors less than \(2...
Input: 82 31 23 111 55 104 63 9250000000 500000000 | Output: 6 4 33 25 20 12 27 1000000000
Beginner
3
408
383
168
19
1,692
H
1692H
H. Gambling
1,700
data structures; dp; greedy; math
Marian is at a casino. The game at the casino works like this.Before each round, the player selects a number between \(1\) and \(10^9\). After that, a dice with \(10^9\) faces is rolled so that a random number between \(1\) and \(10^9\) appears. If the player guesses the number correctly their total money is doubled, e...
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2\cdot 10^5\)) β€” the number of rounds.The second line of each test case contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \leq x_i \leq ...
For each test case, output three integers \(a\), \(l\), and \(r\) such that Marian makes the most amount of money gambling with his strategy. If there are multiple answers, you may output any of them.
For the first test case, the best choice is \(a=4\), \(l=1\), \(r=5\), and the game would go as follows. Marian starts with one dollar. After the first round, he ends up with \(2\) dollars because the numbers coincide with the chosen one. After the second round, he ends up with \(4\) dollars because the numbers coincid...
Input: 454 4 3 4 4511 1 11 1 1111000000000108 8 8 9 9 6 6 9 6 6 | Output: 4 1 5 1 2 2 1000000000 1 1 6 6 10
Medium
4
1,214
496
200
16
1,728
G
1728G
G. Illumination
2,700
binary search; bitmasks; brute force; combinatorics; dp; math; two pointers
Consider a segment \([0, d]\) of the coordinate line. There are \(n\) lanterns and \(m\) points of interest in this segment.For each lantern, you can choose its power β€” an integer between \(0\) and \(d\) (inclusive). A lantern with coordinate \(x\) illuminates the point of interest with coordinate \(y\) if \(|x - y|\) ...
The first line contains three integers \(d\), \(n\) and \(m\) (\(4 \le d \le 3 \cdot 10^5\); \(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 16\)) β€” the size of the segment, the number of lanterns and the number of points of interest, respectively.The second line contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \le l_...
For each query, print one integer β€” the answer to it, taken modulo \(998244353\).
Input: 6 1 1 4 3 3 2 1 5 | Output: 48 47 47
Master
7
815
1,106
81
17
802
E
802E
E. Marmots (medium)
2,200
math
Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.Can you help Heidi estimate each village's population?
Same as the easy version.
Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.Your answer is considered correct if it is an integer that falls into the interval , where P is the real population of the village, used to create the distribution (either Poisson or uniform) from whic...
Hard
1
208
25
353
8
1,041
A
1041A
A. Heist
800
greedy; implementation; sortings
There was an electronic store heist last night.All keyboards which were in the store yesterday were numbered in ascending order from some integer number \(x\). For example, if \(x = 4\) and there were \(3\) keyboards in the store, then the devices had indices \(4\), \(5\) and \(6\), and if \(x = 10\) and there were \(7...
The first line contains single integer \(n\) \((1 \le n \le 1\,000)\) β€” the number of keyboards in the store that remained after the heist.The second line contains \(n\) distinct integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^{9})\) β€” the indices of the remaining keyboards. The integers \(a_i\) are given in arbi...
Print the minimum possible number of keyboards that have been stolen if the staff remember neither \(x\) nor the number of keyboards in the store before the heist.
In the first example, if \(x=8\) then minimum number of stolen keyboards is equal to \(2\). The keyboards with indices \(9\) and \(11\) were stolen during the heist.In the second example, if \(x=4\) then nothing was stolen during the heist.
Input: 410 13 12 8 | Output: 2
Beginner
3
679
358
163
10
1,549
B
1549B
B. Gregor and the Pawn Game
800
dfs and similar; dp; flows; graph matchings; graphs; greedy; implementation
There is a chessboard of size \(n\) by \(n\). The square in the \(i\)-th row from top and \(j\)-th column from the left is labelled \((i,j)\).Currently, Gregor has some pawns in the \(n\)-th row. There are also enemy pawns in the \(1\)-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (f...
The first line of the input contains one integer \(t\) (\(1\le t\le 2\cdot 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case consists of three lines. The first line contains a single integer \(n\) (\(2\le n\le 2\cdot{10}^{5}\)) β€” the size of the chessboard.The second line consists of a str...
For each test case, print one integer: the maximum number of Gregor's pawns which can reach the \(1\)-st row.
In the first example, Gregor can simply advance all \(3\) of his pawns forward. Thus, the answer is \(3\).In the second example, Gregor can guarantee that all \(4\) of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this ""game""! ...
Input: 4 3 000 111 4 1111 1111 3 010 010 5 11001 00000 | Output: 3 4 0 0
Beginner
7
851
809
109
15
754
E
754E
E. Dasha and cyclic table
2,600
bitmasks; brute force; fft; strings; trees
Dasha is fond of challenging puzzles: Rubik's Cube 3 Γ— 3 Γ— 3, 4 Γ— 4 Γ— 4, 5 Γ— 5 Γ— 5 and so on. This time she has a cyclic table of size n Γ— m, and each cell of the table contains a lowercase English letter. Each cell has coordinates (i, j) (0 ≀ i < n, 0 ≀ j < m). The table is cyclic means that to the right of cell (i, j...
The first line contains two integers n and m (1 ≀ n, m ≀ 400) β€” the cyclic table sizes.Each of the next n lines contains a string of m lowercase English characters β€” the description of the cyclic table.The next line contains two integers r and c (1 ≀ r, c ≀ 400) β€” the sizes of the pattern.Each of the next r lines conta...
Print n lines. Each of the n lines should contain m characters. Each of the characters should equal '0' or '1'.The j-th character of the i-th (0-indexed) line should be equal to '1', in case the cell (i, j) is an appearance position, otherwise it should be equal to '0'.
Input: 5 7qcezchshhedywqwikywqyqckrqztbqexcxz3 2??yw?q | Output: 00001000001001000000000000000000000
Expert
5
1,074
418
270
7
1,001
E
1001E
E. Distinguish Bell states
1,600
*special
You are given two qubits which are guaranteed to be in one of the Bell states: Your task is to perform necessary operations and measurements to figure out which state it was and to return the index of that state (0 for , 1 for etc.). The state of the qubits after the operations does not matter.
You have to implement an operation which takes an array of two qubits as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Int { body { // your code here } }}
Medium
1
295
305
0
10
813
E
813E
E. Army Creation
2,200
binary search; data structures
As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires.In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to create a balanced army hiring some subset of warriors. An army is called balanced if for...
The first line contains two integers n and k (1 ≀ n, k ≀ 100000).The second line contains n integers a1, a2, ... an (1 ≀ ai ≀ 100000).The third line contains one integer q (1 ≀ q ≀ 100000).Then q lines follow. ith line contains two numbers xi and yi which represent ith plan (1 ≀ xi, yi ≀ n).You have to keep track of th...
Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan.
In the first example the real plans are: 1 2 1 6 6 6 2 4 4 6
Input: 6 21 1 1 2 2 251 64 31 12 62 6 | Output: 24132
Hard
2
832
566
107
8
717
B
717B
B. R3D3’s Summer Adventure
2,700
dp; greedy
R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to ""The White Buttons"" and their hit song ""Dacan the Baker"", he met a...
The first line of input contains three integers n (2 ≀ n ≀ 108), c0 and c1 (0 ≀ c0, c1 ≀ 108) β€” the number of letters in the alphabet, and costs of '0' and '1', respectively.
Output a single integer β€” minimum possible total a cost of the whole alphabet.
There are 4 letters in the alphabet. The optimal encoding is ""00"", ""01"", ""10"", ""11"". There are 4 zeroes and 4 ones used, so the total cost is 4Β·1 + 4Β·2 = 12.
Input: 4 1 2 | Output: 12
Master
2
1,404
174
78
7
111
E
111E
E. Petya and Rectangle
2,900
Little Petya loves playing with rectangles. Mom bought Petya a rectangle divided into cells n Γ— m in size (containing n rows, m columns). Petya marked two different cells of the rectangle and now he is solving the following task:Let's define a simple path between those two cells as a sequence of distinct cells a1, a2, ...
The first line contains space-separated integers n and m (4 ≀ n, m ≀ 1000) β€” the number of rows and the number of columns in the rectangle, correspondingly. The second line contains space-separated integers x1 and y1 β€” the coordinates of the first marked cell. The third line contains space-separated integers x2 y2 β€” th...
In the first line print the length of the found path β€” k. In the next lines print k pairs of integers, one per line β€” coordinates of the cells that constitute the found path in the order, in which they follow in the path (the path must go from cell (x1, y1) to cell (x2, y2)). If there are several solutions, print any o...
The statement test is described in the picture:
Input: 4 42 23 3 | Output: 152 21 21 12 13 14 14 24 34 43 42 41 41 32 33 3
Master
0
599
793
327
1
93
A
93A
A. Frames
1,700
implementation
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well.This time Igor K. got disappointed in on...
The only line contains four integers n, m, a, b (1 ≀ n, m ≀ 109, 1 ≀ a ≀ b ≀ n). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.
Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from a to b.
The images below illustrate statement tests.The first test:In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection.The second test:In this test we can first select all folders in the first row (2, 3, 4, 5), then β€” all ...
Input: 11 4 3 9 | Output: 3
Medium
1
1,328
231
163
0
724
B
724B
B. Batch Sort
1,500
brute force; greedy; implementation; math
You are given a table consisting of n rows and m columns.Numbers in each row form a permutation of integers from 1 to m.You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to p...
The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20) β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integers β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.
If there is a way to obtain the identity permutation in each row by following the given rules, print ""YES"" (without quotes) in the only line of the output. Otherwise, print ""NO"" (without quotes).
In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1 2 3 4 1 4 3 2 In the second row, swap the second and the fourth elements. Now the table is 1 2 3 4 1 2 3 4
Input: 2 41 3 2 41 3 4 2 | Output: YES
Medium
4
637
292
199
7
1,325
A
1325A
A. EhAb AnD gCd
800
constructive algorithms; greedy; number theory
You are given a positive integer \(x\). Find any such \(2\) positive integers \(a\) and \(b\) such that \(GCD(a,b)+LCM(a,b)=x\).As a reminder, \(GCD(a,b)\) is the greatest integer that divides both \(a\) and \(b\). Similarly, \(LCM(a,b)\) is the smallest integer such that both \(a\) and \(b\) divide it.It's guaranteed ...
The first line contains a single integer \(t\) \((1 \le t \le 100)\) β€” the number of testcases.Each testcase consists of one line containing a single integer, \(x\) \((2 \le x \le 10^9)\).
For each testcase, output a pair of positive integers \(a\) and \(b\) (\(1 \le a, b \le 10^9)\) such that \(GCD(a,b)+LCM(a,b)=x\). It's guaranteed that the solution always exists. If there are several such pairs \((a, b)\), you can output any of them.
In the first testcase of the sample, \(GCD(1,1)+LCM(1,1)=1+1=2\).In the second testcase of the sample, \(GCD(6,4)+LCM(6,4)=2+12=14\).
Input: 2 2 14 | Output: 1 1 6 4
Beginner
3
424
188
251
13
1,945
D
1945D
D. Seraphim the Owl
1,300
dp; greedy
The guys lined up in a queue of \(n\) people, starting with person number \(i = 1\), to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the \(n\)-th person. Kirill is completely di...
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then follows the description of the test case.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 200\,000\)) β€” the number of people...
For each test case, output a single integer β€” the minimum number of coins Kirill needs to spend.
Input: 44 27 3 6 94 3 8 56 26 9 7 1 8 35 8 8 1 4 17 77 2 9 2 6 5 99 1 10 7 1 4 92 12 31 1 | Output: 14 22 9 3
Easy
2
1,150
739
96
19
505
C
505C
C. Mr. Kitayuta, the Treasure Hunter
1,900
dfs and similar; dp; two pointers
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi....
The first line of the input contains two space-separated integers n and d (1 ≀ n, d ≀ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.The next n lines describe the location of the gems. The i-th of them (1 ≀ i ≀ n) contains a integer pi (d ≀ p...
Print the maximum number of gems that Mr. Kitayuta can collect.
In the first sample, the optimal route is 0 β†’ 10 (+1 gem) β†’ 19 β†’ 27 (+2 gems) β†’ ...In the second sample, the optimal route is 0 β†’ 8 β†’ 15 β†’ 21 β†’ 28 (+1 gem) β†’ 36 (+1 gem) β†’ 45 (+1 gem) β†’ 55 (+1 gem) β†’ 66 (+1 gem) β†’ 78 (+1 gem) β†’ ...In the third sample, the optimal route is 0 β†’ 7 β†’ 13 β†’ 18 (+1 gem) β†’ 24 (+2 gems) β†’ 30 (...
Input: 4 1010212727 | Output: 3
Hard
3
1,175
409
63
5
113
C
113C
C. Double Happiness
2,200
brute force; math; number theory
On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as: t = a2 + b2, where a, b are arbitrary positive integers.Now, the boys decided to find o...
The first line of the input contains integer numbers l, r (1 ≀ l, r ≀ 3Β·108).
In the only line print the number of days on the segment [l, r], which are lucky for Peter and Bob at the same time.
Input: 3 5 | Output: 1
Hard
3
605
77
116
1
1,184
A1
1184A1
A1. Heidi Learns Hashing (Easy)
1,200
brute force; math; number theory
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.Heidi figured out that Madame Kovarian uses a v...
The first and only line contains an integer \(r\) (\(1 \le r \le 10^{12}\)).
Output integers \(x, y\) such that \(H(x,y) = r\) and \(x\) is smallest possible, or ""NO"" if no such pair exists.
Input: 19 | Output: 1 8
Easy
3
957
76
115
11
222
E
222E
E. Decoding Genome
1,900
dp; matrices
Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from followin...
The first line contains three space-separated integers n, m, k (1 ≀ n ≀ 1015, 1 ≀ m ≀ 52, 0 ≀ k ≀ m2).Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represen...
Print a single integer β€” the sought number modulo 1000000007 (109 + 7).
In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.In the third test sample we cannot make any DNA of two nucleotides β€” the only possible nucleotide ""a"" cannot occur two times consecutively.
Input: 3 3 2abba | Output: 17
Hard
2
887
1,032
71
2
1,917
E
1917E
E. Construct Matrix
2,500
constructive algorithms; math
You are given an even integer \(n\) and an integer \(k\). Your task is to construct a matrix of size \(n \times n\) consisting of numbers \(0\) and \(1\) in such a way that the following conditions are true, or report that it is impossible: the sum of all the numbers in the matrix is exactly \(k\); the bitwise \(\textt...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 130\)) β€” the number of test cases. The description of the test cases follows.Each test case is described by a single line, which contains two integers \(n\) and \(k\) (\(2 \leq n \leq 1000\), \(n\) is even, \(0 \l...
For each test case, output \(\texttt{Yes}\) if it's possible to construct a matrix that satisfies all of the problem's conditions, and \(\texttt{No}\) otherwise.If it is possible to construct a matrix, the \(i\)-th of the next \(n\) lines should contain \(n\) integers representing the elements in the \(i\)-th row of th...
In the first example, all conditions are satisfied: the sum of all the numbers in the matrix is exactly \(0\); the bitwise \(\texttt{XOR}\) of all the numbers in the row \(i\) is \(0\) for each \(i\); the bitwise \(\texttt{XOR}\) of all the numbers in the column \(j\) is \(0\) for each \(j\). In the third example, it c...
Input: 5 4 0 6 6 6 5 4 2 6 36 | Output: Yes 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Yes 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 No No Yes 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Expert
2
488
421
329
19
1,444
B
1444B
B. Divide and Sum
1,900
combinatorics; math; sortings
You are given an array \(a\) of length \(2n\). Consider a partition of array \(a\) into two subsequences \(p\) and \(q\) of length \(n\) each (each element of array \(a\) should be in exactly one subsequence: either in \(p\) or in \(q\)).Let's sort \(p\) in non-decreasing order, and \(q\) in non-increasing order, we ca...
The first line contains a single integer \(n\) (\(1 \leq n \leq 150\,000\)).The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(1 \leq a_i \leq 10^9\)) β€” elements of array \(a\).
Print one integer β€” the answer to the problem, modulo \(998244353\).
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence \(p\) are different.In the first example, there are two correct partitions of the array \(a\): \(p = [1]\), \(q = [4]\), then \(x = [1]\), \(y = [4]\), \(f(p, q) = |1 - 4| = 3\); \(p = [4]\), \(q = [1]\), ...
Input: 1 1 4 | Output: 6
Hard
3
615
199
68
14
1,774
D
1774D
D. Same Count One
1,600
brute force; constructive algorithms; greedy; implementation; two pointers
ChthollyNotaSeniorious received a special gift from AquaMoon: \(n\) binary arrays of length \(m\). AquaMoon tells him that in one operation, he can choose any two arrays and any position \(pos\) from \(1\) to \(m\), and swap the elements at positions \(pos\) in these arrays.He is fascinated with this game, and he wants...
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 2\cdot 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \leq n \leq 10^5\), \(2 \leq m \leq 10^5\)).The \(i\)-th of the following \(n\) lines ...
For each test case, if the objective is not achievable, output \(-1\). Otherwise, in the first line output \(k\) \((0 \le k \le mn)\) β€” the minimum number of operations required.The \(i\)-th of the following \(k\) lines should contain \(3\) integers, \(x_i, y_i, z_i\) \((1 \le x_i, y_i \le n, 1 \le z_i \le m)\), which ...
In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become \([0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]\), each of them contains exactly two \(1\)s.
Input: 33 41 1 1 00 0 1 01 0 0 14 31 0 00 1 10 0 10 0 02 20 00 1 | Output: 1 2 1 1 1 4 2 2 -1
Medium
5
644
542
451
17
1,774
B
1774B
B. Coloring
1,500
constructive algorithms; greedy; math
Cirno_9baka has a paper tape with \(n\) cells in a row on it. As he thinks that the blank paper tape is too dull, he wants to paint these cells with \(m\) kinds of colors. For some aesthetic reasons, he thinks that the \(i\)-th color must be used exactly \(a_i\) times, and for every \(k\) consecutive cells, their color...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10\,000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(k\) (\(1 \leq k \leq n \leq 10^9\), \(1 \leq m \leq 10^5\), \(m \leq n\)). Here \(n\) denotes the number o...
For each test case, output ""YES"" if there is at least one possible coloring scheme; otherwise, output ""NO"".You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", and ""yEs"" will all be recognized as positive answers).
In the first test case, there is no way to color the cells satisfying all the conditions.In the second test case, we can color the cells as follows: \((1, 2, 1, 2, 3, 4, 3, 4, 5, 6, 5, 6)\). For any \(2\) consecutive cells, their colors are distinct.
Input: 212 6 21 1 1 1 1 712 6 22 2 2 2 2 2 | Output: NO YES
Medium
3
416
753
246
17
1,829
B
1829B
B. Blank Space
800
implementation
You are given a binary array \(a\) of \(n\) elements, a binary array is an array consisting only of \(0\)s and \(1\)s. A blank space is a segment of consecutive elements consisting of only \(0\)s. Your task is to find the length of the longest blank space.
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) β€” the length of the array.The second line of each test case contains \(n\) space-separated integers \(a_i\) (\(0 \leq a_i \leq 1\)) ...
For each test case, output a single integer β€” the length of the longest blank space.
Input: 551 0 0 1 040 1 1 11031 1 191 0 0 0 1 0 0 0 1 | Output: 2 1 1 0 3
Beginner
1
256
348
84
18
2,010
A
2010A
A. Alternating Sum of Numbers
800
brute force; implementation; math
You are given a sequence of integers. Output the alternating sum of this sequence. In other words, output \(a_1 - a_2 + a_3 - a_4 + a_5 - \dots\). That is, the signs of plus and minus alternate, starting with a plus.
The first line of the test contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Then follow \(t\) test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 50\)) β€” the length of the sequence. The second line of the test case contains \(n\) integers \(a_1, a_2, \dots,...
Output \(t\) lines. For each test case, output the required alternating sum of the numbers.
Input: 441 2 3 1711002100 10053 1 4 1 5 | Output: -15 100 0 10
Beginner
3
216
351
91
20
44
F
44F
F. BerPaint
2,700
geometry; graphs
Anfisa the monkey got disappointed in word processors as they aren't good enough at reflecting all the range of her emotions, that's why she decided to switch to graphics editors. Having opened the BerPaint, she saw a white rectangle W Γ— H in size which can be painted on. First Anfisa learnt to navigate the drawing too...
The first input line has two integers W and H (3 ≀ W, H ≀ 104) β€” the sizes of the initially white rectangular painting area. The second line contains integer n β€” the number of black segments (0 ≀ n ≀ 100). On the next n lines are described the segments themselves, each of which is given by coordinates of their endpoint...
For every color present in the final picture print on the single line the name of the color and the total area of areas painted that color with an accuracy of 10 - 6. Print the colors in any order.
Initially the black segments painted by Anfisa can also be painted a color if any of the chosen points lays on the segment. The segments have areas equal to 0. That is why if in the final picture only parts of segments is painted some color, then the area, painted the color is equal to 0.
Input: 4 561 1 1 31 3 3 33 3 3 13 1 1 11 3 3 11 1 3 322 1 red2 2 blue | Output: blue 0.00000000white 20.00000000
Master
2
1,014
880
197
0
1,254
E
1254E
E. Send Tree to Charlie
3,300
combinatorics; dfs and similar; dsu; trees
Christmas was knocking on the door, and our protagonist, Bob, was preparing a spectacular present for his long-time second best friend Charlie. As chocolate boxes are lame, he decided to decorate a tree instead. Bob's tree can be represented as an undirected connected graph with \(n\) nodes (numbered \(1\) to \(n\)) an...
The first line contains a single integer \(n\) (\(2 \leq n \leq 500\,000\)) β€” the number of nodes.Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n\)), denoting that there is an edge connecting two nodes \(u\) and \(v\). It is guaranteed that the given edges form a tree.The la...
Output the number of possible configurations modulo \(1000000007\) (\(10^9+7\)).
In the first example, the possible configurations of the tree are \([2, 4, 1, 3]\) and \([3, 4, 2, 1]\).In the second example, note that while there are \(4! = 24\) possible permutations of the edges, each of them results in a possible configuration, there are only \(12\) different configurations.In the third example, ...
Input: 4 3 4 2 4 4 1 0 4 0 0 | Output: 2
Master
4
1,527
619
80
12
1,931
F
1931F
F. Chat Screenshots
1,700
combinatorics; dfs and similar; graphs
There are \(n\) people in the programming contest chat. Chat participants are ordered by activity, but each person sees himself at the top of the list.For example, there are \(4\) participants in the chat, and their order is \([2, 3, 1, 4]\). Then \(1\)-st user sees the order \([1, 2, 3, 4]\). \(2\)-nd user sees the or...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of input test cases. The descriptions of test cases follow.The first line of the description of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5, n \cdot k \le 2 \cdot 10^5\)) β€” the number of chat pa...
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if there exists at least one order of participants, under which all \(k\) screenshots could have been obtained. Otherwise, output ""NO"".You can output the answer in any case (upper or lower). For example, the s...
Input: 105 11 2 3 4 54 41 2 3 42 3 1 43 2 1 44 2 3 16 21 3 5 2 4 66 3 5 2 1 43 31 2 32 3 13 2 110 21 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 11 115 21 2 3 5 42 1 3 5 43 33 1 22 3 11 3 25 43 5 1 4 22 5 1 4 31 5 4 3 25 1 4 3 23 31 3 22 1 33 2 1 | Output: YES YES YES YES NO YES YES YES YES NO
Medium
3
737
965
407
19
332
E
332E
E. Binary Key
2,400
dp; greedy; implementation
Let's assume that p and q are strings of positive length, called the container and the key correspondingly, string q only consists of characters 0 and 1. Let's take a look at a simple algorithm that extracts message s from the given container p:i = 0;j = 0;s = <>;while i is less than the length of the string p{ if q[j]...
The first two lines of the input are non-empty strings p and s (1 ≀ |p| ≀ 106, 1 ≀ |s| ≀ 200), describing the container and the message, correspondingly. The strings can contain any characters with the ASCII codes from 32 to 126, inclusive.The third line contains a single integer k (1 ≀ k ≀ 2000) β€” the key's length.
Print the required key (string of length k, consisting only of characters 0 and 1). If the key doesn't exist, print the single character 0.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such integer r (0 ≀ r < min(p, q)) that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. Symbols are compared according to their ASCII codes.
Input: abacabaaba6 | Output: 100001
Expert
3
1,118
317
139
3
1,187
G
1187G
G. Gang Up
2,500
flows; graphs
The leader of some very secretive organization has decided to invite all other members to a meeting. All members of the organization live in the same town which can be represented as \(n\) crossroads connected by \(m\) two-directional streets. The meeting will be held in the leader's house near the crossroad \(1\). The...
The first line of the input contains five integer numbers \(n\), \(m\), \(k\), \(c\) and \(d\) (\(2 \le n \le 50\), \(n - 1 \le m \le 50\), \(1 \le k, c, d \le 50\)) β€” the number of crossroads, the number of streets, the number of persons invited to the meeting and the constants affecting the discontent, respectively.T...
Print one integer: the minimum discontent of the leader after everyone reaches crossroad \(1\).
The best course of action in the first test is the following: the first person goes along the street \(2\) to the crossroad \(2\), then goes along the street \(1\) to the crossroad \(1\) and attends the meeting; the second person waits one minute on the crossroad \(3\), then goes along the street \(2\) to the crossroad...
Input: 3 2 4 2 3 3 3 3 3 1 2 2 3 | Output: 52
Expert
2
2,328
864
95
11
346
C
346C
C. Number Transformation II
2,200
greedy; math
You are given a sequence of positive integers x1, x2, ..., xn and two non-negative integers a and b. Your task is to transform a into b. To do that, you can perform the following moves: subtract 1 from the current a; subtract a mod xi (1 ≀ i ≀ n) from the current a. Operation a mod xi means taking the remainder after d...
The first line contains a single integer n (1 ≀ n ≀ 105). The second line contains n space-separated integers x1, x2, ..., xn (2 ≀ xi ≀ 109). The third line contains two integers a and b (0 ≀ b ≀ a ≀ 109, a - b ≀ 106).
Print a single integer β€” the required minimum number of moves needed to transform number a into number b.
Input: 33 4 530 17 | Output: 6
Hard
2
431
218
105
3
587
B
587B
B. Duff in Beach
2,100
dp
While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denot...
The first line of input contains three integers, n, l and k (1 ≀ n, k, n Γ— k ≀ 106 and 1 ≀ l ≀ 1018).The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≀ ai ≀ 109 for each 0 ≀ i ≀ n - 1).
Print the answer modulo 1 000 000 007 in one line.
In the first sample case, . So all such sequences are: , , , , , , , , and .
Input: 3 5 35 9 1 | Output: 10
Hard
1
799
212
50
5
1,539
B
1539B
B. Love Song
800
dp; implementation; strings
Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up \(q\) questions about this song. Each question is about a subsegment of the song starting from the \(l\)-th letter to the \(r\)-th letter. Vasya considers a substring made up from charact...
The first line contains two integers \(n\) and \(q\) (\(1\leq n\leq 100\,000\), \(1\leq q \leq 100\,000\)) β€” the length of the song and the number of questions. The second line contains one string \(s\) β€” the song, consisting of \(n\) lowercase letters of English letters.Vasya's questions are contained in the next \(q\...
Print \(q\) lines: for each question print the length of the string obtained by Vasya.
In the first example Vasya is interested in three questions. In the first question Vasya considers the substring ""aba"", that transforms to ""abba"", so the answer is equal to \(4\). In the second question Vasya considers ""baca"", that transforms to ""bbaccca"", so the answer is \(7\). In the third question Vasya con...
Input: 7 3 abacaba 1 3 2 5 1 7 | Output: 4 7 11
Beginner
3
811
435
86
15
1,805
E
1805E
E. There Should Be a Lot of Maximums
2,300
brute force; data structures; dfs and similar; dp; trees; two pointers
You are given a tree (a connected graph without cycles). Each vertex of the tree contains an integer. Let's define the \(\mathrm{MAD}\) (maximum double) parameter of the tree as the maximum integer that occurs in the vertices of the tree at least \(2\) times. If no number occurs in the tree more than once, then we assu...
The first line contains one integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of vertices in the tree.Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\)) β€” the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.The last line contains \(n\) integ...
For each edge in the input order, print one number β€” the maximum of the \(\mathrm{MAD}\) parameters of the two trees obtained after removing the given edge from the initial tree.
In the first example, after removing edge \((1, 2)\) no number repeats \(2\) times in any of the resulting subtrees, so the answer is \(\max(0, 0)=0\).After removing edge \((2, 3)\), in the bigger subtree, \(1\) is repeated twice, and \(2\) is repeated twice, so the \(\mathrm{MAD}\) of this tree is \(2\).After removing...
Input: 5 1 2 2 3 2 4 1 5 2 1 3 2 1 | Output: 0 2 1 2
Expert
6
698
405
178
18
852
H
852H
H. Bob and stages
3,000
dp; geometry
The citizens of BubbleLand are celebrating their 10th anniversary so they decided to organize a big music festival. Bob got a task to invite N famous singers who would sing on the fest. He was too busy placing stages for their performances that he totally forgot to write the invitation e-mails on time, and unfortunatel...
The first line of input contains two integers N (3 ≀ N ≀ 200) and K (3 ≀ K ≀ min(N, 50)), separated with one empty space, representing number of stages and number of singers, respectively.Each of the next N lines contains two integers Xi and Yi (0 ≀ Xi, Yi ≀ 106) representing the coordinates of the stages. There are no...
Output contains only one line with one number, rounded to exactly two decimal places: the maximal festival area. Rounding is performed so that 0.5 and more rounds up and everything else rounds down.
Example explanation: From all possible convex polygon with 4 vertices and no other vertex inside, the largest is one with points (0, 0), (2, 1), (4, 4) and (1, 5).
Input: 5 40 03 02 14 41 5 | Output: 10.00
Master
2
1,125
352
198
8
587
C
587C
C. Duff in the Army
2,200
data structures; trees
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). E...
The first line of input contains three integers, n, m and q (1 ≀ n, m, q ≀ 105).The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≀ v, u ≀ n, v β‰  u).Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≀ ci ≀ n for each 1 ≀ i ≀ m).Next q lines contai...
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Input: 5 4 51 31 21 44 52 1 4 34 5 61 5 25 5 102 3 35 3 1 | Output: 1 32 2 303 1 2 41 2
Hard
2
1,091
414
81
5
1,537
E1
1537E1
E1. Erase and Extend (Easy Version)
1,600
binary search; brute force; dp; greedy; hashing; implementation; string suffix structures; strings; two pointers
This is the easy version of the problem. The only difference is the constraints on \(n\) and \(k\). You can make hacks only if all versions of the problem are solved.You have a string \(s\), and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: \(s:=s+s\), where \(...
The first line contains two integers \(n\), \(k\) (\(1 \leq n, k \leq 5000\)) β€” the length of the original string \(s\) and the length of the desired string.The second line contains the string \(s\), consisting of \(n\) lowercase English letters.
Print the lexicographically smallest string of length \(k\) that can be obtained by doing the operations on string \(s\).
In the first test, it is optimal to make one duplication: ""dbcadabc"" \(\to\) ""dbcadabcdbcadabc"".In the second test it is optimal to delete the last \(3\) characters, then duplicate the string \(3\) times, then delete the last \(3\) characters to make the string have length \(k\).""abcd"" \(\to\) ""abc"" \(\to\) ""a...
Input: 8 16 dbcadabc | Output: dbcadabcdbcadabc
Medium
9
865
246
121
15
639
D
639D
D. Bear and Contribution
2,400
data structures; greedy; sortings; two pointers
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution β€” an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.Limak is a little polar bear and he's new into competiti...
The first line contains four integers n, k, b and c (2 ≀ k ≀ n ≀ 200 000, 1 ≀ b, c ≀ 1000) β€” the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.The second line contains n integ...
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows. He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. Then he spends 4Β·30 = 120 minutes to read four comments of the 2-nd user and increase his...
Input: 4 3 100 3012 2 6 1 | Output: 220
Expert
4
1,125
411
102
6
2,077
B
2077B
B. Finding OR Sum
1,900
bitmasks; constructive algorithms; implementation; interactive; math
ALTER EGO - Yuta Imai vs Qlarabelle This is an interactive problem.There are two hidden non-negative integers \(x\) and \(y\) (\(0 \leq x, y < 2^{30}\)). You can ask no more than \(2\) queries of the following form. Pick a non-negative integer \(n\) (\(0 \leq n < 2^{30}\)). The judge will respond with the value of \((n...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.
In the first test, the interaction proceeds as follows. SolutionJuryExplanation\(\texttt{2}\)There are 2 test cases.\(\texttt{}\)In the first test case, \(x=1\) and \(y=2\).\(\texttt{0}\)\(\texttt{3}\)The solution requests \((0 \mathbin{|} 1) + (0 \mathbin{|} 2)\), and the jury responds with \(3\).\(\texttt{1}\)\(\text...
Input: 2 3 4 1 0 1 | Output: 0 1 ! 4 0 ! 2
Hard
5
580
161
0
20
1,095
B
1095B
B. Array Stabilization
900
implementation
You are given an array \(a\) consisting of \(n\) integer numbers.Let instability of the array be the following value: \(\max\limits_{i = 1}^{n} a_i - \min\limits_{i = 1}^{n} a_i\).You have to remove exactly one element from this array to minimize instability of the resulting \((n-1)\)-elements array. Your task is to ca...
The first line of the input contains one integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of elements in the array \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)) β€” elements of the array \(a\).
Print one integer β€” the minimum possible instability of the array if you have to remove exactly one element from the array \(a\).
In the first example you can remove \(7\) then instability of the remaining array will be \(3 - 1 = 2\).In the second example you can remove either \(1\) or \(100000\) then instability of the remaining array will be \(100000 - 100000 = 0\) and \(1 - 1 = 0\) correspondingly.
Input: 4 1 3 3 7 | Output: 2
Beginner
1
361
255
129
10
1,387
C
1387C
C. Viruses
2,900
*special; dp; shortest paths; string suffix structures
The Committee for Research on Binary Viruses discovered a method of replication for a large family of viruses whose genetic codes are sequences of zeros and ones. Each virus originates from a single gene; for simplicity genes are denoted by integers from \(0\) to \(G - 1\). At each moment in time a virus is a sequence ...
The first line of the input will contain three integers \(G\), \(N\) and \(M\) (\(G > 2\), \(N \geq G - 2\), \(M \geq 0\)) specifying the number of genes, the number of rows in the mutation table, and the number of antibodies.The following \(N\) lines contain descriptions of rows of the mutation table; each line begins...
Your program needs to output exactly \(G - 2\) lines, containing the answers for the subsequent genes from \(2\) to \(G - 1\).If all viruses that can mutate from this single gene can be detected by the given set of antibodies, you need to print the word ""YES"". You also need to print this if there are no viruses that ...
Input: 6 6 2 2 2 0 1 3 3 2 0 0 3 2 1 3 4 4 0 3 1 2 5 2 2 1 5 1 5 2 1 1 5 0 0 1 0 0 | Output: NO 2 NO 4 NO 9 YES
Master
4
2,237
992
624
13
788
A
788A
A. Functions again
1,600
dp; two pointers
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as...
The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a.The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements.
Print the only integer β€” the maximum value of f.
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].In the second case maximal value of f is reachable only on the whole array.
Input: 51 4 2 3 1 | Output: 3
Medium
2
647
172
48
7
1,630
B
1630B
B. Range and Partition
1,800
binary search; constructive algorithms; data structures; greedy; two pointers
Given an array \(a\) of \(n\) integers, find a range of values \([x, y]\) (\(x \le y\)), and split \(a\) into exactly \(k\) (\(1 \le k \le n\)) subarrays in such a way that: Each subarray is formed by several continuous elements of \(a\), that is, it is equal to \(a_l, a_{l+1}, \ldots, a_r\) for some \(l\) and \(r\) (\...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 3 \cdot 10^4\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β€” the length of the arr...
For each test case, print \(k+1\) lines.In the first line, print \(x\) and \(y\) β€” the limits of the found range.Then print \(k\) lines, the \(i\)-th should contain \(l_i\) and \(r_i\) (\(1\leq l_i \leq r_i \leq n\)) β€” the limits of the \(i\)-th subarray.You can print the subarrays in any order.
In the first test, there should be only one subarray, which must be equal to the whole array. There are \(2\) elements inside the range \([1, 2]\) and \(0\) elements outside, if the chosen range is \([1, 1]\), there will be \(1\) element inside (\(a_1\)) and \(1\) element outside (\(a_2\)), and the answer will be inval...
Input: 32 11 24 21 2 2 211 35 5 5 1 5 5 1 5 5 5 1 | Output: 1 2 1 2 2 2 1 3 4 4 5 5 1 1 2 2 3 11
Medium
5
691
630
296
16
809
B
809B
B. Glad to see you!
2,200
binary search; interactive
This is an interactive problem. In the output section below you will see the information about flushing the output.On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vičkopolis. Upon arrival, they left the car in a huge parking lot near the r...
There are two numbers n and k (2 ≀ k ≀ n ≀ 105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered.
If you want to provide an answer, output a string of the form 2 x y (1 ≀ x, y ≀ n, x β‰  y), if you think the dishes x and y was among dishes ordered by Noora. After that, flush the output and terminate your program.
There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (x = 1), then she'll choose the second dish (a = 2) as the dish with the minimum value . For the second (x = 2) and the third (x = 3) dishes themselves will be optimal, because ...
Input: 3 2NIETAKNIETAKTAKTAK | Output: 1 1 21 2 11 1 31 3 11 2 31 3 22 2 3
Hard
2
1,516
157
214
8
1,511
E
1511E
E. Colorings and Dominoes
2,100
combinatorics; dp; greedy; math
You have a large rectangular board which is divided into \(n \times m\) cells (the board has \(n\) rows and \(m\) columns). Each cell is either white or black.You paint each white cell either red or blue. Obviously, the number of different ways to paint them is \(2^w\), where \(w\) is the number of white cells.After pa...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\); \(nm \le 3 \cdot 10^5\)) β€” the number of rows and columns, respectively.Then \(n\) lines follow, each line contains a string of \(m\) characters. The \(j\)-th character in the \(i\)-th string is * if the \(j\)-th cell in the \(i\)-th...
Print one integer β€” the sum of values of the board over all \(2^w\) possible ways to paint it, taken modulo \(998\,244\,353\).
Input: 3 4 **oo oo*o **oo | Output: 144
Hard
4
985
366
126
15
1,753
E
1753E
E. N Machines
3,300
binary search; brute force; greedy
You have been invited as a production process optimization specialist to some very large company. The company has \(n\) machines at its factory, standing one behind another in the production chain. Each machine can be described in one of the following two ways: \((+,~a_i)\) or \((*,~a_i)\).If a workpiece with the value...
The first line contains four integers \(n\), \(b\), \(p\) and \(m\) (\(1 \le n \le 10^6\), \(1 \le b, p, m \le 10^9\)) β€” the number of machine at the factory, your budget and costs of movements of both kinds of machines.Each of the following \(n\) lines contains description of a machine. The description begins with one...
Print one integer β€” the maximum value of the resulting product that can be achieved if the total cost of movements that are made does not exceed \(b\) coins.
In the first example our budget is too low to move machine \((*,~2)\), but we can move both machines \((+,~1)\) to the beginning of the chain. So the final chain will be \((+,~1)\) \((+,~1)\) \((*,~2)\). If the workpiece with the value \(1\) is supplied to the first machine, its value will be changed in the following w...
Input: 3 2 1 3 * 2 + 1 + 1 | Output: 6
Master
3
1,654
566
157
17
1,367
B
1367B
B. Even Array
800
greedy; math
You are given an array \(a[0 \ldots n-1]\) of length \(n\) which consists of non-negative integers. Note that array indices start from zero.An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all \(i\) (\(0 \le i \le n - 1\)) the eq...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases in the test. Then \(t\) test cases follow.Each test case starts with a line containing an integer \(n\) (\(1 \le n \le 40\)) β€” the length of the array \(a\).The next line contains \(n\) integers \(a_0, a_1, \ldots, a_{n-1}\...
For each test case, output a single integer β€” the minimum number of moves to make the given array \(a\) good, or -1 if this is not possible.
In the first test case, in the first move, you can swap the elements with indices \(0\) and \(1\), and in the second move, you can swap the elements with indices \(2\) and \(3\).In the second test case, in the first move, you need to swap the elements with indices \(0\) and \(1\).In the third test case, you cannot make...
Input: 4 4 3 2 7 6 3 3 2 6 1 7 7 4 9 2 1 18 3 0 | Output: 2 1 -1 0
Beginner
2
900
367
140
13
550
E
550E
E. Brackets in Implications
2,200
constructive algorithms; greedy; implementation; math
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false. Implication is written by using character '', and the arguments and the result of the implication are written as '0' (false) and '1' (true). Accord...
The first line contains integer n (1 ≀ n ≀ 100 000) β€” the number of arguments in a logical expression.The second line contains n numbers a1, a2, ..., an (), which means the values of arguments in the expression in the order they occur.
Print ""NO"" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.Otherwise, print ""YES"" in the first line and the logical expression with the required arrangement of brackets in the second line.The expression should only contain characters '0', '1', '-' (char...
Input: 40 1 1 0 | Output: YES(((0)->1)->(1->0))
Hard
4
803
235
1,150
5
137
C
137C
C. History
1,500
sortings
Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.Everybody knows that the Worl...
The first input line contains integer n (1 ≀ n ≀ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 ≀ ai < bi ≀ 109) β€” the beginning and the end of the i-th event. No two events start or finish in ...
Print the only integer β€” the answer to the problem.
In the first example the fifth event is contained in the fourth. Similarly, the fourth event is contained in the third, the third β€” in the second and the second β€” in the first.In the second example all events except the first one are contained in the first.In the third example only one event, so the answer is 0.
Input: 51 102 93 84 75 6 | Output: 4
Medium
1
945
443
51
1
1,696
C
1696C
C. Fishingprince Plays With Array
1,400
constructive algorithms; greedy; implementation; math
Fishingprince is playing with an array \([a_1,a_2,\dots,a_n]\). He also has a magic number \(m\).He can do the following two operations on it: Select \(1\le i\le n\) such that \(a_i\) is divisible by \(m\) (that is, there exists an integer \(t\) such that \(m \cdot t = a_i\)). Replace \(a_i\) with \(m\) copies of \(\fr...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1\le n\le 5\cdot 10^4\), \(2\le m\le 10^9\)).The second line of each test case contains \...
For each testcase, print Yes if it is possible to turn \(a\) into \(b\), and No otherwise. You can print each letter in any case (upper or lower).
In the first test case of the sample, we can do the second operation with \(i=2\): \([1,\color{red}{2,2},4,2]\to [1,\color{red}{4},4,2]\).In the second testcase of the sample, we can: do the second operation with \(i=2\): \([1,\color{red}{2,2},8,2,2]\to [1,\color{red}{4},8,2,2]\). do the second operation with \(i=4\): ...
Input: 55 21 2 2 4 241 4 4 26 21 2 2 8 2 221 168 33 3 3 3 3 3 3 346 6 6 68 33 9 6 3 12 12 36 12169 3 2 2 2 3 4 12 4 12 4 12 4 12 4 48 33 9 6 3 12 12 36 12712 2 4 3 4 12 56 | Output: Yes Yes No Yes No
Easy
4
1,065
665
146
16
1,364
C
1364C
C. Ehab and Prefix MEXs
1,600
brute force; constructive algorithms; greedy
Given an array \(a\) of length \(n\), find another array, \(b\), of length \(n\) such that: for each \(i\) \((1 \le i \le n)\) \(MEX(\{b_1\), \(b_2\), \(\ldots\), \(b_i\})=a_i\). The \(MEX\) of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine ...
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) β€” the length of the array \(a\).The second line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(0 \le a_i \le i\)) β€” the elements of the array \(a\). It's guaranteed that \(a_i \le a_{i+1}\) for \(1\le i < n\).
If there's no such array, print a single line containing \(-1\).Otherwise, print a single line containing \(n\) integers \(b_1\), \(b_2\), \(\ldots\), \(b_n\) (\(0 \le b_i \le 10^6\))If there are multiple answers, print any.
In the second test case, other answers like \([1,1,1,0]\), for example, are valid.
Input: 3 1 2 3 | Output: 0 1 2
Medium
3
325
291
224
13
1,392
F
1392F
F. Omkar and Landslide
2,400
binary search; constructive algorithms; data structures; greedy; math
Omkar is standing at the foot of Celeste mountain. The summit is \(n\) meters away from him, and he can see all of the mountains up to the summit, so for all \(1 \leq j \leq n\) he knows that the height of the mountain at the point \(j\) meters away from himself is \(h_j\) meters. It turns out that for all \(j\) satisf...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^6\)). The second line contains \(n\) integers \(h_1, h_2, \dots, h_n\) satisfying \(0 \leq h_1 < h_2 < \dots < h_n \leq 10^{12}\) β€” the heights.
Output \(n\) integers, where the \(j\)-th integer is the value of \(h_j\) after the landslide has stopped.
Initially, the mountain has heights \(2, 6, 7, 8\).In the first minute, we have \(2 + 2 \leq 6\), so \(2\) increases to \(3\) and \(6\) decreases to \(5\), leaving \(3, 5, 7, 8\).In the second minute, we have \(3 + 2 \leq 5\) and \(5 + 2 \leq 7\), so \(3\) increases to \(4\), \(5\) is unchanged, and \(7\) decreases to ...
Input: 4 2 6 7 8 | Output: 5 5 6 7
Expert
5
1,438
211
106
13
460
D
460D
D. Little Victor and Set
2,300
brute force; constructive algorithms; math
Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties: for all x the following inequality holds l ≀ x ≀ r; 1 ≀ |S| ≀ k; lets denote the i-th element of the set S a...
The first line contains three space-separated integers l, r, k (1 ≀ l ≀ r ≀ 1012; 1 ≀ k ≀ min(106, r - l + 1)).
Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order.If there are multiple optimal sets, you can print any of them.
Operation represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
Input: 8 15 3 | Output: 1210 11
Expert
3
397
111
191
4
929
C
929C
C. ΠšΡ€Π°ΡΠΈΠ²Π°Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π°
1,700
*special; combinatorics; math
Π—Π°Π²Ρ‚Ρ€Π° Ρƒ Ρ…ΠΎΠΊΠΊΠ΅ΠΉΠ½ΠΎΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ€ΡƒΠΊΠΎΠ²ΠΎΠ΄ΠΈΡ‚ Π•Π²Π³Π΅Π½ΠΈΠΉ, Π²Π°ΠΆΠ½Ρ‹ΠΉ ΠΌΠ°Ρ‚Ρ‡. Π•Π²Π³Π΅Π½ΠΈΡŽ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ ΡˆΠ΅ΡΡ‚ΡŒ ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹ΠΉΠ΄ΡƒΡ‚ Π½Π° Π»Π΅Π΄ Π² стартовом составС: ΠΎΠ΄ΠΈΠ½ Π²Ρ€Π°Ρ‚Π°Ρ€ΡŒ, Π΄Π²Π° Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠ° ΠΈ Ρ‚Ρ€ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ….Π’Π°ΠΊ ΠΊΠ°ΠΊ это стартовый состав, ЕвгСния большС Π²ΠΎΠ»Π½ΡƒΠ΅Ρ‚, насколько красива Π±ΡƒΠ΄Π΅Ρ‚ ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π° Π»ΡŒΠ΄Ρƒ, Ρ‡Π΅ΠΌ способности ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ². А ΠΈΠΌΠ΅Π½Π½...
ΠŸΠ΅Ρ€Π²Π°Ρ строка содСрТит Ρ‚Ρ€ΠΈ Ρ†Π΅Π»Ρ‹Ρ… числа g, d ΠΈ f (1 ≀ g ≀ 1 000, 1 ≀ d ≀ 1 000, 1 ≀ f ≀ 1 000) β€” число Π²Ρ€Π°Ρ‚Π°Ρ€Π΅ΠΉ, Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠΎΠ² ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ… Π² ΠΊΠΎΠΌΠ°Π½Π΄Π΅ ЕвгСния. Вторая строка содСрТит g Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡ€Π΅Π΄Π΅Π»Π°Ρ… ΠΎΡ‚ 1 Π΄ΠΎ 100 000 β€” Π½ΠΎΠΌΠ΅Ρ€Π° Π²Ρ€Π°Ρ‚Π°Ρ€Π΅ΠΉ.Π’Ρ€Π΅Ρ‚ΡŒΡ строка содСрТит d Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡ€Π΅Π΄Π΅Π»Π°Ρ… ΠΎΡ‚ 1 Π΄ΠΎ 100 000 β€” Π½...
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число β€” количСство Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… стартовых составов.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ всСго ΠΎΠ΄ΠΈΠ½ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ для Π²Ρ‹Π±ΠΎΡ€Π° состава, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ удовлСтворяСт описанным условиям, поэтому ΠΎΡ‚Π²Π΅Ρ‚ 1.Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ подходят ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΈΠ³Ρ€ΠΎΠ²Ρ‹Π΅ сочСтания (Π² порядкС Π²Ρ€Π°Ρ‚Π°Ρ€ΡŒ-Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊ-Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ): 16 20 12 13 21 11 16 20 12 13 11 10 16 20 19 13 21 11 16 20 19 13 11 10 16 12 1...
Input: 1 2 31510 1920 11 13 | Output: 1
Medium
3
953
562
68
9
750
B
750B
B. New Year and North Pole
1,300
geometry; implementation
In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20 000 kilometers.Limak, a polar bear, lives on the Nor...
The first line of the input contains a single integer n (1 ≀ n ≀ 50).The i-th of next n lines contains an integer ti and a string diri (1 ≀ ti ≀ 106, ) β€” the length and the direction of the i-th part of the journey, according to the description Limak got.
Print ""YES"" if the description satisfies the three conditions, otherwise print ""NO"", both without the quotes.
Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is ""NO"" because he doesn't end on the North Pole.
Input: 57500 South10000 East3500 North4444 West4000 North | Output: YES
Easy
2
1,285
255
113
7
611
E
611E
E. New Year and Three Musketeers
2,400
data structures; greedy; sortings
Do you know the story about the three musketeers? Anyway, you must help them now.Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength a, Borthos strength b, and Caramis has strength c.The year 2015 is almost over and there are still n ...
The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of criminals.The second line contains three integers a, b and c (1 ≀ a, b, c ≀ 108) β€” strengths of musketeers.The third line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 108) β€” strengths of criminals.
Print one line with the answer.If it's impossible to defeat all criminals, print ""-1"" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals.
In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours: Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1. There are three criminals left, each with strength 1. Each mus...
Input: 510 20 301 1 1 1 50 | Output: 2
Expert
3
2,025
288
214
6
1,270
A
1270A
A. Card Game
800
games; greedy; math
Two players decided to play one interesting card game.There is a deck of \(n\) cards, with values from \(1\) to \(n\). The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each pl...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(k_1\), \(k_2\) (\(2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n\)) ...
For each test case, output ""YES"" in a separate line, if the first player wins. Otherwise, output ""NO"" in a separate line. You can print each letter in any case (upper or lower).
In the first test case of the example, there is only one possible move for every player: the first player will put \(2\), the second player will put \(1\). \(2>1\), so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning...
Input: 2 2 1 1 2 1 5 2 3 2 3 1 4 5 | Output: YES NO
Beginner
3
2,045
775
181
12
1,287
B
1287B
B. Hyperset
1,500
brute force; data structures; implementation
Bees Alice and Alesya gave beekeeper Polina famous card game ""Set"" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For eve...
The first line of each test contains two integers \(n\) and \(k\) (\(1 \le n \le 1500\), \(1 \le k \le 30\)) β€” number of cards and number of features.Each of the following \(n\) lines contains a card description: a string consisting of \(k\) letters ""S"", ""E"", ""T"". The \(i\)-th character of this string decribes th...
Output a single integer β€” the number of ways to choose three cards that form a set.
In the third example test, these two triples of cards are sets: ""SETT"", ""TEST"", ""EEET"" ""TEST"", ""ESTE"", ""STES""
Input: 3 3 SET ETS TSE | Output: 1
Medium
3
1,122
376
83
12
1,286
D
1286D
D. LCC
3,100
data structures; math; matrices; probabilities
An infinitely long Line Chillland Collider (LCC) was built in Chillland. There are \(n\) pipes with coordinates \(x_i\) that are connected to LCC. When the experiment starts at time 0, \(i\)-th proton flies from the \(i\)-th pipe with speed \(v_i\). It flies to the right with probability \(p_i\) and flies to the left w...
The first line of input contains one integer \(n\) β€” the number of pipes (\(1 \le n \le 10^5\)). Each of the following \(n\) lines contains three integers \(x_i\), \(v_i\), \(p_i\) β€” the coordinate of the \(i\)-th pipe, the speed of the \(i\)-th proton and the probability that the \(i\)-th proton flies to the right in ...
It's possible to prove that the answer can always be represented as a fraction \(P/Q\), where \(P\) is an integer and \(Q\) is a natural number not divisible by \(998\,244\,353\). In this case, print \(P \cdot Q^{-1}\) modulo \(998\,244\,353\).
Input: 2 1 1 100 3 1 0 | Output: 1
Master
4
630
483
244
12
840
D
840D
D. Destiny
2,500
data structures; probabilities
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q queries of the form l r k. If there are queries, then they must be answered. Answer for the query is minimal x such that x occurs in the interval l r strictly more than times or - 1 if there is no such number. Help Leha wit...
First line of input data contains two integers n and q (1 ≀ n, q ≀ 3Β·105) β€” number of elements in the array and number of queries respectively.Next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” Leha's array.Each of next q lines contains three integers l, r and k (1 ≀ l ≀ r ≀ n, 2 ≀ k ≀ 5) β€” description of the...
Output answer for each query in new line.
Input: 4 21 1 2 21 3 21 4 2 | Output: 1-1
Expert
2
344
329
41
8
1,672
E
1672E
E. notepad.exe
2,200
binary search; constructive algorithms; greedy; interactive
This is an interactive problem.There are \(n\) words in a text editor. The \(i\)-th word has length \(l_i\) (\(1 \leq l_i \leq 2000\)). The array \(l\) is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not h...
The first and only line of input contains a single integer \(n\) (\(1 \leq n \leq 2000\)) β€” the number of words on the text editor.It is guaranteed that the hidden lengths \(l_i\) satisfy \(1 \leq l_i \leq 2000\).
In the first test case, the words are \(\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}\), so \(l=\{5,2,7,3,5,6\}\). If \(w=1\), then the text editor is not able to display all words properly and will crash. The height of the text editor is \(h_1=0\), so the grader will retur...
Input: 6 0 4 2 | Output: ? 1 ? 9 ? 16 ! 32
Hard
4
1,379
213
0
16
1,690
C
1690C
C. Restoring the Duration of Tasks
800
data structures; greedy; implementation
Recently, Polycarp completed \(n\) successive tasks.For each completed task, the time \(s_i\) is known when it was given, no two tasks were given at the same time. Also given is the time \(f_i\) when the task was completed. For each task, there is an unknown value \(d_i\) (\(d_i>0\)) β€” duration of task execution.It is ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The descriptions of the input data sets follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains exactly \(n\) integers \(s_1 < s_2 < \dots...
For each of \(t\) test cases print \(n\) positive integers \(d_1, d_2, \dots, d_n\) β€” the duration of each task.
First test case:The queue is empty at the beginning: \([ ]\). And that's where the first task comes in. At time \(2\), Polycarp finishes doing the first task, so the duration of the first task is \(2\). The queue is empty so Polycarp is just waiting.At time \(3\), the second task arrives. And at time \(7\), the third t...
Input: 430 3 72 10 11210 1511 16912 16 90 195 1456 1569 3001 5237 1927513 199 200 260 9100 10000 10914 91066 5735533101000000000 | Output: 2 7 1 1 1 1 183 1 60 7644 900 914 80152 5644467 1000000000
Beginner
3
852
564
112
16
1,981
E
1981E
E. Turtle and Intersected Segments
2,600
data structures; dsu; graphs; greedy
Turtle just received \(n\) segments and a sequence \(a_1, a_2, \ldots, a_n\). The \(i\)-th segment is \([l_i, r_i]\).Turtle will create an undirected graph \(G\). If segment \(i\) and segment \(j\) intersect, then Turtle will add an undirected edge between \(i\) and \(j\) with a weight of \(|a_i - a_j|\), for every \(i...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β€” the number of segments.The \(i\)-th of the following \(n\) li...
For each test case, output a single integer β€” the sum of the weights of the edges of the minimum spanning tree of the graph \(G\). If the graph \(G\) has no spanning tree, output \(-1\).
In the first test case, the graph \(G\) is as follows: One of the minimum spanning trees of \(G\) is as follows: The sum of the weights of the edges of the minimum spanning tree is \(9\).In the second test case, the graph \(G\) is as follows: \(G\) is already a tree, and the sum of the weights of the tree is \(13\).In ...
Input: 451 7 32 4 63 5 56 7 93 4 452 7 31 3 64 5 56 7 91 1 441 4 31 2 13 4 51 4 431 3 12 3 34 5 8 | Output: 9 13 4 -1
Expert
4
613
575
186
19
1,242
D
1242D
D. Number Discovery
3,400
math
Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers \(n\) and \(k\). He creates an infinite sequence \(s\) by repeating the following steps. Find \(k\) smallest distinct positive integers that are not in \(s\). Let's call them \(u_{1}, u_{2}, \ldots, u_{k}\) from the s...
The first line contains a single integer \(t\) (\(1 \le t \le 10^{5}\)), the number of test cases.Each of the following \(t\) lines contains two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(2 \le k \le 10^{6}\)), the number to be found in the sequence \(s\) and the parameter used to create the sequence \(s\).
In each of the \(t\) lines, output the answer for the corresponding test case.
In the first sample, \(s = (1, 2, 3, 4, 5, 9, 6, 7, 13, 8, 10, 18, \ldots)\). \(10\) is the \(11\)-th number here, so the answer is \(11\).In the second sample, \(s = (1, 2, 3, 4, 5, 15, 6, 7, 8, 9, 10, 40, \ldots)\).
Input: 2 10 2 40 5 | Output: 11 12
Master
1
738
318
78
12
2,130
A
2130A
A. Submission is All You Need
800
greedy; math
For a multiset \(T\) consisting of non-negative integers, we define: \(\text{sum}(T)\) is the sum of all elements in \(T\). For example, if \(T = \{0,1, 1, 3\}\), then \(\text{sum}(T)= 0+1+1+3=5\). \(\text{mex}(T)\) is the smallest non-negative integer not in \(T\). For example, if \(T = \{0,1, 1, 3\}\), then \(\text{m...
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 single integer \(n\) (\(1 \le n \le 50\)).The second line of each test case contains \(n\) integers \(S_1, S_2, \l...
For each test case, print a single integer β€” the maximum possible score that can be obtained.
In the first test case, a possible optimal strategy is as follows: Select \(S'=\{0,1\}\), add \(\text{mex}(S')=\text{mex}(\{0,1\})=2\) to your score, and then remove \(S'\) from \(S\). Currently, your score is \(2\) and \(S=\{1\}\). Select \(S'=\{1\}\), add \(\text{sum}(S')=\text{sum}(\{1\})=1\) to your score, and then...
Input: 230 1 131 2 3 | Output: 3 6
Beginner
2
945
355
93
21
1,215
E
1215E
E. Marbles
2,200
bitmasks; dp
Monocarp has arranged \(n\) colored marbles in a row. The color of the \(i\)-th marble is \(a_i\). Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). In other words, Monocarp wants...
The first line contains one integer \(n\) \((2 \le n \le 4 \cdot 10^5)\) β€” the number of marbles.The second line contains an integer sequence \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 20)\), where \(a_i\) is the color of the \(i\)-th marble.
Print the minimum number of operations Monocarp has to perform to achieve his goal.
In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is \([3, 4, 3, 2, 4, 2, 2]\). Then Monocarp should swap the second and the third marbles, so the sequence is \([3, 3, 4, 2, 4, 2, 2]\). And finally, Monocarp should swap the fourth...
Input: 7 3 4 2 3 4 2 2 | Output: 3
Hard
2
982
241
83
12
1,906
E
1906E
E. Merge Not Sort
1,900
constructive algorithms; dp
You are currently researching the Merge Sort algorithm. Merge Sort is a sorting algorithm that is based on the principle of Divide and Conquer. It works by dividing an array into two subarrays of equal length, sorting each subarrays, then merging the sorted subarrays back together to form the final sorted array.You are...
The first line consists of an integer \(N\) (\(1 \leq N \leq 1000\)).The following line consists of \(2 \cdot N\) integers \(C_i\). The array \(C\) is a permutation of \(1\) to \(2 \cdot N\).
If it is impossible to construct two arrays \(A\) and \(B\) of length \(N\) such that \(\text{Merge}(A, B) = C\), then output -1.Otherwise, output the arrays \(A\) and \(B\) in two lines. The first line consists of \(N\) integers \(A_i\). The second line consists of \(N\) integers \(B_i\). If there are several possible...
Explanation for the sample input/output #1The solution \(A = [3, 1, 4]\) and \(B = [5, 2, 6]\) is also correct. Explanation for the sample input/output #2The solution \(A = [1, 2, 3, 4]\) and \(B = [5, 6, 7, 8]\) is also correct.
Input: 3 3 1 4 5 2 6 | Output: 3 1 6 4 5 2
Hard
2
1,449
191
349
19
1,672
I
1672I
I. PermutationForces
3,000
data structures; greedy
You have a permutation \(p\) of integers from \(1\) to \(n\).You have a strength of \(s\) and will perform the following operation some times: Choose an index \(i\) such that \(1 \leq i \leq |p|\) and \(|i-p_i| \leq s\). For all \(j\) such that \(1 \leq j \leq |p|\) and \(p_i<p_j\), update \(p_j\) to \(p_j-1\). Delete ...
The first line of input contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)) β€” the length of the permutation \(p\).The second line of input conatains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β€” the elements of the permutation \(p\).It is guaranteed that all elements in \(p\) are disti...
Print the minimum strength \(s\) required.
In the first test case, the minimum \(s\) required is \(1\).Here is how we can transform \(p\) into the empty permutation with \(s=1\): In the first move, you can only choose \(i=2\) as choosing any other value of \(i\) will result in \(|i-p_i| \leq s\) being false. With \(i=2\), \(p\) will be changed to \([2,1]\). In ...
Input: 3 3 2 1 | Output: 1
Master
2
694
324
42
16
1,907
E
1907E
E. Good Triples
1,600
brute force; combinatorics; number theory
Given a non-negative integer number \(n\) (\(n \ge 0\)). Let's say a triple of non-negative integers \((a, b, c)\) is good if \(a + b + c = n\), and \(digsum(a) + digsum(b) + digsum(c) = digsum(n)\), where \(digsum(x)\) is the sum of digits of number \(x\).For example, if \(n = 26\), then the pair \((4, 12, 10)\) is go...
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Descriptions of test cases follow.The first and only line of the test case contains one integer \(n\) (\(0 \le n \le 10^7\)).
For each test case output one integer, the number of good triples for the given integer \(n\). Order of integers in a triple matters.
In the first example, the good triples are \((0, 0, 11)\), \((0, 1, 10)\), \((0, 10, 1)\), \((0, 11, 0)\), \((1, 0, 10)\), \((1, 10, 0)\), \((10, 0, 1)\), \((10, 1, 0)\), \((11, 0, 0)\).In the second example, there is only one good triple \((0, 0, 0)\).
Input: 121101234531419992718999999910000000 | Output: 9 1 3 6 10 15 21 1350 166375 29160 1522435234375 3
Medium
3
605
232
133
19
268
B
268B
B. Buttons
1,000
implementation; math
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you've guessed correctly and pushed the button that goes next in the sequenc...
A single line contains integer n (1 ≀ n ≀ 2000) β€” the number of buttons the lock has.
In a single line print the number of times Manao has to push a button in the worst-case scenario.
Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes.
Input: 2 | Output: 3
Beginner
2
1,059
85
97
2
2,095
I
2095I
I. Mysterious Script
0
*special; expression parsing; number theory
An extraterrestrial species has visited the authors of April Fools Day Contest 2025 while they were preparing problems. They call themselves the ""Balikons"" and expressed interest in chatting with humans. Although they can make sounds that are similar to human speech, they seem to prefer communicating by writing using...
The input contains two integers \(a\) and \(b\) (\(1 \le a, b \le 10^9\)).
Print the sum \(a + b\).
Input: shons tes | Output: leshas
Beginner
3
1,307
74
24
20
786
E
786E
E. ALT
3,200
data structures; flows; graphs; trees
ALT is a planet in a galaxy called ""Encore"". Humans rule this planet but for some reason there's no dog in their planet, so the people there are sad and depressed. Rick and Morty are universal philanthropists and they want to make people in ALT happy. ALT has n cities numbered from 1 to n and n - 1 bidirectional road...
The first line of input contains two integers n and m (2 ≀ n ≀ 2 Γ— 104, 1 ≀ m ≀ 104) β€” number of cities and number of citizens respectively.The next n - 1 lines contain the roads, i-th line contains endpoint of i-th edge, v and u (1 ≀ v, u ≀ n, v β‰  u).The next m lines contain the information about citizens. i-th line c...
In the first line of input print a single integer k, the total number of puppies they need (1 ≀ k ≀ n).In the second line print an integer q, the number of puppies to give to citizens, followed by q distinct integers a1, a2, ..., aq, index of citizens to give puppy to (0 ≀ q ≀ min(m, k), 1 ≀ ai ≀ m).In the third line p...
Map of ALT in the first sample testcase (numbers written on a road is its index): Map of ALT in the second sample testcase (numbers written on a road is its index):
Input: 4 52 43 41 42 42 12 41 22 3 | Output: 31 5 2 3 1
Master
4
1,404
377
552
7
234
H
234H
H. Merging Two Decks
2,000
constructive algorithms; greedy
There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages.The first stage is to merge the two decks in such a way that the relative order of...
The first input line contains a single integer n β€” the number of cards in the first deck (1 ≀ n ≀ 105).The second input line contains n integers, separated by single spaces a1, a2, ..., an (0 ≀ ai ≀ 1). Value ai equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in t...
In the first line print n + m space-separated integers β€” the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to n in the order from top to bottom. The cards from the second deck should mat...
Input: 31 0 141 1 1 1 | Output: 1 4 5 6 7 2 335 6 7
Hard
2
1,156
728
943
2
380
A
380A
A. Sereja and Prefixes
1,600
binary search; brute force
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current seque...
The first line contains integer m (1 ≀ m ≀ 105) β€” the number of stages to build a sequence. Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains int...
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
Input: 61 11 22 2 11 32 5 21 4161 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Output: 1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
Medium
2
724
1,310
104
3
914
H
914H
H. Ember and Storm's Tree Game
3,400
combinatorics; dp; games; trees
Ember and Storm play a game. First, Ember picks a labelled tree T of n vertices, such that the degree of every vertex is at most d. Then, Storm picks two distinct vertices u and v in this tree and writes down the labels of the vertices in the path from u to v in a sequence a1, a2... ak. Finally, Ember picks any index i...
The input consists of a single line containing three integers n, d and m (2 ≀ n ≀ 200, 1 ≀ d < n, 1 ≀ m ≀ 2Β·109).
Print a single number β€” the number of possible tuples if Ember and Storm play as described, modulo m.
In the first sample case, there is only one possible tree. There are two possible paths, 1 to 2 and 2 to 1. For both paths, i can only be 1, and op can take both possibilities. Therefore, the answer is 4.In the second sample, there are no possible trees.In the third sample, there are three possible trees.
Input: 2 1 1000000007 | Output: 4
Master
4
1,250
113
101
9
1,331
E
1331E
E. Jordan Smiley
0
*special; dfs and similar; geometry; implementation
The input contains two integers \(row\), \(col\) (\(0 \le row, col \le 63\)), separated by a single space.
Output ""IN"" or ""OUT"".
Input: 0 0 | Output: OUT
Beginner
4
0
106
25
13
1,023
C
1023C
C. Bracket Subsequence
1,200
greedy
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 ""()()"" and ""...
The first line contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\), both \(n\) and \(k\) are even) β€” the length of \(s\) and the length of the sequence you are asked to find.The second line is a string \(s\) β€” regular bracket sequence of length \(n\).
Print a single string β€” a regular bracket sequence of length exactly \(k\) such that it is also a subsequence of \(s\).It is guaranteed that such sequence always exists.
Input: 6 4()(()) | Output: ()()
Easy
1
828
273
169
10