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
490
C
490C
C. Hacking Cypher
1,700
brute force; math; number theory; strings
Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long intege...
The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers a, b (1 ≤ a, b ≤ 108).
In the first line print ""YES"" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part ...
Input: 11640102497 1024 | Output: YES116401024
Medium
4
749
242
531
4
865
G
865G
G. Flowers and Chocolate
3,300
combinatorics; math; matrices
It's Piegirl's birthday soon, and Pieguy has decided to buy her a bouquet of flowers and a basket of chocolates.The flower shop has F different types of flowers available. The i-th type of flower always has exactly pi petals. Pieguy has decided to buy a bouquet consisting of exactly N flowers. He may buy the same type ...
The first line of input will contain integers F, B, and N (1 ≤ F ≤ 10, 1 ≤ B ≤ 100, 1 ≤ N ≤ 1018), the number of types of flowers, the number of types of boxes, and the number of flowers that must go into the bouquet, respectively.The second line of input will contain F integers p1, p2, ..., pF (1 ≤ pi ≤ 109), the numb...
Print the number of bouquet+basket combinations Pieguy can buy, modulo 1000000007 = 109 + 7.
In the first example, there is 1 way to make a bouquet with 9 petals (3 + 3 + 3), and 1 way to make a basket with 9 pieces of chocolate (3 + 3 + 3), for 1 possible combination. There are 3 ways to make a bouquet with 13 petals (3 + 5 + 5, 5 + 3 + 5, 5 + 5 + 3), and 5 ways to make a basket with 13 pieces of chocolate (3...
Input: 2 3 33 510 3 7 | Output: 17
Master
3
1,434
501
92
8
1,626
B
1626B
B. Minor Reduction
1,100
greedy; strings
You are given a decimal representation of an integer \(x\) without leading zeros.You have to perform the following reduction on it exactly once: take two neighboring digits in \(x\) and replace them with their sum without leading zeros (if the sum is \(0\), it's represented as a single \(0\)).For example, if \(x = 1005...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.Each testcase consists of a single integer \(x\) (\(10 \le x < 10^{200000}\)). \(x\) doesn't contain leading zeros.The total length of the decimal representations of \(x\) over all testcases doesn't exceed \(2 \cdot 10^5\).
For each testcase, print a single integer — the largest number that can be obtained after the reduction is applied exactly once. The number should not contain leading zeros.
The first testcase of the example is already explained in the statement.In the second testcase, there is only one possible reduction: the first and the second digits.
Input: 21005790 | Output: 10012 9
Easy
2
850
319
173
16
997
A
997A
A. Convert to Ones
1,500
brute force; greedy; implementation; math
You've got a string \(a_1, a_2, \dots, a_n\), consisting of zeros and ones.Let's call a sequence of consecutive elements \(a_i, a_{i + 1}, \ldots, a_j\) (\(1\leq i\leq j\leq n\)) a substring of string \(a\). You can apply the following operations any number of times: Choose some substring of string \(a\) (for example, ...
The first line of input contains integers \(n\), \(x\) and \(y\) (\(1 \leq n \leq 300\,000, 0 \leq x, y \leq 10^9\)) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).The second line contains the string \(a\) of length \(n\), c...
Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print \(0\), if you do not need to perform any operations.
In the first sample, at first you need to reverse substring \([1 \dots 2]\), and then you need to invert substring \([2 \dots 5]\). Then the string was changed as follows:«01000» \(\to\) «10000» \(\to\) «11111».The total cost of operations is \(1 + 10 = 11\).In the second sample, at first you need to invert substring \...
Input: 5 1 1001000 | Output: 11
Medium
4
916
348
179
9
1,781
C
1781C
C. Equal Frequencies
1,600
brute force; constructive algorithms; greedy; implementation; sortings; strings
Let's call a string balanced if all characters that are present in it appear the same number of times. For example, ""coder"", ""appall"", and ""ttttttt"" are balanced, while ""wowwow"" and ""codeforces"" are not.You are given a string \(s\) of length \(n\) consisting of lowercase English letters. Find a balanced strin...
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.Each test case consists of two lines. The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the string \(s\).The second line co...
For each test case, print the smallest number of positions where string \(s\) and a balanced string \(t\) can differ, followed by such a string \(t\).If there are multiple solutions, print any. It can be shown that at least one balanced string always exists.
In the first test case, the given string ""hello"" is not balanced: letters 'h', 'e', and 'o' appear in it once, while letter 'l' appears twice. On the other hand, string ""helno"" is balanced: five distinct letters are present in it, and each of them appears exactly once. Strings ""hello"" and ""helno"" differ in just...
Input: 45hello10codeforces5eevee6appall | Output: 1 helno 2 codefofced 1 eeeee 0 appall
Medium
6
567
484
258
17
1,605
C
1605C
C. Dominant Character
1,400
brute force; greedy; implementation; strings
Ashish has a string \(s\) of length \(n\) containing only characters 'a', 'b' and 'c'.He wants to find the length of the smallest substring, which satisfies the following conditions: Length of the substring is at least \(2\) 'a' occurs strictly more times in this substring than 'b' 'a' occurs strictly more times in thi...
The first line contains a single integer \(t\) \((1 \le t \le 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 \le n \le 10^{6})\) — the length of the string \(s\).The second line of each test case contains a string \(s\) c...
For each test case, output the length of the smallest substring which satisfies the given conditions or print \(-1\) if there is no such substring.
Consider the first test case. In the substring ""aa"", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring ""aa"" satisfies the condition and the answer is \(2\). The substring ""a"" also satisfies this condition, however its length is not at least ...
Input: 3 2 aa 5 cbabb 8 cacabccc | Output: 2 -1 3
Easy
4
640
452
147
16
463
E
463E
E. Caisa and Tree
2,100
brute force; dfs and similar; math; number theory; trees
Caisa is now at home and his son has a simple task for him.Given a rooted tree with n vertices, numbered from 1 to n (vertex 1 is the root). Each vertex of the tree has a value. You should answer q queries. Each query is one of the following: Format of the query is ""1 v"". Let's write out the sequence of vertices alon...
The first line contains two space-separated integers n, q (1 ≤ n, q ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2·106), where ai represent the value of node i.Each of the next n - 1 lines contains two integers xi and yi (1 ≤ xi, yi ≤ n; xi ≠ yi), denoting the edge of the tree between vertices ...
For each query of the first type output the result of the query.
gcd(x, y) is greatest common divisor of two integers x and y.
Input: 4 610 8 4 31 22 33 41 11 21 31 42 1 91 4 | Output: -112-11
Hard
5
736
564
64
4
107
C
107C
C. Arrangement
2,400
bitmasks; dp
In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far.The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall.Traditionally GUC has n professors. Each professor has his seniority level. ...
The first line contains three integers n, y and m (1 ≤ n ≤ 16, 2001 ≤ y ≤ 1018, 0 ≤ m ≤ 100) — the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively.The next m lines contain one pair of integers eac...
Print the order in which the professors should be seated in the requested year.If by this year the GUC would have ran out of arrangements, or the given ""senior-junior"" relation are contradictory, print ""The times have changed"" (without quotes).
In the first example the lexicographically first order of seating is 1 2 3.In the third example the GUC will run out of arrangements after the year 3630800.In the fourth example there are no valid arrangements for the seating.The lexicographical comparison of arrangements is performed by the < operator in modern progra...
Input: 3 2001 21 22 3 | Output: 1 2 3
Expert
2
1,615
654
248
1
1,842
A
1842A
A. Tenzing and Tsondu
800
games; math
Tsondu always runs first! ! !Tsondu and Tenzing are playing a card game. Tsondu has \(n\) monsters with ability values \(a_1, a_2, \ldots, a_n\) while Tenzing has \(m\) monsters with ability values \(b_1, b_2, \ldots, b_m\).Tsondu and Tenzing take turns making moves, with Tsondu going first. In each move, the current p...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^3\)) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 50\)) — the number of monsters Tsondu...
For each test case, output ""Tsondu"" if Tsondu wins, ""Tenzing"" if Tenzing wins, and ""Draw"" if the game ends in a draw. (Output without quotes.)Note that the output is case-sensitive. For example, if the answer is ""Tsondu"", the outputs ""tsondu"", ""TSONDU"", and ""tSonDu"" will all be recognized as incorrect out...
Consider the first test case. It can be shown that Tsondu has a winning strategy. The following is a possible way that Tsondu can win (note that the players may not be playing optimally in this example): In the first move, Tsondu chooses a monster with ability value \(9\) on his side to fight against a monster with abi...
Input: 61 391 2 32 31 21 1 13 21 2 31 13 31 1 12 2 210 101 2 3 3 2 2 1 1 2 23 3 3 3 2 1 1 1 1 110 101 2 3 4 5 6 7 8 9 106 7 8 9 10 11 1 1 1 1 | Output: Tsondu Draw Tsondu Tenzing Draw Draw
Beginner
2
956
652
325
18
1,453
F
1453F
F. Even Harder
2,700
dp
Gildong is now developing a puzzle game. The puzzle consists of \(n\) platforms numbered from \(1\) to \(n\). The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the \(1\)-st platform to the \(n\)-th platform.The \(i\)-th platform is labeled wi...
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)).Each test case contains two lines. The first line of each test case consists of an integer \(n\) (\(2 \le n \le 3000\)) — the number of platforms of the game.The second line of each test case contains...
For each test case, print one integer — the minimum number of different labels that should be changed to \(0\) so that there remains exactly one way to win.
In the first case, the player can only move to the next platform until they get to the \(4\)-th platform. Since there is already only one way to win, the answer is zero.In the second case, Gildong can change \(a_2\), \(a_3\), and \(a_4\) to \(0\) so that the game becomes \(4\) \(0\) \(0\) \(0\) \(0\). Now the only way ...
Input: 3 4 1 1 1 0 5 4 3 2 1 0 9 4 1 4 2 1 0 2 1 0 | Output: 0 3 2
Master
1
1,088
578
156
14
1,416
B
1416B
B. Make Them Equal
2,000
constructive algorithms; greedy; math
You are given an array \(a\) consisting of \(n\) positive integers, numbered from \(1\) to \(n\). You can perform the following operation no more than \(3n\) times: choose three integers \(i\), \(j\) and \(x\) (\(1 \le i, j \le n\); \(0 \le x \le 10^9\)); assign \(a_i := a_i - x \cdot i\), \(a_j := a_j + x \cdot i\). A...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^4\)) — the number of elements in the array. The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_...
For each test case print the answer to it as follows: if there is no suitable sequence of operations, print \(-1\); otherwise, print one integer \(k\) (\(0 \le k \le 3n\)) — the number of operations in the sequence. Then print \(k\) lines, the \(m\)-th of which should contain three integers \(i\), \(j\) and \(x\) (\(1 ...
Input: 3 4 2 16 4 18 6 1 2 3 4 5 6 5 11 19 1 1 3 | Output: 2 4 1 2 2 3 3 -1 4 1 2 4 2 4 5 2 3 3 4 5 1
Hard
3
496
446
506
14
260
C
260C
C. Balls and Boxes
1,700
constructive algorithms; greedy; implementation
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting ba...
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents ...
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
Input: 4 44 3 1 6 | Output: 3 2 5 4
Medium
3
1,575
550
240
2
1,211
A
1211A
A. Three Problems
1,000
*special; implementation
Polycarp is choosing three problems for creating a programming test. Totally he has \(n\) problems in his list. The complexity of the \(i\)-th problem equals \(r_i\). All problems are numerated from \(1\) to \(n\).Help Polycarp to choose such three problems \(a\), \(b\) and \(c\), so that the complexity of the first pr...
The first line of the input contains one integer \(n\) (\(3 \le n \le 3000\)) — the number of problems in Polycarp's list.The second line of the input contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(1 \le r_i \le 10^9\)), where \(r_i\) is the complexity of the \(i\)-th problem.
If Polycarp has no ways to choose three problems, you should print three numbers -1. Ih there is a way to choose them, you should print three different integers \(a, b, c\) (\(1 \le a, b, c \le n\)), where \(a\) is the number of the first chosen problem, \(b\) is the number of the second chosen problem and \(c\) is the...
Input: 6 3 1 4 1 5 9 | Output: 4 1 3
Beginner
2
651
282
356
12
925
C
925C
C. Big Secret
2,200
constructive algorithms; math
Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence \(a_1, \ldots, a_n\). In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence \(b_1, \ldots, b_n\) using the ...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)).The second line contains \(n\) integers \(b_1, \ldots, b_n\) (\(1 \leq b_i < 2^{60}\)).
If there are no valid permutations, print a single line containing ""No"".Otherwise in the first line print the word ""Yes"", and in the second line print integers \(b'_1, \ldots, b'_n\) — a valid permutation of integers \(b_i\). The unordered multisets \(\{b_1, \ldots, b_n\}\) and \(\{b'_1, \ldots, b'_n\}\) should be ...
In the first example no permutation is valid.In the second example the given answer lead to the sequence \(a_1 = 4\), \(a_2 = 8\), \(a_3 = 15\), \(a_4 = 16\), \(a_5 = 23\), \(a_6 = 42\).
Input: 31 2 3 | Output: No
Hard
2
1,077
159
641
9
797
B
797B
B. Odd sum
1,400
dp; greedy; implementation
You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.Subsequence is a sequence that can be derived from another sequence by deleting ...
The first line contains integer number n (1 ≤ n ≤ 105).The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Print sum of resulting subseqeuence.
In the first example sum of the second and the fourth elements is 3.
Input: 4-2 2 -3 1 | Output: 3
Easy
3
454
195
36
7
1,610
B
1610B
B. Kalindrome Array
1,100
greedy; two pointers
An array \([b_1, b_2, \ldots, b_m]\) is a palindrome, if \(b_i = b_{m+1-i}\) for each \(i\) from \(1\) to \(m\). Empty array is also a palindrome.An array is called kalindrome, if the following condition holds: It's possible to select some integer \(x\) and delete some of the elements of the array equal to \(x\), so th...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the array.The second line of each test case contains \(n\) integers ...
For each test case, print YES if \(a\) is kalindrome and NO otherwise. You can print each letter in any case.
In the first test case, array \([1]\) is already a palindrome, so it's a kalindrome as well.In the second test case, we can choose \(x = 2\), delete the second element, and obtain array \([1]\), which is a palindrome.In the third test case, it's impossible to obtain a palindrome.In the fourth test case, you can choose ...
Input: 4 1 1 2 1 2 3 1 2 3 5 1 4 4 1 4 | Output: YES YES NO YES
Easy
2
914
480
109
16
833
E
833E
E. Caramel Clouds
3,400
data structures; dp; sortings
It is well-known that the best decoration for a flower bed in Sweetland are vanilla muffins. Seedlings of this plant need sun to grow up. Slastyona has m seedlings, and the j-th seedling needs at least kj minutes of sunlight to grow up.Most of the time it's sunny in Sweetland, but sometimes some caramel clouds come, th...
The first line contains two integers n and C (0 ≤ n ≤ 3·105, 0 ≤ C ≤ 109) – the number of caramel clouds and the number of candies Slastyona has.The next n lines contain three integers each: li, ri, ci (0 ≤ li < ri ≤ 109, 0 ≤ ci ≤ 109), describing one caramel cloud.The next line contains single integer m (1 ≤ m ≤ 3·105...
For each seedling print one integer – the minimum minute Slastyona can grow it up.
Consider the first example. For each k it is optimal to dispel clouds 1 and 3. Then the remaining cloud will give shadow on time segment [1..6]. So, intervals [0..1] and [6..inf) are sunny. In the second example for k = 1 it is not necessary to dispel anything, and for k = 5 the best strategy is to dispel clouds 2 and ...
Input: 3 51 7 11 6 21 7 13725 | Output: 12710
Master
3
1,182
458
82
8
1,430
E
1430E
E. String Reversal
1,900
data structures; greedy; strings
You are given a string \(s\). You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string ""abddea"", you should get t...
The first line contains one integer \(n\) (\(2 \le n \le 200\,000\)) — the length of \(s\).The second line contains \(s\) — a string consisting of \(n\) lowercase Latin letters.
Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string.
In the first example, you have to swap the third and the fourth elements, so the string becomes ""aazaa"". Then you have to swap the second and the third elements, so the string becomes ""azaaa"". So, it is possible to reverse the string in two swaps.Since the string in the second example is a palindrome, you don't hav...
Input: 5 aaaza | Output: 2
Hard
3
522
177
114
14
1,101
B
1101B
B. Accordion
1,300
greedy; implementation
An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code \(091\)), a colon (ASCII code \(058\)), some (possibly zero) vertical line characters (ASCII code \(124\)), another c...
The only line contains one string \(s\) (\(1 \le |s| \le 500000\)). It consists of lowercase Latin letters and characters [, ], : and |.
If it is not possible to obtain an accordion by removing some characters from \(s\), print \(-1\). Otherwise print maximum possible length of the resulting accordion.
Input: |[a:b:|] | Output: 4
Easy
2
900
136
166
11
24
A
24A
A. Ring road
1,400
graphs
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two ot...
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
Input: 31 3 11 2 13 2 1 | Output: 1
Easy
1
811
303
158
0
48
H
48H
H. Black and White
2,800
constructive algorithms
According to the legends the king of Berland Berl I was noted for his love of beauty and order. One day he ordered to tile the palace hall's floor where balls and receptions used to take place with black and white tiles according to a regular geometrical pattern invented by him. However, as is after the case, due to lo...
The first line contains given integers n and m (1 ≤ n, m ≤ 100) which represent the sizes of the rectangle that needs to be tiled. The next line contains non-negative numbers a, b and c, a + b + c = nm, c ≥ m.
Print 2n lines containing 2m characters each — the tiling scheme. Every tile is represented by a square 2 × 2 in the following manner (the order corresponds to the order of the picture above): If multiple solutions exist, output any.
Input: 2 20 0 4 | Output: \../#\/#\##/.\/.
Master
1
911
209
233
0
391
C3
391C3
C3. The Tournament
0
This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points.Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined, t...
The first line contains a pair of integers n and k (1 ≤ k ≤ n + 1). The i-th of the following n lines contains two integers separated by a single space — pi and ei (0 ≤ pi, ei ≤ 200000).The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the co...
Print a single number in a single line — the minimum amount of effort Manao needs to use to rank in the top k. If no amount of effort can earn Manao such a rank, output number -1.
Consider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant ha...
Input: 3 21 11 42 2 | Output: 3
Beginner
0
1,413
602
179
3
1,903
B
1903B
B. StORage room
1,200
bitmasks; brute force; constructive algorithms; greedy
In Cyprus, the weather is pretty hot. Thus, Theofanis saw this as an opportunity to create an ice cream company. He keeps the ice cream safe from other ice cream producers by locking it inside big storage rooms. However, he forgot the password. Luckily, the lock has a special feature for forgetful people! It gives you ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^{3}\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^{3}\)) — the size of the hidden array.The next \(n\) lines describe the rows of \(M\), line \(i\) contains the table values \(M_{i,1}, M_{...
For each test case, if there is a solution print YES and an array that satisfies the property, otherwise print NO.If there are multiple solutions, print any of them.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 resp...
Input: 41040 3 3 53 0 3 73 3 0 75 7 7 050 7 7 5 57 0 3 2 67 3 0 3 75 2 3 0 45 6 7 4 030 0 10 0 01 0 0 | Output: YES 7 YES 1 3 2 5 YES 5 2 3 0 4 NO
Easy
4
781
556
326
19
804
A
804A
A. Find Amir
1,000
constructive algorithms; greedy; math
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j cost...
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.
Print single integer: the minimum cost of tickets needed to visit all schools.
In the first example we can buy a ticket between the schools that costs .
Input: 2 | Output: 0
Beginner
3
479
81
78
8
137
D
137D
D. Palindromes
1,900
dp; strings
Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite ga...
The first input line contains a non-empty string s which is the text of ""Storm and Calm"" (without spaces). The length of the string s does not exceed 500 characters. String s consists of uppercase and lowercase Latin letters. The second line contains a single number k (1 ≤ k ≤ |s|, where |s| represents the length of ...
Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than k palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character ""+"" (ASCII-code 43) to separate consecutiv...
Input: abacaba1 | Output: 0abacaba
Hard
2
1,372
334
513
1
1,956
C
1956C
C. Nene's Magical Matrix
1,600
constructive algorithms; greedy; math
The magical girl Nene has an \(n\times n\) matrix \(a\) filled with zeroes. The \(j\)-th element of the \(i\)-th row of matrix \(a\) is denoted as \(a_{i, j}\).She can perform operations of the following two types with this matrix: Type \(1\) operation: choose an integer \(i\) between \(1\) and \(n\) and a permutation ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of test cases follows.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 500\)) — the size of the matrix \(a\).It is guaranteed that the sum of \(n^2\) over a...
For each test case, in the first line output two integers \(s\) and \(m\) (\(0\leq m\leq 2n\)) — the maximum sum of the numbers in the matrix and the number of operations in your solution.In the \(k\)-th of the next \(m\) lines output the description of the \(k\)-th operation: an integer \(c\) (\(c \in \{1, 2\}\)) — th...
In the first test case, the maximum sum \(s=1\) can be obtained in \(1\) operation by setting \(a_{1, 1}:=1\).In the second test case, the maximum sum \(s=7\) can be obtained in \(3\) operations as follows: It can be shown that it is impossible to make the sum of the numbers in the matrix larger than \(7\).
Input: 212 | Output: 1 1 1 1 1 7 3 1 1 1 2 1 2 1 2 2 1 1 2
Medium
3
1,304
367
798
19
2,121
G
2121G
G. Gangsta
1,900
data structures; divide and conquer; math; sortings
You are given a binary string \(s_1s_2 \ldots s_n\) of length \(n\). A string \(s\) is called binary if it consists only of zeros and ones.For a string \(p\), we define the function \(f(p)\) as the maximum number of occurrences of any character in the string \(p\). For example, \(f(00110) = 3\), \(f(01) = 1\).You need ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows their descriptions.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the binary string.The second line o...
For each test case, output the sum \(f(s_ls_{l+1} \ldots s_r)\) for all pairs \(1 \leq l \leq r \leq n\).
In the first test case, the string \(s\) has one substring, and the value \(f(0) = 1\).In the second test case, all substrings of the string \(s\) are \(0\), \(01\), \(1\). And the answer is \(1 + 1 + 1 = 3\), respectively.In the third test case, all substrings of the string \(s\) are \(0\), \(01\), \(011\), \(0110\), ...
Input: 6102014011061100018100111001101011011100 | Output: 1 3 14 40 78 190
Hard
4
406
524
105
21
1,342
F
1342F
F. Make It Ascending
3,000
bitmasks; brute force; dp
You are given an array \(a\) consisting of \(n\) elements. You may apply several operations (possibly zero) to it.During each operation, you choose two indices \(i\) and \(j\) (\(1 \le i, j \le n\); \(i \ne j\)), increase \(a_j\) by \(a_i\), and remove the \(i\)-th element from the array (so the indices of all elements...
The first line contains one integer \(T\) (\(1 \le T \le 10000\)) — the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(1 \le n \le 15\)) — the number of elements in the initial array \(a\).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(...
For each test case, print the answer as follows:In the first line, print \(k\) — the minimum number of operations you have to perform. Then print \(k\) lines, each containing two indices \(i\) and \(j\) for the corresponding operation. Note that the numeration of elements in the array changes after removing elements fr...
In the first test case, the sequence of operations changes \(a\) as follows:\([2, 1, 3, 5, 1, 2, 4, 5] \rightarrow [2, 1, 3, 5, 1, 4, 7] \rightarrow [1, 3, 5, 1, 6, 7] \rightarrow [2, 3, 5, 6, 7]\).
Input: 4 8 2 1 3 5 1 2 4 5 15 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 2 3 3 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Output: 3 6 8 1 6 4 1 7 1 15 1 13 1 11 1 9 1 7 1 5 1 3 1 2 1 0
Master
3
649
948
404
13
1,195
B
1195B
B. Sport Mafia
1,000
binary search; brute force; math
Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia. For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs \(n\) actions. The first action performed is to put a single candy into the box. For each of the remain...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^9\); \(0 \le k \le 10^9\)) — the total number of moves and the number of candies in the box at the end. It's guaranteed, that for the given \(n\) and \(k\) the answer exists.
Print a single integer — the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers — the answer is unique for any input data.
In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate \(0\) candies.In the second example the possible sequence of Alya's actions looks as follows: put \(1\) candy, put \(2\) candies, eat a candy, eat a candy, put \(3\) candi...
Input: 1 1 | Output: 0
Beginner
3
1,560
245
179
11
1,917
D
1917D
D. Yet Another Inversions Problem
2,300
combinatorics; data structures; dp; implementation; math; number theory
You are given a permutation \(p_0, p_1, \ldots, p_{n-1}\) of odd integers from \(1\) to \(2n-1\) and a permutation \(q_0, q_1, \ldots, q_{k-1}\) of integers from \(0\) to \(k-1\).An array \(a_0, a_1, \ldots, a_{nk-1}\) of length \(nk\) is defined as follows: \(a_{i \cdot k+j}=p_i \cdot 2^{q_j}\) for all \(0 \le i < n\)...
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 two integers \(n\) and \(k\) (\(1 \le n, k \le 2 \cdot 10^5\)) — the lengths of arrays \(p\) and \(q\).The second line of each test case contains \(n\) distinct integers \(p_0, p_1,...
For each test case, output one integer: the number of inversions in array \(a\) modulo \(998\,244\,353\).
In the first test case, array \(a\) is equal to \([3, 6, 5, 10, 1, 2]\). There are \(9\) inversions in it: \((0, 4)\), \((0, 5)\), \((1, 2)\), \((1, 4)\), \((1, 5)\), \((2, 4)\), \((2, 5)\), \((3, 4)\), \((3, 5)\). Note that these are pairs \((i, j)\) such that \(i < j\) and \(a_i > a_j\).In the second test case, array...
Input: 43 23 5 10 13 41 3 53 2 0 11 510 1 2 3 48 35 1 7 11 15 3 9 132 0 1 | Output: 9 25 0 104
Expert
6
796
696
105
19
1,624
A
1624A
A. Plus One on the Subset
800
math
Polycarp got an array of integers \(a[1 \dots n]\) as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become \(a_1=a_2=\dots=a_n\)). In one operation, he can take some indices in the array and increase the elements of the arra...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases in the test.The following are descriptions of the input test cases.The first line of the description of each test case contains one integer \(n\) (\(1 \le n \le 50\)) — the array \(a\).The second line of the de...
For each test case, print one integer — the minimum number of operations to make all elements of the array \(a\) equal.
First test case: \(a=[3,4,2,4,1,2]\) take \(a_3, a_5\) and perform an operation plus one on them, as a result we get \(a=[3,4,3,4,2,2]\). \(a=[3,4,3,4,2,2]\) we take \(a_1, a_5, a_6\) and perform an operation on them plus one, as a result we get \(a=[4,4,3,4,3,3]\). \(a=[4,4,3,4,3,3]\) we take \(a_3, a_5, a_6\) and per...
Input: 363 4 2 4 1 231000 1002 998212 11 | Output: 3 4 1
Beginner
1
755
452
119
16
1,879
A
1879A
A. Rigged!
800
greedy
Monocarp organizes a weightlifting competition. There are \(n\) athletes participating in the competition, the \(i\)-th athlete has strength \(s_i\) and endurance \(e_i\). The \(1\)-st athlete is Monocarp's friend Polycarp, and Monocarp really wants Polycarp to win.The competition will be conducted as follows. The jury...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 100\)) — the number of athletes. Then \(n\) lines follow, the \(i\)-th of them contains two integers \(s_i\) and \(e_i\) (\(1 \le s_i \le 10^9\); \(1 \le ...
For each test case, print the answer as follows: if the answer exists, print one integer — the value of \(w\) meeting the constraints. The integer you print should satisfy \(1 \le w \le 10^9\). It can be shown that if the answer exists, at least one such value of \(w\) exists as well. If there are multiple answers, you...
The first test case of the example is described in the statement.
Input: 347 49 34 62 224 6100 10021337 31337 3 | Output: 5 -1 -1
Beginner
1
1,610
392
380
18
1,536
E
1536E
E. Omkar and Forest
2,300
combinatorics; graphs; math; shortest paths
Omkar's most recent follower, Ajit, has entered the Holy Forest. Ajit realizes that Omkar's forest is an \(n\) by \(m\) grid (\(1 \leq n, m \leq 2000\)) of some non-negative integers. Since the forest is blessed by Omkar, it satisfies some special conditions: For any two adjacent (sharing a side) cells, the absolute va...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 2000, nm \geq 2\)) – the dimensions of the forest.\(n\) lines follow, e...
For each test case, print one integer: the number of valid configurations modulo \(10^9+7\).
For the first test case, the two valid assignments are\(0000\\ 0000\\ 0000\)and\(0000\\ 0010\\ 0000\)
Input: 4 3 4 0000 00#0 0000 2 1 # # 1 2 ## 6 29 ############################# #000##0###0##0#0####0####000# #0#0##00#00##00####0#0###0#0# #0#0##0#0#0##00###00000##00## #000##0###0##0#0##0###0##0#0# ############################# | Output: 2 3 3 319908071
Expert
4
1,086
574
92
15
2,002
D1
2002D1
D1. DFS Checker (Easy Version)
1,900
brute force; data structures; dfs and similar; graphs; hashing; trees
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on \(n\) and \(q\) are lower. You can make hacks only if both versions of the problem are solved.You are given a perfect binary tree\(^\dagger\) consisting of \(n\) vertices. The vertices are numbered f...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(q\) (\(3\le n\le 65\,535\), \(2\le q\le 5 \cdot 10^4\)) — the number of vertices in the tree and ...
For each test case, print \(q\) lines corresponding to the \(q\) queries. For each query, output \(\texttt{YES}\) if there is a DFS order that exactly equals the current permutation, and output \(\texttt{NO}\) otherwise.You can output \(\texttt{Yes}\) and \(\texttt{No}\) in any case (for example, strings \(\texttt{yEs}...
In the first test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,3,2],[1,2,3],[3,2,1]\), respectively. The first two permutations are valid DFS orders; the third is not a DFS order.In the second test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,2,5,4,3...
Input: 23 31 11 2 32 33 21 37 41 1 2 2 3 31 2 3 4 5 6 73 52 53 74 6 | Output: YES YES NO YES NO NO YES
Hard
6
1,326
1,016
423
20
1,386
B
1386B
B. Mixture
2,900
*special; data structures; geometry; math; sortings
Serge, the chef of the famous restaurant ""Salt, Pepper & Garlic"" is trying to obtain his first Michelin star. He has been informed that a secret expert plans to visit his restaurant this evening.Even though the expert's name hasn't been disclosed, Serge is certain he knows which dish from the menu will be ordered as ...
The first row contains three non-negative integers \(S_f\), \(P_f\) and \(G_f\) (\(0 \leq S_f, P_f, G_f\); \(0 < S_f+P_f+G_f \leq 10^6\)) describing the amount of salt, pepper and garlic powder in the expert's favourite mixture. For any real \(\alpha>0\), \((\alpha{S_f}, \alpha{P_f}, \alpha{G_f})\) also is an expert's ...
Output \(N\) rows. The \(j\)-th row (\(1 \leq j \leq N\)) should contain the number \(x_j\), the smallest number of bottles needed to prepare a mixture with the expert's favourite proportions of salt, pepper and garlic powder using the bottles available after the first \(j\) changes on the shelf, or \(0\) if it is not ...
Pay attention that in the example, bottles \(1\) and \(3\) contain the same proportions of salt, pepper and garlic powder.
Input: 1 2 3 6 A 5 6 7 A 3 10 17 R 1 A 15 18 21 A 5 10 15 R 3 | Output: 0 2 0 2 1 1
Master
5
2,033
1,251
329
13
413
E
413E
E. Maze 2D
2,200
data structures; divide and conquer
The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2 × n maze.Imagine a maze that looks like a 2 × n rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from ...
The first line contains two integers, n and m (1 ≤ n ≤ 2·105; 1 ≤ m ≤ 2·105) — the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle).Each of the next m lines contains two intege...
Print m lines. In the i-th line print the answer to the i-th request — either the size of the shortest path or -1, if we can't reach the second cell from the first one.
Input: 4 7.X.....X5 11 37 71 46 14 75 7 | Output: 1405222
Hard
2
898
756
168
4
1,354
E
1354E
E. Graph Coloring
2,100
dfs and similar; dp; graphs
You are given an undirected graph without self-loops or multiple edges which consists of \(n\) vertices and \(m\) edges. Also you are given three integers \(n_1\), \(n_2\) and \(n_3\).Can you label each vertex with one of three numbers 1, 2 or 3 in such way, that: Each vertex should be labeled by exactly one number 1, ...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 5000\); \(0 \le m \le 10^5\)) — the number of vertices and edges in the graph.The second line contains three integers \(n_1\), \(n_2\) and \(n_3\) (\(0 \le n_1, n_2, n_3 \le n\)) — the number of labels 1, 2 and 3, respectively. It's guaranteed that \(n...
If valid labeling exists then print ""YES"" (without quotes) in the first line. In the second line print string of length \(n\) consisting of 1, 2 and 3. The \(i\)-th letter should be equal to the label of the \(i\)-th vertex.If there is no valid labeling, print ""NO"" (without quotes).
Input: 6 3 2 2 2 3 1 5 4 2 5 | Output: YES 112323
Hard
3
691
608
287
13
816
B
816B
B. Karen and Coffee
1,400
binary search; data structures; implementation
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed ""The Art of the Covfefe"".She knows n coffee rec...
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.The next n lines describe the recipes. Specifically,...
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
In the first test case, Karen knows 3 recipes. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissibl...
Input: 3 2 491 9492 9797 9992 9493 9795 9690 100 | Output: 3304
Easy
3
778
723
145
8
592
A
592A
A. PawnChess
1,200
implementation
Galois is one of the strongest chess players of Byteforces. He has even invented a new variant of chess, which he named «PawnChess».This new game is played on a board consisting of 8 rows and 8 columns. At the beginning of every game some black and white pawns are placed on the board. The number of black pawns placed i...
The input consists of the board description given in eight lines, each line contains eight characters. Character 'B' is used to denote a black pawn, and character 'W' represents a white pawn. Empty cell is marked with '.'. It's guaranteed that there will not be white pawns on the first row neither black pawns on the la...
Print 'A' if player A wins the game on the given board, and 'B' if player B will claim the victory. Again, it's guaranteed that there will always be a winner on the given board.
In the first sample player A is able to complete his goal in 3 steps by always moving a pawn initially located at (4, 5). Player B needs at least 5 steps for any of his pawns to reach the row 8. Hence, player A will be the winner.
Input: .................B....B.....W.............W..................... | Output: A
Easy
1
1,874
327
177
5
274
B
274B
B. Zero Tree
1,800
dfs and similar; dp; greedy; trees
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T.You're given a tree w...
The first line of the input contains n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi) indicating there's an edge between vertices ai and bi. It's guaranteed that the input graph is a tree. The last line of the input contains a list of n space-separated integers v1,...
Print the minimum number of operations needed to solve the task.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.
Input: 31 21 31 -1 1 | Output: 3
Medium
4
886
346
213
2
1,207
A
1207A
A. There Are Two Types Of Burgers
800
brute force; greedy; implementation; math
There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have \(b\) buns, \(p\) beef patties and \(f\) chicken cutlets in your restaurant. You can sell one hamb...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) – the number of queries.The first line of each query contains three integers \(b\), \(p\) and \(f\) (\(1 \le b, ~p, ~f \le 100\)) — the number of buns, beef patties and chicken cutlets in your restaurant.The second line of each query contains two integers ...
For each query print one integer — the maximum profit you can achieve.
In first query you have to sell two hamburgers and three chicken burgers. Your income is \(2 \cdot 5 + 3 \cdot 10 = 40\).In second query you have to ell one hamburgers and two chicken burgers. Your income is \(1 \cdot 10 + 2 \cdot 12 = 34\).In third query you can not create any type of burgers because because you have ...
Input: 3 15 2 3 5 10 7 5 2 10 12 1 100 100 100 100 | Output: 40 34 0
Beginner
4
476
423
70
12
1,031
C
1031C
C. Cram Time
1,600
greedy
In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.Lesha knows that today he can study for at most \(a\) hours, and he will have \(b\) hours to study t...
The only line of input contains two integers \(a\) and \(b\) (\(0 \leq a, b \leq 10^{9}\)) — the number of hours Lesha has today and the number of hours Lesha has tomorrow.
In the first line print a single integer \(n\) (\(0 \leq n \leq a\)) — the number of lecture notes Lesha has to read in the first day. In the second line print \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq a\)), the sum of all \(p_i\) should not exceed \(a\).In the third line print a single integ...
In the first example Lesha can read the third note in \(3\) hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending \(3\) hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the th...
Input: 3 3 | Output: 13 22 1
Medium
1
1,264
172
663
10
680
A
680A
A. Bear and Five Cards
800
constructive algorithms; implementation
A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.He is allowed to at most once discard two or three cards with t...
The only line of the input contains five integers t1, t2, t3, t4 and t5 (1 ≤ ti ≤ 100) — numbers written on cards.
Print the minimum possible sum of numbers written on remaining cards.
In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following. Do nothing and the sum would be 7 + 3 + 7 + 3 + 20 = 40. Remove two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26. Remove two cards with a number 3. The remaining sum would be 7 + 7 + 20 = 34. You...
Input: 7 3 7 3 20 | Output: 26
Beginner
2
535
114
69
6
1,761
F1
1761F1
F1. Anti-median (Easy Version)
3,100
dp; math
This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if all versions of the problem are solved.Let's call an array \(a\) of odd length \(2m+1\) (with \(m \ge 1\)) bad, if element \(a_{m+1}\) is equal to the median of this array. In oth...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) \((2 \le n \le 1000)\) — the length of the permutation.The second line of each test case contains \(n\) integers \(p_1,...
For each test case, output a single integer — the number of ways to set unknown values to obtain an anti-median permutation, modulo \(10^9+7\).
In the first test case, both \([1, 2]\) and \([2, 1]\) are anti-median.In the second test case, permutations \([1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2]\) are anti-median. The remaining two permutations, \([1, 2, 3]\), \([3, 2, 1]\), are bad arrays on their own, as their median, \(2\), is in their middle.In the third ...
Input: 52-1 -13-1 -1 -141 2 3 46-1 -1 3 4 -1 -18-1 -1 -1 -1 -1 -1 -1 -1 | Output: 2 4 0 1 316
Master
2
764
655
143
17
298
A
298A
A. Snow Footprints
1,300
greedy; implementation
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint ...
The first line of the input contains integer n (3 ≤ n ≤ 1000).The second line contains the description of the road — the string that consists of n characters. Each character will be either ""."" (a block without footprint), or ""L"" (a block with a left footprint), ""R"" (a block with a right footprint).It's guaranteed...
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
The first test sample is the one in the picture.
Input: 9..RRLL... | Output: 3 4
Easy
2
773
495
126
2
938
D
938D
D. Buy a Ticket
2,000
data structures; graphs; shortest paths
Musicians of a popular band ""Flayer"" have announced that they are going to ""make their exit"" with a world tour. Of course, they will visit Berland as well.There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from...
The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).Then m lines follow, i-th contains three integers vi, ui and wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v...
Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).
Input: 4 21 2 42 3 76 20 1 25 | Output: 6 14 1 25
Hard
3
1,139
461
221
9
1,012
C
1012C
C. Hills
1,900
dp
Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appeara...
The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence.Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence.
Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses.
In the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decre...
Input: 51 1 1 1 1 | Output: 1 2 2
Hard
1
1,207
202
179
10
464
B
464B
B. Restore Cube
2,000
brute force; geometry
Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — c...
Each of the eight lines contains three space-separated integers — the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value.
If there is a way to restore the cube, then print in the first line ""YES"". In each of the next eight lines print three integers — the restored coordinates of the points. The numbers in the i-th output line must be a permutation of the numbers in i-th input line. The numbers should represent the vertices of a cube wit...
Input: 0 0 00 0 10 0 10 0 10 1 10 1 10 1 11 1 1 | Output: YES0 0 00 0 10 1 01 0 00 1 11 0 11 1 01 1 1
Hard
2
995
185
510
4
1,952
F
1952F
F. Grid
0
*special; brute force
The input contains \(21\) lines, each containing \(21\) characters \(\texttt{0}\) or \(\texttt{1}\).
Input: 1111111010111011111111000001000110010000011011101011010010111011011101011001010111011011101010010010111011000001001111010000011111111010101011111110000000000011000000001111001011111100111010001110101011001101011111011011010010000110010010000010000110001111011100001110010110000000010010011111001111111000011010100...
Beginner
2
0
100
0
19
996
A
996A
A. Hit the Lottery
800
dp; greedy
Allen has a LOT of money. He has \(n\) dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are \(1\), \(5\), \(10\), \(20\), \(100\). What is the minimum number of bills Allen could receive after withdrawing his entire ba...
The first and only line of input contains a single integer \(n\) (\(1 \le n \le 10^9\)).
Output the minimum number of bills that Allen could receive.
In the first sample case, Allen can withdraw this with a \(100\) dollar bill, a \(20\) dollar bill, and a \(5\) dollar bill. There is no way for Allen to receive \(125\) dollars in one or two bills.In the second sample case, Allen can withdraw two \(20\) dollar bills and three \(1\) dollar bills.In the third sample cas...
Input: 125 | Output: 3
Beginner
2
326
88
60
9
810
B
810B
B. Summer sell-off
1,300
greedy; sortings
Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.Shop, where Noora is working, has a plan on the following n days. For each day sales manager k...
The first line contains two integers n and f (1 ≤ n ≤ 105, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out.Each line of the following n subsequent lines contains two integers ki, li (0 ≤ ki, li ≤ 109) denoting the number of products on the shelves of th...
Print a single integer denoting the maximal number of products that shop can sell.
In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total 1 + 5 + 2 + 2 = 10 product units.In the sec...
Input: 4 22 13 52 31 5 | Output: 10
Easy
2
1,437
408
82
8
1,975
D
1975D
D. Paint the Tree
1,700
brute force; dfs and similar; dp; greedy; shortest paths; trees
378QAQ has a tree with \(n\) vertices. Initially, all vertices are white. There are two chess pieces called \(P_A\) and \(P_B\) on the tree. \(P_A\) and \(P_B\) are initially located on vertices \(a\) and \(b\) respectively. In one step, 378QAQ will do the following in order: Move \(P_A\) to a neighboring vertex. If th...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1\leq n\leq 2\cdot 10^5\)).The second line of each test case contains two integers \(a\) and \(b...
For each test case, output the minimum number of steps to paint all vertices blue.
In the first test case, 378QAQ can paint all vertices blue in the following order: Initially, \(P_A\) is located on the vertex \(1\), and \(P_B\) is located on the vertex \(2\). The vertex \(1\) is painted red and the vertex \(2\) is white. 378QAQ moves \(P_A\) to the vertex \(2\) and paints it red. Then 378QAQ moves \...
Input: 321 21 251 21 21 31 41 585 47 11 51 88 37 28 63 4 | Output: 2 8 13
Medium
6
780
647
82
19
1,146
H
1146H
H. Satanic Panic
2,900
dp; geometry
You are given a set of \(n\) points in a 2D plane. No three points are collinear.A pentagram is a set of \(5\) points \(A,B,C,D,E\) that can be arranged as follows. Note the length of the line segments don't matter, only that those particular intersections exist.Count the number of ways to choose \(5\) points from the ...
The first line contains an integer \(n\) (\(5 \leq n \leq 300\)) — the number of points.Each of the next \(n\) lines contains two integers \(x_i, y_i\) (\(-10^6 \leq x_i,y_i \leq 10^6\)) — the coordinates of the \(i\)-th point. It is guaranteed that no three points are collinear.
Print a single integer, the number of sets of \(5\) points that form a pentagram.
A picture of the first sample: A picture of the second sample: A picture of the third sample:
Input: 5 0 0 0 2 2 0 2 2 1 3 | Output: 1
Master
2
352
280
81
11
817
F
817F
F. MEX Queries
2,300
binary search; data structures; trees
You are given a set of integer numbers, initially it is empty. You should perform n queries.There are three different types of queries: 1 l r — Add all missing numbers from the interval [l, r] 2 l r — Remove all present numbers from the interval [l, r] 3 l r — Invert the interval [l, r] — add all missing and remove all...
The first line contains one integer number n (1 ≤ n ≤ 105).Next n lines contain three integer numbers t, l, r (1 ≤ t ≤ 3, 1 ≤ l ≤ r ≤ 1018) — type of the query, left and right bounds.
Print MEX of the set after each query.
Here are contents of the set after each query in the first example: {3, 4} — the interval [3, 4] is added {1, 2, 5, 6} — numbers {3, 4} from the interval [1, 6] got deleted and all the others are added {5, 6} — numbers {1, 2} got deleted
Input: 31 3 43 1 62 1 3 | Output: 131
Expert
3
495
183
38
8
1,394
E
1394E
E. Boboniu and Banknote Collection
3,500
strings
No matter what trouble you're in, don't be afraid, but face it with a smile.I've made another billion dollars! — BoboniuBoboniu has issued his currencies, named Bobo Yuan. Bobo Yuan (BBY) is a series of currencies. Boboniu gives each of them a positive integer identifier, such as BBY-1, BBY-2, etc.Boboniu has a BBY col...
The first line contains an integer \(n\) (\(1\le n\le 10^5\)).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\le a_i\le n\)).
Print \(n\) integers. The \(i\)-th of them should be equal to the maximum number of folds of \([a_1,a_2,\ldots,a_i]\).
Formally, for a sequence \(a\) of length \(n\), let's define the folding sequence as a sequence \(b\) of length \(n\) such that: \(b_i\) (\(1\le i\le n\)) is either \(1\) or \(-1\). Let \(p(i)=[b_i=1]+\sum_{j=1}^{i-1}b_j\). For all \(1\le i<j\le n\), if \(p(i)=p(j)\), then \(a_i\) should be equal to \(a_j\). (\([A]\) i...
Input: 9 1 2 3 3 2 1 4 4 1 | Output: 0 0 0 1 1 1 1 2 2
Master
1
1,370
145
118
13
714
B
714B
B. Filya and Homework
1,200
implementation; sortings
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x fr...
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print ""NO"" (without quotes) in the only line of the output. Otherwise print ""YES"" (without quotes).
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
Input: 51 3 3 2 1 | Output: YES
Easy
2
591
206
217
7
1,109
D
1109D
D. Sasha and Interesting Fact from Graph Theory
2,400
brute force; combinatorics; dp; math; trees
Once, during a lesson, Sasha got bored and decided to talk with his friends. Suddenly, he saw Kefa. Since we can talk endlessly about Kefa, we won't even start doing that. The conversation turned to graphs. Kefa promised Sasha to tell him about one interesting fact from graph theory if Sasha helps Kefa to count the num...
The first line contains four integers \(n\), \(m\), \(a\), \(b\) (\(2 \le n \le 10^6\), \(1 \le m \le 10^6\), \(1 \le a, b \le n\), \(a \neq b\)) — the number of vertices in the tree, the maximum weight of an edge and two Kefa's favorite vertices.
Print one integer — the number of beautiful trees modulo \(10^9+7\).
There are \(5\) beautiful trees in the first example:In the second example the following trees are beautiful:
Input: 3 2 1 3 | Output: 5
Expert
5
1,399
247
68
11
105
D
105D
D. Entertaining Geodetics
2,700
brute force; dsu; implementation
The maps in the game are divided into square cells called Geo Panels. Some of these panels are painted. We shall assume that the Geo Panels without color are painted the transparent color. Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent c...
The first line contains two integers n and m (1 ≤ n, m ≤ 300) — the height and the width of the map (in cells).Then follow n line; each of them contains m numbers — the Geo Panels' colors.Then follow n more lines; each of them contains m numbers — the Geo Symbols' description. -1 means that the given position contains ...
Print the single number — the total number of repaintings after the Geo Symbol is eliminated. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (you may also use the %I64d specificator).
All actions of the sample you can see on the following picture: If your browser does not support APNG and you see just static image, you can see GIF version of this image by the following link:http://assets.codeforces.com/images/geo_slow.gif
Input: 5 59 0 1 1 00 0 3 2 01 1 1 3 01 1 1 3 00 1 2 0 3-1 1 -1 3 -1-1 -1 -1 0 -1-1 -1 -1 -1 -1-1 2 3 -1 -1-1 -1 -1 -1 24 2 | Output: 35
Master
3
2,029
837
257
1
178
A3
178A3
A3. Educational Game
1,100
greedy
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence)...
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.The input limitations for getting 20 points are: 1 ≤ n ≤ 300 The input limitations for getting 50 points are: 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: 1 ≤ n ≤ 105
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Input: 41 0 1 2 | Output: 113
Easy
1
1,085
316
314
1
2,027
E1
2027E1
E1. Bit Game (Easy Version)
2,800
bitmasks; brute force; games; math
This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack.Alice and Bob are playing a familiar game where they take turns removing stones from \(n\) pil...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains \(n\) (\(1 \le n \le 10^4\)) — the number of piles.The second line of each test case contains \(n\) integers \(a_1, a...
Print a single line with the winner's name. If Alice wins, print ""Alice"", otherwise print ""Bob"" (without quotes).
In the first test case, neither player can take any stones from the first pile since there is no value of \(d\) satisfying the conditions. For the second pile, to begin with, Alice can remove between \(1\) and \(6\) stones. No matter which move Alice performs, Bob can remove the rest of the stones on his turn. After Bo...
Input: 721 610 7310 8 1525 4 1448 32 65 647 45 126 94320 40 123 55 1512345 9876 86419 8641 16789 54321 7532 97532 1220 6444 61357 109 5569 90 85 | Output: Bob Bob Bob Bob Bob Alice Alice
Master
4
887
555
117
20
1,421
E
1421E
E. Swedish Heroes
2,700
brute force; dp; implementation
While playing yet another strategy game, Mans has recruited \(n\) Swedish heroes, whose powers which can be represented as an array \(a\).Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two cons...
The first line contains a single integer \(n\) (\(1 \le n \le 200000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)) — powers of the heroes.
Print the largest possible power he can achieve after \(n-1\) operations.
Suitable list of operations for the first sample:\([5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]\)
Input: 4 5 6 7 8 | Output: 26
Master
3
779
190
73
14
1,003
A
1003A
A. Polycarp's Pockets
800
implementation
Polycarp has \(n\) coins, the value of the \(i\)-th coin is \(a_i\). Polycarp wants to distribute all the coins between his pockets, but he cannot put two coins with the same value into the same pocket.For example, if Polycarp has got six coins represented as an array \(a = [1, 2, 4, 3, 3, 2]\), he can distribute the c...
The first line of the input contains one integer \(n\) (\(1 \le n \le 100\)) — the number of coins.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)) — values of coins.
Print only one integer — the minimum number of pockets Polycarp needs to distribute all the coins so no two coins with the same value are put into the same pocket.
Input: 61 2 4 3 3 2 | Output: 2
Beginner
1
483
219
163
10
926
G
926G
G. Large Bouquets
1,500
A flower shop has got n bouquets, and the i-th bouquet consists of ai flowers. Vasya, the manager of the shop, decided to make large bouquets from these bouquets. Vasya thinks that a bouquet is large if it is made of two or more initial bouquets, and there is a constraint: the total number of flowers in a large bouquet...
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of initial bouquets.The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the number of flowers in each of the initial bouquets.
Print the maximum number of large bouquets Vasya can make.
In the first example Vasya can make 2 large bouquets. For example, the first bouquet can contain the first and the fifth initial bouquets (the total number of flowers is then equal to 9), and the second bouquet can consist of the second and the third initial bouquets (the total number of flowers is then equal to 7). Th...
Input: 52 3 4 2 7 | Output: 2
Medium
0
587
234
58
9
471
B
471B
B. MUH and Important Things
1,300
implementation; sortings
It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order...
The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.
In the first line print ""YES"" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line ""NO"" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the ord...
In the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.In the second sample there are only two ...
Input: 41 3 3 1 | Output: YES1 4 2 3 4 1 2 3 4 1 3 2
Easy
2
945
247
504
4
1,297
G
1297G
G. M-numbers
0
*special; dp; math
For a given positive integer \(m\), a positive number is called a \(m\)-number if the product of its digits is \(m\). For example, the beginning of a series of \(24\)-numbers are as follows: \(38\), \(46\), \(64\), \(83\), \(138\), \(146\), \(164\), \(183\), \(226\) ...You are given a positive integer \(m\) and \(k\). ...
A single line of input contains two integers \(m\) and \(k\) (\(2 \le m \le 10^9\), \(1 \le k \le 10^9\)).
Print the desired number — \(k\)-th among all \(m\)-numbers if \(m\)-numbers are sorted in ascending order. If the answer does not exist, print -1.
Input: 24 9 | Output: 226
Beginner
3
406
106
147
12
1,081
B
1081B
B. Farewell Party
1,500
constructive algorithms; implementation
Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.Chouti remembered that \(n\) persons took part in that party. To make the party funnier, each person wore one hat among \(n\) kin...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)), the number of persons in the party.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n-1\)), the statements of people.
If there is no solution, print a single line ""Impossible"".Otherwise, print ""Possible"" and then \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\)).If there are multiple answers, print any of them.
In the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind \(1\).In the answer to the second example, the first and the second person wore the hat with type \(1\) and all other wore a hat of type \(2\).So the first two persons will sa...
Input: 30 0 0 | Output: Possible1 1 1
Medium
2
954
222
211
10
1,915
D
1915D
D. Unnatural Language Processing
900
greedy; implementation; strings
Lura was bored and decided to make a simple language using the five letters \(\texttt{a}\), \(\texttt{b}\), \(\texttt{c}\), \(\texttt{d}\), \(\texttt{e}\). There are two types of letters: vowels — the letters \(\texttt{a}\) and \(\texttt{e}\). They are represented by \(\textsf{V}\). consonants — the letters \(\texttt{b...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the word.The second line of each t...
For test case, output a string denoting the word split into syllables by inserting a dot \(\texttt{.}\) between every pair of adjacent syllables.If there are multiple possible splittings, output any of them. The input is given in such a way that at least one possible splitting exists.
Input: 68bacedbab4baba13daddecabeddad3dac6dacdac22dababbabababbabbababba | Output: ba.ced.bab ba.ba dad.de.ca.bed.dad dac dac.dac da.bab.ba.ba.bab.bab.ba.bab.ba
Beginner
3
1,054
686
285
19
1,713
A
1713A
A. Traveling Salesman Problem
800
geometry; greedy; implementation
You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point \((x, y)\), you can: go left, and move to \((x - 1, y)\), or go right, and move to \((x + 1, y)\), or go up, a...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) — the number of boxes.The \(i\)-th line of the following \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-100 \le x_i, y_i \le 1...
For each test case output a single integer — the minimum number of moves required.
In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below. $$$\((0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0)\)\( In the second test case, a possible sequence of moves that ...
Input: 340 -21 0-1 00 230 2-3 00 -110 0 | Output: 12 12 0
Beginner
3
793
481
82
17
832
A
832A
A. Sasha and Sticks
800
games; math
It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends.Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players take ...
The first line contains two integers n and k (1 ≤ n, k ≤ 1018, k ≤ n) — the number of sticks drawn by Sasha and the number k — the number of sticks to be crossed out on each turn.
If Sasha wins, print ""YES"" (without quotes), otherwise print ""NO"" (without quotes).You can print each letter in arbitrary case (upper of lower).
In the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins.In the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha...
Input: 1 1 | Output: YES
Beginner
2
659
179
148
8
1,804
E
1804E
E. Routing
2,400
bitmasks; brute force; dfs and similar; dp; graphs
Ada operates a network that consists of \(n\) servers and \(m\) direct connections between them. Each direct connection between a pair of distinct servers allows bidirectional transmission of information between these two servers. Ada knows that these \(m\) direct connections allow to directly or indirectly transmit in...
The first line of the input contains two integers \(n\) and \(m\) (\(2 \leq n \leq 20\), \(n - 1 \leq m \leq \frac{n \cdot (n - 1)}{2}\)) — the number of servers and the number of direct connections in the given network.Then follow \(m\) lines containing two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\), \(u...
If there is no way to assign an auxiliary server \(a(u)\) for each server \(u\) in such a way that WRP will be able to find a path from any server \(u\) to any other server \(v\), print ""No"" in the only line of the output.Otherwise, print ""Yes"" in the first line of the output. In the second line of the output print...
Input: 6 7 1 2 2 3 3 1 4 5 5 6 4 6 2 5 | Output: Yes 2 5 2 5 2 5
Expert
5
1,821
622
705
18
1,783
B
1783B
B. Matrix of Differences
1,100
constructive algorithms; math
For a square matrix of integers of size \(n \times n\), let's define its beauty as follows: for each pair of side-adjacent elements \(x\) and \(y\), write out the number \(|x-y|\), and then find the number of different numbers among them.For example, for the matrix \(\begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix}\) the nu...
The first line contains a single integer \(t\) (\(1 \le t \le 49\)) – the number of test cases.The first (and only) line of each test case contains a single integer \(n\) (\(2 \le n \le 50\)).
For each test case, print \(n\) rows of \(n\) integers — a matrix of integers of size \(n \times n\), where each number from \(1\) to \(n^2\) occurs exactly once, such that its beauty is the maximum possible among all such matrices. If there are multiple answers, print any of them.
Input: 223 | Output: 1 3 4 2 1 3 4 9 2 7 5 8 6
Easy
2
727
192
282
17
2,041
B
2041B
B. Bowling Frame
1,200
binary search; brute force; math
Bowling is a national sport in Taiwan; everyone in the country plays the sport on a daily basis since their youth. Naturally, there are a lot of bowling alleys all over the country, and the competition between them is as intense as you can imagine.Maw-Shang owns one such bowling alley. To stand out from other competito...
The first line of the input contains a single integer \(t\), the number of test cases. Each of the following \(t\) lines contains two integers \(w\) and \(b\), the number of white and black pins, respectively. \(1 \leq t \leq 100\) \(0 \leq w, b \leq 10^9\)
For each test case, output in a single line the side length \(k\) of the largest pin satisfying Maw-Shang's requirement you can build with the given pins.
Input: 41 23 23 312 0 | Output: 2 2 3 4
Easy
3
2,112
257
154
20
1,665
B
1665B
B. Array Cloning Technique
900
constructive algorithms; greedy; sortings
You are given an array \(a\) of \(n\) integers. Initially there is only one copy of the given array.You can do operations of two types: Choose any array and clone it. After that there is one more copy of the chosen array. Swap two elements from any two copies (maybe in the same copy) on any positions. You need to find ...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array \(a\).The second line of ea...
For each test case output a single integer — the minimal number of operations needed to create at least one copy where all elements are equal.
In the first test case all elements in the array are already equal, that's why the answer is \(0\).In the second test case it is possible to create a copy of the given array. After that there will be two identical arrays:\([ \ 0 \ 1 \ 3 \ 3 \ 7 \ 0 \ ]\) and \([ \ 0 \ 1 \ 3 \ 3 \ 7 \ 0 \ ]\)After that we can swap eleme...
Input: 61178960 1 3 3 7 02-1000000000 100000000044 3 2 152 5 7 6 371 1 1 1 1 1 1 | Output: 0 6 2 5 7 0
Beginner
3
406
530
142
16
1,167
F
1167F
F. Scalar Queries
2,300
combinatorics; data structures; math; sortings
You are given an array \(a_1, a_2, \dots, a_n\). All \(a_i\) are pairwise distinct.Let's define function \(f(l, r)\) as follows: let's define array \(b_1, b_2, \dots, b_{r - l + 1}\), where \(b_i = a_{l - 1 + i}\); sort array \(b\) in increasing order; result of the function \(f(l, r)\) is \(\sum\limits_{i = 1}^{r - l ...
The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) — the length of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\), \(a_i \neq a_j\) for \(i \neq j\)) — array \(a\).
Print one integer — the total sum of \(f\) for all subsegments of \(a\) modulo \(10^9+7\)
Description of the first example: \(f(1, 1) = 5 \cdot 1 = 5\); \(f(1, 2) = 2 \cdot 1 + 5 \cdot 2 = 12\); \(f(1, 3) = 2 \cdot 1 + 4 \cdot 2 + 5 \cdot 3 = 25\); \(f(1, 4) = 2 \cdot 1 + 4 \cdot 2 + 5 \cdot 3 + 7 \cdot 4 = 53\); \(f(2, 2) = 2 \cdot 1 = 2\); \(f(2, 3) = 2 \cdot 1 + 4 \cdot 2 = 10\); \(f(2, 4) = 2 \cdot 1 + ...
Input: 4 5 2 4 7 | Output: 167
Expert
4
497
240
89
11
702
D
702D
D. Road to Post Office
1,900
math
Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to d kilometers.Vasiliy's car is not new — it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but after k kilometers it will br...
The first line contains 5 positive integers d, k, a, b, t (1 ≤ d ≤ 1012; 1 ≤ k, a, b, t ≤ 106; a < b), where: d — the distance from home to the post office; k — the distance, which car is able to drive before breaking; a — the time, which Vasiliy spends to drive 1 kilometer on his car; b — the time, which Vasiliy spend...
Print the minimal time after which Vasiliy will be able to reach the post office.
In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and dri...
Input: 5 2 1 4 10 | Output: 14
Hard
1
701
404
81
7
49
C
49C
C. Disposition
1,700
constructive algorithms; math
Vasya bought the collected works of a well-known Berland poet Petya in n volumes. The volumes are numbered from 1 to n. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the disposition’s divisors — the positive integers i such that for at least one...
The first line contains number n (1 ≤ n ≤ 100000) which represents the number of volumes and free places.
Print n numbers — the sought disposition with the minimum divisor number. The j-th number (1 ≤ j ≤ n) should be equal to p(j) — the number of tome that stands on the j-th place. If there are several solutions, print any of them.
Input: 2 | Output: 2 1
Medium
2
698
105
228
0
1,204
C
1204C
C. Anna, Svyatoslav and Maps
1,700
dp; graphs; greedy; shortest paths
The main characters have been omitted to be short.You are given a directed unweighted graph without loops with \(n\) vertexes and a path in it (that path is not necessary simple) given by a sequence \(p_1, p_2, \ldots, p_m\) of \(m\) vertexes; for each \(1 \leq i < m\) there is an arc from \(p_i\) to \(p_{i+1}\).Define...
The first line contains a single integer \(n\) (\(2 \le n \le 100\)) — the number of vertexes in a graph. The next \(n\) lines define the graph by an adjacency matrix: the \(j\)-th character in the \(i\)-st line is equal to \(1\) if there is an arc from vertex \(i\) to the vertex \(j\) else it is equal to \(0\). It is ...
In the first line output a single integer \(k\) (\(2 \leq k \leq m\)) — the length of the shortest good subsequence. In the second line output \(k\) integers \(v_1\), \(\ldots\), \(v_k\) (\(1 \leq v_i \leq n\)) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutiv...
Below you can see the graph from the first example:The given path is passing through vertexes \(1\), \(2\), \(3\), \(4\). The sequence \(1-2-4\) is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the s...
Input: 4 0110 0010 0001 1000 4 1 2 3 4 | Output: 3 1 2 4
Medium
4
881
692
349
12
219
B
219B
B. Special Offer! Super Price 999 Bourles!
1,400
implementation
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he r...
The first line contains two integers p and d (1 ≤ p ≤ 1018; 0 ≤ d < p) — the initial price of scissors and the maximum possible price reduction.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.
Print the required price — the maximum price that ends with the largest number of nines and that is less than p by no more than d.The required number shouldn't have leading zeroes.
Input: 1029 102 | Output: 999
Easy
1
796
287
180
2
1,530
G
1530G
G. What a Reversal
3,300
constructive algorithms
You have two strings \(a\) and \(b\) of equal length \(n\) consisting of characters 0 and 1, and an integer \(k\).You need to make strings \(a\) and \(b\) equal.In one step, you can choose any substring of \(a\) containing exactly \(k\) characters 1 (and arbitrary number of characters 0) and reverse it. Formally, if \(...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2000\)). Description of the test cases follows.Each test case consists of three lines. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2000\); \(0 \le k \le n\)).The second...
For each test case, if it's impossible to make \(a\) equal to \(b\) in at most \(4n\) reversals, print a single integer \(-1\).Otherwise, print an integer \(m\) (\(0 \le m \le 4n\)), denoting the number of reversals in your sequence of steps, followed by \(m\) pairs of integers \(l_i, r_i\) (\(1 \le l_i \le r_i \le n\)...
In the first test case, after the first reversal \(a = \) 011010, after the second reversal \(a = \) 010110, after the third reversal \(a = \) 010101.
Input: 6 6 1 101010 010101 6 3 101010 010101 6 0 101010 010101 6 6 101010 010101 4 2 0000 1111 9 2 011100101 101001011 | Output: 3 1 2 3 4 5 6 1 1 6 -1 -1 -1 5 4 8 8 9 3 6 1 4 3 6
Master
1
784
548
574
15
1,438
F
1438F
F. Olha and Igor
3,000
interactive; probabilities; trees
This is an interactive problem.Igor wants to find the key to Olha's heart. The problem is, that it's at the root of a binary tree.There is a perfect binary tree of height \(h\) consisting of \(n = 2^{h} - 1\) nodes. The nodes have been assigned distinct labels from \(1\) to \(n\). However, Igor only knows \(h\) and doe...
The first and only line contains a single integer \(h\) (\(3 \le h \le 18\)) — the height of the tree.
The labels corresponding to the tree in the example are [\(4\),\(7\),\(2\),\(6\),\(1\),\(5\),\(3\)], meaning the root is labelled \(4\), and for \(1 < i \le n\), the parent of \(p_i\) is \(p_{ \lfloor{\frac{i}{2}}\rfloor }\).
Input: 3 2 7 4 | Output: ? 7 3 5 ? 1 6 4 ? 1 5 4 ! 4
Master
3
879
102
0
14
2,085
D
2085D
D. Serval and Kaitenzushi Buffet
2,000
data structures; graph matchings; greedy
Serval has just found a Kaitenzushi buffet restaurant. Kaitenzushi means that there is a conveyor belt in the restaurant, delivering plates of sushi in front of the customer, Serval.In this restaurant, each plate contains exactly \(k\) pieces of sushi and the \(i\)-th plate has a deliciousness \(d_i\). Serval will have...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1\leq k<n\leq 2\cdot 10^5\)) — the number of minutes of the meal and the number of s...
For each test case, print a single integer — the maximum possible sum of the deliciousnesses of all the plates Serval took.
In the first test case, it can be shown that Serval can eat up at most one plate of sushi. Since the second plate of sushi has the greatest deliciousness \(6\) among all the plates, he will take it from the belt in the second minute, and then eat it up in the following \(2\) minutes. Minute\(1\)\(2\)\(3\)\(4\)\(5\)Acti...
Input: 55 23 6 4 1 27 13 1 4 1 5 9 24 34 3 2 16 21 3 5 2 4 66 11000000000 1 1000000000 1 1000000000 1 | Output: 6 16 4 6 3000000000
Hard
3
1,178
562
123
20
1,713
F
1713F
F. Lost Array
2,900
bitmasks; combinatorics; constructive algorithms; dp; math
My orzlers, we can optimize this problem from \(O(S^3)\) to \(O\left(T^\frac{5}{9}\right)\)!— Spyofgame, founder of Orzlim religionA long time ago, Spyofgame invented the famous array \(a\) (\(1\)-indexed) of length \(n\) that contains information about the world and life. After that, he decided to convert it into the ...
The first line contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)).The second line contains \(n\) integers \(b_{1,n}, b_{2,n}, \ldots, b_{n,n}\) (\(0 \leq b_{i,n} < 2^{30}\)).
If some array \(a\) is consistent with the information, print a line containing \(n\) integers \(a_1, a_2, \ldots, a_n\). If there are multiple solutions, output any.If such an array does not exist, output \(-1\) instead.
If we let \(a = [1,2,3]\), then \(b\) will be: \(\bf{0}\)\(\bf{1}\)\(\bf{2}\)\(\bf{3}\)\(\bf{0}\)\(1\)\(3\)\(0\)\(\bf{0}\)\(1\)\(2\)\(2\)\(\bf{0}\)\(1\)\(3\)\(1\) The values of \(b_{1,n}, b_{2,n}, \ldots, b_{n,n}\) generated are \([0,2,1]\) which is consistent with what the archaeologists have discovered.
Input: 3 0 2 1 | Output: 1 2 3
Master
5
1,089
188
221
17
316
B2
316B2
B2. EKG
1,600
dfs and similar; dp
In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another.(Cultural note: standing in huge and disorganized queues for hours is a native tr...
The first line contains two integers n (1 ≤ n ≤ 103) and x (1 ≤ x ≤ n) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to n.The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n) — the number of the beaver fol...
Print all possible positions of the Smart Beaver in the line in the increasing order.
Picture for the fourth test.
Input: 6 12 0 4 0 6 0 | Output: 246
Medium
2
2,407
821
85
3
2,097
C
2097C
C. Bermuda Triangle
2,400
chinese remainder theorem; geometry; implementation; math; number theory
The Bermuda Triangle — a mysterious area in the Atlantic Ocean where, according to rumors, ships and airplanes disappear without a trace. Some blame magnetic anomalies, others — portals to other worlds, but the truth remains hidden in a fog of mysteries.A regular passenger flight 814 was traveling from Miami to Nassau ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The only line of each test case contains five integers \(n\), \(x\), \(y\), \(v_x\), and \(v_y\) (\(3 \le n \le 10^9\), \(1 \le x, y\), \(x+y < n\), \(1 \le v...
For each test case, output a single integer — the number of times the airplane will hit the boundary of the triangle before it escapes. If the airplane never escapes from the triangle, output \(-1\).
An illustration for the first test case is provided below: In the second test case, the answer is the same as in the first, as all initial data, except for the speed, are the same, but the airplanes are initially moving in the same direction.In the third test case, the answer is \(-1\), as the airplane will move exclus...
Input: 66 2 2 5 26 2 2 20 84 1 2 1 14 1 1 1 24 1 1 2 16 2 3 2 3 | Output: 2 2 -1 -1 -1 5
Expert
5
1,772
464
199
20
1,070
I
1070I
I. Privatization of Roads in Berland
2,400
flows; graph matchings; graphs
There are \(n\) cities and \(m\) two-way roads in Berland, each road connecting two distinct cities.Recently the Berland government has made a tough decision to transfer ownership of the roads to private companies. In total, there are \(100500\) private companies in Berland, numbered by integers from \(1\) to \(100500\...
Input contains one or several test cases. The first line contains an integer \(t\) (\(1 \le t \le 300\)) — the number of test cases in the input. Solve test cases separately, test cases are completely independent and do not affect each other.The following lines describe the test cases. Each case starts with a line cons...
Print \(t\) lines: the \(i\)-th line should contain the answer for the \(i\)-th test case. For a test case, print a sequence of integers \(c_1, c_2, \dots, c_m\) separated by space, where \(c_i\) (\(1 \le c_i \le 100500\)) is the company which owns the \(i\)-th road in your plan. If there are multiple solutions, output...
Input: 33 3 21 22 33 14 5 21 21 31 42 32 44 6 21 21 31 42 32 43 4 | Output: 1 2 3 2 1 1 2 3 0 0 0 0 0 0
Expert
3
854
966
406
10
1,702
G1
1702G1
G1. Passable Paths (easy version)
1,900
dfs and similar; trees
This is an easy version of the problem. The only difference between an easy and a hard version is in the number of queries.Polycarp grew a tree from \(n\) vertices. We remind you that a tree of \(n\) vertices is an undirected connected graph of \(n\) vertices and \(n-1\) edges that does not contain cycles.He calls a se...
The first line of input contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — number of vertices.Following \(n - 1\) lines a description of the tree..Each line contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) — indices of vertices connected by an edge.Following line contains single i...
Output \(q\) lines, each of which contains the answer to the corresponding query. As an answer, output ""YES"" if the set is passable, 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: 5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4 | Output: YES NO YES NO YES
Hard
2
987
788
299
17
978
B
978B
B. File Name
800
greedy; strings
You can not just take the file and send it. When Polycarp trying to send a file in the social network ""Codehorses"", he encountered an unexpected problem. If the name of the file contains three or more ""x"" (lowercase Latin letters ""x"") in a row, the system considers that the file content does not correspond to the...
The first line contains integer \(n\) \((3 \le n \le 100)\) — the length of the file name.The second line contains a string of length \(n\) consisting of lowercase Latin letters only — the file name.
Print the minimum number of characters to remove from the file name so after that the name does not contain ""xxx"" as a substring. If initially the file name dost not contain a forbidden substring ""xxx"", print 0.
In the first example Polycarp tried to send a file with name contains number \(33\), written in Roman numerals. But he can not just send the file, because it name contains three letters ""x"" in a row. To send the file he needs to remove any one of this letters.
Input: 6xxxiii | Output: 1
Beginner
2
916
199
215
9
348
B
348B
B. Apple Tree
2,100
dfs and similar; number theory; trees
You are given a rooted tree with n vertices. In each leaf vertex there's a single integer — the number of apples in this vertex. The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf.A tree is balan...
The first line contains integer n (2 ≤ n ≤ 105), showing the number of vertices in the tree. The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 108), ai is the number of apples in the vertex number i. The number of apples in non-leaf vertices is guaranteed to be zero. Then follow n - 1 lines, describing the tr...
Print a single integer — the minimum number of apples to remove in order to make the tree balanced.Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.
Input: 60 0 12 13 5 61 21 31 42 52 6 | Output: 6
Hard
3
661
497
258
3
1,251
D
1251D
D. Salary Changing
1,900
binary search; greedy; sortings
You are the head of a large enterprise. \(n\) people work at you, and \(n\) is odd (i. e. \(n\) is not divisible by \(2\)).You have to distribute salaries to your employees. Initially, you have \(s\) dollars for it, and the \(i\)-th employee should get a salary from \(l_i\) to \(r_i\) dollars. You have to distribute sa...
The first line contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) — the number of test cases.The first line of each query contains two integers \(n\) and \(s\) (\(1 \le n < 2 \cdot 10^5\), \(1 \le s \le 2 \cdot 10^{14}\)) — the number of employees and the amount of money you have. The value \(n\) is not divisibl...
For each test case print one integer — the maximum median salary that you can obtain.
In the first test case, you can distribute salaries as follows: \(sal_1 = 12, sal_2 = 2, sal_3 = 11\) (\(sal_i\) is the salary of the \(i\)-th employee). Then the median salary is \(11\).In the second test case, you have to pay \(1337\) dollars to the only employee.In the third test case, you can distribute salaries as...
Input: 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 | Output: 11 1337 6
Hard
3
848
733
85
12
839
C
839C
C. Journey
1,500
dfs and similar; dp; graphs; probabilities; trees
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. W...
The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road.It is guaranteed that one can reach any city from any other by the roads.
Print a number — the expected length of their journey. The journey starts in the city 1.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
Input: 41 21 32 4 | Output: 1.500000000000000
Medium
5
861
304
320
8
2,077
D
2077D
D. Maximum Polygon
3,100
brute force; data structures; greedy; implementation; math
Given an array \(a\) of length \(n\), determine the lexicographically largest\(^{\text{∗}}\) subsequence\(^{\text{†}}\) \(s\) of \(a\) such that \(s\) can be the side lengths of a polygon.Recall that \(s\) can be the side lengths of a polygon if and only if \(|s| \geq 3\) and$$$\( 2 \cdot \max(s_1, s_2, \ldots, s_{|s|}...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 2 \cdot 10^5\)) — the length of the array \(a\).The second line contains \(n...
For each test case, output the answer in the following format:If the answer exists, output the following.In the first line, output the integer \(k\) (\(1 \leq k \leq n\)) — the length of the subsequence \(s\).In the second line, output \(k\) integers \(s_1, s_2, \ldots, s_k\) (\(1 \leq s_i \leq 10^9\), \(s\) is a subse...
In the first test case, there are no subsequences that can be the side lengths of a polygon.In the second test case, there are \(2\) subsequences that can be the side lengths of a polygon: \(1, 4, 2, 3\) and \(4, 2, 3\). The latter is the lexicographically larger subsequence.
Input: 533 1 241 4 2 361 6 4 5 3 2643 12 99 53 22 479 764 54 73 22 23 1 | Output: -1 3 4 2 3 4 6 5 3 2 5 43 99 53 22 4 4 54 73 23 1
Master
5
870
517
475
20
276
B
276B
B. Little Girl and Game
1,300
games; greedy
The Little Girl loves problems on games very much. Here's one of them.Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules: The players move in turns; In one move the player can remove an arbitrary letter from string s. If the player before ...
The input contains a single line, containing string s (1 ≤ |s| ≤ 103). String s consists of lowercase English letters.
In a single line print word ""First"" if the first player wins (provided that both players play optimally well). Otherwise, print word ""Second"". Print the words without the quotes.
Input: aba | Output: First
Easy
2
706
118
182
2
1,778
E
1778E
E. The Tree Has Fallen!
2,500
bitmasks; dfs and similar; math; trees
Recently, a tree has fallen on Bob's head from the sky. The tree has \(n\) nodes. Each node \(u\) of the tree has an integer number \(a_u\) written on it. But the tree has no fixed root, as it has fallen from the sky.Bob is currently studying the tree. To add some twist, Alice proposes a game. First, Bob chooses some n...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line contains an integer \(n\) (\(2\leq n\leq 2 \cdot 10^5\)) — the number of nodes of the tree. The next line contains \(n\) space-separated integer...
For each test case, in each query, print a line containing an integer denoting the maximum score Bob can achieve.
In each of the below figures, the green-colored node is the node picked by Bob, and the red-colored node is the node picked by Alice. The values of the nodes are placed in the blue boxes beside the nodes.Consider the first example. In the first query, if we put the root node \(4\) at the top of the tree, the tree looks...
Input: 3612 12 8 25 6 11 51 22 62 32 434 23 51 223 81 242 22 11 21 133 8 71 22 322 22 1 | Output: 15 6 29 11 3 8 11 15 3
Expert
4
1,068
986
113
17
1,885
A
1885A
A. Deterministic Scheduling for Extended Reality over 5G and Beyond
0
*special
Background Extended reality (XR) service is a promising application for future communications. In wireless communications, XR data are transmitted over radio between base stations and mobile terminals. A region is usually divided into multiple cells, each of which is equipped with a base station to serve users. One bas...
The input of a single test has \((4 + R \cdot K \cdot T + N \cdot R \cdot K + 1 + J)\) lines, which contains user number \(N\), cell number \(K\), TTI number \(T\), RBG number \(R\), initial SINRs \(s_{0, r n t}^{(k)}\), interference factors \(d_{mrn}\), frame number \(J\) and information about \(J\) frames. The detail...
Output for a certain input is the optimization result of \(p_{r n t}^{(k)}\) (float), which has \(R \cdot K \cdot T\) lines. Each line has \(N\) elements, corresponding to \(N\) users. \(p_{r n t}^{(k)}\) is the \((n+1)\)-th element of line \((1+r+k \cdot R+t \cdot K \cdot R)\). Note that the optimization result of \(b...
Two sets of tests are prepared in this problem. For the duration of the competition, each submission is tested on the preliminary set of tests. When the competition is finished, for each contestant:The jury takes the latest submission with non-zero score on preliminary tests;This submission is tested on the final set o...
Input: 2 2 2 1 1.3865 11.3865 1.3865 11.3865 2.3865 2.3865 2.3865 2.3865 0 -2 -2 0 0 -2 -2 0 2 0 250 0 0 2 1 25 1 0 2 | Output: 0.000000 0.004950 0.000000 0.004950 0.245039 0.000000 0.245039 0.000000
Beginner
1
5,101
2,009
739
18
221
B
221B
B. Little Elephant and Numbers
1,300
implementation
The Little Elephant loves numbers. He has a positive integer x. The Little Elephant wants to find the number of positive integers d, such that d is the divisor of x, and x and d have at least one common (the same) digit in their decimal representations. Help the Little Elephant to find the described number.
A single line contains a single integer x (1 ≤ x ≤ 109).
In a single line print an integer — the answer to the problem.
Input: 1 | Output: 1
Easy
1
308
56
62
2
1,968
F
1968F
F. Equal XOR Segments
1,800
binary search; data structures
Let us call an array \(x_1,\dots,x_m\) interesting if it is possible to divide the array into \(k>1\) parts so that bitwise XOR of values from each part are equal. More formally, you must split array \(x\) into \(k\) consecutive segments, each element of \(x\) must belong to exactly \(1\) segment. Let \(y_1,\dots,y_k\)...
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 two integers \(n\) and \(q\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le q \le 2 \cdot 10^5\)) — the number of elements in the array and the number of queries respectively.The next line co...
For each query, output ""YES"" if the subarray is interesting and ""NO"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, the strings ""yES"", ""yes"", and ""Yes"" will be recognized as correct answers).
Explanation for the first test case:The first query is described in the statement.In the second query, we should divide \([1,2,3]\). A possible division is \([1,2],[3]\), since \(1\oplus 2=3\).It can be shown that for queries \(3,4,5\), the subarrays are not interesting.
Input: 45 51 1 2 3 01 52 43 51 33 45 51 2 3 4 51 52 43 51 32 37 412 9 10 9 10 11 91 51 72 62 711 40 0 1 0 0 1 0 1 1 0 11 22 56 97 11 | Output: YES YES NO NO NO YES NO NO YES NO NO NO NO NO YES NO YES YES
Medium
2
839
703
224
19
1,515
H
1515H
H. Phoenix and Bits
3,500
bitmasks; brute force; data structures; sortings
Phoenix loves playing with bits — specifically, by using the bitwise operations AND, OR, and XOR. He has \(n\) integers \(a_1, a_2, \dots, a_n\), and will perform \(q\) of the following queries: replace all numbers \(a_i\) where \(l \le a_i \le r\) with \(a_i\) AND \(x\); replace all numbers \(a_i\) where \(l \le a_i \...
The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le q \le 10^5\)) — the number of integers and the number of queries, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < 2^{20}\)) — the integers that Phoenix starts with.The next \(q\) line...
Print the answer for each query where \(t=4\).
In the first example: For the first query, \(2\) is replaced by \(2\) AND \(2 = 2\) and \(3\) is replaced with \(3\) AND \(2 = 2\). The set of numbers is \(\{1, 2, 4, 5\}\). For the second query, there are \(3\) distinct numbers between \(2\) and \(5\): \(2\), \(4\), and \(5\). For the third query, \(2\) is replaced by...
Input: 5 6 5 4 3 2 1 1 2 3 2 4 2 5 3 2 5 3 4 1 6 2 1 1 8 4 8 10 | Output: 3 2 1
Master
4
628
744
46
15
2,022
A
2022A
A. Bus to Pénjamo
800
constructive algorithms; greedy; implementation; math
Ya vamos llegando a Péeeenjamoo ♫♫♫ There are \(n\) families travelling to Pénjamo to witness Mexico's largest-ever ""walking a chicken on a leash"" marathon. The \(i\)-th family has \(a_i\) family members. All families will travel using a single bus consisting of \(r\) rows with \(2\) seats each.A person is considered...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(r\) (\(1 \le n \le 100\); \(1 \le r \le 500\)) — the number of families and the number of ro...
For each test case, output the maximum number of happy people in an optimal seating arrangement.
In the first test case, the two members of the first family can sit together in the first row, while the two members of the second family can sit together in the second row. The remaining member of the second family can sit in the third row along with a member of the third family. This seating arrangement is shown belo...
Input: 43 32 3 13 32 2 24 51 1 2 24 53 1 1 3 | Output: 4 6 6 6
Beginner
4
723
469
96
20
1,316
F
1316F
F. Battalion Strength
2,800
data structures; divide and conquer; probabilities
There are \(n\) officers in the Army of Byteland. Each officer has some power associated with him. The power of the \(i\)-th officer is denoted by \(p_{i}\). As the war is fast approaching, the General would like to know the strength of the army.The strength of an army is calculated in a strange way in Byteland. The Ge...
The first line of the input contains a single integer \(n\) (\(1 \leq n \leq 3⋅10^{5}\)) — the number of officers in Byteland's Army.The second line contains \(n\) integers \(p_{1},p_{2},\ldots,p_{n}\) (\(1 \leq p_{i} \leq 10^{9}\)).The third line contains a single integer \(q\) (\(1 \leq q \leq 3⋅10^{5}\)) — the numbe...
In the first line output the initial strength of the army.In \(i\)-th of the next \(q\) lines, output the strength of the army after \(i\)-th update.
In first testcase, initially, there are four possible battalions {} Strength = \(0\) {\(1\)} Strength = \(0\) {\(2\)} Strength = \(0\) {\(1,2\)} Strength = \(2\) So strength of army is \(\frac{0+0+0+2}{4}\) = \(\frac{1}{2}\)After changing \(p_{1}\) to \(2\), strength of battallion {\(1,2\)} changes to \(4\), so strengt...
Input: 2 1 2 2 1 2 2 1 | Output: 500000004 1 500000004
Master
3
1,682
497
149
13
485
A
485A
A. Factory
1,400
implementation; math; matrices
One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce (remainder after dividing x by m) more details. Unfortunately, no customer ...
The first line contains two integers a and m (1 ≤ a, m ≤ 105).
Print ""Yes"" (without quotes) if the production will eventually stop, otherwise print ""No"".
Input: 1 5 | Output: No
Easy
3
714
62
94
4