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
238
C
238C
C. World Eater Brothers
2,100
dfs and similar; dp; greedy; trees
You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world. As you know there are n countries in the world. These countries are connected by n - 1 directed roads. If you don't consider directi...
The first line of input contains an integer n (1 ≀ n ≀ 3000). Each of the next n - 1 lines contains two space-separated integers ai and bi (1 ≀ ai, bi ≀ n; ai β‰  bi) saying there is a road from country ai to country bi.Consider that countries are numbered from 1 to n. It's guaranteed that if you don't consider direction...
In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world.
Input: 42 13 14 1 | Output: 1
Hard
4
979
442
152
2
1,868
D
1868D
D. Flower-like Pseudotree
3,000
constructive algorithms; graphs; greedy; implementation; trees
A pseudotree is a connected graph which has exactly one cycle and no self-loops. Note that a pseudotree may contain multiple-edges. It can be shown that a pseudotree with \(n\) vertices always contains \(n\) edges.After deleting all edges on the cycle in the pseudotree, a forest\(^{\dagger}\) will be formed. It can be ...
The first line of the input contains a single integer \(t\) (\(1\leq t\leq 10^5\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 10^6\)) β€” the number of vertices.The second line of each test case contains \(n\) integers ...
For each test case, if there exist a possible flower-like pseudotree: Print ""Yes"" (without quotes) in the first line. Then, output \(n\) lines, in each line print two integers \(u_i\) and \(v_i\) β€” the two vertices that the \(i\)-th edge connects. If there are multiple answers, you may output any of them.Otherwise, p...
In the first test case, the only possible flower-like psuedotree is: After deleting all edges on the cycle in the pseudotree, each tree has depth \(0\).In the second test case, it can be proven that there's no such flower-like psuedotree.In the third test case, one of the possible flower-like psuedotrees is:
Input: 632 2 241 2 3 474 3 3 1 1 1 161 1 2 2 3 3101 1 5 2 1 1 1 1 1 691 1 3 1 1 4 1 1 5 | Output: Yes 1 2 2 3 3 1 No Yes 1 2 2 3 3 1 1 4 1 5 2 6 3 7 Yes 5 6 6 5 1 3 2 4 3 5 4 6 No Yes 3 6 6 9 9 3 1 3 2 6 4 6 5 9 7 9 8 9
Master
5
1,279
477
559
18
963
C
963C
C. Cutting Rectangle
2,600
brute force; math; number theory
A rectangle with sides \(A\) and \(B\) is cut into rectangles with cuts parallel to its sides. For example, if \(p\) horizontal and \(q\) vertical cuts were made, \((p + 1) \cdot (q + 1)\) rectangles were left after the cutting. After the cutting, rectangles were of \(n\) different types. Two rectangles are different i...
The first line consists of a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^{5}\)) β€” amount of different types of rectangles left after cutting the initial rectangle.The next \(n\) lines each consist of three integers \(w_{i}, h_{i}, c_{i}\) \((1 \leq w_{i}, h_{i}, c_{i} \leq 10^{12})\) β€” the lengths of the sides of ...
Output one integer β€” the answer to the problem.
In the first sample there are three suitable pairs: \((1; 9)\), \((3; 3)\) and \((9; 1)\).In the second sample case there are 6 suitable pairs: \((2; 220)\), \((4; 110)\), \((8; 55)\), \((10; 44)\), \((20; 22)\) and \((40; 11)\).Here the sample of cut for \((20; 22)\). The third sample has no suitable pairs.
Input: 11 1 9 | Output: 3
Expert
3
967
468
47
9
414
D
414D
D. Mashmokh and Water Tanks
2,300
binary search; data structures; greedy; trees; two pointers
Mashmokh is playing a new game. In the beginning he has k liters of water and p coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of m vertices. Each vertex of the tree contains a water tank that is empty in the beginning.The game begins with the fact that Mashmokh chooses s...
The first line of the input contains three space-separated integers m, k, p (2 ≀ m ≀ 105; 0 ≀ k, p ≀ 109). Each of the following m - 1 lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ m; ai β‰  bi) β€” the edges of the tree.Consider that the vertices of the tree are numbered from 1 to m. The root of the tre...
Output a single integer, the number Mashmokh asked you to find.
The tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water.One way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below.Then in the first move Mash...
Input: 10 2 11 21 33 43 52 66 86 79 88 10 | Output: 2
Expert
5
1,650
335
63
4
1,725
E
1725E
E. Electrical Efficiency
2,500
combinatorics; data structures; dp; math; number theory; trees
In the country of Dengkleknesia, there are \(N\) factories numbered from \(1\) to \(N\). Factory \(i\) has an electrical coefficient of \(A_i\). There are also \(N-1\) power lines with the \(j\)-th power line connecting factory \(U_j\) and factory \(V_j\). It can be guaranteed that each factory in Dengkleknesia is conn...
The first line contains a single integer \(N\) (\(1 \le N \le 2 \cdot 10^5\)) β€” the number of factories in Dengkleknesia.The second line contains \(N\) integers \(A_1, A_2, \dots, A_N\) (\(1 \leq A_i \leq 2 \cdot 10^5\)) β€” the electrical coefficients of the factories.The \(j\)-th of the next \(N-1\) lines contains two ...
An integer representing the sum of \(f(x, y, z) \times g(x, y, z)\) for all combinations of \((x, y, z)\) such that \(1 \leq x < y < z \leq N\), modulo \(998\,244\,353\)
In the first example, the only \((x, y, z)\) possible is \((1, 2, 3)\). Because \(\text{GCD}(A_1, A_2, A_3) = \text{GCD}(1, 2, 3) = 1\) has \(0\) distinct prime factors, therefore \(f(x, y, z) \times g(x, y, z) = 2 \times 0 = 0\).In the second example, all triples \((x, y, z)\) that satisfy the condition are as follows...
Input: 3 1 2 3 1 2 2 3 | Output: 0
Expert
6
1,412
474
169
17
25
E
25E
E. Test
2,200
hashing; strings
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings β€” input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop i...
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output one number β€” what is minimal length of the string, containing s1, s2 and s3 as substrings.
Input: abbccd | Output: 4
Hard
2
585
192
97
0
990
D
990D
D. Graph And Its Complement
1,700
constructive algorithms; graphs; implementation
Given three numbers \(n, a, b\). You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to \(a\), and the number of components in its complement is \(b\). The matrix must be symmetric, and all digits on the main diagonal must be zeroes.In an undirected graph loops ...
In a single line, three numbers are given \(n, a, b \,(1 \le n \le 1000, 1 \le a, b \le n)\): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.
If there is no graph that satisfies these constraints on a single line, print ""NO"" (without quotes).Otherwise, on the first line, print ""YES""(without quotes). In each of the next \(n\) lines, output \(n\) digits such that \(j\)-th digit of \(i\)-th line must be \(1\) if and only if there is an edge between vertices...
Input: 3 1 2 | Output: YES001001110
Medium
3
1,237
261
540
9
1,140
G
1140G
G. Double Tree
2,700
data structures; divide and conquer; shortest paths; trees
You are given a special undirected graph. It consists of \(2n\) vertices numbered from \(1\) to \(2n\). The following properties hold for the graph: there are exactly \(3n-2\) edges in the graph: \(n\) edges connect vertices having odd numbers with vertices having even numbers, \(n - 1\) edges connect vertices having o...
The first line of the input contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(w_{1, 2}\), \(w_{3,4}\), ..., \(w_{2n - 1, 2n}\) (\(1 \le w_{i, i + 1} \le 10^{12}\)). These integers describe the weights of the edges connecting odd vertices with even ones.Then \(n-1\) line...
Print \(q\) integers, \(i\)-th integer should be equal to the answer to the \(i\)-th query.
The graph in the first test looks like that:
Input: 5 3 6 15 4 8 1 2 5 4 2 3 5 7 1 4 1 5 1 5 2 1 3 1 2 5 6 1 10 | Output: 3 15 4
Master
4
1,310
990
91
11
699
A
699A
A. Launch of Collider
1,000
implementation
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the dista...
The first line contains the positive integer n (1 ≀ n ≀ 200 000) β€” the number of particles. The second line contains n symbols ""L"" and ""R"". If the i-th symbol equals ""L"", then the i-th particle will move to the left, otherwise the i-th symbol equals ""R"" and the i-th particle will move to the right.The third lin...
In the first line print the only integer β€” the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen.
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
Input: 4RLRL2 4 6 10 | Output: 1
Beginner
1
1,152
567
223
6
1,213
E
1213E
E. Two Small Strings
1,900
brute force; constructive algorithms
You are given two strings \(s\) and \(t\) both of length \(2\) and both consisting only of characters 'a', 'b' and 'c'.Possible examples of strings \(s\) and \(t\): ""ab"", ""ca"", ""bb"".You have to find a string \(res\) consisting of \(3n\) characters, \(n\) characters should be 'a', \(n\) characters should be 'b' an...
The first line of the input contains one integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of characters 'a', 'b' and 'c' in the resulting string.The second line of the input contains one string \(s\) of length \(2\) consisting of characters 'a', 'b' and 'c'.The third line of the input contains one string \(t\) of leng...
If it is impossible to find the suitable string, print ""NO"" on the first line. Otherwise print ""YES"" on the first line and string \(res\) on the second line. \(res\) should consist of \(3n\) characters, \(n\) characters should be 'a', \(n\) characters should be 'b' and \(n\) characters should be 'c' and \(s\) and \...
Input: 2 ab bc | Output: YES acbbac
Hard
2
707
371
424
12
1,928
D
1928D
D. Lonely Mountain Dungeons
1,900
brute force; data structures; greedy; math; ternary search
Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.The army of Middle-earth inhabitants will ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(b\), and \(x\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le b \le 10^...
For each test case, output a single integer β€” the maximum strength of the army that the inhabitants of Middle-earth can assemble.
In the first test case, the inhabitants of Middle-earth can form \(3\) squads. Since \(x = 0\), the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows: The single representative of the first species can be sent to the first squad. The first repr...
Input: 53 1 01 2 33 5 102 5 34 3 33 2 1 24 1 04 1 4 24 1 104 1 4 2 | Output: 4 40 9 13 0
Hard
5
966
711
129
19
1,608
G
1608G
G. Alphabetic Tree
3,500
binary search; data structures; dfs and similar; hashing; string suffix structures; strings; trees
You are given \(m\) strings and a tree on \(n\) nodes. Each edge has some letter written on it.You have to answer \(q\) queries. Each query is described by \(4\) integers \(u\), \(v\), \(l\) and \(r\). The answer to the query is the total number of occurrences of \(str(u,v)\) in strings with indices from \(l\) to \(r\)...
The first line of the input contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 10^5\), \(1 \le m,q \le 10^5\)).The \(i\)-th of the following \(n-1\) lines contains two integers \(u_i, v_i\) and a lowercase Latin letter \(c_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\)), denoting the edge between nodes \(u_...
For each query print a single integer β€” the answer to the query.
Input: 2 5 3 1 2 a aab abab aaa b a 2 1 1 5 1 2 1 3 2 1 3 5 | Output: 8 7 4
Master
7
495
718
64
16
2,110
D
2110D
D. Fewer Batteries
1,700
binary search; dfs and similar; dp; graphs; greedy; hashing
In 2077, when robots took over the world, they decided to compete in the following game.There are \(n\) checkpoints, and the \(i\)-th checkpoint contains \(b_i\) batteries. Initially, the Robot starts at the \(1\)-st checkpoint with no batteries and must reach the \(n\)-th checkpoint.There are a total of \(m\) one-way ...
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, m\) (\(2 \leq n \leq 2 \cdot 10^5, 0 \leq m \leq 3 \cdot 10^5\)) β€” the number of checkpoints and t...
For each test case, output the minimum number of batteries that you can have at the end of the journey, or \(-1\) if it is impossible to reach point \(n\).
In the first test case, you need to take \(1\) battery at the starting point, then move to point \(2\), and then to point \(3\).In the second test case, you need to take \(2\) batteries at the starting point, then move to point \(2\), take another \(2\) batteries, move to point \(4\), and then to point \(5\).In the thi...
Input: 43 32 0 01 2 12 3 11 3 25 62 2 5 0 11 2 21 3 11 4 33 5 52 4 44 5 32 01 14 43 10 0 01 2 11 3 32 3 103 4 5 | Output: 1 4 -1 10
Medium
6
1,044
836
155
21
1,210
G
1210G
G. Mateusz and Escape Room
3,500
dp
Mateusz likes to travel! However, on his \(42\)nd visit to Saint Computersburg there is not much left to sightsee. That's why he decided to go to an escape room with his friends!The team has solved all riddles flawlessly. There is only one riddle remaining β€” a huge circular table! There are \(n\) weighing scales lying ...
The first line contains an integer \(n\) (\(3 \le n \le 35\,000\)) β€” the number of weighing scales in the circle.The following \(n\) lines describe the scales. The \(i\)-th of these lines describes the \(i\)-th scale and consists of three integers \(a_i, l_i, r_i\) (\(0 \le a_i \le 35\,000\), \(0 \le l_i \le r_i \le 35...
Output one integer β€” the minimum number of operations required to solve the riddle.
Input: 5 0 2 3 1 2 3 4 3 3 4 3 3 4 3 3 | Output: 4
Master
1
1,307
446
83
12
468
A
468A
A. 24 Game
1,500
constructive algorithms; greedy; math
Little X used to play a card game called ""24 Game"", but recently he has found it too easy. So he invented a new game.Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, ...
The first line contains a single integer n (1 ≀ n ≀ 105).
If it's possible, print ""YES"" in the first line. Otherwise, print ""NO"" (without the quotes).If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: ""a op b = c"". Where a and b are the numbers you've pi...
Input: 1 | Output: NO
Medium
3
425
57
676
4
23
D
23D
D. Tetragon
2,600
geometry; math
You're given the centers of three equal sides of a strictly convex tetragon. Your task is to restore the initial tetragon.
The first input line contains one number T β€” amount of tests (1 ≀ T ≀ 5Β·104). Each of the following T lines contains numbers x1, y1, x2, y2, x3, y3 β€” coordinates of different points that are the centers of three equal sides (non-negative integer numbers, not exceeding 10).
For each test output two lines. If the required tetragon exists, output in the first line YES, in the second line β€” four pairs of numbers β€” coordinates of the polygon's vertices in clockwise or counter-clockwise order. Don't forget, please, that the tetragon should be strictly convex, i.e. no 3 of its points lie on one...
Input: 31 1 2 2 3 30 1 1 0 2 29 3 7 9 9 8 | Output: NOYES3.5 1.5 0.5 2.5 -0.5 -0.5 2.5 0.5NO
Expert
2
122
273
482
0
557
E
557E
E. Ann and Half-Palindrome
2,300
data structures; dp; graphs; string suffix structures; strings; trees
Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark. On the last theoretical class the teacher introduced the notion of a half-palindrome. String t is a half-palindrome, if for all the odd positions i () the following condition is held: ti = t|t| - i + 1, where |t| is the length of...
The first line of the input contains string s (1 ≀ |s| ≀ 5000), consisting only of characters 'a' and 'b', where |s| is the length of string s.The second line contains a positive integer k β€” the lexicographical number of the requested string among all the half-palindrome substrings of the given string s. The strings ar...
Print a substring of the given string that is the k-th in the lexicographical order of all substrings of the given string that are half-palindromes.
By definition, string a = a1a2... an is lexicographically less than string b = b1b2... bm, if either a is a prefix of b and doesn't coincide with b, or there exists such i, that a1 = b1, a2 = b2, ... ai - 1 = bi - 1, ai < bi.In the first sample half-palindrome substrings are the following strings β€” a, a, a, a, aa, aba,...
Input: abbabaab7 | Output: abaa
Expert
6
992
467
148
5
761
E
761E
E. Dasha and Puzzle
2,000
constructive algorithms; dfs and similar; graphs; greedy; trees
Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity β€” origami, but remembered the puzzle that she could not solve. The tree is a non-oriented connected graph without cycles. In particular, there always are n - 1 edges in a tree with n vertices.The puzzle is to posi...
The first line contains single integer n (1 ≀ n ≀ 30) β€” the number of vertices in the tree. Each of next n - 1 lines contains two integers ui, vi (1 ≀ ui, vi ≀ n) that mean that the i-th edge of the tree connects vertices ui and vi.It is guaranteed that the described graph is a tree.
If the puzzle doesn't have a solution then in the only line print ""NO"".Otherwise, the first line should contain ""YES"". The next n lines should contain the pair of integers xi, yi (|xi|, |yi| ≀ 1018) β€” the coordinates of the point which corresponds to the i-th vertex of the tree.If there are several solutions, print...
In the first sample one of the possible positions of tree is:
Input: 71 21 32 42 53 63 7 | Output: YES0 01 00 12 01 -1-1 10 2
Hard
5
941
284
333
7
546
E
546E
E. Soldier and Traveling
2,100
flows; graphs; math
In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.Check if is it possib...
First line of input consists of two integers n and m (1 ≀ n ≀ 100, 0 ≀ m ≀ 200).Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100).Next line contains n integers b1, b2, ..., bn (0 ≀ bi ≀ 100).Then m lines follow, each of them consists of two integers p and q (1 ≀ p, q ≀ n, p β‰  q) denoting that there is an und...
If the conditions can not be met output single word ""NO"".Otherwise output word ""YES"" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i β‰  j) or how many soldiers should stay in city i (if i = j)....
Input: 4 41 2 6 33 5 3 11 22 33 44 2 | Output: YES1 0 0 0 2 0 0 0 0 5 1 0 0 0 2 1
Hard
3
393
433
385
5
54
B
54B
B. Cutting Jigsaw Puzzle
1,800
hashing; implementation
The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.Soon the Hedgehog came up with a brilliant idea: inste...
The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20.Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters.
In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X an...
The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
Input: 2 4ABDCABDC | Output: 32 1
Medium
2
1,463
251
473
0
1,136
E
1136E
E. Nastya Hasn't Written a Legend
2,200
binary search; data structures
In this task, Nastya asked us to write a formal statement.An array \(a\) of length \(n\) and an array \(k\) of length \(n-1\) are given. Two types of queries should be processed: increase \(a_i\) by \(x\). Then if \(a_{i+1} < a_i + k_i\), \(a_{i+1}\) becomes exactly \(a_i + k_i\); again, if \(a_{i+2} < a_{i+1} + k_{i+1...
The first line contains a single integer \(n\) (\(2 \leq n \leq 10^{5}\)) β€” the number of elements in the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^{9} \leq a_i \leq 10^{9}\)) β€” the elements of the array \(a\).The third line contains \(n-1\) integers \(k_1, k_2, \ldots, k_{n-1...
For each query of the second type print a single integer in a new line β€” the sum of the corresponding subarray.
In the first example: after the first change \(a = [3, 4, 3]\); after the second change \(a = [3, 4, 4]\). In the second example: after the first change \(a = [6, 9, 10]\); after the second change \(a = [6, 13, 14]\).
Input: 3 1 2 3 1 -1 5 s 2 3 + 1 2 s 1 2 + 3 1 s 2 3 | Output: 5 7 8
Hard
2
617
1,051
111
11
1,442
D
1442D
D. Sum
2,800
data structures; divide and conquer; dp; greedy
You are given \(n\) non-decreasing arrays of non-negative numbers. Vasya repeats the following operation \(k\) times: Selects a non-empty array. Puts the first element of the selected array in his pocket. Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket.
The first line contains two integers \(n\) and \(k\) (\(1 \le n, k \le 3\,000\)): the number of arrays and operations.Each of the next \(n\) lines contain an array. The first integer in each line is \(t_i\) (\(1 \le t_i \le 10^6\)): the size of the \(i\)-th array. The following \(t_i\) integers \(a_{i, j}\) (\(0 \le a_...
Print one integer: the maximum possible sum of all elements in Vasya's pocket after \(k\) operations.
Input: 3 3 2 5 10 3 1 2 3 2 1 20 | Output: 26
Master
4
318
469
101
14
1,172
E
1172E
E. Nauuo and ODT
3,300
data structures
Nauuo is a girl who loves traveling.One day she went to a tree, Old Driver Tree, literally, a tree with an old driver on it.The tree is a connected graph consisting of \(n\) nodes and \(n-1\) edges. Each node has a color, and Nauuo will visit the ODT through a simple path on the tree in the old driver's car.Nauuo wants...
The first line contains two integers \(n\) and \(m\) (\(2\le n\le 4\cdot 10^5\), \(1\le m\le 4\cdot 10^5\)) β€” the number of nodes and the number of modifications.The second line contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(1\le c_i\le n\)), where \(c_i\) is the initial color of node \(i\).Each of the next \(n-1\) ...
The output contains \(m+1\) integers β€” the first integer is the answer at the beginning, the rest integers are the answers after every modification in the given order.
Example 1The number of colors on each simple path at the beginning:
Input: 5 3 1 2 1 2 3 1 2 1 3 3 4 3 5 3 3 4 1 4 3 | Output: 47 51 49 45
Master
1
912
649
167
11
1,941
A
1941A
A. Rudolf and the Ticket
800
brute force; math
Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed \(k\).Rudolf has two pockets with coins. In the left pocket, there are \(n\) coins with denominations \(b_1, b_2, \dots, b_n\). In t...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Then follows the description of each test case.The first line of each test case contains three natural numbers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 100, 1 \le k \le 2000\)) β€” the number of coins in the left and right pockets...
For each testcase, output a single integer β€” the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed \(k\).
Note that the pairs indicate the indices of the coins in the array, not their denominations.In the first test case, Rudolf can choose the following pairs of coins: \([1, 1], [1, 2], [1, 4], [2, 1], [2, 2], [2, 4]\).In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two...
Input: 44 4 81 5 10 142 1 8 12 3 44 81 2 34 2 71 1 1 12 73 4 20001 1 11 1 1 1 | Output: 6 0 4 12
Beginner
2
635
685
173
19
1,508
A
1508A
A. Binary Literature
1,900
constructive algorithms; greedy; implementation; strings; two pointers
A bitstring is a string that contains only the characters 0 and 1.Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length \(2n\). A valid novel for the cont...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).Each of the following three lines contains a bitstring of length \(2n\). It is guaranteed that these three strings are pairwise distinc...
For each test case, print a single line containing a bitstring of length at most \(3n\) that has at least two of the given bitstrings as subsequences.It can be proven that under the constraints of the problem, such a bitstring always exists.If there are multiple possible answers, you may output any of them.
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Input: 2 1 00 11 01 3 011001 111010 010001 | Output: 010 011001010
Hard
5
695
408
308
15
1,517
B
1517B
B. Morning Jogging
1,200
constructive algorithms; greedy; sortings
The 2050 volunteers are organizing the ""Run! Chase the Rising Sun"" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are \(n+1\) checkpoints on the trail. They are numbered by \(0\), \(1\), ..., \(n\). A runner must start at checkpoint \(0\) and finish at checkpo...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 100\)).The \(i\)-th of the next \(n\) lines contains \(m\) integers \(...
For each test case, output \(n\) lines. The \(j\)-th number in the \(i\)-th line should contain the length of the path that runner \(j\) chooses to run from checkpoint \(i-1\) to checkpoint \(i\). There should be exactly \(m\) integers in the \(i\)-th line and these integers should form a permuatation of \(b_{i, 1}\), ...
In the first case, the sum of tiredness is \(\min(2,5) + \min(3,3) + \min(4,1) = 6\). In the second case, the sum of tiredness is \(\min(2,4,3) + \min(3,1,5) = 3\).
Input: 2 2 3 2 3 4 1 3 5 3 2 2 3 4 1 3 5 | Output: 2 3 4 5 3 1 2 3 4 1 3 5
Easy
3
1,401
481
403
15
643
A
643A
A. Bear and Colors
1,500
implementation
Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest ...
The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” the number of balls.The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ n) where ti is the color of the i-th ball.
Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.
In the first sample, color 2 is dominant in three intervals: An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4, 4] contains one ball, with color 2 again. An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and c...
Input: 41 2 1 2 | Output: 7 3 0 0
Medium
1
588
197
106
6
143
A
143A
A. Help Vasilisa the Wise 2
1,000
brute force; math
Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with tha...
The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of th...
Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number ""-1"" (without the quotes).If there are several solutions, output any.
Pay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number ""5"". However, Vasilisa only has one gem with each number from 1 to 9.
Input: 3 74 65 5 | Output: 1 23 4
Beginner
2
1,301
750
301
1
163
D
163D
D. Large Refrigerator
2,900
brute force
Vasya wants to buy a new refrigerator. He believes that a refrigerator should be a rectangular parallelepiped with integer edge lengths. Vasya calculated that for daily use he will need a refrigerator with volume of at least V. Moreover, Vasya is a minimalist by nature, so the volume should be no more than V, either β€” ...
The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of data sets.The description of t data sets follows. Each set consists of a single integer V (2 ≀ V ≀ 1018), given by its factorization as follows.Let V = p1a1p2a2... pkak, where pi are different prime numbers and ai are positive integer powers. Then...
Print t lines, on the i-th line print the answer to the i-th data set as four space-separated integers: the minimum possible surface area S and the corresponding edge lengths a, b, c. If there are multiple variants of the lengths of edges that give the minimum area, you are allowed to print any of them. You can print t...
In the first data set of the sample the fridge's volume V = 23 = 8, and the minimum surface area will be produced by the edges of equal length.In the second data set the volume V = 17, and it can be produced by only one set of integer lengths.
Input: 312 3117 133 12 35 1 | Output: 24 2 2 270 1 1 17148 4 6 5
Master
1
863
555
366
1
2,000
F
2000F
F. Color Rows and Columns
1,900
dp; greedy; implementation; math
You have \(n\) rectangles, the \(i\)-th of which has a width of \(a_i\) and a height of \(b_i\).You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.Each time you completely color any row or column, you earn \(1\) point. Your task is to score at le...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The following are the descriptions of the test cases.The first line of each test case description contains two integers \(n\) and \(k\) (\(1 \le n \le 1000, 1 \le k \le 100\)) β€” the number of rectangles in the case and the requir...
For each test case, output a single integer β€” the minimum number of operations required to score at least \(k\) points. If it is impossible to score at least \(k\) points, output -1.
Input: 71 46 31 54 45 101 11 11 11 11 12 1001 25 63 112 23 34 43 259 24 38 104 185 48 58 36 2 | Output: 12 14 5 -1 17 80 35
Hard
4
555
643
182
20
1,490
A
1490A
A. Dense Array
800
greedy; math
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any \(i\) (\(1 \le i \le n-1\)), this condition must be satisfied: $$$\(\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2\)\(For example, the arrays \)[1, 2, 3, 4, 3]\(, \)[1, 1...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(2 \le n \le 50\)) β€” the length of the array \(a\).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 50\)).
For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense.
The first test case is explained in the statements.In the second test case, you can insert one element, \(a=[1,\underline{\textbf{2}},3]\).In the third test case, you can insert two elements, \(a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1]\).In the fourth test case, you can insert one element, \(a=[1,\underline...
Input: 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 | Output: 5 1 2 1 0 3
Beginner
2
972
295
120
14
1,843
F2
1843F2
F2. Omsk Metro (hard version)
2,300
data structures; dfs and similar; divide and conquer; dp; math; trees
This is the hard version of the problem. The only difference between the simple and hard versions is that in this version \(u\) can take any possible value.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations conne...
The first line contains a single number \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of events.Then there are \(n\) lines describing the events. In the \(i\)-th line, one of the following options is pos...
For each of Alex's questions, output ""Yes"" (without quotes) if the subsegment described in the condition exists, otherwise output ""No"" (without quotes).You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
Explanation of the first sample.The answer to the second question is ""Yes"", because there is a path \(1\).In the fourth question, we can choose the \(1\) path again.In the fifth query, the answer is ""Yes"", since there is a path \(1-3\).In the sixth query, we can choose an empty path because the sum of the weights o...
Input: 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0 | Output: NO YES NO YES YES YES
Expert
6
1,761
1,073
299
18
254
C
254C
C. Anagram
1,800
greedy; strings
String x is an anagram of string y, if we can rearrange the letters in string x and get exact string y. For example, strings ""DOG"" and ""GOD"" are anagrams, so are strings ""BABA"" and ""AABB"", but strings ""ABBAC"" and ""CAABA"" are not.You are given two strings s and t of the same length, consisting of uppercase E...
The input consists of two lines. The first line contains string s, the second line contains string t. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters.
In the first line print z β€” the minimum number of replacement operations, needed to get an anagram of string t from string s. In the second line print the lexicographically minimum anagram that could be obtained in z operations.
The second sample has eight anagrams of string t, that can be obtained from string s by replacing exactly two letters: ""ADBADC"", ""ADDABC"", ""CDAABD"", ""CDBAAD"", ""CDBADA"", ""CDDABA"", ""DDAABC"", ""DDBAAC"". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is ""ADBADC...
Input: ABACBA | Output: 1ABC
Medium
2
1,093
203
228
2
1,764
E
1764E
E. Doremy's Number Line
2,400
dp; greedy; sortings
Doremy has two arrays \(a\) and \(b\) of \(n\) integers each, and an integer \(k\).Initially, she has a number line where no integers are colored. She chooses a permutation \(p\) of \([1,2,\ldots,n]\) then performs \(n\) moves. On the \(i\)-th move she does the following: Pick an uncolored integer \(x\) on the number l...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le 10^9\)).Each of the following \(n\) lines contain...
For each test case, output ""YES"" (without quotes) if the point \(k\) can be colored with color \(1\). Otherwise, output ""NO"" (without quotes).You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response).
For the first test case, it is impossible to color point \(16\) with color \(1\).For the second test case, \(p=[2,1,3,4]\) is one possible choice, the detail is shown below. On the first move, pick \(x=8\) and color it with color \(2\) since \(x=8\) is uncolored and \(x \le a_2\). On the second move, pick \(x=16\) and ...
Input: 64 165 38 1210 715 14 168 1210 715 15 34 1610 715 15 38 124 1615 15 38 1210 71 1000000000500000000 5000000002 10000000001 9999999991 1 | Output: NO YES YES YES NO YES
Expert
3
560
468
286
17
1,423
I
1423I
I. Lookup Tables
3,000
bitmasks
John has \(Q\) closed intervals of consecutive \(2K\)-bit numbers \([l_i, r_i]\) and one 16-bit value \(v_i\) for each interval. (\(0 \leq i < Q\))John wants to implement a function F that maps \(2K\)-bit numbers to 16-bit numbers in such a way that inputs from each interval are mapped to that interval's value. In othe...
The first line contains two integers \(K\) and \(Q\) (\( 1 <= K <= 16\), \(1 <= Q <= 2\cdot 10^5\)).Each of the next \(Q\) lines contains three integers \(l_i\), \(r_i\) and \(v_i\). ( \(0 \leq l_i \leq r_i < 2^{2K}\), \(0 \leq v_i < 2^{16}\)).
On the first line output ""possible"" (without quotes) if two tables satisfying the conditions exist, or ""impossible"" (without quotes) if they don't exist.If a solution exists, in the next \(2 \cdot 2^K\) lines your program should output all values of the two lookup tables (LSBTable and MSBTable) it found. When there...
A closed interval \([a, b]\) includes both a and b.In the first sample, tables \(\textrm{LSBTable} = [1,3]\) and \(\textrm{MSBTable} = [1,3]\) satisfy the conditions: \(F[0] = \textrm{LSBTable}[0] \& \textrm{MSBTable}[0] = 1 \& 1 = 1\), \(F[1] = \textrm{LSBTable}[1] \& \textrm{MSBTable}[0] = 3 \& 1 = 1\), \(F[2] = \tex...
Input: 1 2 0 2 1 3 3 3 | Output: possible 1 3 1 3
Master
1
1,198
244
652
14
1,251
A
1251A
A. Broken Keyboard
1,000
brute force; strings; two pointers
Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains \(26\) buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed s...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases in the input.Then the test cases follow. Each test case is represented by one line containing a string \(s\) consisting of no less than \(1\) and no more than \(500\) lowercase Latin letters.
For each test case, print one line containing a string \(res\). The string \(res\) should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, \(res\) should be empty.
Input: 4 a zzaaz ccff cbddbb | Output: a z bc
Beginner
3
1,543
282
279
12
225
A
225A
A. Dice Tower
1,100
constructive algorithms; greedy
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice ...
The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of dice in the tower.The second line contains an integer x (1 ≀ x ≀ 6) β€” the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≀ ai, bi ≀ 6; ai β‰  bi) β€” the number...
Print ""YES"" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print ""NO"" (without the quotes).
Input: 363 25 42 4 | Output: YES
Easy
2
944
634
188
2
1,158
F
1158F
F. Density of subarrays
3,500
dp; math
Let \(c\) be some positive integer. Let's call an array \(a_1, a_2, \ldots, a_n\) of positive integers \(c\)-array, if for all \(i\) condition \(1 \leq a_i \leq c\) is satisfied. Let's call \(c\)-array \(b_1, b_2, \ldots, b_k\) a subarray of \(c\)-array \(a_1, a_2, \ldots, a_n\), if there exists such set of \(k\) indic...
The first line contains two integers \(n\) and \(c\), separated by spaces (\(1 \leq n, c \leq 3\,000\)). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\), separated by spaces (\(1 \leq a_i \leq c\)).
Print \(n + 1\) numbers \(s_0, s_1, \ldots, s_n\). \(s_p\) should be equal to the number of sequences of indices \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\) for all \(1 \leq k \leq n\) by modulo \(998\,244\,353\), such that the density of array \(a_{i_1}, a_{i_2}, \ldots, a_{i_k}\) is equal to \(p\).
In the first example, it's easy to see that the density of array will always be equal to its length. There exists \(4\) sequences with one index, \(6\) with two indices, \(4\) with three and \(1\) with four.In the second example, the only sequence of indices, such that the array will have non-zero density is all indice...
Input: 4 1 1 1 1 1 | Output: 0 4 6 4 1
Master
2
996
216
300
11
167
C
167C
C. Wizards and Numbers
2,300
games; math
In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it β€” a and b. The order of the numbers is not important. Let's consider a ≀ b for the sake of definiteness. The players can cast one of the two spells in turns: Replace b with b - ak. Number k can be chosen by the pl...
The first line contains a single integer t β€” the number of input data sets (1 ≀ t ≀ 104). Each of the next t lines contains two integers a, b (0 ≀ a, b ≀ 1018). The numbers are separated by a space.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout stre...
For any of the t input sets print ""First"" (without the quotes) if the player who moves first wins. Print ""Second"" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input.
In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win.In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win.In the third sample, the first player has no moves....
Input: 410 2131 100 110 30 | Output: FirstSecondSecondFirst
Expert
2
936
350
286
1
1,203
B
1203B
B. Equal Rectangles
1,200
greedy; math
You are given \(4n\) sticks, the length of the \(i\)-th stick is \(a_i\).You have to create \(n\) rectangles, each rectangle will consist of exactly \(4\) sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick c...
The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β€” the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 100\)) β€” the number of rectangles.The second line of the query contains \(4n\) integers \(a_1, a_2, \dots, a_{4n}\) (\(1 \...
For each query print the answer to it. If it is impossible to create exactly \(n\) rectangles of equal area using given sticks, print ""NO"". Otherwise print ""YES"".
Input: 5 1 1 1 10 10 2 10 5 2 10 1 1 2 5 2 10 5 1 10 5 1 1 1 2 1 1 1 1 1 1 1 1 1 10000 10000 10000 10000 | Output: YES YES NO YES YES
Easy
2
707
390
166
12
683
E
683E
E. Hammer throwing
1,800
*special
n athletes take part in the hammer throw. Each of them has his own unique identifier β€” the integer from 1 to n (all athletes have distinct identifiers). After the draw, the order in which the athletes will throw the hammer has been determined (they will do it one by one).Unfortunately, a not very attentive judge lost t...
The first line contains the positive integer n (1 ≀ n ≀ 1000) β€” the number of athletes.The next line contains the sequence of integers a1, a2, ..., an (0 ≀ ai < n), where ai is equal to the number of the athletes with identifiers larger than i, who should throw the hammer before the athlete with identifier i.
Print n distinct numbers β€” the sequence of athletes' identifiers in the order in which they will throw the hammer. If there are several answers it is allowed to print any of them.
Input: 42 0 1 0 | Output: 2 4 1 3
Medium
1
583
310
179
6
1,081
H
1081H
H. Palindromic Magic
3,500
data structures; hashing; strings
After learning some fancy algorithms about palindromes, Chouti found palindromes very interesting, so he wants to challenge you with this problem.Chouti has got two strings \(A\) and \(B\). Since he likes palindromes, he would like to pick \(a\) as some non-empty palindromic substring of \(A\) and \(b\) as some non-emp...
The first line contains a single string \(A\) (\(1 \le |A| \le 2 \cdot 10^5\)).The second line contains a single string \(B\) (\(1 \le |B| \le 2 \cdot 10^5\)).Strings \(A\) and \(B\) contain only lowercase English letters.
The first and only line should contain a single integer β€” the number of possible strings.
In the first example, attainable strings are ""a"" + ""a"" = ""aa"", ""aa"" + ""a"" = ""aaa"", ""aa"" + ""aba"" = ""aaaba"", ""aa"" + ""b"" = ""aab"", ""a"" + ""aba"" = ""aaba"", ""a"" + ""b"" = ""ab"". In the second example, attainable strings are ""aa"", ""aaa"", ""aaaa"", ""aaaba"", ""aab"", ""aaba"", ""ab"", ""abaa...
Input: aaaba | Output: 6
Master
3
520
222
89
10
433
D
433D
D. Nanami's Digital Board
2,000
dsu; implementation
Nanami is an expert at playing games. This day, Nanami's good friend Hajime invited her to watch a game of baseball. Unwilling as she was, she followed him to the stadium. But Nanami had no interest in the game, so she looked around to see if there was something that might interest her. That's when she saw the digital ...
The first line contains three space-separated integers n, m and q (1 ≀ n, m, q ≀ 1000) β€” the height and width of the digital board, and the number of operations.Then follow n lines, each line containing m space-separated integers. The j-th integer of the i-th line is ai, j β€” the initial state of pixel (i, j). If ai, j ...
For each query, print a single line containing one integer β€” the answer to Nanami's query.
Consider the first sample.The first query specifies pixel (2, 2), which is dark itself, so there are no valid light blocks, thus the answer is 0.The second query specifies pixel (1, 2). The biggest light block is the block with (1, 2) as its upper-left vertex and (1, 3) as its lower-right vertex.The last query specifie...
Input: 3 4 50 1 1 01 0 0 10 1 1 02 2 22 1 21 2 21 2 32 2 2 | Output: 026
Hard
2
1,372
724
90
4
2,131
C
2131C
C. Make it Equal
0
math; number theory
Given two multisets \(S\) and \(T\) of size \(n\) and a positive integer \(k\), you may perform the following operations any number (including zero) of times on \(S\): Select an element \(x\) in \(S\), and remove one occurrence of \(x\) in \(S\). Then, either insert \(x+k\) into \(S\), or insert \(|x-k|\) into \(S\). D...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows. The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \( 1 \le k \le 10^9\)) β€” the size of \(S\) and the constant, ...
For each test case, output ""YES"" if it is possible to make \(S\) equal to \(T\), 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 test case, we can remove one occurrence of \(1\) from \(S\) and insert \(|1-k|=|1-3|=2\) into \(S\), making \(S\) equal to \(T\).In the second test case, we can remove one occurrence of \(4\) from \(S\) and insert \(4+k=4+8=12\) into \(S\), making \(S\) equal to \(T\).In the last test case, we can show tha...
Input: 51 3121 84123 56 2 98 4 112 72 82 93 20 1 01 0 1 | Output: YES YES YES NO NO
Beginner
2
486
648
266
21
1,085
C
1085C
C. Connect Three
1,600
implementation; math
The Squareland national forest is divided into equal \(1 \times 1\) square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates \((x, y)\) of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of lan...
The first line contains two integers \(x_A\) and \(y_A\) β€” coordinates of the plot \(A\) (\(0 \leq x_A, y_A \leq 1000\)). The following two lines describe coordinates \((x_B, y_B)\) and \((x_C, y_C)\) of plots \(B\) and \(C\) respectively in the same format (\(0 \leq x_B, y_B, x_C, y_C \leq 1000\)). It is guaranteed th...
On the first line print a single integer \(k\) β€” the smallest number of plots needed to be cleaned from trees. The following \(k\) lines should contain coordinates of all plots needed to be cleaned. All \(k\) plots should be distinct. You can output the plots in any order.If there are multiple solutions, print any of t...
The first example is shown on the picture in the legend.The second example is illustrated with the following image:
Input: 0 0 1 1 2 2 | Output: 5 0 0 1 0 1 1 1 2 2 2
Medium
2
1,009
352
324
10
1,310
E
1310E
E. Strange Function
2,900
dp
Let's define the function \(f\) of multiset \(a\) as the multiset of number of occurences of every number, that is present in \(a\).E.g., \(f(\{5, 5, 1, 2, 5, 2, 3, 3, 9, 5\}) = \{1, 1, 2, 2, 4\}\).Let's define \(f^k(a)\), as applying \(f\) to array \(a\) \(k\) times: \(f^k(a) = f(f^{k-1}(a)), f^0(a) = a\). E.g., \(f^2...
The first and only line of input consists of two integers \(n, k\) (\(1 \le n, k \le 2020\)).
Print one number β€” the number of different values of function \(f^k(a)\) on all possible non-empty arrays with no more than \(n\) elements modulo \(998\,244\,353\).
Input: 3 1 | Output: 6
Master
1
605
93
164
13
1,107
C
1107C
C. Brutality
1,300
greedy; sortings; two pointers
You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have \(26\) buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on button...
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β€” the number of hits and the maximum number of times you can push the same button in a row.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the d...
Print one integer \(dmg\) β€” the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.
In the first example you can choose hits with numbers \([1, 3, 4, 5, 6, 7]\) with the total damage \(1 + 16 + 18 + 7 + 2 + 10 = 54\).In the second example you can choose all hits so the total damage is \(2 + 4 + 1 + 3 + 1000 = 1010\).In the third example you can choose all hits expect the third one so the total damage ...
Input: 7 3 1 5 16 18 7 2 10 baaaaca | Output: 54
Easy
3
1,237
571
135
11
24
B
24B
B. F1 Champions
1,500
implementation
Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive points in the following order 25, 18, 15, 12, 10, 8, 6, 4, 2, 1. At the conclusion of the championship the driver with most points is the cha...
The first line contain integer t (1 ≀ t ≀ 20), where t is the number of races. After that all races are described one by one. Every race description start with an integer n (1 ≀ n ≀ 50) on a line of itself, where n is the number of clasified drivers in the given race. After that n lines follow with the classification f...
Your output should contain exactly two line. On the first line is the name of the champion according to the original rule, and on the second line the name of the champion according to the alternative rule.
It is not guaranteed that the same drivers participate in all races. For the championship consider every driver that has participated in at least one race. The total number of drivers during the whole season is not more then 50.
Input: 33HamiltonVettelWebber2WebberVettel2HamiltonVettel | Output: VettelHamilton
Medium
1
1,041
602
205
0
1,158
D
1158D
D. Winding polygonal line
2,600
constructive algorithms; geometry; greedy; math
Vasya has \(n\) different points \(A_1, A_2, \ldots A_n\) on the plane. No three of them lie on the same line He wants to place them in some order \(A_{p_1}, A_{p_2}, \ldots, A_{p_n}\), where \(p_1, p_2, \ldots, p_n\) β€” some permutation of integers from \(1\) to \(n\).After doing so, he will draw oriented polygonal lin...
The first line contains one integer \(n\) β€” the number of points (\(3 \leq n \leq 2000\)). Next \(n\) lines contains two integers \(x_i\) and \(y_i\), divided by space β€” coordinates of the point \(A_i\) on the plane (\(-10^9 \leq x_i, y_i \leq 10^9\)). The last line contains a string \(s\) consisting of symbols ""L"" a...
If the satisfying permutation doesn't exists, print \(-1\). In the other case, print \(n\) numbers \(p_1, p_2, \ldots, p_n\) β€” the permutation which was found (\(1 \leq p_i \leq n\) and all \(p_1, p_2, \ldots, p_n\) are different). If there exists more than one solution, you can find any.
This is the picture with the polygonal line from the \(1\) test: As we see, this polygonal line is non-self-intersecting and winding, because the turn in point \(2\) is left.This is the picture with the polygonal line from the \(2\) test:
Input: 3 1 1 3 1 1 3 L | Output: 1 2 3
Expert
4
2,000
440
289
11
1,769
C2
1769C2
C2. ΠŸΠΎΠ΄ΠΊΡ€ΡƒΡ‚ΠΊΠ° II
1,300
*special; dp
Π’ этой вСрсии Π·Π°Π΄Π°Ρ‡ΠΈ \(n \le 2 \cdot 10^5\) ΠΈ \(a_i \le 10^6\) (Π° Ρ‚Π°ΠΊΠΆΠ΅ Π΅ΡΡ‚ΡŒ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ Π½Π° сумму \(n\) ΠΏΠΎ Π½Π°Π±ΠΎΡ€Π°ΠΌ Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ тСста).Π’ΠΈΠΊΠ° Π·Π° врСмя Ρ€Π°Π±ΠΎΡ‚Ρ‹ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ VK ΡƒΠΆΠ΅ сдСлала \(n\) ΠΊΠΎΠΌΠΌΠΈΡ‚ΠΎΠ² Π² систСмС контроля вСрсий. \(i\)-ΠΉ ΠΊΠΎΠΌΠΌΠΈΡ‚ Π±Ρ‹Π» сдСлан Π² \(a_i\)-ΠΉ дСнь Ρ€Π°Π±ΠΎΡ‚Ρ‹ Π’ΠΈΠΊΠΈ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ. Π’ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π΄...
ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ тСст состоит ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… Π½Π°Π±ΠΎΡ€ΠΎΠ² Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…. Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС находится ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число \(t\) (\(1 \le t \le 100\)) β€” количСство Π½Π°Π±ΠΎΡ€ΠΎΠ² Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…. Π”Π°Π»Π΅Π΅ слСдуСт описаниС Π½Π°Π±ΠΎΡ€ΠΎΠ² Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ….ΠŸΠ΅Ρ€Π²Π°Ρ строка ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Π½Π°Π±ΠΎΡ€Π° Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… содСрТит ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€”...
Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Π½Π°Π±ΠΎΡ€Π° Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число β€” ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡƒΡŽ Π΄Π»ΠΈΠ½Ρƒ ΠΎΡ‚Ρ€Π΅Π·ΠΊΠ° Π΄Π½Π΅ΠΉ, Π² ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΈΠ· ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρƒ Π’ΠΈΠΊΠΈ Π² ΠΏΡ€ΠΎΡ„ΠΈΠ»Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ°Ρ‚ΡŒΡΡ хотя Π±Ρ‹ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ‚, послС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠΎΠ΄ΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… ΠΊΠΎΠΌΠΌΠΈΡ‚ΠΎΠ² Π²ΠΏΠ΅Ρ€Ρ‘Π΄ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ Ρ‡Π΅ΠΌ Π½Π° сутки.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ Π½Π°Π±ΠΎΡ€Π΅ Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠΌΠ΅Π½ΡΡ‚ΡŒ Π΄Π°Ρ‚Ρƒ ΠΊΠΎΠΌΠΌΠΈΡ‚Π° Π² дСнь \(3\) Π½Π° дСнь \(4\), Π΄Π°Ρ‚Ρƒ ΠΊΠΎΠΌΠΌΠΈΡ‚Π° Π² дСнь \(4\) β€” Π½Π° дСнь \(5\), Π° Π΄Π°Ρ‚Ρƒ любого ΠΈΠ· ΠΊΠΎΠΌΠΌΠΈΡ‚ΠΎΠ² Π² дСнь \(6\) β€” Π½Π° дСнь \(7\). Π’ΠΎΠ³Π΄Π° Π² ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΈΠ· Π΄Π½Π΅ΠΉ \(4\), \(5\), \(6\), \(7\) ΠΈ \(8\) Π² ΠΏΡ€ΠΎΡ„ΠΈΠ»Π΅ Π’ΠΈΠΊΠΈ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ°Ρ‚ΡŒΡΡ хотя Π±Ρ‹ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ‚, ΠΈ наибольший ΠΎΡ‚Ρ€Π΅Π·ΠΎΠΊ ...
Input: 391 1 3 4 6 6 6 8 1061 2 3 4 5 6510 10 10 10 10 | Output: 5 6 2
Easy
2
1,177
617
264
17
1,583
F
1583F
F. Defender of Childhood Dreams
2,500
bitmasks; constructive algorithms; divide and conquer
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right?You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city.Liyue can be represented as a...
The only line of input contains two integers \(n\) and \(k\) (\(2 \leq k < n \leq 1000\)).
On the first line, output \(c\), the minimum colors you need to satisfy the above requirements.On the second line, print a valid edge coloring as an array of \(\frac{n(n-1)}{2}\) integers ranging from \(1\) to \(c\). Exactly \(c\) distinct colors should exist in the construction. Print the edges in increasing order by ...
The corresponding construction for the first test case looks like this: It is impossible to satisfy the constraints with less than \(2\) colors.The corresponding construction for the second test case looks like this: One can show there exists no construction using less than \(3\) colors.
Input: 5 3 | Output: 2 1 2 2 2 2 2 2 1 1 1
Expert
3
1,288
90
541
15
708
A
708A
A. Letters Cyclic Shift
1,200
constructive algorithms; greedy; implementation; strings
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.What is the lexicog...
The only line of the input contains the string s (1 ≀ |s| ≀ 100 000) consisting of lowercase English letters.
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
String s is lexicographically smaller than some other string t of the same length if there exists some 1 ≀ i ≀ |s|, such that s1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti.
Input: codeforces | Output: bncdenqbdr
Easy
4
412
109
126
7
453
A
453A
A. Little Pony and Expected Maximum
1,600
probabilities
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, an...
A single line contains two integers m and n (1 ≀ m, n ≀ 105).
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Consider the third test example. If you've made two tosses: You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. You can get 2 in the first toss, an...
Input: 6 1 | Output: 3.500000000000
Medium
1
613
61
161
4
1,930
E
1930E
E. 2..3...4.... Wonderful! Wonderful!
2,400
combinatorics; dp; math
Stack has an array \(a\) of length \(n\) such that \(a_i = i\) for all \(i\) (\(1 \leq i \leq n\)). He will select a positive integer \(k\) (\(1 \leq k \leq \lfloor \frac{n-1}{2} \rfloor\)) and do the following operation on \(a\) any number (possibly \(0\)) of times. Select a subsequence\(^\dagger\) \(s\) of length \(2...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^3\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^6\)) β€” the length of the array \(a\).It is gu...
For each test, on a new line, print \(\lfloor \frac{n-1}{2} \rfloor\) space-separated integers β€” the \(i\)-th integer representing the number of arrays modulo \(998\,244\,353\) that Stack can get if he selects \(k=i\).
In the first test case, two \(a\) are possible for \(k=1\): \([1,2,3]\); \([2]\). In the second test case, four \(a\) are possible for \(k=1\): \([1,2,3,4]\); \([1,3]\); \([2,3]\); \([2,4]\). In the third test case, two \(a\) are possible for \(k=2\): \([1,2,3,4,5]\); \([3]\).
Input: 434510 | Output: 2 4 10 2 487 162 85 10
Expert
3
1,139
396
218
19
1,800
G
1800G
G. Symmetree
2,200
dfs and similar; hashing; implementation; trees
Kid was gifted a tree of \(n\) vertices with the root in the vertex \(1\). Since he really like symmetrical objects, Kid wants to find out if this tree is symmetrical. For example, the trees in the picture above are symmetrical. And the trees in this picture are not symmetrical. Formally, a tree is symmetrical if there...
The first line of input data contains single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.The first line of each case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of vertices in the tree.The following \(n-1\) lines contain two integers each \(u\) and \(v\) (\(1 \l...
Output \(t\) strings, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if this tree is symmetrical, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
Input: 661 51 61 22 32 471 51 33 61 44 74 291 22 42 33 51 77 67 88 9102 99 102 36 74 31 23 82 56 5103 28 109 74 28 22 14 56 55 71 | Output: YES NO YES NO NO YES
Hard
4
693
480
304
18
1,506
A
1506A
A. Strange Table
800
math
Polycarp found a rectangular table consisting of \(n\) rows and \(m\) columns. He noticed that each cell of the table has its number, obtained by the following algorithm ""by columns"": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; num...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.Each test case consists of a single line containing three integers \(n\), \(m\), \(x\) (\(1 \le n, m \le 10^6\), \(1 \le x \le n \cdot m\)), where \(n\) and \(m\) are the number of rows and columns in the table, and \(x\...
For each test case, output the cell number in the numbering ""by rows"".
Input: 5 1 1 1 2 2 3 3 5 11 100 100 7312 1000000 1000000 1000000000000 | Output: 1 2 9 1174 1000000000000
Beginner
1
1,262
509
72
15
1,181
D
1181D
D. Irrigation
2,200
binary search; data structures; implementation; sortings; trees; two pointers
Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first \(n\) olympiads the organizers...
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \leq n, m, q \leq 500\,000\)) β€” the number of olympiads before the rule was introduced, the number of cities in Berland wishing to host the olympiad, and the number of years Misha's mother is interested in, respectively.The next line contains \(n\) inte...
Print \(q\) integers. The \(i\)-th of them should be the city the olympiad will be hosted in the year \(k_i\).
In the first example Misha's mother is interested in the first \(10\) years after the rule was introduced. The host cities these years are 4, 3, 4, 2, 3, 4, 1, 2, 3, 4.In the second example the host cities after the new city is introduced are 2, 3, 1, 2, 3, 5, 1, 2, 3, 4, 5, 1.
Input: 6 4 10 3 1 1 1 2 2 7 8 9 10 11 12 13 14 15 16 | Output: 4 3 4 2 3 4 1 2 3 4
Hard
6
1,034
687
110
11
1,006
C
1006C
C. Three Parts of the Array
1,200
binary search; data structures; two pointers
You are given an array \(d_1, d_2, \dots, d_n\) consisting of \(n\) integer numbers.Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (poss...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in the array \(d\).The second line of the input contains \(n\) integers \(d_1, d_2, \dots, d_n\) (\(1 \le d_i \le 10^9\)) β€” the elements of the array \(d\).
Print a single integer β€” the maximum possible value of \(sum_1\), considering that the condition \(sum_1 = sum_3\) must be met.Obviously, at least one valid way to split the array exists (use \(a=c=0\) and \(b=n\)).
In the first example there is only one possible splitting which maximizes \(sum_1\): \([1, 3, 1], [~], [1, 4]\).In the second example the only way to have \(sum_1=4\) is: \([1, 3], [2, 1], [4]\).In the third example there is only one way to split the array: \([~], [4, 1, 2], [~]\).
Input: 51 3 1 1 4 | Output: 5
Easy
3
1,135
267
215
10
602
B
602B
B. Approximating a Constant Range
1,400
dp; implementation; two pointers
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their aver...
The first line of the input contains a single integer n (2 ≀ n ≀ 100 000) β€” the number of data points.The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 100 000).
Print a single number β€” the maximum length of an almost constant range of the given sequence.
In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Input: 51 2 3 3 2 | Output: 4
Easy
3
967
173
93
6
177
E1
177E1
E1. Space Voyage
1,700
binary search
The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit n planets. For planet i ai is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and bi is the number of citizens on the planet.The Smart Beaver is going to bring some ...
The first input line contains space-separated integers n and c β€” the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly.The next n lines contain pairs of space-separated integers ai, bi (1 ≀ i ≀ n) β€” the number of suitcases he can bring to the i-th...
Print a single number k β€” the number of ways to choose x so as to travel for exactly c days. If there are infinitely many possible values of x, print -1.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
In the first example there is only one suitable value x = 5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days...
Input: 2 51 52 4 | Output: 1
Medium
1
1,298
753
295
1
50
B
50B
B. Choosing Symbol Pairs
1,500
strings
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 ≀ i, j ≀ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.
Input: great10 | Output: 7
Medium
1
217
163
178
0
1,615
E
1615E
E. Purple Crayon
2,400
data structures; dfs and similar; games; graphs; greedy; math; sortings; trees
Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game.The game works as follows: there is a tree of size \(n\), rooted at node \(1\), where each node is initially white. Red an...
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le k \le n\)) β€” the number of vertices in the tree and the maximum number of red nodes.Next \(n - 1\) lines contains description of edges. The \(i\)-th line contains two space separated integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_...
Print one integer β€” the resulting score if both Red and Blue play optimally.
In the first test case, the optimal strategy is as follows: Red chooses to color the subtrees of nodes \(2\) and \(3\). Blue chooses to color the subtree of node \(4\). At the end of this process, nodes \(2\) and \(3\) are red, node \(4\) is blue, and node \(1\) is white. The score of the game is \(1 \cdot (2 - 1) = 1\...
Input: 4 2 1 2 1 3 1 4 | Output: 1
Expert
8
1,488
426
76
16
83
A
83A
A. Magical Array
1,300
math
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.Valera absolutely ...
The first line of the input data contains an integer n (1 ≀ n ≀ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109).
Print on the single line the answer to the problem: the amount of subarrays, which are magical.Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Notes to sample tests:Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Input: 42 1 1 4 | Output: 5
Easy
1
1,043
161
260
0
1,705
E
1705E
E. Mark and Professor Koro
2,300
binary search; bitmasks; brute force; combinatorics; data structures; greedy
After watching a certain anime before going to sleep, Mark dreams of standing in an old classroom with a blackboard that has a sequence of \(n\) positive integers \(a_1, a_2,\dots,a_n\) on it.Then, professor Koro comes in. He can perform the following operation: select an integer \(x\) that appears at least \(2\) times...
The first line of the input contains two integers \(n\) and \(q\) (\(2\leq n\leq 2\cdot 10^5\), \(1\leq q\leq 2\cdot 10^5\)) β€” the length of the sequence \(a\) and the number of updates, respectively.The second line contains \(n\) integers \(a_1,a_2,\dots,a_n\) (\(1\leq a_i\leq 2\cdot 10^5\))Then, \(q\) lines follow, e...
Print \(q\) lines. The \(i\)-th line should consist of a single integer β€” the answer after the \(i\)-th update.
In the first example test, the program must proceed through \(4\) updates.The sequence after the first update is \([2,3,2,4,5]\). One sequence of operations that achieves the number \(6\) the following. Initially, the blackboard has numbers \([2,3,2,4,5]\). Erase two copies of \(2\) and write \(3\), yielding \([3,4,5,\...
Input: 5 4 2 2 2 4 5 2 3 5 3 4 1 1 4 | Output: 6 5 4 5
Expert
6
1,066
452
111
17
414
A
414A
A. Mashmokh and Numbers
1,500
constructive algorithms; number theory
It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he remov...
The first line of input contains two space-separated integers n, k (1 ≀ n ≀ 105; 0 ≀ k ≀ 108).
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109).
gcd(x, y) is greatest common divisor of x and y.
Input: 5 2 | Output: 1 2 3 4 5
Medium
2
984
94
125
4
41
B
41B
B. Martian Dollar
1,400
brute force
One day Vasya got hold of information on the Martian dollar course in bourles for the next n days. The buying prices and the selling prices for one dollar on day i are the same and are equal to ai. Vasya has b bourles. He can buy a certain number of dollars and then sell it no more than once in n days. According to Mar...
The first line contains two integers n and b (1 ≀ n, b ≀ 2000) β€” the number of days and the initial number of money in bourles. The next line contains n integers ai (1 ≀ ai ≀ 2000) β€” the prices of Martian dollars.
Print the single number β€” which maximal sum of money in bourles can Vasya get by the end of day n.
Input: 2 43 7 | Output: 8
Easy
1
450
213
98
0
1,657
A
1657A
A. Integer Moves
800
brute force; math
There's a chip in the point \((0, 0)\) of the coordinate plane. In one operation, you can move the chip from some point \((x_1, y_1)\) to some point \((x_2, y_2)\) if the Euclidean distance between these two points is an integer (i.e. \(\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}\) is integer).Your task is to determine the minimum ...
The first line contains a single integer \(t\) (\(1 \le t \le 3000\)) β€” number of test cases.The single line of each test case contains two integers \(x\) and \(y\) (\(0 \le x, y \le 50\)) β€” the coordinates of the destination point.
For each test case, print one integer β€” the minimum number of operations required to move the chip from the point \((0, 0)\) to the point \((x, y)\).
In the first example, one operation \((0, 0) \rightarrow (8, 6)\) is enough. \(\sqrt{(0-8)^2+(0-6)^2}=\sqrt{64+36}=\sqrt{100}=10\) is an integer.In the second example, the chip is already at the destination point.In the third example, the chip can be moved as follows: \((0, 0) \rightarrow (5, 12) \rightarrow (9, 15)\)....
Input: 38 60 09 15 | Output: 1 0 2
Beginner
2
417
232
149
16
1,183
E
1183E
E. Subsequences (easy version)
2,000
dp; graphs; implementation; shortest paths
The only difference between the easy and the hard versions is constraints.A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them...
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n, k \le 100\)) β€” the length of the string and the size of the set, correspondingly.The second line of the input contains a string \(s\) consisting of \(n\) lowercase Latin letters.
Print one integer β€” if it is impossible to obtain the set \(S\) of size \(k\), print -1. Otherwise, print the minimum possible total cost to do it.
In the first example we can generate \(S\) = { ""asdf"", ""asd"", ""adf"", ""asf"", ""sdf"" }. The cost of the first element in \(S\) is \(0\) and the cost of the others is \(1\). So the total cost of \(S\) is \(4\).
Input: 4 5 asdf | Output: 4
Hard
4
1,029
256
147
11
264
C
264C
C. Choosing Balls
2,000
dp
There are n balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the i-th ball is ci and the value of the i-th ball is vi.Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wants to maximize ...
The first line contains two integers n and q (1 ≀ n ≀ 105; 1 ≀ q ≀ 500). The second line contains n integers: v1, v2, ..., vn (|vi| ≀ 105). The third line contains n integers: c1, c2, ..., cn (1 ≀ ci ≀ n).The following q lines contain the values of the constants a and b for queries. The i-th of these lines contains two...
For each query, output a line containing an integer β€” the answer to the query. The i-th line contains the answer to the i-th query in the input order.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first example, to achieve the maximal value: In the first query, you should select 1st, 3rd, and 4th ball. In the second query, you should select 3rd, 4th, 5th and 6th ball. In the third query, you should select 2nd and 4th ball. Note that there may be other ways to achieve the maximal value.
Input: 6 31 -2 3 4 0 -11 2 1 2 1 15 1-2 11 0 | Output: 2094
Hard
1
909
412
299
2
1,993
A
1993A
A. Question Marks
800
greedy; implementation
Tim is doing a test consisting of \(4n\) questions; each question has \(4\) options: 'A', 'B', 'C', and 'D'. For each option, there are exactly \(n\) correct answers corresponding to that option β€” meaning there are \(n\) questions with the answer 'A', \(n\) questions with the answer 'B', \(n\) questions with the answer...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)).The second line of each test case contains a string \(s\) of \(4n\) characters (\(s_i \in \{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \t...
For each test case, print a single integer β€” the maximum score that Tim can achieve.
In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct.In the second test case, there are only two correct answers 'A' which makes him get exactly \(2\) points in any case.In the third test case, Tim can get at most \(2\) cor...
Input: 61ABCD2AAAAAAAA2AAAABBBB2????????3ABCABCABCABC5ACADC??ACAC?DCAABC?C | Output: 4 2 4 0 9 13
Beginner
2
630
368
84
19
449
D
449D
D. Jzzhu and Numbers
2,400
bitmasks; combinatorics; dp
Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≀ k ≀ n)? Help him and print this number modulo 1000000007 (109 + 7). Operation x & y denote...
The first line contains a single integer n (1 ≀ n ≀ 106). The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 106).
Output a single integer representing the number of required groups modulo 1000000007 (109 + 7).
Input: 32 3 3 | Output: 0
Expert
3
359
125
95
4
1,374
E1
1374E1
E1. Reading Books (easy version)
1,600
data structures; greedy; sortings
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will re...
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)).The next \(n\) lines contain descriptions of books, one description per line: the \(i\)-th line contains three integers \(t_i\), \(a_i\) and \(b_i\) (\(1 \le t_i \le 10^4\), \(0 \le a_i, b_i \le 1\)), where: \(t_i\) β€”...
If there is no solution, print only one integer -1. Otherwise print one integer \(T\) β€” the minimum total reading time of the suitable set of books.
Input: 8 47 1 12 1 14 0 18 1 11 0 11 1 11 0 13 0 0 | Output: 18
Medium
3
1,309
527
148
13
1,932
D
1932D
D. Card Game
1,400
greedy; implementation
Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters 'C', 'D', 'H', and 'S', respectively. And there are 8 ranks, in increasing order: '2', '3', '4', '5', '6',...
The first line contains integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of a test case contains the integer number \(n\) (\(1\le n\le 16\)).The second line of a test case contains one character, the trump suit. It is one of ""CDHS"".The third line of a test ca...
For each test case print the answer to it: Print \(n\) lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it. If there is no solution, print a single line ""IMPOSSIB...
Input: 83S3C 9S 4C 6D 3S 7S2C3S 5D 9S 6H1H6C 5D1S7S 3S1H9S 9H1S9S 9H1C9D 8H2C9C 9S 6H 8C | Output: 3C 4C 6D 9S 3S 7S IMPOSSIBLE IMPOSSIBLE 3S 7S 9S 9H 9H 9S IMPOSSIBLE 6H 9C 9S 8C
Easy
2
1,386
584
376
19
706
A
706A
A. Beru-taxi
900
brute force; geometry; implementation
Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi, yi) and moves with a speed vi. Consider that each of n drivers will move directly to Vasiliy an...
The first line of the input contains two integers a and b ( - 100 ≀ a, b ≀ 100) β€” coordinates of Vasiliy's home.The second line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of available Beru-taxi cars nearby. The i-th of the following n lines contains three integers xi, yi and vi ( - 100 ≀ xi, yi ≀ 100, 1 ≀ ...
Print a single real value β€” the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer corre...
In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.In the second sample, cars 2 and 3 will arrive simultaneously.
Input: 0 022 0 10 2 2 | Output: 1.00000000000000000000
Beginner
3
425
512
328
7
1,942
C2
1942C2
C2. Bessie's Birthday Cake (Hard Version)
1,700
geometry; greedy; math
Proof Geometric Construction Can Solve All Love Affairs - manbo-pβ €This is the hard version of the problem. The only difference between the two versions is the constraint on \(y\). In this version \(0 \leq y \leq n - x\). You can make hacks only if both versions are solved.Bessie has received a birthday cake from her be...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case consists of three integers, \(n\), \(x\), and \(y\) (\(4 \leq n \leq 10^9\), \(2 \leq x \leq \min(n, 2 \cdot 10^5)\), \(0 \leq y \leq n - x\)) β€” the number of sides of the polygon, number ...
For each test case, output a single integer: the maximum number of non-intersecting triangular pieces of cake she can give out.
In test cases \(1\), \(2\) and \(3\), you can get \(6\), \(5\) and \(2\) non-intersecting triangular pieces of cake, respectively. A possible construction is shown in the following pictures:The green dots represent vertices that Bessie chose, the yellow dots represent vertices that you chose, the blue lines represent d...
Input: 38 4 21 6 2 57 3 16 4 34 2 21 3 | Output: 6 5 2
Medium
3
1,235
611
127
19
178
B2
178B2
B2. Greedy Merchants
1,600
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities....
The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 ≀ ai, bi ≀ n, ai β‰  bi), separated by a space β€” the numbers of cities connected by the i-th road. It is guaranteed that...
Print exactly k lines, the i-th line should contain a single integer di β€” the number of dinars that the i-th merchant paid.
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is des...
Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120
Medium
0
1,676
1,052
123
1
1,037
F
1037F
F. Maximum Reduction
2,500
combinatorics; data structures; math
Given an array \(a\) of \(n\) integers and an integer \(k\) (\(2 \le k \le n\)), where each element of the array is denoted by \(a_i\) (\(0 \le i < n\)). Perform the operation \(z\) given below on \(a\) and print the value of \(z(a,k)\) modulo \(10^{9}+7\).function z(array a, integer k): if length(a) < k: return 0 else...
The first line of input contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 10^6\)) β€” the length of the initial array \(a\) and the parameter \(k\).The second line of input contains \(n\) integers \(a_0, a_1, \ldots, a_{n - 1}\) (\(1 \le a_{i} \le 10^9\)) β€” the elements of the array \(a\).
Output the only integer, the value of \(z(a,k)\) modulo \(10^9+7\).
In the first example: for \(a=(9,1,10)\), \(ans=19\) and \(b=(9,10)\), for \(a=(9,10)\), \(ans=10\) and \(b=(10)\), for \(a=(10)\), \(ans=0\). So the returned value is \(19+10+0=29\).In the second example: for \(a=(5,8,7,1,9)\), \(ans=25\) and \(b=(8,8,9)\), for \(a=(8,8,9)\), \(ans=9\) and \(b=(9)\), for \(a=(9)\), \(...
Input: 3 29 1 10 | Output: 29
Expert
3
502
297
67
10
1,656
B
1656B
B. Subtract Operation
1,100
data structures; greedy; math; two pointers
You are given a list of \(n\) integers. You can perform the following operation: you choose an element \(x\) from the list, erase \(x\) from the list, and subtract the value of \(x\) from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly \(1\).Given an integer \(k\) (\(k...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 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\) (\(2 \leq n \leq 2\cdot 10^5\), \(1 \leq k \leq 10^9\)), the number ...
For each test case, print YES if you can achieve \(k\) with a sequence of \(n-1\) operations. Otherwise, print NO.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as a positive answer).
In the first example we have the list \(\{4, 2, 2, 7\}\), and we have the target \(k = 5\). One way to achieve it is the following: first we choose the third element, obtaining the list \(\{2, 0, 5\}\). Next we choose the first element, obtaining the list \(\{-2, 3\}\). Finally, we choose the first element, obtaining t...
Input: 44 54 2 2 75 41 9 1 3 42 1717 02 1718 18 | Output: YES NO YES NO
Easy
4
479
607
246
16
1,009
F
1009F
F. Dominant Indices
2,300
data structures; dsu; trees
You are given a rooted undirected tree consisting of \(n\) vertices. Vertex \(1\) is the root.Let's denote a depth array of vertex \(x\) as an infinite sequence \([d_{x, 0}, d_{x, 1}, d_{x, 2}, \dots]\), where \(d_{x, i}\) is the number of vertices \(y\) such that both conditions hold: \(x\) is an ancestor of \(y\); th...
The first line contains one integer \(n\) (\(1 \le n \le 10^6\)) β€” the number of vertices in a tree.Then \(n - 1\) lines follow, each containing two integers \(x\) and \(y\) (\(1 \le x, y \le n\), \(x \ne y\)). This line denotes an edge of the tree.It is guaranteed that these edges form a tree.
Output \(n\) numbers. \(i\)-th number should be equal to the dominant index of vertex \(i\).
Input: 41 22 33 4 | Output: 0000
Expert
3
668
295
92
10
1,606
E
1606E
E. Arena
2,100
combinatorics; dp; math
There are \(n\) heroes fighting in the arena. Initially, the \(i\)-th hero has \(a_i\) health points.The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals \(1\) damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than \(1...
The only line contains two integers \(n\) and \(x\) (\(2 \le n \le 500; 1 \le x \le 500\)).
Print one integer β€” the number of ways to choose the initial health points for each hero \(a_i\), where \(1 \le a_i \le x\), so that there is no winner of the fight, taken modulo \(998244353\).
Input: 2 5 | Output: 5
Hard
3
882
91
193
16
2,004
F
2004F
F. Make a Palindrome
2,600
binary search; brute force; data structures; greedy; math
You are given an array \(a\) consisting of \(n\) integers.Let the function \(f(b)\) return the minimum number of operations needed to make an array \(b\) a palindrome. The operations you can make are: choose two adjacent elements \(b_i\) and \(b_{i+1}\), remove them, and replace them with a single element equal to \((b...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)).Additional constraint on the input: the sum o...
For each test case, print a single integer β€” the sum of the values of the function \(f\) for all subarrays of the array \(a\).
Input: 432 1 341 1 1 154 2 3 1 541 2 1 2 | Output: 3 0 14 5
Expert
5
943
373
126
20
1,219
G
1219G
G. Harvester
2,000
implementation
It is Bubble Cup finals season and farmer Johnny Bubbles must harvest his bubbles. The bubbles are in a rectangular bubblefield formed of \(N\) x \(M\) square parcels divided into \(N\) rows and \(M\) columns. The parcel in \(i^{th}\) row and \(j^{th}\) column yields \(A_{i,j}\) bubbles.Johnny Bubbles has available a v...
The first line contains two integers \(N\) and \(M\) (\(1\) \(\leq\) \(N\), \(M\) \(\leq\) \(N\) * \(M\) \(\leq\) \(10^{5}\)) - the bubblefield size.Each of the next \(N\) lines contains \(M\) integers. The \(j^{th}\) element in the \(i^{th}\) line is \(A_{i,j}\) (\(0\) \(\leq\) \(a_{i,j}\) \(\leq\) \(10^{9}\)) β€” the y...
Output contains one integer number - maximum number of the bubbles Johnny can harvest on the first day.
In the first example, farmer Johnny can harvest all the bubbles by positioning the harvester on the first and the second row.In the second example, one way Johnny can harvest maximum number of bubbles is to position the harvester in the second row, the fourth row, the second column and the fourth column.
Input: 2 2 1 2 3 4 | Output: 10
Hard
1
1,005
395
103
12
661
B
661B
B. Seasons
1,900
*special
You are given a name of a month. Output the season of the year to which it belongs (based on Northern hemisphere).
The input consists of a single string containing the name of one of the twelve months (January, February, March, April, May, June, July, August, September, October, November or December). The string is capitalized as given here.
Output a single string β€” the season of the year to which the given month belongs (winter, spring, summer or autumn). The name of the season should be in lowercase.
Assume that winter is December through February, spring is March through May, summer is June through August and autumn is September through November.
Input: April | Output: spring
Hard
1
114
228
163
6
1,141
G
1141G
G. Privatization of Roads in Treeland
1,900
binary search; constructive algorithms; dfs and similar; graphs; greedy; trees
Treeland consists of \(n\) cities and \(n-1\) roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right β€” the country's topology is an undirected tree.There are some private road companies in Treeland. The government decided to sell road...
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 200000, 0 \le k \le n - 1\)) β€” the number of cities and the maximal number of cities which can have two or more roads belonging to one company.The following \(n-1\) lines contain roads, one road per line. Each line contains a pair of integers \(x_i\), ...
In the first line print the required \(r\) (\(1 \le r \le n - 1\)). In the second line print \(n-1\) numbers \(c_1, c_2, \dots, c_{n-1}\) (\(1 \le c_i \le r\)), where \(c_i\) is the company to own the \(i\)-th road. If there are multiple answers, print any of them.
Input: 6 2 1 4 4 3 3 5 3 6 5 2 | Output: 2 1 2 1 1 2
Hard
6
1,657
423
265
11
988
C
988C
C. Equal Sums
1,400
implementation; sortings
You are given \(k\) sequences of integers. The length of the \(i\)-th sequence equals to \(n_i\).You have to choose exactly two sequences \(i\) and \(j\) (\(i \ne j\)) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence \(i\) (its length will be equal to \(n_i...
The first line contains an integer \(k\) (\(2 \le k \le 2 \cdot 10^5\)) β€” the number of sequences.Then \(k\) pairs of lines follow, each pair containing a sequence.The first line in the \(i\)-th pair contains one integer \(n_i\) (\(1 \le n_i < 2 \cdot 10^5\)) β€” the length of the \(i\)-th sequence. The second line of th...
If it is impossible to choose two sequences such that they satisfy given conditions, print ""NO"" (without quotes). Otherwise in the first line print ""YES"" (without quotes), in the second line β€” two integers \(i\), \(x\) (\(1 \le i \le k, 1 \le x \le n_i\)), in the third line β€” two integers \(j\), \(y\) (\(1 \le j \l...
In the first example there are two sequences \([2, 3, 1, 3, 2]\) and \([1, 1, 2, 2, 2, 1]\). You can remove the second element from the first sequence to get \([2, 1, 3, 2]\) and you can remove the sixth element from the second sequence to get \([1, 1, 2, 2, 2]\). The sums of the both resulting sequences equal to \(8\)...
Input: 252 3 1 3 261 1 2 2 2 1 | Output: YES2 61 2
Easy
2
588
614
688
9
1,550
A
1550A
A. Find The Array
800
greedy; math
Let's call an array \(a\) consisting of \(n\) positive (greater than \(0\)) integers beautiful if the following condition is held for every \(i\) from \(1\) to \(n\): either \(a_i = 1\), or at least one of the numbers \(a_i - 1\) and \(a_i - 2\) exists in the array as well.For example: the array \([5, 3, 1]\) is beauti...
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β€” the number of test cases.Then \(t\) lines follow, the \(i\)-th line contains one integer \(s\) (\(1 \le s \le 5000\)) for the \(i\)-th test case.
Print \(t\) integers, the \(i\)-th integer should be the answer for the \(i\)-th testcase: the minimum possible size of a beautiful array with the sum of elements equal to \(s\).
Consider the example test: in the first test case, the array \([1]\) meets all conditions; in the second test case, the array \([3, 4, 1]\) meets all conditions; in the third test case, the array \([1, 2, 4]\) meets all conditions; in the fourth test case, the array \([1, 4, 6, 8, 10, 2, 11]\) meets all conditions.
Input: 4 1 8 7 42 | Output: 1 3 3 7
Beginner
2
1,270
212
178
15
317
D
317D
D. Game with Powers
2,300
dp; games
Vasya and Petya wrote down all integers from 1 to n to play the ""powers"" game (n can be quite large; however, Vasya and Petya are not confused by this fact).Players choose numbers in turn (Vasya chooses first). If some number x is chosen at the current turn, it is forbidden to choose x or all of its other positive in...
Input contains single integer n (1 ≀ n ≀ 109).
Print the name of the winner β€” ""Vasya"" or ""Petya"" (without quotes).
In the first sample Vasya will choose 1 and win immediately.In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win.
Input: 1 | Output: Vasya
Expert
2
597
46
71
3
670
D2
670D2
D2. Magic Powder - 2
1,500
binary search; implementation
The term of this problem is the same as the previous one, the only exception β€” increased restrictions.
The first line contains two positive integers n and k (1 ≀ n ≀ 100 000, 1 ≀ k ≀ 109) β€” the number of ingredients and the number of grams of the magic powder.The second line contains the sequence a1, a2, ..., an (1 ≀ ai ≀ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake ...
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Input: 1 100000000011000000000 | Output: 2000000000
Medium
2
102
500
132
6
346
D
346D
D. Robot Control
2,600
dp; graphs; shortest paths
The boss of the Company of Robot is a cruel man. His motto is ""Move forward Or Die!"". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called ""Three Laws of Robotics"": Law 1. The Robot will destroy itself ...
The first line contains two integers n (1 ≀ n ≀ 106) β€” the number of vertices of the graph, and m (1 ≀ m ≀ 106) β€” the number of edges. Then m lines follow, each with two integers ui and vi (1 ≀ ui, vi ≀ n; vi β‰  ui), these integers denote that there is a directed edge from vertex ui to vertex vi. The last line contains ...
If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1.
Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can ...
Input: 4 61 22 11 33 12 43 41 4 | Output: 1
Expert
3
1,756
421
118
3
425
A
425A
A. Sereja and Swaps
1,500
brute force; sortings
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:A swap operation is the following sequence of actions: choose two indexes i, j (i β‰  j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to...
The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000).
In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.
Input: 10 210 -1 2 2 2 2 2 2 -1 10 | Output: 32
Medium
2
355
152
123
4
1,749
E
1749E
E. Cactus Wall
2,400
constructive algorithms; dfs and similar; graphs; shortest paths
Monocarp is playing Minecraft and wants to build a wall of cacti. He wants to build it on a field of sand of the size of \(n \times m\) cells. Initially, there are cacti in some cells of the field. Note that, in Minecraft, cacti cannot grow on cells adjacent to each other by side β€” and the initial field meets this rest...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β€” number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\); \(n \times m \le 4 \cdot 10^5\)) β€” the number of rows and columns, respectively.Then \(n\) rows follow, \(i\)-th row conta...
For each test case, print NO in the first line if it is impossible to build a cactus wall without breaking the rules. Otherwise, print YES in the first line, then print \(n\) lines of \(m\) characters each β€” the field itself, where the \(j\)-th character of the \(i\)-th line is equal to '#', if there is a cactus on the...
Input: 42 4.#....#.3 3#.#....#.5 5.........................4 3#...#.#.#... | Output: YES .#.# #.#. NO YES ....# ...#. ..#.. .#... #.... YES #.. .#. #.# ...
Expert
4
895
578
457
17
1,451
C
1451C
C. String Equality
1,400
dp; greedy; hashing; implementation; strings
Ashish has two strings \(a\) and \(b\), each of length \(n\), and an integer \(k\). The strings only contain lowercase English letters.He wants to convert string \(a\) into string \(b\) by performing some (possibly zero) operations on \(a\).In one move, he can either choose an index \(i\) (\(1 \leq i\leq n-1\)) and swa...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases. The description of each test case is as follows.The first line of each test case contains two integers \(n\) (\(2 \leq n \leq 10^6\)) and \(k\) (\(1 \leq k \leq n\)).The second line of each test case contains the string ...
For each test case, print ""Yes"" if Ashish can convert \(a\) into \(b\) after some moves, else print ""No"".You may print the letters of the answer in any case (upper or lower).
In the first test case it can be shown that it is impossible to convert \(a\) into \(b\).In the second test case,""abba"" \(\xrightarrow{\text{inc}}\) ""acca"" \(\xrightarrow{\text{inc}}\) \(\ldots\) \(\xrightarrow{\text{inc}}\) ""azza"".Here ""swap"" denotes an operation of the first type, and ""inc"" denotes an opera...
Input: 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc | Output: No Yes No Yes
Easy
5
855
589
178
14
1,533
G
1533G
G. Biome Map
0
*special; constructive algorithms; dfs and similar; graphs
Polycarp decided to generate a biome map for his game. A map is a matrix divided into cells \(1 \times 1\). Each cell of the map must contain one of the available biomes.Each biome is defined by two parameters: temperature (an integer from \(1\) to \(n\)) and humidity (an integer from \(1\) to \(m\)). But not for every...
The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10\)) β€” maximum temperature and humidity parameters.The following \(n\) lines contain \(m\) integers each \(a_{i,1}, a_{i, 2}, \dots, a_...
For each test case, print the answer in the following format: print \(-1\) in a single line if there is no map that meets all the conditions; otherwise, in the first line, print two integers \(h\) and \(w\) β€” the number of rows and columns of the map, respectively. In the following \(h\) lines, print \(w\) integers β€” t...
Input: 4 2 3 0 2 5 0 1 0 3 5 0 3 4 9 11 1 5 0 10 12 0 6 7 0 0 2 2 2 0 0 5 1 2 13 37 | Output: 1 3 5 2 1 2 8 11 9 4 3 5 1 5 6 12 10 9 4 3 5 6 7 -1 1 2 13 37
Beginner
4
1,015
596
387
15
1,499
G
1499G
G. Graph Coloring
3,100
data structures; graphs; interactive
You are given a bipartite graph consisting of \(n_1\) vertices in the first part, \(n_2\) vertices in the second part, and \(m\) edges, numbered from \(1\) to \(m\). You have to color each edge into one of two colors, red and blue. You have to minimize the following value: \(\sum \limits_{v \in V} |r(v) - b(v)|\), wher...
The first line contains three integers \(n_1\), \(n_2\) and \(m\) (\(1 \le n_1, n_2, m \le 2 \cdot 10^5\)).Then \(m\) lines follow, the \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i \le n_1\); \(1 \le y_i \le n_2\)) meaning that the \(i\)-th edge connects the vertex \(x_i\) from the first part...
To answer a query of type \(1\), print one integer β€” the hash of the optimal coloring.To answer a query of type \(2\), print one line. It should begin with the integer \(k\) β€” the number of red edges. Then, \(k\) distinct integer should follow β€” the indices of red edges in your coloring, in any order. Each index should...
Input: 3 4 2 1 2 3 4 10 1 1 3 1 2 3 2 1 3 3 2 1 2 4 2 1 2 1 1 1 1 2 | Output: 8 8 1 3 40 2 3 5 104 3 5 6 3 104 360 4 5 6 3 8
Master
3
2,020
786
538
14
1,811
A
1811A
A. Insert Digit
800
greedy; math; strings
You have a positive number of length \(n\) and one additional digit.You can insert this digit anywhere in the number, including at the beginning or at the end.Your task is to make the result as large as possible.For example, you have the number \(76543\), and the additional digit is \(4\). Then the maximum number you c...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The descriptions of the test cases follow.The first line of the description of each test case contains two integers \(n\) and \(d\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le d \le 9\)) β€” the length of the number and an additio...
For each test case, output a string consisting of \(n + 1\) digits β€” the maximum possible number that can be obtained.
Input: 115 4765431 012 5443 66665 6135795 89753119 498765432101234567895 7737378 1200000007 0705895912 1828127127732 | Output: 765443 10 544 6666 613579 987531 98765443210123456789 773737 210000000 70589590 8281271277321
Beginner
3
459
635
118
18
145
C
145C
C. Lucky Subsequence
2,100
combinatorics; dp; math
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence ...
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
In the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Input: 3 210 10 10 | Output: 3
Hard
3
1,054
133
112
1
1,924
F
1924F
F. Anti-Proxy Attendance
3,500
constructive algorithms; dp; interactive; ternary search
This is an interactive problem!Mr. 1048576 is one of those faculty who hates wasting his time in taking class attendance. Instead of taking attendance the old-fashioned way, he decided to try out something new today.There are \(n\) students in his class, having roll numbers \(1\) to \(n\). He knows that exactly \(1\) s...
For the first test case, the student with roll number \(2\) is absent and the truth sequence (see section for hacks) is TFFTFTTF. During execution of your solution, this test case will use a non-adaptive grader.For the second test case, the student with roll number \(4\) is absent, and the truth sequence is FFTFTTFT. D...
Input: 2 5 3 2 1 2 0 1 0 2 0 1 6 6 2 2 0 1 1 0 0 0 1 | Output: ? 1 4 ? 3 5 ? 2 2 ? 1 3 ? 3 3 ? 3 3 ! 3 ? 2 4 ? 4 4 ! 2 # ? 1 6 ? 1 3 ? 4 6 ? 1 1 ? 3 3 ? 5 5 ! 3 ? 2 2 ? 4 4 ! 4 #
Master
4
2,410
0
0
19
15
A
15A
A. Cottage Village
1,200
implementation; sortings
A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.The architect bureau, where Pete...
The first line of the input data contains numbers n and t (1 ≀ n, t ≀ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi β€” x-coordinate of the centre of the i-th house, and ai β€” length of its side ( - 1000 ≀ xi ≀ 1000, 1 ≀ ai ≀ 1000).
Output the amount of possible positions of the new house.
It is possible for the x-coordinate of the new house to have non-integer value.
Input: 2 20 46 2 | Output: 4
Easy
2
819
292
57
0
1,578
I
1578I
I. Interactive Rays
3,300
geometry; interactive
This is an interactive problem.Your goal is to find a circle on a plane by shooting rays and getting the distance to the circle as a result. Interactor has three hidden integer parameters that are determined in advance for each test, but which you don't know β€” \(x_c\), \(y_c\), and \(r_c\). \((x_c, y_c)\) are coordinat...
Illustration of the queries from the example interaction.
Input: ? 0 -10 ? 10 -10 ? 10 0 ? 10 10 ? 10 20 ? 10 30 ! 20 10 10 | Output: 12.360679775 11.2132034356 0.0 0.0 3.416407865 5.8113883008
Master
2
733
0
0
15
1,656
G
1656G
G. Cycle Palindrome
3,200
constructive algorithms; graphs; math
We say that a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\) is a palindrome if for all \(1 \leq i \leq n\), \(a_i = a_{n-i+1}\). You are given a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\) and you have to find, if it exists, a cycle permutation \(\sigma\) so that the sequence \(a_{\sigma(1)}, a_{\sigma(...
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 an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β€” the size of the sequence.The second l...
For each test case, output one line with YES if a cycle permutation exists, otherwise output one line with NO.If the answer is YES, output one additional line with \(n\) integers \(\sigma(1), \sigma(2), \ldots, \sigma(n)\), the permutation. If there is more than one permutation, you may print any.
Input: 3 4 1 2 2 1 3 1 2 1 7 1 3 3 3 1 2 2 | Output: YES 3 1 4 2 NO YES 5 3 7 2 6 4 1
Master
3
738
475
298
16